1 /***************************************
4 C Cross Referencing & Documentation tool. Version 1.5.
6 Collects the typedef stuff.
7 ******************/ /******************
8 Written by Andrew M. Bishop
10 This file Copyright 1995,96,97,99 Andrew M. Bishop
11 It may be distributed under the GNU Public License, version 2, or
12 any higher version. See section COPYING of the GNU Public license
13 for conditions under which this file may be redistributed.
14 ***************************************/
16 /*+ Control the output of debugging information in this file. +*/
28 /*+ The file that is currently being processed. +*/
31 /*+ Whether we are parsing a typedef or not. +*/
32 extern int in_typedef
;
34 /*+ The defined types that we have seen. +*/
35 static StringList2 typedefs
=NULL
;
37 /*+ The current struct / union or enum definition. +*/
38 static StructUnion cur_su
=NULL
;
40 /*+ The current struct / union if seen in a typedef. +*/
41 static StructUnion cur_type_su
=NULL
;
43 /*+ The last typedef seen, used when two types share a typedef statement. +*/
44 static Typedef last_typedef
=NULL
;
46 /*+ The line number that a typedef or structure was seen on. +*/
47 static int type_lineno
=0;
49 static Typedef
NewTypedefType(char *name
,char *type
);
50 static StructUnion
NewStructUnionType(char *name
);
51 static void DeleteStructUnionType(StructUnion su
);
52 static StructUnion
CopyStructUnion(StructUnion su
);
55 /*++++++++++++++++++++++++++++++++++++++
56 Function that is called when a typedef is seen in the current file. The name of the typedef is stored for future reference.
58 char* name The name of the defined type.
60 int what_type Set to 1 for normal types or -1 for a function type (not pointer to function).
61 ++++++++++++++++++++++++++++++++++++++*/
63 void SeenTypedefName(char* name
,int what_type
)
66 printf("#Type.c# Type defined '%s'\n",name
);
70 typedefs
=NewStringList2();
71 AddToStringList2(typedefs
,name
,what_type
==0?"\0":what_type
>0?"n":"f",0,1);
75 /*++++++++++++++++++++++++++++++++++++++
76 Function that is called when an IDENTIFIER is seen in the current file, it may be a defined type.
78 int IsATypeName Returns 1 if the argument is a type that has been defined.
80 char* name The name of the possible defined type.
81 ++++++++++++++++++++++++++++++++++++++*/
83 int IsATypeName(char* name
)
88 for(i
=0;i
<typedefs
->n
;i
++)
89 if(!strcmp(name
,typedefs
->s1
[i
]))
90 return((int)*typedefs
->s2
[i
]);
96 /*++++++++++++++++++++++++++++++++++++++
97 Function that is called when the start of a struct or union or enum definition is seen.
99 char* name The name of the struct type.
100 ++++++++++++++++++++++++++++++++++++++*/
102 void SeenStructUnionStart(char* name
)
105 printf("#Type.c# Start Struct / Union '%s'\n",name
);
108 cur_su
=NewStructUnionType(name
);
111 cur_su
->comment
=MallocString(GetCurrentComment());
113 type_lineno
=parse_line
;
117 /*++++++++++++++++++++++++++++++++++++++
118 Function called when a component of a struct / union / enum is seen.
120 char* name The name of the struct / union / enum component.
122 int depth The depth within the nested struct / union / enum.
123 ++++++++++++++++++++++++++++++++++++++*/
125 void SeenStructUnionComp(char* name
,int depth
)
127 StructUnion s
,t
=cur_su
;
131 printf("#Type.c# Struct / Union Component (%d) '%s'\n",depth
,name
);
134 for(i
=1;i
<depth
;i
++,t
=s
)
135 s
=t
->comps
[t
->n_comp
-1];
137 if(t
->comps
&& strchr(name
,'{'))
139 char* ob
=strchr(name
,'{');
140 char* cb
=strchr(name
,'}'),*nb
;
142 while((nb
=strchr(cb
+1,'}'))) cb
=nb
;
144 if(strcmp(name
,"enum {") && strcmp(name
,"union {") && strcmp(name
,"struct {"))
146 Typedef typdef
=NewTypedefType(t
->comps
[t
->n_comp
-1]->name
,NULL
);
148 typdef
->comment
=MallocString(GetCurrentComment());
149 t
->comps
[t
->n_comp
-1]->comment
=MallocString(typdef
->comment
);
151 typdef
->sutype
=CopyStructUnion(t
->comps
[t
->n_comp
-1]);
153 AddToLinkedList(CurFile
->typedefs
,Typedef
,typdef
);
156 t
->comps
[t
->n_comp
-1]->comment
=MallocString(GetCurrentComment());
158 Free(t
->comps
[t
->n_comp
-1]->name
);
159 t
->comps
[t
->n_comp
-1]->name
=MallocString(ConcatStrings(3,name
,"...",cb
));
164 t
->comps
=(StructUnion
*)Malloc(8*sizeof(StructUnion
));
167 t
->comps
=(StructUnion
*)Realloc(t
->comps
,(t
->n_comp
+8)*sizeof(StructUnion
));
169 s
=NewStructUnionType(name
);
170 s
->comment
=MallocString(GetCurrentComment());
172 t
->comps
[t
->n_comp
++]=s
;
177 /*++++++++++++++++++++++++++++++++++++++
178 Function that is called when the end of a struct or union or enum definition is seen.
179 ++++++++++++++++++++++++++++++++++++++*/
181 void SeenStructUnionEnd(void)
184 printf("#Type.c# End Struct / Union\n");
191 Typedef xref
=CurFile
->typedefs
;
192 Typedef typdef
=NewTypedefType(cur_su
->name
,NULL
);
196 if(xref
->type
&& !strncmp(cur_su
->name
,xref
->type
,strlen(cur_su
->name
)))
197 xref
->typexref
=typdef
;
201 typdef
->comment
=cur_su
->comment
; cur_su
->comment
=NULL
;
202 typdef
->sutype
=cur_su
;
204 typdef
->lineno
=type_lineno
;
206 AddToLinkedList(CurFile
->typedefs
,Typedef
,typdef
);
213 /*++++++++++++++++++++++++++++++++++++++
214 Function that is called when a typedef is seen in the current file. This is recorded fully for later output.
216 char* name The name of the defined type.
218 char* type The type that it is defined to be.
219 ++++++++++++++++++++++++++++++++++++++*/
221 void SeenTypedef(char* name
,char* type
)
228 type_lineno
=parse_line
;
233 printf("#Type.c# Typedef '%s' '%s'\n",name
,type
);
236 typdef
=NewTypedefType(name
,type
);
238 typdef
->comment
=MallocString(GetCurrentComment());
242 Typedef xref
=CurFile
->typedefs
;
244 typdef
->typexref
=NULL
;
247 if(!strncmp(xref
->name
,typdef
->type
,strlen(xref
->name
)))
248 typdef
->typexref
=xref
;
251 if(!typdef
->typexref
)
252 typdef
->typexref
=last_typedef
;
256 typdef
->sutype
=cur_type_su
;
258 typdef
->typexref
=NULL
;
261 typdef
->lineno
=type_lineno
;
263 if(!typdef
->typexref
)
266 AddToLinkedList(CurFile
->typedefs
,Typedef
,typdef
);
270 /*++++++++++++++++++++++++++++++++++++++
271 Tidy up all of the local variables in case of a problem and abnormal parser termination.
272 ++++++++++++++++++++++++++++++++++++++*/
274 void ResetTypeAnalyser(void)
276 if(typedefs
) DeleteStringList2(typedefs
);
287 /*++++++++++++++++++++++++++++++++++++++
288 Create a new Typedef type.
290 Typedef NewTypedefType Returns the new type.
292 char *name The name of the type.
294 char *type The type of the type.
295 ++++++++++++++++++++++++++++++++++++++*/
297 static Typedef
NewTypedefType(char *name
,char *type
)
299 Typedef typed
=(Typedef
)Calloc(1,sizeof(struct _Typedef
)); /* clear unused pointers */
301 typed
->name
=MallocString(name
);
302 typed
->type
=MallocString(type
);
308 /*++++++++++++++++++++++++++++++++++++++
309 Delete the specified Typedef type.
311 Typedef type The Typedef type to be deleted.
312 ++++++++++++++++++++++++++++++++++++++*/
314 void DeleteTypedefType(Typedef type
)
316 if(type
->comment
) Free(type
->comment
);
317 if(type
->name
) Free(type
->name
);
318 if(type
->type
) Free(type
->type
);
319 if(type
->sutype
) DeleteStructUnionType(type
->sutype
);
324 /*++++++++++++++++++++++++++++++++++++++
325 Create a new struct / union type.
327 StructUnion NewStructUnionType Return the new StructUnion type.
329 char *name The name of the new struct / union.
330 ++++++++++++++++++++++++++++++++++++++*/
332 static StructUnion
NewStructUnionType(char *name
)
334 StructUnion su
=(StructUnion
)Calloc(1,sizeof(struct _StructUnion
));
336 su
->name
=MallocString(name
);
342 /*++++++++++++++++++++++++++++++++++++++
343 Free the memory associated with a Struct / Union structure.
345 StructUnion su The struct / union to delete.
347 This needs to call itself recursively.
348 ++++++++++++++++++++++++++++++++++++++*/
350 static void DeleteStructUnionType(StructUnion su
)
354 if(su
->name
) Free(su
->name
);
355 if(su
->comment
) Free(su
->comment
);
356 for(i
=0;i
<su
->n_comp
;i
++)
358 DeleteStructUnionType(su
->comps
[i
]);
359 if(su
->comps
) Free(su
->comps
);
364 /*++++++++++++++++++++++++++++++++++++++
365 Make a copy of the specified Struct / Union structure.
367 StructUnion CopyStructUnion Returns a malloced copy of the specified struct / union.
369 StructUnion su The struct / union to copy.
371 This needs to call itself recursively.
372 ++++++++++++++++++++++++++++++++++++++*/
374 static StructUnion
CopyStructUnion(StructUnion su
)
379 new=NewStructUnionType(su
->name
);
381 new->comment
=MallocString(su
->comment
);
382 new->n_comp
=su
->n_comp
;
385 new->comps
=(StructUnion
*)Malloc(su
->n_comp
*sizeof(StructUnion
));
386 for(i
=0;i
<su
->n_comp
;i
++)
387 new->comps
[i
]=CopyStructUnion(su
->comps
[i
]);