comphook.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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[20] = 'Fatal:';
  55. errorstr : string[20] = 'Error:';
  56. warningstr : string[20] = 'Warning:';
  57. notestr : string[20] = 'Note:';
  58. hintstr : string[20] = 'Hint:';
  59. type
  60. PCompilerStatus = ^TCompilerStatus;
  61. TCompilerStatus = record
  62. { Current status }
  63. currentmodule,
  64. currentsourceppufilename, { the name of the ppu where the source file
  65. comes from where the error location is given }
  66. currentsourcepath,
  67. currentsource : string; { filename }
  68. currentline,
  69. currentcolumn : longint; { current line and column }
  70. currentmodulestate : string[20];
  71. { Total Status }
  72. compiledlines : longint; { the number of lines which are compiled }
  73. errorcount, { this field should never be increased directly,
  74. use Verbose.GenerateError procedure to do this,
  75. this allows easier error catching using GDB by
  76. adding a single breakpoint at this procedure }
  77. countWarnings,
  78. countNotes,
  79. countHints : longint; { number of found errors/warnings/notes/hints }
  80. codesize,
  81. datasize : qword;
  82. { program info }
  83. isexe,
  84. ispackage,
  85. islibrary : boolean;
  86. { Settings for the output }
  87. showmsgnrs : boolean;
  88. verbosity : longint;
  89. maxerrorcount : longint;
  90. errorwarning,
  91. errornote,
  92. errorhint,
  93. skip_error,
  94. use_stderr,
  95. use_redir,
  96. use_bugreport,
  97. use_gccoutput,
  98. sources_avail,
  99. print_source_path : boolean;
  100. { Redirection support }
  101. redirfile : text;
  102. { Special file for bug report }
  103. reportbugfile : text;
  104. end;
  105. type
  106. EControlCAbort=class(Exception)
  107. constructor Create;
  108. end;
  109. ECompilerAbort=class(Exception)
  110. constructor Create;
  111. end;
  112. ECompilerAbortSilent=class(Exception)
  113. constructor Create;
  114. end;
  115. var
  116. status : tcompilerstatus;
  117. { Default Functions }
  118. Function def_status:boolean;
  119. Function def_comment(Level:Longint;const s:ansistring):boolean;
  120. function def_internalerror(i:longint):boolean;
  121. function def_CheckVerbosity(v:longint):boolean;
  122. procedure def_initsymbolinfo;
  123. procedure def_donesymbolinfo;
  124. procedure def_extractsymbolinfo;
  125. function def_openinputfile(const filename: TPathStr): tinputfile;
  126. Function def_getnamedfiletime(Const F : TPathStr) : Longint;
  127. { Function redirecting for IDE support }
  128. type
  129. tstopprocedure = procedure(err:longint);
  130. tstatusfunction = function:boolean;
  131. tcommentfunction = function(Level:Longint;const s:ansistring):boolean;
  132. tinternalerrorfunction = function(i:longint):boolean;
  133. tcheckverbosityfunction = function(i:longint):boolean;
  134. tinitsymbolinfoproc = procedure;
  135. tdonesymbolinfoproc = procedure;
  136. textractsymbolinfoproc = procedure;
  137. topeninputfilefunc = function(const filename: TPathStr): tinputfile;
  138. tgetnamedfiletimefunc = function(const filename: TPathStr): longint;
  139. const
  140. do_status : tstatusfunction = @def_status;
  141. do_comment : tcommentfunction = @def_comment;
  142. do_internalerror : tinternalerrorfunction = @def_internalerror;
  143. do_checkverbosity : tcheckverbosityfunction = @def_checkverbosity;
  144. do_initsymbolinfo : tinitsymbolinfoproc = @def_initsymbolinfo;
  145. do_donesymbolinfo : tdonesymbolinfoproc = @def_donesymbolinfo;
  146. do_extractsymbolinfo : textractsymbolinfoproc = @def_extractsymbolinfo;
  147. needsymbolinfo : boolean =false;
  148. do_openinputfile : topeninputfilefunc = @def_openinputfile;
  149. do_getnamedfiletime : tgetnamedfiletimefunc = @def_getnamedfiletime;
  150. implementation
  151. uses
  152. cutils, systems, globals
  153. ;
  154. {****************************************************************************
  155. Helper Routines
  156. ****************************************************************************}
  157. function gccfilename(const s : string) : string;
  158. var
  159. i : longint;
  160. begin
  161. for i:=1to length(s) do
  162. begin
  163. case s[i] of
  164. '\' : gccfilename[i]:='/';
  165. 'A'..'Z' : if not (tf_files_case_aware in source_info.flags) and
  166. not (tf_files_case_sensitive in source_info.flags) then
  167. gccfilename[i]:=chr(ord(s[i])+32)
  168. else
  169. gccfilename[i]:=s[i];
  170. else
  171. gccfilename[i]:=s[i];
  172. end;
  173. end;
  174. gccfilename[0]:=s[0];
  175. end;
  176. function tostr(i : longint) : string;
  177. var
  178. hs : string;
  179. begin
  180. str(i,hs);
  181. tostr:=hs;
  182. end;
  183. {****************************************************************************
  184. Stopping the compiler
  185. ****************************************************************************}
  186. constructor EControlCAbort.Create;
  187. begin
  188. inherited Create('Ctrl-C Signaled!');
  189. end;
  190. constructor ECompilerAbort.Create;
  191. begin
  192. inherited Create('Compilation Aborted');
  193. end;
  194. constructor ECompilerAbortSilent.Create;
  195. begin
  196. inherited Create('Compilation Aborted');
  197. end;
  198. {****************************************************************************
  199. Predefined default Handlers
  200. ****************************************************************************}
  201. function def_status:boolean;
  202. var
  203. hstatus : TFPCHeapStatus;
  204. begin
  205. def_status:=false; { never stop }
  206. { Status info?, Called every line }
  207. if ((status.verbosity and V_Status)<>0) then
  208. begin
  209. if (status.compiledlines=1) or
  210. (status.currentline mod 100=0) then
  211. begin
  212. if status.currentline>0 then
  213. Write(status.currentline,' ');
  214. hstatus:=GetFPCHeapStatus;
  215. WriteLn(DStr(hstatus.CurrHeapUsed shr 10),'/',DStr(hstatus.CurrHeapSize shr 10),' Kb Used');
  216. end;
  217. end;
  218. {$ifdef macos}
  219. Yield;
  220. {$endif}
  221. end;
  222. Function def_comment(Level:Longint;const s:ansistring):boolean;
  223. const
  224. rh_errorstr = 'error:';
  225. rh_warningstr = 'warning:';
  226. var
  227. hs : ansistring;
  228. hs2 : ansistring;
  229. begin
  230. def_comment:=false; { never stop }
  231. hs:='';
  232. if not(status.use_gccoutput) then
  233. begin
  234. if (status.verbosity and Level)=V_Hint then
  235. hs:=hintstr;
  236. if (status.verbosity and Level)=V_Note then
  237. hs:=notestr;
  238. if (status.verbosity and Level)=V_Warning then
  239. hs:=warningstr;
  240. if (status.verbosity and Level)=V_Error then
  241. hs:=errorstr;
  242. if (status.verbosity and Level)=V_Fatal then
  243. hs:=fatalstr;
  244. if (status.verbosity and Level)=V_Used then
  245. hs:=PadSpace('('+status.currentmodule+')',10);
  246. end
  247. else
  248. begin
  249. if (status.verbosity and Level)=V_Hint then
  250. hs:=rh_warningstr;
  251. if (status.verbosity and Level)=V_Note then
  252. hs:=rh_warningstr;
  253. if (status.verbosity and Level)=V_Warning then
  254. hs:=rh_warningstr;
  255. if (status.verbosity and Level)=V_Error then
  256. hs:=rh_errorstr;
  257. if (status.verbosity and Level)=V_Fatal then
  258. hs:=rh_errorstr;
  259. end;
  260. { Generate line prefix }
  261. if ((Level and V_LineInfo)=V_LineInfo) and
  262. (status.currentsource<>'') and
  263. (status.currentline>0) then
  264. begin
  265. {$ifndef macos}
  266. { Adding the column should not confuse RHIDE,
  267. even if it does not yet use it PM
  268. but only if it is after error or warning !! PM }
  269. if status.currentcolumn>0 then
  270. begin
  271. if status.use_gccoutput then
  272. hs:=gccfilename(status.currentsource)+':'+tostr(status.currentline)+': '+hs+' '+
  273. tostr(status.currentcolumn)+': '+s
  274. else
  275. begin
  276. hs:=status.currentsource+'('+tostr(status.currentline)+
  277. ','+tostr(status.currentcolumn)+') '+hs+' '+s;
  278. end;
  279. if status.print_source_path then
  280. if status.sources_avail then
  281. hs:=status.currentsourcepath+hs
  282. else
  283. hs:=status.currentsourceppufilename+':'+hs;
  284. end
  285. else
  286. begin
  287. if status.use_gccoutput then
  288. hs:=gccfilename(status.currentsource)+': '+hs+' '+tostr(status.currentline)+': '+s
  289. else
  290. hs:=status.currentsource+'('+tostr(status.currentline)+') '+hs+' '+s;
  291. end;
  292. {$else}
  293. {MPW style error}
  294. if status.currentcolumn>0 then
  295. hs:='File "'+status.currentsourcepath+status.currentsource+'"; Line '+tostr(status.currentline)+
  296. ' #[' + tostr(status.currentcolumn) + '] ' +hs+' '+s
  297. else
  298. hs:='File "'+status.currentsourcepath+status.currentsource+'"; Line '+tostr(status.currentline)+' # '+hs+' '+s;
  299. {$endif}
  300. end
  301. else
  302. begin
  303. if hs<>'' then
  304. hs:=hs+' '+s
  305. else
  306. hs:=s;
  307. end;
  308. if (status.verbosity and V_TimeStamps)<>0 then
  309. begin
  310. system.str(getrealtime-starttime:0:3,hs2);
  311. hs:='['+hs2+'] '+hs;
  312. end;
  313. { Display line }
  314. if (Level<>V_None) and
  315. ((status.verbosity and (Level and V_LevelMask))=(Level and V_LevelMask)) then
  316. begin
  317. if status.use_stderr then
  318. begin
  319. writeln(stderr,hs);
  320. flush(stderr);
  321. end
  322. else
  323. begin
  324. if status.use_redir then
  325. writeln(status.redirfile,hs)
  326. else
  327. writeln(hs);
  328. end;
  329. end;
  330. { include everything in the bugreport file }
  331. if status.use_bugreport then
  332. begin
  333. Write(status.reportbugfile,hexstr(level,8)+':');
  334. Writeln(status.reportbugfile,hs);
  335. end;
  336. end;
  337. function def_internalerror(i : longint) : boolean;
  338. begin
  339. do_comment(V_Fatal+V_LineInfo,'Internal error '+tostr(i));
  340. {$ifdef EXTDEBUG}
  341. { Internalerror() and def_internalerror() do not
  342. have a stackframe }
  343. dump_stack(stdout,get_caller_frame(get_frame));
  344. {$endif EXTDEBUG}
  345. def_internalerror:=true;
  346. end;
  347. function def_CheckVerbosity(v:longint):boolean;
  348. begin
  349. result:=status.use_bugreport or
  350. ((v<>V_None) and
  351. ((status.verbosity and (v and V_LevelMask))=(v and V_LevelMask)));
  352. end;
  353. procedure def_initsymbolinfo;
  354. begin
  355. end;
  356. procedure def_donesymbolinfo;
  357. begin
  358. end;
  359. procedure def_extractsymbolinfo;
  360. begin
  361. end;
  362. function def_openinputfile(const filename: TPathStr): tinputfile;
  363. begin
  364. def_openinputfile:=tdosinputfile.create(filename);
  365. end;
  366. Function def_GetNamedFileTime (Const F : TPathStr) : Longint;
  367. begin
  368. Result:=FileAge(F);
  369. end;
  370. end.