verbose.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. interface
  20. uses
  21. cutils,cobjects,
  22. messages;
  23. {$ifdef TP}
  24. {$define EXTERN_MSG}
  25. {$endif}
  26. {$ifndef EXTERN_MSG}
  27. {$i msgtxt.inc}
  28. {$endif}
  29. {$i msgidx.inc}
  30. Const
  31. { <$10000 will show file and line }
  32. V_None = $0;
  33. V_Fatal = $1;
  34. V_Error = $2;
  35. V_Normal = $4; { doesn't show a text like Error: }
  36. V_Warning = $8;
  37. V_Note = $10;
  38. V_Hint = $20;
  39. V_Macro = $100;
  40. V_Procedure = $200;
  41. V_Conditional = $400;
  42. V_Assem = $800;
  43. V_Declarations = $1000;
  44. V_Info = $10000;
  45. V_Status = $20000;
  46. V_Used = $40000;
  47. V_Tried = $80000;
  48. V_Debug = $100000;
  49. V_Executable = $200000;
  50. V_ShowFile = $ffff;
  51. V_All = $ffffffff;
  52. V_Default = V_Fatal + V_Error + V_Normal;
  53. var
  54. msg : pmessage;
  55. procedure SetRedirectFile(const fn:string);
  56. function SetVerbosity(const s:string):boolean;
  57. procedure LoadMsgFile(const fn:string);
  58. procedure Stop;
  59. procedure ShowStatus;
  60. function ErrorCount:longint;
  61. procedure SetErrorFlags(const s:string);
  62. procedure GenerateError;
  63. procedure Internalerror(i:longint);
  64. procedure Comment(l:longint;s:string);
  65. function MessagePchar(w:longint):pchar;
  66. procedure Message(w:longint);
  67. procedure Message1(w:longint;const s1:string);
  68. procedure Message2(w:longint;const s1,s2:string);
  69. procedure Message3(w:longint;const s1,s2,s3:string);
  70. procedure MessagePos(const pos:tfileposinfo;w:longint);
  71. procedure MessagePos1(const pos:tfileposinfo;w:longint;const s1:string);
  72. procedure MessagePos2(const pos:tfileposinfo;w:longint;const s1,s2:string);
  73. procedure MessagePos3(const pos:tfileposinfo;w:longint;const s1,s2,s3:string);
  74. procedure InitVerbose;
  75. procedure DoneVerbose;
  76. implementation
  77. uses
  78. fmodule,comphook,
  79. version,globals;
  80. var
  81. redirexitsave : pointer;
  82. {****************************************************************************
  83. Extra Handlers for default compiler
  84. ****************************************************************************}
  85. procedure DoneRedirectFile;{$ifndef FPC}far;{$ENDIF}
  86. begin
  87. exitproc:=redirexitsave;
  88. if status.use_redir then
  89. close(status.redirfile);
  90. end;
  91. procedure SetRedirectFile(const fn:string);
  92. begin
  93. assign(status.redirfile,fn);
  94. {$I-}
  95. append(status.redirfile);
  96. if ioresult <> 0 then
  97. rewrite(status.redirfile);
  98. {$I+}
  99. status.use_redir:=(ioresult=0);
  100. if status.use_redir then
  101. begin
  102. redirexitsave:=exitproc;
  103. exitproc:=@DoneRedirectFile;
  104. end;
  105. end;
  106. function SetVerbosity(const s:string):boolean;
  107. var
  108. m : Longint;
  109. i : Integer;
  110. inverse : boolean;
  111. c : char;
  112. begin
  113. Setverbosity:=false;
  114. val(s,m,i);
  115. if (i=0) and (s<>'') then
  116. status.verbosity:=m
  117. else
  118. begin
  119. i:=1;
  120. while i<=length(s) do
  121. begin
  122. c:=upcase(s[i]);
  123. inverse:=false;
  124. { on/off ? }
  125. if (i<length(s)) then
  126. case s[i+1] of
  127. '-' : begin
  128. inc(i);
  129. inverse:=true;
  130. end;
  131. '+' : inc(i);
  132. end;
  133. { handle switch }
  134. case c of
  135. { Special cases }
  136. 'A' : status.verbosity:=V_All;
  137. '0' : status.verbosity:=V_Default;
  138. 'R' : begin
  139. if inverse then
  140. begin
  141. status.use_gccoutput:=false;
  142. status.use_stderr:=false;
  143. end
  144. else
  145. begin
  146. status.use_gccoutput:=true;
  147. status.use_stderr:=true;
  148. end;
  149. end;
  150. { Normal cases - do an or }
  151. 'E' : if inverse then
  152. status.verbosity:=status.verbosity and (not V_Error)
  153. else
  154. status.verbosity:=status.verbosity or V_Error;
  155. 'I' : if inverse then
  156. status.verbosity:=status.verbosity and (not V_Info)
  157. else
  158. status.verbosity:=status.verbosity or V_Info;
  159. 'W' : if inverse then
  160. status.verbosity:=status.verbosity and (not V_Warning)
  161. else
  162. status.verbosity:=status.verbosity or V_Warning;
  163. 'N' : if inverse then
  164. status.verbosity:=status.verbosity and (not V_Note)
  165. else
  166. status.verbosity:=status.verbosity or V_Note;
  167. 'H' : if inverse then
  168. status.verbosity:=status.verbosity and (not V_Hint)
  169. else
  170. status.verbosity:=status.verbosity or V_Hint;
  171. 'L' : if inverse then
  172. status.verbosity:=status.verbosity and (not V_Status)
  173. else
  174. status.verbosity:=status.verbosity or V_Status;
  175. 'U' : if inverse then
  176. status.verbosity:=status.verbosity and (not V_Used)
  177. else
  178. status.verbosity:=status.verbosity or V_Used;
  179. 'T' : if inverse then
  180. status.verbosity:=status.verbosity and (not V_Tried)
  181. else
  182. status.verbosity:=status.verbosity or V_Tried;
  183. 'M' : if inverse then
  184. status.verbosity:=status.verbosity and (not V_Macro)
  185. else
  186. status.verbosity:=status.verbosity or V_Macro;
  187. 'P' : if inverse then
  188. status.verbosity:=status.verbosity and (not V_Procedure)
  189. else
  190. status.verbosity:=status.verbosity or V_Procedure;
  191. 'C' : if inverse then
  192. status.verbosity:=status.verbosity and (not V_Conditional)
  193. else
  194. status.verbosity:=status.verbosity or V_Conditional;
  195. 'D' : if inverse then
  196. status.verbosity:=status.verbosity and (not V_Debug)
  197. else
  198. status.verbosity:=status.verbosity or V_Debug;
  199. 'B' : if inverse then
  200. status.verbosity:=status.verbosity and (not V_Declarations)
  201. else
  202. status.verbosity:=status.verbosity or V_Declarations;
  203. 'X' : if inverse then
  204. status.verbosity:=status.verbosity and (not V_Executable)
  205. else
  206. status.verbosity:=status.verbosity or V_Executable;
  207. 'Z' : if inverse then
  208. status.verbosity:=status.verbosity and (not V_Assem)
  209. else
  210. status.verbosity:=status.verbosity or V_Assem;
  211. end;
  212. inc(i);
  213. end;
  214. end;
  215. if status.verbosity=0 then
  216. status.verbosity:=V_Default;
  217. setverbosity:=true;
  218. end;
  219. procedure LoadMsgFile(const fn:string);
  220. begin
  221. if not msg^.LoadExtern(fn) then
  222. begin
  223. {$IFDEF TP}
  224. writeln('Fatal: Cannot find error message file.');
  225. halt(3);
  226. {$ELSE}
  227. msg^.LoadIntern(@msgtxt,msgtxtsize);
  228. {$ENDIF TP}
  229. end;
  230. end;
  231. var
  232. lastfileidx,
  233. lastmoduleidx : longint;
  234. Procedure UpdateStatus;
  235. begin
  236. { fix status }
  237. status.currentline:=aktfilepos.line;
  238. status.currentcolumn:=aktfilepos.column;
  239. if assigned(current_module) and assigned(current_module^.sourcefiles) and
  240. ((current_module^.unit_index<>lastmoduleidx) or
  241. (aktfilepos.fileindex<>lastfileidx)) then
  242. begin
  243. { update status record }
  244. status.currentmodule:=current_module^.modulename^;
  245. status.currentsource:=current_module^.sourcefiles^.get_file_name(aktfilepos.fileindex);
  246. status.currentsourcepath:=current_module^.sourcefiles^.get_file_path(aktfilepos.fileindex);
  247. { update lastfileidx only if name known PM }
  248. if status.currentsource<>'' then
  249. lastfileidx:=aktfilepos.fileindex
  250. else
  251. lastfileidx:=0;
  252. lastmoduleidx:=current_module^.unit_index;
  253. end;
  254. end;
  255. procedure stop;
  256. begin
  257. do_stop{$ifdef FPC}(){$endif};
  258. end;
  259. procedure ShowStatus;
  260. begin
  261. UpdateStatus;
  262. if do_status{$ifdef FPC}(){$endif} then
  263. stop;
  264. end;
  265. function ErrorCount:longint;
  266. begin
  267. ErrorCount:=status.errorcount;
  268. end;
  269. procedure SetErrorFlags(const s:string);
  270. var
  271. code : integer;
  272. i,j,l : longint;
  273. begin
  274. { empty string means error count = 1 for backward compatibility (PFV) }
  275. if s='' then
  276. begin
  277. status.maxerrorcount:=1;
  278. exit;
  279. end;
  280. i:=0;
  281. while (i<length(s)) do
  282. begin
  283. inc(i);
  284. case s[i] of
  285. '0'..'9' :
  286. begin
  287. j:=i;
  288. while (j<=length(s)) and (s[j] in ['0'..'9']) do
  289. inc(j);
  290. val(copy(s,i,j-i),l,code);
  291. if code<>0 then
  292. l:=1;
  293. status.maxerrorcount:=l;
  294. i:=j;
  295. end;
  296. 'w','W' :
  297. status.errorwarning:=true;
  298. 'n','N' :
  299. status.errornote:=true;
  300. 'h','H' :
  301. status.errorhint:=true;
  302. end;
  303. end;
  304. end;
  305. procedure GenerateError;
  306. begin
  307. inc(status.errorcount);
  308. end;
  309. procedure internalerror(i : longint);
  310. begin
  311. UpdateStatus;
  312. do_internalerror(i);
  313. inc(status.errorcount);
  314. stop;
  315. end;
  316. procedure Comment(l:longint;s:string);
  317. var
  318. dostop : boolean;
  319. begin
  320. dostop:=((l and V_Fatal)<>0);
  321. if ((l and V_Error)<>0) or
  322. (status.errorwarning and ((l and V_Warning)<>0)) or
  323. (status.errornote and ((l and V_Note)<>0)) or
  324. (status.errorhint and ((l and V_Hint)<>0)) then
  325. inc(status.errorcount);
  326. { Create status info }
  327. UpdateStatus;
  328. { Fix replacements }
  329. DefaultReplacements(s);
  330. { show comment }
  331. if do_comment(l,s) or dostop then
  332. stop;
  333. if (status.errorcount>=status.maxerrorcount) and not status.skip_error then
  334. begin
  335. Message1(unit_f_errors_in_unit,tostr(status.errorcount));
  336. status.skip_error:=true;
  337. stop;
  338. end;
  339. end;
  340. Procedure Msg2Comment(s:string);
  341. var
  342. idx,i,v : longint;
  343. dostop : boolean;
  344. begin
  345. {Reset}
  346. dostop:=false;
  347. v:=0;
  348. {Parse options}
  349. idx:=pos('_',s);
  350. if idx=0 then
  351. v:=V_Normal
  352. else
  353. if (idx >= 1) And (idx <= 5) then
  354. begin
  355. for i:=1 to idx do
  356. begin
  357. case upcase(s[i]) of
  358. 'F' :
  359. begin
  360. v:=v or V_Fatal;
  361. inc(status.errorcount);
  362. dostop:=true;
  363. end;
  364. 'E' :
  365. begin
  366. v:=v or V_Error;
  367. inc(status.errorcount);
  368. end;
  369. 'O' :
  370. v:=v or V_Normal;
  371. 'W':
  372. begin
  373. v:=v or V_Warning;
  374. if status.errorwarning then
  375. inc(status.errorcount);
  376. end;
  377. 'N' :
  378. begin
  379. v:=v or V_Note;
  380. if status.errornote then
  381. inc(status.errorcount);
  382. end;
  383. 'H' :
  384. begin
  385. v:=v or V_Hint;
  386. if status.errorhint then
  387. inc(status.errorcount);
  388. end;
  389. 'I' :
  390. v:=v or V_Info;
  391. 'L' :
  392. v:=v or V_Status;
  393. 'U' :
  394. v:=v or V_Used;
  395. 'T' :
  396. v:=v or V_Tried;
  397. 'M' :
  398. v:=v or V_Macro;
  399. 'P' :
  400. v:=v or V_Procedure;
  401. 'C' :
  402. v:=v or V_Conditional;
  403. 'D' :
  404. v:=v or V_Debug;
  405. 'B' :
  406. v:=v or V_Declarations;
  407. 'X' :
  408. v:=v or V_Executable;
  409. 'Z' :
  410. v:=v or V_Assem;
  411. 'S' :
  412. dostop:=true;
  413. '_' : ;
  414. end;
  415. end;
  416. end;
  417. Delete(s,1,idx);
  418. { fix status }
  419. UpdateStatus;
  420. { Fix replacements }
  421. DefaultReplacements(s);
  422. { show comment }
  423. if do_comment(v,s) or dostop then
  424. stop;
  425. if (status.errorcount>=status.maxerrorcount) and not status.skip_error then
  426. begin
  427. Message1(unit_f_errors_in_unit,tostr(status.errorcount));
  428. status.skip_error:=true;
  429. stop;
  430. end;
  431. end;
  432. function MessagePchar(w:longint):pchar;
  433. begin
  434. MessagePchar:=msg^.GetPchar(w)
  435. end;
  436. procedure Message(w:longint);
  437. begin
  438. Msg2Comment(msg^.Get(w));
  439. end;
  440. procedure Message1(w:longint;const s1:string);
  441. begin
  442. Msg2Comment(msg^.Get1(w,s1));
  443. end;
  444. procedure Message2(w:longint;const s1,s2:string);
  445. begin
  446. Msg2Comment(msg^.Get2(w,s1,s2));
  447. end;
  448. procedure Message3(w:longint;const s1,s2,s3:string);
  449. begin
  450. Msg2Comment(msg^.Get3(w,s1,s2,s3));
  451. end;
  452. procedure MessagePos(const pos:tfileposinfo;w:longint);
  453. var
  454. oldpos : tfileposinfo;
  455. begin
  456. oldpos:=aktfilepos;
  457. aktfilepos:=pos;
  458. Msg2Comment(msg^.Get(w));
  459. aktfilepos:=oldpos;
  460. end;
  461. procedure MessagePos1(const pos:tfileposinfo;w:longint;const s1:string);
  462. var
  463. oldpos : tfileposinfo;
  464. begin
  465. oldpos:=aktfilepos;
  466. aktfilepos:=pos;
  467. Msg2Comment(msg^.Get1(w,s1));
  468. aktfilepos:=oldpos;
  469. end;
  470. procedure MessagePos2(const pos:tfileposinfo;w:longint;const s1,s2:string);
  471. var
  472. oldpos : tfileposinfo;
  473. begin
  474. oldpos:=aktfilepos;
  475. aktfilepos:=pos;
  476. Msg2Comment(msg^.Get2(w,s1,s2));
  477. aktfilepos:=oldpos;
  478. end;
  479. procedure MessagePos3(const pos:tfileposinfo;w:longint;const s1,s2,s3:string);
  480. var
  481. oldpos : tfileposinfo;
  482. begin
  483. oldpos:=aktfilepos;
  484. aktfilepos:=pos;
  485. Msg2Comment(msg^.Get3(w,s1,s2,s3));
  486. aktfilepos:=oldpos;
  487. end;
  488. procedure InitVerbose;
  489. begin
  490. { Init }
  491. msg:=new(pmessage,Init(20,msgidxmax));
  492. if msg=nil then
  493. begin
  494. writeln('Fatal: MsgIdx Wrong');
  495. halt(3);
  496. end;
  497. {$ifndef EXTERN_MSG}
  498. msg^.LoadIntern(@msgtxt,msgtxtsize);
  499. {$else}
  500. LoadMsgFile(exepath+'errore.msg');
  501. {$endif}
  502. FillChar(Status,sizeof(TCompilerStatus),0);
  503. status.verbosity:=V_Default;
  504. Status.MaxErrorCount:=50;
  505. end;
  506. procedure DoneVerbose;
  507. begin
  508. if assigned(msg) then
  509. begin
  510. dispose(msg,Done);
  511. msg:=nil;
  512. end;
  513. end;
  514. end.
  515. {
  516. $Log$
  517. Revision 1.4 2000-08-27 16:11:55 peter
  518. * moved some util functions from globals,cobjects to cutils
  519. * splitted files into finput,fmodule
  520. Revision 1.3 2000/08/13 12:54:55 peter
  521. * class member decl wrong then no other error after it
  522. * -vb has now also line numbering
  523. * -vb is also used for interface/implementation different decls and
  524. doesn't list the current function (merged)
  525. Revision 1.2 2000/07/13 11:32:54 michael
  526. + removed logs
  527. }