browser.pas 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by the FPC development team
  4. Support routines for the browser
  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. {$ifdef TP}
  19. {$N+,E+}
  20. {$endif}
  21. unit browser;
  22. interface
  23. uses
  24. cobjects,files;
  25. const
  26. {$ifdef TP}
  27. logbufsize = 1024;
  28. {$else}
  29. logbufsize = 16384;
  30. {$endif}
  31. type
  32. pref = ^tref;
  33. tref = object
  34. nextref : pref;
  35. posinfo : tfileposinfo;
  36. moduleindex : word;
  37. constructor init(ref:pref;pos:pfileposinfo);
  38. destructor done; virtual;
  39. function get_file_line : string;
  40. end;
  41. pbrowser=^tbrowser;
  42. tbrowser=object
  43. fname : string;
  44. logopen : boolean;
  45. f : file;
  46. buf : pchar;
  47. bufidx : longint;
  48. identidx : longint;
  49. constructor init;
  50. destructor done;
  51. procedure setfilename(const fn:string);
  52. procedure createlog;
  53. procedure flushlog;
  54. procedure addlog(const s:string);
  55. procedure addlogrefs(p:pref);
  56. procedure closelog;
  57. procedure ident;
  58. procedure unident;
  59. end;
  60. var
  61. browse : tbrowser;
  62. function get_source_file(moduleindex,fileindex : word) : pinputfile;
  63. implementation
  64. uses
  65. globals,systems,verbose;
  66. {****************************************************************************
  67. TRef
  68. ****************************************************************************}
  69. constructor tref.init(ref :pref;pos : pfileposinfo);
  70. begin
  71. nextref:=nil;
  72. if assigned(pos) then
  73. posinfo:=pos^;
  74. if assigned(current_module) then
  75. moduleindex:=current_module^.unit_index;
  76. if assigned(ref) then
  77. ref^.nextref:=@self;
  78. end;
  79. destructor tref.done;
  80. var
  81. inputfile : pinputfile;
  82. ref : pref;
  83. begin
  84. inputfile:=get_source_file(moduleindex,posinfo.fileindex);
  85. if inputfile<>nil then
  86. dec(inputfile^.ref_count);
  87. ref:=@self;
  88. if assigned(ref^.nextref) then
  89. dispose(ref^.nextref,done);
  90. nextref:=nil;
  91. end;
  92. function tref.get_file_line : string;
  93. var
  94. inputfile : pinputfile;
  95. begin
  96. get_file_line:='';
  97. inputfile:=get_source_file(moduleindex,posinfo.fileindex);
  98. if assigned(inputfile) then
  99. if Use_Rhide then
  100. get_file_line:=lower(inputfile^.name^+inputfile^.ext^)
  101. +':'+tostr(posinfo.line)+':'+tostr(posinfo.column)+':'
  102. else
  103. get_file_line:=inputfile^.name^+inputfile^.ext^
  104. +'('+tostr(posinfo.line)+','+tostr(posinfo.column)+')'
  105. else
  106. if Use_Rhide then
  107. get_file_line:='file_unknown:'
  108. +tostr(posinfo.line)+':'+tostr(posinfo.column)+':'
  109. else
  110. get_file_line:='file_unknown('
  111. +tostr(posinfo.line)+','+tostr(posinfo.column)+')'
  112. end;
  113. {****************************************************************************
  114. TBrowser
  115. ****************************************************************************}
  116. constructor tbrowser.init;
  117. begin
  118. fname:=FixFileName('browser.log');
  119. logopen:=false;
  120. end;
  121. destructor tbrowser.done;
  122. begin
  123. if logopen then
  124. closelog;
  125. end;
  126. procedure tbrowser.setfilename(const fn:string);
  127. begin
  128. fname:=FixFileName(fn);
  129. end;
  130. procedure tbrowser.createlog;
  131. begin
  132. if logopen then
  133. closelog;
  134. assign(f,fname);
  135. {$I-}
  136. rewrite(f,1);
  137. {$I+}
  138. if ioresult<>0 then
  139. exit;
  140. logopen:=true;
  141. getmem(buf,logbufsize);
  142. bufidx:=0;
  143. identidx:=0;
  144. end;
  145. procedure tbrowser.flushlog;
  146. begin
  147. if logopen then
  148. blockwrite(f,buf^,bufidx);
  149. bufidx:=0;
  150. end;
  151. procedure tbrowser.closelog;
  152. begin
  153. if logopen then
  154. begin
  155. flushlog;
  156. close(f);
  157. freemem(buf,logbufsize);
  158. logopen:=false;
  159. end;
  160. end;
  161. procedure tbrowser.addlog(const s:string);
  162. begin
  163. if not logopen then
  164. exit;
  165. { add ident }
  166. if identidx>0 then
  167. begin
  168. if bufidx+identidx>logbufsize then
  169. flushlog;
  170. fillchar(buf[bufidx],identidx,' ');
  171. inc(bufidx,identidx);
  172. end;
  173. { add text }
  174. if bufidx+length(s)>logbufsize-2 then
  175. flushlog;
  176. move(s[1],buf[bufidx],length(s));
  177. inc(bufidx,length(s));
  178. { add crlf }
  179. buf[bufidx]:=target_os.newline[1];
  180. inc(bufidx);
  181. if length(target_os.newline)=2 then
  182. begin
  183. buf[bufidx]:=target_os.newline[2];
  184. inc(bufidx);
  185. end;
  186. end;
  187. procedure tbrowser.addlogrefs(p:pref);
  188. var
  189. ref : pref;
  190. begin
  191. ref:=p;
  192. Ident;
  193. while assigned(ref) do
  194. begin
  195. Browse.AddLog(ref^.get_file_line);
  196. ref:=ref^.nextref;
  197. end;
  198. Unident;
  199. end;
  200. procedure tbrowser.ident;
  201. begin
  202. inc(identidx,2);
  203. end;
  204. procedure tbrowser.unident;
  205. begin
  206. dec(identidx,2);
  207. end;
  208. {****************************************************************************
  209. Helpers
  210. ****************************************************************************}
  211. function get_source_file(moduleindex,fileindex : word) : pinputfile;
  212. var
  213. hp : pmodule;
  214. f : pinputfile;
  215. begin
  216. hp:=pmodule(loaded_units.first);
  217. while assigned(hp) and (hp^.unit_index<>moduleindex) do
  218. hp:=pmodule(hp^.next);
  219. get_source_file:=nil;
  220. if not assigned(hp) then
  221. exit;
  222. f:=pinputfile(hp^.sourcefiles.files);
  223. while assigned(f) do
  224. begin
  225. if f^.ref_index=fileindex then
  226. begin
  227. get_source_file:=f;
  228. exit;
  229. end;
  230. f:=pinputfile(f^._next);
  231. end;
  232. end;
  233. begin
  234. browse.init
  235. end.
  236. {
  237. $Log$
  238. Revision 1.5 1998-06-13 00:10:04 peter
  239. * working browser and newppu
  240. * some small fixes against crashes which occured in bp7 (but not in
  241. fpc?!)
  242. Revision 1.4 1998/06/11 10:11:57 peter
  243. * -gb works again
  244. Revision 1.3 1998/05/20 09:42:32 pierre
  245. + UseTokenInfo now default
  246. * unit in interface uses and implementation uses gives error now
  247. * only one error for unknown symbol (uses lastsymknown boolean)
  248. the problem came from the label code !
  249. + first inlined procedures and function work
  250. (warning there might be allowed cases were the result is still wrong !!)
  251. * UseBrower updated gives a global list of all position of all used symbols
  252. with switch -gb
  253. Revision 1.2 1998/04/30 15:59:39 pierre
  254. * GDB works again better :
  255. correct type info in one pass
  256. + UseTokenInfo for better source position
  257. * fixed one remaining bug in scanner for line counts
  258. * several little fixes
  259. }