verbose.pas 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  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, UTF8Char, 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. UTF8Char := CurrentChar and $3F; { The data bits of the continuation byte }
  1099. UTF8Len := 1; { This variable actually holds 1 less than the length }
  1100. { By setting DoASCII to true, it marks the string as 'invalid UTF-8'
  1101. automatically if it reaches the beginning of the string unexpectedly }
  1102. DoASCII := True;
  1103. Dec(X);
  1104. while X > 0 do
  1105. begin
  1106. CurrentChar := Ord(Result[X]);
  1107. case CurrentChar of
  1108. { A standard character here is invalid UTF-8 }
  1109. $00..$7F:
  1110. Break;
  1111. { Another continuation byte }
  1112. $80..$BF:
  1113. begin
  1114. UTF8Char := UTF8Char or ((CurrentChar and $3F) shl (6 * UTF8Len));
  1115. dec(X);
  1116. Inc(UTF8Len);
  1117. if UTF8Len >= 4 then
  1118. { Sequence too long }
  1119. Break;
  1120. end;
  1121. { Lead byte for 2-byte sequences }
  1122. $C2..$DF:
  1123. begin
  1124. if UTF8Len <> 1 then Break;
  1125. UTF8Char := UTF8Char or ((CurrentChar and $1F) shl 6);
  1126. { Check to see if the code is in range and not part of an 'overlong' sequence }
  1127. case UTF8Char of
  1128. $0080..$07FF:
  1129. DoASCII := False;
  1130. else
  1131. { Do nothing - DoASCII is already true }
  1132. end;
  1133. Break;
  1134. end;
  1135. { Lead byte for 3-byte sequences }
  1136. $E0..$EF:
  1137. begin
  1138. if UTF8Len <> 2 then Break;
  1139. UTF8Char := UTF8Char or ((CurrentChar and $0F) shl 12);
  1140. { Check to see if the code is in range and not part of an 'overlong' sequence }
  1141. case UTF8Char of
  1142. $0800..$D7FF, $E000..$FFFF: { $D800..$DFFF is reserved and hence invalid }
  1143. DoASCII := False;
  1144. else
  1145. { Do nothing - DoASCII is already true }
  1146. end;
  1147. Break;
  1148. end;
  1149. { Lead byte for 4-byte sequences }
  1150. $F0..$F4:
  1151. begin
  1152. if UTF8Len <> 3 then Break;
  1153. UTF8Char := UTF8Char or ((CurrentChar and $07) shl 18);
  1154. { Check to see if the code is in range and not part of an 'overlong' sequence }
  1155. case UTF8Char of
  1156. $010000..$10FFFF:
  1157. DoASCII := False;
  1158. else
  1159. { Do nothing - DoASCII is already true }
  1160. end;
  1161. Break;
  1162. end;
  1163. { Invalid character }
  1164. else
  1165. Break;
  1166. end;
  1167. end;
  1168. if DoASCII then
  1169. Break;
  1170. { If all is fine, we don't need to encode any more characters }
  1171. end;
  1172. { Invalid UTF-8 bytes and lead bytes without continuation bytes }
  1173. $C0..$FF:
  1174. begin
  1175. DoASCII := True;
  1176. Break;
  1177. end;
  1178. end;
  1179. Dec(X);
  1180. end;
  1181. { UTF-8 failed, so encode the string as plain ASCII }
  1182. if DoASCII then
  1183. begin
  1184. { Reset the flags and Result }
  1185. needs_quoting := False;
  1186. Result := S;
  1187. in_quotes := False;
  1188. add_end_quote := True;
  1189. for X := Length(Result) downto 1 do
  1190. begin
  1191. CurrentChar := Ord(Result[X]);
  1192. { Control characters and extended characters need special handling }
  1193. case CurrentChar of
  1194. $00..$1F, $7F:
  1195. EncodeControlChar(CurrentChar);
  1196. $20..$7E:
  1197. EncodeStandardChar(CurrentChar);
  1198. { Extended characters }
  1199. else
  1200. EncodeExtendedChar(CurrentChar);
  1201. end;
  1202. end;
  1203. end;
  1204. if needs_quoting then
  1205. begin
  1206. if in_quotes then
  1207. Result := '''' + Result;
  1208. if add_end_quote then
  1209. Result := Result + '''';
  1210. end;
  1211. end;
  1212. {$endif DEBUG_NODE_XML}
  1213. finalization
  1214. { Be sure to close the redirect files to flush all data }
  1215. DoneRedirectFile;
  1216. end.