1 /***************************************
4 C Cross Referencing & Documentation tool. Version 1.5f.
7 ******************/ /******************
8 Written by Andrew M. Bishop
10 This file Copyright 1995,96,97,99,2001 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 debugging information from this file. +*/
28 /*+ The current parsing options. +*/
29 extern int option_xref
;
31 /*+ The current file that is being processed. +*/
34 /*+ When in a header file include functions from that file (except inline functions). +*/
37 /*+ The current function, this is initialised by the start of a possible declaration and maintained until all of the
38 arguments have been added and confirmation that it is a definition and not a prototype is seen. +*/
39 static Function cur_func
=NULL
;
41 /*+ The list of function prototypes and the files that they are defined in. +*/
42 static StringList2 prototypes
=NULL
;
44 static Function
NewFunctionType(char *name
,char *type
);
47 /*++++++++++++++++++++++++++++++++++++++
48 Function that is called when a function prototype is seen.
50 char* name The name of the function.
52 int in_a_function Whether the reference is from within a function or at the top level of the file.
53 ++++++++++++++++++++++++++++++++++++++*/
55 void SeenFunctionProto(char* name
,int in_a_function
)
57 if(!(option_xref
&XREF_FUNC
))
61 printf("#Func.c# Function prototype '%s'\n",name
);
67 prototypes
=NewStringList2();
68 AddToStringList2(prototypes
,name
,parse_file
,0,1);
71 AddToStringList(cur_func
->protos
,name
,0,1);
75 /*++++++++++++++++++++++++++++++++++++++
76 Function that is called when a function declaration is seen.
77 This may or may not be a function defintion, we will need to wait and see.
79 char* name The name of the function.
81 int scope The scope of the function definition.
82 ++++++++++++++++++++++++++++++++++++++*/
84 void SeenFunctionDeclaration(char* name
,int scope
)
87 printf("#Func.c# Function declaration for '%s()'\n",name
);
91 DeleteFunctionType(cur_func
);
93 cur_func
=NewFunctionType(name
,NULL
);
95 cur_func
->comment
=MallocString(GetCurrentComment());
96 cur_func
->scope
=scope
;
98 cur_func
->lineno
=parse_line
;
101 cur_func
->incfrom
=MallocString(parse_file
);
105 /*++++++++++++++++++++++++++++++++++++++
106 Called when a possible function definition is confirmed.
108 char* type The type of the function, or NULL at the end of a definition.
109 ++++++++++++++++++++++++++++++++++++++*/
111 void SeenFunctionDefinition(char* type
)
113 Function
*func
=&CurFile
->functions
;
116 if(cur_func
->scope
&INLINED
&& cur_func
->incfrom
)
120 printf("#Func.c# Function definition %s for '%s()'\n",type
?"start":"end",cur_func
->name
);
124 {cur_func
=NULL
;return;}
126 cur_func
->type
=MallocString(type
);
128 cur_func
->cret
=MallocString(SplitComment(&cur_func
->comment
,type
));
130 cur_func
->cret
=MallocString(GetCurrentComment());
132 if(option_xref
&XREF_FUNC
)
134 for(i
=0;i
<prototypes
->n
;i
++)
135 if(!strcmp(cur_func
->name
,prototypes
->s1
[i
]))
136 {cur_func
->protofile
=MallocString(prototypes
->s2
[i
]); break;}
138 for(i
=0;i
<cur_func
->args
->n
;i
++)
139 if(strcmp(cur_func
->args
->s1
[i
],"void") && strcmp(cur_func
->args
->s1
[i
],"...") && !strchr(cur_func
->args
->s1
[i
],' '))
141 char *old
=cur_func
->args
->s1
[i
];
142 cur_func
->args
->s1
[i
]=MallocString(ConcatStrings(2,"int ",old
));
143 cur_func
->args
->s2
[i
]=MallocString(SplitComment(&cur_func
->comment
,cur_func
->args
->s1
[i
]));
149 if(strcmp(cur_func
->name
,(*func
)->name
)<0)
163 /*++++++++++++++++++++++++++++++++++++++
164 Function that is called when a function argument is seen in the current function declaration.
166 char* name The name of the argument.
168 char* type The type of the argument, or NULL if a traditional style function definition.
169 ++++++++++++++++++++++++++++++++++++++*/
171 void SeenFunctionArg(char* name
,char *type
)
174 printf("#Func.c# Function arg %s '%s' in %s()\n",name
?name
:"K&R",type
?type
:"K&R",cur_func
->name
);
179 if(type
&& strcmp(type
,"void"))
183 for(i
=0;i
<cur_func
->args
->n
;i
++)
184 if(!strcmp(cur_func
->args
->s1
[i
],name
))
186 Free(cur_func
->args
->s1
[i
]);
187 cur_func
->args
->s1
[i
]=MallocString(type
);
188 cur_func
->args
->s2
[i
]=MallocString(SplitComment(&cur_func
->comment
,type
));
191 if(i
==cur_func
->args
->n
)
192 AddToStringList2(cur_func
->args
,type
,SplitComment(&cur_func
->comment
,type
),0,0);
194 if(!cur_func
->args
->s2
[i
])
195 cur_func
->args
->s2
[i
]=MallocString(GetCurrentComment());
198 AddToStringList2(cur_func
->args
,name
,NULL
,0,0);
203 /*++++++++++++++++++++++++++++++++++++++
204 Function that is called when a comment is seen, that may be in a function body.
206 int SeenFuncIntComment Returns a true value if the comment was accepted as an function internal comment.
208 char* comment The comment that has been seen.
209 ++++++++++++++++++++++++++++++++++++++*/
211 int SeenFuncIntComment(char* comment
)
213 if(!cur_func
|| !cur_func
->type
)
217 printf("#Func.c# Function internal comment '%s' in %s()\n",comment
,cur_func
->name
);
220 if(cur_func
->comment
)
222 char* c
=cur_func
->comment
;
224 cur_func
->comment
=MallocString(ConcatStrings(3,c
,"\n\n",comment
));
228 cur_func
->comment
=MallocString(comment
);
234 /*++++++++++++++++++++++++++++++++++++++
235 Function that is called when a function call is seen in the current function.
237 char* name The name of the function that is called.
238 ++++++++++++++++++++++++++++++++++++++*/
240 void SeenFunctionCall(char* name
)
242 if(!(option_xref
&XREF_FUNC
))
246 printf("#Func.c# Function call for '%s()' in %s()\n",name
,cur_func
->name
);
249 AddToStringList2(cur_func
->calls
,name
,NULL
,1,1);
253 /*++++++++++++++++++++++++++++++++++++++
254 Function that is called when a function or variable is referenced in the current function.
256 char* name The name of the function or variable that is referenced.
258 int in_a_function Whether the reference is from within a function or at the top level of the file.
259 ++++++++++++++++++++++++++++++++++++++*/
261 void CheckFunctionVariableRef(char* name
,int in_a_function
)
263 Variable var
=CurFile
->variables
;
264 Function func
=CurFile
->functions
;
267 if(!(option_xref
&(XREF_VAR
|XREF_FUNC
)))
270 if(IsAScopeVariable(name
))
274 printf("#Func.c# Function/Variable reference for '%s' in %s\n",name
,in_a_function
?cur_func
->name
:CurFile
->name
);
277 if(option_xref
&XREF_VAR
)
280 if(!strcmp(var
->name
,name
))
291 if(!sl
&& option_xref
&XREF_FUNC
)
294 if(!strcmp(func
->name
,name
))
305 if(!sl
&& option_xref
&XREF_FUNC
)
309 for(i
=0;i
<cur_func
->protos
->n
;i
++)
310 if(!strcmp(name
,cur_func
->protos
->s
[i
]))
316 if(!sl
&& prototypes
)
317 for(i
=0;i
<prototypes
->n
;i
++)
318 if(!strcmp(name
,prototypes
->s1
[i
]))
328 /* Now add the function or variable to the Function / File structure. */
331 AddToStringList2(sl
,name
,NULL
,1,1);
335 /*++++++++++++++++++++++++++++++++++++++
336 Tidy up all of the local variables in case of a problem and abnormal parser termination.
337 ++++++++++++++++++++++++++++++++++++++*/
339 void ResetFunctionAnalyser(void)
341 if(prototypes
) DeleteStringList2(prototypes
);
346 Function func
=CurFile
->functions
;
347 int delete_cur_func
=1;
358 DeleteFunctionType(cur_func
);
365 /*++++++++++++++++++++++++++++++++++++++
366 Create a new Function Type variable.
368 Function NewFunctionType Returns the new Function variable.
370 char *name The name of the function.
372 char *type The type of the function.
373 ++++++++++++++++++++++++++++++++++++++*/
375 static Function
NewFunctionType(char *name
,char *type
)
377 Function func
=(Function
)Calloc(1,sizeof(struct _Function
)); /* clear unused pointers */
379 func
->name
=MallocString(name
);
380 func
->type
=MallocString(type
);
381 func
->args
=NewStringList2();
382 func
->protos
=NewStringList();
383 func
->calls
=NewStringList2();
384 func
->called
=NewStringList2();
385 func
->used
=NewStringList2();
386 func
->v_refs
=NewStringList2();
387 func
->f_refs
=NewStringList2();
393 /*++++++++++++++++++++++++++++++++++++++
394 Delete the specified Function type.
396 Function func The Function type to be deleted.
397 ++++++++++++++++++++++++++++++++++++++*/
399 void DeleteFunctionType(Function func
)
401 if(func
->comment
) Free(func
->comment
);
402 if(func
->name
) Free(func
->name
);
403 if(func
->type
) Free(func
->type
);
404 if(func
->cret
) Free(func
->cret
);
405 if(func
->protofile
) Free(func
->protofile
);
406 if(func
->incfrom
) Free(func
->incfrom
);
407 if(func
->args
) DeleteStringList2(func
->args
);
408 if(func
->protos
) DeleteStringList(func
->protos
);
409 if(func
->calls
) DeleteStringList2(func
->calls
);
410 if(func
->called
) DeleteStringList2(func
->called
);
411 if(func
->used
) DeleteStringList2(func
->used
);
412 if(func
->v_refs
) DeleteStringList2(func
->v_refs
);
413 if(func
->f_refs
) DeleteStringList2(func
->f_refs
);