verbose.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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,cobjects,
  25. messages;
  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;
  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 LoadPrefixes;
  220. function loadprefix(w:longint):string;
  221. var
  222. s : string;
  223. idx : longint;
  224. begin
  225. s:=msg^.get(w);
  226. idx:=pos('_',s);
  227. if idx>0 then
  228. LoadPrefix:=Copy(s,idx+1,255)
  229. else
  230. LoadPrefix:=s;
  231. end;
  232. begin
  233. { Load the prefixes }
  234. fatalstr:=LoadPrefix(general_i_fatal);
  235. errorstr:=LoadPrefix(general_i_error);
  236. warningstr:=LoadPrefix(general_i_warning);
  237. notestr:=LoadPrefix(general_i_note);
  238. hintstr:=LoadPrefix(general_i_hint);
  239. end;
  240. procedure LoadMsgFile(const fn:string);
  241. begin
  242. { reload the internal messages if not already loaded }
  243. {$ifndef EXTERN_MSG}
  244. if not msg^.msgintern then
  245. msg^.LoadIntern(@msgtxt,msgtxtsize);
  246. {$endif}
  247. if not msg^.LoadExtern(fn) then
  248. begin
  249. {$ifdef EXTERN_MSG}
  250. writeln('Fatal: Cannot find error message file.');
  251. halt(3);
  252. {$else}
  253. msg^.LoadIntern(@msgtxt,msgtxtsize);
  254. {$endif}
  255. end;
  256. { reload the prefixes using the new messages }
  257. LoadPrefixes;
  258. end;
  259. var
  260. lastfileidx,
  261. lastmoduleidx : longint;
  262. Procedure UpdateStatus;
  263. begin
  264. { fix status }
  265. status.currentline:=aktfilepos.line;
  266. status.currentcolumn:=aktfilepos.column;
  267. if assigned(current_module) and assigned(current_module^.sourcefiles) and
  268. ((current_module^.unit_index<>lastmoduleidx) or
  269. (aktfilepos.fileindex<>lastfileidx)) then
  270. begin
  271. { update status record }
  272. status.currentmodule:=current_module^.modulename^;
  273. status.currentsource:=current_module^.sourcefiles^.get_file_name(aktfilepos.fileindex);
  274. status.currentsourcepath:=current_module^.sourcefiles^.get_file_path(aktfilepos.fileindex);
  275. { update lastfileidx only if name known PM }
  276. if status.currentsource<>'' then
  277. lastfileidx:=aktfilepos.fileindex
  278. else
  279. lastfileidx:=0;
  280. lastmoduleidx:=current_module^.unit_index;
  281. end;
  282. end;
  283. procedure stop;
  284. begin
  285. do_stop{$ifdef FPCPROCVAR}(){$endif};
  286. end;
  287. procedure ShowStatus;
  288. begin
  289. UpdateStatus;
  290. if do_status{$ifdef FPCPROCVAR}(){$endif} then
  291. stop;
  292. end;
  293. function ErrorCount:longint;
  294. begin
  295. ErrorCount:=status.errorcount;
  296. end;
  297. procedure SetErrorFlags(const s:string);
  298. var
  299. code : integer;
  300. i,j,l : longint;
  301. begin
  302. { empty string means error count = 1 for backward compatibility (PFV) }
  303. if s='' then
  304. begin
  305. status.maxerrorcount:=1;
  306. exit;
  307. end;
  308. i:=0;
  309. while (i<length(s)) do
  310. begin
  311. inc(i);
  312. case s[i] of
  313. '0'..'9' :
  314. begin
  315. j:=i;
  316. while (j<=length(s)) and (s[j] in ['0'..'9']) do
  317. inc(j);
  318. val(copy(s,i,j-i),l,code);
  319. if code<>0 then
  320. l:=1;
  321. status.maxerrorcount:=l;
  322. i:=j;
  323. end;
  324. 'w','W' :
  325. status.errorwarning:=true;
  326. 'n','N' :
  327. status.errornote:=true;
  328. 'h','H' :
  329. status.errorhint:=true;
  330. end;
  331. end;
  332. end;
  333. procedure GenerateError;
  334. begin
  335. inc(status.errorcount);
  336. end;
  337. procedure internalerror(i : longint);
  338. begin
  339. UpdateStatus;
  340. do_internalerror(i);
  341. inc(status.errorcount);
  342. stop;
  343. end;
  344. procedure Comment(l:longint;s:string);
  345. var
  346. dostop : boolean;
  347. begin
  348. dostop:=((l and V_Fatal)<>0);
  349. if ((l and V_Error)<>0) or
  350. (status.errorwarning and ((l and V_Warning)<>0)) or
  351. (status.errornote and ((l and V_Note)<>0)) or
  352. (status.errorhint and ((l and V_Hint)<>0)) then
  353. inc(status.errorcount);
  354. { Create status info }
  355. UpdateStatus;
  356. { Fix replacements }
  357. DefaultReplacements(s);
  358. { show comment }
  359. if do_comment(l,s) or dostop then
  360. stop;
  361. if (status.errorcount>=status.maxerrorcount) and not status.skip_error then
  362. begin
  363. Message1(unit_f_errors_in_unit,tostr(status.errorcount));
  364. status.skip_error:=true;
  365. stop;
  366. end;
  367. end;
  368. Procedure Msg2Comment(s:string);
  369. var
  370. idx,i,v : longint;
  371. dostop : boolean;
  372. begin
  373. {Reset}
  374. dostop:=false;
  375. v:=0;
  376. {Parse options}
  377. idx:=pos('_',s);
  378. if idx=0 then
  379. v:=V_Normal
  380. else
  381. if (idx >= 1) And (idx <= 5) then
  382. begin
  383. for i:=1 to idx do
  384. begin
  385. case upcase(s[i]) of
  386. 'F' :
  387. begin
  388. v:=v or V_Fatal;
  389. inc(status.errorcount);
  390. dostop:=true;
  391. end;
  392. 'E' :
  393. begin
  394. v:=v or V_Error;
  395. inc(status.errorcount);
  396. end;
  397. 'O' :
  398. v:=v or V_Normal;
  399. 'W':
  400. begin
  401. v:=v or V_Warning;
  402. if status.errorwarning then
  403. inc(status.errorcount);
  404. end;
  405. 'N' :
  406. begin
  407. v:=v or V_Note;
  408. if status.errornote then
  409. inc(status.errorcount);
  410. end;
  411. 'H' :
  412. begin
  413. v:=v or V_Hint;
  414. if status.errorhint then
  415. inc(status.errorcount);
  416. end;
  417. 'I' :
  418. v:=v or V_Info;
  419. 'L' :
  420. v:=v or V_Status;
  421. 'U' :
  422. v:=v or V_Used;
  423. 'T' :
  424. v:=v or V_Tried;
  425. 'M' :
  426. v:=v or V_Macro;
  427. 'P' :
  428. v:=v or V_Procedure;
  429. 'C' :
  430. v:=v or V_Conditional;
  431. 'D' :
  432. v:=v or V_Debug;
  433. 'B' :
  434. v:=v or V_Declarations;
  435. 'X' :
  436. v:=v or V_Executable;
  437. 'Z' :
  438. v:=v or V_Assem;
  439. 'S' :
  440. dostop:=true;
  441. '_' : ;
  442. end;
  443. end;
  444. end;
  445. Delete(s,1,idx);
  446. { fix status }
  447. UpdateStatus;
  448. { Fix replacements }
  449. DefaultReplacements(s);
  450. { show comment }
  451. if do_comment(v,s) or dostop then
  452. stop;
  453. if (status.errorcount>=status.maxerrorcount) and not status.skip_error then
  454. begin
  455. Message1(unit_f_errors_in_unit,tostr(status.errorcount));
  456. status.skip_error:=true;
  457. stop;
  458. end;
  459. end;
  460. function MessagePchar(w:longint):pchar;
  461. begin
  462. MessagePchar:=msg^.GetPchar(w)
  463. end;
  464. procedure Message(w:longint);
  465. begin
  466. Msg2Comment(msg^.Get(w));
  467. end;
  468. procedure Message1(w:longint;const s1:string);
  469. begin
  470. Msg2Comment(msg^.Get1(w,s1));
  471. end;
  472. procedure Message2(w:longint;const s1,s2:string);
  473. begin
  474. Msg2Comment(msg^.Get2(w,s1,s2));
  475. end;
  476. procedure Message3(w:longint;const s1,s2,s3:string);
  477. begin
  478. Msg2Comment(msg^.Get3(w,s1,s2,s3));
  479. end;
  480. procedure MessagePos(const pos:tfileposinfo;w:longint);
  481. var
  482. oldpos : tfileposinfo;
  483. begin
  484. oldpos:=aktfilepos;
  485. aktfilepos:=pos;
  486. Msg2Comment(msg^.Get(w));
  487. aktfilepos:=oldpos;
  488. end;
  489. procedure MessagePos1(const pos:tfileposinfo;w:longint;const s1:string);
  490. var
  491. oldpos : tfileposinfo;
  492. begin
  493. oldpos:=aktfilepos;
  494. aktfilepos:=pos;
  495. Msg2Comment(msg^.Get1(w,s1));
  496. aktfilepos:=oldpos;
  497. end;
  498. procedure MessagePos2(const pos:tfileposinfo;w:longint;const s1,s2:string);
  499. var
  500. oldpos : tfileposinfo;
  501. begin
  502. oldpos:=aktfilepos;
  503. aktfilepos:=pos;
  504. Msg2Comment(msg^.Get2(w,s1,s2));
  505. aktfilepos:=oldpos;
  506. end;
  507. procedure MessagePos3(const pos:tfileposinfo;w:longint;const s1,s2,s3:string);
  508. var
  509. oldpos : tfileposinfo;
  510. begin
  511. oldpos:=aktfilepos;
  512. aktfilepos:=pos;
  513. Msg2Comment(msg^.Get3(w,s1,s2,s3));
  514. aktfilepos:=oldpos;
  515. end;
  516. procedure InitVerbose;
  517. begin
  518. { Init }
  519. msg:=new(pmessage,Init(20,msgidxmax));
  520. if msg=nil then
  521. begin
  522. writeln('Fatal: MsgIdx Wrong');
  523. halt(3);
  524. end;
  525. {$ifndef EXTERN_MSG}
  526. msg^.LoadIntern(@msgtxt,msgtxtsize);
  527. {$else}
  528. LoadMsgFile(exepath+'errore.msg');
  529. {$endif}
  530. FillChar(Status,sizeof(TCompilerStatus),0);
  531. status.verbosity:=V_Default;
  532. Status.MaxErrorCount:=50;
  533. LoadPrefixes;
  534. end;
  535. procedure DoneVerbose;
  536. begin
  537. if assigned(msg) then
  538. begin
  539. dispose(msg,Done);
  540. msg:=nil;
  541. end;
  542. end;
  543. end.
  544. {
  545. $Log$
  546. Revision 1.6 2000-09-24 21:33:48 peter
  547. * message updates merges
  548. Revision 1.5 2000/09/24 15:06:33 peter
  549. * use defines.inc
  550. Revision 1.4 2000/08/27 16:11:55 peter
  551. * moved some util functions from globals,cobjects to cutils
  552. * splitted files into finput,fmodule
  553. Revision 1.3 2000/08/13 12:54:55 peter
  554. * class member decl wrong then no other error after it
  555. * -vb has now also line numbering
  556. * -vb is also used for interface/implementation different decls and
  557. doesn't list the current function (merged)
  558. Revision 1.2 2000/07/13 11:32:54 michael
  559. + removed logs
  560. }