verbose.pas 21 KB

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