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
|
/***************************************
$Header: /home/amb/cxref/RCS/preproc.c 1.14 1998/02/26 19:54:30 amb Exp $
C Cross Referencing & Documentation tool. Version 1.4b.
Collects the pre-processing instruction stuff.
******************/ /******************
Written by Andrew M. Bishop
This file Copyright 1995,96,97 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 for this file. +*/
#define DEBUG 0
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "memory.h"
#include "datatype.h"
#include "cxref.h"
/*+ The file that is currently being processed. +*/
extern File CurFile;
/*+ The name of the include directories specified on the command line. +*/
extern char **option_incdirs;
/*+ The number of include directories on the command line. +*/
extern int option_nincdirs;
/*+ When in a header file, this is set to 1, to allow most of the stuff to be skipped. +*/
int in_header=0;
/*+ The current #include we are looking at. +*/
static Include cur_inc=NULL;
/*+ The current #define we are looking at. +*/
static Define cur_def=NULL;
/*+ The depth of includes. +*/
static int inc_depth=0;
/*+ The type of include at this depth. +*/
static char *inc_type=NULL;
/*+ The working directory. +*/
static char *cwd=NULL;
static Include NewIncludeType(char *name);
static Define NewDefineType(char *name);
/*++++++++++++++++++++++++++++++++++++++
Function that is called when an included file is seen in the current file.
char *name The name of the file from the source code.
++++++++++++++++++++++++++++++++++++++*/
void SeenInclude(char *name)
{
#if DEBUG
printf("#Preproc.c# #include %s\n",name);
#endif
if(!inc_type || inc_depth==0 || inc_type[inc_depth-1]==LOCAL)
{
Include inc,*t=&CurFile->includes;
int inc_scope=(*name=='"')?LOCAL:GLOBAL;
int i;
name++;
name[strlen(name)-1]=0;
if(inc_scope==LOCAL && option_nincdirs)
for(i=0;i<option_nincdirs;i++)
{
char *newname=CanonicaliseName(ConcatStrings(3,option_incdirs[i],"/",name));
struct stat buf;
if(!lstat(newname,&buf))
{name=newname;break;}
}
for(i=0;i<inc_depth;i++)
{
while(*t && (*t)->next)
t=&(*t)->next;
t=&(*t)->includes;
}
inc=NewIncludeType(name);
inc->comment=MallocString(GetCurrentComment());
inc->scope=inc_scope;
AddToLinkedList(*t,Include,inc);
cur_inc=inc;
}
else
cur_inc=NULL;
}
/*++++++++++++++++++++++++++++++++++++++
Function that is called when a comment is seen following a #include.
++++++++++++++++++++++++++++++++++++++*/
void SeenIncludeComment(void)
{
char* comment=GetCurrentComment();
#if DEBUG
printf("#Preproc.c# #include trailing comment '%s' for %s\n",comment,cur_inc->name);
#endif
if(!cur_inc->comment)
cur_inc->comment=MallocString(comment);
}
/*++++++++++++++++++++++++++++++++++++++
Function that is called when a change in current file is seen.
char *name The pathname of the included file as determined by gcc.
int flag The flags that GCC leaves in the file
++++++++++++++++++++++++++++++++++++++*/
void SeenFileChange(char *name,int flag)
{
if(!cwd)
{
cwd=(char*)Malloc(256);
if(!getcwd(cwd,255))
cwd[0]=0;
}
#if DEBUG
printf("#Preproc.c# FileChange - %s %s (flag=%d)\n",flag&2?"Included ":"Return to",name,flag);
#endif
name=CanonicaliseName(name);
if(!strncmp(name,cwd,strlen(cwd)))
name=name+strlen(cwd);
/* Store the information. */
if(flag&2 && (!inc_type || inc_depth==0 || inc_type[inc_depth-1]==LOCAL))
{
if(!cur_inc)
{
if(flag&8)
SeenInclude(ConcatStrings(3,"<",name,">"));
else
SeenInclude(ConcatStrings(3,"\"",name,"\""));
}
else if(!(flag&8))
{
Free(cur_inc->name);
cur_inc->name=MallocString(name);
}
}
cur_inc=NULL;
if(flag&2)
{
inc_depth++;
if(!inc_type)
inc_type=(char*)Malloc(16);
else
if(!(inc_depth%16))
inc_type=(char*)Realloc(inc_type,(unsigned)(inc_depth+16));
if(inc_depth>1 && inc_type[inc_depth-2]==GLOBAL)
inc_type[inc_depth-1]=GLOBAL;
else
inc_type[inc_depth-1]=(flag&8)?GLOBAL:LOCAL;
}
else
inc_depth--;
if(inc_type && inc_depth>0)
in_header=inc_type[inc_depth-1];
else
in_header=0;
}
/*++++++++++++++++++++++++++++++++++++++
Function that is called when a #define is seen in the current file.
char* name The name of the #defined symbol.
++++++++++++++++++++++++++++++++++++++*/
void SeenDefine(char* name)
{
Define def;
#if DEBUG
printf("#Preproc.c# Defined name '%s'\n",name);
#endif
def=NewDefineType(name);
def->comment=MallocString(GetCurrentComment());
AddToLinkedList(CurFile->defines,Define,def);
cur_def=def;
}
/*++++++++++++++++++++++++++++++++++++++
Function that is called when a comment is seen in a #define definition.
++++++++++++++++++++++++++++++++++++++*/
void SeenDefineComment(void)
{
char* comment=GetCurrentComment();
#if DEBUG
printf("#Preproc.c# #define inline comment '%s' in %s\n",comment,cur_def->name);
#endif
if(!cur_def->comment)
cur_def->comment=MallocString(comment);
}
/*++++++++++++++++++++++++++++++++++++++
Function that is called when a #define value is seen in the current file.
char* value The value of the #defined symbol.
++++++++++++++++++++++++++++++++++++++*/
void SeenDefineValue(char* value)
{
#if DEBUG
printf("#Preproc.c# #define value '%s' for %s\n",value,cur_def->name);
#endif
cur_def->value=MallocString(value);
}
/*++++++++++++++++++++++++++++++++++++++
Function that is called when a #define function argument is seen in the current definition.
char* name The argument.
++++++++++++++++++++++++++++++++++++++*/
void SeenDefineFunctionArg(char* name)
{
#if DEBUG
printf("#Preproc.c# #define Function arg '%s' in %s()\n",name,cur_def->name);
#endif
AddToStringList2(cur_def->args,name,SplitComment(&cur_def->comment,name),0,0);
}
/*++++++++++++++++++++++++++++++++++++++
Function that is called when a comment is seen in a #define function definition.
++++++++++++++++++++++++++++++++++++++*/
void SeenDefineFuncArgComment(void)
{
char* comment=GetCurrentComment();
#if DEBUG
printf("#Preproc.c# #define Function arg comment '%s' in %s()\n",comment,cur_def->name);
#endif
if(!cur_def->args->s2[cur_def->args->n-1])
cur_def->args->s2[cur_def->args->n-1]=MallocString(comment);
}
/*++++++++++++++++++++++++++++++++++++++
Tidy up all of the local variables in case of a problem and abnormal parser termination.
++++++++++++++++++++++++++++++++++++++*/
void ResetPreProcAnalyser(void)
{
in_header=0;
cur_inc=NULL;
cur_def=NULL;
inc_depth=0;
if(inc_type) Free(inc_type);
inc_type=NULL;
if(cwd) Free(cwd);
cwd=NULL;
}
/*++++++++++++++++++++++++++++++++++++++
Create a new Include datatype.
Include NewIncludeType Return the new Include type.
char *name The name of the new include.
++++++++++++++++++++++++++++++++++++++*/
static Include NewIncludeType(char *name)
{
Include inc=(Include)Calloc(1,sizeof(struct _Include));
inc->name=MallocString(name);
return(inc);
}
/*++++++++++++++++++++++++++++++++++++++
Delete the specified Include type.
Include inc The Include type to be deleted.
++++++++++++++++++++++++++++++++++++++*/
void DeleteIncludeType(Include inc)
{
if(inc->comment) Free(inc->comment);
if(inc->name) Free(inc->name);
if(inc->includes)
{
Include p=inc->includes;
do{
Include n=p->next;
DeleteIncludeType(p);
p=n;
}
while(p);
}
Free(inc);
}
/*++++++++++++++++++++++++++++++++++++++
Create a new Define datatype.
Define NewDefineType Return the new Define type.
char *name The name of the new define.
++++++++++++++++++++++++++++++++++++++*/
static Define NewDefineType(char *name)
{
Define def=(Define)Calloc(1,sizeof(struct _Define));
def->name=MallocString(name);
def->args=NewStringList2();
return(def);
}
/*++++++++++++++++++++++++++++++++++++++
Delete the specified Define type.
Define def The Define type to be deleted.
++++++++++++++++++++++++++++++++++++++*/
void DeleteDefineType(Define def)
{
if(def->comment) Free(def->comment);
if(def->name) Free(def->name);
if(def->value) Free(def->value);
if(def->args) DeleteStringList2(def->args);
Free(def);
}
|