2
0

verbose.pas 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  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. end;
  161. procedure FreeLocalVerbosity(var fstate : pmessagestaterecord);
  162. var pstate : pmessagestaterecord;
  163. begin
  164. pstate:=unaligned(fstate);
  165. while assigned(pstate) do
  166. begin
  167. unaligned(fstate):=pstate^.next;
  168. dispose(pstate);
  169. pstate:=unaligned(fstate);
  170. end;
  171. end;
  172. function ChangeMessageVerbosity(s: ansistring; var i : integer;state:tmsgstate): boolean;
  173. var
  174. tok : ansistring;
  175. msgnr, code : longint;
  176. begin
  177. { delete everything up to and including 'm' }
  178. delete(s,1,i);
  179. { the rest of the string must be message numbers }
  180. inc(i,length(s)+1);
  181. result:=false;
  182. repeat
  183. tok:=GetToken(s,',');
  184. if (tok='') then
  185. break;
  186. val(tok, msgnr, code);
  187. if (code<>0) then
  188. exit;
  189. if not msg^.valid(msgnr) then
  190. exit
  191. else
  192. recordpendingmessagestate(msgnr, state);
  193. until false;
  194. result:=true;
  195. end;
  196. function SetMessageVerbosity(v:longint;state:tmsgstate):boolean;
  197. begin
  198. result:=msg^.setverbosity(v,state);
  199. end;
  200. function CheckVerbosity(v:longint):boolean;
  201. begin
  202. result:=do_checkverbosity(v);
  203. end;
  204. function SetVerbosity(const s:TCmdStr):boolean;
  205. const
  206. message_verbosity:array[boolean] of tmsgstate=(ms_off_global,ms_on_global);
  207. var
  208. m : Longint;
  209. i : Integer;
  210. inverse : boolean;
  211. c : char;
  212. begin
  213. Setverbosity:=false;
  214. val(s,m,i);
  215. if (i=0) and (s<>'') then
  216. status.verbosity:=m
  217. else
  218. begin
  219. i:=1;
  220. while i<=length(s) do
  221. begin
  222. c:=upcase(s[i]);
  223. inverse:=false;
  224. { on/off ? }
  225. if (i<length(s)) then
  226. case s[i+1] of
  227. '-' : begin
  228. inc(i);
  229. inverse:=true;
  230. end;
  231. '+' : inc(i);
  232. end;
  233. { handle switch }
  234. case c of
  235. { Special cases }
  236. '0' : status.verbosity:=V_Default;
  237. 'A' : status.verbosity:=V_All;
  238. 'B' : begin
  239. if inverse then
  240. status.print_source_path:=false
  241. else
  242. status.print_source_path:=true;
  243. end;
  244. 'M' : if not ChangeMessageVerbosity(s,i,message_verbosity[inverse]) then
  245. begin
  246. result:=false;
  247. exit
  248. end;
  249. 'P' : begin
  250. if inverse then
  251. paraprintnodetree:=0
  252. else
  253. paraprintnodetree:=1;
  254. end;
  255. 'Q' : begin
  256. if inverse then
  257. status.showmsgnrs:=false
  258. else
  259. status.showmsgnrs:=true;
  260. end;
  261. 'R' : begin
  262. if inverse then
  263. begin
  264. status.use_gccoutput:=false;
  265. status.use_stderr:=false;
  266. end
  267. else
  268. begin
  269. status.use_gccoutput:=true;
  270. status.use_stderr:=true;
  271. end;
  272. end;
  273. 'V' : PrepareReport;
  274. 'Z' : begin
  275. if inverse then
  276. status.use_stderr:=false
  277. else
  278. status.use_stderr:=true;
  279. end;
  280. { Normal cases - do an or }
  281. 'C' : if inverse then
  282. status.verbosity:=status.verbosity and (not V_Conditional)
  283. else
  284. status.verbosity:=status.verbosity or V_Conditional;
  285. 'D' : if inverse then
  286. status.verbosity:=status.verbosity and (not V_Debug)
  287. else
  288. status.verbosity:=status.verbosity or V_Debug;
  289. 'E' : if inverse then
  290. status.verbosity:=status.verbosity and (not V_Error)
  291. else
  292. status.verbosity:=status.verbosity or V_Error;
  293. 'H' : if inverse then
  294. status.verbosity:=status.verbosity and (not V_Hint)
  295. else
  296. status.verbosity:=status.verbosity or V_Hint;
  297. 'I' : if inverse then
  298. status.verbosity:=status.verbosity and (not V_Info)
  299. else
  300. status.verbosity:=status.verbosity or V_Info;
  301. 'J' : if inverse then
  302. status.verbosity:=status.verbosity and (not V_Parallel)
  303. else
  304. status.verbosity:=status.verbosity or V_Parallel;
  305. 'L' : if inverse then
  306. status.verbosity:=status.verbosity and (not V_Status)
  307. else
  308. status.verbosity:=status.verbosity or V_Status;
  309. 'N' : if inverse then
  310. status.verbosity:=status.verbosity and (not V_Note)
  311. else
  312. status.verbosity:=status.verbosity or V_Note;
  313. 'S' : if inverse then
  314. status.verbosity:=status.verbosity and (not V_TimeStamps)
  315. else
  316. status.verbosity:=status.verbosity or V_TimeStamps;
  317. 'T' : if inverse then
  318. status.verbosity:=status.verbosity and (not V_Tried)
  319. else
  320. status.verbosity:=status.verbosity or V_Tried;
  321. 'U' : if inverse then
  322. status.verbosity:=status.verbosity and (not V_Used)
  323. else
  324. status.verbosity:=status.verbosity or V_Used;
  325. 'W' : if inverse then
  326. status.verbosity:=status.verbosity and (not V_Warning)
  327. else
  328. status.verbosity:=status.verbosity or V_Warning;
  329. 'X' : if inverse then
  330. status.verbosity:=status.verbosity and (not V_Executable)
  331. else
  332. status.verbosity:=status.verbosity or V_Executable;
  333. end;
  334. inc(i);
  335. end;
  336. end;
  337. if status.verbosity=0 then
  338. status.verbosity:=V_Default;
  339. setverbosity:=true;
  340. end;
  341. procedure Loadprefixes;
  342. function loadprefix(w:longint):string;
  343. var
  344. s : string;
  345. idx : longint;
  346. begin
  347. s:=msg^.get(w,[]);
  348. idx:=pos('_',s);
  349. if idx>0 then
  350. Loadprefix:=Copy(s,idx+1,255)
  351. else
  352. Loadprefix:=s;
  353. end;
  354. begin
  355. { Load the prefixes }
  356. fatalstr:=Loadprefix(general_i_fatal);
  357. errorstr:=Loadprefix(general_i_error);
  358. warningstr:=Loadprefix(general_i_warning);
  359. notestr:=Loadprefix(general_i_note);
  360. hintstr:=Loadprefix(general_i_hint);
  361. end;
  362. procedure LoadMsgFile(const fn:string);
  363. begin
  364. { reload the internal messages if not already loaded }
  365. {$ifndef EXTERN_MSG}
  366. if not msg^.msgintern then
  367. msg^.LoadIntern(@msgtxt,msgtxtsize,msgtxt_codepage);
  368. {$endif}
  369. if not msg^.LoadExtern(fn) then
  370. begin
  371. {$ifdef EXTERN_MSG}
  372. writeln('Fatal: Cannot find error message file.');
  373. halt(3);
  374. {$else}
  375. msg^.LoadIntern(@msgtxt,msgtxtsize,msgtxt_codepage);
  376. {$endif}
  377. end;
  378. { reload the prefixes using the new messages }
  379. Loadprefixes;
  380. end;
  381. procedure MaybeLoadMessageFile;
  382. begin
  383. { Load new message file }
  384. if (msgfilename<>'') then
  385. begin
  386. LoadMsgFile(msgfilename);
  387. msgfilename:='';
  388. end;
  389. end;
  390. var
  391. lastfileidx,
  392. lastmoduleidx : longint;
  393. Procedure UpdateStatus;
  394. var
  395. module : tmodule;
  396. begin
  397. { fix status }
  398. status.currentline:=current_filepos.line;
  399. status.currentcolumn:=current_filepos.column;
  400. if (current_filepos.moduleindex <> lastmoduleidx) or
  401. (current_filepos.fileindex <> lastfileidx) then
  402. begin
  403. module:=get_module(current_filepos.moduleindex);
  404. if assigned(module) and assigned(module.sourcefiles) then
  405. begin
  406. { update status record }
  407. status.currentmodule:=module.modulename^;
  408. status.currentsourceppufilename:=module.ppufilename;
  409. status.currentmodulestate:=ModuleStateStr[module.state];
  410. status.currentsource:=module.sourcefiles.get_file_name(current_filepos.fileindex);
  411. status.currentsourcepath:=module.sourcefiles.get_file_path(current_filepos.fileindex);
  412. status.sources_avail:=module.sources_avail;
  413. { if currentsourcepath is relative, make it absolute }
  414. if not path_absolute(status.currentsourcepath) then
  415. status.currentsourcepath:=GetCurrentDir+status.currentsourcepath;
  416. { update lastfileidx only if name known PM }
  417. if status.currentsource<>'' then
  418. lastfileidx:=current_filepos.fileindex
  419. else
  420. lastfileidx:=0;
  421. lastmoduleidx:=module.unit_index;
  422. end;
  423. end;
  424. end;
  425. procedure ShowStatus;
  426. begin
  427. UpdateStatus;
  428. if do_status() then
  429. raise ECompilerAbort.Create;
  430. end;
  431. function ErrorCount:longint;
  432. begin
  433. ErrorCount:=status.errorcount;
  434. end;
  435. procedure SetErrorFlags(const s:string);
  436. var
  437. code : integer;
  438. i,j,l : longint;
  439. begin
  440. { empty string means error count = 1 for backward compatibility (PFV) }
  441. if s='' then
  442. begin
  443. status.maxerrorcount:=1;
  444. exit;
  445. end;
  446. i:=0;
  447. while (i<length(s)) do
  448. begin
  449. inc(i);
  450. case s[i] of
  451. '0'..'9' :
  452. begin
  453. j:=i;
  454. while (j<=length(s)) and (s[j] in ['0'..'9']) do
  455. inc(j);
  456. val(copy(s,i,j-i),l,code);
  457. if code<>0 then
  458. l:=1;
  459. status.maxerrorcount:=l;
  460. i:=j-1;
  461. end;
  462. 'w','W' :
  463. begin
  464. if (i<length(s)) and (s[i+1]='-') then
  465. begin
  466. inc(i);
  467. status.errorwarning:=false;
  468. end
  469. else
  470. begin
  471. status.errorwarning:=true;
  472. { Enable writing of warnings, to avoid getting errors without any message }
  473. status.verbosity:=status.verbosity or V_Warning;
  474. end;
  475. end;
  476. 'n','N' :
  477. begin
  478. if (i<length(s)) and (s[i+1]='-') then
  479. begin
  480. inc(i);
  481. status.errornote:=false;
  482. end
  483. else
  484. begin
  485. status.errornote:=true;
  486. { Enable writing of notes, to avoid getting errors without any message }
  487. status.verbosity:=status.verbosity or V_Note;
  488. end;
  489. end;
  490. 'h','H' :
  491. begin
  492. if (i<length(s)) and (s[i+1]='-') then
  493. begin
  494. inc(i);
  495. status.errorhint:=false;
  496. end
  497. else
  498. begin
  499. status.errorhint:=true;
  500. { Enable writing of hints, to avoid getting errors without any message }
  501. status.verbosity:=status.verbosity or V_Hint;
  502. end;
  503. end;
  504. end;
  505. end;
  506. end;
  507. procedure GenerateError;
  508. begin
  509. inc(status.errorcount);
  510. end;
  511. procedure internalerror(i : longint);noreturn;
  512. begin
  513. InternalError(i,'');
  514. end;
  515. procedure InternalError(i:longint; const s : ansistring);noreturn;
  516. procedure doraise;
  517. begin
  518. raise ECompilerAbort.Create;
  519. end;
  520. begin
  521. UpdateStatus;
  522. do_internalerrorex(i,s);
  523. GenerateError;
  524. doraise;
  525. end;
  526. procedure Comment(l:longint;s:ansistring);
  527. var
  528. dostop : boolean;
  529. begin
  530. dostop:=((l and V_Fatal)<>0);
  531. if ((l and V_Error)<>0) or
  532. ((l and V_Fatal)<>0) or
  533. (status.errorwarning and ((l and V_Warning)<>0)) or
  534. (status.errornote and ((l and V_Note)<>0)) or
  535. (status.errorhint and ((l and V_Hint)<>0)) then
  536. GenerateError
  537. else
  538. if l and V_Warning <> 0 then
  539. inc(status.countWarnings)
  540. else
  541. if l and V_Note <> 0 then
  542. inc(status.countNotes)
  543. else
  544. if l and V_Hint <> 0 then
  545. inc(status.countHints);
  546. { check verbosity level }
  547. if not CheckVerbosity(l) then
  548. exit;
  549. if (l and V_LineInfoMask)<>0 then
  550. l:=l or V_LineInfo;
  551. { Create status info }
  552. UpdateStatus;
  553. { Fix replacements }
  554. DefaultReplacements(s,false);
  555. { show comment }
  556. if do_comment(l,s) or dostop then
  557. raise ECompilerAbort.Create;
  558. if (status.errorcount>=status.maxerrorcount) and not status.skip_error then
  559. begin
  560. Message1(unit_f_errors_in_unit,tostr(status.errorcount));
  561. status.skip_error:=true;
  562. raise ECompilerAbort.Create;
  563. end;
  564. end;
  565. function GetMessageState(m:longint):tmsgstate;
  566. var
  567. i: integer;
  568. begin
  569. i:=m div 1000;
  570. { get the default state }
  571. Result:=msg^.msgstates[i][m mod 1000];
  572. { and search at the current unit settings }
  573. { todo }
  574. end;
  575. Procedure Msg2Comment(s:ansistring;w:longint;onqueue:tmsgqueueevent);
  576. var
  577. idx,i,v : longint;
  578. dostop : boolean;
  579. doqueue : boolean;
  580. st : tmsgstate;
  581. ch : char;
  582. begin
  583. {Reset}
  584. dostop:=false;
  585. doqueue:=false;
  586. v:=0;
  587. {Parse options}
  588. idx:=pos('_',s);
  589. if idx=0 then
  590. v:=V_None
  591. else
  592. if (idx >= 1) And (idx <= 5) then
  593. begin
  594. for i:=1 to idx do
  595. begin
  596. ch:=upcase(s[i]);
  597. case ch of
  598. 'F' :
  599. begin
  600. v:=v or V_Fatal;
  601. GenerateError;
  602. dostop:=true;
  603. end;
  604. 'E','W','N','H':
  605. begin
  606. if ch='E' then
  607. st:=ms_error
  608. else
  609. st:=GetMessageState(w);
  610. { We only want to know about local value }
  611. st:= tmsgstate(ord(st) and ms_local_mask);
  612. if st=ms_error then
  613. begin
  614. v:=v or V_Error;
  615. GenerateError;
  616. end
  617. else if st<>ms_off then
  618. case ch of
  619. 'W':
  620. begin
  621. v:=v or V_Warning;
  622. if CheckVerbosity(V_Warning) then
  623. if status.errorwarning then
  624. GenerateError
  625. else
  626. inc(status.countWarnings);
  627. end;
  628. 'N' :
  629. begin
  630. v:=v or V_Note;
  631. if CheckVerbosity(V_Note) then
  632. if status.errornote then
  633. GenerateError
  634. else
  635. inc(status.countNotes);
  636. end;
  637. 'H' :
  638. begin
  639. v:=v or V_Hint;
  640. if CheckVerbosity(V_Hint) then
  641. if status.errorhint then
  642. GenerateError
  643. else
  644. inc(status.countHints);
  645. end;
  646. end;
  647. end;
  648. 'O' :
  649. v:=v or V_Normal;
  650. 'I' :
  651. v:=v or V_Info;
  652. 'L' :
  653. v:=v or V_LineInfo;
  654. 'U' :
  655. v:=v or V_Used;
  656. 'T' :
  657. v:=v or V_Tried;
  658. 'C' :
  659. v:=v or V_Conditional;
  660. 'D' :
  661. v:=v or V_Debug;
  662. 'X' :
  663. v:=v or V_Executable;
  664. 'S' :
  665. dostop:=true;
  666. '_' : ;
  667. end;
  668. end;
  669. end;
  670. Delete(s,1,idx);
  671. { check verbosity level }
  672. if not CheckVerbosity(v) then
  673. begin
  674. doqueue := onqueue <> nil;
  675. if not doqueue then
  676. exit;
  677. end;
  678. if (v and V_LineInfoMask)<>0 then
  679. v:=v or V_LineInfo;
  680. { fix status }
  681. UpdateStatus;
  682. { Fix replacements }
  683. DefaultReplacements(s,false);
  684. if status.showmsgnrs and ((v and V_Normal)=0) then
  685. s:='('+tostr(w)+') '+s;
  686. if doqueue then
  687. begin
  688. onqueue(s,v,w);
  689. exit;
  690. end;
  691. { show comment }
  692. if do_comment(v,s) or dostop then
  693. raise ECompilerAbort.Create;
  694. if (status.errorcount>=status.maxerrorcount) and not status.skip_error then
  695. begin
  696. Message1(unit_f_errors_in_unit,tostr(status.errorcount));
  697. status.skip_error:=true;
  698. raise ECompilerAbort.Create;
  699. end;
  700. end;
  701. function MessageStr(w:longint):TMsgStr;
  702. begin
  703. MaybeLoadMessageFile;
  704. MessageStr:=msg^.Get(w,[]);
  705. end;
  706. procedure Message(w:longint;onqueue:tmsgqueueevent=nil);
  707. begin
  708. MaybeLoadMessageFile;
  709. Msg2Comment(msg^.Get(w,[]),w,onqueue);
  710. end;
  711. procedure Message1(w:longint;const s1:TMsgStr;onqueue:tmsgqueueevent=nil);
  712. begin
  713. MaybeLoadMessageFile;
  714. Msg2Comment(msg^.Get(w,[s1]),w,onqueue);
  715. end;
  716. procedure Message2(w:longint;const s1,s2:TMsgStr;onqueue:tmsgqueueevent=nil);
  717. begin
  718. MaybeLoadMessageFile;
  719. Msg2Comment(msg^.Get(w,[s1,s2]),w,onqueue);
  720. end;
  721. procedure Message3(w:longint;const s1,s2,s3:TMsgStr;onqueue:tmsgqueueevent=nil);
  722. begin
  723. MaybeLoadMessageFile;
  724. Msg2Comment(msg^.Get(w,[s1,s2,s3]),w,onqueue);
  725. end;
  726. procedure Message4(w:longint;const s1,s2,s3,s4:TMsgStr;onqueue:tmsgqueueevent=nil);
  727. begin
  728. MaybeLoadMessageFile;
  729. Msg2Comment(msg^.Get(w,[s1,s2,s3,s4]),w,onqueue);
  730. end;
  731. procedure MessagePos(const pos:tfileposinfo;w:longint;onqueue:tmsgqueueevent=nil);
  732. var
  733. oldpos : tfileposinfo;
  734. begin
  735. oldpos:=current_filepos;
  736. current_filepos:=pos;
  737. MaybeLoadMessageFile;
  738. Msg2Comment(msg^.Get(w,[]),w,onqueue);
  739. current_filepos:=oldpos;
  740. end;
  741. procedure MessagePos1(const pos:tfileposinfo;w:longint;const s1:TMsgStr;onqueue:tmsgqueueevent=nil);
  742. var
  743. oldpos : tfileposinfo;
  744. begin
  745. oldpos:=current_filepos;
  746. current_filepos:=pos;
  747. MaybeLoadMessageFile;
  748. Msg2Comment(msg^.Get(w,[s1]),w,onqueue);
  749. current_filepos:=oldpos;
  750. end;
  751. procedure MessagePos2(const pos:tfileposinfo;w:longint;const s1,s2:TMsgStr;onqueue:tmsgqueueevent=nil);
  752. var
  753. oldpos : tfileposinfo;
  754. begin
  755. oldpos:=current_filepos;
  756. current_filepos:=pos;
  757. MaybeLoadMessageFile;
  758. Msg2Comment(msg^.Get(w,[s1,s2]),w,onqueue);
  759. current_filepos:=oldpos;
  760. end;
  761. procedure MessagePos3(const pos:tfileposinfo;w:longint;const s1,s2,s3:TMsgStr;onqueue:tmsgqueueevent=nil);
  762. var
  763. oldpos : tfileposinfo;
  764. begin
  765. oldpos:=current_filepos;
  766. current_filepos:=pos;
  767. MaybeLoadMessageFile;
  768. Msg2Comment(msg^.Get(w,[s1,s2,s3]),w,onqueue);
  769. current_filepos:=oldpos;
  770. end;
  771. procedure MessagePos4(const pos:tfileposinfo;w:longint;const s1,s2,s3,s4:TMsgStr;onqueue:tmsgqueueevent=nil);
  772. var
  773. oldpos : tfileposinfo;
  774. begin
  775. oldpos:=current_filepos;
  776. current_filepos:=pos;
  777. MaybeLoadMessageFile;
  778. Msg2Comment(msg^.Get(w,[s1,s2,s3,s4]),w,onqueue);
  779. current_filepos:=oldpos;
  780. end;
  781. {*****************************************************************************
  782. override the message calls to set codegenerror
  783. *****************************************************************************}
  784. procedure cgmessage(t : longint);
  785. var
  786. olderrorcount : longint;
  787. begin
  788. if not(codegenerror) then
  789. begin
  790. olderrorcount:=Errorcount;
  791. verbose.Message(t);
  792. codegenerror:=olderrorcount<>Errorcount;
  793. end;
  794. end;
  795. procedure cgmessage1(t : longint;const s : TMsgStr);
  796. var
  797. olderrorcount : longint;
  798. begin
  799. if not(codegenerror) then
  800. begin
  801. olderrorcount:=Errorcount;
  802. verbose.Message1(t,s);
  803. codegenerror:=olderrorcount<>Errorcount;
  804. end;
  805. end;
  806. procedure cgmessage2(t : longint;const s1,s2 : TMsgStr);
  807. var
  808. olderrorcount : longint;
  809. begin
  810. if not(codegenerror) then
  811. begin
  812. olderrorcount:=Errorcount;
  813. verbose.Message2(t,s1,s2);
  814. codegenerror:=olderrorcount<>Errorcount;
  815. end;
  816. end;
  817. procedure cgmessage3(t : longint;const s1,s2,s3 : TMsgStr);
  818. var
  819. olderrorcount : longint;
  820. begin
  821. if not(codegenerror) then
  822. begin
  823. olderrorcount:=Errorcount;
  824. verbose.Message3(t,s1,s2,s3);
  825. codegenerror:=olderrorcount<>Errorcount;
  826. end;
  827. end;
  828. procedure cgmessagepos(const pos:tfileposinfo;t : longint);
  829. var
  830. olderrorcount : longint;
  831. begin
  832. if not(codegenerror) then
  833. begin
  834. olderrorcount:=Errorcount;
  835. verbose.MessagePos(pos,t);
  836. codegenerror:=olderrorcount<>Errorcount;
  837. end;
  838. end;
  839. procedure cgmessagepos1(const pos:tfileposinfo;t : longint;const s1 : TMsgStr);
  840. var
  841. olderrorcount : longint;
  842. begin
  843. if not(codegenerror) then
  844. begin
  845. olderrorcount:=Errorcount;
  846. verbose.MessagePos1(pos,t,s1);
  847. codegenerror:=olderrorcount<>Errorcount;
  848. end;
  849. end;
  850. procedure cgmessagepos2(const pos:tfileposinfo;t : longint;const s1,s2 : TMsgStr);
  851. var
  852. olderrorcount : longint;
  853. begin
  854. if not(codegenerror) then
  855. begin
  856. olderrorcount:=Errorcount;
  857. verbose.MessagePos2(pos,t,s1,s2);
  858. codegenerror:=olderrorcount<>Errorcount;
  859. end;
  860. end;
  861. procedure cgmessagepos3(const pos:tfileposinfo;t : longint;const s1,s2,s3 : TMsgStr);
  862. var
  863. olderrorcount : longint;
  864. begin
  865. if not(codegenerror) then
  866. begin
  867. olderrorcount:=Errorcount;
  868. verbose.MessagePos3(pos,t,s1,s2,s3);
  869. codegenerror:=olderrorcount<>Errorcount;
  870. end;
  871. end;
  872. procedure FlushOutput;
  873. begin
  874. if not (Status.Use_StdErr) then (* StdErr is flushed after every line *)
  875. begin
  876. if Status.Use_Redir then
  877. Flush(Status.RedirFile)
  878. else
  879. Flush(Output);
  880. end;
  881. end;
  882. {*****************************************************************************
  883. Initialization
  884. *****************************************************************************}
  885. procedure InitVerbose;
  886. begin
  887. { Init }
  888. msg:=new(pmessage,Init(20,msgidxmax));
  889. if msg=nil then
  890. begin
  891. writeln('Fatal: MsgIdx Wrong');
  892. halt(3);
  893. end;
  894. {$ifndef EXTERN_MSG}
  895. msg^.LoadIntern(@msgtxt,msgtxtsize,msgtxt_codepage);
  896. {$else EXTERN_MSG}
  897. LoadMsgFile(exepath+'errore.msg');
  898. {$endif EXTERN_MSG}
  899. FillChar(Status,sizeof(TCompilerStatus),0);
  900. status.verbosity:=V_Default;
  901. Status.MaxErrorCount:=50;
  902. Status.codesize:=aword(-1);
  903. Status.datasize:=aword(-1);
  904. Loadprefixes;
  905. lastfileidx:=-1;
  906. lastmoduleidx:=-1;
  907. status.currentmodule:='';
  908. status.currentsourceppufilename:='';
  909. status.currentsource:='';
  910. status.currentsourcepath:='';
  911. { Register internalerrorproc for cutils/cclasses }
  912. internalerrorproc:=@internalerror;
  913. end;
  914. procedure DoneVerbose;
  915. begin
  916. if assigned(msg) then
  917. begin
  918. dispose(msg,Done);
  919. msg:=nil;
  920. end;
  921. DoneRedirectFile;
  922. end;
  923. procedure printnodeindent; inline;
  924. begin
  925. printnodeindention:=printnodeindention+printnodespacing;
  926. end;
  927. procedure printnodeunindent; inline;
  928. begin
  929. delete(printnodeindention,1,length(printnodespacing));
  930. end;
  931. {$ifdef DEBUG_NODE_XML}
  932. function WritePointer(const P: Pointer): ansistring;
  933. begin
  934. case PtrUInt(P) of
  935. 0:
  936. WritePointer := 'nil';
  937. 1..$FFFF:
  938. WritePointer := '$' + hexstr(PtrUInt(P), 4);
  939. {$if sizeof(Pointer) >= 4}
  940. $10000..$FFFFFFFF:
  941. WritePointer := '$' + hexstr(PtrUInt(P), 8);
  942. {$ifend sizeof(Pointer) >= 4}
  943. {$if sizeof(Pointer) > 4}
  944. else
  945. WritePointer := '$' + hexstr(PtrUInt(P), 2*sizeof(Pointer));
  946. {$ifend sizeof(Pointer) > 4}
  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, 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. UTF8Len := 1;
  1104. repeat
  1105. inc(UTF8Len);
  1106. dec(X);
  1107. until (X = 0) or (UTF8Len >= 4) or (ord(Result[X]) shr 6 <> 2);
  1108. if (X = 0) or (Utf8CodepointLen(@Result[X], UTF8Len, False) <> UTF8Len) then
  1109. begin
  1110. DoASCII := True;
  1111. break;
  1112. end;
  1113. end;
  1114. { Invalid UTF-8 bytes and lead bytes without continuation bytes }
  1115. $C0..$FF:
  1116. begin
  1117. DoASCII := True;
  1118. Break;
  1119. end;
  1120. end;
  1121. Dec(X);
  1122. end;
  1123. { UTF-8 failed, so encode the string as plain ASCII }
  1124. if DoASCII then
  1125. begin
  1126. { Reset the flags and Result }
  1127. needs_quoting := False;
  1128. Result := S;
  1129. in_quotes := False;
  1130. add_end_quote := True;
  1131. for X := Length(Result) downto 1 do
  1132. begin
  1133. CurrentChar := Ord(Result[X]);
  1134. { Control characters and extended characters need special handling }
  1135. case CurrentChar of
  1136. $00..$1F, $7F:
  1137. EncodeControlChar(CurrentChar);
  1138. $20..$7E:
  1139. EncodeStandardChar(CurrentChar);
  1140. { Extended characters }
  1141. else
  1142. EncodeExtendedChar(CurrentChar);
  1143. end;
  1144. end;
  1145. end;
  1146. if needs_quoting then
  1147. begin
  1148. if in_quotes then
  1149. Result := '''' + Result;
  1150. if add_end_quote then
  1151. Result := Result + '''';
  1152. end;
  1153. end;
  1154. {$endif DEBUG_NODE_XML}
  1155. finalization
  1156. { Be sure to close the redirect files to flush all data }
  1157. DoneRedirectFile;
  1158. end.