comphook.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. This unit handles the compilerhooks for output to external programs
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit comphook;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. {$IFNDEF USE_FAKE_SYSUTILS}
  22. sysutils,
  23. {$ELSE}
  24. fksysutl,
  25. {$ENDIF}
  26. globtype,
  27. finput;
  28. Const
  29. { Levels }
  30. V_None = $0;
  31. V_Fatal = $1;
  32. V_Error = $2;
  33. V_Normal = $4; { doesn't show a text like Error: }
  34. V_Warning = $8;
  35. V_Note = $10;
  36. V_Hint = $20;
  37. V_LineInfoMask = $fff;
  38. { From here by default no line info }
  39. V_Info = $1000;
  40. V_Status = $2000;
  41. V_Used = $4000;
  42. V_Tried = $8000;
  43. V_Conditional = $10000;
  44. V_Debug = $20000;
  45. V_Executable = $40000;
  46. V_TimeStamps = $80000;
  47. V_LevelMask = $fffffff;
  48. V_All = V_LevelMask;
  49. V_Default = V_Fatal + V_Error + V_Normal;
  50. { Flags }
  51. V_LineInfo = $10000000;
  52. const
  53. { RHIDE expect gcc like error output }
  54. fatalstr : string[6] = 'Fatal:';
  55. errorstr : string[6] = 'Error:';
  56. warningstr : string[8] = 'Warning:';
  57. notestr : string[5] = 'Note:';
  58. hintstr : string[5] = 'Hint:';
  59. warningerrorstr : string[29] = 'Warning: (treated as error)';
  60. noteerrorstr : string[27] = 'Note: (treated as error)';
  61. hinterrorstr : string[27] = 'Hint: (treated as error)';
  62. type
  63. PCompilerStatus = ^TCompilerStatus;
  64. TCompilerStatus = record
  65. { Current status }
  66. currentmodule,
  67. currentsourceppufilename, { the name of the ppu where the source file
  68. comes from where the error location is given }
  69. currentsourcepath,
  70. currentsource : string; { filename }
  71. currentline,
  72. currentcolumn : longint; { current line and column }
  73. currentmodulestate : string[20];
  74. { Total Status }
  75. compiledlines : longint; { the number of lines which are compiled }
  76. errorcount, { this field should never be increased directly,
  77. use Verbose.GenerateError procedure to do this,
  78. this allows easier error catching using GDB by
  79. adding a single breakpoint at this procedure }
  80. countWarnings,
  81. countNotes,
  82. countHints : longint; { number of found errors/warnings/notes/hints }
  83. codesize,
  84. datasize : qword;
  85. { program info }
  86. isexe,
  87. ispackage,
  88. islibrary : boolean;
  89. { Settings for the output }
  90. showmsgnrs : boolean;
  91. verbosity : longint;
  92. maxerrorcount : longint;
  93. errorwarning,
  94. errornote,
  95. errorhint,
  96. skip_error,
  97. use_stderr,
  98. use_redir,
  99. use_bugreport,
  100. use_gccoutput,
  101. sources_avail,
  102. print_source_path : boolean;
  103. { Redirection support }
  104. redirfile : text;
  105. { Special file for bug report }
  106. reportbugfile : text;
  107. end;
  108. type
  109. EControlCAbort=class(Exception)
  110. constructor Create;
  111. end;
  112. ECompilerAbort=class(Exception)
  113. constructor Create;
  114. end;
  115. ECompilerAbortSilent=class(Exception)
  116. constructor Create;
  117. end;
  118. var
  119. status : tcompilerstatus;
  120. { Default Functions }
  121. Function def_status:boolean;
  122. Function def_comment(Level:Longint;const s:ansistring):boolean;
  123. function def_internalerror(i:longint):boolean;
  124. function def_CheckVerbosity(v:longint):boolean;
  125. procedure def_initsymbolinfo;
  126. procedure def_donesymbolinfo;
  127. procedure def_extractsymbolinfo;
  128. function def_openinputfile(const filename: TPathStr): tinputfile;
  129. Function def_getnamedfiletime(Const F : TPathStr) : Longint;
  130. { Function redirecting for IDE support }
  131. type
  132. tstopprocedure = procedure(err:longint);
  133. tstatusfunction = function:boolean;
  134. tcommentfunction = function(Level:Longint;const s:ansistring):boolean;
  135. tinternalerrorfunction = function(i:longint):boolean;
  136. tcheckverbosityfunction = function(i:longint):boolean;
  137. tinitsymbolinfoproc = procedure;
  138. tdonesymbolinfoproc = procedure;
  139. textractsymbolinfoproc = procedure;
  140. topeninputfilefunc = function(const filename: TPathStr): tinputfile;
  141. tgetnamedfiletimefunc = function(const filename: TPathStr): longint;
  142. const
  143. do_status : tstatusfunction = @def_status;
  144. do_comment : tcommentfunction = @def_comment;
  145. do_internalerror : tinternalerrorfunction = @def_internalerror;
  146. do_checkverbosity : tcheckverbosityfunction = @def_checkverbosity;
  147. do_initsymbolinfo : tinitsymbolinfoproc = @def_initsymbolinfo;
  148. do_donesymbolinfo : tdonesymbolinfoproc = @def_donesymbolinfo;
  149. do_extractsymbolinfo : textractsymbolinfoproc = @def_extractsymbolinfo;
  150. needsymbolinfo : boolean =false;
  151. do_openinputfile : topeninputfilefunc = @def_openinputfile;
  152. do_getnamedfiletime : tgetnamedfiletimefunc = @def_getnamedfiletime;
  153. implementation
  154. uses
  155. cutils, systems, globals, comptty;
  156. {****************************************************************************
  157. Helper Routines
  158. ****************************************************************************}
  159. function gccfilename(const s : string) : string;
  160. var
  161. i : longint;
  162. begin
  163. for i:=1to length(s) do
  164. begin
  165. case s[i] of
  166. '\' : gccfilename[i]:='/';
  167. 'A'..'Z' : if not (tf_files_case_aware in source_info.flags) and
  168. not (tf_files_case_sensitive in source_info.flags) then
  169. gccfilename[i]:=chr(ord(s[i])+32)
  170. else
  171. gccfilename[i]:=s[i];
  172. else
  173. gccfilename[i]:=s[i];
  174. end;
  175. end;
  176. gccfilename[0]:=s[0];
  177. end;
  178. function tostr(i : longint) : string;
  179. var
  180. hs : string;
  181. begin
  182. str(i,hs);
  183. tostr:=hs;
  184. end;
  185. type
  186. TOutputColor = (oc_black,oc_red,oc_green,oc_orange,og_blue,oc_magenta,oc_cyan,oc_lightgray);
  187. procedure WriteColoredOutput(var t: Text;color: TOutputColor;const s : AnsiString);
  188. begin
  189. if TTYCheckSupported and IsATTY(t) then
  190. begin
  191. case color of
  192. oc_black:
  193. write(t,#27'[1m'#27'[30m');
  194. oc_red:
  195. write(t,#27'[1m'#27'[31m');
  196. oc_green:
  197. write(t,#27'[1m'#27'[32m');
  198. oc_orange:
  199. write(t,#27'[1m'#27'[33m');
  200. og_blue:
  201. write(t,#27'[1m'#27'[34m');
  202. oc_magenta:
  203. write(t,#27'[1m'#27'[35m');
  204. oc_cyan:
  205. write(t,#27'[1m'#27'[36m');
  206. oc_lightgray:
  207. write(t,#27'[1m'#27'[37m');
  208. end;
  209. end;
  210. write(t,s);
  211. if TTYCheckSupported and IsATTY(t) then
  212. write(t,#27'[0m');
  213. end;
  214. {****************************************************************************
  215. Stopping the compiler
  216. ****************************************************************************}
  217. constructor EControlCAbort.Create;
  218. begin
  219. inherited Create('Ctrl-C Signaled!');
  220. end;
  221. constructor ECompilerAbort.Create;
  222. begin
  223. inherited Create('Compilation Aborted');
  224. end;
  225. constructor ECompilerAbortSilent.Create;
  226. begin
  227. inherited Create('Compilation Aborted');
  228. end;
  229. {****************************************************************************
  230. Predefined default Handlers
  231. ****************************************************************************}
  232. function def_status:boolean;
  233. var
  234. hstatus : TFPCHeapStatus;
  235. begin
  236. def_status:=false; { never stop }
  237. { Status info?, Called every line }
  238. if ((status.verbosity and V_Status)<>0) then
  239. begin
  240. if (status.compiledlines=1) or
  241. (status.currentline mod 100=0) then
  242. begin
  243. if status.currentline>0 then
  244. Write(status.currentline,' ');
  245. hstatus:=GetFPCHeapStatus;
  246. WriteLn(DStr(hstatus.CurrHeapUsed shr 10),'/',DStr(hstatus.CurrHeapSize shr 10),' Kb Used');
  247. end;
  248. end;
  249. {$ifdef macos}
  250. Yield;
  251. {$endif}
  252. end;
  253. Function def_comment(Level:Longint;const s:ansistring):boolean;
  254. const
  255. rh_errorstr = 'error:';
  256. rh_warningstr = 'warning:';
  257. procedure WriteMsgTypeColored(var t : text;const s : AnsiString);
  258. begin
  259. case (status.verbosity and Level) of
  260. V_Warning:
  261. WriteColoredOutput(t,oc_magenta,s);
  262. V_Error,
  263. V_Fatal:
  264. WriteColoredOutput(t,oc_red,s);
  265. else
  266. write(t,s);
  267. end;
  268. end;
  269. var
  270. hs2,
  271. MsgTypeStr,
  272. MsgLocStr,
  273. MsgTimeStr: AnsiString;
  274. begin
  275. def_comment:=false; { never stop }
  276. MsgTypeStr:='';
  277. MsgLocStr:='';
  278. MsgTimeStr:='';
  279. if not(status.use_gccoutput) then
  280. begin
  281. if (status.verbosity and Level)=V_Hint then
  282. if status.errorhint then
  283. MsgTypeStr:=hinterrorstr
  284. else
  285. MsgTypeStr:=hintstr;
  286. if (status.verbosity and Level)=V_Note then
  287. if status.errornote then
  288. MsgTypeStr:=noteerrorstr
  289. else
  290. MsgTypeStr:=notestr;
  291. if (status.verbosity and Level)=V_Warning then
  292. if status.errorwarning then
  293. MsgTypeStr:=warningerrorstr
  294. else
  295. MsgTypeStr:=warningstr;
  296. if (status.verbosity and Level)=V_Error then
  297. MsgTypeStr:=errorstr;
  298. if (status.verbosity and Level)=V_Fatal then
  299. MsgTypeStr:=fatalstr;
  300. if (status.verbosity and Level)=V_Used then
  301. MsgTypeStr:=PadSpace('('+status.currentmodule+')',10);
  302. end
  303. else
  304. begin
  305. if (status.verbosity and Level)=V_Hint then
  306. MsgTypeStr:=rh_warningstr;
  307. if (status.verbosity and Level)=V_Note then
  308. MsgTypeStr:=rh_warningstr;
  309. if (status.verbosity and Level)=V_Warning then
  310. MsgTypeStr:=rh_warningstr;
  311. if (status.verbosity and Level)=V_Error then
  312. MsgTypeStr:=rh_errorstr;
  313. if (status.verbosity and Level)=V_Fatal then
  314. MsgTypeStr:=rh_errorstr;
  315. end;
  316. { Generate line prefix }
  317. if ((Level and V_LineInfo)=V_LineInfo) and
  318. (status.currentsource<>'') and
  319. (status.currentline>0) then
  320. begin
  321. {$ifndef macos}
  322. { Adding the column should not confuse RHIDE,
  323. even if it does not yet use it PM
  324. but only if it is after error or warning !! PM }
  325. if status.currentcolumn>0 then
  326. begin
  327. if status.use_gccoutput then
  328. MsgLocStr:=gccfilename(status.currentsource)+':'+tostr(status.currentline)+':'+tostr(status.currentcolumn)+':'
  329. else
  330. MsgLocStr:=status.currentsource+'('+tostr(status.currentline)+','+tostr(status.currentcolumn)+')';
  331. end
  332. else
  333. begin
  334. if status.use_gccoutput then
  335. MsgLocStr:=gccfilename(status.currentsource)+':'+tostr(status.currentline)+':'
  336. else
  337. MsgLocStr:=status.currentsource+'('+tostr(status.currentline)+')';
  338. end;
  339. if status.print_source_path then
  340. if status.sources_avail then
  341. MsgLocStr:=status.currentsourcepath+MsgLocStr
  342. else
  343. MsgLocStr:=status.currentsourceppufilename+':'+MsgLocStr;
  344. {$else macos}
  345. { MPW style error }
  346. if status.currentcolumn>0 then
  347. MsgLocStr:='File "'+status.currentsourcepath+status.currentsource+'"; Line '+tostr(status.currentline)+' #[' + tostr(status.currentcolumn) + ']'
  348. else
  349. MsgLocStr:='File "'+status.currentsourcepath+status.currentsource+'"; Line '+tostr(status.currentline)+' # ';
  350. {$endif macos}
  351. end;
  352. if MsgLocStr<>'' then
  353. MsgLocStr:=MsgLocStr+' ';
  354. if MsgTypeStr<>'' then
  355. MsgTypeStr:=MsgTypeStr+' ';
  356. if (status.verbosity and V_TimeStamps)<>0 then
  357. begin
  358. system.str(getrealtime-starttime:0:3,hs2);
  359. MsgTimeStr:='['+hs2+'] ';
  360. end;
  361. { Display line }
  362. if (Level<>V_None) and
  363. ((status.verbosity and (Level and V_LevelMask))=(Level and V_LevelMask)) then
  364. begin
  365. if status.use_stderr then
  366. begin
  367. write(StdErr,MsgTimeStr+MsgLocStr);
  368. WriteMsgTypeColored(StdErr,MsgTypeStr);
  369. writeln(StdErr,s);
  370. flush(StdErr);
  371. end
  372. else
  373. begin
  374. if status.use_redir then
  375. writeln(status.redirfile,MsgTimeStr+MsgLocStr+MsgTypeStr+s)
  376. else
  377. begin
  378. write(MsgTimeStr+MsgLocStr);
  379. WriteMsgTypeColored(Output,MsgTypeStr);
  380. writeln(s);
  381. end;
  382. end;
  383. end;
  384. { include everything in the bugreport file }
  385. if status.use_bugreport then
  386. begin
  387. Write(status.reportbugfile,hexstr(level,8)+':');
  388. Writeln(status.reportbugfile,MsgTimeStr+MsgLocStr+MsgTypeStr+s);
  389. end;
  390. end;
  391. function def_internalerror(i : longint) : boolean;
  392. begin
  393. do_comment(V_Fatal+V_LineInfo,'Internal error '+tostr(i));
  394. {$ifdef EXTDEBUG}
  395. { Internalerror() and def_internalerror() do not
  396. have a stackframe }
  397. dump_stack(stdout,get_caller_frame(get_frame));
  398. {$endif EXTDEBUG}
  399. def_internalerror:=true;
  400. end;
  401. function def_CheckVerbosity(v:longint):boolean;
  402. begin
  403. result:=status.use_bugreport or
  404. ((v<>V_None) and
  405. ((status.verbosity and (v and V_LevelMask))=(v and V_LevelMask)));
  406. end;
  407. procedure def_initsymbolinfo;
  408. begin
  409. end;
  410. procedure def_donesymbolinfo;
  411. begin
  412. end;
  413. procedure def_extractsymbolinfo;
  414. begin
  415. end;
  416. function def_openinputfile(const filename: TPathStr): tinputfile;
  417. begin
  418. def_openinputfile:=tdosinputfile.create(filename);
  419. end;
  420. Function def_GetNamedFileTime (Const F : TPathStr) : Longint;
  421. begin
  422. Result:=FileAge(F);
  423. end;
  424. end.