comphook.pas 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. interface
  20. Const
  21. { <$10000 will show file and line }
  22. V_None = $0;
  23. V_Fatal = $1;
  24. V_Error = $2;
  25. V_Normal = $4; { doesn't show a text like Error: }
  26. V_Warning = $8;
  27. V_Note = $10;
  28. V_Hint = $20;
  29. V_Macro = $100;
  30. V_Procedure = $200;
  31. V_Conditional = $400;
  32. V_Assem = $800;
  33. V_Info = $10000;
  34. V_Status = $20000;
  35. V_Used = $40000;
  36. V_Tried = $80000;
  37. V_Debug = $100000;
  38. V_Declarations = $200000;
  39. V_Executable = $400000;
  40. V_ShowFile = $ffff;
  41. V_All = $ffffffff;
  42. V_Default = V_Fatal + V_Error + V_Normal;
  43. type
  44. PCompilerStatus = ^TCompilerStatus;
  45. TCompilerStatus = record
  46. { Current status }
  47. currentmodule,
  48. currentsourcepath,
  49. currentsource : string; { filename }
  50. currentline,
  51. currentcolumn : longint; { current line and column }
  52. { Total Status }
  53. compiledlines : longint; { the number of lines which are compiled }
  54. errorcount : longint; { number of generated errors }
  55. { Settings for the output }
  56. verbosity : longint;
  57. maxerrorcount : longint;
  58. errorwarning,
  59. errornote,
  60. errorhint,
  61. skip_error,
  62. use_stderr,
  63. use_redir,
  64. use_gccoutput : boolean;
  65. { Redirection support }
  66. redirfile : text;
  67. end;
  68. var
  69. status : tcompilerstatus;
  70. { Default Functions }
  71. procedure def_stop;
  72. procedure def_halt(i : longint);
  73. Function def_status:boolean;
  74. Function def_comment(Level:Longint;const s:string):boolean;
  75. function def_internalerror(i:longint):boolean;
  76. procedure def_initsymbolinfo;
  77. procedure def_donesymbolinfo;
  78. procedure def_extractsymbolinfo;
  79. {$ifdef DEBUG}
  80. { allow easy stopping in GDB
  81. using
  82. b DEF_GDB_STOP
  83. cond 1 LEVEL <= 8 }
  84. procedure def_gdb_stop(level : longint);
  85. {$endif DEBUG}
  86. { Function redirecting for IDE support }
  87. type
  88. tstopprocedure = procedure;
  89. thaltprocedure = procedure(i : longint);
  90. tstatusfunction = function:boolean;
  91. tcommentfunction = function(Level:Longint;const s:string):boolean;
  92. tinternalerrorfunction = function(i:longint):boolean;
  93. tinitsymbolinfoproc = procedure;
  94. tdonesymbolinfoproc = procedure;
  95. textractsymbolinfoproc = procedure;
  96. const
  97. do_stop : tstopprocedure = def_stop;
  98. do_halt : thaltprocedure = def_halt;
  99. do_status : tstatusfunction = def_status;
  100. do_comment : tcommentfunction = def_comment;
  101. do_internalerror : tinternalerrorfunction = def_internalerror;
  102. do_initsymbolinfo : tinitsymbolinfoproc = def_initsymbolinfo;
  103. do_donesymbolinfo : tdonesymbolinfoproc = def_donesymbolinfo;
  104. do_extractsymbolinfo : textractsymbolinfoproc = def_extractsymbolinfo;
  105. implementation
  106. {$ifdef USEEXCEPT}
  107. uses tpexcept;
  108. {$endif USEEXCEPT}
  109. {****************************************************************************
  110. Helper Routines
  111. ****************************************************************************}
  112. function gccfilename(const s : string) : string;
  113. var
  114. i : longint;
  115. begin
  116. for i:=1to length(s) do
  117. begin
  118. case s[i] of
  119. '\' : gccfilename[i]:='/';
  120. 'A'..'Z' : gccfilename[i]:=chr(ord(s[i])+32);
  121. else
  122. gccfilename[i]:=s[i];
  123. end;
  124. end;
  125. {$ifndef TP}
  126. {$ifopt H+}
  127. setlength(gccfilename,length(s));
  128. {$else}
  129. gccfilename[0]:=s[0];
  130. {$endif}
  131. {$else}
  132. gccfilename[0]:=s[0];
  133. {$endif}
  134. end;
  135. function tostr(i : longint) : string;
  136. var
  137. hs : string;
  138. begin
  139. str(i,hs);
  140. tostr:=hs;
  141. end;
  142. {****************************************************************************
  143. Predefined default Handlers
  144. ****************************************************************************}
  145. { predefined handler when then compiler stops }
  146. procedure def_stop;
  147. begin
  148. {$ifndef USEEXCEPT}
  149. Halt(1);
  150. {$else USEEXCEPT}
  151. Halt(1);
  152. {$endif USEEXCEPT}
  153. end;
  154. {$ifdef DEBUG}
  155. { allow easy stopping in GDB
  156. using
  157. b DEF_GDB_STOP
  158. cond 1 LEVEL <= 8 }
  159. procedure def_gdb_stop(level : longint);
  160. begin
  161. { Its only a dummy for GDB }
  162. end;
  163. {$endif DEBUG}
  164. procedure def_halt(i : longint);
  165. begin
  166. halt(i);
  167. end;
  168. function def_status:boolean;
  169. begin
  170. def_status:=false; { never stop }
  171. { Status info?, Called every line }
  172. if ((status.verbosity and V_Status)<>0) then
  173. begin
  174. {$ifndef Delphi}
  175. if (status.compiledlines=1) then
  176. WriteLn(memavail shr 10,' Kb Free');
  177. {$endif Delphi}
  178. if (status.currentline>0) and (status.currentline mod 100=0) then
  179. {$ifdef FPC}
  180. WriteLn(status.currentline,' ',memavail shr 10,'/',system.heapsize shr 10,' Kb Free');
  181. {$else}
  182. {$ifndef Delphi}
  183. WriteLn(status.currentline,' ',memavail shr 10,' Kb Free');
  184. {$endif Delphi}
  185. {$endif}
  186. end
  187. end;
  188. Function def_comment(Level:Longint;const s:string):boolean;
  189. const
  190. { RHIDE expect gcc like error output }
  191. rh_errorstr='error: ';
  192. rh_warningstr='warning: ';
  193. fatalstr='Fatal: ';
  194. errorstr='Error: ';
  195. warningstr='Warning: ';
  196. notestr='Note: ';
  197. hintstr='Hint: ';
  198. var
  199. hs : string;
  200. begin
  201. def_comment:=false; { never stop }
  202. if (status.verbosity and Level)=Level then
  203. begin
  204. hs:='';
  205. if not(status.use_gccoutput) then
  206. begin
  207. if (status.verbosity and Level)=V_Hint then
  208. hs:=hintstr;
  209. if (status.verbosity and Level)=V_Note then
  210. hs:=notestr;
  211. if (status.verbosity and Level)=V_Warning then
  212. hs:=warningstr;
  213. if (status.verbosity and Level)=V_Error then
  214. hs:=errorstr;
  215. if (status.verbosity and Level)=V_Fatal then
  216. hs:=fatalstr;
  217. end
  218. else
  219. begin
  220. if (status.verbosity and Level)=V_Hint then
  221. hs:=rh_warningstr;
  222. if (status.verbosity and Level)=V_Note then
  223. hs:=rh_warningstr;
  224. if (status.verbosity and Level)=V_Warning then
  225. hs:=rh_warningstr;
  226. if (status.verbosity and Level)=V_Error then
  227. hs:=rh_errorstr;
  228. if (status.verbosity and Level)=V_Fatal then
  229. hs:=rh_errorstr;
  230. end;
  231. if (Level<=V_ShowFile) and (status.currentsource<>'') and (status.currentline>0) then
  232. begin
  233. { Adding the column should not confuse RHIDE,
  234. even if it does not yet use it PM
  235. but only if it is after error or warning !! PM }
  236. if status.currentcolumn>0 then
  237. begin
  238. if status.use_gccoutput then
  239. hs:=gccfilename(status.currentsource)+':'+tostr(status.currentline)+': '+hs
  240. +tostr(status.currentcolumn)+': '
  241. else
  242. hs:=status.currentsource+'('+tostr(status.currentline)
  243. +','+tostr(status.currentcolumn)+') '+hs;
  244. end
  245. else
  246. begin
  247. if status.use_gccoutput then
  248. hs:=gccfilename(status.currentsource)+': '+hs+tostr(status.currentline)+': '
  249. else
  250. hs:=status.currentsource+'('+tostr(status.currentline)+') '+hs;
  251. end;
  252. end;
  253. { add the message to the text }
  254. hs:=hs+s;
  255. {$ifdef FPC}
  256. if status.use_stderr then
  257. begin
  258. writeln(stderr,hs);
  259. flush(stderr);
  260. end
  261. else
  262. {$endif}
  263. begin
  264. if status.use_redir then
  265. writeln(status.redirfile,hs)
  266. else
  267. writeln(hs);
  268. end;
  269. {$ifdef DEBUG}
  270. def_gdb_stop(level);
  271. {$endif DEBUG}
  272. end;
  273. end;
  274. function def_internalerror(i : longint) : boolean;
  275. begin
  276. do_comment(V_Fatal,'Internal error '+tostr(i));
  277. def_internalerror:=true;
  278. end;
  279. procedure def_initsymbolinfo;
  280. begin
  281. end;
  282. procedure def_donesymbolinfo;
  283. begin
  284. end;
  285. procedure def_extractsymbolinfo;
  286. begin
  287. end;
  288. end.
  289. {
  290. $Log$
  291. Revision 1.2 2000-07-13 11:32:38 michael
  292. + removed logs
  293. }