1 /* Code for handling XREF output from GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
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)
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.
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. */
31 extern char *getpwd ();
33 extern char *index ();
34 extern char *rindex ();
36 /* The character(s) used to join a directory specification (obtained with
37 getwd or equivalent) with a non-absolute file name. */
39 #ifndef FILE_NAME_JOINER
40 #define FILE_NAME_JOINER "/"
43 /* Nonzero if NAME as a file name is absolute. */
44 #ifndef FILE_NAME_ABSOLUTE_P
45 #define FILE_NAME_ABSOLUTE_P(NAME) (NAME[0] == '/')
48 /* For cross referencing. */
52 /************************************************************************/
54 /* Common definitions */
56 /************************************************************************/
68 #define PALLOC(typ) ((typ *) calloc(1,sizeof(typ)))
71 /* Return a malloc'd copy of STR. */
73 ((char *) ((str) == NULL ? NULL \
74 : (char *) strcpy ((char *) malloc (strlen ((str)) + 1), (str))))
75 #define SFREE(str) (str != NULL && (free(str),0))
77 #define STREQL(s1,s2) (strcmp((s1),(s2)) == 0)
78 #define STRNEQ(s1,s2) (strcmp((s1),(s2)) != 0)
79 #define STRLSS(s1,s2) (strcmp((s1),(s2)) < 0)
80 #define STRLEQ(s1,s2) (strcmp((s1),(s2)) <= 0)
81 #define STRGTR(s1,s2) (strcmp((s1),(s2)) > 0)
82 #define STRGEQ(s1,s2) (strcmp((s1),(s2)) >= 0)
84 /************************************************************************/
86 /* Type definitions */
88 /************************************************************************/
91 typedef struct _XREF_FILE
* XREF_FILE
;
92 typedef struct _XREF_SCOPE
* XREF_SCOPE
;
94 typedef struct _XREF_FILE
101 typedef struct _XREF_SCOPE
110 /************************************************************************/
114 /************************************************************************/
116 static char doing_xref
= 0;
117 static FILE * xref_file
= NULL
;
118 static char xref_name
[1024];
119 static XREF_FILE all_files
= NULL
;
120 static char * wd_name
= NULL
;
121 static XREF_SCOPE cur_scope
= NULL
;
122 static int scope_ctr
= 0;
123 static XREF_FILE last_file
= NULL
;
124 static tree last_fndecl
= NULL
;
126 /************************************************************************/
128 /* Forward definitions */
130 /************************************************************************/
132 extern void GNU_xref_begin();
133 extern void GNU_xref_end();
134 extern void GNU_xref_file();
135 extern void GNU_xref_start_scope();
136 extern void GNU_xref_end_scope();
137 extern void GNU_xref_ref();
138 extern void GNU_xref_decl();
139 extern void GNU_xref_call();
140 extern void GNU_xref_function();
141 extern void GNU_xref_assign();
142 extern void GNU_xref_hier();
143 extern void GNU_xref_member();
145 static void gen_assign();
146 static XREF_FILE
find_file();
147 static char * filename();
148 static char * fctname();
149 static char * declname();
150 static void simplify_type();
151 static char * fixname();
152 static void open_xref_file();
154 extern char * type_as_string();
156 /* Start cross referencing. FILE is the name of the file we xref. */
159 GNU_xref_begin (file
)
164 if (file
!= NULL
&& STRNEQ (file
,"-"))
166 open_xref_file(file
);
171 /* Finish cross-referencing. ERRCNT is the number of errors
180 if (!doing_xref
) return;
182 xf
= find_file (input_filename
);
183 if (xf
== NULL
) return;
185 while (cur_scope
!= NULL
)
186 GNU_xref_end_scope(cur_scope
->gid
,0,0,0,0);
190 if (xref_file
== NULL
) return;
197 if (ect
> 0) unlink (xref_name
);
200 /* Write out xref for file named NAME. */
208 if (!doing_xref
|| name
== NULL
) return;
210 if (xref_file
== NULL
)
212 open_xref_file (name
);
213 if (!doing_xref
) return;
216 if (all_files
== NULL
)
217 fprintf(xref_file
,"SCP * 0 0 0 0 RESET\n");
219 xf
= find_file (name
);
220 if (xf
!= NULL
) return;
222 xf
= PALLOC (XREF_FILE_INFO
);
223 xf
->name
= SALLOC (name
);
224 xf
->next
= all_files
;
230 if (FILE_NAME_ABSOLUTE_P (name
) || ! wd_name
)
231 xf
->outname
= xf
->name
;
235 = (char *) malloc (strlen (wd_name
) + strlen (FILE_NAME_JOINER
)
236 + strlen (name
) + 1);
237 sprintf (nmbuf
, "%s%s%s", wd_name
, FILE_NAME_JOINER
, name
);
242 fprintf (xref_file
, "FIL %s %s 0\n", name
, wd_name
);
248 /* Start a scope identified at level ID. */
251 GNU_xref_start_scope (id
)
257 if (!doing_xref
) return;
258 xf
= find_file (input_filename
);
260 xs
= PALLOC (XREF_SCOPE_INFO
);
263 if (xs
->start
<= 0) xs
->start
= 1;
265 xs
->lid
= ++scope_ctr
;
266 xs
->outer
= cur_scope
;
270 /* Finish a scope at level ID.
273 KEEP is nonzero iff this scope is retained (nonzero if it's
274 a compiler-generated invisible scope).
278 GNU_xref_end_scope (id
,inid
,prm
,keep
,trns
)
284 XREF_SCOPE xs
,lxs
,oxs
;
287 if (!doing_xref
) return;
288 xf
= find_file (input_filename
);
289 if (xf
== NULL
) return;
292 for (xs
= cur_scope
; xs
!= NULL
; xs
= xs
->outer
)
294 if (xs
->gid
== id
) break;
297 if (xs
== NULL
) return;
300 for (oxs
= cur_scope
; oxs
!= NULL
; oxs
= oxs
->outer
) {
301 if (oxs
->gid
== inid
) break;
303 if (oxs
== NULL
) return;
307 if (prm
== 2) stype
= "SUE";
308 else if (prm
!= 0) stype
= "ARGS";
309 else if (keep
== 2 || inid
!= 0) stype
= "INTERN";
310 else stype
= "EXTERN";
312 fprintf (xref_file
,"SCP %s %d %d %d %d %s\n",
313 filename (xf
), xs
->start
, lineno
,xs
->lid
, inid
, stype
);
315 if (lxs
== NULL
) cur_scope
= xs
->outer
;
316 else lxs
->outer
= xs
->outer
;
321 /* Output a reference to NAME in FNDECL. */
324 GNU_xref_ref (fndecl
,name
)
330 if (!doing_xref
) return;
331 xf
= find_file (input_filename
);
332 if (xf
== NULL
) return;
334 fprintf (xref_file
, "REF %s %d %s %s\n",
335 filename (xf
), lineno
, fctname (fndecl
), name
);
338 /* Output a reference to DECL in FNDECL. */
341 GNU_xref_decl (fndecl
,decl
)
351 if (!doing_xref
) return;
352 xf
= find_file (input_filename
);
353 if (xf
== NULL
) return;
357 if (TREE_CODE (decl
) == TYPE_DECL
) cls
= "TYPEDEF";
358 else if (TREE_CODE (decl
) == FIELD_DECL
) cls
= "FIELD";
359 else if (TREE_CODE (decl
) == VAR_DECL
)
361 if (fndecl
== NULL
&& TREE_STATIC(decl
)
362 && TREE_READONLY(decl
) && DECL_INITIAL(decl
) != 0
363 && !TREE_PUBLIC(decl
) && !DECL_EXTERNAL(decl
)
364 && DECL_MODE(decl
) != BLKmode
) cls
= "CONST";
365 else if (DECL_EXTERNAL(decl
)) cls
= "EXTERN";
366 else if (TREE_PUBLIC(decl
)) cls
= "EXTDEF";
367 else if (TREE_STATIC(decl
)) cls
= "STATIC";
368 else if (DECL_REGISTER(decl
)) cls
= "REGISTER";
371 else if (TREE_CODE (decl
) == PARM_DECL
) cls
= "PARAM";
372 else if (TREE_CODE (decl
) == FIELD_DECL
) cls
= "FIELD";
373 else if (TREE_CODE (decl
) == CONST_DECL
) cls
= "CONST";
374 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
376 if (DECL_EXTERNAL (decl
)) cls
= "EXTERN";
377 else if (TREE_PUBLIC (decl
)) cls
= "EFUNCTION";
378 else cls
= "SFUNCTION";
380 else if (TREE_CODE (decl
) == LABEL_DECL
) cls
= "LABEL";
381 else if (TREE_CODE (decl
) == UNION_TYPE
)
384 decl
= TYPE_NAME (decl
);
387 else if (TREE_CODE (decl
) == RECORD_TYPE
)
389 if (CLASSTYPE_DECLARED_CLASS (decl
)) cls
= "CLASSID";
390 else if (IS_SIGNATURE (decl
)) cls
= "SIGNATUREID";
391 else cls
= "STRUCTID";
392 decl
= TYPE_NAME (decl
);
395 else if (TREE_CODE (decl
) == ENUMERAL_TYPE
)
398 decl
= TYPE_NAME (decl
);
401 else if (TREE_CODE (decl
) == TEMPLATE_DECL
)
403 if (DECL_TEMPLATE_IS_CLASS (decl
))
405 else if (TREE_CODE (DECL_RESULT (decl
)) == FUNCTION_DECL
)
407 else if (TREE_CODE (DECL_RESULT (decl
)) == VAR_DECL
)
410 my_friendly_abort (358);
413 else cls
= "UNKNOWN";
415 if (decl
== NULL
|| DECL_NAME (decl
) == NULL
) return;
417 if (uselin
&& decl
->decl
.linenum
> 0 && decl
->decl
.filename
!= NULL
)
419 xf1
= find_file (decl
->decl
.filename
);
422 lineno
= decl
->decl
.linenum
;
427 if (DECL_ASSEMBLER_NAME (decl
))
428 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
430 name
= IDENTIFIER_POINTER (DECL_NAME (decl
));
432 strcpy (buf
, type_as_string (TREE_TYPE (decl
), 0));
435 fprintf (xref_file
, "DCL %s %d %s %d %s %s %s\n",
436 filename(xf
), lineno
, name
,
437 (cur_scope
!= NULL
? cur_scope
->lid
: 0),
438 cls
, fctname(fndecl
), buf
);
440 if (STREQL (cls
, "STRUCTID") || STREQL (cls
, "UNIONID")
441 || STREQL (cls
, "SIGNATUREID"))
444 fprintf (xref_file
, "DCL %s %d %s %d %s %s %s\n",
445 filename(xf
), lineno
,name
,
446 (cur_scope
!= NULL
? cur_scope
->lid
: 0),
447 cls
, fctname(fndecl
), buf
);
451 /* Output a reference to a call to NAME in FNDECL. */
454 GNU_xref_call (fndecl
, name
)
462 if (!doing_xref
) return;
463 xf
= find_file (input_filename
);
464 if (xf
== NULL
) return;
465 name
= fixname (name
, buf
);
467 for (s
= name
; *s
!= 0; ++s
)
468 if (*s
== '_' && s
[1] == '_') break;
469 if (*s
!= 0) GNU_xref_ref (fndecl
, name
);
471 fprintf (xref_file
, "CAL %s %d %s %s\n",
472 filename (xf
), lineno
, name
, fctname (fndecl
));
475 /* Output cross-reference info about FNDECL. If non-NULL,
476 ARGS are the arguments for the function (i.e., before the FUNCTION_DECL
477 has been fully built). */
480 GNU_xref_function (fndecl
, args
)
488 if (!doing_xref
) return;
489 xf
= find_file (input_filename
);
490 if (xf
== NULL
) return;
494 if (args
== NULL
) args
= DECL_ARGUMENTS (fndecl
);
496 GNU_xref_decl (NULL
, fndecl
);
498 for ( ; args
!= NULL
; args
= TREE_CHAIN (args
))
500 GNU_xref_decl (fndecl
,args
);
501 if (ct
!= 0) strcat (buf
,",");
502 strcat (buf
, declname (args
));
506 fprintf (xref_file
, "PRC %s %d %s %d %d %s\n",
507 filename(xf
), lineno
, declname(fndecl
),
508 (cur_scope
!= NULL
? cur_scope
->lid
: 0),
512 /* Output cross-reference info about an assignment to NAME. */
515 GNU_xref_assign(name
)
520 if (!doing_xref
) return;
521 xf
= find_file(input_filename
);
522 if (xf
== NULL
) return;
524 gen_assign(xf
, name
);
536 switch (TREE_CODE (name
))
538 case IDENTIFIER_NODE
:
539 s
= IDENTIFIER_POINTER(name
);
545 gen_assign(xf
, TREE_OPERAND(name
, 0));
546 gen_assign(xf
, TREE_OPERAND(name
, 1));
552 gen_assign(xf
, TREE_OPERAND(name
, 0));
555 gen_assign(xf
, TREE_OPERAND(name
, 1));
562 fprintf(xref_file
, "ASG %s %d %s\n", filename(xf
), lineno
, s
);
565 /* Output cross-reference info about a class hierarchy.
566 CLS is the class type of interest. BASE is a baseclass
567 for CLS. PUB and VIRT give the access info about
568 the class derivation. FRND is nonzero iff BASE is a friend
571 ??? Needs to handle nested classes. */
573 GNU_xref_hier(cls
, base
, pub
, virt
, frnd
)
582 if (!doing_xref
) return;
583 xf
= find_file(input_filename
);
584 if (xf
== NULL
) return;
586 fprintf(xref_file
, "HIE %s %d %s %s %d %d %d\n",
587 filename(xf
), lineno
, cls
, base
, pub
, virt
, frnd
);
590 /* Output cross-reference info about class members. CLS
591 is the containing type; FLD is the class member. */
594 GNU_xref_member(cls
, fld
)
603 char buf
[1024], bufa
[1024];
605 if (!doing_xref
) return;
606 xf
= find_file(fld
->decl
.filename
);
607 if (xf
== NULL
) return;
609 if (TREE_PRIVATE (fld
)) prot
= "PRIVATE";
610 else if (TREE_PROTECTED(fld
)) prot
= "PROTECTED";
611 else prot
= "PUBLIC";
614 if (TREE_CODE (fld
) == FUNCTION_DECL
&& DECL_CONST_MEMFUNC_P(fld
))
616 else if (TREE_CODE (fld
) == CONST_DECL
)
620 if (TREE_CODE (fld
) == FUNCTION_DECL
&& DECL_ABSTRACT_VIRTUAL_P(fld
))
623 d
= IDENTIFIER_POINTER(cls
);
624 sprintf(buf
, "%d%s", strlen(d
), d
);
626 strcpy(bufa
, declname(fld
));
628 #ifdef XREF_SHORT_MEMBER_NAMES
629 for (p
= &bufa
[1]; *p
!= 0; ++p
)
631 if (p
[0] == '_' && p
[1] == '_' && p
[2] >= '0' && p
[2] <= '9') {
632 if (strncmp(&p
[2], buf
, i
) == 0) *p
= 0;
635 else if (p
[0] == '_' && p
[1] == '_' && p
[2] == 'C' && p
[3] >= '0' && p
[3] <= '9') {
636 if (strncmp(&p
[3], buf
, i
) == 0) *p
= 0;
642 fprintf(xref_file
, "MEM %s %d %s %s %s %d %d %d %d %d %d %d\n",
643 filename(xf
), fld
->decl
.linenum
, d
, bufa
, prot
,
644 (TREE_CODE (fld
) == FUNCTION_DECL
? 0 : 1),
645 (DECL_INLINE (fld
) ? 1 : 0),
646 (DECL_FRIEND_P(fld
) ? 1 : 0),
647 (DECL_VINDEX(fld
) ? 1 : 0),
648 (TREE_STATIC(fld
) ? 1 : 0),
652 /* Find file entry given name. */
660 for (xf
= all_files
; xf
!= NULL
; xf
= xf
->next
) {
661 if (STREQL(name
, xf
->name
)) break;
667 /* Return filename for output purposes. */
678 if (last_file
== xf
) return "*";
685 /* Return function name for output purposes. */
691 static char fctbuf
[1024];
694 if (fndecl
== NULL
&& last_fndecl
== NULL
) return "*";
702 if (fndecl
== last_fndecl
) return "*";
704 last_fndecl
= fndecl
;
706 s
= declname(fndecl
);
707 s
= fixname(s
, fctbuf
);
712 /* Return decl name for output purposes. */
718 if (DECL_NAME (dcl
) == NULL
) return "?";
720 if (DECL_ASSEMBLER_NAME (dcl
))
721 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (dcl
));
723 return IDENTIFIER_POINTER (DECL_NAME (dcl
));
726 /* Simplify a type string by removing unneeded parenthesis. */
736 while (i
> 0 && isspace(typ
[i
-1])) typ
[--i
] = 0;
738 if (i
> 7 && STREQL(&typ
[i
-5], "const"))
744 if (typ
[i
-1] != ')') return;
749 if (*s
== ')') ++lvl
;
763 if (*s
!= 0 && s
[-1] == ')')
767 if (*s
== '(') s
[2] = 0;
768 else if (*s
== ':') {
769 while (*s
!= '(') --s
;
776 /* Fixup a function name (take care of embedded spaces). */
802 if (fg
== 0) return nam
;
807 /* Open file for xreffing. */
815 #ifdef XREF_FILE_NAME
816 XREF_FILE_NAME (xref_name
, file
);
818 s
= rindex (file
, '/');
820 sprintf (xref_name
, ".%s.gxref", file
);
824 strcpy (xref_name
, file
);
825 t
= rindex (xref_name
, '/');
829 strcat (t
, ".gxref");
831 #endif /* no XREF_FILE_NAME */
833 xref_file
= fopen(xref_name
, "w");
835 if (xref_file
== NULL
)
837 error("Can't create cross-reference file `%s'", xref_name
);