1 /* Code for handling XREF output from GNU C++.
2 Copyright (C) 1992, 1993 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
30 extern char *getpwd ();
32 extern char *index ();
33 extern char *rindex ();
35 /* The character(s) used to join a directory specification (obtained with
36 getwd or equivalent) with a non-absolute file name. */
38 #ifndef FILE_NAME_JOINER
39 #define FILE_NAME_JOINER "/"
42 /* Nonzero if NAME as a file name is absolute. */
43 #ifndef FILE_NAME_ABSOLUTE_P
44 #define FILE_NAME_ABSOLUTE_P(NAME) (NAME[0] == '/')
47 /* For cross referencing. */
51 /************************************************************************/
53 /* Common definitions */
55 /************************************************************************/
67 #define PALLOC(typ) ((typ *) calloc(1,sizeof(typ)))
70 /* Return a malloc'd copy of STR. */
72 ((char *) ((str) == NULL ? NULL \
73 : (char *) strcpy ((char *) malloc (strlen ((str)) + 1), (str))))
74 #define SFREE(str) (str != NULL && (free(str),0))
76 #define STREQL(s1,s2) (strcmp((s1),(s2)) == 0)
77 #define STRNEQ(s1,s2) (strcmp((s1),(s2)) != 0)
78 #define STRLSS(s1,s2) (strcmp((s1),(s2)) < 0)
79 #define STRLEQ(s1,s2) (strcmp((s1),(s2)) <= 0)
80 #define STRGTR(s1,s2) (strcmp((s1),(s2)) > 0)
81 #define STRGEQ(s1,s2) (strcmp((s1),(s2)) >= 0)
83 /************************************************************************/
85 /* Type definitions */
87 /************************************************************************/
90 typedef struct _XREF_FILE * XREF_FILE;
91 typedef struct _XREF_SCOPE * XREF_SCOPE;
93 typedef struct _XREF_FILE
100 typedef struct _XREF_SCOPE
109 /************************************************************************/
113 /************************************************************************/
115 static char doing_xref = 0;
116 static FILE * xref_file = NULL;
117 static char xref_name[1024];
118 static XREF_FILE all_files = NULL;
119 static char * wd_name = NULL;
120 static XREF_SCOPE cur_scope = NULL;
121 static int scope_ctr = 0;
122 static XREF_FILE last_file = NULL;
123 static tree last_fndecl = NULL;
125 /************************************************************************/
127 /* Forward definitions */
129 /************************************************************************/
131 extern void GNU_xref_begin();
132 extern void GNU_xref_end();
133 extern void GNU_xref_file();
134 extern void GNU_xref_start_scope();
135 extern void GNU_xref_end_scope();
136 extern void GNU_xref_ref();
137 extern void GNU_xref_decl();
138 extern void GNU_xref_call();
139 extern void GNU_xref_function();
140 extern void GNU_xref_assign();
141 extern void GNU_xref_hier();
142 extern void GNU_xref_member();
144 static void gen_assign();
145 static XREF_FILE find_file();
146 static char * filename();
147 static char * fctname();
148 static char * declname();
149 static void simplify_type();
150 static char * fixname();
151 static void open_xref_file();
153 extern char * type_as_string();
155 /* Start cross referencing. FILE is the name of the file we xref. */
158 GNU_xref_begin (file)
163 if (file != NULL && STRNEQ (file,"-"))
165 open_xref_file(file);
170 /* Finish cross-referencing. ERRCNT is the number of errors
179 if (!doing_xref) return;
181 xf = find_file (input_filename);
182 if (xf == NULL) return;
184 while (cur_scope != NULL)
185 GNU_xref_end_scope(cur_scope->gid,0,0,0,0);
189 if (xref_file == NULL) return;
196 if (ect > 0) unlink (xref_name);
199 /* Write out xref for file named NAME. */
207 if (!doing_xref || name == NULL) return;
209 if (xref_file == NULL)
211 open_xref_file (name);
212 if (!doing_xref) return;
215 if (all_files == NULL)
216 fprintf(xref_file,"SCP * 0 0 0 0 RESET\n");
218 xf = find_file (name);
219 if (xf != NULL) return;
221 xf = PALLOC (XREF_FILE_INFO);
222 xf->name = SALLOC (name);
223 xf->next = all_files;
229 if (FILE_NAME_ABSOLUTE_P (name) || ! wd_name)
230 xf->outname = xf->name;
234 = (char *) malloc (strlen (wd_name) + strlen (FILE_NAME_JOINER)
235 + strlen (name) + 1);
236 sprintf (nmbuf, "%s%s%s", wd_name, FILE_NAME_JOINER, name);
241 fprintf (xref_file, "FIL %s %s 0\n", name, wd_name);
247 /* Start a scope identified at level ID. */
250 GNU_xref_start_scope (id)
256 if (!doing_xref) return;
257 xf = find_file (input_filename);
259 xs = PALLOC (XREF_SCOPE_INFO);
262 if (xs->start <= 0) xs->start = 1;
264 xs->lid = ++scope_ctr;
265 xs->outer = cur_scope;
269 /* Finish a scope at level ID.
272 KEEP is nonzero iff this scope is retained (nonzero if it's
273 a compiler-generated invisible scope).
277 GNU_xref_end_scope (id,inid,prm,keep,trns)
283 XREF_SCOPE xs,lxs,oxs;
286 if (!doing_xref) return;
287 xf = find_file (input_filename);
288 if (xf == NULL) return;
291 for (xs = cur_scope; xs != NULL; xs = xs->outer)
293 if (xs->gid == id) break;
296 if (xs == NULL) return;
299 for (oxs = cur_scope; oxs != NULL; oxs = oxs->outer) {
300 if (oxs->gid == inid) break;
302 if (oxs == NULL) return;
306 if (prm == 2) stype = "SUE";
307 else if (prm != 0) stype = "ARGS";
308 else if (keep == 2 || inid != 0) stype = "INTERN";
309 else stype = "EXTERN";
311 fprintf (xref_file,"SCP %s %d %d %d %d %s\n",
312 filename (xf), xs->start, lineno,xs->lid, inid, stype);
314 if (lxs == NULL) cur_scope = xs->outer;
315 else lxs->outer = xs->outer;
320 /* Output a reference to NAME in FNDECL. */
323 GNU_xref_ref (fndecl,name)
329 if (!doing_xref) return;
330 xf = find_file (input_filename);
331 if (xf == NULL) return;
333 fprintf (xref_file, "REF %s %d %s %s\n",
334 filename (xf), lineno, fctname (fndecl), name);
337 /* Output a reference to DECL in FNDECL. */
340 GNU_xref_decl (fndecl,decl)
350 if (!doing_xref) return;
351 xf = find_file (input_filename);
352 if (xf == NULL) return;
356 if (TREE_CODE (decl) == TYPE_DECL) cls = "TYPEDEF";
357 else if (TREE_CODE (decl) == FIELD_DECL) cls = "FIELD";
358 else if (TREE_CODE (decl) == VAR_DECL)
360 if (fndecl == NULL && TREE_STATIC(decl)
361 && TREE_READONLY(decl) && DECL_INITIAL(decl) != 0
362 && !TREE_PUBLIC(decl) && !DECL_EXTERNAL(decl)
363 && DECL_MODE(decl) != BLKmode) cls = "CONST";
364 else if (DECL_EXTERNAL(decl)) cls = "EXTERN";
365 else if (TREE_PUBLIC(decl)) cls = "EXTDEF";
366 else if (TREE_STATIC(decl)) cls = "STATIC";
367 else if (DECL_REGISTER(decl)) cls = "REGISTER";
370 else if (TREE_CODE (decl) == PARM_DECL) cls = "PARAM";
371 else if (TREE_CODE (decl) == FIELD_DECL) cls = "FIELD";
372 else if (TREE_CODE (decl) == CONST_DECL) cls = "CONST";
373 else if (TREE_CODE (decl) == FUNCTION_DECL)
375 if (DECL_EXTERNAL (decl)) cls = "EXTERN";
376 else if (TREE_PUBLIC (decl)) cls = "EFUNCTION";
377 else cls = "SFUNCTION";
379 else if (TREE_CODE (decl) == LABEL_DECL) cls = "LABEL";
380 else if (TREE_CODE (decl) == UNION_TYPE)
383 decl = TYPE_NAME (decl);
386 else if (TREE_CODE (decl) == RECORD_TYPE)
388 if (CLASSTYPE_DECLARED_CLASS (decl)) cls = "CLASSID";
389 else if (IS_SIGNATURE (decl)) cls = "SIGNATUREID";
390 else cls = "STRUCTID";
391 decl = TYPE_NAME (decl);
394 else if (TREE_CODE (decl) == ENUMERAL_TYPE)
397 decl = TYPE_NAME (decl);
400 else cls = "UNKNOWN";
402 if (decl == NULL || DECL_NAME (decl) == NULL) return;
404 if (uselin && decl->decl.linenum > 0 && decl->decl.filename != NULL)
406 xf1 = find_file (decl->decl.filename);
409 lineno = decl->decl.linenum;
414 if (DECL_ASSEMBLER_NAME (decl))
415 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
417 name = IDENTIFIER_POINTER (DECL_NAME (decl));
419 strcpy (buf, type_as_string (TREE_TYPE (decl), 0));
422 fprintf (xref_file, "DCL %s %d %s %d %s %s %s\n",
423 filename(xf), lineno, name,
424 (cur_scope != NULL ? cur_scope->lid : 0),
425 cls, fctname(fndecl), buf);
427 if (STREQL (cls, "STRUCTID") || STREQL (cls, "UNIONID")
428 || STREQL (cls, "SIGNATUREID"))
431 fprintf (xref_file, "DCL %s %d %s %d %s %s %s\n",
432 filename(xf), lineno,name,
433 (cur_scope != NULL ? cur_scope->lid : 0),
434 cls, fctname(fndecl), buf);
438 /* Output a reference to a call to NAME in FNDECL. */
441 GNU_xref_call (fndecl, name)
449 if (!doing_xref) return;
450 xf = find_file (input_filename);
451 if (xf == NULL) return;
452 name = fixname (name, buf);
454 for (s = name; *s != 0; ++s)
455 if (*s == '_' && s[1] == '_') break;
456 if (*s != 0) GNU_xref_ref (fndecl, name);
458 fprintf (xref_file, "CAL %s %d %s %s\n",
459 filename (xf), lineno, name, fctname (fndecl));
462 /* Output cross-reference info about FNDECL. If non-NULL,
463 ARGS are the arguments for the function (i.e., before the FUNCTION_DECL
464 has been fully built). */
467 GNU_xref_function (fndecl, args)
475 if (!doing_xref) return;
476 xf = find_file (input_filename);
477 if (xf == NULL) return;
481 if (args == NULL) args = DECL_ARGUMENTS (fndecl);
483 GNU_xref_decl (NULL, fndecl);
485 for ( ; args != NULL; args = TREE_CHAIN (args))
487 GNU_xref_decl (fndecl,args);
488 if (ct != 0) strcat (buf,",");
489 strcat (buf, declname (args));
493 fprintf (xref_file, "PRC %s %d %s %d %d %s\n",
494 filename(xf), lineno, declname(fndecl),
495 (cur_scope != NULL ? cur_scope->lid : 0),
499 /* Output cross-reference info about an assignment to NAME. */
502 GNU_xref_assign(name)
507 if (!doing_xref) return;
508 xf = find_file(input_filename);
509 if (xf == NULL) return;
511 gen_assign(xf, name);
523 switch (TREE_CODE (name))
525 case IDENTIFIER_NODE :
526 s = IDENTIFIER_POINTER(name);
532 gen_assign(xf, TREE_OPERAND(name, 0));
533 gen_assign(xf, TREE_OPERAND(name, 1));
539 gen_assign(xf, TREE_OPERAND(name, 0));
542 gen_assign(xf, TREE_OPERAND(name, 1));
549 fprintf(xref_file, "ASG %s %d %s\n", filename(xf), lineno, s);
552 /* Output cross-reference info about a class hierarchy.
553 CLS is the class type of interest. BASE is a baseclass
554 for CLS. PUB and VIRT give the access info about
555 the class derivation. FRND is nonzero iff BASE is a friend
558 ??? Needs to handle nested classes. */
560 GNU_xref_hier(cls, base, pub, virt, frnd)
569 if (!doing_xref) return;
570 xf = find_file(input_filename);
571 if (xf == NULL) return;
573 fprintf(xref_file, "HIE %s %d %s %s %d %d %d\n",
574 filename(xf), lineno, cls, base, pub, virt, frnd);
577 /* Output cross-reference info about class members. CLS
578 is the containing type; FLD is the class member. */
581 GNU_xref_member(cls, fld)
590 char buf[1024], bufa[1024];
592 if (!doing_xref) return;
593 xf = find_file(fld->decl.filename);
594 if (xf == NULL) return;
596 if (TREE_PRIVATE (fld)) prot = "PRIVATE";
597 else if (TREE_PROTECTED(fld)) prot = "PROTECTED";
598 else prot = "PUBLIC";
601 if (TREE_CODE (fld) == FUNCTION_DECL && DECL_CONST_MEMFUNC_P(fld))
603 else if (TREE_CODE (fld) == CONST_DECL)
607 if (TREE_CODE (fld) == FUNCTION_DECL && DECL_ABSTRACT_VIRTUAL_P(fld))
610 d = IDENTIFIER_POINTER(cls);
611 sprintf(buf, "%d%s", strlen(d), d);
613 strcpy(bufa, declname(fld));
615 #ifdef XREF_SHORT_MEMBER_NAMES
616 for (p = &bufa[1]; *p != 0; ++p)
618 if (p[0] == '_' && p[1] == '_' && p[2] >= '0' && p[2] <= '9') {
619 if (strncmp(&p[2], buf, i) == 0) *p = 0;
622 else if (p[0] == '_' && p[1] == '_' && p[2] == 'C' && p[3] >= '0' && p[3] <= '9') {
623 if (strncmp(&p[3], buf, i) == 0) *p = 0;
629 fprintf(xref_file, "MEM %s %d %s %s %s %d %d %d %d %d %d %d\n",
630 filename(xf), fld->decl.linenum, d, bufa, prot,
631 (TREE_CODE (fld) == FUNCTION_DECL ? 0 : 1),
632 (DECL_INLINE (fld) ? 1 : 0),
633 (DECL_FRIEND_P(fld) ? 1 : 0),
634 (DECL_VINDEX(fld) ? 1 : 0),
635 (TREE_STATIC(fld) ? 1 : 0),
639 /* Find file entry given name. */
647 for (xf = all_files; xf != NULL; xf = xf->next) {
648 if (STREQL(name, xf->name)) break;
654 /* Return filename for output purposes. */
665 if (last_file == xf) return "*";
672 /* Return function name for output purposes. */
678 static char fctbuf[1024];
681 if (fndecl == NULL && last_fndecl == NULL) return "*";
689 if (fndecl == last_fndecl) return "*";
691 last_fndecl = fndecl;
693 s = declname(fndecl);
694 s = fixname(s, fctbuf);
699 /* Return decl name for output purposes. */
705 if (DECL_NAME (dcl) == NULL) return "?";
707 if (DECL_ASSEMBLER_NAME (dcl))
708 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (dcl));
710 return IDENTIFIER_POINTER (DECL_NAME (dcl));
713 /* Simplify a type string by removing unneeded parenthesis. */
723 while (i > 0 && isspace(typ[i-1])) typ[--i] = 0;
725 if (i > 7 && STREQL(&typ[i-5], "const"))
731 if (typ[i-1] != ')') return;
736 if (*s == ')') ++lvl;
750 if (*s != 0 && s[-1] == ')')
754 if (*s == '(') s[2] = 0;
755 else if (*s == ':') {
756 while (*s != '(') --s;
763 /* Fixup a function name (take care of embedded spaces). */
789 if (fg == 0) return nam;
794 /* Open file for xrefing. */
802 #ifdef XREF_FILE_NAME
803 XREF_FILE_NAME (xref_name, file);
805 s = rindex (file, '/');
807 sprintf (xref_name, ".%s.gxref", file);
811 strcpy (xref_name, file);
812 t = rindex (xref_name, '/');
816 strcat (t, ".gxref");
818 #endif /* no XREF_FILE_NAME */
820 xref_file = fopen(xref_name, "w");
822 if (xref_file == NULL)
824 error("Can't create cross-reference file `%s'", xref_name);