verbose.pas 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  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. {$if sizeof(TConstPtrUInt) > 4}
  961. else
  962. WriteConstPUInt := '$' + hexstr(P, 2*sizeof(TConstPtrUInt));
  963. {$endif sizeof(TConstPtrUInt) > 4}
  964. end;
  965. end;
  966. function WriteGUID(const GUID: TGUID): ansistring;
  967. var
  968. i: Integer;
  969. begin
  970. Result := '{' + hexstr(GUID.D1, 8) + '-' + hexstr(GUID.D2, 4) + '-' + hexstr(GUID.D3, 4) + '-';
  971. for i := 0 to 7 do
  972. Result := Result + hexstr(GUID.D4[i], 2);
  973. Result := Result + '}';
  974. end;
  975. function SanitiseXMLString(const S: ansistring): ansistring;
  976. var
  977. X, UTF8Len, UTF8Char, CurrentChar: Integer;
  978. needs_quoting, in_quotes, add_end_quote: Boolean;
  979. DoASCII: Boolean;
  980. { Write the given byte as #xxx }
  981. procedure EncodeControlChar(Value: Byte);
  982. begin
  983. if X = Length(Result) then
  984. add_end_quote := False;
  985. Delete(Result, X, 1);
  986. if in_quotes then
  987. begin
  988. Insert('#' + tostr(Value) + '''', Result, X);
  989. { If the entire string consists of control characters, it
  990. doesn't need quoting, so only set the flag here }
  991. needs_quoting := True;
  992. in_quotes := False;
  993. end
  994. else
  995. Insert('#' + tostr(Value), Result, X);
  996. end;
  997. { Write the given byte as either a plain character or an XML keyword }
  998. procedure EncodeStandardChar(Value: Byte);
  999. begin
  1000. if not in_quotes then
  1001. begin
  1002. in_quotes := True;
  1003. if (X < Length(Result)) then
  1004. begin
  1005. needs_quoting := True;
  1006. Insert('''', Result, X + 1)
  1007. end;
  1008. end;
  1009. { Check the character for anything that could be mistaken for an XML element }
  1010. case CurrentChar of
  1011. Ord('#'):
  1012. { Required to differentiate '#27' from the escape code #27, for example }
  1013. needs_quoting:=true;
  1014. Ord('<'):
  1015. begin
  1016. Delete(Result, X, 1);
  1017. Insert('&lt;', Result, X);
  1018. end;
  1019. Ord('>'):
  1020. begin
  1021. Delete(Result, X, 1);
  1022. Insert('&gt;', Result, X);
  1023. end;
  1024. Ord('&'):
  1025. begin
  1026. Delete(Result, X, 1);
  1027. Insert('&amp;', Result, X);
  1028. end;
  1029. Ord('"'):
  1030. begin
  1031. needs_quoting := True;
  1032. Delete(Result, X, 1);
  1033. Insert('&quot;', Result, X);
  1034. end;
  1035. Ord(''''):
  1036. begin
  1037. needs_quoting:=true;
  1038. { Simply double it like in pascal strings }
  1039. Insert('''', Result, X);
  1040. end;
  1041. else
  1042. { Do nothing };
  1043. end;
  1044. end;
  1045. { Convert character between $80 and $FF to UTF-8 }
  1046. procedure EncodeExtendedChar(Value: Byte);
  1047. begin
  1048. if not in_quotes then
  1049. begin
  1050. in_quotes := True;
  1051. if (X < Length(Result)) then
  1052. begin
  1053. needs_quoting := True;
  1054. Insert('''', Result, X + 1)
  1055. end;
  1056. end;
  1057. case Value of
  1058. $80..$BF: { Add $C2 before the value }
  1059. Insert(#$C2, Result, X);
  1060. $C0..$FF: { Zero the $40 bit and add $C3 before the value }
  1061. begin
  1062. Result[X] := Char(Byte(Result[X]) and $BF);
  1063. Insert(#$C3, Result, X);
  1064. end;
  1065. else
  1066. { Previous conditions should prevent this procedure from being
  1067. called if Value < $80 }
  1068. InternalError(2019061901);
  1069. end;
  1070. end;
  1071. begin
  1072. needs_quoting := False;
  1073. Result := S;
  1074. { Gets set to True if an invalid UTF-8 sequence is found }
  1075. DoASCII := False;
  1076. { By setting in_quotes to false here, we can exclude the single
  1077. quotation marks surrounding the string if it doesn't contain any
  1078. control characters, or consists entirely of control characters. }
  1079. in_quotes := False;
  1080. add_end_quote := True;
  1081. X := Length(Result);
  1082. while X > 0 do
  1083. begin
  1084. CurrentChar := Ord(Result[X]);
  1085. { Control characters and extended characters need special handling }
  1086. case CurrentChar of
  1087. $00..$1F, $7F:
  1088. EncodeControlChar(CurrentChar);
  1089. $20..$7E:
  1090. EncodeStandardChar(CurrentChar);
  1091. { UTF-8 continuation byte }
  1092. $80..$BF:
  1093. begin
  1094. if not in_quotes then
  1095. begin
  1096. in_quotes := True;
  1097. if (X < Length(Result)) then
  1098. begin
  1099. needs_quoting := True;
  1100. Insert('''', Result, X + 1)
  1101. end;
  1102. end;
  1103. UTF8Char := CurrentChar and $3F; { The data bits of the continuation byte }
  1104. UTF8Len := 1; { This variable actually holds 1 less than the length }
  1105. { By setting DoASCII to true, it marks the string as 'invalid UTF-8'
  1106. automatically if it reaches the beginning of the string unexpectedly }
  1107. DoASCII := True;
  1108. Dec(X);
  1109. while X > 0 do
  1110. begin
  1111. CurrentChar := Ord(Result[X]);
  1112. case CurrentChar of
  1113. { A standard character here is invalid UTF-8 }
  1114. $00..$7F:
  1115. Break;
  1116. { Another continuation byte }
  1117. $80..$BF:
  1118. begin
  1119. UTF8Char := UTF8Char or ((CurrentChar and $3F) shl (6 * UTF8Len));
  1120. Inc(UTF8Len);
  1121. if UTF8Len >= 4 then
  1122. { Sequence too long }
  1123. Break;
  1124. end;
  1125. { Lead byte for 2-byte sequences }
  1126. $C2..$DF:
  1127. begin
  1128. if UTF8Len <> 1 then Break;
  1129. UTF8Char := UTF8Char or ((CurrentChar and $1F) shl 6);
  1130. { Check to see if the code is in range and not part of an 'overlong' sequence }
  1131. case UTF8Char of
  1132. $0080..$07FF:
  1133. DoASCII := False;
  1134. else
  1135. { Do nothing - DoASCII is already true }
  1136. end;
  1137. Break;
  1138. end;
  1139. { Lead byte for 3-byte sequences }
  1140. $E0..$EF:
  1141. begin
  1142. if UTF8Len <> 2 then Break;
  1143. UTF8Char := UTF8Char or ((CurrentChar and $0F) shl 12);
  1144. { Check to see if the code is in range and not part of an 'overlong' sequence }
  1145. case UTF8Char of
  1146. $0800..$D7FF, $E000..$FFFF: { $D800..$DFFF is reserved and hence invalid }
  1147. DoASCII := False;
  1148. else
  1149. { Do nothing - DoASCII is already true }
  1150. end;
  1151. Break;
  1152. end;
  1153. { Lead byte for 4-byte sequences }
  1154. $F0..$F4:
  1155. begin
  1156. if UTF8Len <> 3 then Break;
  1157. UTF8Char := UTF8Char or ((CurrentChar and $07) shl 18);
  1158. { Check to see if the code is in range and not part of an 'overlong' sequence }
  1159. case UTF8Char of
  1160. $010000..$10FFFF:
  1161. DoASCII := False;
  1162. else
  1163. { Do nothing - DoASCII is already true }
  1164. end;
  1165. Break;
  1166. end;
  1167. { Invalid character }
  1168. else
  1169. Break;
  1170. end;
  1171. end;
  1172. if DoASCII then
  1173. Break;
  1174. { If all is fine, we don't need to encode any more characters }
  1175. end;
  1176. { Invalid UTF-8 bytes and lead bytes without continuation bytes }
  1177. $C0..$FF:
  1178. begin
  1179. DoASCII := True;
  1180. Break;
  1181. end;
  1182. end;
  1183. Dec(X);
  1184. end;
  1185. { UTF-8 failed, so encode the string as plain ASCII }
  1186. if DoASCII then
  1187. begin
  1188. { Reset the flags and Result }
  1189. needs_quoting := False;
  1190. Result := S;
  1191. in_quotes := False;
  1192. add_end_quote := True;
  1193. for X := Length(Result) downto 1 do
  1194. begin
  1195. CurrentChar := Ord(Result[X]);
  1196. { Control characters and extended characters need special handling }
  1197. case CurrentChar of
  1198. $00..$1F, $7F:
  1199. EncodeControlChar(CurrentChar);
  1200. $20..$7E:
  1201. EncodeStandardChar(CurrentChar);
  1202. { Extended characters }
  1203. else
  1204. EncodeExtendedChar(CurrentChar);
  1205. end;
  1206. end;
  1207. end;
  1208. if needs_quoting then
  1209. begin
  1210. if in_quotes then
  1211. Result := '''' + Result;
  1212. if add_end_quote then
  1213. Result := Result + '''';
  1214. end;
  1215. end;
  1216. {$endif DEBUG_NODE_XML}
  1217. initialization
  1218. constexp.internalerrorproc:=@internalerror;
  1219. finalization
  1220. { Be sure to close the redirect files to flush all data }
  1221. DoneRedirectFile;
  1222. end.