void add_include(struct filepointer *filep, struct inclist *file, struct inclist *file_red, const char *include, int type, boolean failOK) { register struct inclist *newfile; register struct filepointer *content; /* * First decide what the pathname of this include file really is. */ newfile = inc_path(file->i_file, include, type); if (newfile == NULL) { if (failOK) return; if (file != file_red) warning("%s (reading %s, line %ld): ", file_red->i_file, file->i_file, filep->f_line); else warning("%s, line %ld: ", file->i_file, filep->f_line); warning1("cannot find include file \"%s\"\n", include); show_where_not = TRUE; newfile = inc_path(file->i_file, include, type); show_where_not = FALSE; } if (newfile) { included_by(file, newfile); if (!(newfile->i_flags & SEARCHED)) { newfile->i_flags |= SEARCHED; content = getfile(newfile->i_file); find_includes(content, newfile, file_red, 0, failOK); freefile(content); } } }
int find_includes(struct filepointer *filep, struct inclist *file, struct inclist *file_red, int recursion, boolean failOK, struct IncludesCollection* incCollection, struct symhash *symbols) { char *line; int type; boolean recfailOK; while ((line = get_line(filep))) { switch(type = deftype(line, filep, file_red, file, TRUE, symbols)) { case IF: doif: type = find_includes(filep, file, file_red, recursion+1, failOK, incCollection, symbols); while ((type == ELIF) || (type == ELIFFALSE) || (type == ELIFGUESSFALSE)) type = gobble(filep, file, file_red, symbols); if (type == ELSE) gobble(filep, file, file_red, symbols); break; case IFFALSE: case IFGUESSFALSE: doiffalse: if (type == IFGUESSFALSE || type == ELIFGUESSFALSE) recfailOK = TRUE; else recfailOK = failOK; type = gobble(filep, file, file_red, symbols); if (type == ELSE) find_includes(filep, file, file_red, recursion+1, recfailOK, incCollection, symbols); else if (type == ELIF) goto doif; else if ((type == ELIFFALSE) || (type == ELIFGUESSFALSE)) goto doiffalse; break; case IFDEF: case IFNDEF: if ((type == IFDEF && hash_lookup(line, symbols)) || (type == IFNDEF && !hash_lookup(line, symbols))) { debug(1,(type == IFNDEF ? "line %d: %s !def'd in %s via %s%s\n" : "", filep->f_line, line, file->i_file, file_red->i_file, ": doit")); type = find_includes(filep, file, file_red, recursion+1, failOK, incCollection, symbols); while (type == ELIF || type == ELIFFALSE || type == ELIFGUESSFALSE) type = gobble(filep, file, file_red, symbols); if (type == ELSE) gobble(filep, file, file_red, symbols); } else { debug(1,(type == IFDEF ? "line %d: %s !def'd in %s via %s%s\n" : "", filep->f_line, line, file->i_file, file_red->i_file, ": gobble")); type = gobble(filep, file, file_red, symbols); if (type == ELSE) find_includes(filep, file, file_red, recursion + 1, failOK, incCollection, symbols); else if (type == ELIF) goto doif; else if (type == ELIFFALSE || type == ELIFGUESSFALSE) goto doiffalse; } break; case ELSE: case ELIFFALSE: case ELIFGUESSFALSE: case ELIF: if (!recursion) gobble(filep, file, file_red, symbols); /*fall-through*/ case ENDIF: if (recursion) return type; /*fall-through*/ case DEFINE: define(line, &symbols); break; case UNDEF: if (!*line) { warning("%s, line %d: incomplete undef == \"%s\"\n", file_red->i_file, filep->f_line, line); break; } hash_undefine(line, symbols); break; case INCLUDE: add_include(filep, file, file_red, line, FALSE, failOK, incCollection, symbols); break; case INCLUDEDOT: add_include(filep, file, file_red, line, TRUE, failOK, incCollection, symbols); break; case ERROR: warning("%s: %d: %s\n", file_red->i_file, filep->f_line, line); break; case PRAGMA: case IDENT: case SCCS: case EJECT: break; case -1: warning("%s", file_red->i_file); if (file_red != file) warning1(" (reading %s)", file->i_file); warning1(", line %d: unknown directive == \"%s\"\n", filep->f_line, line); break; case -2: warning("%s", file_red->i_file); if (file_red != file) warning1(" (reading %s)", file->i_file); warning1(", line %d: incomplete include == \"%s\"\n", filep->f_line, line); break; } } return -1; }
int find_includes (struct filepointer *filep, struct inclist *file, struct inclist *file_red, int recursion, bool failOK) { register char *line; register int type; bool recfailOK; while ((line = getline (filep))) { switch (type = deftype (line, filep, file_red, file, true)) { case IF: doif: type = find_includes (filep, file, file_red, recursion + 1, failOK); while ((type == ELIF) || (type == ELIFFALSE) || (type == ELIFGUESSFALSE)) type = gobble (filep, file, file_red); if (type == ELSE) gobble (filep, file, file_red); break; case IFFALSE: case IFGUESSFALSE: doiffalse: if (type == IFGUESSFALSE || type == ELIFGUESSFALSE) recfailOK = true; else recfailOK = failOK; type = gobble (filep, file, file_red); if (type == ELSE) find_includes (filep, file, file_red, recursion + 1, recfailOK); else if (type == ELIF) goto doif; else if ((type == ELIFFALSE) || (type == ELIFGUESSFALSE)) goto doiffalse; break; case IFDEF: case IFNDEF: if ((type == IFDEF && isdefined (line, file_red, NULL)) || (type == IFNDEF && !isdefined (line, file_red, NULL))) { debug (1, (type == IFNDEF ? "line %d: %s !def'd in %s via %s%s\n" : "", filep->f_line, line, file->i_file, file_red->i_file, ": doit")); type = find_includes (filep, file, file_red, recursion + 1, failOK); while (type == ELIF || type == ELIFFALSE || type == ELIFGUESSFALSE) type = gobble (filep, file, file_red); if (type == ELSE) gobble (filep, file, file_red); } else { debug (1, (type == IFDEF ? "line %d: %s !def'd in %s via %s%s\n" : "", filep->f_line, line, file->i_file, file_red->i_file, ": gobble")); type = gobble (filep, file, file_red); if (type == ELSE) find_includes (filep, file, file_red, recursion + 1, failOK); else if (type == ELIF) goto doif; else if (type == ELIFFALSE || type == ELIFGUESSFALSE) goto doiffalse; } break; case ELSE: case ELIFFALSE: case ELIFGUESSFALSE: case ELIF: if (!recursion) gobble (filep, file, file_red); case ENDIF: if (recursion) return (type); case DEFINE: define (line, file); break; case UNDEF: if (!*line) { warning ("%s, line %d: incomplete undef == \"%s\"\n", file->i_file, filep->f_line, line); display_included (file_red, file); break; } undefine (line, file_red); break; case INCLUDE: add_include (filep, file, file_red, line, false, failOK); break; case INCLUDEDOT: add_include (filep, file, file_red, line, true, failOK); break; case ERROR: case WARNING: warning ("%s: %d: %s\n", file->i_file, filep->f_line, line); display_included (file_red, file); break; case PRAGMA: case IDENT: case SCCS: case EJECT: break; case -1: warning ("%s", file_red->i_file); if (file_red != file) warning1 (" (reading %s)", file->i_file); warning1 (", line %d: unknown directive == \"%s\"\n", filep->f_line, line); break; case -2: warning ("%s", file_red->i_file); if (file_red != file) warning1 (" (reading %s)", file->i_file); warning1 (", line %d: incomplete include == \"%s\"\n", filep->f_line, line); break; } } file->i_flags |= FINISHED; return (-1); }
int main(int argc, char **argv) { llist_t *paths,*src,*todo; set_t *incl; map_t *deps; if (argc < 2) { fprintf(stderr,"FastDep v%s for LAMMPS\n" "Usage: %s [-I <path> ...] -- <src1> [<src2> ...]\n", version,argv[0]); fprintf(stderr,"Supported extensions: %s, %s, %s\n", extensions[0], extensions[1], extensions[2]); return 1; } /* hash tables for all known included files and dependencies * we guesstimate a little over 2x as many entries as sources. */ incl = set_init(2*argc); deps = map_init(2*argc); /* list of include search paths. prefixed by "." and "..". */ paths = llist_init(); llist_append(paths,"."); llist_append(paths,".."); while (++argv, --argc > 0) { if (strncmp(*argv, "-I", 2) == 0) { if ((*argv)[2] != '\0') { llist_append(paths,trim_path(*argv+2)); } else { ++argv; --argc; if (argc > 0) { if (strcmp(*argv,"--") == 0) { break; } else { llist_append(paths,trim_path(*argv)); } } } } else if (strcmp(*argv,"--") == 0) { break; } /* ignore all unrecognized arguments before '--'. */ } src = llist_init(); while (++argv, --argc > 0) { llist_append(src,*argv); } /* process files to look for includes */ todo = llist_init(); find_includes(src->head,todo,paths,incl,deps); find_includes(todo->head,todo,paths,incl,deps); llist_free(todo); fprintf(stdout,"# FastDep v%s for LAMMPS\n",version); fputs("# Search path: ",stdout); llist_print(paths); fprintf(stdout,"# % 5d sources\n# % 5d includes\n# % 5d depfiles\n", llist_size(src),set_size(incl),map_size(deps)); set_free(incl); do_depend(src->head,deps); llist_free(src); llist_free(paths); map_free(deps); return 0; }
int find_includes(struct filepointer *filep, struct inclist *file, struct inclist *file_red, int recursion, boolean failOK) { struct inclist *inclistp; char **includedirsp; register char *line; register int type; boolean recfailOK; while ((line = getnextline(filep))) { switch(type = deftype(line, filep, file_red, file, TRUE)) { case IF: doif: type = find_includes(filep, file, file_red, recursion+1, failOK); while ((type == ELIF) || (type == ELIFFALSE) || (type == ELIFGUESSFALSE)) type = gobble(filep, file, file_red); if (type == ELSE) gobble(filep, file, file_red); break; case IFFALSE: case IFGUESSFALSE: doiffalse: if (type == IFGUESSFALSE || type == ELIFGUESSFALSE) recfailOK = TRUE; else recfailOK = failOK; type = gobble(filep, file, file_red); if (type == ELSE) find_includes(filep, file, file_red, recursion+1, recfailOK); else if (type == ELIF) goto doif; else if ((type == ELIFFALSE) || (type == ELIFGUESSFALSE)) goto doiffalse; break; case IFDEF: case IFNDEF: { int isdef = (isdefined(line, file_red, NULL) != NULL); if (type == IFNDEF) isdef = !isdef; if (isdef) { debug(1,(type == IFNDEF ? "line %d: %s !def'd in %s via %s%s\n" : "", filep->f_line, line, file->i_file, file_red->i_file, ": doit")); type = find_includes(filep, file, file_red, recursion+1, failOK); while (type == ELIF || type == ELIFFALSE || type == ELIFGUESSFALSE) type = gobble(filep, file, file_red); if (type == ELSE) gobble(filep, file, file_red); } else { debug(1,(type == IFDEF ? "line %d: %s !def'd in %s via %s%s\n" : "", filep->f_line, line, file->i_file, file_red->i_file, ": gobble")); type = gobble(filep, file, file_red); if (type == ELSE) find_includes(filep, file, file_red, recursion+1, failOK); else if (type == ELIF) goto doif; else if (type == ELIFFALSE || type == ELIFGUESSFALSE) goto doiffalse; } } break; case ELSE: case ELIFFALSE: case ELIFGUESSFALSE: case ELIF: if (!recursion) gobble(filep, file, file_red); case ENDIF: if (recursion) return(type); case DEFINE: define(line, file); break; case UNDEF: if (!*line) { warning("%s", file_red->i_file); if (file_red != file) warning1(" (reading %s)", file->i_file); warning1(", line %d: incomplete undef == \"%s\"\n", filep->f_line, line); break; } undefine(line, file_red); break; case INCLUDE: case INCLUDEDOT: case INCLUDENEXT: case INCLUDENEXTDOT: inclistp = inclistnext; includedirsp = includedirsnext; debug(2,("%s, reading %s, includes %s\n", file_red->i_file, file->i_file, line)); add_include(filep, file, file_red, line, type, failOK); inclistnext = inclistp; includedirsnext = includedirsp; break; case ERROR: case WARNING: warning("%s", file_red->i_file); if (file_red != file) warning1(" (reading %s)", file->i_file); warning1(", line %d: %s\n", filep->f_line, line); break; case PRAGMA: case IDENT: case SCCS: case EJECT: break; case -1: warning("%s", file_red->i_file); if (file_red != file) warning1(" (reading %s)", file->i_file); warning1(", line %d: unknown directive == \"%s\"\n", filep->f_line, line); break; case -2: warning("%s", file_red->i_file); if (file_red != file) warning1(" (reading %s)", file->i_file); warning1(", line %d: incomplete include == \"%s\"\n", filep->f_line, line); break; } } file->i_flags |= FINISHED; debug(2,("finished with %s\n", file->i_file)); return(-1); }
int main (int argc, char **argv) { register char **fp = filelist; register char **incp = includedirs; register char *p; register struct inclist *ip; char *makefile = NULL; struct filepointer *filecontent; struct symtab *psymp = predefs; const char *endmarker = NULL; char *defincdir = NULL; char **undeflist = NULL; int i, numundefs = 0; #if defined (INITIALIZE) // initialization code INITIALIZE; #endif ProgramName = argv[0]; while (psymp->s_name) { define2 (psymp->s_name, psymp->s_value, &maininclist); psymp++; } if (argc == 2 && argv[1][0] == '@') { struct stat ast; int afd; char *args; char **nargv; int nargc; char quotechar = '\0'; nargc = 1; if ((afd = open (argv[1] + 1, O_RDONLY)) < 0) fatalerr ("cannot open \"%s\"\n", argv[1] + 1); fstat (afd, &ast); args = (char *) malloc (ast.st_size + 1); if ((ast.st_size = read (afd, args, ast.st_size)) < 0) fatalerr ("failed to read %s\n", argv[1] + 1); args[ast.st_size] = '\0'; close (afd); for (p = args; *p; p++) { if (quotechar) { if (quotechar == '\\' || (*p == quotechar && p[-1] != '\\')) quotechar = '\0'; continue; } switch (*p) { case '\\': case '"': case '\'': quotechar = *p; break; case ' ': case '\n': *p = '\0'; if (p > args && p[-1]) nargc++; break; } } if (p[-1]) nargc++; nargv = (char **) malloc (nargc * sizeof (char *)); nargv[0] = argv[0]; argc = 1; for (p = args; argc < nargc; p += strlen (p) + 1) if (*p) nargv[argc++] = p; argv = nargv; } for (argc--, argv++; argc; argc--, argv++) { /* if looking for endmarker then check before parsing */ if (endmarker && strcmp (endmarker, *argv) == 0) { endmarker = NULL; continue; } if (**argv != '-') { /* treat +thing as an option for C++ */ if (endmarker && **argv == '+') continue; *fp++ = argv[0]; continue; } switch (argv[0][1]) { case '-': endmarker = &argv[0][2]; if (endmarker[0] == '\0') endmarker = "--"; break; case 'D': if (argv[0][2] == '\0') { argv++; argc--; } for (p = argv[0] + 2; *p; p++) if (*p == '=') { *p = ' '; break; } define (argv[0] + 2, &maininclist); break; case 'I': if (incp >= includedirs + MAXDIRS) fatalerr ("Too many -I flags.\n"); *incp++ = argv[0] + 2; if (**(incp - 1) == '\0') { *(incp - 1) = *(++argv); argc--; } break; case 'U': /* Undef's override all -D's so save them up */ numundefs++; if (numundefs == 1) undeflist = (char **)malloc (sizeof (char *)); else undeflist = (char **)realloc (undeflist, numundefs * sizeof (char *)); if (argv[0][2] == '\0') { argv++; argc--; } undeflist[numundefs - 1] = argv[0] + 2; break; case 'Y': defincdir = argv[0] + 2; break; /* do not use if endmarker processing */ case 'a': if (endmarker) break; opt_append = true; break; case 'b': if (endmarker) break; opt_backup = true; break; case 'c': if (endmarker) break; opt_create = true; break; case 'r': if (endmarker) break; opt_remove_prefix = true; break; case 'w': if (endmarker) break; if (argv[0][2] == '\0') { argv++; argc--; width = atoi (argv[0]); } else width = atoi (argv[0] + 2); break; case 'o': if (endmarker) break; if (argv[0][2] == '\0') { argv++; argc--; objsuffix = argv[0]; } else objsuffix = argv[0] + 2; break; case 'p': if (endmarker) break; if (argv[0][2] == '\0') { argv++; argc--; objprefix = argv[0]; } else objprefix = argv[0] + 2; break; case 'S': if (endmarker) break; opt_sysincludes = true; break; case 'v': if (endmarker) break; opt_verbose = true; #ifdef CS_DEBUG if (argv[0][2]) _debugmask = atoi (argv[0] + 2); #endif break; case 's': if (endmarker) break; startat = argv[0] + 2; if (*startat == '\0') { startat = *(++argv); argc--; } if (*startat != '#') fatalerr ("-s flag's value should start %s\n", "with '#'."); break; case 'f': if (endmarker) break; makefile = argv[0] + 2; if (*makefile == '\0') { makefile = *(++argv); argc--; } break; case 'm': opt_warn_multiple = true; break; /* Ignore -O, -g so we can just pass ${CFLAGS} to makedepend */ case 'O': case 'g': break; case 'h': if (endmarker) break; display_help (); return 0; case 'V': if (endmarker) break; display_version (); return 0; default: if (endmarker) break; /* fatalerr("unknown opt = %s\n", argv[0]); */ warning ("ignoring option %s\n", argv[0]); } } /* Now do the undefs from the command line */ for (i = 0; i < numundefs; i++) undefine (undeflist[i], &maininclist); if (numundefs > 0) free (undeflist); if (!defincdir) { #ifdef PREINCDIR add_include (PREINCDIR, incp); #endif #if defined (__GNUC__) || defined (_WIN32) add_include ( # if defined (__GNUC__) getenv ("C_INCLUDE_PATH"), # else getenv ("INCLUDE"), # endif incp); #else /* !__GNUC__, does not use INCLUDEDIR at all */ if (incp >= includedirs + MAXDIRS) fatalerr ("Too many -I flags.\n"); *incp++ = INCLUDEDIR; #endif #ifdef POSTINCDIR add_include (POSTINCDIR, incp); #endif } else if (*defincdir) add_include (defincdir, incp); /* if nothing to do, abort */ if (!*filelist) fatalerr ("No files specified, try \"makedep -h\"\n"); redirect (startat, makefile); /* catch signals */ #ifdef USGISH /* should really reset SIGINT to SIG_IGN if it was. */ #ifdef SIGHUP signal (SIGHUP, sighandler); #endif signal (SIGINT, sighandler); #ifdef SIGQUIT signal (SIGQUIT, sighandler); #endif signal (SIGILL, sighandler); #ifdef SIGBUS signal (SIGBUS, sighandler); #endif signal (SIGSEGV, sighandler); #ifdef SIGSYS signal (SIGSYS, sighandler); #endif #else sig_act.sa_handler = sighandler; #ifdef _POSIX_SOURCE sigemptyset (&sig_act.sa_mask); sigaddset (&sig_act.sa_mask, SIGINT); sigaddset (&sig_act.sa_mask, SIGQUIT); #ifdef SIGBUS sigaddset (&sig_act.sa_mask, SIGBUS); #endif sigaddset (&sig_act.sa_mask, SIGILL); sigaddset (&sig_act.sa_mask, SIGSEGV); sigaddset (&sig_act.sa_mask, SIGHUP); sigaddset (&sig_act.sa_mask, SIGPIPE); #ifdef SIGSYS sigaddset (&sig_act.sa_mask, SIGSYS); #endif #else sig_act.sa_mask = ((1 << (SIGINT - 1)) #ifdef SIGQUIT | (1 << (SIGQUIT - 1)) #endif #ifdef SIGBUS | (1 << (SIGBUS - 1)) #endif | (1 << (SIGILL - 1)) | (1 << (SIGSEGV - 1)) #ifdef SIGHUP | (1 << (SIGHUP - 1)) #endif #ifdef SIGPIPE | (1 << (SIGPIPE - 1)) #endif #ifdef SIGSYS | (1 << (SIGSYS - 1)) #endif ); #endif /* _POSIX_SOURCE */ sig_act.sa_flags = 0; #ifdef SIGHUP sigaction (SIGHUP, &sig_act, (struct sigaction *) 0); #endif sigaction (SIGINT, &sig_act, (struct sigaction *) 0); #ifdef SIGQUIT sigaction (SIGQUIT, &sig_act, (struct sigaction *) 0); #endif sigaction (SIGILL, &sig_act, (struct sigaction *) 0); #ifdef SIGBUS sigaction (SIGBUS, &sig_act, (struct sigaction *) 0); #endif sigaction (SIGSEGV, &sig_act, (struct sigaction *) 0); #ifdef SIGSYS sigaction (SIGSYS, &sig_act, (struct sigaction *) 0); #endif #endif /* USGISH */ /* * now peruse through the list of files. */ for (fp = filelist; *fp; fp++) { filecontent = getfile (*fp); ip = newinclude (*fp, (char *) NULL); find_includes (filecontent, ip, ip, 0, false); freefile (filecontent); recursive_pr_include (ip, ip->i_file, base_name (*fp)); inc_clean (); } if (opt_printed) printf ("\n"); return 0; }