comphook.pas 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. use_stderr,
  54. use_redir,
  55. use_gccoutput : boolean;
  56. { Redirection support }
  57. redirfile : text;
  58. end;
  59. var
  60. status : tcompilerstatus;
  61. { Default Functions }
  62. procedure def_stop;
  63. Function def_status:boolean;
  64. Function def_comment(Level:Longint;const s:string):boolean;
  65. function def_internalerror(i:longint):boolean;
  66. { Function redirecting for IDE support }
  67. type
  68. tstopprocedure = procedure;
  69. tstatusfunction = function:boolean;
  70. tcommentfunction = function(Level:Longint;const s:string):boolean;
  71. tinternalerrorfunction = function(i:longint):boolean;
  72. const
  73. do_stop : tstopprocedure = def_stop;
  74. do_status : tstatusfunction = def_status;
  75. do_comment : tcommentfunction = def_comment;
  76. do_internalerror : tinternalerrorfunction = def_internalerror;
  77. implementation
  78. {****************************************************************************
  79. Helper Routines
  80. ****************************************************************************}
  81. function gccfilename(const s : string) : string;
  82. var
  83. i : longint;
  84. begin
  85. for i:=1to length(s) do
  86. begin
  87. case s[i] of
  88. '\' : gccfilename[i]:='/';
  89. 'A'..'Z' : gccfilename[i]:=chr(ord(s[i])+32);
  90. else
  91. gccfilename[i]:=s[i];
  92. end;
  93. end;
  94. gccfilename[0]:=s[0];
  95. end;
  96. function tostr(i : longint) : string;
  97. var
  98. hs : string;
  99. begin
  100. str(i,hs);
  101. tostr:=hs;
  102. end;
  103. {****************************************************************************
  104. Predefined default Handlers
  105. ****************************************************************************}
  106. { predefined handler when then compiler stops }
  107. procedure def_stop;
  108. begin
  109. Halt(1);
  110. end;
  111. function def_status:boolean;
  112. begin
  113. def_status:=false; { never stop }
  114. { Status info?, Called every line }
  115. if ((status.verbosity and V_Status)<>0) then
  116. begin
  117. if (status.compiledlines=1) then
  118. WriteLn(memavail shr 10,' Kb Free');
  119. if (status.currentline>0) and (status.currentline mod 100=0) then
  120. {$ifdef FPC}
  121. WriteLn(status.currentline,' ',memavail shr 10,'/',system.heapsize shr 10,' Kb Free');
  122. {$else}
  123. WriteLn(status.currentline,' ',memavail shr 10,' Kb Free');
  124. {$endif}
  125. end
  126. end;
  127. Function def_comment(Level:Longint;const s:string):boolean;
  128. const
  129. { RHIDE expect gcc like error output }
  130. rh_errorstr='error: ';
  131. rh_warningstr='warning: ';
  132. fatalstr='Fatal: ';
  133. errorstr='Error: ';
  134. warningstr='Warning: ';
  135. notestr='Note: ';
  136. hintstr='Hint: ';
  137. var
  138. hs : string;
  139. begin
  140. def_comment:=false; { never stop }
  141. if (status.verbosity and Level)=Level then
  142. begin
  143. hs:='';
  144. if not(status.use_gccoutput) then
  145. begin
  146. if (status.verbosity and Level)=V_Hint then
  147. hs:=hintstr;
  148. if (status.verbosity and Level)=V_Note then
  149. hs:=notestr;
  150. if (status.verbosity and Level)=V_Warning then
  151. hs:=warningstr;
  152. if (status.verbosity and Level)=V_Error then
  153. hs:=errorstr;
  154. if (status.verbosity and Level)=V_Fatal then
  155. hs:=fatalstr;
  156. end
  157. else
  158. begin
  159. if (status.verbosity and Level)=V_Hint then
  160. hs:=rh_warningstr;
  161. if (status.verbosity and Level)=V_Note then
  162. hs:=rh_warningstr;
  163. if (status.verbosity and Level)=V_Warning then
  164. hs:=rh_warningstr;
  165. if (status.verbosity and Level)=V_Error then
  166. hs:=rh_errorstr;
  167. if (status.verbosity and Level)=V_Fatal then
  168. hs:=rh_errorstr;
  169. end;
  170. if (Level<=V_ShowFile) and (status.currentline>0) then
  171. begin
  172. { Adding the column should not confuse RHIDE,
  173. even if it does not yet use it PM }
  174. if status.use_gccoutput then
  175. hs:=gccfilename(status.currentsource)+':'+tostr(status.currentline)
  176. +':'+tostr(status.currentcolumn)+': '+hs
  177. else
  178. hs:=status.currentsource+'('+tostr(status.currentline)
  179. +','+tostr(status.currentcolumn)+') '+hs;
  180. end;
  181. { add the message to the text }
  182. hs:=hs+s;
  183. {$ifdef FPC}
  184. if status.use_stderr then
  185. begin
  186. writeln(stderr,hs);
  187. flush(stderr);
  188. end
  189. else
  190. {$endif}
  191. begin
  192. if status.use_redir then
  193. writeln(status.redirfile,hs)
  194. else
  195. writeln(hs);
  196. end;
  197. end;
  198. end;
  199. function def_internalerror(i : longint) : boolean;
  200. begin
  201. do_comment(V_Fatal,'Internal error '+tostr(i));
  202. def_internalerror:=true;
  203. end;
  204. end.
  205. {
  206. $Log$
  207. Revision 1.1 1998-08-10 10:18:24 peter
  208. + Compiler,Comphook unit which are the new interface units to the
  209. compiler
  210. Revision 1.14 1998/08/04 13:22:48 pierre
  211. * weird bug fixed :
  212. a pchar ' ' (simple space or any other letter) was found to
  213. be equal to a string of length zero !!!
  214. thus printing out non sense
  215. found that out while checking Control-C !!
  216. + added column info also in RHIDE format as
  217. it might be usefull later
  218. Revision 1.13 1998/07/14 14:47:12 peter
  219. * released NEWINPUT
  220. Revision 1.12 1998/07/07 11:20:19 peter
  221. + NEWINPUT for a better inputfile and scanner object
  222. Revision 1.11 1998/06/19 15:40:00 peter
  223. * bp7 fix
  224. Revision 1.10 1998/06/16 11:32:19 peter
  225. * small cosmetic fixes
  226. Revision 1.9 1998/05/23 01:21:33 peter
  227. + aktasmmode, aktoptprocessor, aktoutputformat
  228. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  229. + $LIBNAME to set the library name where the unit will be put in
  230. * splitted cgi386 a bit (codeseg to large for bp7)
  231. * nasm, tasm works again. nasm moved to ag386nsm.pas
  232. Revision 1.8 1998/05/21 19:33:38 peter
  233. + better procedure directive handling and only one table
  234. Revision 1.7 1998/05/12 10:47:01 peter
  235. * moved printstatus to verb_def
  236. + V_Normal which is between V_Error and V_Warning and doesn't have a
  237. prefix like error: warning: and is included in V_Default
  238. * fixed some messages
  239. * first time parameter scan is only for -v and -T
  240. - removed old style messages
  241. Revision 1.6 1998/05/11 13:07:58 peter
  242. + $ifdef NEWPPU for the new ppuformat
  243. + $define GDB not longer required
  244. * removed all warnings and stripped some log comments
  245. * no findfirst/findnext anymore to remove smartlink *.o files
  246. Revision 1.5 1998/04/30 15:59:43 pierre
  247. * GDB works again better :
  248. correct type info in one pass
  249. + UseTokenInfo for better source position
  250. * fixed one remaining bug in scanner for line counts
  251. * several little fixes
  252. Revision 1.4 1998/04/29 10:34:09 pierre
  253. + added some code for ansistring (not complete nor working yet)
  254. * corrected operator overloading
  255. * corrected nasm output
  256. + started inline procedures
  257. + added starstarn : use ** for exponentiation (^ gave problems)
  258. + started UseTokenInfo cond to get accurate positions
  259. }