verbose.pas 43 KB

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