verbose.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 fpcdefs.inc}
  20. { Don't include messages in the executable }
  21. {$ifdef Delphi}
  22. {$define EXTERN_MSG}
  23. {$endif}
  24. interface
  25. uses
  26. cutils,
  27. globals,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. const
  59. msgfilename : string = '';
  60. procedure SetRedirectFile(const fn:string);
  61. function SetVerbosity(const s:string):boolean;
  62. procedure PrepareReport;
  63. function CheckVerbosity(v:longint):boolean;
  64. procedure SetCompileModule(p:tmodulebase);
  65. procedure Stop;
  66. procedure ShowStatus;
  67. function ErrorCount:longint;
  68. procedure SetErrorFlags(const s:string);
  69. procedure GenerateError;
  70. procedure Internalerror(i:longint);
  71. procedure Comment(l:longint;s:string);
  72. function MessagePchar(w:longint):pchar;
  73. procedure Message(w:longint);
  74. procedure Message1(w:longint;const s1:string);
  75. procedure Message2(w:longint;const s1,s2:string);
  76. procedure Message3(w:longint;const s1,s2,s3:string);
  77. procedure Message4(w:longint;const s1,s2,s3,s4:string);
  78. procedure MessagePos(const pos:tfileposinfo;w:longint);
  79. procedure MessagePos1(const pos:tfileposinfo;w:longint;const s1:string);
  80. procedure MessagePos2(const pos:tfileposinfo;w:longint;const s1,s2:string);
  81. procedure MessagePos3(const pos:tfileposinfo;w:longint;const s1,s2,s3:string);
  82. procedure MessagePos4(const pos:tfileposinfo;w:longint;const s1,s2,s3,s4:string);
  83. procedure InitVerbose;
  84. procedure DoneVerbose;
  85. implementation
  86. uses
  87. comphook;
  88. var
  89. compiling_module : tmodulebase;
  90. {****************************************************************************
  91. Extra Handlers for default compiler
  92. ****************************************************************************}
  93. procedure DoneRedirectFile;
  94. begin
  95. if status.use_redir then
  96. begin
  97. close(status.redirfile);
  98. status.use_redir:=false;
  99. end;
  100. if status.use_bugreport then
  101. begin
  102. close(status.reportbugfile);
  103. status.use_bugreport:=false;
  104. end;
  105. end;
  106. procedure SetRedirectFile(const fn:string);
  107. begin
  108. assign(status.redirfile,fn);
  109. {$I-}
  110. append(status.redirfile);
  111. if ioresult <> 0 then
  112. rewrite(status.redirfile);
  113. {$I+}
  114. status.use_redir:=(ioresult=0);
  115. end;
  116. procedure PrepareReport;
  117. var
  118. fn : string;
  119. begin
  120. if status.use_bugreport then
  121. exit;
  122. fn:='fpcdebug.txt';
  123. assign(status.reportbugfile,fn);
  124. {$I-}
  125. append(status.reportbugfile);
  126. if ioresult <> 0 then
  127. rewrite(status.reportbugfile);
  128. {$I+}
  129. status.use_bugreport:=(ioresult=0);
  130. if status.use_bugreport then
  131. writeln(status.reportbugfile,'FPC bug report file');
  132. end;
  133. function CheckVerbosity(v:longint):boolean;
  134. begin
  135. CheckVerbosity:=status.use_bugreport or
  136. ((status.verbosity and (v and V_LevelMask))=(v and V_LevelMask));
  137. end;
  138. function SetVerbosity(const s:string):boolean;
  139. var
  140. m : Longint;
  141. i : Integer;
  142. inverse : boolean;
  143. c : char;
  144. begin
  145. Setverbosity:=false;
  146. val(s,m,i);
  147. if (i=0) and (s<>'') then
  148. status.verbosity:=m
  149. else
  150. begin
  151. i:=1;
  152. while i<=length(s) do
  153. begin
  154. c:=upcase(s[i]);
  155. inverse:=false;
  156. { on/off ? }
  157. if (i<length(s)) then
  158. case s[i+1] of
  159. '-' : begin
  160. inc(i);
  161. inverse:=true;
  162. end;
  163. '+' : inc(i);
  164. end;
  165. { handle switch }
  166. case c of
  167. { Special cases }
  168. 'A' : status.verbosity:=V_All;
  169. '0' : status.verbosity:=V_Default;
  170. 'R' : begin
  171. if inverse then
  172. begin
  173. status.use_gccoutput:=false;
  174. status.use_stderr:=false;
  175. end
  176. else
  177. begin
  178. status.use_gccoutput:=true;
  179. status.use_stderr:=true;
  180. end;
  181. end;
  182. { Normal cases - do an or }
  183. 'E' : if inverse then
  184. status.verbosity:=status.verbosity and (not V_Error)
  185. else
  186. status.verbosity:=status.verbosity or V_Error;
  187. 'I' : if inverse then
  188. status.verbosity:=status.verbosity and (not V_Info)
  189. else
  190. status.verbosity:=status.verbosity or V_Info;
  191. 'W' : if inverse then
  192. status.verbosity:=status.verbosity and (not V_Warning)
  193. else
  194. status.verbosity:=status.verbosity or V_Warning;
  195. 'N' : if inverse then
  196. status.verbosity:=status.verbosity and (not V_Note)
  197. else
  198. status.verbosity:=status.verbosity or V_Note;
  199. 'H' : if inverse then
  200. status.verbosity:=status.verbosity and (not V_Hint)
  201. else
  202. status.verbosity:=status.verbosity or V_Hint;
  203. 'L' : if inverse then
  204. status.verbosity:=status.verbosity and (not V_Status)
  205. else
  206. status.verbosity:=status.verbosity or V_Status;
  207. 'U' : if inverse then
  208. status.verbosity:=status.verbosity and (not V_Used)
  209. else
  210. status.verbosity:=status.verbosity or V_Used;
  211. 'T' : if inverse then
  212. status.verbosity:=status.verbosity and (not V_Tried)
  213. else
  214. status.verbosity:=status.verbosity or V_Tried;
  215. 'C' : if inverse then
  216. status.verbosity:=status.verbosity and (not V_Conditional)
  217. else
  218. status.verbosity:=status.verbosity or V_Conditional;
  219. 'D' : if inverse then
  220. status.verbosity:=status.verbosity and (not V_Debug)
  221. else
  222. status.verbosity:=status.verbosity or V_Debug;
  223. 'X' : if inverse then
  224. status.verbosity:=status.verbosity and (not V_Executable)
  225. else
  226. status.verbosity:=status.verbosity or V_Executable;
  227. end;
  228. inc(i);
  229. end;
  230. end;
  231. if status.verbosity=0 then
  232. status.verbosity:=V_Default;
  233. setverbosity:=true;
  234. end;
  235. procedure Loadprefixes;
  236. function loadprefix(w:longint):string;
  237. var
  238. s : string;
  239. idx : longint;
  240. begin
  241. s:=msg^.get(w);
  242. idx:=pos('_',s);
  243. if idx>0 then
  244. Loadprefix:=Copy(s,idx+1,255)
  245. else
  246. Loadprefix:=s;
  247. end;
  248. begin
  249. { Load the prefixes }
  250. fatalstr:=Loadprefix(general_i_fatal);
  251. errorstr:=Loadprefix(general_i_error);
  252. warningstr:=Loadprefix(general_i_warning);
  253. notestr:=Loadprefix(general_i_note);
  254. hintstr:=Loadprefix(general_i_hint);
  255. end;
  256. procedure LoadMsgFile(const fn:string);
  257. begin
  258. { reload the internal messages if not already loaded }
  259. {$ifndef EXTERN_MSG}
  260. if not msg^.msgintern then
  261. msg^.LoadIntern(@msgtxt,msgtxtsize);
  262. {$endif}
  263. if not msg^.LoadExtern(fn) then
  264. begin
  265. {$ifdef EXTERN_MSG}
  266. writeln('Fatal: Cannot find error message file.');
  267. halt(3);
  268. {$else}
  269. msg^.LoadIntern(@msgtxt,msgtxtsize);
  270. {$endif}
  271. end;
  272. { reload the prefixes using the new messages }
  273. Loadprefixes;
  274. end;
  275. procedure MaybeLoadMessageFile;
  276. begin
  277. { Load new message file }
  278. if (msgfilename<>'') then
  279. begin
  280. LoadMsgFile(msgfilename);
  281. msgfilename:='';
  282. end;
  283. end;
  284. procedure SetCompileModule(p:tmodulebase);
  285. begin
  286. compiling_module:=p;
  287. end;
  288. var
  289. lastfileidx,
  290. lastmoduleidx : longint;
  291. Procedure UpdateStatus;
  292. begin
  293. { fix status }
  294. status.currentline:=aktfilepos.line;
  295. status.currentcolumn:=aktfilepos.column;
  296. if assigned(compiling_module) and
  297. assigned(compiling_module.sourcefiles) and
  298. ((compiling_module.unit_index<>lastmoduleidx) or
  299. (aktfilepos.fileindex<>lastfileidx)) then
  300. begin
  301. { update status record }
  302. status.currentmodule:=compiling_module.modulename^;
  303. status.currentsource:=compiling_module.sourcefiles.get_file_name(aktfilepos.fileindex);
  304. status.currentsourcepath:=compiling_module.sourcefiles.get_file_path(aktfilepos.fileindex);
  305. { update lastfileidx only if name known PM }
  306. if status.currentsource<>'' then
  307. lastfileidx:=aktfilepos.fileindex
  308. else
  309. lastfileidx:=0;
  310. lastmoduleidx:=compiling_module.unit_index;
  311. end;
  312. if assigned(compiling_module) then
  313. status.compiling_current:=(compiling_module.state in [ms_compile,ms_second_compile]);
  314. end;
  315. procedure stop;
  316. begin
  317. do_stop{$ifdef FPCPROCVAR}(){$endif};
  318. end;
  319. procedure ShowStatus;
  320. begin
  321. UpdateStatus;
  322. if do_status{$ifdef FPCPROCVAR}(){$endif} then
  323. stop;
  324. end;
  325. function ErrorCount:longint;
  326. begin
  327. ErrorCount:=status.errorcount;
  328. end;
  329. procedure SetErrorFlags(const s:string);
  330. var
  331. code : integer;
  332. i,j,l : longint;
  333. begin
  334. { empty string means error count = 1 for backward compatibility (PFV) }
  335. if s='' then
  336. begin
  337. status.maxerrorcount:=1;
  338. exit;
  339. end;
  340. i:=0;
  341. while (i<length(s)) do
  342. begin
  343. inc(i);
  344. case s[i] of
  345. '0'..'9' :
  346. begin
  347. j:=i;
  348. while (j<=length(s)) and (s[j] in ['0'..'9']) do
  349. inc(j);
  350. val(copy(s,i,j-i),l,code);
  351. if code<>0 then
  352. l:=1;
  353. status.maxerrorcount:=l;
  354. i:=j;
  355. end;
  356. 'w','W' :
  357. status.errorwarning:=true;
  358. 'n','N' :
  359. status.errornote:=true;
  360. 'h','H' :
  361. status.errorhint:=true;
  362. end;
  363. end;
  364. end;
  365. procedure GenerateError;
  366. begin
  367. inc(status.errorcount);
  368. end;
  369. procedure internalerror(i : longint);
  370. begin
  371. UpdateStatus;
  372. do_internalerror(i);
  373. inc(status.errorcount);
  374. stop;
  375. end;
  376. procedure Comment(l:longint;s:string);
  377. var
  378. dostop : boolean;
  379. begin
  380. dostop:=((l and V_Fatal)<>0);
  381. if ((l and V_Error)<>0) or
  382. (status.errorwarning and ((l and V_Warning)<>0)) or
  383. (status.errornote and ((l and V_Note)<>0)) or
  384. (status.errorhint and ((l and V_Hint)<>0)) then
  385. inc(status.errorcount);
  386. { check verbosity level }
  387. if not CheckVerbosity(l) then
  388. exit;
  389. if (l and V_LineInfoMask)<>0 then
  390. l:=l or V_LineInfo;
  391. { Create status info }
  392. UpdateStatus;
  393. { Fix replacements }
  394. DefaultReplacements(s);
  395. { show comment }
  396. if do_comment(l,s) or dostop then
  397. stop;
  398. if (status.errorcount>=status.maxerrorcount) and not status.skip_error then
  399. begin
  400. Message1(unit_f_errors_in_unit,tostr(status.errorcount));
  401. status.skip_error:=true;
  402. stop;
  403. end;
  404. end;
  405. Procedure Msg2Comment(s:string);
  406. var
  407. idx,i,v : longint;
  408. dostop : boolean;
  409. begin
  410. {Reset}
  411. dostop:=false;
  412. v:=0;
  413. {Parse options}
  414. idx:=pos('_',s);
  415. if idx=0 then
  416. v:=V_Normal
  417. else
  418. if (idx >= 1) And (idx <= 5) then
  419. begin
  420. for i:=1 to idx do
  421. begin
  422. case upcase(s[i]) of
  423. 'F' :
  424. begin
  425. v:=v or V_Fatal;
  426. inc(status.errorcount);
  427. dostop:=true;
  428. end;
  429. 'E' :
  430. begin
  431. v:=v or V_Error;
  432. inc(status.errorcount);
  433. end;
  434. 'O' :
  435. v:=v or V_Normal;
  436. 'W':
  437. begin
  438. v:=v or V_Warning;
  439. if status.errorwarning then
  440. inc(status.errorcount);
  441. end;
  442. 'N' :
  443. begin
  444. v:=v or V_Note;
  445. if status.errornote then
  446. inc(status.errorcount);
  447. end;
  448. 'H' :
  449. begin
  450. v:=v or V_Hint;
  451. if status.errorhint then
  452. inc(status.errorcount);
  453. end;
  454. 'I' :
  455. v:=v or V_Info;
  456. 'L' :
  457. v:=v or V_LineInfo;
  458. 'U' :
  459. v:=v or V_Used;
  460. 'T' :
  461. v:=v or V_Tried;
  462. 'C' :
  463. v:=v or V_Conditional;
  464. 'D' :
  465. v:=v or V_Debug;
  466. 'X' :
  467. v:=v or V_Executable;
  468. 'S' :
  469. dostop:=true;
  470. '_' : ;
  471. end;
  472. end;
  473. end;
  474. Delete(s,1,idx);
  475. { check verbosity level }
  476. if not CheckVerbosity(v) then
  477. exit;
  478. if (v and V_LineInfoMask)<>0 then
  479. v:=v or V_LineInfo;
  480. { fix status }
  481. UpdateStatus;
  482. { Fix replacements }
  483. DefaultReplacements(s);
  484. { show comment }
  485. if do_comment(v,s) or dostop then
  486. stop;
  487. if (status.errorcount>=status.maxerrorcount) and not status.skip_error then
  488. begin
  489. Message1(unit_f_errors_in_unit,tostr(status.errorcount));
  490. status.skip_error:=true;
  491. stop;
  492. end;
  493. end;
  494. function MessagePchar(w:longint):pchar;
  495. begin
  496. MaybeLoadMessageFile;
  497. MessagePchar:=msg^.GetPchar(w)
  498. end;
  499. procedure Message(w:longint);
  500. begin
  501. MaybeLoadMessageFile;
  502. Msg2Comment(msg^.Get(w));
  503. end;
  504. procedure Message1(w:longint;const s1:string);
  505. begin
  506. MaybeLoadMessageFile;
  507. Msg2Comment(msg^.Get1(w,s1));
  508. end;
  509. procedure Message2(w:longint;const s1,s2:string);
  510. begin
  511. MaybeLoadMessageFile;
  512. Msg2Comment(msg^.Get2(w,s1,s2));
  513. end;
  514. procedure Message3(w:longint;const s1,s2,s3:string);
  515. begin
  516. MaybeLoadMessageFile;
  517. Msg2Comment(msg^.Get3(w,s1,s2,s3));
  518. end;
  519. procedure Message4(w:longint;const s1,s2,s3,s4:string);
  520. begin
  521. MaybeLoadMessageFile;
  522. Msg2Comment(msg^.Get4(w,s1,s2,s3,s4));
  523. end;
  524. procedure MessagePos(const pos:tfileposinfo;w:longint);
  525. var
  526. oldpos : tfileposinfo;
  527. begin
  528. oldpos:=aktfilepos;
  529. aktfilepos:=pos;
  530. MaybeLoadMessageFile;
  531. Msg2Comment(msg^.Get(w));
  532. aktfilepos:=oldpos;
  533. end;
  534. procedure MessagePos1(const pos:tfileposinfo;w:longint;const s1:string);
  535. var
  536. oldpos : tfileposinfo;
  537. begin
  538. oldpos:=aktfilepos;
  539. aktfilepos:=pos;
  540. MaybeLoadMessageFile;
  541. Msg2Comment(msg^.Get1(w,s1));
  542. aktfilepos:=oldpos;
  543. end;
  544. procedure MessagePos2(const pos:tfileposinfo;w:longint;const s1,s2:string);
  545. var
  546. oldpos : tfileposinfo;
  547. begin
  548. oldpos:=aktfilepos;
  549. aktfilepos:=pos;
  550. MaybeLoadMessageFile;
  551. Msg2Comment(msg^.Get2(w,s1,s2));
  552. aktfilepos:=oldpos;
  553. end;
  554. procedure MessagePos3(const pos:tfileposinfo;w:longint;const s1,s2,s3:string);
  555. var
  556. oldpos : tfileposinfo;
  557. begin
  558. oldpos:=aktfilepos;
  559. aktfilepos:=pos;
  560. MaybeLoadMessageFile;
  561. Msg2Comment(msg^.Get3(w,s1,s2,s3));
  562. aktfilepos:=oldpos;
  563. end;
  564. procedure MessagePos4(const pos:tfileposinfo;w:longint;const s1,s2,s3,s4:string);
  565. var
  566. oldpos : tfileposinfo;
  567. begin
  568. oldpos:=aktfilepos;
  569. aktfilepos:=pos;
  570. MaybeLoadMessageFile;
  571. Msg2Comment(msg^.Get4(w,s1,s2,s3,s4));
  572. aktfilepos:=oldpos;
  573. end;
  574. procedure InitVerbose;
  575. begin
  576. { Init }
  577. msg:=new(pmessage,Init(20,msgidxmax));
  578. if msg=nil then
  579. begin
  580. writeln('Fatal: MsgIdx Wrong');
  581. halt(3);
  582. end;
  583. {$ifndef EXTERN_MSG}
  584. msg^.LoadIntern(@msgtxt,msgtxtsize);
  585. {$else EXTERN_MSG}
  586. LoadMsgFile(exepath+'errore.msg');
  587. {$endif EXTERN_MSG}
  588. FillChar(Status,sizeof(TCompilerStatus),0);
  589. status.verbosity:=V_Default;
  590. Status.MaxErrorCount:=50;
  591. Loadprefixes;
  592. lastfileidx:=-1;
  593. lastmoduleidx:=-1;
  594. status.currentmodule:='';
  595. status.currentsource:='';
  596. status.currentsourcepath:='';
  597. status.compiling_current:=false;
  598. end;
  599. procedure DoneVerbose;
  600. begin
  601. if assigned(msg) then
  602. begin
  603. dispose(msg,Done);
  604. msg:=nil;
  605. end;
  606. DoneRedirectFile;
  607. end;
  608. finalization
  609. { Be sure to close the redirect files to flush all data }
  610. DoneRedirectFile;
  611. end.
  612. {
  613. $Log$
  614. Revision 1.24 2003-01-09 21:52:38 peter
  615. * merged some verbosity options.
  616. * V_LineInfo is a verbosity flag to include line info
  617. Revision 1.23 2002/12/29 14:57:50 peter
  618. * unit loading changed to first register units and load them
  619. afterwards. This is needed to support uses xxx in yyy correctly
  620. * unit dependency check fixed
  621. Revision 1.22 2002/11/15 01:58:54 peter
  622. * merged changes from 1.0.7 up to 04-11
  623. - -V option for generating bug report tracing
  624. - more tracing for option parsing
  625. - errors for cdecl and high()
  626. - win32 import stabs
  627. - win32 records<=8 are returned in eax:edx (turned off by default)
  628. - heaptrc update
  629. - more info for temp management in .s file with EXTDEBUG
  630. Revision 1.21 2002/10/05 12:43:29 carl
  631. * fixes for Delphi 6 compilation
  632. (warning : Some features do not work under Delphi)
  633. Revision 1.20 2002/08/18 19:59:03 peter
  634. * renamed local current_module to compiling_module because it
  635. confused a lot in gdb
  636. Revision 1.19 2002/05/18 13:34:21 peter
  637. * readded missing revisions
  638. Revision 1.18 2002/05/16 19:46:47 carl
  639. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  640. + try to fix temp allocation (still in ifdef)
  641. + generic constructor calls
  642. + start of tassembler / tmodulebase class cleanup
  643. }