Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / gcc / cp / xref.c
1 /* Code for handling XREF output from GNU C++.
2    Copyright (C) 1992, 93-97, 1998 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 "system.h"
25 #include "tree.h"
26 #include "cp-tree.h"
27 #include "input.h"
28 #include "toplev.h"
29
30 extern char *getpwd PROTO((void));
31
32 /* The character(s) used to join a directory specification (obtained with
33    getwd or equivalent) with a non-absolute file name.  */
34
35 #ifndef FILE_NAME_JOINER
36 #define FILE_NAME_JOINER "/"
37 #endif
38
39 /* Nonzero if NAME as a file name is absolute.  */
40 #ifndef FILE_NAME_ABSOLUTE_P
41 #define FILE_NAME_ABSOLUTE_P(NAME) (NAME[0] == '/')
42 #endif
43
44 /* For cross referencing.  */
45
46 int flag_gnu_xref;
47
48 /************************************************************************/
49 /*                                                                      */
50 /*      Common definitions                                              */
51 /*                                                                      */
52 /************************************************************************/
53
54 #ifndef TRUE
55 #define TRUE 1
56 #endif
57 #ifndef FALSE
58 #define FALSE 0
59 #endif
60
61 #define PALLOC(typ) ((typ *) calloc(1,sizeof(typ)))
62
63
64 /* Return a malloc'd copy of STR.  */
65 #define SALLOC(str) \
66  ((char *) ((str) == NULL ? NULL        \
67             : (char *) strcpy ((char *) malloc (strlen ((str)) + 1), (str))))
68 #define SFREE(str) (str != NULL && (free(str),0))
69
70 #define STREQL(s1,s2) (strcmp((s1),(s2)) == 0)
71 #define STRNEQ(s1,s2) (strcmp((s1),(s2)) != 0)
72 #define STRLSS(s1,s2) (strcmp((s1),(s2)) < 0)
73 #define STRLEQ(s1,s2) (strcmp((s1),(s2)) <= 0)
74 #define STRGTR(s1,s2) (strcmp((s1),(s2)) > 0)
75 #define STRGEQ(s1,s2) (strcmp((s1),(s2)) >= 0)
76
77 /************************************************************************/
78 /*                                                                      */
79 /*      Type definitions                                                */
80 /*                                                                      */
81 /************************************************************************/
82
83
84 typedef struct _XREF_FILE *     XREF_FILE;
85 typedef struct _XREF_SCOPE *    XREF_SCOPE;
86
87 typedef struct _XREF_FILE
88 {
89   const char *name;
90   const char *outname;
91   XREF_FILE next;
92 } XREF_FILE_INFO;
93
94 typedef struct _XREF_SCOPE
95 {
96   int gid;
97   int lid;
98   XREF_FILE file;
99   int start;
100   XREF_SCOPE outer;
101 } XREF_SCOPE_INFO;
102
103 /************************************************************************/
104 /*                                                                      */
105 /*      Local storage                                                   */
106 /*                                                                      */
107 /************************************************************************/
108
109 static  char            doing_xref = 0;
110 static  FILE *          xref_file = NULL;
111 static  char            xref_name[1024];
112 static  XREF_FILE       all_files = NULL;
113 static  char *          wd_name = NULL;
114 static  XREF_SCOPE      cur_scope = NULL;
115 static  int     scope_ctr = 0;
116 static  XREF_FILE       last_file = NULL;
117 static  tree            last_fndecl = NULL;
118
119 /************************************************************************/
120 /*                                                                      */
121 /*      Forward definitions                                             */
122 /*                                                                      */
123 /************************************************************************/
124 static  void            gen_assign PROTO((XREF_FILE, tree));
125 static  XREF_FILE       find_file PROTO((const char *));
126 static  const char *    filename PROTO((XREF_FILE));
127 static  const char *    fctname PROTO((tree));
128 static  const char *    declname PROTO((tree));
129 static  void            simplify_type PROTO((char *));
130 static  const char *    fixname PROTO((const char *, char *));
131 static  void            open_xref_file PROTO((const char *));
132 static  const char *    classname PROTO((tree));
133
134 /* Start cross referencing.  FILE is the name of the file we xref.  */
135
136 void
137 GNU_xref_begin (file)
138    const char *file;
139 {
140   doing_xref = 1;
141
142   if (file != NULL && STRNEQ (file,"-"))
143     {
144       open_xref_file(file);
145       GNU_xref_file(file);
146     }
147 }
148
149 /* Finish cross-referencing.  ERRCNT is the number of errors
150    we encountered.  */
151
152 void
153 GNU_xref_end (ect)
154    int ect;
155 {
156   XREF_FILE xf;
157
158   if (!doing_xref) return;
159
160   xf = find_file (input_filename);
161   if (xf == NULL) return;
162
163   while (cur_scope != NULL)
164     GNU_xref_end_scope(cur_scope->gid,0,0,0);
165
166   doing_xref = 0;
167
168   if (xref_file == NULL) return;
169
170   fclose (xref_file);
171
172   xref_file = NULL;
173   all_files = NULL;
174
175   if (ect > 0) unlink (xref_name);
176 }
177
178 /* Write out xref for file named NAME.  */
179
180 void
181 GNU_xref_file (name)
182    const char *name;
183 {
184   XREF_FILE xf;
185
186   if (!doing_xref || name == NULL) return;
187
188   if (xref_file == NULL)
189     {
190       open_xref_file (name);
191       if (!doing_xref) return;
192     }
193
194   if (all_files == NULL)
195     fprintf(xref_file,"SCP * 0 0 0 0 RESET\n");
196
197   xf = find_file (name);
198   if (xf != NULL) return;
199
200   xf = PALLOC (XREF_FILE_INFO);
201   xf->name = SALLOC (name);
202   xf->next = all_files;
203   all_files = xf;
204
205   if (wd_name == NULL)
206     wd_name = getpwd ();
207
208   if (FILE_NAME_ABSOLUTE_P (name) || ! wd_name)
209     xf->outname = xf->name;
210   else
211     {
212       char *nmbuf
213         = (char *) xmalloc (strlen (wd_name) + strlen (FILE_NAME_JOINER)
214                             + strlen (name) + 1);
215       sprintf (nmbuf, "%s%s%s", wd_name, FILE_NAME_JOINER, name);
216       name = nmbuf;
217       xf->outname = nmbuf;
218     }
219
220   fprintf (xref_file, "FIL %s %s 0\n", name, wd_name);
221
222   filename (xf);
223   fctname (NULL);
224 }
225
226 /* Start a scope identified at level ID.  */
227
228 void
229 GNU_xref_start_scope (id)
230    HOST_WIDE_INT id;
231 {
232   XREF_SCOPE xs;
233   XREF_FILE xf;
234
235   if (!doing_xref) return;
236   xf = find_file (input_filename);
237
238   xs = PALLOC (XREF_SCOPE_INFO);
239   xs->file = xf;
240   xs->start = lineno;
241   if (xs->start <= 0) xs->start = 1;
242   xs->gid = id;
243   xs->lid = ++scope_ctr;
244   xs->outer = cur_scope;
245   cur_scope = xs;
246 }
247
248 /* Finish a scope at level ID.
249    INID is ???
250    PRM is ???
251    KEEP is nonzero iff this scope is retained (nonzero if it's
252    a compiler-generated invisible scope).
253    TRNS is ???  */
254
255 void
256 GNU_xref_end_scope (id,inid,prm,keep)
257    HOST_WIDE_INT id;
258    HOST_WIDE_INT inid;
259    int prm,keep;
260 {
261   XREF_FILE xf;
262   XREF_SCOPE xs,lxs,oxs;
263   const char *stype;
264
265   if (!doing_xref) return;
266   xf = find_file (input_filename);
267   if (xf == NULL) return;
268
269   lxs = NULL;
270   for (xs = cur_scope; xs != NULL; xs = xs->outer)
271     {
272       if (xs->gid == id) break;
273       lxs = xs;
274     }
275   if (xs == NULL) return;
276
277   if (inid != 0) {
278     for (oxs = cur_scope; oxs != NULL; oxs = oxs->outer) {
279       if (oxs->gid == inid) break;
280     }
281     if (oxs == NULL) return;
282     inid = oxs->lid;
283   }
284
285   if (prm == 2) stype = "SUE";
286   else if (prm != 0) stype = "ARGS";
287   else if (keep == 2 || inid != 0) stype = "INTERN";
288   else stype = "EXTERN";
289
290   fprintf (xref_file, "SCP %s %d %d %d ",
291            filename (xf), xs->start, lineno,xs->lid);
292   fprintf (xref_file, HOST_WIDE_INT_PRINT_DEC, inid);
293   fprintf (xref_file, " %s\n", stype);
294
295   if (lxs == NULL) cur_scope = xs->outer;
296   else lxs->outer = xs->outer;
297
298   free (xs);
299 }
300
301 /* Output a reference to NAME in FNDECL.  */
302
303 void
304 GNU_xref_ref (fndecl,name)
305    tree fndecl;
306    const char *name;
307 {
308   XREF_FILE xf;
309
310   if (!doing_xref) return;
311   xf = find_file (input_filename);
312   if (xf == NULL) return;
313
314   fprintf (xref_file, "REF %s %d %s %s\n",
315            filename (xf), lineno, fctname (fndecl), name);
316 }
317
318 /* Output a reference to DECL in FNDECL.  */
319
320 void
321 GNU_xref_decl (fndecl,decl)
322    tree fndecl;
323    tree decl;
324 {
325   XREF_FILE xf,xf1;
326   const char *cls = 0;
327   const char *name;
328   char buf[10240];
329   int uselin;
330
331   if (!doing_xref) return;
332   xf = find_file (input_filename);
333   if (xf == NULL) return;
334
335   uselin = FALSE;
336
337   if (TREE_CODE (decl) == TYPE_DECL) cls = "TYPEDEF";
338   else if (TREE_CODE (decl) == FIELD_DECL) cls = "FIELD";
339   else if (TREE_CODE (decl) == VAR_DECL)
340     {
341       if (fndecl == NULL && TREE_STATIC(decl)
342           && TREE_READONLY(decl) && DECL_INITIAL(decl) != 0
343           && !TREE_PUBLIC(decl) && !DECL_EXTERNAL(decl)
344           && DECL_MODE(decl) != BLKmode) cls = "CONST";
345       else if (DECL_EXTERNAL(decl)) cls = "EXTERN";
346       else if (TREE_PUBLIC(decl)) cls = "EXTDEF";
347       else if (TREE_STATIC(decl)) cls = "STATIC";
348       else if (DECL_REGISTER(decl)) cls = "REGISTER";
349       else cls = "AUTO";
350     }
351   else if (TREE_CODE (decl) == PARM_DECL) cls = "PARAM";
352   else if (TREE_CODE (decl) == FIELD_DECL) cls = "FIELD";
353   else if (TREE_CODE (decl) == CONST_DECL) cls = "CONST";
354   else if (TREE_CODE (decl) == FUNCTION_DECL)
355     {
356       if (DECL_EXTERNAL (decl)) cls = "EXTERN";
357       else if (TREE_PUBLIC (decl)) cls = "EFUNCTION";
358       else cls = "SFUNCTION";
359     }
360   else if (TREE_CODE (decl) == LABEL_DECL) cls = "LABEL";
361   else if (TREE_CODE (decl) == UNION_TYPE)
362     {
363       cls = "UNIONID";
364       decl = TYPE_NAME (decl);
365       uselin = TRUE;
366     }
367   else if (TREE_CODE (decl) == RECORD_TYPE)
368     {
369       if (CLASSTYPE_DECLARED_CLASS (decl)) cls = "CLASSID";
370       else if (IS_SIGNATURE (decl)) cls = "SIGNATUREID";
371       else cls = "STRUCTID";
372       decl = TYPE_NAME (decl);
373       uselin = TRUE;
374     }
375   else if (TREE_CODE (decl) == ENUMERAL_TYPE)
376     {
377       cls = "ENUMID";
378       decl = TYPE_NAME (decl);
379       uselin = TRUE;
380     }
381   else if (TREE_CODE (decl) == TEMPLATE_DECL)
382     {
383       if (TREE_CODE (DECL_RESULT (decl)) == TYPE_DECL)
384         cls = "CLASSTEMP";
385       else if (TREE_CODE (DECL_RESULT (decl)) == FUNCTION_DECL)
386         cls = "FUNCTEMP";
387       else if (TREE_CODE (DECL_RESULT (decl)) == VAR_DECL)
388         cls = "VARTEMP";
389       else
390         my_friendly_abort (358);
391       uselin = TRUE;
392     }
393   else cls = "UNKNOWN";
394
395   if (decl == NULL || DECL_NAME (decl) == NULL) return;
396
397   if (uselin && decl->decl.linenum > 0 && decl->decl.filename != NULL)
398     {
399       xf1 = find_file (decl->decl.filename);
400       if (xf1 != NULL)
401         {
402           lineno = decl->decl.linenum;
403           xf = xf1;
404         }
405     }
406
407   if (DECL_ASSEMBLER_NAME (decl))
408     name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
409   else
410     name = IDENTIFIER_POINTER (DECL_NAME (decl));
411
412   strcpy (buf, type_as_string (TREE_TYPE (decl), 0));
413   simplify_type (buf);
414
415   fprintf (xref_file, "DCL %s %d %s %d %s %s %s\n",
416            filename(xf), lineno, name,
417            (cur_scope != NULL ? cur_scope->lid : 0),
418            cls, fctname(fndecl), buf);
419
420   if (STREQL (cls, "STRUCTID") || STREQL (cls, "UNIONID")
421       || STREQL (cls, "SIGNATUREID"))
422     {
423       cls = "CLASSID";
424       fprintf (xref_file, "DCL %s %d %s %d %s %s %s\n",
425                filename(xf), lineno,name,
426                (cur_scope != NULL ? cur_scope->lid : 0),
427                cls, fctname(fndecl), buf);
428     }
429 }
430
431 /* Output a reference to a call to NAME in FNDECL.  */
432
433 void
434 GNU_xref_call (fndecl, name)
435    tree fndecl;
436    const char *name;
437 {
438   XREF_FILE xf;
439   char buf[1024];
440   const char *s;
441
442   if (!doing_xref) return;
443   xf = find_file (input_filename);
444   if (xf == NULL) return;
445   name = fixname (name, buf);
446
447   for (s = name; *s != 0; ++s)
448     if (*s == '_' && s[1] == '_') break;
449   if (*s != 0) GNU_xref_ref (fndecl, name);
450
451   fprintf (xref_file, "CAL %s %d %s %s\n",
452            filename (xf), lineno, name, fctname (fndecl));
453 }
454
455 /* Output cross-reference info about FNDECL.  If non-NULL,
456    ARGS are the arguments for the function (i.e., before the FUNCTION_DECL
457    has been fully built).  */
458
459 void
460 GNU_xref_function (fndecl, args)
461    tree fndecl;
462    tree args;
463 {
464   XREF_FILE xf;
465   int ct;
466   char buf[1024];
467
468   if (!doing_xref) return;
469   xf = find_file (input_filename);
470   if (xf == NULL) return;
471
472   ct = 0;
473   buf[0] = 0;
474   if (args == NULL) args = DECL_ARGUMENTS (fndecl);
475
476   GNU_xref_decl (NULL, fndecl);
477
478   for ( ; args != NULL; args = TREE_CHAIN (args))
479     {
480       GNU_xref_decl (fndecl,args);
481       if (ct != 0) strcat (buf,",");
482       strcat (buf, declname (args));
483       ++ct;
484     }
485
486   fprintf (xref_file, "PRC %s %d %s %d %d %s\n",
487            filename(xf), lineno, declname(fndecl),
488            (cur_scope != NULL ? cur_scope->lid : 0),
489            ct, buf);
490 }
491
492 /* Output cross-reference info about an assignment to NAME.  */
493
494 void
495 GNU_xref_assign(name)
496    tree name;
497 {
498   XREF_FILE xf;
499
500   if (!doing_xref) return;
501   xf = find_file(input_filename);
502   if (xf == NULL) return;
503
504   gen_assign(xf, name);
505 }
506
507 static void
508 gen_assign(xf, name)
509    XREF_FILE xf;
510    tree name;
511 {
512   const char *s;
513
514   s = NULL;
515
516   switch (TREE_CODE (name))
517     {
518     case IDENTIFIER_NODE :
519       s = IDENTIFIER_POINTER(name);
520       break;
521     case VAR_DECL :
522       s = declname(name);
523       break;
524     case COMPONENT_REF :
525       gen_assign(xf, TREE_OPERAND(name, 0));
526       gen_assign(xf, TREE_OPERAND(name, 1));
527       break;
528     case INDIRECT_REF :
529     case OFFSET_REF :
530     case ARRAY_REF :
531     case BUFFER_REF :
532       gen_assign(xf, TREE_OPERAND(name, 0));
533       break;
534     case COMPOUND_EXPR :
535       gen_assign(xf, TREE_OPERAND(name, 1));
536       break;
537       default :
538       break;
539     }
540
541   if (s != NULL)
542     fprintf(xref_file, "ASG %s %d %s\n", filename(xf), lineno, s);
543 }
544
545 static const char *
546 classname (cls)
547      tree cls;
548 {
549   if (cls && TREE_CODE_CLASS (TREE_CODE (cls)) == 't')
550     cls = TYPE_NAME (cls);
551   if (cls && TREE_CODE_CLASS (TREE_CODE (cls)) == 'd')
552     cls = DECL_NAME (cls);
553   if (cls && TREE_CODE (cls) == IDENTIFIER_NODE)
554     return IDENTIFIER_POINTER (cls);
555   return "?";
556 }
557
558 /* Output cross-reference info about a class hierarchy.
559    CLS is the class type of interest.  BASE is a baseclass
560    for CLS.  PUB and VIRT give the access info about
561    the class derivation.  FRND is nonzero iff BASE is a friend
562    of CLS.
563
564    ??? Needs to handle nested classes.  */
565
566 void
567 GNU_xref_hier(cls, base, pub, virt, frnd)
568    tree cls;
569    tree base;
570    int pub;
571    int virt;
572    int frnd;
573 {
574   XREF_FILE xf;
575
576   if (!doing_xref) return;
577   xf = find_file(input_filename);
578   if (xf == NULL) return;
579
580   fprintf(xref_file, "HIE %s %d %s %s %d %d %d\n",
581           filename(xf), lineno, classname (cls), classname (base), 
582           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   const char *prot;
595   int confg, pure;
596   const 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", (int) 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    const 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 const 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 const char *
687 fctname(fndecl)
688    tree fndecl;
689 {
690   static char fctbuf[1024];
691   const 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 const 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((unsigned char) 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 const char *
778 fixname(nam, buf)
779    const char *nam;
780    char *buf;
781 {
782   const char *s;
783   char *t;
784   int fg;
785
786   s = nam;
787   t = buf;
788   fg = 0;
789
790   while (*s != 0)
791     {
792       if (*s == ' ')
793         {
794           *t++ = '\36';
795           ++fg;
796         }
797       else *t++ = *s;
798       ++s;
799     }
800   *t = 0;
801
802   if (fg == 0) return nam;
803
804   return buf;
805 }
806
807 /* Open file for xreffing.  */
808
809 static void
810 open_xref_file(file)
811    const char *file;
812 {
813   const char *s;
814   char *t;
815
816 #ifdef XREF_FILE_NAME
817   XREF_FILE_NAME (xref_name, file);
818 #else
819   s = rindex (file, '/');
820   if (s == NULL)
821     sprintf (xref_name, ".%s.gxref", file);
822   else
823     {
824       ++s;
825       strcpy (xref_name, file);
826       t = rindex (xref_name, '/');
827       ++t;
828       *t++ = '.';
829       strcpy (t, s);
830       strcat (t, ".gxref");
831     }
832 #endif /* no XREF_FILE_NAME */
833
834   xref_file = fopen(xref_name, "w");
835
836   if (xref_file == NULL)
837     {
838       error("Can't create cross-reference file `%s'", xref_name);
839       doing_xref = 0;
840     }
841 }