verbose.pas 40 KB

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