comphook.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Peter Vreman
  4. This unit handles the compilerhooks for output to external programs
  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 comphook;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. finput;
  23. Const
  24. { <$10000 will show file and line }
  25. V_None = $0;
  26. V_Fatal = $1;
  27. V_Error = $2;
  28. V_Normal = $4; { doesn't show a text like Error: }
  29. V_Warning = $8;
  30. V_Note = $10;
  31. V_Hint = $20;
  32. V_Macro = $100;
  33. V_Procedure = $200;
  34. V_Conditional = $400;
  35. V_Assem = $800;
  36. V_Declarations = $1000;
  37. V_Info = $10000;
  38. V_Status = $20000;
  39. V_Used = $40000;
  40. V_Tried = $80000;
  41. V_Debug = $100000;
  42. V_Executable = $200000;
  43. V_ShowFile = $ffff;
  44. V_All = $ffffffff;
  45. V_Default = V_Fatal + V_Error + V_Normal;
  46. const
  47. { RHIDE expect gcc like error output }
  48. fatalstr : string[20] = 'Fatal:';
  49. errorstr : string[20] = 'Error:';
  50. warningstr : string[20] = 'Warning:';
  51. notestr : string[20] = 'Note:';
  52. hintstr : string[20] = 'Hint:';
  53. type
  54. PCompilerStatus = ^TCompilerStatus;
  55. TCompilerStatus = record
  56. { Current status }
  57. currentmodule,
  58. currentsourcepath,
  59. currentsource : string; { filename }
  60. currentline,
  61. currentcolumn : longint; { current line and column }
  62. { Total Status }
  63. compiledlines : longint; { the number of lines which are compiled }
  64. errorcount : longint; { number of generated errors }
  65. { program info }
  66. isexe,
  67. islibrary : boolean;
  68. { Settings for the output }
  69. verbosity : longint;
  70. maxerrorcount : longint;
  71. errorwarning,
  72. errornote,
  73. errorhint,
  74. skip_error,
  75. use_stderr,
  76. use_redir,
  77. use_bugreport,
  78. use_gccoutput,
  79. compiling_current : boolean;
  80. { Redirection support }
  81. redirfile : text;
  82. { Special file for bug report }
  83. reportbugfile : text;
  84. end;
  85. var
  86. status : tcompilerstatus;
  87. { Default Functions }
  88. procedure def_stop;
  89. procedure def_halt(i : longint);
  90. Function def_status:boolean;
  91. Function def_comment(Level:Longint;const s:string):boolean;
  92. function def_internalerror(i:longint):boolean;
  93. procedure def_initsymbolinfo;
  94. procedure def_donesymbolinfo;
  95. procedure def_extractsymbolinfo;
  96. function def_openinputfile(const filename: string): tinputfile;
  97. Function def_getnamedfiletime(Const F : String) : Longint;
  98. {$ifdef DEBUG}
  99. { allow easy stopping in GDB
  100. using
  101. b DEF_GDB_STOP
  102. cond 1 LEVEL <= 8 }
  103. procedure def_gdb_stop(level : longint);
  104. {$endif DEBUG}
  105. { Function redirecting for IDE support }
  106. type
  107. tstopprocedure = procedure;
  108. thaltprocedure = procedure(i : longint);
  109. tstatusfunction = function:boolean;
  110. tcommentfunction = function(Level:Longint;const s:string):boolean;
  111. tinternalerrorfunction = function(i:longint):boolean;
  112. tinitsymbolinfoproc = procedure;
  113. tdonesymbolinfoproc = procedure;
  114. textractsymbolinfoproc = procedure;
  115. topeninputfilefunc = function(const filename: string): tinputfile;
  116. tgetnamedfiletimefunc = function(const filename: string): longint;
  117. const
  118. do_stop : tstopprocedure = {$ifdef FPCPROCVAR}@{$endif}def_stop;
  119. do_halt : thaltprocedure = {$ifdef FPCPROCVAR}@{$endif}def_halt;
  120. do_status : tstatusfunction = {$ifdef FPCPROCVAR}@{$endif}def_status;
  121. do_comment : tcommentfunction = {$ifdef FPCPROCVAR}@{$endif}def_comment;
  122. do_internalerror : tinternalerrorfunction = {$ifdef FPCPROCVAR}@{$endif}def_internalerror;
  123. do_initsymbolinfo : tinitsymbolinfoproc = {$ifdef FPCPROCVAR}@{$endif}def_initsymbolinfo;
  124. do_donesymbolinfo : tdonesymbolinfoproc = {$ifdef FPCPROCVAR}@{$endif}def_donesymbolinfo;
  125. do_extractsymbolinfo : textractsymbolinfoproc = {$ifdef FPCPROCVAR}@{$endif}def_extractsymbolinfo;
  126. do_openinputfile : topeninputfilefunc = {$ifdef FPCPROCVAR}@{$endif}def_openinputfile;
  127. do_getnamedfiletime : tgetnamedfiletimefunc = {$ifdef FPCPROCVAR}@{$endif}def_getnamedfiletime;
  128. implementation
  129. uses
  130. {$ifdef delphi}
  131. dmisc,
  132. {$else}
  133. dos,
  134. {$endif}
  135. cutils
  136. ;
  137. {****************************************************************************
  138. Helper Routines
  139. ****************************************************************************}
  140. function gccfilename(const s : string) : string;
  141. var
  142. i : longint;
  143. begin
  144. for i:=1to length(s) do
  145. begin
  146. case s[i] of
  147. '\' : gccfilename[i]:='/';
  148. 'A'..'Z' : gccfilename[i]:=chr(ord(s[i])+32);
  149. else
  150. gccfilename[i]:=s[i];
  151. end;
  152. end;
  153. gccfilename[0]:=s[0];
  154. end;
  155. function tostr(i : longint) : string;
  156. var
  157. hs : string;
  158. begin
  159. str(i,hs);
  160. tostr:=hs;
  161. end;
  162. {****************************************************************************
  163. Predefined default Handlers
  164. ****************************************************************************}
  165. { predefined handler when then compiler stops }
  166. procedure def_stop;
  167. begin
  168. Halt(1);
  169. end;
  170. {$ifdef DEBUG}
  171. { allow easy stopping in GDB
  172. using
  173. b DEF_GDB_STOP
  174. cond 1 LEVEL <= 8 }
  175. procedure def_gdb_stop(level : longint);
  176. begin
  177. { Its only a dummy for GDB }
  178. end;
  179. {$endif DEBUG}
  180. procedure def_halt(i : longint);
  181. begin
  182. halt(i);
  183. end;
  184. function def_status:boolean;
  185. begin
  186. def_status:=false; { never stop }
  187. { Status info?, Called every line }
  188. if ((status.verbosity and V_Status)<>0) then
  189. begin
  190. {$ifndef Delphi}
  191. if (status.compiledlines=1) then
  192. WriteLn(memavail shr 10,' Kb Free');
  193. {$endif Delphi}
  194. if (status.currentline>0) and (status.currentline mod 100=0) then
  195. {$ifdef FPC}
  196. WriteLn(status.currentline,' ',DStr(memavail shr 10),'/',DStr(system.heapsize shr 10),' Kb Free');
  197. {$else}
  198. {$ifndef Delphi}
  199. WriteLn(status.currentline,' ',DStr(memavail shr 10),' Kb Free');
  200. {$endif Delphi}
  201. {$endif}
  202. end
  203. end;
  204. Function def_comment(Level:Longint;const s:string):boolean;
  205. const
  206. rh_errorstr = 'error:';
  207. rh_warningstr = 'warning:';
  208. var
  209. hs : string;
  210. begin
  211. def_comment:=false; { never stop }
  212. hs:='';
  213. if not(status.use_gccoutput) then
  214. begin
  215. if (status.verbosity and Level)=V_Hint then
  216. hs:=hintstr;
  217. if (status.verbosity and Level)=V_Note then
  218. hs:=notestr;
  219. if (status.verbosity and Level)=V_Warning then
  220. hs:=warningstr;
  221. if (status.verbosity and Level)=V_Error then
  222. hs:=errorstr;
  223. if (status.verbosity and Level)=V_Fatal then
  224. hs:=fatalstr;
  225. end
  226. else
  227. begin
  228. if (status.verbosity and Level)=V_Hint then
  229. hs:=rh_warningstr;
  230. if (status.verbosity and Level)=V_Note then
  231. hs:=rh_warningstr;
  232. if (status.verbosity and Level)=V_Warning then
  233. hs:=rh_warningstr;
  234. if (status.verbosity and Level)=V_Error then
  235. hs:=rh_errorstr;
  236. if (status.verbosity and Level)=V_Fatal then
  237. hs:=rh_errorstr;
  238. end;
  239. if (Level<=V_ShowFile) and (status.currentsource<>'') and (status.currentline>0) then
  240. begin
  241. { Adding the column should not confuse RHIDE,
  242. even if it does not yet use it PM
  243. but only if it is after error or warning !! PM }
  244. if status.currentcolumn>0 then
  245. begin
  246. if status.use_gccoutput then
  247. hs:=gccfilename(status.currentsource)+':'+tostr(status.currentline)+': '+hs+' '+
  248. tostr(status.currentcolumn)+': '+s
  249. else
  250. hs:=status.currentsource+'('+tostr(status.currentline)+
  251. ','+tostr(status.currentcolumn)+') '+hs+' '+s;
  252. end
  253. else
  254. begin
  255. if status.use_gccoutput then
  256. hs:=gccfilename(status.currentsource)+': '+hs+' '+tostr(status.currentline)+': '+s
  257. else
  258. hs:=status.currentsource+'('+tostr(status.currentline)+') '+hs+' '+s;
  259. end;
  260. end
  261. else
  262. begin
  263. if hs<>'' then
  264. hs:=hs+' '+s
  265. else
  266. hs:=s;
  267. end;
  268. { only show when the level is required }
  269. if ((status.verbosity and Level)=Level) then
  270. begin
  271. {$ifdef FPC}
  272. if status.use_stderr then
  273. begin
  274. writeln(stderr,hs);
  275. flush(stderr);
  276. end
  277. else
  278. {$endif}
  279. begin
  280. if status.use_redir then
  281. writeln(status.redirfile,hs)
  282. else
  283. writeln(hs);
  284. end;
  285. end;
  286. { include everything in the bugreport file }
  287. if status.use_bugreport then
  288. begin
  289. {$ifdef FPC}
  290. Write(status.reportbugfile,hexstr(level,8)+':');
  291. Writeln(status.reportbugfile,hs);
  292. {$endif}
  293. end;
  294. {$ifdef DEBUG}
  295. def_gdb_stop(level);
  296. {$endif DEBUG}
  297. end;
  298. function def_internalerror(i : longint) : boolean;
  299. begin
  300. do_comment(V_Fatal,'Internal error '+tostr(i));
  301. def_internalerror:=true;
  302. end;
  303. procedure def_initsymbolinfo;
  304. begin
  305. end;
  306. procedure def_donesymbolinfo;
  307. begin
  308. end;
  309. procedure def_extractsymbolinfo;
  310. begin
  311. end;
  312. function def_openinputfile(const filename: string): tinputfile;
  313. begin
  314. def_openinputfile:=tdosinputfile.create(filename);
  315. end;
  316. Function def_GetNamedFileTime (Const F : String) : Longint;
  317. var
  318. L : Longint;
  319. info : SearchRec;
  320. begin
  321. l:=-1;
  322. {$ifdef delphi}
  323. dmisc.FindFirst (F,archive+readonly+hidden,info);
  324. {$else delphi}
  325. FindFirst (F,archive+readonly+hidden,info);
  326. {$endif delphi}
  327. if DosError=0 then
  328. l:=info.time;
  329. FindClose(info);
  330. def_GetNamedFileTime:=l;
  331. end;
  332. end.
  333. {
  334. $Log$
  335. Revision 1.21 2002-11-15 01:58:46 peter
  336. * merged changes from 1.0.7 up to 04-11
  337. - -V option for generating bug report tracing
  338. - more tracing for option parsing
  339. - errors for cdecl and high()
  340. - win32 import stabs
  341. - win32 records<=8 are returned in eax:edx (turned off by default)
  342. - heaptrc update
  343. - more info for temp management in .s file with EXTDEBUG
  344. Revision 1.20 2002/09/05 19:29:42 peter
  345. * memdebug enhancements
  346. Revision 1.19 2002/05/18 13:34:06 peter
  347. * readded missing revisions
  348. Revision 1.18 2002/05/16 19:46:35 carl
  349. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  350. + try to fix temp allocation (still in ifdef)
  351. + generic constructor calls
  352. + start of tassembler / tmodulebase class cleanup
  353. }