comphook.pas 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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_Fatal = $0;
  23. V_Error = $1;
  24. V_Normal = $2; { doesn't show a text like Error: }
  25. V_Warning = $4;
  26. V_Note = $8;
  27. V_Hint = $10;
  28. V_Macro = $100;
  29. V_Procedure = $200;
  30. V_Conditional = $400;
  31. V_Info = $10000;
  32. V_Status = $20000;
  33. V_Used = $40000;
  34. V_Tried = $80000;
  35. V_Debug = $100000;
  36. V_ShowFile = $ffff;
  37. V_All = $ffffffff;
  38. V_Default = V_Fatal + V_Error + V_Normal;
  39. type
  40. PCompilerStatus = ^TCompilerStatus;
  41. TCompilerStatus = record
  42. { Current status }
  43. currentmodule,
  44. currentsourcepath,
  45. currentsource : string; { filename }
  46. currentline,
  47. currentcolumn : longint; { current line and column }
  48. { Total Status }
  49. compiledlines : longint; { the number of lines which are compiled }
  50. errorcount : longint; { number of generated errors }
  51. { Settings for the output }
  52. verbosity : longint;
  53. maxerrorcount : longint;
  54. skip_error,
  55. use_stderr,
  56. use_redir,
  57. use_gccoutput : boolean;
  58. { Redirection support }
  59. redirfile : text;
  60. end;
  61. var
  62. status : tcompilerstatus;
  63. { Default Functions }
  64. procedure def_stop;
  65. Function def_status:boolean;
  66. Function def_comment(Level:Longint;const s:string):boolean;
  67. function def_internalerror(i:longint):boolean;
  68. { Function redirecting for IDE support }
  69. type
  70. tstopprocedure = procedure;
  71. tstatusfunction = function:boolean;
  72. tcommentfunction = function(Level:Longint;const s:string):boolean;
  73. tinternalerrorfunction = function(i:longint):boolean;
  74. const
  75. do_stop : tstopprocedure = def_stop;
  76. do_status : tstatusfunction = def_status;
  77. do_comment : tcommentfunction = def_comment;
  78. do_internalerror : tinternalerrorfunction = def_internalerror;
  79. implementation
  80. {$ifdef USEEXCEPT}
  81. uses tpexcept;
  82. {$endif USEEXCEPT}
  83. {****************************************************************************
  84. Helper Routines
  85. ****************************************************************************}
  86. function gccfilename(const s : string) : string;
  87. var
  88. i : longint;
  89. begin
  90. for i:=1to length(s) do
  91. begin
  92. case s[i] of
  93. '\' : gccfilename[i]:='/';
  94. 'A'..'Z' : gccfilename[i]:=chr(ord(s[i])+32);
  95. else
  96. gccfilename[i]:=s[i];
  97. end;
  98. end;
  99. {$ifndef TP}
  100. {$ifopt H+}
  101. setlength(gccfilename,length(s));
  102. {$else}
  103. gccfilename[0]:=s[0];
  104. {$endif}
  105. {$else}
  106. gccfilename[0]:=s[0];
  107. {$endif}
  108. end;
  109. function tostr(i : longint) : string;
  110. var
  111. hs : string;
  112. begin
  113. str(i,hs);
  114. tostr:=hs;
  115. end;
  116. {****************************************************************************
  117. Predefined default Handlers
  118. ****************************************************************************}
  119. { predefined handler when then compiler stops }
  120. procedure def_stop;
  121. begin
  122. {$ifndef USEEXCEPT}
  123. Halt(1);
  124. {$else USEEXCEPT}
  125. Halt(1);
  126. {$endif USEEXCEPT}
  127. end;
  128. function def_status:boolean;
  129. begin
  130. def_status:=false; { never stop }
  131. { Status info?, Called every line }
  132. if ((status.verbosity and V_Status)<>0) then
  133. begin
  134. if (status.compiledlines=1) then
  135. WriteLn(memavail shr 10,' Kb Free');
  136. if (status.currentline>0) and (status.currentline mod 100=0) then
  137. {$ifdef FPC}
  138. WriteLn(status.currentline,' ',memavail shr 10,'/',system.heapsize shr 10,' Kb Free');
  139. {$else}
  140. WriteLn(status.currentline,' ',memavail shr 10,' Kb Free');
  141. {$endif}
  142. end
  143. end;
  144. Function def_comment(Level:Longint;const s:string):boolean;
  145. const
  146. { RHIDE expect gcc like error output }
  147. rh_errorstr='error: ';
  148. rh_warningstr='warning: ';
  149. fatalstr='Fatal: ';
  150. errorstr='Error: ';
  151. warningstr='Warning: ';
  152. notestr='Note: ';
  153. hintstr='Hint: ';
  154. var
  155. hs : string;
  156. begin
  157. def_comment:=false; { never stop }
  158. if (status.verbosity and Level)=Level then
  159. begin
  160. hs:='';
  161. if not(status.use_gccoutput) then
  162. begin
  163. if (status.verbosity and Level)=V_Hint then
  164. hs:=hintstr;
  165. if (status.verbosity and Level)=V_Note then
  166. hs:=notestr;
  167. if (status.verbosity and Level)=V_Warning then
  168. hs:=warningstr;
  169. if (status.verbosity and Level)=V_Error then
  170. hs:=errorstr;
  171. if (status.verbosity and Level)=V_Fatal then
  172. hs:=fatalstr;
  173. end
  174. else
  175. begin
  176. if (status.verbosity and Level)=V_Hint then
  177. hs:=rh_warningstr;
  178. if (status.verbosity and Level)=V_Note then
  179. hs:=rh_warningstr;
  180. if (status.verbosity and Level)=V_Warning then
  181. hs:=rh_warningstr;
  182. if (status.verbosity and Level)=V_Error then
  183. hs:=rh_errorstr;
  184. if (status.verbosity and Level)=V_Fatal then
  185. hs:=rh_errorstr;
  186. end;
  187. if (Level<=V_ShowFile) and (status.currentsource<>'') and (status.currentline>0) then
  188. begin
  189. { Adding the column should not confuse RHIDE,
  190. even if it does not yet use it PM
  191. but only if it is after error or warning !! PM }
  192. if status.currentcolumn>0 then
  193. begin
  194. if status.use_gccoutput then
  195. hs:=gccfilename(status.currentsource)+':'+tostr(status.currentline)+': '+hs
  196. +tostr(status.currentcolumn)+': '
  197. else
  198. hs:=status.currentsource+'('+tostr(status.currentline)
  199. +','+tostr(status.currentcolumn)+') '+hs;
  200. end
  201. else
  202. begin
  203. if status.use_gccoutput then
  204. hs:=gccfilename(status.currentsource)+': '+hs+tostr(status.currentline)+': '
  205. else
  206. hs:=status.currentsource+'('+tostr(status.currentline)+') '+hs;
  207. end;
  208. end;
  209. { add the message to the text }
  210. hs:=hs+s;
  211. {$ifdef FPC}
  212. if status.use_stderr then
  213. begin
  214. writeln(stderr,hs);
  215. flush(stderr);
  216. end
  217. else
  218. {$endif}
  219. begin
  220. if status.use_redir then
  221. writeln(status.redirfile,hs)
  222. else
  223. writeln(hs);
  224. end;
  225. end;
  226. end;
  227. function def_internalerror(i : longint) : boolean;
  228. begin
  229. do_comment(V_Fatal,'Internal error '+tostr(i));
  230. def_internalerror:=true;
  231. end;
  232. end.
  233. {
  234. $Log$
  235. Revision 1.15 1999-01-15 12:27:23 peter
  236. * removed path from output, was there only for debugging
  237. Revision 1.14 1999/01/14 21:47:09 peter
  238. * status.currentmodule is now also updated
  239. + status.currentsourcepath
  240. Revision 1.13 1998/12/11 00:03:12 peter
  241. + globtype,tokens,version unit splitted from globals
  242. }