2
0

verbose.pas 43 KB

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