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)
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. */
30 extern char *getpwd PROTO((void));
32 /* The character(s) used to join a directory specification (obtained with
33 getwd or equivalent) with a non-absolute file name. */
35 #ifndef FILE_NAME_JOINER
36 #define FILE_NAME_JOINER "/"
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] == '/')
44 /* For cross referencing. */
48 /************************************************************************/
50 /* Common definitions */
52 /************************************************************************/
61 #define PALLOC(typ) ((typ *) calloc(1,sizeof(typ)))
64 /* Return a malloc'd copy of STR. */
66 ((char *) ((str) == NULL ? NULL \
67 : (char *) strcpy ((char *) malloc (strlen ((str)) + 1), (str))))
68 #define SFREE(str) (str != NULL && (free(str),0))
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)
77 /************************************************************************/
79 /* Type definitions */
81 /************************************************************************/
84 typedef struct _XREF_FILE * XREF_FILE;
85 typedef struct _XREF_SCOPE * XREF_SCOPE;
87 typedef struct _XREF_FILE
94 typedef struct _XREF_SCOPE
103 /************************************************************************/
107 /************************************************************************/
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;
119 /************************************************************************/
121 /* Forward definitions */
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));
134 /* Start cross referencing. FILE is the name of the file we xref. */
137 GNU_xref_begin (file)
142 if (file != NULL && STRNEQ (file,"-"))
144 open_xref_file(file);
149 /* Finish cross-referencing. ERRCNT is the number of errors
158 if (!doing_xref) return;
160 xf = find_file (input_filename);
161 if (xf == NULL) return;
163 while (cur_scope != NULL)
164 GNU_xref_end_scope(cur_scope->gid,0,0,0);
168 if (xref_file == NULL) return;
175 if (ect > 0) unlink (xref_name);
178 /* Write out xref for file named NAME. */
186 if (!doing_xref || name == NULL) return;
188 if (xref_file == NULL)
190 open_xref_file (name);
191 if (!doing_xref) return;
194 if (all_files == NULL)
195 fprintf(xref_file,"SCP * 0 0 0 0 RESET\n");
197 xf = find_file (name);
198 if (xf != NULL) return;
200 xf = PALLOC (XREF_FILE_INFO);
201 xf->name = SALLOC (name);
202 xf->next = all_files;
208 if (FILE_NAME_ABSOLUTE_P (name) || ! wd_name)
209 xf->outname = xf->name;
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);
220 fprintf (xref_file, "FIL %s %s 0\n", name, wd_name);
226 /* Start a scope identified at level ID. */
229 GNU_xref_start_scope (id)
235 if (!doing_xref) return;
236 xf = find_file (input_filename);
238 xs = PALLOC (XREF_SCOPE_INFO);
241 if (xs->start <= 0) xs->start = 1;
243 xs->lid = ++scope_ctr;
244 xs->outer = cur_scope;
248 /* Finish a scope at level ID.
251 KEEP is nonzero iff this scope is retained (nonzero if it's
252 a compiler-generated invisible scope).
256 GNU_xref_end_scope (id,inid,prm,keep)
262 XREF_SCOPE xs,lxs,oxs;
265 if (!doing_xref) return;
266 xf = find_file (input_filename);
267 if (xf == NULL) return;
270 for (xs = cur_scope; xs != NULL; xs = xs->outer)
272 if (xs->gid == id) break;
275 if (xs == NULL) return;
278 for (oxs = cur_scope; oxs != NULL; oxs = oxs->outer) {
279 if (oxs->gid == inid) break;
281 if (oxs == NULL) return;
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";
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);
295 if (lxs == NULL) cur_scope = xs->outer;
296 else lxs->outer = xs->outer;
301 /* Output a reference to NAME in FNDECL. */
304 GNU_xref_ref (fndecl,name)
310 if (!doing_xref) return;
311 xf = find_file (input_filename);
312 if (xf == NULL) return;
314 fprintf (xref_file, "REF %s %d %s %s\n",
315 filename (xf), lineno, fctname (fndecl), name);
318 /* Output a reference to DECL in FNDECL. */
321 GNU_xref_decl (fndecl,decl)
331 if (!doing_xref) return;
332 xf = find_file (input_filename);
333 if (xf == NULL) return;
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)
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";
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)
356 if (DECL_EXTERNAL (decl)) cls = "EXTERN";
357 else if (TREE_PUBLIC (decl)) cls = "EFUNCTION";
358 else cls = "SFUNCTION";
360 else if (TREE_CODE (decl) == LABEL_DECL) cls = "LABEL";
361 else if (TREE_CODE (decl) == UNION_TYPE)
364 decl = TYPE_NAME (decl);
367 else if (TREE_CODE (decl) == RECORD_TYPE)
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);
375 else if (TREE_CODE (decl) == ENUMERAL_TYPE)
378 decl = TYPE_NAME (decl);
381 else if (TREE_CODE (decl) == TEMPLATE_DECL)
383 if (TREE_CODE (DECL_RESULT (decl)) == TYPE_DECL)
385 else if (TREE_CODE (DECL_RESULT (decl)) == FUNCTION_DECL)
387 else if (TREE_CODE (DECL_RESULT (decl)) == VAR_DECL)
390 my_friendly_abort (358);
393 else cls = "UNKNOWN";
395 if (decl == NULL || DECL_NAME (decl) == NULL) return;
397 if (uselin && decl->decl.linenum > 0 && decl->decl.filename != NULL)
399 xf1 = find_file (decl->decl.filename);
402 lineno = decl->decl.linenum;
407 if (DECL_ASSEMBLER_NAME (decl))
408 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
410 name = IDENTIFIER_POINTER (DECL_NAME (decl));
412 strcpy (buf, type_as_string (TREE_TYPE (decl), 0));
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);
420 if (STREQL (cls, "STRUCTID") || STREQL (cls, "UNIONID")
421 || STREQL (cls, "SIGNATUREID"))
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);
431 /* Output a reference to a call to NAME in FNDECL. */
434 GNU_xref_call (fndecl, name)
442 if (!doing_xref) return;
443 xf = find_file (input_filename);
444 if (xf == NULL) return;
445 name = fixname (name, buf);
447 for (s = name; *s != 0; ++s)
448 if (*s == '_' && s[1] == '_') break;
449 if (*s != 0) GNU_xref_ref (fndecl, name);
451 fprintf (xref_file, "CAL %s %d %s %s\n",
452 filename (xf), lineno, name, fctname (fndecl));
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). */
460 GNU_xref_function (fndecl, args)
468 if (!doing_xref) return;
469 xf = find_file (input_filename);
470 if (xf == NULL) return;
474 if (args == NULL) args = DECL_ARGUMENTS (fndecl);
476 GNU_xref_decl (NULL, fndecl);
478 for ( ; args != NULL; args = TREE_CHAIN (args))
480 GNU_xref_decl (fndecl,args);
481 if (ct != 0) strcat (buf,",");
482 strcat (buf, declname (args));
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),
492 /* Output cross-reference info about an assignment to NAME. */
495 GNU_xref_assign(name)
500 if (!doing_xref) return;
501 xf = find_file(input_filename);
502 if (xf == NULL) return;
504 gen_assign(xf, name);
516 switch (TREE_CODE (name))
518 case IDENTIFIER_NODE :
519 s = IDENTIFIER_POINTER(name);
525 gen_assign(xf, TREE_OPERAND(name, 0));
526 gen_assign(xf, TREE_OPERAND(name, 1));
532 gen_assign(xf, TREE_OPERAND(name, 0));
535 gen_assign(xf, TREE_OPERAND(name, 1));
542 fprintf(xref_file, "ASG %s %d %s\n", filename(xf), lineno, s);
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);
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
564 ??? Needs to handle nested classes. */
567 GNU_xref_hier(cls, base, pub, virt, frnd)
576 if (!doing_xref) return;
577 xf = find_file(input_filename);
578 if (xf == NULL) return;
580 fprintf(xref_file, "HIE %s %d %s %s %d %d %d\n",
581 filename(xf), lineno, classname (cls), classname (base),
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", (int) 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((unsigned char) 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). */
802 if (fg == 0) return nam;
807 /* Open file for xreffing. */
816 #ifdef XREF_FILE_NAME
817 XREF_FILE_NAME (xref_name, file);
819 s = rindex (file, '/');
821 sprintf (xref_name, ".%s.gxref", file);
825 strcpy (xref_name, file);
826 t = rindex (xref_name, '/');
830 strcat (t, ".gxref");
832 #endif /* no XREF_FILE_NAME */
834 xref_file = fopen(xref_name, "w");
836 if (xref_file == NULL)
838 error("Can't create cross-reference file `%s'", xref_name);