comphook.pas 7.9 KB

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