1 /************************************************************************/
5 /* Code for handling XREF output from g++ */
7 /************************************************************************/
14 #include "cplus-tree.h"
24 /************************************************************************/
26 /* Common definitions */
28 /************************************************************************/
31 typedef char * String
;
45 #define PALLOC(typ) ((typ *) calloc(1,sizeof(typ)))
48 #define SALLOC(str) ((String) ((str == NULL) ? NULL : (strcpy(malloc(strlen(str)+1),str))))
49 #define SFREE(str) (str != NULL && (free(str),0))
51 #define STREQL(s1,s2) (strcmp((s1),(s2)) == 0)
52 #define STRNEQ(s1,s2) (strcmp((s1),(s2)) != 0)
53 #define STRLSS(s1,s2) (strcmp((s1),(s2)) < 0)
54 #define STRLEQ(s1,s2) (strcmp((s1),(s2)) <= 0)
55 #define STRGTR(s1,s2) (strcmp((s1),(s2)) > 0)
56 #define STRGEQ(s1,s2) (strcmp((s1),(s2)) >= 0)
62 /************************************************************************/
64 /* Type definitions */
66 /************************************************************************/
69 typedef struct _XREF_FILE
* XREF_FILE
;
70 typedef struct _XREF_SCOPE
* XREF_SCOPE
;
74 typedef struct _XREF_FILE
{
83 typedef struct _XREF_SCOPE
{
96 /************************************************************************/
100 /************************************************************************/
103 static char doing_xref
= 0;
104 static FILE * xref_file
= NULL
;
105 static char xref_name
[1024];
106 static XREF_FILE all_files
= NULL
;
107 static String wd_name
= NULL
;
108 static XREF_SCOPE cur_scope
= NULL
;
109 static Integer scope_ctr
= 0;
110 static XREF_FILE last_file
= NULL
;
111 static tree last_fct
= NULL
;
116 /************************************************************************/
118 /* Forward definitions */
120 /************************************************************************/
123 extern void FIELD_xref_begin();
124 extern void FIELD_xref_end();
125 extern void FIELD_xref_file();
126 extern void FIELD_xref_start_scope();
127 extern void FIELD_xref_end_scope();
128 extern void FIELD_xref_ref();
129 extern void FIELD_xref_decl();
130 extern void FIELD_xref_call();
131 extern void FIELD_xref_function();
132 extern void FIELD_xref_assign();
133 extern void FIELD_xref_hier();
134 extern void FIELD_xref_member();
137 static void gen_assign();
138 static XREF_FILE
find_file();
139 static String
filename();
140 static String
fctname();
141 static String
declname();
142 static void simplify_type();
143 static String
fixname();
144 static void open_xref_file();
146 extern char * type_as_string();
151 /************************************************************************/
153 /* FIELD_xref_begin -- start cross referencing */
155 /************************************************************************/
159 FIELD_xref_begin(file
)
166 if (file
!= NULL
&& STRNEQ(file
,"-")) {
167 open_xref_file(file
);
168 FIELD_xref_file(file
);
176 /************************************************************************/
178 /* FIELD_xref_end -- finish cross referencing */
180 /************************************************************************/
189 if (!doing_xref
) return;
191 xf
= find_file(input_filename
);
192 if (xf
== NULL
) return;
194 while (cur_scope
!= NULL
) {
195 FIELD_xref_end_scope(cur_scope
->gid
,0,0,0,0);
200 if (xref_file
== NULL
) return;
207 if (ect
> 0) unlink(xref_name
);
215 /************************************************************************/
217 /* FIELD_xref_file -- handle file xrefing */
219 /************************************************************************/
223 FIELD_xref_file(name
)
229 if (!doing_xref
|| name
== NULL
) return;
231 if (xref_file
== NULL
) {
232 open_xref_file(name
);
233 if (!doing_xref
) return;
236 if (all_files
== NULL
) {
237 fprintf(xref_file
,"SCP * 0 0 0 0 RESET\n");
240 xf
= find_file(name
);
241 if (xf
!= NULL
) return;
243 xf
= PALLOC(XREF_FILE_INFO
);
244 xf
->name
= SALLOC(name
);
245 xf
->next
= all_files
;
248 if (wd_name
== NULL
) {
250 wd_name
= SALLOC(wdbuf
);
253 fprintf(xref_file
,"FIL %s %s 0\n",name
,wd_name
);
264 /************************************************************************/
266 /* FIELD_xref_start_scope -- begin a scope */
267 /* FIELD_xref_end_scope -- finish a scope */
269 /************************************************************************/
273 FIELD_xref_start_scope(id
)
279 if (!doing_xref
) return;
280 xf
= find_file(input_filename
);
282 xs
= PALLOC(XREF_SCOPE_INFO
);
285 if (xs
->start
<= 0) xs
->start
= 1;
287 xs
->lid
= ++scope_ctr
;
288 xs
->outer
= cur_scope
;
297 FIELD_xref_end_scope(id
,inid
,prm
,keep
,trns
)
300 Integer prm
,keep
,trns
;
303 XREF_SCOPE xs
,lxs
,oxs
;
306 if (!doing_xref
) return;
307 xf
= find_file(input_filename
);
308 if (xf
== NULL
) return;
311 for (xs
= cur_scope
; xs
!= NULL
; xs
= xs
->outer
) {
312 if (xs
->gid
== id
) break;
315 if (xs
== NULL
) return;
318 for (oxs
= cur_scope
; oxs
!= NULL
; oxs
= oxs
->outer
) {
319 if (oxs
->gid
== inid
) break;
321 if (oxs
== NULL
) return;
325 if (prm
== 2) stype
= "SUE";
326 else if (prm
!= 0) stype
= "ARGS";
327 else if (keep
== 2 || inid
!= 0) stype
= "INTERN";
328 else stype
= "EXTERN";
330 fprintf(xref_file
,"SCP %s %d %d %d %d %s\n",
331 filename(xf
),xs
->start
,lineno
,xs
->lid
,inid
,stype
);
333 if (lxs
== NULL
) cur_scope
= xs
->outer
;
334 else lxs
->outer
= xs
->outer
;
343 /************************************************************************/
345 /* FIELD_xref_ref -- handle reference */
347 /************************************************************************/
351 FIELD_xref_ref(fct
,name
)
357 if (!doing_xref
) return;
358 xf
= find_file(input_filename
);
359 if (xf
== NULL
) return;
361 fprintf(xref_file
,"REF %s %d %s %s\n",
362 filename(xf
),lineno
,fctname(fct
),name
);
370 /************************************************************************/
372 /* FIELD_xref_decl -- handle declaration */
374 /************************************************************************/
378 FIELD_xref_decl(fct
,decl
)
387 if (!doing_xref
) return;
388 xf
= find_file(input_filename
);
389 if (xf
== NULL
) return;
391 if (DECL_NAME(decl
) == NULL
) return;
393 if (TREE_CODE(decl
) == TYPE_DECL
) cls
= "TYPEDEF";
394 else if (TREE_CODE(decl
) == FIELD_DECL
) cls
= "FIELD";
395 else if (TREE_CODE(decl
) == VAR_DECL
) {
396 if (fct
== NULL
&& TREE_STATIC(decl
) &&
397 TREE_READONLY(decl
) && DECL_INITIAL(decl
) != 0 &&
398 !TREE_PUBLIC(decl
) && !TREE_EXTERNAL(decl
) &&
399 DECL_MODE(decl
) != BLKmode
) cls
= "CONST";
400 else if (TREE_EXTERNAL(decl
)) cls
= "EXTERN";
401 else if (TREE_PUBLIC(decl
)) cls
= "EXTDEF";
402 else if (TREE_STATIC(decl
)) cls
= "STATIC";
403 else if (TREE_REGDECL(decl
)) cls
= "REGISTER";
406 else if (TREE_CODE(decl
) == PARM_DECL
) cls
= "PARAM";
407 else if (TREE_CODE(decl
) == FIELD_DECL
) cls
= "FIELD";
408 else if (TREE_CODE(decl
) == CONST_DECL
) cls
= "CONST";
409 else if (TREE_CODE(decl
) == FUNCTION_DECL
) {
410 if (TREE_EXTERNAL(decl
)) cls
= "EXTERN";
411 else if (TREE_PUBLIC(decl
)) cls
= "EFUNCTION";
412 else cls
= "SFUNCTION";
414 else if (TREE_CODE(decl
) == LABEL_DECL
) cls
= "LABEL";
415 else if (TREE_CODE(decl
) == UNION_TYPE
) {
417 decl
= TYPE_NAME(decl
);
419 else if (TREE_CODE(decl
) == RECORD_TYPE
) {
420 if (CLASSTYPE_DECLARED_CLASS(decl
)) cls
= "CLASSID";
421 else cls
= "STRUCTID";
422 decl
= TYPE_NAME(decl
);
424 else if (TREE_CODE(decl
) == ENUMERAL_TYPE
) {
426 decl
= TYPE_NAME(decl
);
428 else cls
= "UNKNOWN";
430 name
= IDENTIFIER_POINTER(DECL_NAME(decl
));
432 type_as_string(buf
,TREE_TYPE(decl
));
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(fct
),buf
);
446 /************************************************************************/
448 /* FIELD_xref_call -- handle call */
450 /************************************************************************/
454 FIELD_xref_call(fct
,name
)
461 if (!doing_xref
) return;
462 xf
= find_file(input_filename
);
463 if (xf
== NULL
) return;
464 name
= fixname(name
,buf
);
466 fprintf(xref_file
,"CAL %s %d %s %s\n",
467 filename(xf
),lineno
,name
,fctname(fct
));
475 /************************************************************************/
477 /* FIELD_xref_function -- handle functions */
479 /************************************************************************/
483 FIELD_xref_function(fct
,args
)
491 if (!doing_xref
) return;
492 xf
= find_file(input_filename
);
493 if (xf
== NULL
) return;
497 if (args
== NULL
) args
= DECL_ARGUMENTS(fct
);
499 FIELD_xref_decl(NULL
,fct
);
501 for ( ; args
!= NULL
; args
= TREE_CHAIN(args
)) {
502 FIELD_xref_decl(fct
,args
);
503 if (ct
!= 0) strcat(buf
,",");
504 strcat(buf
,declname(args
));
508 fprintf(xref_file
,"PRC %s %d %s %d %d %s\n",
509 filename(xf
),lineno
,declname(fct
),
510 (cur_scope
!= NULL
? cur_scope
->lid
: 0),
519 /************************************************************************/
521 /* FIELD_xref_assign -- handle assignment */
523 /************************************************************************/
527 FIELD_xref_assign(name
)
532 if (!doing_xref
) return;
533 xf
= find_file(input_filename
);
534 if (xf
== NULL
) return;
550 switch (TREE_CODE(name
)) {
551 case IDENTIFIER_NODE
:
552 s
= IDENTIFIER_POINTER(name
);
558 gen_assign(xf
,TREE_OPERAND(name
,0));
559 gen_assign(xf
,TREE_OPERAND(name
,1));
565 gen_assign(xf
,TREE_OPERAND(name
,0));
568 gen_assign(xf
,TREE_OPERAND(name
,1));
575 fprintf(xref_file
,"ASG %s %d %s\n",filename(xf
),lineno
,s
);
583 /************************************************************************/
585 /* FIELD_xref_hier -- handle hierarchy */
587 /************************************************************************/
591 FIELD_xref_hier(cls
,base
,pub
,virt
,frnd
)
600 if (!doing_xref
) return;
601 xf
= find_file(input_filename
);
602 if (xf
== NULL
) return;
604 fprintf(xref_file
,"HIE %s %d %s %s %d %d %d\n",
605 filename(xf
),lineno
,cls
,base
,pub
,virt
,frnd
);
612 /************************************************************************/
614 /* FIELD_xref_member -- handle member */
616 /************************************************************************/
620 FIELD_xref_member(cls
,fld
)
629 char buf
[1024],bufa
[1024];
631 if (!doing_xref
) return;
632 xf
= find_file(fld
->decl
.filename
);
633 if (xf
== NULL
) return;
635 if (TREE_PRIVATE(fld
)) prot
= "PRIVATE";
636 else if (TREE_PROTECTED(fld
)) prot
= "PROTECTED";
637 else prot
= "PUBLIC";
640 if (TREE_CODE(fld
) == FUNCTION_DECL
&& DECL_CONST_MEMFUNC_P(fld
)) confg
= 1;
641 else if (TREE_CODE(fld
) == CONST_DECL
) confg
= 1;
644 if (TREE_CODE(fld
) == FUNCTION_DECL
&& DECL_ABSTRACT_VIRTUAL_P(fld
)) pure
= 1;
646 d
= IDENTIFIER_POINTER(cls
);
647 sprintf(buf
,"%d%s",strlen(d
),d
);
649 strcpy(bufa
,declname(fld
));
650 for (p
= &bufa
[1]; *p
!= 0; ++p
) {
651 if (p
[0] == '_' && p
[1] == '_' && p
[2] >= '0' && p
[2] <= '9') {
652 if (strncmp(&p
[2],buf
,i
) == 0) *p
= 0;
655 else if (p
[0] == '_' && p
[1] == '_' && p
[2] == 'C' && p
[3] >= '0' && p
[3] <= '9') {
656 if (strncmp(&p
[3],buf
,i
) == 0) *p
= 0;
661 fprintf(xref_file
,"MEM %s %d %s %s %s %d %d %d %d %d %d %d\n",
662 filename(xf
),fld
->decl
.linenum
,d
,
665 (TREE_CODE(fld
) == FUNCTION_DECL
? 0 : 1),
666 (TREE_INLINE(fld
) ? 1 : 0),
667 (DECL_FRIEND_P(fld
) ? 1 : 0),
668 (DECL_VIRTUAL_P(fld
) ? 1 : 0),
669 (TREE_STATIC(fld
) ? 1 : 0),
678 /************************************************************************/
680 /* find_file -- find file entry given name */
682 /************************************************************************/
692 for (xf
= all_files
; xf
!= NULL
; xf
= xf
->next
) {
693 if (STREQL(name
,xf
->name
)) break;
703 /************************************************************************/
705 /* filename -- return name for output */
707 /************************************************************************/
719 if (last_file
== xf
) return "*";
731 /************************************************************************/
733 /* fctname -- return name for function */
735 /************************************************************************/
742 extern char * declname();
743 static char fctbuf
[1024];
746 if (fct
== NULL
&& last_fct
== NULL
) return "*";
753 if (fct
== last_fct
) return "*";
758 s
= fixname(s
,fctbuf
);
768 /************************************************************************/
770 /* declname -- print name for declaration */
772 /************************************************************************/
779 if (DECL_NAME(dcl
) == NULL
) return "?";
781 return IDENTIFIER_POINTER (DECL_NAME (dcl
));
788 /************************************************************************/
790 /* simplify_type -- simplify a type string */
792 /************************************************************************/
803 while (i
> 0 && isspace(typ
[i
-1])) typ
[--i
] = 0;
805 if (i
> 7 && STREQL(&typ
[i
-5],"const")) {
810 if (typ
[i
-1] != ')') return;
815 if (*s
== ')') ++lvl
;
816 else if (*s
== '(') {
827 if (*s
!= 0 && s
[-1] == ')') {
830 if (*s
== '(') s
[2] = 0;
831 else if (*s
== ':') {
832 while (*s
!= '(') --s
;
843 /************************************************************************/
845 /* fixname -- fixup a function name (take care of embedded spaces */
847 /************************************************************************/
871 if (fg
== 0) return nam
;
880 /************************************************************************/
882 /* open_xref_file -- open file for xrefing */
884 /************************************************************************/
893 s
= rindex(file
,'/');
894 if (s
== NULL
) sprintf(xref_name
,".%s.gxref",file
);
897 strcpy(xref_name
,file
);
898 t
= rindex(xref_name
,'/');
905 xref_file
= fopen(xref_name
,"w");
907 if (xref_file
== NULL
) {
908 error("Can't create cross-reference file `%s'",xref_name
);
918 /* end of cplus-field.c */