verbose.pas 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. This unit handles the verbose management
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit verbose;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. {$IFNDEF USE_FAKE_SYSUTILS}
  22. sysutils,
  23. {$ELSE}
  24. fksysutl,
  25. {$ENDIF}
  26. cutils,
  27. globtype,finput,
  28. cmsgs;
  29. {$ifndef EXTERN_MSG}
  30. {$i msgtxt.inc}
  31. {$endif}
  32. {$i msgidx.inc}
  33. var
  34. msg : pmessage;
  35. type
  36. tmsgqueueevent = procedure(const s:TMsgStr;v,w:longint) of object;
  37. const
  38. msgfilename : string = '';
  39. procedure SetRedirectFile(const fn:string);
  40. function SetVerbosity(const s:TCmdStr):boolean;
  41. procedure PrepareReport;
  42. function CheckVerbosity(v:longint):boolean;
  43. function SetMessageVerbosity(v:longint;state:tmsgstate):boolean;
  44. procedure RestoreLocalVerbosity(pstate : pmessagestaterecord);
  45. procedure FreeLocalVerbosity(var fstate : pmessagestaterecord);
  46. function ChangeMessageVerbosity(s: ansistring; var i: integer;state:tmsgstate): boolean;
  47. procedure ShowStatus;
  48. function ErrorCount:longint;
  49. procedure SetErrorFlags(const s:string);
  50. procedure GenerateError;
  51. procedure Internalerror(i:longint);noreturn;
  52. procedure Internalerror(i:longint; const s : ansistring);noreturn;
  53. procedure Comment(l:longint;s:ansistring);
  54. function MessageStr(w:longint):TMsgStr;
  55. procedure Message(w:longint;onqueue:tmsgqueueevent=nil);
  56. procedure Message1(w:longint;const s1:TMsgStr;onqueue:tmsgqueueevent=nil);
  57. procedure Message2(w:longint;const s1,s2:TMsgStr;onqueue:tmsgqueueevent=nil);
  58. procedure Message3(w:longint;const s1,s2,s3:TMsgStr;onqueue:tmsgqueueevent=nil);
  59. procedure Message4(w:longint;const s1,s2,s3,s4:TMsgStr;onqueue:tmsgqueueevent=nil);
  60. procedure MessagePos(const pos:tfileposinfo;w:longint;onqueue:tmsgqueueevent=nil);
  61. procedure MessagePos1(const pos:tfileposinfo;w:longint;const s1:TMsgStr;onqueue:tmsgqueueevent=nil);
  62. procedure MessagePos2(const pos:tfileposinfo;w:longint;const s1,s2:TMsgStr;onqueue:tmsgqueueevent=nil);
  63. procedure MessagePos3(const pos:tfileposinfo;w:longint;const s1,s2,s3:TMsgStr;onqueue:tmsgqueueevent=nil);
  64. procedure MessagePos4(const pos:tfileposinfo;w:longint;const s1,s2,s3,s4:TMsgStr;onqueue:tmsgqueueevent=nil);
  65. { message calls with codegenerror support }
  66. procedure cgmessage(t : longint);
  67. procedure cgmessage1(t : longint;const s : TMsgStr);
  68. procedure cgmessage2(t : longint;const s1,s2 : TMsgStr);
  69. procedure cgmessage3(t : longint;const s1,s2,s3 : TMsgStr);
  70. procedure CGMessagePos(const pos:tfileposinfo;t:longint);
  71. procedure CGMessagePos1(const pos:tfileposinfo;t:longint;const s1:TMsgStr);
  72. procedure CGMessagePos2(const pos:tfileposinfo;t:longint;const s1,s2:TMsgStr);
  73. procedure CGMessagePos3(const pos:tfileposinfo;t:longint;const s1,s2,s3:TMsgStr);
  74. procedure FlushOutput;
  75. procedure InitVerbose;
  76. procedure DoneVerbose;
  77. const
  78. printnodespacing = ' ';
  79. var
  80. { indention used when writing a node tree to the screen }
  81. printnodeindention : string;
  82. { Node dumping support functions }
  83. procedure printnodeindent; inline;
  84. procedure printnodeunindent; inline;
  85. {$ifdef DEBUG_NODE_XML}
  86. function SanitiseXMLString(const S: ansistring): ansistring;
  87. function WritePointer(const P: Pointer): ansistring;
  88. function WriteConstPUInt(const P: TConstPtrUInt): ansistring;
  89. function WriteGUID(const GUID: TGUID): ansistring;
  90. {$endif DEBUG_NODE_XML}
  91. implementation
  92. uses
  93. comphook,fmodule,constexp,globals,cfileutl,switches,cclasses;
  94. {****************************************************************************
  95. Extra Handlers for default compiler
  96. ****************************************************************************}
  97. procedure DoneRedirectFile;
  98. begin
  99. if status.use_redir then
  100. begin
  101. close(status.redirfile);
  102. status.use_redir:=false;
  103. end;
  104. if status.use_bugreport then
  105. begin
  106. close(status.reportbugfile);
  107. status.use_bugreport:=false;
  108. end;
  109. end;
  110. procedure SetRedirectFile(const fn:string);
  111. begin
  112. { close old redirection file because FileRedirection is handled in both passes }
  113. if status.use_redir then
  114. close(status.redirfile);
  115. assign(status.redirfile,fn);
  116. {$push}{$I-}
  117. append(status.redirfile);
  118. if ioresult <> 0 then
  119. begin
  120. assign(status.redirfile,fn);
  121. rewrite(status.redirfile);
  122. end;
  123. {$pop}
  124. status.use_redir:=(ioresult=0);
  125. end;
  126. procedure PrepareReport;
  127. var
  128. fn : string;
  129. begin
  130. if status.use_bugreport then
  131. exit;
  132. fn:='fpcdebug.txt';
  133. assign(status.reportbugfile,fn);
  134. {$push}{$I-}
  135. append(status.reportbugfile);
  136. if ioresult <> 0 then
  137. rewrite(status.reportbugfile);
  138. {$pop}
  139. status.use_bugreport:=(ioresult=0);
  140. if status.use_bugreport then
  141. writeln(status.reportbugfile,'FPC bug report file');
  142. end;
  143. procedure RestoreLocalVerbosity(pstate : pmessagestaterecord);
  144. var
  145. msgset : thashset;
  146. msgfound : boolean;
  147. begin
  148. msg^.ResetStates;
  149. msgset:=thashset.create(10,false,false);
  150. while assigned(pstate) do
  151. begin
  152. msgfound:=false;
  153. { only apply the newest message state }
  154. if not assigned(msgset.findoradd(@pstate^.value,sizeof(pstate^.value),msgfound)) or
  155. not msgfound then
  156. SetMessageVerbosity(pstate^.value,pstate^.state);
  157. pstate:=pstate^.next;
  158. end;
  159. msgset.free;
  160. msgset := nil;
  161. end;
  162. procedure FreeLocalVerbosity(var fstate : pmessagestaterecord);
  163. var pstate : pmessagestaterecord;
  164. begin
  165. pstate:=unaligned(fstate);
  166. while assigned(pstate) do
  167. begin
  168. unaligned(fstate):=pstate^.next;
  169. dispose(pstate);
  170. pstate:=unaligned(fstate);
  171. end;
  172. end;
  173. function ChangeMessageVerbosity(s: ansistring; var i : integer;state:tmsgstate): boolean;
  174. var
  175. tok : ansistring;
  176. msgnr, code : longint;
  177. begin
  178. { delete everything up to and including 'm' }
  179. delete(s,1,i);
  180. { the rest of the string must be message numbers }
  181. inc(i,length(s)+1);
  182. result:=false;
  183. repeat
  184. tok:=GetToken(s,',');
  185. if (tok='') then
  186. break;
  187. val(tok, msgnr, code);
  188. if (code<>0) then
  189. exit;
  190. if not msg^.valid(msgnr) then
  191. exit
  192. else
  193. recordpendingmessagestate(msgnr, state);
  194. until false;
  195. result:=true;
  196. end;
  197. function SetMessageVerbosity(v:longint;state:tmsgstate):boolean;
  198. begin
  199. result:=msg^.setverbosity(v,state);
  200. end;
  201. function CheckVerbosity(v:longint):boolean;
  202. begin
  203. result:=do_checkverbosity(v);
  204. end;
  205. function SetVerbosity(const s:TCmdStr):boolean;
  206. const
  207. message_verbosity:array[boolean] of tmsgstate=(ms_off_global,ms_on_global);
  208. var
  209. m : Longint;
  210. i : Integer;
  211. inverse : boolean;
  212. c : char;
  213. begin
  214. Setverbosity:=false;
  215. val(s,m,i);
  216. if (i=0) and (s<>'') then
  217. status.verbosity:=m
  218. else
  219. begin
  220. i:=1;
  221. while i<=length(s) do
  222. begin
  223. c:=upcase(s[i]);
  224. inverse:=false;
  225. { on/off ? }
  226. if (i<length(s)) then
  227. case s[i+1] of
  228. '-' : begin
  229. inc(i);
  230. inverse:=true;
  231. end;
  232. '+' : inc(i);
  233. end;
  234. { handle switch }
  235. case c of
  236. { Special cases }
  237. '0' : status.verbosity:=V_Default;
  238. 'A' : status.verbosity:=V_All;
  239. 'B' : begin
  240. if inverse then
  241. status.print_source_path:=false
  242. else
  243. status.print_source_path:=true;
  244. end;
  245. 'M' : if not ChangeMessageVerbosity(s,i,message_verbosity[inverse]) then
  246. begin
  247. result:=false;
  248. exit
  249. end;
  250. 'P' : begin
  251. if inverse then
  252. paraprintnodetree:=0
  253. else
  254. paraprintnodetree:=1;
  255. end;
  256. 'Q' : begin
  257. if inverse then
  258. status.showmsgnrs:=false
  259. else
  260. status.showmsgnrs:=true;
  261. end;
  262. 'R' : begin
  263. if inverse then
  264. begin
  265. status.use_gccoutput:=false;
  266. status.use_stderr:=false;
  267. end
  268. else
  269. begin
  270. status.use_gccoutput:=true;
  271. status.use_stderr:=true;
  272. end;
  273. end;
  274. 'V' : PrepareReport;
  275. 'Z' : begin
  276. if inverse then
  277. status.use_stderr:=false
  278. else
  279. status.use_stderr:=true;
  280. end;
  281. { Normal cases - do an or }
  282. 'C' : if inverse then
  283. status.verbosity:=status.verbosity and (not V_Conditional)
  284. else
  285. status.verbosity:=status.verbosity or V_Conditional;
  286. 'D' : if inverse then
  287. status.verbosity:=status.verbosity and (not V_Debug)
  288. else
  289. status.verbosity:=status.verbosity or V_Debug;
  290. 'E' : if inverse then
  291. status.verbosity:=status.verbosity and (not V_Error)
  292. else
  293. status.verbosity:=status.verbosity or V_Error;
  294. 'H' : if inverse then
  295. status.verbosity:=status.verbosity and (not V_Hint)
  296. else
  297. status.verbosity:=status.verbosity or V_Hint;
  298. 'I' : if inverse then
  299. status.verbosity:=status.verbosity and (not V_Info)
  300. else
  301. status.verbosity:=status.verbosity or V_Info;
  302. 'J' : if inverse then
  303. status.verbosity:=status.verbosity and (not V_Parallel)
  304. else
  305. status.verbosity:=status.verbosity or V_Parallel;
  306. 'L' : if inverse then
  307. status.verbosity:=status.verbosity and (not V_Status)
  308. else
  309. status.verbosity:=status.verbosity or V_Status;
  310. 'N' : if inverse then
  311. status.verbosity:=status.verbosity and (not V_Note)
  312. else
  313. status.verbosity:=status.verbosity or V_Note;
  314. 'S' : if inverse then
  315. status.verbosity:=status.verbosity and (not V_TimeStamps)
  316. else
  317. status.verbosity:=status.verbosity or V_TimeStamps;
  318. 'T' : if inverse then
  319. status.verbosity:=status.verbosity and (not V_Tried)
  320. else
  321. status.verbosity:=status.verbosity or V_Tried;
  322. 'U' : if inverse then
  323. status.verbosity:=status.verbosity and (not V_Used)
  324. else
  325. status.verbosity:=status.verbosity or V_Used;
  326. 'W' : if inverse then
  327. status.verbosity:=status.verbosity and (not V_Warning)
  328. else
  329. status.verbosity:=status.verbosity or V_Warning;
  330. 'X' : if inverse then
  331. status.verbosity:=status.verbosity and (not V_Executable)
  332. else
  333. status.verbosity:=status.verbosity or V_Executable;
  334. end;
  335. inc(i);
  336. end;
  337. end;
  338. if status.verbosity=0 then
  339. status.verbosity:=V_Default;
  340. setverbosity:=true;
  341. end;
  342. procedure Loadprefixes;
  343. function loadprefix(w:longint):string;
  344. var
  345. s : string;
  346. idx : longint;
  347. begin
  348. s:=msg^.get(w,[]);
  349. idx:=pos('_',s);
  350. if idx>0 then
  351. Loadprefix:=Copy(s,idx+1,255)
  352. else
  353. Loadprefix:=s;
  354. end;
  355. begin
  356. { Load the prefixes }
  357. fatalstr:=Loadprefix(general_i_fatal);
  358. errorstr:=Loadprefix(general_i_error);
  359. warningstr:=Loadprefix(general_i_warning);
  360. notestr:=Loadprefix(general_i_note);
  361. hintstr:=Loadprefix(general_i_hint);
  362. end;
  363. procedure LoadMsgFile(const fn:string);
  364. begin
  365. { reload the internal messages if not already loaded }
  366. {$ifndef EXTERN_MSG}
  367. if not msg^.msgintern then
  368. msg^.LoadIntern(@msgtxt,msgtxtsize,msgtxt_codepage);
  369. {$endif}
  370. if not msg^.LoadExtern(fn) then
  371. begin
  372. {$ifdef EXTERN_MSG}
  373. writeln('Fatal: Cannot find error message file.');
  374. halt(3);
  375. {$else}
  376. msg^.LoadIntern(@msgtxt,msgtxtsize,msgtxt_codepage);
  377. {$endif}
  378. end;
  379. { reload the prefixes using the new messages }
  380. Loadprefixes;
  381. end;
  382. procedure MaybeLoadMessageFile;
  383. begin
  384. { Load new message file }
  385. if (msgfilename<>'') then
  386. begin
  387. LoadMsgFile(msgfilename);
  388. msgfilename:='';
  389. end;
  390. end;
  391. var
  392. lastfileidx,
  393. lastmoduleidx : longint;
  394. Procedure UpdateStatus;
  395. var
  396. module : tmodule;
  397. begin
  398. { fix status }
  399. status.currentline:=current_filepos.line;
  400. status.currentcolumn:=current_filepos.column;
  401. if (current_filepos.moduleindex <> lastmoduleidx) or
  402. (current_filepos.fileindex <> lastfileidx) then
  403. begin
  404. module:=get_module(current_filepos.moduleindex);
  405. if assigned(module) and assigned(module.sourcefiles) then
  406. begin
  407. { update status record }
  408. status.currentmodule:=module.modulename^;
  409. status.currentsourceppufilename:=module.ppufilename;
  410. status.currentmodulestate:=ModuleStateStr[module.state];
  411. status.currentsource:=module.sourcefiles.get_file_name(current_filepos.fileindex);
  412. status.currentsourcepath:=module.sourcefiles.get_file_path(current_filepos.fileindex);
  413. status.sources_avail:=module.sources_avail;
  414. { if currentsourcepath is relative, make it absolute }
  415. if not path_absolute(status.currentsourcepath) then
  416. status.currentsourcepath:=GetCurrentDir+status.currentsourcepath;
  417. { update lastfileidx only if name known PM }
  418. if status.currentsource<>'' then
  419. lastfileidx:=current_filepos.fileindex
  420. else
  421. lastfileidx:=0;
  422. lastmoduleidx:=module.unit_index;
  423. end;
  424. end;
  425. end;
  426. procedure ShowStatus;
  427. begin
  428. UpdateStatus;
  429. if do_status() then
  430. raise ECompilerAbort.Create;
  431. end;
  432. function ErrorCount:longint;
  433. begin
  434. ErrorCount:=status.errorcount;
  435. end;
  436. procedure SetErrorFlags(const s:string);
  437. var
  438. code : integer;
  439. i,j,l : longint;
  440. begin
  441. { empty string means error count = 1 for backward compatibility (PFV) }
  442. if s='' then
  443. begin
  444. status.maxerrorcount:=1;
  445. exit;
  446. end;
  447. i:=0;
  448. while (i<length(s)) do
  449. begin
  450. inc(i);
  451. case s[i] of
  452. '0'..'9' :
  453. begin
  454. j:=i;
  455. while (j<=length(s)) and (s[j] in ['0'..'9']) do
  456. inc(j);
  457. val(copy(s,i,j-i),l,code);
  458. if code<>0 then
  459. l:=1;
  460. status.maxerrorcount:=l;
  461. i:=j-1;
  462. end;
  463. 'w','W' :
  464. begin
  465. if (i<length(s)) and (s[i+1]='-') then
  466. begin
  467. inc(i);
  468. status.errorwarning:=false;
  469. end
  470. else
  471. begin
  472. status.errorwarning:=true;
  473. { Enable writing of warnings, to avoid getting errors without any message }
  474. status.verbosity:=status.verbosity or V_Warning;
  475. end;
  476. end;
  477. 'n','N' :
  478. begin
  479. if (i<length(s)) and (s[i+1]='-') then
  480. begin
  481. inc(i);
  482. status.errornote:=false;
  483. end
  484. else
  485. begin
  486. status.errornote:=true;
  487. { Enable writing of notes, to avoid getting errors without any message }
  488. status.verbosity:=status.verbosity or V_Note;
  489. end;
  490. end;
  491. 'h','H' :
  492. begin
  493. if (i<length(s)) and (s[i+1]='-') then
  494. begin
  495. inc(i);
  496. status.errorhint:=false;
  497. end
  498. else
  499. begin
  500. status.errorhint:=true;
  501. { Enable writing of hints, to avoid getting errors without any message }
  502. status.verbosity:=status.verbosity or V_Hint;
  503. end;
  504. end;
  505. end;
  506. end;
  507. end;
  508. procedure GenerateError;
  509. begin
  510. inc(status.errorcount);
  511. end;
  512. procedure internalerror(i : longint);noreturn;
  513. begin
  514. InternalError(i,'');
  515. end;
  516. procedure InternalError(i:longint; const s : ansistring);noreturn;
  517. procedure doraise;
  518. begin
  519. raise ECompilerAbort.Create;
  520. end;
  521. begin
  522. UpdateStatus;
  523. do_internalerrorex(i,s);
  524. GenerateError;
  525. doraise;
  526. end;
  527. procedure Comment(l:longint;s:ansistring);
  528. var
  529. dostop : boolean;
  530. begin
  531. dostop:=((l and V_Fatal)<>0);
  532. if ((l and V_Error)<>0) or
  533. ((l and V_Fatal)<>0) or
  534. (status.errorwarning and ((l and V_Warning)<>0)) or
  535. (status.errornote and ((l and V_Note)<>0)) or
  536. (status.errorhint and ((l and V_Hint)<>0)) then
  537. GenerateError
  538. else
  539. if l and V_Warning <> 0 then
  540. inc(status.countWarnings)
  541. else
  542. if l and V_Note <> 0 then
  543. inc(status.countNotes)
  544. else
  545. if l and V_Hint <> 0 then
  546. inc(status.countHints);
  547. { check verbosity level }
  548. if not CheckVerbosity(l) then
  549. exit;
  550. if (l and V_LineInfoMask)<>0 then
  551. l:=l or V_LineInfo;
  552. { Create status info }
  553. UpdateStatus;
  554. { Fix replacements }
  555. DefaultReplacements(s,false);
  556. { show comment }
  557. if do_comment(l,s) or dostop then
  558. raise ECompilerAbort.Create;
  559. if (status.errorcount>=status.maxerrorcount) and not status.skip_error then
  560. begin
  561. Message1(unit_f_errors_in_unit,tostr(status.errorcount));
  562. status.skip_error:=true;
  563. raise ECompilerAbort.Create;
  564. end;
  565. end;
  566. function GetMessageState(m:longint):tmsgstate;
  567. var
  568. i: integer;
  569. begin
  570. i:=m div 1000;
  571. { get the default state }
  572. Result:=msg^.msgstates[i][m mod 1000];
  573. { and search at the current unit settings }
  574. { todo }
  575. end;
  576. Procedure Msg2Comment(s:ansistring;w:longint;onqueue:tmsgqueueevent);
  577. var
  578. idx,i,v : longint;
  579. dostop : boolean;
  580. doqueue : boolean;
  581. st : tmsgstate;
  582. ch : char;
  583. begin
  584. {Reset}
  585. dostop:=false;
  586. doqueue:=false;
  587. v:=0;
  588. {Parse options}
  589. idx:=pos('_',s);
  590. if idx=0 then
  591. v:=V_None
  592. else
  593. if (idx >= 1) And (idx <= 5) then
  594. begin
  595. for i:=1 to idx do
  596. begin
  597. ch:=upcase(s[i]);
  598. case ch of
  599. 'F' :
  600. begin
  601. v:=v or V_Fatal;
  602. GenerateError;
  603. dostop:=true;
  604. end;
  605. 'E','W','N','H':
  606. begin
  607. if ch='E' then
  608. st:=ms_error
  609. else
  610. st:=GetMessageState(w);
  611. { We only want to know about local value }
  612. st:= tmsgstate(ord(st) and ms_local_mask);
  613. if st=ms_error then
  614. begin
  615. v:=v or V_Error;
  616. GenerateError;
  617. end
  618. else if st<>ms_off then
  619. case ch of
  620. 'W':
  621. begin
  622. v:=v or V_Warning;
  623. if CheckVerbosity(V_Warning) then
  624. if status.errorwarning then
  625. GenerateError
  626. else
  627. inc(status.countWarnings);
  628. end;
  629. 'N' :
  630. begin
  631. v:=v or V_Note;
  632. if CheckVerbosity(V_Note) then
  633. if status.errornote then
  634. GenerateError
  635. else
  636. inc(status.countNotes);
  637. end;
  638. 'H' :
  639. begin
  640. v:=v or V_Hint;
  641. if CheckVerbosity(V_Hint) then
  642. if status.errorhint then
  643. GenerateError
  644. else
  645. inc(status.countHints);
  646. end;
  647. end;
  648. end;
  649. 'O' :
  650. v:=v or V_Normal;
  651. 'I' :
  652. v:=v or V_Info;
  653. 'L' :
  654. v:=v or V_LineInfo;
  655. 'U' :
  656. v:=v or V_Used;
  657. 'T' :
  658. v:=v or V_Tried;
  659. 'C' :
  660. v:=v or V_Conditional;
  661. 'D' :
  662. v:=v or V_Debug;
  663. 'X' :
  664. v:=v or V_Executable;
  665. 'S' :
  666. dostop:=true;
  667. '_' : ;
  668. end;
  669. end;
  670. end;
  671. Delete(s,1,idx);
  672. { check verbosity level }
  673. if not CheckVerbosity(v) then
  674. begin
  675. doqueue := onqueue <> nil;
  676. if not doqueue then
  677. exit;
  678. end;
  679. if (v and V_LineInfoMask)<>0 then
  680. v:=v or V_LineInfo;
  681. { fix status }
  682. UpdateStatus;
  683. { Fix replacements }
  684. DefaultReplacements(s,false);
  685. if status.showmsgnrs and ((v and V_Normal)=0) then
  686. s:='('+tostr(w)+') '+s;
  687. if doqueue then
  688. begin
  689. onqueue(s,v,w);
  690. exit;
  691. end;
  692. { show comment }
  693. if do_comment(v,s) or dostop then
  694. raise ECompilerAbort.Create;
  695. if (status.errorcount>=status.maxerrorcount) and not status.skip_error then
  696. begin
  697. Message1(unit_f_errors_in_unit,tostr(status.errorcount));
  698. status.skip_error:=true;
  699. raise ECompilerAbort.Create;
  700. end;
  701. end;
  702. function MessageStr(w:longint):TMsgStr;
  703. begin
  704. MaybeLoadMessageFile;
  705. MessageStr:=msg^.Get(w,[]);
  706. end;
  707. procedure Message(w:longint;onqueue:tmsgqueueevent=nil);
  708. begin
  709. MaybeLoadMessageFile;
  710. Msg2Comment(msg^.Get(w,[]),w,onqueue);
  711. end;
  712. procedure Message1(w:longint;const s1:TMsgStr;onqueue:tmsgqueueevent=nil);
  713. begin
  714. MaybeLoadMessageFile;
  715. Msg2Comment(msg^.Get(w,[s1]),w,onqueue);
  716. end;
  717. procedure Message2(w:longint;const s1,s2:TMsgStr;onqueue:tmsgqueueevent=nil);
  718. begin
  719. MaybeLoadMessageFile;
  720. Msg2Comment(msg^.Get(w,[s1,s2]),w,onqueue);
  721. end;
  722. procedure Message3(w:longint;const s1,s2,s3:TMsgStr;onqueue:tmsgqueueevent=nil);
  723. begin
  724. MaybeLoadMessageFile;
  725. Msg2Comment(msg^.Get(w,[s1,s2,s3]),w,onqueue);
  726. end;
  727. procedure Message4(w:longint;const s1,s2,s3,s4:TMsgStr;onqueue:tmsgqueueevent=nil);
  728. begin
  729. MaybeLoadMessageFile;
  730. Msg2Comment(msg^.Get(w,[s1,s2,s3,s4]),w,onqueue);
  731. end;
  732. procedure MessagePos(const pos:tfileposinfo;w:longint;onqueue:tmsgqueueevent=nil);
  733. var
  734. oldpos : tfileposinfo;
  735. begin
  736. oldpos:=current_filepos;
  737. current_filepos:=pos;
  738. MaybeLoadMessageFile;
  739. Msg2Comment(msg^.Get(w,[]),w,onqueue);
  740. current_filepos:=oldpos;
  741. end;
  742. procedure MessagePos1(const pos:tfileposinfo;w:longint;const s1:TMsgStr;onqueue:tmsgqueueevent=nil);
  743. var
  744. oldpos : tfileposinfo;
  745. begin
  746. oldpos:=current_filepos;
  747. current_filepos:=pos;
  748. MaybeLoadMessageFile;
  749. Msg2Comment(msg^.Get(w,[s1]),w,onqueue);
  750. current_filepos:=oldpos;
  751. end;
  752. procedure MessagePos2(const pos:tfileposinfo;w:longint;const s1,s2:TMsgStr;onqueue:tmsgqueueevent=nil);
  753. var
  754. oldpos : tfileposinfo;
  755. begin
  756. oldpos:=current_filepos;
  757. current_filepos:=pos;
  758. MaybeLoadMessageFile;
  759. Msg2Comment(msg^.Get(w,[s1,s2]),w,onqueue);
  760. current_filepos:=oldpos;
  761. end;
  762. procedure MessagePos3(const pos:tfileposinfo;w:longint;const s1,s2,s3:TMsgStr;onqueue:tmsgqueueevent=nil);
  763. var
  764. oldpos : tfileposinfo;
  765. begin
  766. oldpos:=current_filepos;
  767. current_filepos:=pos;
  768. MaybeLoadMessageFile;
  769. Msg2Comment(msg^.Get(w,[s1,s2,s3]),w,onqueue);
  770. current_filepos:=oldpos;
  771. end;
  772. procedure MessagePos4(const pos:tfileposinfo;w:longint;const s1,s2,s3,s4:TMsgStr;onqueue:tmsgqueueevent=nil);
  773. var
  774. oldpos : tfileposinfo;
  775. begin
  776. oldpos:=current_filepos;
  777. current_filepos:=pos;
  778. MaybeLoadMessageFile;
  779. Msg2Comment(msg^.Get(w,[s1,s2,s3,s4]),w,onqueue);
  780. current_filepos:=oldpos;
  781. end;
  782. {*****************************************************************************
  783. override the message calls to set codegenerror
  784. *****************************************************************************}
  785. procedure cgmessage(t : longint);
  786. var
  787. olderrorcount : longint;
  788. begin
  789. if not(codegenerror) then
  790. begin
  791. olderrorcount:=Errorcount;
  792. verbose.Message(t);
  793. codegenerror:=olderrorcount<>Errorcount;
  794. end;
  795. end;
  796. procedure cgmessage1(t : longint;const s : TMsgStr);
  797. var
  798. olderrorcount : longint;
  799. begin
  800. if not(codegenerror) then
  801. begin
  802. olderrorcount:=Errorcount;
  803. verbose.Message1(t,s);
  804. codegenerror:=olderrorcount<>Errorcount;
  805. end;
  806. end;
  807. procedure cgmessage2(t : longint;const s1,s2 : TMsgStr);
  808. var
  809. olderrorcount : longint;
  810. begin
  811. if not(codegenerror) then
  812. begin
  813. olderrorcount:=Errorcount;
  814. verbose.Message2(t,s1,s2);
  815. codegenerror:=olderrorcount<>Errorcount;
  816. end;
  817. end;
  818. procedure cgmessage3(t : longint;const s1,s2,s3 : TMsgStr);
  819. var
  820. olderrorcount : longint;
  821. begin
  822. if not(codegenerror) then
  823. begin
  824. olderrorcount:=Errorcount;
  825. verbose.Message3(t,s1,s2,s3);
  826. codegenerror:=olderrorcount<>Errorcount;
  827. end;
  828. end;
  829. procedure cgmessagepos(const pos:tfileposinfo;t : longint);
  830. var
  831. olderrorcount : longint;
  832. begin
  833. if not(codegenerror) then
  834. begin
  835. olderrorcount:=Errorcount;
  836. verbose.MessagePos(pos,t);
  837. codegenerror:=olderrorcount<>Errorcount;
  838. end;
  839. end;
  840. procedure cgmessagepos1(const pos:tfileposinfo;t : longint;const s1 : TMsgStr);
  841. var
  842. olderrorcount : longint;
  843. begin
  844. if not(codegenerror) then
  845. begin
  846. olderrorcount:=Errorcount;
  847. verbose.MessagePos1(pos,t,s1);
  848. codegenerror:=olderrorcount<>Errorcount;
  849. end;
  850. end;
  851. procedure cgmessagepos2(const pos:tfileposinfo;t : longint;const s1,s2 : TMsgStr);
  852. var
  853. olderrorcount : longint;
  854. begin
  855. if not(codegenerror) then
  856. begin
  857. olderrorcount:=Errorcount;
  858. verbose.MessagePos2(pos,t,s1,s2);
  859. codegenerror:=olderrorcount<>Errorcount;
  860. end;
  861. end;
  862. procedure cgmessagepos3(const pos:tfileposinfo;t : longint;const s1,s2,s3 : TMsgStr);
  863. var
  864. olderrorcount : longint;
  865. begin
  866. if not(codegenerror) then
  867. begin
  868. olderrorcount:=Errorcount;
  869. verbose.MessagePos3(pos,t,s1,s2,s3);
  870. codegenerror:=olderrorcount<>Errorcount;
  871. end;
  872. end;
  873. procedure FlushOutput;
  874. begin
  875. if not (Status.Use_StdErr) then (* StdErr is flushed after every line *)
  876. begin
  877. if Status.Use_Redir then
  878. Flush(Status.RedirFile)
  879. else
  880. Flush(Output);
  881. end;
  882. end;
  883. {*****************************************************************************
  884. Initialization
  885. *****************************************************************************}
  886. procedure InitVerbose;
  887. begin
  888. { Init }
  889. msg:=new(pmessage,Init(20,msgidxmax));
  890. if msg=nil then
  891. begin
  892. writeln('Fatal: MsgIdx Wrong');
  893. halt(3);
  894. end;
  895. {$ifndef EXTERN_MSG}
  896. msg^.LoadIntern(@msgtxt,msgtxtsize,msgtxt_codepage);
  897. {$else EXTERN_MSG}
  898. LoadMsgFile(exepath+'errore.msg');
  899. {$endif EXTERN_MSG}
  900. FillChar(Status,sizeof(TCompilerStatus),0);
  901. status.verbosity:=V_Default;
  902. Status.MaxErrorCount:=50;
  903. Status.codesize:=aword(-1);
  904. Status.datasize:=aword(-1);
  905. Loadprefixes;
  906. lastfileidx:=-1;
  907. lastmoduleidx:=-1;
  908. status.currentmodule:='';
  909. status.currentsourceppufilename:='';
  910. status.currentsource:='';
  911. status.currentsourcepath:='';
  912. { Register internalerrorproc for cutils/cclasses }
  913. internalerrorproc:=@internalerror;
  914. end;
  915. procedure DoneVerbose;
  916. begin
  917. if assigned(msg) then
  918. begin
  919. dispose(msg,Done);
  920. msg:=nil;
  921. end;
  922. DoneRedirectFile;
  923. end;
  924. procedure printnodeindent; inline;
  925. begin
  926. printnodeindention:=printnodeindention+printnodespacing;
  927. end;
  928. procedure printnodeunindent; inline;
  929. begin
  930. delete(printnodeindention,1,length(printnodespacing));
  931. end;
  932. {$ifdef DEBUG_NODE_XML}
  933. function WritePointer(const P: Pointer): ansistring;
  934. begin
  935. case PtrUInt(P) of
  936. 0:
  937. WritePointer := 'nil';
  938. 1..$FFFF:
  939. WritePointer := '$' + hexstr(PtrUInt(P), 4);
  940. {$if sizeof(Pointer) >= 4}
  941. $10000..$FFFFFFFF:
  942. WritePointer := '$' + hexstr(PtrUInt(P), 8);
  943. {$ifend sizeof(Pointer) >= 4}
  944. {$if sizeof(Pointer) > 4}
  945. else
  946. WritePointer := '$' + hexstr(PtrUInt(P), 2*sizeof(Pointer));
  947. {$ifend sizeof(Pointer) > 4}
  948. end;
  949. end;
  950. function WriteConstPUInt(const P: TConstPtrUInt): ansistring;
  951. begin
  952. case P of
  953. 0:
  954. WriteConstPUInt := 'nil';
  955. 1..$FFFF:
  956. WriteConstPUInt := '$' + hexstr(P, 4);
  957. {$if sizeof(TConstPtrUInt) >= 4}
  958. $10000..$FFFFFFFF:
  959. WriteConstPUInt := '$' + hexstr(P, 8);
  960. {$ifend sizeof(TConstPtrUInt) >= 4}
  961. {$if sizeof(TConstPtrUInt) > 4}
  962. else
  963. WriteConstPUInt := '$' + hexstr(P, 2*sizeof(TConstPtrUInt));
  964. {$endif sizeof(TConstPtrUInt) > 4}
  965. end;
  966. end;
  967. function WriteGUID(const GUID: TGUID): ansistring;
  968. var
  969. i: Integer;
  970. begin
  971. Result := '{' + hexstr(GUID.D1, 8) + '-' + hexstr(GUID.D2, 4) + '-' + hexstr(GUID.D3, 4) + '-';
  972. for i := 0 to 7 do
  973. Result := Result + hexstr(GUID.D4[i], 2);
  974. Result := Result + '}';
  975. end;
  976. function SanitiseXMLString(const S: ansistring): ansistring;
  977. var
  978. X, UTF8Len, CurrentChar: Integer;
  979. needs_quoting, in_quotes, add_end_quote: Boolean;
  980. DoASCII: Boolean;
  981. { Write the given byte as #xxx }
  982. procedure EncodeControlChar(Value: Byte);
  983. begin
  984. if X = Length(Result) then
  985. add_end_quote := False;
  986. Delete(Result, X, 1);
  987. if in_quotes then
  988. begin
  989. Insert('#' + tostr(Value) + '''', Result, X);
  990. { If the entire string consists of control characters, it
  991. doesn't need quoting, so only set the flag here }
  992. needs_quoting := True;
  993. in_quotes := False;
  994. end
  995. else
  996. Insert('#' + tostr(Value), Result, X);
  997. end;
  998. { Write the given byte as either a plain character or an XML keyword }
  999. procedure EncodeStandardChar(Value: Byte);
  1000. begin
  1001. if not in_quotes then
  1002. begin
  1003. in_quotes := True;
  1004. if (X < Length(Result)) then
  1005. begin
  1006. needs_quoting := True;
  1007. Insert('''', Result, X + 1)
  1008. end;
  1009. end;
  1010. { Check the character for anything that could be mistaken for an XML element }
  1011. case CurrentChar of
  1012. Ord('#'):
  1013. { Required to differentiate '#27' from the escape code #27, for example }
  1014. needs_quoting:=true;
  1015. Ord('<'):
  1016. begin
  1017. Delete(Result, X, 1);
  1018. Insert('&lt;', Result, X);
  1019. end;
  1020. Ord('>'):
  1021. begin
  1022. Delete(Result, X, 1);
  1023. Insert('&gt;', Result, X);
  1024. end;
  1025. Ord('&'):
  1026. begin
  1027. Delete(Result, X, 1);
  1028. Insert('&amp;', Result, X);
  1029. end;
  1030. Ord('"'):
  1031. begin
  1032. needs_quoting := True;
  1033. Delete(Result, X, 1);
  1034. Insert('&quot;', Result, X);
  1035. end;
  1036. Ord(''''):
  1037. begin
  1038. needs_quoting:=true;
  1039. { Simply double it like in pascal strings }
  1040. Insert('''', Result, X);
  1041. end;
  1042. else
  1043. { Do nothing };
  1044. end;
  1045. end;
  1046. { Convert character between $80 and $FF to UTF-8 }
  1047. procedure EncodeExtendedChar(Value: Byte);
  1048. begin
  1049. if not in_quotes then
  1050. begin
  1051. in_quotes := True;
  1052. if (X < Length(Result)) then
  1053. begin
  1054. needs_quoting := True;
  1055. Insert('''', Result, X + 1)
  1056. end;
  1057. end;
  1058. case Value of
  1059. $80..$BF: { Add $C2 before the value }
  1060. Insert(#$C2, Result, X);
  1061. $C0..$FF: { Zero the $40 bit and add $C3 before the value }
  1062. begin
  1063. Result[X] := Char(Byte(Result[X]) and $BF);
  1064. Insert(#$C3, Result, X);
  1065. end;
  1066. else
  1067. { Previous conditions should prevent this procedure from being
  1068. called if Value < $80 }
  1069. InternalError(2019061901);
  1070. end;
  1071. end;
  1072. begin
  1073. needs_quoting := False;
  1074. Result := S;
  1075. { Gets set to True if an invalid UTF-8 sequence is found }
  1076. DoASCII := False;
  1077. { By setting in_quotes to false here, we can exclude the single
  1078. quotation marks surrounding the string if it doesn't contain any
  1079. control characters, or consists entirely of control characters. }
  1080. in_quotes := False;
  1081. add_end_quote := True;
  1082. X := Length(Result);
  1083. while X > 0 do
  1084. begin
  1085. CurrentChar := Ord(Result[X]);
  1086. { Control characters and extended characters need special handling }
  1087. case CurrentChar of
  1088. $00..$1F, $7F:
  1089. EncodeControlChar(CurrentChar);
  1090. $20..$7E:
  1091. EncodeStandardChar(CurrentChar);
  1092. { UTF-8 continuation byte }
  1093. $80..$BF:
  1094. begin
  1095. if not in_quotes then
  1096. begin
  1097. in_quotes := True;
  1098. if (X < Length(Result)) then
  1099. begin
  1100. needs_quoting := True;
  1101. Insert('''', Result, X + 1)
  1102. end;
  1103. end;
  1104. UTF8Len := 1;
  1105. repeat
  1106. inc(UTF8Len);
  1107. dec(X);
  1108. until (X = 0) or (UTF8Len >= 4) or (ord(Result[X]) shr 6 <> 2);
  1109. if (X = 0) or (Utf8CodepointLen(@Result[X], UTF8Len, False) <> UTF8Len) then
  1110. begin
  1111. DoASCII := True;
  1112. break;
  1113. end;
  1114. end;
  1115. { Invalid UTF-8 bytes and lead bytes without continuation bytes }
  1116. $C0..$FF:
  1117. begin
  1118. DoASCII := True;
  1119. Break;
  1120. end;
  1121. end;
  1122. Dec(X);
  1123. end;
  1124. { UTF-8 failed, so encode the string as plain ASCII }
  1125. if DoASCII then
  1126. begin
  1127. { Reset the flags and Result }
  1128. needs_quoting := False;
  1129. Result := S;
  1130. in_quotes := False;
  1131. add_end_quote := True;
  1132. for X := Length(Result) downto 1 do
  1133. begin
  1134. CurrentChar := Ord(Result[X]);
  1135. { Control characters and extended characters need special handling }
  1136. case CurrentChar of
  1137. $00..$1F, $7F:
  1138. EncodeControlChar(CurrentChar);
  1139. $20..$7E:
  1140. EncodeStandardChar(CurrentChar);
  1141. { Extended characters }
  1142. else
  1143. EncodeExtendedChar(CurrentChar);
  1144. end;
  1145. end;
  1146. end;
  1147. if needs_quoting then
  1148. begin
  1149. if in_quotes then
  1150. Result := '''' + Result;
  1151. if add_end_quote then
  1152. Result := Result + '''';
  1153. end;
  1154. end;
  1155. {$endif DEBUG_NODE_XML}
  1156. finalization
  1157. { Be sure to close the redirect files to flush all data }
  1158. DoneRedirectFile;
  1159. end.