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)
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. */
39 extern char *getpwd
PROTO((void));
45 #ifdef NEED_DECLARATION_RINDEX
46 extern char *rindex ();
49 /* The character(s) used to join a directory specification (obtained with
50 getwd or equivalent) with a non-absolute file name. */
52 #ifndef FILE_NAME_JOINER
53 #define FILE_NAME_JOINER "/"
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] == '/')
61 /* For cross referencing. */
65 /************************************************************************/
67 /* Common definitions */
69 /************************************************************************/
78 #define PALLOC(typ) ((typ *) calloc(1,sizeof(typ)))
81 /* Return a malloc'd copy of STR. */
83 ((char *) ((str) == NULL ? NULL \
84 : (char *) strcpy ((char *) malloc (strlen ((str)) + 1), (str))))
85 #define SFREE(str) (str != NULL && (free(str),0))
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)
94 /************************************************************************/
96 /* Type definitions */
98 /************************************************************************/
101 typedef struct _XREF_FILE
* XREF_FILE
;
102 typedef struct _XREF_SCOPE
* XREF_SCOPE
;
104 typedef struct _XREF_FILE
111 typedef struct _XREF_SCOPE
120 /************************************************************************/
124 /************************************************************************/
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
;
136 /************************************************************************/
138 /* Forward definitions */
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 *));
150 /* Start cross referencing. FILE is the name of the file we xref. */
153 GNU_xref_begin (file
)
158 if (file
!= NULL
&& STRNEQ (file
,"-"))
160 open_xref_file(file
);
165 /* Finish cross-referencing. ERRCNT is the number of errors
174 if (!doing_xref
) return;
176 xf
= find_file (input_filename
);
177 if (xf
== NULL
) return;
179 while (cur_scope
!= NULL
)
180 GNU_xref_end_scope(cur_scope
->gid
,0,0,0);
184 if (xref_file
== NULL
) return;
191 if (ect
> 0) unlink (xref_name
);
194 /* Write out xref for file named NAME. */
202 if (!doing_xref
|| name
== NULL
) return;
204 if (xref_file
== NULL
)
206 open_xref_file (name
);
207 if (!doing_xref
) return;
210 if (all_files
== NULL
)
211 fprintf(xref_file
,"SCP * 0 0 0 0 RESET\n");
213 xf
= find_file (name
);
214 if (xf
!= NULL
) return;
216 xf
= PALLOC (XREF_FILE_INFO
);
217 xf
->name
= SALLOC (name
);
218 xf
->next
= all_files
;
224 if (FILE_NAME_ABSOLUTE_P (name
) || ! wd_name
)
225 xf
->outname
= xf
->name
;
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
);
236 fprintf (xref_file
, "FIL %s %s 0\n", name
, wd_name
);
242 /* Start a scope identified at level ID. */
245 GNU_xref_start_scope (id
)
251 if (!doing_xref
) return;
252 xf
= find_file (input_filename
);
254 xs
= PALLOC (XREF_SCOPE_INFO
);
257 if (xs
->start
<= 0) xs
->start
= 1;
259 xs
->lid
= ++scope_ctr
;
260 xs
->outer
= cur_scope
;
264 /* Finish a scope at level ID.
267 KEEP is nonzero iff this scope is retained (nonzero if it's
268 a compiler-generated invisible scope).
272 GNU_xref_end_scope (id
,inid
,prm
,keep
)
278 XREF_SCOPE xs
,lxs
,oxs
;
281 if (!doing_xref
) return;
282 xf
= find_file (input_filename
);
283 if (xf
== NULL
) return;
286 for (xs
= cur_scope
; xs
!= NULL
; xs
= xs
->outer
)
288 if (xs
->gid
== id
) break;
291 if (xs
== NULL
) return;
294 for (oxs
= cur_scope
; oxs
!= NULL
; oxs
= oxs
->outer
) {
295 if (oxs
->gid
== inid
) break;
297 if (oxs
== NULL
) return;
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";
306 fprintf (xref_file
,"SCP %s %d %d %d %d %s\n",
307 filename (xf
), xs
->start
, lineno
,xs
->lid
, inid
, stype
);
309 if (lxs
== NULL
) cur_scope
= xs
->outer
;
310 else lxs
->outer
= xs
->outer
;
315 /* Output a reference to NAME in FNDECL. */
318 GNU_xref_ref (fndecl
,name
)
324 if (!doing_xref
) return;
325 xf
= find_file (input_filename
);
326 if (xf
== NULL
) return;
328 fprintf (xref_file
, "REF %s %d %s %s\n",
329 filename (xf
), lineno
, fctname (fndecl
), name
);
332 /* Output a reference to DECL in FNDECL. */
335 GNU_xref_decl (fndecl
,decl
)
345 if (!doing_xref
) return;
346 xf
= find_file (input_filename
);
347 if (xf
== NULL
) return;
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
)
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";
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
)
370 if (DECL_EXTERNAL (decl
)) cls
= "EXTERN";
371 else if (TREE_PUBLIC (decl
)) cls
= "EFUNCTION";
372 else cls
= "SFUNCTION";
374 else if (TREE_CODE (decl
) == LABEL_DECL
) cls
= "LABEL";
375 else if (TREE_CODE (decl
) == UNION_TYPE
)
378 decl
= TYPE_NAME (decl
);
381 else if (TREE_CODE (decl
) == RECORD_TYPE
)
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
);
389 else if (TREE_CODE (decl
) == ENUMERAL_TYPE
)
392 decl
= TYPE_NAME (decl
);
395 else if (TREE_CODE (decl
) == TEMPLATE_DECL
)
397 if (TREE_CODE (DECL_RESULT (decl
)) == TYPE_DECL
)
399 else if (TREE_CODE (DECL_RESULT (decl
)) == FUNCTION_DECL
)
401 else if (TREE_CODE (DECL_RESULT (decl
)) == VAR_DECL
)
404 my_friendly_abort (358);
407 else cls
= "UNKNOWN";
409 if (decl
== NULL
|| DECL_NAME (decl
) == NULL
) return;
411 if (uselin
&& decl
->decl
.linenum
> 0 && decl
->decl
.filename
!= NULL
)
413 xf1
= find_file (decl
->decl
.filename
);
416 lineno
= decl
->decl
.linenum
;
421 if (DECL_ASSEMBLER_NAME (decl
))
422 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
424 name
= IDENTIFIER_POINTER (DECL_NAME (decl
));
426 strcpy (buf
, type_as_string (TREE_TYPE (decl
), 0));
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
);
434 if (STREQL (cls
, "STRUCTID") || STREQL (cls
, "UNIONID")
435 || STREQL (cls
, "SIGNATUREID"))
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
);
445 /* Output a reference to a call to NAME in FNDECL. */
448 GNU_xref_call (fndecl
, name
)
456 if (!doing_xref
) return;
457 xf
= find_file (input_filename
);
458 if (xf
== NULL
) return;
459 name
= fixname (name
, buf
);
461 for (s
= name
; *s
!= 0; ++s
)
462 if (*s
== '_' && s
[1] == '_') break;
463 if (*s
!= 0) GNU_xref_ref (fndecl
, name
);
465 fprintf (xref_file
, "CAL %s %d %s %s\n",
466 filename (xf
), lineno
, name
, fctname (fndecl
));
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). */
474 GNU_xref_function (fndecl
, args
)
482 if (!doing_xref
) return;
483 xf
= find_file (input_filename
);
484 if (xf
== NULL
) return;
488 if (args
== NULL
) args
= DECL_ARGUMENTS (fndecl
);
490 GNU_xref_decl (NULL
, fndecl
);
492 for ( ; args
!= NULL
; args
= TREE_CHAIN (args
))
494 GNU_xref_decl (fndecl
,args
);
495 if (ct
!= 0) strcat (buf
,",");
496 strcat (buf
, declname (args
));
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),
506 /* Output cross-reference info about an assignment to NAME. */
509 GNU_xref_assign(name
)
514 if (!doing_xref
) return;
515 xf
= find_file(input_filename
);
516 if (xf
== NULL
) return;
518 gen_assign(xf
, name
);
530 switch (TREE_CODE (name
))
532 case IDENTIFIER_NODE
:
533 s
= IDENTIFIER_POINTER(name
);
539 gen_assign(xf
, TREE_OPERAND(name
, 0));
540 gen_assign(xf
, TREE_OPERAND(name
, 1));
546 gen_assign(xf
, TREE_OPERAND(name
, 0));
549 gen_assign(xf
, TREE_OPERAND(name
, 1));
556 fprintf(xref_file
, "ASG %s %d %s\n", filename(xf
), lineno
, s
);
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
565 ??? Needs to handle nested classes. */
568 GNU_xref_hier(cls
, base
, pub
, virt
, frnd
)
577 if (!doing_xref
) return;
578 xf
= find_file(input_filename
);
579 if (xf
== NULL
) return;
581 fprintf(xref_file
, "HIE %s %d %s %s %d %d %d\n",
582 filename(xf
), lineno
, cls
, base
, pub
, virt
, frnd
);
585 /* Output cross-reference info about class members. CLS
586 is the containing type; FLD is the class member. */
589 GNU_xref_member(cls
, fld
)
597 #ifdef XREF_SHORT_MEMBER_NAMES
600 char buf
[1024], bufa
[1024];
602 if (!doing_xref
) return;
603 xf
= find_file(fld
->decl
.filename
);
604 if (xf
== NULL
) return;
606 if (TREE_PRIVATE (fld
)) prot
= "PRIVATE";
607 else if (TREE_PROTECTED(fld
)) prot
= "PROTECTED";
608 else prot
= "PUBLIC";
611 if (TREE_CODE (fld
) == FUNCTION_DECL
&& DECL_CONST_MEMFUNC_P(fld
))
613 else if (TREE_CODE (fld
) == CONST_DECL
)
617 if (TREE_CODE (fld
) == FUNCTION_DECL
&& DECL_ABSTRACT_VIRTUAL_P(fld
))
620 d
= IDENTIFIER_POINTER(cls
);
621 sprintf(buf
, "%d%s", strlen(d
), d
);
622 #ifdef XREF_SHORT_MEMBER_NAMES
625 strcpy(bufa
, declname(fld
));
627 #ifdef XREF_SHORT_MEMBER_NAMES
628 for (p
= &bufa
[1]; *p
!= 0; ++p
)
630 if (p
[0] == '_' && p
[1] == '_' && p
[2] >= '0' && p
[2] <= '9') {
631 if (strncmp(&p
[2], buf
, i
) == 0) *p
= 0;
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;
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),
651 /* Find file entry given name. */
659 for (xf
= all_files
; xf
!= NULL
; xf
= xf
->next
) {
660 if (STREQL(name
, xf
->name
)) break;
666 /* Return filename for output purposes. */
677 if (last_file
== xf
) return "*";
684 /* Return function name for output purposes. */
690 static char fctbuf
[1024];
693 if (fndecl
== NULL
&& last_fndecl
== NULL
) return "*";
701 if (fndecl
== last_fndecl
) return "*";
703 last_fndecl
= fndecl
;
705 s
= declname(fndecl
);
706 s
= fixname(s
, fctbuf
);
711 /* Return decl name for output purposes. */
717 if (DECL_NAME (dcl
) == NULL
) return "?";
719 if (DECL_ASSEMBLER_NAME (dcl
))
720 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (dcl
));
722 return IDENTIFIER_POINTER (DECL_NAME (dcl
));
725 /* Simplify a type string by removing unneeded parenthesis. */
735 while (i
> 0 && isspace(typ
[i
-1])) typ
[--i
] = 0;
737 if (i
> 7 && STREQL(&typ
[i
-5], "const"))
743 if (typ
[i
-1] != ')') return;
748 if (*s
== ')') ++lvl
;
762 if (*s
!= 0 && s
[-1] == ')')
766 if (*s
== '(') s
[2] = 0;
767 else if (*s
== ':') {
768 while (*s
!= '(') --s
;
775 /* Fixup a function name (take care of embedded spaces). */
801 if (fg
== 0) return nam
;
806 /* Open file for xreffing. */
814 #ifdef XREF_FILE_NAME
815 XREF_FILE_NAME (xref_name
, file
);
817 s
= rindex (file
, '/');
819 sprintf (xref_name
, ".%s.gxref", file
);
823 strcpy (xref_name
, file
);
824 t
= rindex (xref_name
, '/');
828 strcat (t
, ".gxref");
830 #endif /* no XREF_FILE_NAME */
832 xref_file
= fopen(xref_name
, "w");
834 if (xref_file
== NULL
)
836 error("Can't create cross-reference file `%s'", xref_name
);