verbose.pas 43 KB

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