1 /***************************************
4 C Cross Referencing & Documentation tool. Version 1.5f.
6 Writes the raw information and / or warnings out.
7 ******************/ /******************
8 Written by Andrew M. Bishop
10 This file Copyright 1995,96,97,99,2001,04 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 ***************************************/
24 static void WriteWarnRawFilePart(File file
);
25 static void WriteWarnRawInclude(Include inc
);
26 static void WriteWarnRawSubInclude(Include inc
,int depth
);
27 static void WriteWarnRawDefine(Define def
);
28 static void WriteWarnRawTypedef(Typedef type
);
29 static void WriteWarnRawStructUnion(StructUnion su
, int depth
,StructUnion base
);
30 static void WriteWarnRawVariable(Variable var
);
31 static void WriteWarnRawFunction(Function func
);
33 /*+ Output option. +*/
34 extern int option_warn
,option_raw
,option_xref
,option_index
;
36 /*+ The name of the current file. +*/
37 static char* filename
=NULL
;
39 /*++++++++++++++++++++++++++++++++++++++
40 Write the raw / warning output for a complete File structure and all components.
42 File file The File structure to output.
43 ++++++++++++++++++++++++++++++++++++++*/
45 void WriteWarnRawFile(File file
)
47 Include inc
=file
->includes
;
48 Define def
=file
->defines
;
49 Typedef type
=file
->typedefs
;
50 Variable var
=file
->variables
;
51 Function func
=file
->functions
;
55 /*+ The file structure is broken into its components and they are each written out. +*/
58 printf("----------------------------------------\n");
60 WriteWarnRawFilePart(file
);
64 WriteWarnRawInclude(inc
);
70 WriteWarnRawDefine(def
);
76 WriteWarnRawTypedef(type
);
82 WriteWarnRawVariable(var
);
88 WriteWarnRawFunction(func
);
93 printf("----------------------------------------\n\n");
97 /*++++++++++++++++++++++++++++++++++++++
98 Write a File structure out.
100 File file The File structure to output.
101 ++++++++++++++++++++++++++++++++++++++*/
103 static void WriteWarnRawFilePart(File file
)
108 printf("FILE : '%s'\n",file
->name
);
110 if(file
->comment
&& option_raw
)
111 printf("<<<\n%s\n>>>\n",file
->comment
);
113 if(option_warn
&WARN_COMMENT
&& !file
->comment
)
114 printf("Warning %16s : File does not have a comment.\n",filename
);
116 if(option_xref
&XREF_FILE
)
119 for(i
=0;i
<file
->inc_in
->n
;i
++)
120 printf("Included in %s\n",file
->inc_in
->s
[i
]);
122 if(option_warn
&WARN_XREF
)
124 int len
=strlen(file
->name
)-2;
125 if(!file
->inc_in
->n
&& !strcmp(&file
->name
[len
],".h"))
126 printf("Warning %16s : Header file '%s' is not included in any files.\n",filename
,file
->name
);
127 if( file
->inc_in
->n
&& !strcmp(&file
->name
[len
],".c"))
128 printf("Warning %16s : Source file '%s' is included in another file.\n",filename
,file
->name
);
132 if(option_xref
&XREF_FUNC
)
133 for(i
=0;i
<file
->f_refs
->n
;i
++)
137 if(file
->f_refs
->s2
[i
])
138 printf("References Function %s : %s\n",file
->f_refs
->s1
[i
],file
->f_refs
->s2
[i
]);
140 printf("References Function %s\n",file
->f_refs
->s1
[i
]);
142 if(option_warn
&WARN_XREF
&& !file
->f_refs
->s2
[i
])
143 printf("Warning %16s : File references function '%s()' whose definition is unknown.\n",filename
,file
->f_refs
->s1
[i
]);
146 if(option_xref
&XREF_VAR
)
147 for(i
=0;i
<file
->v_refs
->n
;i
++)
151 if(file
->v_refs
->s2
[i
])
152 printf("References Variable %s : %s\n",file
->v_refs
->s1
[i
],file
->v_refs
->s2
[i
]);
154 printf("References Variable %s\n",file
->v_refs
->s1
[i
]);
156 if(option_warn
&WARN_XREF
&& !file
->v_refs
->s2
[i
])
157 printf("Warning %16s : File references variable '%s' whose definition is unknown.\n",filename
,file
->v_refs
->s1
[i
]);
162 /*++++++++++++++++++++++++++++++++++++++
163 Write an Include structure out.
165 Include inc The Include structure to output.
166 ++++++++++++++++++++++++++++++++++++++*/
168 static void WriteWarnRawInclude(Include inc
)
171 printf("\nINCLUDES : '%s' [%s file]\n",inc
->name
,(inc
->scope
==GLOBAL
?"System":"Local"));
173 if(inc
->comment
&& option_raw
)
174 printf("<<<\n%s\n>>>\n",inc
->comment
);
175 if(option_warn
&WARN_COMMENT
&& !inc
->comment
)
176 printf("Warning %16s : #Include '%s' does not have a comment.\n",filename
,inc
->name
);
178 if(option_raw
&& inc
->includes
)
179 WriteWarnRawSubInclude(inc
->includes
,1);
183 /*++++++++++++++++++++++++++++++++++++++
184 Write an Sub-Include structure out.
186 Include inc The Include structure to output.
188 int depth The depth of the include hierarchy.
189 ++++++++++++++++++++++++++++++++++++++*/
191 static void WriteWarnRawSubInclude(Include inc
,int depth
)
197 for(i
=0;i
<depth
;i
++) printf(" ");
198 printf("INCLUDES : '%s' [%s file]\n",inc
->name
,(inc
->scope
==GLOBAL
?"System":"Local"));
201 WriteWarnRawSubInclude(inc
->includes
,depth
+1);
208 /*++++++++++++++++++++++++++++++++++++++
209 Write a Define structure out.
211 Define def The Define structure to output.
212 ++++++++++++++++++++++++++++++++++++++*/
214 static void WriteWarnRawDefine(Define def
)
220 printf("\nDEFINES : '%s' ",def
->name
);
223 printf("= %s",def
->value
);
228 for(i
=0;i
<def
->args
->n
;i
++)
229 printf(i
?",%s":"%s",def
->args
->s1
[i
]);
236 if(def
->comment
&& option_raw
)
237 printf("<<<\n%s\n>>>\n",def
->comment
);
238 if(option_warn
&WARN_COMMENT
&& !def
->comment
)
239 printf("Warning %16s : #Define '%s' does not have a comment.\n",filename
,def
->name
);
242 printf("Defined: %s:%d\n",filename
,def
->lineno
);
244 for(i
=0;i
<def
->args
->n
;i
++)
249 printf("Arguments: %s <<<%s>>>\n",def
->args
->s1
[i
],def
->args
->s2
[i
]);
251 printf("Arguments: %s\n",def
->args
->s1
[i
]);
253 if(option_warn
&WARN_COMMENT
&& !def
->args
->s2
[i
])
254 printf("Warning %16s : #Define '%s' has an argument '%s' with no comment.\n",filename
,def
->name
,def
->args
->s1
[i
]);
259 /*++++++++++++++++++++++++++++++++++++++
260 Write a Typedef structure out.
262 Typedef type The Typedef structure to output.
263 ++++++++++++++++++++++++++++++++++++++*/
265 static void WriteWarnRawTypedef(Typedef type
)
270 printf("\nTYPEDEF : '%s'\n",type
->name
);
272 printf("\nTYPE : '%s'\n",type
->name
);
275 if(type
->comment
&& option_raw
)
276 printf("<<<\n%s\n>>>\n",type
->comment
);
277 if(option_warn
&WARN_COMMENT
&& !type
->comment
)
278 printf("Warning %16s : Type '%s' does not have a comment.\n",filename
,type
->name
);
281 printf("Defined: %s:%d\n",filename
,type
->lineno
);
285 printf("Type: %s\n",type
->type
);
289 printf("See: %s %s\n",type
->typexref
->type
?"Typedef":"Type",type
->typexref
->name
);
292 WriteWarnRawStructUnion(type
->sutype
,0,type
->sutype
);
296 /*++++++++++++++++++++++++++++++++++++++
297 Write a structure / union / enum out.
299 StructUnion su The structure / union / enum to write.
301 int depth The depth within the structure.
303 StructUnion base The base struct union that this one is part of.
304 ++++++++++++++++++++++++++++++++++++++*/
306 static void WriteWarnRawStructUnion(StructUnion su
, int depth
,StructUnion base
)
311 if(option_warn
&WARN_COMMENT
&& depth
&& !su
->comment
)
312 printf("Warning %16s : Struct/Union component '%s' in '%s' does not have a comment.\n",filename
,su
->name
,base
->name
);
314 splitsu
=strstr(su
->name
,"{...}");
315 if(splitsu
) splitsu
[-1]=0;
319 for(i
=0;i
<depth
;i
++) printf(" ");
320 if(depth
&& su
->comment
&& !su
->comps
)
321 printf("%s; <<<%s>>>\n",su
->name
,su
->comment
);
322 else if(!depth
|| su
->comps
)
323 printf("%s\n",su
->name
);
325 printf("%s;\n",su
->name
);
328 if(!depth
|| su
->comps
)
332 for(i
=0;i
<depth
;i
++) printf(" ");
335 for(i
=0;i
<su
->n_comp
;i
++)
336 WriteWarnRawStructUnion(su
->comps
[i
],depth
+1,base
);
339 for(i
=0;i
<depth
;i
++) printf(" ");
343 for(i
=0;i
<depth
;i
++) printf(" ");
344 if(depth
&& su
->comment
)
345 printf("%s; <<<%s>>>\n",splitsu
[5]?&splitsu
[6]:"",su
->comment
);
347 printf("%s;\n",splitsu
[5]?&splitsu
[6]:"");
352 if(splitsu
) splitsu
[-1]=' ';
356 /*++++++++++++++++++++++++++++++++++++++
357 Write a Variable structure out.
359 Variable var The Variable structure to output.
360 ++++++++++++++++++++++++++++++++++++++*/
362 static void WriteWarnRawVariable(Variable var
)
370 printf("\nVARIABLE : %s [",var
->name
);
371 if(var
->scope
&LOCAL
) done
=printf("Local");
372 if(var
->scope
&GLOBAL
) done
=printf("Global");
373 if(var
->scope
&EXTERNAL
) done
=printf("%sExternal",done
?" and ":"");
374 if(var
->scope
&EXTERN_H
) done
=printf("%sExternal from header file",done
?" and ":"");
375 if(var
->scope
&EXTERN_F
) printf("%sExternal within function",done
?" and ":"");
379 printf("<<<\n%s\n>>>\n",var
->comment
);
382 if(option_warn
&WARN_COMMENT
&& !var
->comment
&& (var
->scope
&(GLOBAL
|LOCAL
|EXTERNAL
|EXTERN_F
) || option_raw
))
383 printf("Warning %16s : Variable '%s' does not have a comment.\n",filename
,var
->name
);
386 printf("Defined: %s:%d\n",var
->incfrom
?var
->incfrom
:filename
,var
->lineno
);
389 printf("Type: %s\n",var
->type
);
391 if(option_raw
&& var
->incfrom
)
392 printf("Included from: %s\n",var
->incfrom
);
394 if(option_xref
&XREF_VAR
)
398 if(var
->scope
&(EXTERNAL
|EXTERN_F
) && var
->defined
)
399 printf("Declared global in '%s'\n",var
->defined
);
401 if(var
->scope
&(GLOBAL
|LOCAL
))
403 for(i
=0;i
<var
->visible
->n
;i
++)
404 if(var
->visible
->s1
[i
][0]=='$' && !var
->visible
->s1
[i
][1])
405 printf("Visible in %s\n",var
->visible
->s2
[i
]);
407 printf("Visible in %s : %s\n",var
->visible
->s1
[i
],var
->visible
->s2
[i
]);
409 for(i
=0;i
<var
->used
->n
;i
++)
410 if(var
->used
->s1
[i
][0]=='$' && !var
->used
->s1
[i
][1])
411 printf("Used in %s\n",var
->used
->s2
[i
]);
413 printf("Used in %s : %s\n",var
->used
->s1
[i
],var
->used
->s2
[i
]);
417 if(option_warn
&WARN_XREF
)
419 if(var
->scope
&(EXTERNAL
|EXTERN_F
) && !var
->defined
)
420 printf("Warning %16s : Variable '%s' has an unknown global definition.\n",filename
,var
->name
);
422 if(var
->scope
&(GLOBAL
|LOCAL
|EXTERNAL
|EXTERN_F
) && !var
->used
->n
)
423 printf("Warning %16s : Variable '%s' is not used anywhere.\n",filename
,var
->name
);
425 if(var
->scope
&(GLOBAL
|EXTERNAL
|EXTERN_F
) && var
->used
->n
)
427 int is_used_elsewhere
=0,is_used_here
=0;
428 for(i
=0;i
<var
->used
->n
;i
++)
429 if(!strcmp(filename
,var
->used
->s2
[i
]))
433 if(!is_used_elsewhere
)
434 printf("Warning %16s : Variable '%s' is %s but only used in this file.\n",filename
,var
->name
,var
->scope
&GLOBAL
?"global":"extern");
436 printf("Warning %16s : Variable '%s' is %s but not used in this file.\n",filename
,var
->name
,var
->scope
&GLOBAL
?"global":"extern");
443 /*++++++++++++++++++++++++++++++++++++++
444 Write a Function structure out.
446 Function func The Function structure to output.
447 ++++++++++++++++++++++++++++++++++++++*/
449 static void WriteWarnRawFunction(Function func
)
457 printf("\nFUNCTION : %s [",func
->name
);
458 if(func
->scope
&LOCAL
) done
=printf("Local");
459 if(func
->scope
&GLOBAL
) done
=printf("Global");
460 if(func
->scope
&EXTERNAL
) done
=printf("External");
461 if(func
->scope
&INLINED
) printf("%sInline",done
?" and ":"");
465 printf("<<<\n%s\n>>>\n",func
->comment
);
468 if(option_warn
&WARN_COMMENT
&& !func
->comment
)
469 printf("Warning %16s : Function '%s()' does not have a comment.\n",filename
,func
->name
);
472 printf("Defined: %s:%d\n",func
->incfrom
?func
->incfrom
:filename
,func
->lineno
);
474 if(option_xref
&XREF_FUNC
)
476 if(func
->protofile
&& option_raw
)
477 printf("Prototyped in %s\n",func
->protofile
);
478 if(option_warn
&WARN_XREF
&& !func
->protofile
)
479 printf("Warning %16s : Function '%s()' is not prototyped.\n",filename
,func
->name
);
485 printf("Type: %s <<<%s>>>\n",func
->type
,func
->cret
);
487 printf("Type: %s\n",func
->type
);
489 if(option_warn
&WARN_COMMENT
&& !func
->cret
&& strncmp("void ",func
->type
,5))
490 printf("Warning %16s : Function '%s()' has a return value with no comment.\n",filename
,func
->name
);
492 for(i
=0;i
<func
->args
->n
;i
++)
496 if(func
->args
->s2
[i
])
497 printf("Arguments: %s <<<%s>>>\n",func
->args
->s1
[i
],func
->args
->s2
[i
]);
499 printf("Arguments: %s\n",func
->args
->s1
[i
]);
501 if(option_warn
&WARN_COMMENT
&& !func
->args
->s2
[i
] && strcmp("void",func
->args
->s1
[i
]))
502 printf("Warning %16s : Function '%s()' has an argument '%s' with no comment.\n",filename
,func
->name
,func
->args
->s1
[i
]);
505 if(option_raw
&& func
->incfrom
)
506 printf("Included from: %s\n",func
->incfrom
);
508 if(option_xref
&XREF_FUNC
)
510 for(i
=0;i
<func
->calls
->n
;i
++)
514 if(func
->calls
->s2
[i
])
515 printf("Calls %s : %s\n",func
->calls
->s1
[i
],func
->calls
->s2
[i
]);
517 printf("Calls %s\n",func
->calls
->s1
[i
]);
519 #if 0 /* Too verbose */
520 if(option_warn
&WARN_XREF
&& !func
->calls
->s2
[i
])
521 printf("Warning %16s : Function '%s()' calls function '%s()' whose definition is unknown.\n",filename
,func
->name
,func
->calls
->s1
[i
]);
526 for(i
=0;i
<func
->called
->n
;i
++)
527 printf("Called from %s : %s\n",func
->called
->s1
[i
],func
->called
->s2
[i
]);
530 for(i
=0;i
<func
->used
->n
;i
++)
532 if(func
->used
->s1
[i
][0]=='$' && !func
->used
->s1
[i
][1])
533 printf("Used in %s\n",func
->used
->s2
[i
]);
535 printf("Used in %s : %s\n",func
->used
->s1
[i
],func
->used
->s2
[i
]);
538 for(i
=0;i
<func
->f_refs
->n
;i
++)
542 if(func
->f_refs
->s2
[i
])
543 printf("References Function %s : %s\n",func
->f_refs
->s1
[i
],func
->f_refs
->s2
[i
]);
545 printf("References Function %s\n",func
->f_refs
->s1
[i
]);
547 if(option_warn
&WARN_XREF
&& !func
->f_refs
->s2
[i
])
548 printf("Warning %16s : Function '%s()' references function '%s()' whose definition is unknown.\n",filename
,func
->name
,func
->f_refs
->s1
[i
]);
552 if(option_xref
&XREF_VAR
)
553 for(i
=0;i
<func
->v_refs
->n
;i
++)
557 if(func
->v_refs
->s2
[i
])
558 printf("References Variable %s : %s\n",func
->v_refs
->s1
[i
],func
->v_refs
->s2
[i
]);
560 printf("References Variable %s\n",func
->v_refs
->s1
[i
]);
562 if(option_warn
&WARN_XREF
&& !func
->v_refs
->s2
[i
])
563 printf("Warning %16s : Function '%s()' references variable '%s' whose definition is unknown.\n",filename
,func
->name
,func
->v_refs
->s1
[i
]);
567 if(option_warn
&WARN_XREF
)
569 if(!func
->used
->n
&& !func
->called
->n
)
570 printf("Warning %16s : Function '%s()' is not used anywhere.\n",filename
,func
->name
);
572 if(func
->scope
&(GLOBAL
|EXTERNAL
) && (func
->called
->n
|| func
->used
->n
))
574 int is_used_elsewhere
=0;
575 for(i
=0;i
<func
->called
->n
;i
++)
576 if(strcmp(func
->called
->s2
[i
],filename
))
577 {is_used_elsewhere
=1;break;}
578 for(i
=0;i
<func
->used
->n
;i
++)
579 if(strcmp(func
->used
->s2
[i
],filename
))
580 {is_used_elsewhere
=1;break;}
581 if(!is_used_elsewhere
)
582 printf("Warning %16s : Function '%s()' is global but is only used in this file.\n",filename
,func
->name
);
588 /*++++++++++++++++++++++++++++++++++++++
589 Write out a raw version of the appendix.
591 StringList files The list of files to write.
593 StringList2 funcs The list of functions to write.
595 StringList2 vars The list of variables to write.
597 StringList2 types The list of types to write.
598 ++++++++++++++++++++++++++++++++++++++*/
600 void WriteWarnRawAppendix(StringList files
,StringList2 funcs
,StringList2 vars
,StringList2 types
)
604 /* Write out the appendix of files. */
606 if(option_index
&INDEX_FILE
)
610 printf("\nAppendix - Files\n\n");
611 for(i
=0;i
<files
->n
;i
++)
612 printf("%s\n",files
->s
[i
]);
615 if(option_warn
&WARN_XREF
)
616 printf("Warning Index : No global files to index.\n");
619 /* Write out the appendix of functions. */
621 if(option_index
&INDEX_FUNC
)
625 printf("\nAppendix - Global Functions\n\n");
626 for(i
=0;i
<funcs
->n
;i
++)
627 printf("%s : %s\n",funcs
->s1
[i
],funcs
->s2
[i
]);
630 if(option_warn
&WARN_XREF
)
631 printf("Warning Index : No global functions to index.\n");
634 /* Write out the appendix of variables. */
636 if(option_index
&INDEX_VAR
)
640 printf("\nAppendix - Global Variables\n\n");
641 for(i
=0;i
<vars
->n
;i
++)
642 printf("%s : %s\n",vars
->s1
[i
],vars
->s2
[i
]);
645 if(option_warn
&WARN_XREF
)
646 printf("Warning Index : No global variables to index.\n");
649 /* Write out the appendix of types. */
651 if(option_index
&INDEX_TYPE
)
655 printf("\nAppendix - Defined Types\n\n");
656 for(i
=0;i
<types
->n
;i
++)
657 printf("%s : %s\n",types
->s1
[i
],types
->s2
[i
]);
660 if(option_warn
&WARN_XREF
)
661 printf("Warning Index : No types to index.\n");
666 /*++++++++++++++++++++++++++++++++++++++
667 Decide if to copy or skip the next line.
669 int CopyOrSkip Returns the number of characters to skip.
671 char *string The string that starts the next line.
673 char *type The type of file we are outputing.
675 int *copy Returns true if we are to copy the line verbatim.
677 int *skip Returns true if we are to skip the line.
678 ++++++++++++++++++++++++++++++++++++++*/
680 int CopyOrSkip(char *string
,char *type
,int *copy
,int *skip
)
687 while(*p
==' ' || *p
=='\t')
695 if(!strncmp(p
,"+html+",s
=6) || !strncmp(p
,"-latex-",s
=7) || !strncmp(p
,"-sgml-",s
=6) || !strncmp(p
,"-rtf-",s
=5))
697 if(!strncmp(p
,"-html-",s
=6) || !strncmp(p
,"+latex+",s
=7) || !strncmp(p
,"+sgml+",s
=6) || !strncmp(p
,"+rtf+",s
=5) || !strncmp(p
,"+none+",s
=6))
700 case 'l': /* latex */
701 if(!strncmp(p
,"-html-",s
=6) || !strncmp(p
,"+latex+",s
=7) || !strncmp(p
,"-sgml-",s
=6) || !strncmp(p
,"-rtf-",s
=5))
703 if(!strncmp(p
,"+html+",s
=6) || !strncmp(p
,"-latex-",s
=7) || !strncmp(p
,"+sgml+",s
=6) || !strncmp(p
,"+rtf+",s
=5) || !strncmp(p
,"+none+",s
=6))
707 if(!strncmp(p
,"-html-",s
=6) || !strncmp(p
,"-latex-",s
=7) || !strncmp(p
,"+sgml+",s
=6) || !strncmp(p
,"-rtf-",s
=5))
709 if(!strncmp(p
,"+html+",s
=6) || !strncmp(p
,"+latex+",s
=7) || !strncmp(p
,"-sgml-",s
=6) || !strncmp(p
,"+rtf+",s
=5) || !strncmp(p
,"+none+",s
=6))
713 if(!strncmp(p
,"-html-",s
=6) || !strncmp(p
,"-latex-",s
=7) || !strncmp(p
,"-sgml-",s
=6) || !strncmp(p
,"+rtf+",s
=5))
715 if(!strncmp(p
,"+html+",s
=6) || !strncmp(p
,"+latex+",s
=7) || !strncmp(p
,"+sgml+",s
=6) || !strncmp(p
,"-rtf-",s
=5) || !strncmp(p
,"+none+",s
=6))