File: type.c

package info (click to toggle)
cxref 1.6e-3
  • links: PTS
  • area: main
  • in suites: buster
  • size: 2,996 kB
  • sloc: ansic: 16,598; yacc: 2,091; sh: 937; lex: 470; perl: 452; makefile: 433; lisp: 256; cpp: 188; python: 80
file content (395 lines) | stat: -rw-r--r-- 9,584 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/***************************************
  C Cross Referencing & Documentation tool. Version 1.6e.

  Collects the typedef stuff.
  ******************/ /******************
  Written by Andrew M. Bishop

  This file Copyright 1995-2013 Andrew M. Bishop
  It may be distributed under the GNU Public License, version 2, or
  any higher version.  See section COPYING of the GNU Public license
  for conditions under which this file may be redistributed.
  ***************************************/

/*+ Control the output of debugging information in this file. +*/
#define DEBUG 0

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "memory.h"
#include "datatype.h"
#include "parse-yy.h"
#include "cxref.h"

/*+ The file that is currently being processed. +*/
extern File CurFile;

/*+ Whether we are parsing a typedef or not. +*/
extern int in_typedef;

/*+ The defined types that we have seen. +*/
static StringList2 typedefs=NULL;

/*+ The current struct / union or enum definition. +*/
static StructUnion cur_su=NULL;

/*+ The current struct / union if seen in a typedef. +*/
static StructUnion cur_type_su=NULL;

/*+ The last typedef seen, used when two types share a typedef statement. +*/
static Typedef last_typedef=NULL;

/*+ The line number that a typedef or structure was seen on. +*/
static int type_lineno=0;

static Typedef NewTypedefType(char *name,char *type);
static StructUnion NewStructUnionType(char *name);
static void DeleteStructUnionType(StructUnion su);
static StructUnion CopyStructUnion(StructUnion su);


/*++++++++++++++++++++++++++++++++++++++
  Function that is called when a typedef is seen in the current file. The name of the typedef is stored for future reference.

  char* name The name of the defined type.

  int what_type Set to 1 for normal types or -1 for a function type (not pointer to function).
  ++++++++++++++++++++++++++++++++++++++*/

void SeenTypedefName(char* name,int what_type)
{
#if DEBUG
 printf("#Type.c# Type defined '%s'\n",name);
#endif

 if(!typedefs)
    typedefs=NewStringList2();
 AddToStringList2(typedefs,name,what_type==0?"\0":what_type>0?"n":"f",0,1);
}


/*++++++++++++++++++++++++++++++++++++++
  Function that is called when an IDENTIFIER is seen in the current file, it may be a defined type.

  int IsATypeName Returns 1 if the argument is a type that has been defined.

  char* name The name of the possible defined type.
  ++++++++++++++++++++++++++++++++++++++*/

int IsATypeName(char* name)
{
 int i;

 if(typedefs)
    for(i=0;i<typedefs->n;i++)
       if(!strcmp(name,typedefs->s1[i]))
          return((int)*typedefs->s2[i]);

 return(0);
}


/*++++++++++++++++++++++++++++++++++++++
  Function that is called when the start of a struct or union or enum definition is seen.

  char* name The name of the struct type.
  ++++++++++++++++++++++++++++++++++++++*/

void SeenStructUnionStart(char* name)
{
#if DEBUG
 printf("#Type.c# Start Struct / Union '%s'\n",name);
#endif

 if(cur_su)
    DeleteStructUnionType(cur_su);

 cur_su=NewStructUnionType(name);

 if(!in_typedef)
    cur_su->comment=MallocString(GetCurrentComment());

 type_lineno=parse_line;
}


/*++++++++++++++++++++++++++++++++++++++
  Function called when a component of a struct / union / enum is seen.

  char* name The name of the struct / union / enum component.

  int depth The depth within the nested struct / union / enum.
  ++++++++++++++++++++++++++++++++++++++*/

void SeenStructUnionComp(char* name,int depth)
{
 StructUnion s,t=cur_su;
 int i;

#if DEBUG
 printf("#Type.c# Struct / Union Component (%d) '%s'\n",depth,name);
#endif

 for(i=1;i<depth;i++,t=s)
    s=t->comps[t->n_comp-1];

 if(t->comps && strchr(name,'{'))
   {
    char* ob=strchr(name,'{');
    char* cb=strchr(name,'}'),*nb;

    while((nb=strchr(cb+1,'}'))) cb=nb;
    ob[1]=0;
    if(strcmp(name,"enum {") && strcmp(name,"union {") && strcmp(name,"struct {"))
      {
       Typedef typdef=NewTypedefType(t->comps[t->n_comp-1]->name,NULL);

       typdef->comment=MallocString(GetCurrentComment());
       t->comps[t->n_comp-1]->comment=MallocString(typdef->comment);

       typdef->sutype=CopyStructUnion(t->comps[t->n_comp-1]);

       typdef->lineno=parse_line;

       AddToLinkedList(CurFile->typedefs,Typedef,typdef);
      }
    else
       t->comps[t->n_comp-1]->comment=MallocString(GetCurrentComment());

    Free(t->comps[t->n_comp-1]->name);
    t->comps[t->n_comp-1]->name=MallocString(ConcatStrings(3,name,"...",cb));
   }
 else
   {
    if(!t->comps)
       t->comps=(StructUnion*)Malloc(8*sizeof(StructUnion));
    else
       if(t->n_comp%8==0)
          t->comps=(StructUnion*)Realloc(t->comps,(t->n_comp+8)*sizeof(StructUnion));

    s=NewStructUnionType(name);
    s->comment=MallocString(GetCurrentComment());

    t->comps[t->n_comp++]=s;
   }
}


/*++++++++++++++++++++++++++++++++++++++
  Function that is called when the end of a struct or union or enum definition is seen.
  ++++++++++++++++++++++++++++++++++++++*/

void SeenStructUnionEnd(void)
{
#if DEBUG
 printf("#Type.c# End Struct / Union\n");
#endif

 if(in_typedef)
    cur_type_su=cur_su;
 else
   {
    Typedef xref=CurFile->typedefs;
    Typedef typdef=NewTypedefType(cur_su->name,NULL);

    while(xref)
      {
       if(xref->type && !strncmp(cur_su->name,xref->type,strlen(cur_su->name)))
          xref->typexref=typdef;
       xref=xref->next;
      }

    typdef->comment=cur_su->comment; cur_su->comment=NULL;
    typdef->sutype=cur_su;

    typdef->lineno=type_lineno;

    AddToLinkedList(CurFile->typedefs,Typedef,typdef);
   }

 cur_su=NULL;
}


/*++++++++++++++++++++++++++++++++++++++
  Function that is called when a typedef is seen in the current file. This is recorded fully for later output.

  char* name The name of the defined type.

  char* type The type that it is defined to be.
  ++++++++++++++++++++++++++++++++++++++*/

void SeenTypedef(char* name,char* type)
{
 Typedef typdef;

 if(!name)
   {
    last_typedef=NULL;
    type_lineno=parse_line;
    return;
   }

#if DEBUG
 printf("#Type.c# Typedef '%s' '%s'\n",name,type);
#endif

 typdef=NewTypedefType(name,type);

 typdef->comment=MallocString(GetCurrentComment());

 if(!cur_type_su)
   {
    Typedef xref=CurFile->typedefs;
    typdef->sutype=NULL;
    typdef->typexref=NULL;
    while(xref)
      {
       if(!strncmp(xref->name,typdef->type,strlen(xref->name)))
          typdef->typexref=xref;
       xref=xref->next;
      }
    if(!typdef->typexref)
       typdef->typexref=last_typedef;
   }
 else
   {
    typdef->sutype=cur_type_su;
    cur_type_su=NULL;
    typdef->typexref=NULL;
   }

 typdef->lineno=type_lineno;

 if(!typdef->typexref)
    last_typedef=typdef;

 AddToLinkedList(CurFile->typedefs,Typedef,typdef);
}


/*++++++++++++++++++++++++++++++++++++++
  Tidy up all of the local variables in case of a problem and abnormal parser termination.
  ++++++++++++++++++++++++++++++++++++++*/

void ResetTypeAnalyser(void)
{
 if(typedefs) DeleteStringList2(typedefs);
 typedefs=NULL;

 if(cur_su) DeleteStructUnionType(cur_su);
 cur_su=NULL;

 cur_type_su=NULL;

 last_typedef=NULL;
}


/*++++++++++++++++++++++++++++++++++++++
  Create a new Typedef type.

  Typedef NewTypedefType Returns the new type.

  char *name The name of the type.

  char *type The type of the type.
  ++++++++++++++++++++++++++++++++++++++*/

static Typedef NewTypedefType(char *name,char *type)
{
 Typedef typed=(Typedef)Calloc(1,sizeof(struct _Typedef)); /* clear unused pointers */

 typed->name=MallocString(name);
 typed->type=MallocString(type);

 return(typed);
}


/*++++++++++++++++++++++++++++++++++++++
  Delete the specified Typedef type.

  Typedef type The Typedef type to be deleted.
  ++++++++++++++++++++++++++++++++++++++*/

void DeleteTypedefType(Typedef type)
{
 if(type->comment) Free(type->comment);
 if(type->name)    Free(type->name);
 if(type->type)    Free(type->type);
 if(type->sutype)  DeleteStructUnionType(type->sutype);
 Free(type);
}


/*++++++++++++++++++++++++++++++++++++++
  Create a new struct / union type.

  StructUnion NewStructUnionType Return the new StructUnion type.

  char *name The name of the new struct / union.
  ++++++++++++++++++++++++++++++++++++++*/

static StructUnion NewStructUnionType(char *name)
{
 StructUnion su=(StructUnion)Calloc(1,sizeof(struct _StructUnion));

 su->name=MallocString(name);

 return(su);
}


/*++++++++++++++++++++++++++++++++++++++
  Free the memory associated with a Struct / Union structure.

  StructUnion su The struct / union to delete.

  This needs to call itself recursively.
  ++++++++++++++++++++++++++++++++++++++*/

static void DeleteStructUnionType(StructUnion su)
{
 int i;

 if(su->name)    Free(su->name);
 if(su->comment) Free(su->comment);
 for(i=0;i<su->n_comp;i++)
    if(su->comps[i])
       DeleteStructUnionType(su->comps[i]);
 if(su->comps)   Free(su->comps);
 Free(su);
}


/*++++++++++++++++++++++++++++++++++++++
  Make a copy of the specified Struct / Union structure.

  StructUnion CopyStructUnion Returns a malloced copy of the specified struct / union.

  StructUnion su The struct / union to copy.

  This needs to call itself recursively.
  ++++++++++++++++++++++++++++++++++++++*/

static StructUnion CopyStructUnion(StructUnion su)
{
 StructUnion new;
 int i;

 new=NewStructUnionType(su->name);

 new->comment=MallocString(su->comment);
 new->n_comp=su->n_comp;
 if(su->n_comp)
   {
    new->comps=(StructUnion*)Malloc(su->n_comp*sizeof(StructUnion));
    for(i=0;i<su->n_comp;i++)
       new->comps[i]=CopyStructUnion(su->comps[i]);
   }

 return(new);
}