comphook.pas 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. 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. {$ifdef DEBUG}
  74. { allow easy stopping in GDB
  75. using
  76. b DEF_GDB_STOP
  77. cond 1 LEVEL <= 8 }
  78. procedure def_gdb_stop(level : longint);
  79. {$endif DEBUG}
  80. { Function redirecting for IDE support }
  81. type
  82. tstopprocedure = procedure;
  83. thaltprocedure = procedure(i : longint);
  84. tstatusfunction = function:boolean;
  85. tcommentfunction = function(Level:Longint;const s:string):boolean;
  86. tinternalerrorfunction = function(i:longint):boolean;
  87. const
  88. do_stop : tstopprocedure = def_stop;
  89. do_halt : thaltprocedure = def_halt;
  90. do_status : tstatusfunction = def_status;
  91. do_comment : tcommentfunction = def_comment;
  92. do_internalerror : tinternalerrorfunction = def_internalerror;
  93. implementation
  94. {$ifdef USEEXCEPT}
  95. uses tpexcept;
  96. {$endif USEEXCEPT}
  97. {****************************************************************************
  98. Helper Routines
  99. ****************************************************************************}
  100. function gccfilename(const s : string) : string;
  101. var
  102. i : longint;
  103. begin
  104. for i:=1to length(s) do
  105. begin
  106. case s[i] of
  107. '\' : gccfilename[i]:='/';
  108. 'A'..'Z' : gccfilename[i]:=chr(ord(s[i])+32);
  109. else
  110. gccfilename[i]:=s[i];
  111. end;
  112. end;
  113. {$ifndef TP}
  114. {$ifopt H+}
  115. setlength(gccfilename,length(s));
  116. {$else}
  117. gccfilename[0]:=s[0];
  118. {$endif}
  119. {$else}
  120. gccfilename[0]:=s[0];
  121. {$endif}
  122. end;
  123. function tostr(i : longint) : string;
  124. var
  125. hs : string;
  126. begin
  127. str(i,hs);
  128. tostr:=hs;
  129. end;
  130. {****************************************************************************
  131. Predefined default Handlers
  132. ****************************************************************************}
  133. { predefined handler when then compiler stops }
  134. procedure def_stop;
  135. begin
  136. {$ifndef USEEXCEPT}
  137. Halt(1);
  138. {$else USEEXCEPT}
  139. Halt(1);
  140. {$endif USEEXCEPT}
  141. end;
  142. {$ifdef DEBUG}
  143. { allow easy stopping in GDB
  144. using
  145. b DEF_GDB_STOP
  146. cond 1 LEVEL <= 8 }
  147. procedure def_gdb_stop(level : longint);
  148. begin
  149. { Its only a dummy for GDB }
  150. end;
  151. {$endif DEBUG}
  152. procedure def_halt(i : longint);
  153. begin
  154. halt(i);
  155. end;
  156. function def_status:boolean;
  157. begin
  158. def_status:=false; { never stop }
  159. { Status info?, Called every line }
  160. if ((status.verbosity and V_Status)<>0) then
  161. begin
  162. {$ifndef Delphi}
  163. if (status.compiledlines=1) then
  164. WriteLn(memavail shr 10,' Kb Free');
  165. {$endif Delphi}
  166. if (status.currentline>0) and (status.currentline mod 100=0) then
  167. {$ifdef FPC}
  168. WriteLn(status.currentline,' ',memavail shr 10,'/',system.heapsize shr 10,' Kb Free');
  169. {$else}
  170. {$ifndef Delphi}
  171. WriteLn(status.currentline,' ',memavail shr 10,' Kb Free');
  172. {$endif Delphi}
  173. {$endif}
  174. end
  175. end;
  176. Function def_comment(Level:Longint;const s:string):boolean;
  177. const
  178. { RHIDE expect gcc like error output }
  179. rh_errorstr='error: ';
  180. rh_warningstr='warning: ';
  181. fatalstr='Fatal: ';
  182. errorstr='Error: ';
  183. warningstr='Warning: ';
  184. notestr='Note: ';
  185. hintstr='Hint: ';
  186. var
  187. hs : string;
  188. begin
  189. def_comment:=false; { never stop }
  190. if (status.verbosity and Level)=Level then
  191. begin
  192. hs:='';
  193. if not(status.use_gccoutput) then
  194. begin
  195. if (status.verbosity and Level)=V_Hint then
  196. hs:=hintstr;
  197. if (status.verbosity and Level)=V_Note then
  198. hs:=notestr;
  199. if (status.verbosity and Level)=V_Warning then
  200. hs:=warningstr;
  201. if (status.verbosity and Level)=V_Error then
  202. hs:=errorstr;
  203. if (status.verbosity and Level)=V_Fatal then
  204. hs:=fatalstr;
  205. end
  206. else
  207. begin
  208. if (status.verbosity and Level)=V_Hint then
  209. hs:=rh_warningstr;
  210. if (status.verbosity and Level)=V_Note then
  211. hs:=rh_warningstr;
  212. if (status.verbosity and Level)=V_Warning then
  213. hs:=rh_warningstr;
  214. if (status.verbosity and Level)=V_Error then
  215. hs:=rh_errorstr;
  216. if (status.verbosity and Level)=V_Fatal then
  217. hs:=rh_errorstr;
  218. end;
  219. if (Level<=V_ShowFile) and (status.currentsource<>'') and (status.currentline>0) then
  220. begin
  221. { Adding the column should not confuse RHIDE,
  222. even if it does not yet use it PM
  223. but only if it is after error or warning !! PM }
  224. if status.currentcolumn>0 then
  225. begin
  226. if status.use_gccoutput then
  227. hs:=gccfilename(status.currentsource)+':'+tostr(status.currentline)+': '+hs
  228. +tostr(status.currentcolumn)+': '
  229. else
  230. hs:=status.currentsource+'('+tostr(status.currentline)
  231. +','+tostr(status.currentcolumn)+') '+hs;
  232. end
  233. else
  234. begin
  235. if status.use_gccoutput then
  236. hs:=gccfilename(status.currentsource)+': '+hs+tostr(status.currentline)+': '
  237. else
  238. hs:=status.currentsource+'('+tostr(status.currentline)+') '+hs;
  239. end;
  240. end;
  241. { add the message to the text }
  242. hs:=hs+s;
  243. {$ifdef FPC}
  244. if status.use_stderr then
  245. begin
  246. writeln(stderr,hs);
  247. flush(stderr);
  248. end
  249. else
  250. {$endif}
  251. begin
  252. if status.use_redir then
  253. writeln(status.redirfile,hs)
  254. else
  255. writeln(hs);
  256. end;
  257. {$ifdef DEBUG}
  258. def_gdb_stop(level);
  259. {$endif DEBUG}
  260. end;
  261. end;
  262. function def_internalerror(i : longint) : boolean;
  263. begin
  264. do_comment(V_Fatal,'Internal error '+tostr(i));
  265. def_internalerror:=true;
  266. end;
  267. end.
  268. {
  269. $Log$
  270. Revision 1.21 2000-02-09 13:22:50 peter
  271. * log truncated
  272. Revision 1.20 2000/01/07 01:14:23 peter
  273. * updated copyright to 2000
  274. Revision 1.19 1999/11/18 15:34:45 pierre
  275. * Notes/Hints for local syms changed to
  276. Set_varstate function
  277. Revision 1.18 1999/09/07 14:03:48 pierre
  278. + added do_halt procedure
  279. Revision 1.17 1999/08/05 16:52:53 peter
  280. * V_Fatal=1, all other V_ are also increased
  281. * Check for local procedure when assigning procvar
  282. * fixed comment parsing because directives
  283. * oldtp mode directives better supported
  284. * added some messages to errore.msg
  285. }