comphook.pas 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. {
  2. $Id$
  3. Copyright (c) 1998 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. skip_error,
  59. use_stderr,
  60. use_redir,
  61. use_gccoutput : boolean;
  62. { Redirection support }
  63. redirfile : text;
  64. end;
  65. var
  66. status : tcompilerstatus;
  67. { Default Functions }
  68. procedure def_stop;
  69. procedure def_halt(i : longint);
  70. Function def_status:boolean;
  71. Function def_comment(Level:Longint;const s:string):boolean;
  72. function def_internalerror(i:longint):boolean;
  73. { Function redirecting for IDE support }
  74. type
  75. tstopprocedure = procedure;
  76. thaltprocedure = procedure(i : longint);
  77. tstatusfunction = function:boolean;
  78. tcommentfunction = function(Level:Longint;const s:string):boolean;
  79. tinternalerrorfunction = function(i:longint):boolean;
  80. const
  81. do_stop : tstopprocedure = def_stop;
  82. do_halt : thaltprocedure = def_halt;
  83. do_status : tstatusfunction = def_status;
  84. do_comment : tcommentfunction = def_comment;
  85. do_internalerror : tinternalerrorfunction = def_internalerror;
  86. implementation
  87. {$ifdef USEEXCEPT}
  88. uses tpexcept;
  89. {$endif USEEXCEPT}
  90. {****************************************************************************
  91. Helper Routines
  92. ****************************************************************************}
  93. function gccfilename(const s : string) : string;
  94. var
  95. i : longint;
  96. begin
  97. for i:=1to length(s) do
  98. begin
  99. case s[i] of
  100. '\' : gccfilename[i]:='/';
  101. 'A'..'Z' : gccfilename[i]:=chr(ord(s[i])+32);
  102. else
  103. gccfilename[i]:=s[i];
  104. end;
  105. end;
  106. {$ifndef TP}
  107. {$ifopt H+}
  108. setlength(gccfilename,length(s));
  109. {$else}
  110. gccfilename[0]:=s[0];
  111. {$endif}
  112. {$else}
  113. gccfilename[0]:=s[0];
  114. {$endif}
  115. end;
  116. function tostr(i : longint) : string;
  117. var
  118. hs : string;
  119. begin
  120. str(i,hs);
  121. tostr:=hs;
  122. end;
  123. {****************************************************************************
  124. Predefined default Handlers
  125. ****************************************************************************}
  126. { predefined handler when then compiler stops }
  127. procedure def_stop;
  128. begin
  129. {$ifndef USEEXCEPT}
  130. Halt(1);
  131. {$else USEEXCEPT}
  132. Halt(1);
  133. {$endif USEEXCEPT}
  134. end;
  135. procedure def_halt(i : longint);
  136. begin
  137. halt(i);
  138. end;
  139. function def_status:boolean;
  140. begin
  141. def_status:=false; { never stop }
  142. { Status info?, Called every line }
  143. if ((status.verbosity and V_Status)<>0) then
  144. begin
  145. {$ifndef Delphi}
  146. if (status.compiledlines=1) then
  147. WriteLn(memavail shr 10,' Kb Free');
  148. {$endif Delphi}
  149. if (status.currentline>0) and (status.currentline mod 100=0) then
  150. {$ifdef FPC}
  151. WriteLn(status.currentline,' ',memavail shr 10,'/',system.heapsize shr 10,' Kb Free');
  152. {$else}
  153. {$ifndef Delphi}
  154. WriteLn(status.currentline,' ',memavail shr 10,' Kb Free');
  155. {$endif Delphi}
  156. {$endif}
  157. end
  158. end;
  159. Function def_comment(Level:Longint;const s:string):boolean;
  160. const
  161. { RHIDE expect gcc like error output }
  162. rh_errorstr='error: ';
  163. rh_warningstr='warning: ';
  164. fatalstr='Fatal: ';
  165. errorstr='Error: ';
  166. warningstr='Warning: ';
  167. notestr='Note: ';
  168. hintstr='Hint: ';
  169. var
  170. hs : string;
  171. begin
  172. def_comment:=false; { never stop }
  173. if (status.verbosity and Level)=Level then
  174. begin
  175. hs:='';
  176. if not(status.use_gccoutput) then
  177. begin
  178. if (status.verbosity and Level)=V_Hint then
  179. hs:=hintstr;
  180. if (status.verbosity and Level)=V_Note then
  181. hs:=notestr;
  182. if (status.verbosity and Level)=V_Warning then
  183. hs:=warningstr;
  184. if (status.verbosity and Level)=V_Error then
  185. hs:=errorstr;
  186. if (status.verbosity and Level)=V_Fatal then
  187. hs:=fatalstr;
  188. end
  189. else
  190. begin
  191. if (status.verbosity and Level)=V_Hint then
  192. hs:=rh_warningstr;
  193. if (status.verbosity and Level)=V_Note then
  194. hs:=rh_warningstr;
  195. if (status.verbosity and Level)=V_Warning then
  196. hs:=rh_warningstr;
  197. if (status.verbosity and Level)=V_Error then
  198. hs:=rh_errorstr;
  199. if (status.verbosity and Level)=V_Fatal then
  200. hs:=rh_errorstr;
  201. end;
  202. if (Level<=V_ShowFile) and (status.currentsource<>'') and (status.currentline>0) then
  203. begin
  204. { Adding the column should not confuse RHIDE,
  205. even if it does not yet use it PM
  206. but only if it is after error or warning !! PM }
  207. if status.currentcolumn>0 then
  208. begin
  209. if status.use_gccoutput then
  210. hs:=gccfilename(status.currentsource)+':'+tostr(status.currentline)+': '+hs
  211. +tostr(status.currentcolumn)+': '
  212. else
  213. hs:=status.currentsource+'('+tostr(status.currentline)
  214. +','+tostr(status.currentcolumn)+') '+hs;
  215. end
  216. else
  217. begin
  218. if status.use_gccoutput then
  219. hs:=gccfilename(status.currentsource)+': '+hs+tostr(status.currentline)+': '
  220. else
  221. hs:=status.currentsource+'('+tostr(status.currentline)+') '+hs;
  222. end;
  223. end;
  224. { add the message to the text }
  225. hs:=hs+s;
  226. {$ifdef FPC}
  227. if status.use_stderr then
  228. begin
  229. writeln(stderr,hs);
  230. flush(stderr);
  231. end
  232. else
  233. {$endif}
  234. begin
  235. if status.use_redir then
  236. writeln(status.redirfile,hs)
  237. else
  238. writeln(hs);
  239. end;
  240. end;
  241. end;
  242. function def_internalerror(i : longint) : boolean;
  243. begin
  244. do_comment(V_Fatal,'Internal error '+tostr(i));
  245. def_internalerror:=true;
  246. end;
  247. end.
  248. {
  249. $Log$
  250. Revision 1.18 1999-09-07 14:03:48 pierre
  251. + added do_halt procedure
  252. Revision 1.17 1999/08/05 16:52:53 peter
  253. * V_Fatal=1, all other V_ are also increased
  254. * Check for local procedure when assigning procvar
  255. * fixed comment parsing because directives
  256. * oldtp mode directives better supported
  257. * added some messages to errore.msg
  258. Revision 1.16 1999/05/04 21:44:38 florian
  259. * changes to compile it with Delphi 4.0
  260. Revision 1.15 1999/01/15 12:27:23 peter
  261. * removed path from output, was there only for debugging
  262. Revision 1.14 1999/01/14 21:47:09 peter
  263. * status.currentmodule is now also updated
  264. + status.currentsourcepath
  265. Revision 1.13 1998/12/11 00:03:12 peter
  266. + globtype,tokens,version unit splitted from globals
  267. }