File: func.c

package info (click to toggle)
cxref 1.5c-9
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,504 kB
  • ctags: 1,359
  • sloc: ansic: 16,721; yacc: 1,930; sh: 722; lex: 416; makefile: 345; lisp: 256; python: 80; perl: 78
file content (415 lines) | stat: -rw-r--r-- 10,803 bytes parent folder | download
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/***************************************
  $Header: /home/amb/cxref/RCS/func.c 1.19 2001/01/06 13:05:12 amb Exp $

  C Cross Referencing & Documentation tool. Version 1.5c.

  Handle Function stuff.
  ******************/ /******************
  Written by Andrew M. Bishop

  This file Copyright 1995,96,97,99,2001 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 debugging information from 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 current parsing options. +*/
extern int option_xref;

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

/*+ When in a header file include functions from that file (except inline functions). +*/
extern int in_header;

/*+ The current function, this is initialised by the start of a possible declaration and maintained until all of the
    arguments have been added and confirmation that it is a definition and not a prototype is seen. +*/
static Function cur_func=NULL;

/*+ The list of function prototypes and the files that they are defined in. +*/
static StringList2 prototypes=NULL;

static Function NewFunctionType(char *name,char *type);


/*++++++++++++++++++++++++++++++++++++++
  Function that is called when a function prototype is seen.

  char* name The name of the function.

  int in_a_function Whether the reference is from within a function or at the top level of the file.
  ++++++++++++++++++++++++++++++++++++++*/

void SeenFunctionProto(char* name,int in_a_function)
{
 if(!(option_xref&XREF_FUNC))
    return;

#if DEBUG
 printf("#Func.c# Function prototype '%s'\n",name);
#endif

 if(!in_a_function)
   {
    if(!prototypes)
       prototypes=NewStringList2();
    AddToStringList2(prototypes,name,parse_file,0,1);
   }
 else
    AddToStringList(cur_func->protos,name,0,1);
}


/*++++++++++++++++++++++++++++++++++++++
  Function that is called when a function declaration is seen.
  This may or may not be a function defintion, we will need to wait and see.

  char* name The name of the function.

  int scope The scope of the function definition.
  ++++++++++++++++++++++++++++++++++++++*/

void SeenFunctionDeclaration(char* name,int scope)
{
#if DEBUG
 printf("#Func.c# Function declaration for '%s()'\n",name);
#endif

 if(cur_func)
    DeleteFunctionType(cur_func);

 cur_func=NewFunctionType(name,NULL);

 cur_func->comment=MallocString(GetCurrentComment());
 cur_func->scope=scope;

 cur_func->lineno=parse_line;

 if(in_header)
    cur_func->incfrom=MallocString(parse_file);
}


/*++++++++++++++++++++++++++++++++++++++
  Called when a possible function definition is confirmed.

  char* type The type of the function, or NULL at the end of a definition.
  ++++++++++++++++++++++++++++++++++++++*/

void SeenFunctionDefinition(char* type)
{
 Function *func=&CurFile->functions;
 int i;

 if(cur_func->scope&INLINED && cur_func->incfrom)
    return;

#if DEBUG
 printf("#Func.c# Function definition %s for '%s()'\n",type?"start":"end",cur_func->name);
#endif

 if(!type)
   {cur_func=NULL;return;}

 cur_func->type=MallocString(type);

 cur_func->cret=MallocString(SplitComment(&cur_func->comment,type));
 if(!cur_func->cret)
    cur_func->cret=MallocString(GetCurrentComment());

 if(option_xref&XREF_FUNC)
    if(prototypes)
       for(i=0;i<prototypes->n;i++)
          if(!strcmp(cur_func->name,prototypes->s1[i]))
            {cur_func->protofile=MallocString(prototypes->s2[i]); break;}

 for(i=0;i<cur_func->args->n;i++)
    if(strcmp(cur_func->args->s1[i],"void") && strcmp(cur_func->args->s1[i],"...") && !strchr(cur_func->args->s1[i],' '))
      {
       char *old=cur_func->args->s1[i];
       cur_func->args->s1[i]=MallocString(ConcatStrings(2,"int ",old));
       cur_func->args->s2[i]=MallocString(SplitComment(&cur_func->comment,cur_func->args->s1[i]));
       Free(old);
      }

 while(*func)
   {
    if(strcmp(cur_func->name,(*func)->name)<0)
      {
       Function temp=*func;
       *func=cur_func;
       cur_func->next=temp;
       break;
      }
    func=&(*func)->next;
   }

 if(!cur_func->next)
    *func=cur_func;
}

/*++++++++++++++++++++++++++++++++++++++
  Function that is called when a function argument is seen in the current function declaration.

  char* name The name of the argument.

  char* type The type of the argument, or NULL if a traditional style function definition.
  ++++++++++++++++++++++++++++++++++++++*/

void SeenFunctionArg(char* name,char *type)
{
#if DEBUG
 printf("#Func.c# Function arg %s '%s' in %s()\n",name?name:"K&R",type?type:"K&R",cur_func->name);
#endif

 if(name)
   {
    if(type)
      {
       int i;

       for(i=0;i<cur_func->args->n;i++)
          if(!strcmp(cur_func->args->s1[i],name))
            {
             Free(cur_func->args->s1[i]);
             cur_func->args->s1[i]=MallocString(type);
             cur_func->args->s2[i]=MallocString(SplitComment(&cur_func->comment,type));
             break;
            }
       if(i==cur_func->args->n)
          AddToStringList2(cur_func->args,type,SplitComment(&cur_func->comment,type),0,0);

       if(!cur_func->args->s2[i])
          cur_func->args->s2[i]=MallocString(GetCurrentComment());
      }
    else
       AddToStringList2(cur_func->args,name,NULL,0,0);
   }
}


/*++++++++++++++++++++++++++++++++++++++
  Function that is called when a comment is seen, that may be in a function body.

  int SeenFuncIntComment Returns a true value if the comment was accepted as an function internal comment.

  char* comment The comment that has been seen.
  ++++++++++++++++++++++++++++++++++++++*/

int SeenFuncIntComment(char* comment)
{
 if(!cur_func || !cur_func->type)
    return(0);

#if DEBUG
 printf("#Func.c# Function internal comment '%s' in %s()\n",comment,cur_func->name);
#endif

 if(cur_func->comment)
   {
    char* c=cur_func->comment;

    cur_func->comment=MallocString(ConcatStrings(3,c,"\n\n",comment));
    Free(c);
   }
 else
    cur_func->comment=MallocString(comment);

 return(1);
}


/*++++++++++++++++++++++++++++++++++++++
  Function that is called when a function call is seen in the current function.

  char* name The name of the function that is called.
  ++++++++++++++++++++++++++++++++++++++*/

void SeenFunctionCall(char* name)
{
 if(!(option_xref&XREF_FUNC))
    return;

#if DEBUG
 printf("#Func.c# Function call for '%s()' in %s()\n",name,cur_func->name);
#endif

 AddToStringList2(cur_func->calls,name,NULL,1,1);
}


/*++++++++++++++++++++++++++++++++++++++
  Function that is called when a function or variable is referenced in the current function.

  char* name The name of the function or variable that is referenced.

  int in_a_function Whether the reference is from within a function or at the top level of the file.
  ++++++++++++++++++++++++++++++++++++++*/

void CheckFunctionVariableRef(char* name,int in_a_function)
{
 Variable var =CurFile->variables;
 Function func=CurFile->functions;
 StringList2 sl=NULL;

 if(!(option_xref&(XREF_VAR|XREF_FUNC)))
    return;

 if(IsAScopeVariable(name))
    return;

#if DEBUG
 printf("#Func.c# Function/Variable reference for '%s' in %s\n",name,in_a_function?cur_func->name:CurFile->name);
#endif

 if(option_xref&XREF_VAR)
    while(var)
      {
       if(!strcmp(var->name,name))
         {
          if(in_a_function)
             sl=cur_func->v_refs;
          else
             sl=CurFile->v_refs;
          break;
         }
       var=var->next;
      }

 if(!sl && option_xref&XREF_FUNC)
    while(func)
      {
       if(!strcmp(func->name,name))
         {
          if(in_a_function)
             sl=cur_func->f_refs;
          else
             sl=CurFile->f_refs;
          break;
         }
       func=func->next;
      }

 if(!sl && option_xref&XREF_FUNC)
   {
    int i;
    if(in_a_function)
       for(i=0;i<cur_func->protos->n;i++)
          if(!strcmp(name,cur_func->protos->s[i]))
            {
             sl=cur_func->f_refs;
             break;
            }

    if(!sl && prototypes)
       for(i=0;i<prototypes->n;i++)
          if(!strcmp(name,prototypes->s1[i]))
            {
             if(in_a_function)
                sl=cur_func->f_refs;
             else
                sl=CurFile->f_refs;
             break;
            }
   }

 /* Now add the function or variable to the Function / File structure. */

 if(sl)
    AddToStringList2(sl,name,NULL,1,1);
}


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

void ResetFunctionAnalyser(void)
{
 if(prototypes) DeleteStringList2(prototypes);
 prototypes=NULL;

 if(cur_func)
   {
    Function func=CurFile->functions;
    int delete_cur_func=1;

    while(func)
      {
       if(func==cur_func)
          delete_cur_func=0;

       func=func->next;
      }

    if(delete_cur_func)
       DeleteFunctionType(cur_func);

    cur_func=NULL;
   }
}


/*++++++++++++++++++++++++++++++++++++++
  Create a new Function Type variable.

  Function NewFunctionType Returns the new Function variable.

  char *name The name of the function.

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

static Function NewFunctionType(char *name,char *type)
{
 Function func=(Function)Calloc(1,sizeof(struct _Function)); /* clear unused pointers */

 func->name  =MallocString(name);
 func->type  =MallocString(type);
 func->args  =NewStringList2();
 func->protos=NewStringList();
 func->calls =NewStringList2();
 func->called=NewStringList2();
 func->used  =NewStringList2();
 func->v_refs=NewStringList2();
 func->f_refs=NewStringList2();

 return(func);
}


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

  Function func The Function type to be deleted.
  ++++++++++++++++++++++++++++++++++++++*/

void DeleteFunctionType(Function func)
{
 if(func->comment)   Free(func->comment);
 if(func->name)      Free(func->name);
 if(func->type)      Free(func->type);
 if(func->cret)      Free(func->cret);
 if(func->protofile) Free(func->protofile);
 if(func->incfrom)   Free(func->incfrom);
 if(func->args)      DeleteStringList2(func->args);
 if(func->protos)    DeleteStringList(func->protos);
 if(func->calls)     DeleteStringList2(func->calls);
 if(func->called)    DeleteStringList2(func->called);
 if(func->used)      DeleteStringList2(func->used);
 if(func->v_refs)    DeleteStringList2(func->v_refs);
 if(func->f_refs)    DeleteStringList2(func->f_refs);
 Free(func);
}