comphook.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. gccfilename[0]:=s[0];
  99. end;
  100. function tostr(i : longint) : string;
  101. var
  102. hs : string;
  103. begin
  104. str(i,hs);
  105. tostr:=hs;
  106. end;
  107. {****************************************************************************
  108. Predefined default Handlers
  109. ****************************************************************************}
  110. { predefined handler when then compiler stops }
  111. procedure def_stop;
  112. begin
  113. {$ifndef USEEXCEPT}
  114. Halt(1);
  115. {$else USEEXCEPT}
  116. Halt(1);
  117. {$endif USEEXCEPT}
  118. end;
  119. function def_status:boolean;
  120. begin
  121. def_status:=false; { never stop }
  122. { Status info?, Called every line }
  123. if ((status.verbosity and V_Status)<>0) then
  124. begin
  125. if (status.compiledlines=1) then
  126. WriteLn(memavail shr 10,' Kb Free');
  127. if (status.currentline>0) and (status.currentline mod 100=0) then
  128. {$ifdef FPC}
  129. WriteLn(status.currentline,' ',memavail shr 10,'/',system.heapsize shr 10,' Kb Free');
  130. {$else}
  131. WriteLn(status.currentline,' ',memavail shr 10,' Kb Free');
  132. {$endif}
  133. end
  134. end;
  135. Function def_comment(Level:Longint;const s:string):boolean;
  136. const
  137. { RHIDE expect gcc like error output }
  138. rh_errorstr='error: ';
  139. rh_warningstr='warning: ';
  140. fatalstr='Fatal: ';
  141. errorstr='Error: ';
  142. warningstr='Warning: ';
  143. notestr='Note: ';
  144. hintstr='Hint: ';
  145. var
  146. hs : string;
  147. begin
  148. def_comment:=false; { never stop }
  149. if (status.verbosity and Level)=Level then
  150. begin
  151. hs:='';
  152. if not(status.use_gccoutput) then
  153. begin
  154. if (status.verbosity and Level)=V_Hint then
  155. hs:=hintstr;
  156. if (status.verbosity and Level)=V_Note then
  157. hs:=notestr;
  158. if (status.verbosity and Level)=V_Warning then
  159. hs:=warningstr;
  160. if (status.verbosity and Level)=V_Error then
  161. hs:=errorstr;
  162. if (status.verbosity and Level)=V_Fatal then
  163. hs:=fatalstr;
  164. end
  165. else
  166. begin
  167. if (status.verbosity and Level)=V_Hint then
  168. hs:=rh_warningstr;
  169. if (status.verbosity and Level)=V_Note then
  170. hs:=rh_warningstr;
  171. if (status.verbosity and Level)=V_Warning then
  172. hs:=rh_warningstr;
  173. if (status.verbosity and Level)=V_Error then
  174. hs:=rh_errorstr;
  175. if (status.verbosity and Level)=V_Fatal then
  176. hs:=rh_errorstr;
  177. end;
  178. if (Level<=V_ShowFile) and (status.currentsource<>'') and (status.currentline>0) then
  179. begin
  180. { Adding the column should not confuse RHIDE,
  181. even if it does not yet use it PM
  182. but only if it is after error or warning !! PM }
  183. if status.currentcolumn>0 then
  184. begin
  185. if status.use_gccoutput then
  186. hs:=gccfilename(status.currentsource)+':'+tostr(status.currentline)+': '+hs
  187. +tostr(status.currentcolumn)+': '
  188. else
  189. hs:=status.currentsource+'('+tostr(status.currentline)
  190. +','+tostr(status.currentcolumn)+') '+hs;
  191. end
  192. else
  193. begin
  194. if status.use_gccoutput then
  195. hs:=gccfilename(status.currentsource)+': '+hs+tostr(status.currentline)+': '
  196. else
  197. hs:=status.currentsource+'('+tostr(status.currentline)+') '+hs;
  198. end;
  199. end;
  200. { add the message to the text }
  201. hs:=hs+s;
  202. {$ifdef FPC}
  203. if status.use_stderr then
  204. begin
  205. writeln(stderr,hs);
  206. flush(stderr);
  207. end
  208. else
  209. {$endif}
  210. begin
  211. if status.use_redir then
  212. writeln(status.redirfile,hs)
  213. else
  214. writeln(hs);
  215. end;
  216. end;
  217. end;
  218. function def_internalerror(i : longint) : boolean;
  219. begin
  220. do_comment(V_Fatal,'Internal error '+tostr(i));
  221. def_internalerror:=true;
  222. end;
  223. end.
  224. {
  225. $Log$
  226. Revision 1.10 1998-10-27 13:45:25 pierre
  227. * classes get a vmt allways
  228. * better error info (tried to remove
  229. several error strings introduced by the tpexcept handling)
  230. Revision 1.9 1998/10/26 17:15:16 pierre
  231. + added two level of longjump to
  232. allow clean freeing of used memory on errors
  233. Revision 1.8 1998/09/15 10:49:32 pierre
  234. merged from fixes branch
  235. Revision 1.7.2.1 1998/09/15 10:30:17 pierre
  236. RHIDE output corrected
  237. Revision 1.7 1998/09/04 17:34:21 pierre
  238. * bug with datalabel corrected
  239. + assembler errors better commented
  240. * one nested record crash removed
  241. Revision 1.6 1998/09/01 17:37:59 peter
  242. * nicer output when column=0
  243. Revision 1.5 1998/08/18 15:11:51 peter
  244. * recompiles again
  245. Revision 1.4 1998/08/18 14:17:08 pierre
  246. * bug about assigning the return value of a function to
  247. a procvar fixed : warning
  248. assigning a proc to a procvar need @ in FPC mode !!
  249. * missing file/line info restored
  250. Revision 1.3 1998/08/18 09:24:40 pierre
  251. * small warning position bug fixed
  252. * support_mmx switches splitting was missing
  253. * rhide error and warning output corrected
  254. Revision 1.2 1998/08/11 14:02:45 peter
  255. * don't write line if no sourcefile is set
  256. Revision 1.1 1998/08/10 10:18:24 peter
  257. + Compiler,Comphook unit which are the new interface units to the
  258. compiler
  259. Revision 1.14 1998/08/04 13:22:48 pierre
  260. * weird bug fixed :
  261. a pchar ' ' (simple space or any other letter) was found to
  262. be equal to a string of length zero !!!
  263. thus printing out non sense
  264. found that out while checking Control-C !!
  265. + added column info also in RHIDE format as
  266. it might be usefull later
  267. Revision 1.13 1998/07/14 14:47:12 peter
  268. * released NEWINPUT
  269. Revision 1.12 1998/07/07 11:20:19 peter
  270. + NEWINPUT for a better inputfile and scanner object
  271. Revision 1.11 1998/06/19 15:40:00 peter
  272. * bp7 fix
  273. Revision 1.10 1998/06/16 11:32:19 peter
  274. * small cosmetic fixes
  275. Revision 1.9 1998/05/23 01:21:33 peter
  276. + aktasmmode, aktoptprocessor, aktoutputformat
  277. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  278. + $LIBNAME to set the library name where the unit will be put in
  279. * splitted cgi386 a bit (codeseg to large for bp7)
  280. * nasm, tasm works again. nasm moved to ag386nsm.pas
  281. Revision 1.8 1998/05/21 19:33:38 peter
  282. + better procedure directive handling and only one table
  283. Revision 1.7 1998/05/12 10:47:01 peter
  284. * moved printstatus to verb_def
  285. + V_Normal which is between V_Error and V_Warning and doesn't have a
  286. prefix like error: warning: and is included in V_Default
  287. * fixed some messages
  288. * first time parameter scan is only for -v and -T
  289. - removed old style messages
  290. Revision 1.6 1998/05/11 13:07:58 peter
  291. + $ifdef NEWPPU for the new ppuformat
  292. + $define GDB not longer required
  293. * removed all warnings and stripped some log comments
  294. * no findfirst/findnext anymore to remove smartlink *.o files
  295. Revision 1.5 1998/04/30 15:59:43 pierre
  296. * GDB works again better :
  297. correct type info in one pass
  298. + UseTokenInfo for better source position
  299. * fixed one remaining bug in scanner for line counts
  300. * several little fixes
  301. Revision 1.4 1998/04/29 10:34:09 pierre
  302. + added some code for ansistring (not complete nor working yet)
  303. * corrected operator overloading
  304. * corrected nasm output
  305. + started inline procedures
  306. + added starstarn : use ** for exponentiation (^ gave problems)
  307. + started UseTokenInfo cond to get accurate positions
  308. }