comphook.pas 8.6 KB

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