OSDN Git Service

Merge from gcc-2.8
[pf3gnuchains/gcc-fork.git] / gcc / cp / xref.c
1 /* Code for handling XREF output from GNU C++.
2    Copyright (C) 1992, 1993, 1994, 1995, 1997 Free Software Foundation, Inc.
3    Contributed by Michael Tiemann (tiemann@cygnus.com)
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22
23 #include "config.h"
24 #include "tree.h"
25 #include <stdio.h>
26 #include "cp-tree.h"
27 #include "input.h"
28
29 #include <ctype.h>
30
31 #ifdef HAVE_STDLIB_H
32 #include <stdlib.h>
33 #endif
34
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38
39 extern char *getpwd PROTO((void));
40
41 #ifdef HAVE_STRING_H
42 #include <string.h>
43 #endif
44
45 #ifdef NEED_DECLARATION_RINDEX
46 extern char *rindex ();
47 #endif
48
49 /* The character(s) used to join a directory specification (obtained with
50    getwd or equivalent) with a non-absolute file name.  */
51
52 #ifndef FILE_NAME_JOINER
53 #define FILE_NAME_JOINER "/"
54 #endif
55
56 /* Nonzero if NAME as a file name is absolute.  */
57 #ifndef FILE_NAME_ABSOLUTE_P
58 #define FILE_NAME_ABSOLUTE_P(NAME) (NAME[0] == '/')
59 #endif
60
61 /* For cross referencing.  */
62
63 int flag_gnu_xref;
64
65 /************************************************************************/
66 /*                                                                      */
67 /*      Common definitions                                              */
68 /*                                                                      */
69 /************************************************************************/
70
71 #ifndef TRUE
72 #define TRUE 1
73 #endif
74 #ifndef FALSE
75 #define FALSE 0
76 #endif
77
78 #define PALLOC(typ) ((typ *) calloc(1,sizeof(typ)))
79
80
81 /* Return a malloc'd copy of STR.  */
82 #define SALLOC(str) \
83  ((char *) ((str) == NULL ? NULL        \
84             : (char *) strcpy ((char *) malloc (strlen ((str)) + 1), (str))))
85 #define SFREE(str) (str != NULL && (free(str),0))
86
87 #define STREQL(s1,s2) (strcmp((s1),(s2)) == 0)
88 #define STRNEQ(s1,s2) (strcmp((s1),(s2)) != 0)
89 #define STRLSS(s1,s2) (strcmp((s1),(s2)) < 0)
90 #define STRLEQ(s1,s2) (strcmp((s1),(s2)) <= 0)
91 #define STRGTR(s1,s2) (strcmp((s1),(s2)) > 0)
92 #define STRGEQ(s1,s2) (strcmp((s1),(s2)) >= 0)
93
94 /************************************************************************/
95 /*                                                                      */
96 /*      Type definitions                                                */
97 /*                                                                      */
98 /************************************************************************/
99
100
101 typedef struct _XREF_FILE *     XREF_FILE;
102 typedef struct _XREF_SCOPE *    XREF_SCOPE;
103
104 typedef struct _XREF_FILE
105 {
106   char *name;
107   char *outname;
108   XREF_FILE next;
109 } XREF_FILE_INFO;
110
111 typedef struct _XREF_SCOPE
112 {
113   int gid;
114   int lid;
115   XREF_FILE file;
116   int start;
117   XREF_SCOPE outer;
118 } XREF_SCOPE_INFO;
119
120 /************************************************************************/
121 /*                                                                      */
122 /*      Local storage                                                   */
123 /*                                                                      */
124 /************************************************************************/
125
126 static  char            doing_xref = 0;
127 static  FILE *          xref_file = NULL;
128 static  char            xref_name[1024];
129 static  XREF_FILE       all_files = NULL;
130 static  char *          wd_name = NULL;
131 static  XREF_SCOPE      cur_scope = NULL;
132 static  int     scope_ctr = 0;
133 static  XREF_FILE       last_file = NULL;
134 static  tree            last_fndecl = NULL;
135
136 /************************************************************************/
137 /*                                                                      */
138 /*      Forward definitions                                             */
139 /*                                                                      */
140 /************************************************************************/
141 static  void            gen_assign PROTO((XREF_FILE, tree));
142 static  XREF_FILE       find_file PROTO((char *));
143 static  char *          filename PROTO((XREF_FILE));
144 static  char *          fctname PROTO((tree));
145 static  char *          declname PROTO((tree));
146 static  void            simplify_type PROTO((char *));
147 static  char *          fixname PROTO((char *, char *));
148 static  void            open_xref_file PROTO((char *));
149
150 /* Start cross referencing.  FILE is the name of the file we xref.  */
151
152 void
153 GNU_xref_begin (file)
154    char *file;
155 {
156   doing_xref = 1;
157
158   if (file != NULL && STRNEQ (file,"-"))
159     {
160       open_xref_file(file);
161       GNU_xref_file(file);
162     }
163 }
164
165 /* Finish cross-referencing.  ERRCNT is the number of errors
166    we encountered.  */
167
168 void
169 GNU_xref_end (ect)
170    int ect;
171 {
172   XREF_FILE xf;
173
174   if (!doing_xref) return;
175
176   xf = find_file (input_filename);
177   if (xf == NULL) return;
178
179   while (cur_scope != NULL)
180     GNU_xref_end_scope(cur_scope->gid,0,0,0);
181
182   doing_xref = 0;
183
184   if (xref_file == NULL) return;
185
186   fclose (xref_file);
187
188   xref_file = NULL;
189   all_files = NULL;
190
191   if (ect > 0) unlink (xref_name);
192 }
193
194 /* Write out xref for file named NAME.  */
195
196 void
197 GNU_xref_file (name)
198    char *name;
199 {
200   XREF_FILE xf;
201
202   if (!doing_xref || name == NULL) return;
203
204   if (xref_file == NULL)
205     {
206       open_xref_file (name);
207       if (!doing_xref) return;
208     }
209
210   if (all_files == NULL)
211     fprintf(xref_file,"SCP * 0 0 0 0 RESET\n");
212
213   xf = find_file (name);
214   if (xf != NULL) return;
215
216   xf = PALLOC (XREF_FILE_INFO);
217   xf->name = SALLOC (name);
218   xf->next = all_files;
219   all_files = xf;
220
221   if (wd_name == NULL)
222     wd_name = getpwd ();
223
224   if (FILE_NAME_ABSOLUTE_P (name) || ! wd_name)
225     xf->outname = xf->name;
226   else
227     {
228       char *nmbuf
229         = (char *) malloc (strlen (wd_name) + strlen (FILE_NAME_JOINER)
230                            + strlen (name) + 1);
231       sprintf (nmbuf, "%s%s%s", wd_name, FILE_NAME_JOINER, name);
232       name = nmbuf;
233       xf->outname = nmbuf;
234     }
235
236   fprintf (xref_file, "FIL %s %s 0\n", name, wd_name);
237
238   filename (xf);
239   fctname (NULL);
240 }
241
242 /* Start a scope identified at level ID.  */
243
244 void
245 GNU_xref_start_scope (id)
246    HOST_WIDE_INT id;
247 {
248   XREF_SCOPE xs;
249   XREF_FILE xf;
250
251   if (!doing_xref) return;
252   xf = find_file (input_filename);
253
254   xs = PALLOC (XREF_SCOPE_INFO);
255   xs->file = xf;
256   xs->start = lineno;
257   if (xs->start <= 0) xs->start = 1;
258   xs->gid = id;
259   xs->lid = ++scope_ctr;
260   xs->outer = cur_scope;
261   cur_scope = xs;
262 }
263
264 /* Finish a scope at level ID.
265    INID is ???
266    PRM is ???
267    KEEP is nonzero iff this scope is retained (nonzero if it's
268    a compiler-generated invisible scope).
269    TRNS is ???  */
270
271 void
272 GNU_xref_end_scope (id,inid,prm,keep)
273    HOST_WIDE_INT id;
274    HOST_WIDE_INT inid;
275    int prm,keep;
276 {
277   XREF_FILE xf;
278   XREF_SCOPE xs,lxs,oxs;
279   char *stype;
280
281   if (!doing_xref) return;
282   xf = find_file (input_filename);
283   if (xf == NULL) return;
284
285   lxs = NULL;
286   for (xs = cur_scope; xs != NULL; xs = xs->outer)
287     {
288       if (xs->gid == id) break;
289       lxs = xs;
290     }
291   if (xs == NULL) return;
292
293   if (inid != 0) {
294     for (oxs = cur_scope; oxs != NULL; oxs = oxs->outer) {
295       if (oxs->gid == inid) break;
296     }
297     if (oxs == NULL) return;
298     inid = oxs->lid;
299   }
300
301   if (prm == 2) stype = "SUE";
302   else if (prm != 0) stype = "ARGS";
303   else if (keep == 2 || inid != 0) stype = "INTERN";
304   else stype = "EXTERN";
305
306   fprintf (xref_file,"SCP %s %d %d %d %d %s\n",
307            filename (xf), xs->start, lineno,xs->lid, inid, stype);
308
309   if (lxs == NULL) cur_scope = xs->outer;
310   else lxs->outer = xs->outer;
311
312   free (xs);
313 }
314
315 /* Output a reference to NAME in FNDECL.  */
316
317 void
318 GNU_xref_ref (fndecl,name)
319    tree fndecl;
320    char *name;
321 {
322   XREF_FILE xf;
323
324   if (!doing_xref) return;
325   xf = find_file (input_filename);
326   if (xf == NULL) return;
327
328   fprintf (xref_file, "REF %s %d %s %s\n",
329            filename (xf), lineno, fctname (fndecl), name);
330 }
331
332 /* Output a reference to DECL in FNDECL.  */
333
334 void
335 GNU_xref_decl (fndecl,decl)
336    tree fndecl;
337    tree decl;
338 {
339   XREF_FILE xf,xf1;
340   char *cls;
341   char *name;
342   char buf[10240];
343   int uselin;
344
345   if (!doing_xref) return;
346   xf = find_file (input_filename);
347   if (xf == NULL) return;
348
349   uselin = FALSE;
350
351   if (TREE_CODE (decl) == TYPE_DECL) cls = "TYPEDEF";
352   else if (TREE_CODE (decl) == FIELD_DECL) cls = "FIELD";
353   else if (TREE_CODE (decl) == VAR_DECL)
354     {
355       if (fndecl == NULL && TREE_STATIC(decl)
356           && TREE_READONLY(decl) && DECL_INITIAL(decl) != 0
357           && !TREE_PUBLIC(decl) && !DECL_EXTERNAL(decl)
358           && DECL_MODE(decl) != BLKmode) cls = "CONST";
359       else if (DECL_EXTERNAL(decl)) cls = "EXTERN";
360       else if (TREE_PUBLIC(decl)) cls = "EXTDEF";
361       else if (TREE_STATIC(decl)) cls = "STATIC";
362       else if (DECL_REGISTER(decl)) cls = "REGISTER";
363       else cls = "AUTO";
364     }
365   else if (TREE_CODE (decl) == PARM_DECL) cls = "PARAM";
366   else if (TREE_CODE (decl) == FIELD_DECL) cls = "FIELD";
367   else if (TREE_CODE (decl) == CONST_DECL) cls = "CONST";
368   else if (TREE_CODE (decl) == FUNCTION_DECL)
369     {
370       if (DECL_EXTERNAL (decl)) cls = "EXTERN";
371       else if (TREE_PUBLIC (decl)) cls = "EFUNCTION";
372       else cls = "SFUNCTION";
373     }
374   else if (TREE_CODE (decl) == LABEL_DECL) cls = "LABEL";
375   else if (TREE_CODE (decl) == UNION_TYPE)
376     {
377       cls = "UNIONID";
378       decl = TYPE_NAME (decl);
379       uselin = TRUE;
380     }
381   else if (TREE_CODE (decl) == RECORD_TYPE)
382     {
383       if (CLASSTYPE_DECLARED_CLASS (decl)) cls = "CLASSID";
384       else if (IS_SIGNATURE (decl)) cls = "SIGNATUREID";
385       else cls = "STRUCTID";
386       decl = TYPE_NAME (decl);
387       uselin = TRUE;
388     }
389   else if (TREE_CODE (decl) == ENUMERAL_TYPE)
390     {
391       cls = "ENUMID";
392       decl = TYPE_NAME (decl);
393       uselin = TRUE;
394     }
395   else if (TREE_CODE (decl) == TEMPLATE_DECL)
396     {
397       if (TREE_CODE (DECL_RESULT (decl)) == TYPE_DECL)
398         cls = "CLASSTEMP";
399       else if (TREE_CODE (DECL_RESULT (decl)) == FUNCTION_DECL)
400         cls = "FUNCTEMP";
401       else if (TREE_CODE (DECL_RESULT (decl)) == VAR_DECL)
402         cls = "VARTEMP";
403       else
404         my_friendly_abort (358);
405       uselin = TRUE;
406     }
407   else cls = "UNKNOWN";
408
409   if (decl == NULL || DECL_NAME (decl) == NULL) return;
410
411   if (uselin && decl->decl.linenum > 0 && decl->decl.filename != NULL)
412     {
413       xf1 = find_file (decl->decl.filename);
414       if (xf1 != NULL)
415         {
416           lineno = decl->decl.linenum;
417           xf = xf1;
418         }
419     }
420
421   if (DECL_ASSEMBLER_NAME (decl))
422     name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
423   else
424     name = IDENTIFIER_POINTER (DECL_NAME (decl));
425
426   strcpy (buf, type_as_string (TREE_TYPE (decl), 0));
427   simplify_type (buf);
428
429   fprintf (xref_file, "DCL %s %d %s %d %s %s %s\n",
430            filename(xf), lineno, name,
431            (cur_scope != NULL ? cur_scope->lid : 0),
432            cls, fctname(fndecl), buf);
433
434   if (STREQL (cls, "STRUCTID") || STREQL (cls, "UNIONID")
435       || STREQL (cls, "SIGNATUREID"))
436     {
437       cls = "CLASSID";
438       fprintf (xref_file, "DCL %s %d %s %d %s %s %s\n",
439                filename(xf), lineno,name,
440                (cur_scope != NULL ? cur_scope->lid : 0),
441                cls, fctname(fndecl), buf);
442     }
443 }
444
445 /* Output a reference to a call to NAME in FNDECL.  */
446
447 void
448 GNU_xref_call (fndecl, name)
449    tree fndecl;
450    char *name;
451 {
452   XREF_FILE xf;
453   char buf[1024];
454   char *s;
455
456   if (!doing_xref) return;
457   xf = find_file (input_filename);
458   if (xf == NULL) return;
459   name = fixname (name, buf);
460
461   for (s = name; *s != 0; ++s)
462     if (*s == '_' && s[1] == '_') break;
463   if (*s != 0) GNU_xref_ref (fndecl, name);
464
465   fprintf (xref_file, "CAL %s %d %s %s\n",
466            filename (xf), lineno, name, fctname (fndecl));
467 }
468
469 /* Output cross-reference info about FNDECL.  If non-NULL,
470    ARGS are the arguments for the function (i.e., before the FUNCTION_DECL
471    has been fully built).  */
472
473 void
474 GNU_xref_function (fndecl, args)
475    tree fndecl;
476    tree args;
477 {
478   XREF_FILE xf;
479   int ct;
480   char buf[1024];
481
482   if (!doing_xref) return;
483   xf = find_file (input_filename);
484   if (xf == NULL) return;
485
486   ct = 0;
487   buf[0] = 0;
488   if (args == NULL) args = DECL_ARGUMENTS (fndecl);
489
490   GNU_xref_decl (NULL, fndecl);
491
492   for ( ; args != NULL; args = TREE_CHAIN (args))
493     {
494       GNU_xref_decl (fndecl,args);
495       if (ct != 0) strcat (buf,",");
496       strcat (buf, declname (args));
497       ++ct;
498     }
499
500   fprintf (xref_file, "PRC %s %d %s %d %d %s\n",
501            filename(xf), lineno, declname(fndecl),
502            (cur_scope != NULL ? cur_scope->lid : 0),
503            ct, buf);
504 }
505
506 /* Output cross-reference info about an assignment to NAME.  */
507
508 void
509 GNU_xref_assign(name)
510    tree name;
511 {
512   XREF_FILE xf;
513
514   if (!doing_xref) return;
515   xf = find_file(input_filename);
516   if (xf == NULL) return;
517
518   gen_assign(xf, name);
519 }
520
521 static void
522 gen_assign(xf, name)
523    XREF_FILE xf;
524    tree name;
525 {
526   char *s;
527
528   s = NULL;
529
530   switch (TREE_CODE (name))
531     {
532     case IDENTIFIER_NODE :
533       s = IDENTIFIER_POINTER(name);
534       break;
535     case VAR_DECL :
536       s = declname(name);
537       break;
538     case COMPONENT_REF :
539       gen_assign(xf, TREE_OPERAND(name, 0));
540       gen_assign(xf, TREE_OPERAND(name, 1));
541       break;
542     case INDIRECT_REF :
543     case OFFSET_REF :
544     case ARRAY_REF :
545     case BUFFER_REF :
546       gen_assign(xf, TREE_OPERAND(name, 0));
547       break;
548     case COMPOUND_EXPR :
549       gen_assign(xf, TREE_OPERAND(name, 1));
550       break;
551       default :
552       break;
553     }
554
555   if (s != NULL)
556     fprintf(xref_file, "ASG %s %d %s\n", filename(xf), lineno, s);
557 }
558
559 /* Output cross-reference info about a class hierarchy.
560    CLS is the class type of interest.  BASE is a baseclass
561    for CLS.  PUB and VIRT give the access info about
562    the class derivation.  FRND is nonzero iff BASE is a friend
563    of CLS.
564
565    ??? Needs to handle nested classes.  */
566
567 void
568 GNU_xref_hier(cls, base, pub, virt, frnd)
569    char *cls;
570    char *base;
571    int pub;
572    int virt;
573    int frnd;
574 {
575   XREF_FILE xf;
576
577   if (!doing_xref) return;
578   xf = find_file(input_filename);
579   if (xf == NULL) return;
580
581   fprintf(xref_file, "HIE %s %d %s %s %d %d %d\n",
582           filename(xf), lineno, cls, base, pub, virt, frnd);
583 }
584
585 /* Output cross-reference info about class members.  CLS
586    is the containing type; FLD is the class member.  */
587
588 void
589 GNU_xref_member(cls, fld)
590    tree cls;
591    tree fld;
592 {
593   XREF_FILE xf;
594   char *prot;
595   int confg, pure;
596   char *d;
597 #ifdef XREF_SHORT_MEMBER_NAMES
598   int i;
599 #endif
600   char buf[1024], bufa[1024];
601
602   if (!doing_xref) return;
603   xf = find_file(fld->decl.filename);
604   if (xf == NULL) return;
605
606   if (TREE_PRIVATE (fld)) prot = "PRIVATE";
607   else if (TREE_PROTECTED(fld)) prot = "PROTECTED";
608   else prot = "PUBLIC";
609
610   confg = 0;
611   if (TREE_CODE (fld) == FUNCTION_DECL && DECL_CONST_MEMFUNC_P(fld))
612     confg = 1;
613   else if (TREE_CODE (fld) == CONST_DECL)
614     confg = 1;
615
616   pure = 0;
617   if (TREE_CODE (fld) == FUNCTION_DECL && DECL_ABSTRACT_VIRTUAL_P(fld))
618     pure = 1;
619
620   d = IDENTIFIER_POINTER(cls);
621   sprintf(buf, "%d%s", strlen(d), d);
622 #ifdef XREF_SHORT_MEMBER_NAMES
623   i = strlen(buf);
624 #endif
625   strcpy(bufa, declname(fld));
626
627 #ifdef XREF_SHORT_MEMBER_NAMES
628   for (p = &bufa[1]; *p != 0; ++p)
629     {
630       if (p[0] == '_' && p[1] == '_' && p[2] >= '0' && p[2] <= '9') {
631         if (strncmp(&p[2], buf, i) == 0) *p = 0;
632         break;
633       }
634       else if (p[0] == '_' && p[1] == '_' && p[2] == 'C' && p[3] >= '0' && p[3] <= '9') {
635         if (strncmp(&p[3], buf, i) == 0) *p = 0;
636         break;
637       }
638     }
639 #endif
640
641   fprintf(xref_file, "MEM %s %d %s %s %s %d %d %d %d %d %d %d\n",
642           filename(xf), fld->decl.linenum, d,  bufa,  prot,
643           (TREE_CODE (fld) == FUNCTION_DECL ? 0 : 1),
644           (DECL_INLINE (fld) ? 1 : 0),
645           (DECL_LANG_SPECIFIC(fld) && DECL_FRIEND_P(fld) ? 1 : 0),
646           (DECL_VINDEX(fld) ? 1 : 0),
647           (TREE_STATIC(fld) ? 1 : 0),
648           pure, confg);
649 }
650
651 /* Find file entry given name.  */
652
653 static XREF_FILE
654 find_file(name)
655    char *name;
656 {
657   XREF_FILE xf;
658
659   for (xf = all_files; xf != NULL; xf = xf->next) {
660     if (STREQL(name, xf->name)) break;
661   }
662
663   return xf;
664 }
665
666 /* Return filename for output purposes.  */
667
668 static char *
669 filename(xf)
670    XREF_FILE xf;
671 {
672   if (xf == NULL) {
673     last_file = NULL;
674     return "*";
675   }
676
677   if (last_file == xf) return "*";
678
679   last_file = xf;
680
681   return xf->outname;
682 }
683
684 /* Return function name for output purposes.  */
685
686 static char *
687 fctname(fndecl)
688    tree fndecl;
689 {
690   static char fctbuf[1024];
691   char *s;
692
693   if (fndecl == NULL && last_fndecl == NULL) return "*";
694
695   if (fndecl == NULL)
696     {
697       last_fndecl = NULL;
698       return "*TOP*";
699     }
700
701   if (fndecl == last_fndecl) return "*";
702
703   last_fndecl = fndecl;
704
705   s = declname(fndecl);
706   s = fixname(s, fctbuf);
707
708   return s;
709 }
710
711 /* Return decl name for output purposes.  */
712
713 static char *
714 declname(dcl)
715    tree dcl;
716 {
717   if (DECL_NAME (dcl) == NULL) return "?";
718
719   if (DECL_ASSEMBLER_NAME (dcl))
720     return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (dcl));
721   else
722     return IDENTIFIER_POINTER (DECL_NAME (dcl));
723 }
724
725 /* Simplify a type string by removing unneeded parenthesis.  */
726
727 static void
728 simplify_type(typ)
729    char *typ;
730 {
731   char *s;
732   int lvl, i;
733
734   i = strlen(typ);
735   while (i > 0 && isspace(typ[i-1])) typ[--i] = 0;
736
737   if (i > 7 && STREQL(&typ[i-5], "const"))
738     {
739       typ[i-5] = 0;
740       i -= 5;
741     }
742
743   if (typ[i-1] != ')') return;
744
745   s = &typ[i-2];
746   lvl = 1;
747   while (*s != 0) {
748     if (*s == ')') ++lvl;
749     else if (*s == '(')
750       {
751         --lvl;
752         if (lvl == 0)
753           {
754             s[1] = ')';
755             s[2] = 0;
756             break;
757           }
758       }
759     --s;
760   }
761
762   if (*s != 0 && s[-1] == ')')
763     {
764       --s;
765       --s;
766       if (*s == '(') s[2] = 0;
767       else if (*s == ':') {
768         while (*s != '(') --s;
769         s[1] = ')';
770         s[2] = 0;
771       }
772     }
773 }
774
775 /* Fixup a function name (take care of embedded spaces).  */
776
777 static char *
778 fixname(nam, buf)
779    char *nam;
780    char *buf;
781 {
782   char *s, *t;
783   int fg;
784
785   s = nam;
786   t = buf;
787   fg = 0;
788
789   while (*s != 0)
790     {
791       if (*s == ' ')
792         {
793           *t++ = '\36';
794           ++fg;
795         }
796       else *t++ = *s;
797       ++s;
798     }
799   *t = 0;
800
801   if (fg == 0) return nam;
802
803   return buf;
804 }
805
806 /* Open file for xreffing.  */
807
808 static void
809 open_xref_file(file)
810    char *file;
811 {
812   char *s, *t;
813
814 #ifdef XREF_FILE_NAME
815   XREF_FILE_NAME (xref_name, file);
816 #else
817   s = rindex (file, '/');
818   if (s == NULL)
819     sprintf (xref_name, ".%s.gxref", file);
820   else
821     {
822       ++s;
823       strcpy (xref_name, file);
824       t = rindex (xref_name, '/');
825       ++t;
826       *t++ = '.';
827       strcpy (t, s);
828       strcat (t, ".gxref");
829     }
830 #endif /* no XREF_FILE_NAME */
831
832   xref_file = fopen(xref_name, "w");
833
834   if (xref_file == NULL)
835     {
836       error("Can't create cross-reference file `%s'", xref_name);
837       doing_xref = 0;
838     }
839 }