verbose.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Peter Vreman
  4. This unit handles the verbose management
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit verbose;
  19. {$i defines.inc}
  20. { Don't include messages in the executable }
  21. {.$define EXTERN_MSG}
  22. interface
  23. uses
  24. cutils,
  25. globals,finput,
  26. messages;
  27. {$ifndef EXTERN_MSG}
  28. {$i msgtxt.inc}
  29. {$endif}
  30. {$i msgidx.inc}
  31. Const
  32. { <$10000 will show file and line }
  33. V_None = $0;
  34. V_Fatal = $1;
  35. V_Error = $2;
  36. V_Normal = $4; { doesn't show a text like Error: }
  37. V_Warning = $8;
  38. V_Note = $10;
  39. V_Hint = $20;
  40. V_Macro = $100;
  41. V_Procedure = $200;
  42. V_Conditional = $400;
  43. V_Assem = $800;
  44. V_Declarations = $1000;
  45. V_Info = $10000;
  46. V_Status = $20000;
  47. V_Used = $40000;
  48. V_Tried = $80000;
  49. V_Debug = $100000;
  50. V_Executable = $200000;
  51. V_ShowFile = $ffff;
  52. V_All = longint($ffffffff);
  53. V_Default = V_Fatal + V_Error + V_Normal;
  54. var
  55. msg : pmessage;
  56. const
  57. msgfilename : string = '';
  58. procedure SetRedirectFile(const fn:string);
  59. function SetVerbosity(const s:string):boolean;
  60. procedure SetCompileModule(p:tmodulebase);
  61. procedure Stop;
  62. procedure ShowStatus;
  63. function ErrorCount:longint;
  64. procedure SetErrorFlags(const s:string);
  65. procedure GenerateError;
  66. procedure Internalerror(i:longint);
  67. procedure Comment(l:longint;s:string);
  68. function MessagePchar(w:longint):pchar;
  69. procedure Message(w:longint);
  70. procedure Message1(w:longint;const s1:string);
  71. procedure Message2(w:longint;const s1,s2:string);
  72. procedure Message3(w:longint;const s1,s2,s3:string);
  73. procedure Message4(w:longint;const s1,s2,s3,s4:string);
  74. procedure MessagePos(const pos:tfileposinfo;w:longint);
  75. procedure MessagePos1(const pos:tfileposinfo;w:longint;const s1:string);
  76. procedure MessagePos2(const pos:tfileposinfo;w:longint;const s1,s2:string);
  77. procedure MessagePos3(const pos:tfileposinfo;w:longint;const s1,s2,s3:string);
  78. procedure MessagePos4(const pos:tfileposinfo;w:longint;const s1,s2,s3,s4:string);
  79. procedure InitVerbose;
  80. procedure DoneVerbose;
  81. implementation
  82. uses
  83. comphook;
  84. var
  85. current_module : tmodulebase;
  86. {****************************************************************************
  87. Extra Handlers for default compiler
  88. ****************************************************************************}
  89. procedure DoneRedirectFile;
  90. begin
  91. if status.use_redir then
  92. begin
  93. close(status.redirfile);
  94. status.use_redir:=false;
  95. end;
  96. end;
  97. procedure SetRedirectFile(const fn:string);
  98. begin
  99. assign(status.redirfile,fn);
  100. {$I-}
  101. append(status.redirfile);
  102. if ioresult <> 0 then
  103. rewrite(status.redirfile);
  104. {$I+}
  105. status.use_redir:=(ioresult=0);
  106. end;
  107. function SetVerbosity(const s:string):boolean;
  108. var
  109. m : Longint;
  110. i : Integer;
  111. inverse : boolean;
  112. c : char;
  113. begin
  114. Setverbosity:=false;
  115. val(s,m,i);
  116. if (i=0) and (s<>'') then
  117. status.verbosity:=m
  118. else
  119. begin
  120. i:=1;
  121. while i<=length(s) do
  122. begin
  123. c:=upcase(s[i]);
  124. inverse:=false;
  125. { on/off ? }
  126. if (i<length(s)) then
  127. case s[i+1] of
  128. '-' : begin
  129. inc(i);
  130. inverse:=true;
  131. end;
  132. '+' : inc(i);
  133. end;
  134. { handle switch }
  135. case c of
  136. { Special cases }
  137. 'A' : status.verbosity:=V_All;
  138. '0' : status.verbosity:=V_Default;
  139. 'R' : begin
  140. if inverse then
  141. begin
  142. status.use_gccoutput:=false;
  143. status.use_stderr:=false;
  144. end
  145. else
  146. begin
  147. status.use_gccoutput:=true;
  148. status.use_stderr:=true;
  149. end;
  150. end;
  151. { Normal cases - do an or }
  152. 'E' : if inverse then
  153. status.verbosity:=status.verbosity and (not V_Error)
  154. else
  155. status.verbosity:=status.verbosity or V_Error;
  156. 'I' : if inverse then
  157. status.verbosity:=status.verbosity and (not V_Info)
  158. else
  159. status.verbosity:=status.verbosity or V_Info;
  160. 'W' : if inverse then
  161. status.verbosity:=status.verbosity and (not V_Warning)
  162. else
  163. status.verbosity:=status.verbosity or V_Warning;
  164. 'N' : if inverse then
  165. status.verbosity:=status.verbosity and (not V_Note)
  166. else
  167. status.verbosity:=status.verbosity or V_Note;
  168. 'H' : if inverse then
  169. status.verbosity:=status.verbosity and (not V_Hint)
  170. else
  171. status.verbosity:=status.verbosity or V_Hint;
  172. 'L' : if inverse then
  173. status.verbosity:=status.verbosity and (not V_Status)
  174. else
  175. status.verbosity:=status.verbosity or V_Status;
  176. 'U' : if inverse then
  177. status.verbosity:=status.verbosity and (not V_Used)
  178. else
  179. status.verbosity:=status.verbosity or V_Used;
  180. 'T' : if inverse then
  181. status.verbosity:=status.verbosity and (not V_Tried)
  182. else
  183. status.verbosity:=status.verbosity or V_Tried;
  184. 'M' : if inverse then
  185. status.verbosity:=status.verbosity and (not V_Macro)
  186. else
  187. status.verbosity:=status.verbosity or V_Macro;
  188. 'P' : if inverse then
  189. status.verbosity:=status.verbosity and (not V_Procedure)
  190. else
  191. status.verbosity:=status.verbosity or V_Procedure;
  192. 'C' : if inverse then
  193. status.verbosity:=status.verbosity and (not V_Conditional)
  194. else
  195. status.verbosity:=status.verbosity or V_Conditional;
  196. 'D' : if inverse then
  197. status.verbosity:=status.verbosity and (not V_Debug)
  198. else
  199. status.verbosity:=status.verbosity or V_Debug;
  200. 'B' : if inverse then
  201. status.verbosity:=status.verbosity and (not V_Declarations)
  202. else
  203. status.verbosity:=status.verbosity or V_Declarations;
  204. 'X' : if inverse then
  205. status.verbosity:=status.verbosity and (not V_Executable)
  206. else
  207. status.verbosity:=status.verbosity or V_Executable;
  208. 'Z' : if inverse then
  209. status.verbosity:=status.verbosity and (not V_Assem)
  210. else
  211. status.verbosity:=status.verbosity or V_Assem;
  212. end;
  213. inc(i);
  214. end;
  215. end;
  216. if status.verbosity=0 then
  217. status.verbosity:=V_Default;
  218. setverbosity:=true;
  219. end;
  220. procedure Loadprefixes;
  221. function loadprefix(w:longint):string;
  222. var
  223. s : string;
  224. idx : longint;
  225. begin
  226. s:=msg^.get(w);
  227. idx:=pos('_',s);
  228. if idx>0 then
  229. Loadprefix:=Copy(s,idx+1,255)
  230. else
  231. Loadprefix:=s;
  232. end;
  233. begin
  234. { Load the prefixes }
  235. fatalstr:=Loadprefix(general_i_fatal);
  236. errorstr:=Loadprefix(general_i_error);
  237. warningstr:=Loadprefix(general_i_warning);
  238. notestr:=Loadprefix(general_i_note);
  239. hintstr:=Loadprefix(general_i_hint);
  240. end;
  241. procedure LoadMsgFile(const fn:string);
  242. begin
  243. { reload the internal messages if not already loaded }
  244. {$ifndef EXTERN_MSG}
  245. if not msg^.msgintern then
  246. msg^.LoadIntern(@msgtxt,msgtxtsize);
  247. {$endif}
  248. if not msg^.LoadExtern(fn) then
  249. begin
  250. {$ifdef EXTERN_MSG}
  251. writeln('Fatal: Cannot find error message file.');
  252. halt(3);
  253. {$else}
  254. msg^.LoadIntern(@msgtxt,msgtxtsize);
  255. {$endif}
  256. end;
  257. { reload the prefixes using the new messages }
  258. Loadprefixes;
  259. end;
  260. procedure MaybeLoadMessageFile;
  261. begin
  262. { Load new message file }
  263. if (msgfilename<>'') then
  264. begin
  265. LoadMsgFile(msgfilename);
  266. msgfilename:='';
  267. end;
  268. end;
  269. procedure SetCompileModule(p:tmodulebase);
  270. begin
  271. current_module:=p;
  272. end;
  273. var
  274. lastfileidx,
  275. lastmoduleidx : longint;
  276. Procedure UpdateStatus;
  277. begin
  278. { fix status }
  279. status.currentline:=aktfilepos.line;
  280. status.currentcolumn:=aktfilepos.column;
  281. if assigned(current_module) and
  282. assigned(current_module.sourcefiles) and
  283. ((current_module.unit_index<>lastmoduleidx) or
  284. (aktfilepos.fileindex<>lastfileidx)) then
  285. begin
  286. { update status record }
  287. status.currentmodule:=current_module.modulename^;
  288. status.currentsource:=current_module.sourcefiles.get_file_name(aktfilepos.fileindex);
  289. status.currentsourcepath:=current_module.sourcefiles.get_file_path(aktfilepos.fileindex);
  290. { update lastfileidx only if name known PM }
  291. if status.currentsource<>'' then
  292. lastfileidx:=aktfilepos.fileindex
  293. else
  294. lastfileidx:=0;
  295. lastmoduleidx:=current_module.unit_index;
  296. end;
  297. end;
  298. procedure stop;
  299. begin
  300. do_stop{$ifdef FPCPROCVAR}(){$endif};
  301. end;
  302. procedure ShowStatus;
  303. begin
  304. UpdateStatus;
  305. if do_status{$ifdef FPCPROCVAR}(){$endif} then
  306. stop;
  307. end;
  308. function ErrorCount:longint;
  309. begin
  310. ErrorCount:=status.errorcount;
  311. end;
  312. procedure SetErrorFlags(const s:string);
  313. var
  314. code : integer;
  315. i,j,l : longint;
  316. begin
  317. { empty string means error count = 1 for backward compatibility (PFV) }
  318. if s='' then
  319. begin
  320. status.maxerrorcount:=1;
  321. exit;
  322. end;
  323. i:=0;
  324. while (i<length(s)) do
  325. begin
  326. inc(i);
  327. case s[i] of
  328. '0'..'9' :
  329. begin
  330. j:=i;
  331. while (j<=length(s)) and (s[j] in ['0'..'9']) do
  332. inc(j);
  333. val(copy(s,i,j-i),l,code);
  334. if code<>0 then
  335. l:=1;
  336. status.maxerrorcount:=l;
  337. i:=j;
  338. end;
  339. 'w','W' :
  340. status.errorwarning:=true;
  341. 'n','N' :
  342. status.errornote:=true;
  343. 'h','H' :
  344. status.errorhint:=true;
  345. end;
  346. end;
  347. end;
  348. procedure GenerateError;
  349. begin
  350. inc(status.errorcount);
  351. end;
  352. procedure internalerror(i : longint);
  353. begin
  354. UpdateStatus;
  355. do_internalerror(i);
  356. inc(status.errorcount);
  357. stop;
  358. end;
  359. procedure Comment(l:longint;s:string);
  360. var
  361. dostop : boolean;
  362. begin
  363. dostop:=((l and V_Fatal)<>0);
  364. if ((l and V_Error)<>0) or
  365. (status.errorwarning and ((l and V_Warning)<>0)) or
  366. (status.errornote and ((l and V_Note)<>0)) or
  367. (status.errorhint and ((l and V_Hint)<>0)) then
  368. inc(status.errorcount);
  369. { check verbosity level }
  370. if (status.verbosity and l)<>l then
  371. exit;
  372. { Create status info }
  373. UpdateStatus;
  374. { Fix replacements }
  375. DefaultReplacements(s);
  376. { show comment }
  377. if do_comment(l,s) or dostop then
  378. stop;
  379. if (status.errorcount>=status.maxerrorcount) and not status.skip_error then
  380. begin
  381. Message1(unit_f_errors_in_unit,tostr(status.errorcount));
  382. status.skip_error:=true;
  383. stop;
  384. end;
  385. end;
  386. Procedure Msg2Comment(s:string);
  387. var
  388. idx,i,v : longint;
  389. dostop : boolean;
  390. begin
  391. {Reset}
  392. dostop:=false;
  393. v:=0;
  394. {Parse options}
  395. idx:=pos('_',s);
  396. if idx=0 then
  397. v:=V_Normal
  398. else
  399. if (idx >= 1) And (idx <= 5) then
  400. begin
  401. for i:=1 to idx do
  402. begin
  403. case upcase(s[i]) of
  404. 'F' :
  405. begin
  406. v:=v or V_Fatal;
  407. inc(status.errorcount);
  408. dostop:=true;
  409. end;
  410. 'E' :
  411. begin
  412. v:=v or V_Error;
  413. inc(status.errorcount);
  414. end;
  415. 'O' :
  416. v:=v or V_Normal;
  417. 'W':
  418. begin
  419. v:=v or V_Warning;
  420. if status.errorwarning then
  421. inc(status.errorcount);
  422. end;
  423. 'N' :
  424. begin
  425. v:=v or V_Note;
  426. if status.errornote then
  427. inc(status.errorcount);
  428. end;
  429. 'H' :
  430. begin
  431. v:=v or V_Hint;
  432. if status.errorhint then
  433. inc(status.errorcount);
  434. end;
  435. 'I' :
  436. v:=v or V_Info;
  437. 'L' :
  438. v:=v or V_Status;
  439. 'U' :
  440. v:=v or V_Used;
  441. 'T' :
  442. v:=v or V_Tried;
  443. 'M' :
  444. v:=v or V_Macro;
  445. 'P' :
  446. v:=v or V_Procedure;
  447. 'C' :
  448. v:=v or V_Conditional;
  449. 'D' :
  450. v:=v or V_Debug;
  451. 'B' :
  452. v:=v or V_Declarations;
  453. 'X' :
  454. v:=v or V_Executable;
  455. 'Z' :
  456. v:=v or V_Assem;
  457. 'S' :
  458. dostop:=true;
  459. '_' : ;
  460. end;
  461. end;
  462. end;
  463. Delete(s,1,idx);
  464. { check verbosity level }
  465. if (status.verbosity and v)<>v then
  466. exit;
  467. { fix status }
  468. UpdateStatus;
  469. { Fix replacements }
  470. DefaultReplacements(s);
  471. { show comment }
  472. if do_comment(v,s) or dostop then
  473. stop;
  474. if (status.errorcount>=status.maxerrorcount) and not status.skip_error then
  475. begin
  476. Message1(unit_f_errors_in_unit,tostr(status.errorcount));
  477. status.skip_error:=true;
  478. stop;
  479. end;
  480. end;
  481. function MessagePchar(w:longint):pchar;
  482. begin
  483. MaybeLoadMessageFile;
  484. MessagePchar:=msg^.GetPchar(w)
  485. end;
  486. procedure Message(w:longint);
  487. begin
  488. MaybeLoadMessageFile;
  489. Msg2Comment(msg^.Get(w));
  490. end;
  491. procedure Message1(w:longint;const s1:string);
  492. begin
  493. MaybeLoadMessageFile;
  494. Msg2Comment(msg^.Get1(w,s1));
  495. end;
  496. procedure Message2(w:longint;const s1,s2:string);
  497. begin
  498. MaybeLoadMessageFile;
  499. Msg2Comment(msg^.Get2(w,s1,s2));
  500. end;
  501. procedure Message3(w:longint;const s1,s2,s3:string);
  502. begin
  503. MaybeLoadMessageFile;
  504. Msg2Comment(msg^.Get3(w,s1,s2,s3));
  505. end;
  506. procedure Message4(w:longint;const s1,s2,s3,s4:string);
  507. begin
  508. MaybeLoadMessageFile;
  509. Msg2Comment(msg^.Get4(w,s1,s2,s3,s4));
  510. end;
  511. procedure MessagePos(const pos:tfileposinfo;w:longint);
  512. var
  513. oldpos : tfileposinfo;
  514. begin
  515. oldpos:=aktfilepos;
  516. aktfilepos:=pos;
  517. MaybeLoadMessageFile;
  518. Msg2Comment(msg^.Get(w));
  519. aktfilepos:=oldpos;
  520. end;
  521. procedure MessagePos1(const pos:tfileposinfo;w:longint;const s1:string);
  522. var
  523. oldpos : tfileposinfo;
  524. begin
  525. oldpos:=aktfilepos;
  526. aktfilepos:=pos;
  527. MaybeLoadMessageFile;
  528. Msg2Comment(msg^.Get1(w,s1));
  529. aktfilepos:=oldpos;
  530. end;
  531. procedure MessagePos2(const pos:tfileposinfo;w:longint;const s1,s2:string);
  532. var
  533. oldpos : tfileposinfo;
  534. begin
  535. oldpos:=aktfilepos;
  536. aktfilepos:=pos;
  537. MaybeLoadMessageFile;
  538. Msg2Comment(msg^.Get2(w,s1,s2));
  539. aktfilepos:=oldpos;
  540. end;
  541. procedure MessagePos3(const pos:tfileposinfo;w:longint;const s1,s2,s3:string);
  542. var
  543. oldpos : tfileposinfo;
  544. begin
  545. oldpos:=aktfilepos;
  546. aktfilepos:=pos;
  547. MaybeLoadMessageFile;
  548. Msg2Comment(msg^.Get3(w,s1,s2,s3));
  549. aktfilepos:=oldpos;
  550. end;
  551. procedure MessagePos4(const pos:tfileposinfo;w:longint;const s1,s2,s3,s4:string);
  552. var
  553. oldpos : tfileposinfo;
  554. begin
  555. oldpos:=aktfilepos;
  556. aktfilepos:=pos;
  557. MaybeLoadMessageFile;
  558. Msg2Comment(msg^.Get4(w,s1,s2,s3,s4));
  559. aktfilepos:=oldpos;
  560. end;
  561. procedure InitVerbose;
  562. begin
  563. { Init }
  564. msg:=new(pmessage,Init(20,msgidxmax));
  565. if msg=nil then
  566. begin
  567. writeln('Fatal: MsgIdx Wrong');
  568. halt(3);
  569. end;
  570. {$ifndef EXTERN_MSG}
  571. msg^.LoadIntern(@msgtxt,msgtxtsize);
  572. {$else EXTERN_MSG}
  573. LoadMsgFile(exepath+'errore.msg');
  574. {$endif EXTERN_MSG}
  575. FillChar(Status,sizeof(TCompilerStatus),0);
  576. status.verbosity:=V_Default;
  577. Status.MaxErrorCount:=50;
  578. Loadprefixes;
  579. end;
  580. procedure DoneVerbose;
  581. begin
  582. if assigned(msg) then
  583. begin
  584. dispose(msg,Done);
  585. msg:=nil;
  586. end;
  587. if status.use_redir then
  588. DoneRedirectFile;
  589. end;
  590. end.
  591. {
  592. $Log$
  593. Revision 1.14 2001-05-27 14:30:55 florian
  594. + some widestring stuff added
  595. Revision 1.13 2001/04/13 01:22:17 peter
  596. * symtable change to classes
  597. * range check generation and errors fixed, make cycle DEBUG=1 works
  598. * memory leaks fixed
  599. Revision 1.12 2001/03/13 20:59:56 peter
  600. * message loading fixes from Sergey (merged)
  601. Revision 1.11 2000/12/26 15:58:29 peter
  602. * check for verbosity in verbose instead of comphook
  603. Revision 1.10 2000/12/25 00:07:30 peter
  604. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  605. tlinkedlist objects)
  606. Revision 1.9 2000/12/07 17:19:45 jonas
  607. * new constant handling: from now on, hex constants >$7fffffff are
  608. parsed as unsigned constants (otherwise, $80000000 got sign extended
  609. and became $ffffffff80000000), all constants in the longint range
  610. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  611. are cardinals and the rest are int64's.
  612. * added lots of longint typecast to prevent range check errors in the
  613. compiler and rtl
  614. * type casts of symbolic ordinal constants are now preserved
  615. * fixed bug where the original resulttype wasn't restored correctly
  616. after doing a 64bit rangecheck
  617. Revision 1.8 2000/11/29 00:30:43 florian
  618. * unused units removed from uses clause
  619. * some changes for widestrings
  620. Revision 1.7 2000/10/31 22:02:55 peter
  621. * symtable splitted, no real code changes
  622. Revision 1.6 2000/09/24 21:33:48 peter
  623. * message updates merges
  624. Revision 1.5 2000/09/24 15:06:33 peter
  625. * use defines.inc
  626. Revision 1.4 2000/08/27 16:11:55 peter
  627. * moved some util functions from globals,cobjects to cutils
  628. * splitted files into finput,fmodule
  629. Revision 1.3 2000/08/13 12:54:55 peter
  630. * class member decl wrong then no other error after it
  631. * -vb has now also line numbering
  632. * -vb is also used for interface/implementation different decls and
  633. doesn't list the current function (merged)
  634. Revision 1.2 2000/07/13 11:32:54 michael
  635. + removed logs
  636. }