comphook.pas 7.1 KB

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