verbose.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. messages,cobjects;
  22. {$ifdef TP}
  23. {$define EXTERN_MSG}
  24. {$endif}
  25. {$ifndef EXTERN_MSG}
  26. {$i msgtxt.inc}
  27. {$endif}
  28. {$i msgidx.inc}
  29. Const
  30. { <$10000 will show file and line }
  31. V_None = $0;
  32. V_Fatal = $1;
  33. V_Error = $2;
  34. V_Normal = $4; { doesn't show a text like Error: }
  35. V_Warning = $8;
  36. V_Note = $10;
  37. V_Hint = $20;
  38. V_Macro = $100;
  39. V_Procedure = $200;
  40. V_Conditional = $400;
  41. V_Assem = $800;
  42. V_Info = $10000;
  43. V_Status = $20000;
  44. V_Used = $40000;
  45. V_Tried = $80000;
  46. V_Debug = $100000;
  47. V_Declarations = $200000;
  48. V_Executable = $400000;
  49. V_ShowFile = $ffff;
  50. V_All = $ffffffff;
  51. V_Default = V_Fatal + V_Error + V_Normal;
  52. var
  53. msg : pmessage;
  54. procedure SetRedirectFile(const fn:string);
  55. function SetVerbosity(const s:string):boolean;
  56. procedure LoadMsgFile(const fn:string);
  57. procedure Stop;
  58. procedure ShowStatus;
  59. function ErrorCount:longint;
  60. procedure SetMaxErrorCount(count:longint);
  61. procedure GenerateError;
  62. procedure Internalerror(i:longint);
  63. procedure Comment(l:longint;s:string);
  64. procedure Message(w:tmsgconst);
  65. procedure Message1(w:tmsgconst;const s1:string);
  66. procedure Message2(w:tmsgconst;const s1,s2:string);
  67. procedure Message3(w:tmsgconst;const s1,s2,s3:string);
  68. procedure MessagePos(const pos:tfileposinfo;w:tmsgconst);
  69. procedure MessagePos1(const pos:tfileposinfo;w:tmsgconst;const s1:string);
  70. procedure MessagePos2(const pos:tfileposinfo;w:tmsgconst;const s1,s2:string);
  71. procedure MessagePos3(const pos:tfileposinfo;w:tmsgconst;const s1,s2,s3:string);
  72. procedure InitVerbose;
  73. procedure DoneVerbose;
  74. implementation
  75. uses
  76. files,comphook,
  77. version,globals;
  78. var
  79. redirexitsave : pointer;
  80. {****************************************************************************
  81. Extra Handlers for default compiler
  82. ****************************************************************************}
  83. procedure DoneRedirectFile;{$ifndef FPC}far;{$ENDIF}
  84. begin
  85. exitproc:=redirexitsave;
  86. if status.use_redir then
  87. close(status.redirfile);
  88. end;
  89. procedure SetRedirectFile(const fn:string);
  90. begin
  91. assign(status.redirfile,fn);
  92. {$I-}
  93. rewrite(status.redirfile);
  94. {$I+}
  95. status.use_redir:=(ioresult=0);
  96. if status.use_redir then
  97. begin
  98. redirexitsave:=exitproc;
  99. exitproc:=@DoneRedirectFile;
  100. end;
  101. end;
  102. function SetVerbosity(const s:string):boolean;
  103. var
  104. m : Longint;
  105. i : Integer;
  106. inverse : boolean;
  107. c : char;
  108. begin
  109. Setverbosity:=false;
  110. val(s,m,i);
  111. if (i=0) and (s<>'') then
  112. status.verbosity:=m
  113. else
  114. begin
  115. i:=1;
  116. while i<=length(s) do
  117. begin
  118. c:=upcase(s[i]);
  119. inverse:=false;
  120. { on/off ? }
  121. if (i<length(s)) then
  122. case s[i+1] of
  123. '-' : begin
  124. inc(i);
  125. inverse:=true;
  126. end;
  127. '+' : inc(i);
  128. end;
  129. { handle switch }
  130. case c of
  131. { Special cases }
  132. 'A' : status.verbosity:=V_All;
  133. '0' : status.verbosity:=V_Default;
  134. 'R' : begin
  135. if inverse then
  136. begin
  137. status.use_gccoutput:=false;
  138. status.use_stderr:=false;
  139. end
  140. else
  141. begin
  142. status.use_gccoutput:=true;
  143. status.use_stderr:=true;
  144. end;
  145. end;
  146. { Normal cases - do an or }
  147. 'E' : if inverse then
  148. status.verbosity:=status.verbosity and (not V_Error)
  149. else
  150. status.verbosity:=status.verbosity or V_Error;
  151. 'I' : if inverse then
  152. status.verbosity:=status.verbosity and (not V_Info)
  153. else
  154. status.verbosity:=status.verbosity or V_Info;
  155. 'W' : if inverse then
  156. status.verbosity:=status.verbosity and (not V_Warning)
  157. else
  158. status.verbosity:=status.verbosity or V_Warning;
  159. 'N' : if inverse then
  160. status.verbosity:=status.verbosity and (not V_Note)
  161. else
  162. status.verbosity:=status.verbosity or V_Note;
  163. 'H' : if inverse then
  164. status.verbosity:=status.verbosity and (not V_Hint)
  165. else
  166. status.verbosity:=status.verbosity or V_Hint;
  167. 'L' : if inverse then
  168. status.verbosity:=status.verbosity and (not V_Status)
  169. else
  170. status.verbosity:=status.verbosity or V_Status;
  171. 'U' : if inverse then
  172. status.verbosity:=status.verbosity and (not V_Used)
  173. else
  174. status.verbosity:=status.verbosity or V_Used;
  175. 'T' : if inverse then
  176. status.verbosity:=status.verbosity and (not V_Tried)
  177. else
  178. status.verbosity:=status.verbosity or V_Tried;
  179. 'M' : if inverse then
  180. status.verbosity:=status.verbosity and (not V_Macro)
  181. else
  182. status.verbosity:=status.verbosity or V_Macro;
  183. 'P' : if inverse then
  184. status.verbosity:=status.verbosity and (not V_Procedure)
  185. else
  186. status.verbosity:=status.verbosity or V_Procedure;
  187. 'C' : if inverse then
  188. status.verbosity:=status.verbosity and (not V_Conditional)
  189. else
  190. status.verbosity:=status.verbosity or V_Conditional;
  191. 'D' : if inverse then
  192. status.verbosity:=status.verbosity and (not V_Debug)
  193. else
  194. status.verbosity:=status.verbosity or V_Debug;
  195. 'B' : if inverse then
  196. status.verbosity:=status.verbosity and (not V_Declarations)
  197. else
  198. status.verbosity:=status.verbosity or V_Declarations;
  199. 'X' : if inverse then
  200. status.verbosity:=status.verbosity and (not V_Executable)
  201. else
  202. status.verbosity:=status.verbosity or V_Executable;
  203. 'Z' : if inverse then
  204. status.verbosity:=status.verbosity and (not V_Assem)
  205. else
  206. status.verbosity:=status.verbosity or V_Assem;
  207. end;
  208. inc(i);
  209. end;
  210. end;
  211. if status.verbosity=0 then
  212. status.verbosity:=V_Default;
  213. setverbosity:=true;
  214. end;
  215. procedure LoadMsgFile(const fn:string);
  216. begin
  217. if not(msg=nil) then
  218. dispose(msg,Done);
  219. msg:=new(pmessage,InitExtern(fn,ord(endmsgconst)));
  220. end;
  221. var
  222. lastfileidx,
  223. lastmoduleidx : longint;
  224. Procedure UpdateStatus;
  225. begin
  226. { fix status }
  227. status.currentline:=aktfilepos.line;
  228. status.currentcolumn:=aktfilepos.column;
  229. if assigned(current_module) and assigned(current_module^.sourcefiles) and
  230. ((current_module^.unit_index<>lastmoduleidx) or
  231. (aktfilepos.fileindex<>lastfileidx)) then
  232. begin
  233. { update status record }
  234. status.currentmodule:=current_module^.modulename^;
  235. status.currentsource:=current_module^.sourcefiles^.get_file_name(aktfilepos.fileindex);
  236. status.currentsourcepath:=current_module^.sourcefiles^.get_file_path(aktfilepos.fileindex);
  237. { update lastfileidx only if name known PM }
  238. if status.currentsource<>'' then
  239. lastfileidx:=aktfilepos.fileindex
  240. else
  241. lastfileidx:=0;
  242. lastmoduleidx:=current_module^.unit_index;
  243. end;
  244. end;
  245. procedure stop;
  246. begin
  247. {$ifndef TP}
  248. do_stop();
  249. {$else}
  250. do_stop;
  251. {$endif}
  252. end;
  253. procedure ShowStatus;
  254. begin
  255. UpdateStatus;
  256. {$ifndef TP}
  257. if do_status() then
  258. stop;
  259. {$else}
  260. if do_status then
  261. stop;
  262. {$endif}
  263. end;
  264. function ErrorCount:longint;
  265. begin
  266. ErrorCount:=status.errorcount;
  267. end;
  268. procedure SetMaxErrorCount(count:longint);
  269. begin
  270. status.maxerrorcount:=count;
  271. end;
  272. procedure GenerateError;
  273. begin
  274. inc(status.errorcount);
  275. end;
  276. procedure internalerror(i : longint);
  277. begin
  278. UpdateStatus;
  279. do_internalerror(i);
  280. inc(status.errorcount);
  281. stop;
  282. end;
  283. procedure Comment(l:longint;s:string);
  284. var
  285. dostop : boolean;
  286. begin
  287. dostop:=((l and V_Fatal)<>0);
  288. if (l and V_Error)<>0 then
  289. inc(status.errorcount);
  290. { Create status info }
  291. UpdateStatus;
  292. { Fix replacements }
  293. DefaultReplacements(s);
  294. { show comment }
  295. if do_comment(l,s) or dostop then
  296. stop;
  297. if (status.errorcount>=status.maxerrorcount) and not status.skip_error then
  298. begin
  299. Message1(unit_f_errors_in_unit,tostr(status.errorcount));
  300. status.skip_error:=true;
  301. stop;
  302. end;
  303. end;
  304. Procedure Msg2Comment(s:string);
  305. var
  306. idx,i,v : longint;
  307. dostop : boolean;
  308. begin
  309. {Reset}
  310. dostop:=false;
  311. v:=0;
  312. {Parse options}
  313. idx:=pos('_',s);
  314. if idx=0 then
  315. v:=V_Normal
  316. else
  317. if (idx >= 1) And (idx <= 5) then
  318. begin
  319. for i:=1 to idx do
  320. begin
  321. case upcase(s[i]) of
  322. 'F' : begin
  323. v:=v or V_Fatal;
  324. inc(status.errorcount);
  325. dostop:=true;
  326. end;
  327. 'E' : begin
  328. v:=v or V_Error;
  329. inc(status.errorcount);
  330. end;
  331. 'O' : v:=v or V_Normal;
  332. 'W':
  333. v:=v or V_Warning;
  334. 'N' : v:=v or V_Note;
  335. 'H' : v:=v or V_Hint;
  336. 'I' : v:=v or V_Info;
  337. 'L' : v:=v or V_Status;
  338. 'U' : v:=v or V_Used;
  339. 'T' : v:=v or V_Tried;
  340. 'M' : v:=v or V_Macro;
  341. 'P' : v:=v or V_Procedure;
  342. 'C' : v:=v or V_Conditional;
  343. 'D' : v:=v or V_Debug;
  344. 'B' : v:=v or V_Declarations;
  345. 'X' : v:=v or V_Executable;
  346. 'Z' : v:=v or V_Assem;
  347. 'S' : dostop:=true;
  348. '_' : ;
  349. end;
  350. end;
  351. end;
  352. Delete(s,1,idx);
  353. { fix status }
  354. UpdateStatus;
  355. { Fix replacements }
  356. DefaultReplacements(s);
  357. { show comment }
  358. if do_comment(v,s) or dostop then
  359. stop;
  360. if (status.errorcount>=status.maxerrorcount) and not status.skip_error then
  361. begin
  362. Message1(unit_f_errors_in_unit,tostr(status.errorcount));
  363. status.skip_error:=true;
  364. stop;
  365. end;
  366. end;
  367. procedure Message(w:tmsgconst);
  368. begin
  369. Msg2Comment(msg^.Get(ord(w)));
  370. end;
  371. procedure Message1(w:tmsgconst;const s1:string);
  372. begin
  373. Msg2Comment(msg^.Get1(ord(w),s1));
  374. end;
  375. procedure Message2(w:tmsgconst;const s1,s2:string);
  376. begin
  377. Msg2Comment(msg^.Get2(ord(w),s1,s2));
  378. end;
  379. procedure Message3(w:tmsgconst;const s1,s2,s3:string);
  380. begin
  381. Msg2Comment(msg^.Get3(ord(w),s1,s2,s3));
  382. end;
  383. procedure MessagePos(const pos:tfileposinfo;w:tmsgconst);
  384. var
  385. oldpos : tfileposinfo;
  386. begin
  387. oldpos:=aktfilepos;
  388. aktfilepos:=pos;
  389. Msg2Comment(msg^.Get(ord(w)));
  390. aktfilepos:=oldpos;
  391. end;
  392. procedure MessagePos1(const pos:tfileposinfo;w:tmsgconst;const s1:string);
  393. var
  394. oldpos : tfileposinfo;
  395. begin
  396. oldpos:=aktfilepos;
  397. aktfilepos:=pos;
  398. Msg2Comment(msg^.Get1(ord(w),s1));
  399. aktfilepos:=oldpos;
  400. end;
  401. procedure MessagePos2(const pos:tfileposinfo;w:tmsgconst;const s1,s2:string);
  402. var
  403. oldpos : tfileposinfo;
  404. begin
  405. oldpos:=aktfilepos;
  406. aktfilepos:=pos;
  407. Msg2Comment(msg^.Get2(ord(w),s1,s2));
  408. aktfilepos:=oldpos;
  409. end;
  410. procedure MessagePos3(const pos:tfileposinfo;w:tmsgconst;const s1,s2,s3:string);
  411. var
  412. oldpos : tfileposinfo;
  413. begin
  414. oldpos:=aktfilepos;
  415. aktfilepos:=pos;
  416. Msg2Comment(msg^.Get3(ord(w),s1,s2,s3));
  417. aktfilepos:=oldpos;
  418. end;
  419. procedure InitVerbose;
  420. begin
  421. { Init }
  422. {$ifndef EXTERN_MSG}
  423. msg:=new(pmessage,Init(@msgtxt,ord(endmsgconst)));
  424. {$else}
  425. LoadMsgFile(exepath+'errore.msg');
  426. {$endif}
  427. FillChar(Status,sizeof(TCompilerStatus),0);
  428. status.verbosity:=V_Default;
  429. Status.MaxErrorCount:=50;
  430. end;
  431. procedure DoneVerbose;
  432. begin
  433. if assigned(msg) then
  434. begin
  435. dispose(msg,Done);
  436. msg:=nil;
  437. end;
  438. end;
  439. end.
  440. {
  441. $Log$
  442. Revision 1.45 2000-02-09 13:23:09 peter
  443. * log truncated
  444. Revision 1.44 2000/01/07 01:14:49 peter
  445. * updated copyright to 2000
  446. Revision 1.43 1999/11/06 14:34:32 peter
  447. * truncated log to 20 revs
  448. Revision 1.42 1999/08/05 16:53:28 peter
  449. * V_Fatal=1, all other V_ are also increased
  450. * Check for local procedure when assigning procvar
  451. * fixed comment parsing because directives
  452. * oldtp mode directives better supported
  453. * added some messages to errore.msg
  454. }