diff options
author | Eric S. Raymond <esr@thyrsus.com> | 2008-11-08 14:08:59 +0000 |
---|---|---|
committer | Eric S. Raymond <esr@thyrsus.com> | 2008-11-08 14:08:59 +0000 |
commit | 1c50c0216f1f791bd336e10cf6874889aefa7395 (patch) | |
tree | 08ab06ad5f38f2fa1878e47880b3e12e3625f75a | |
parent | 62ddc13fe89535f7816d17fe9fec1aa17100957e (diff) | |
download | deheader-1c50c0216f1f791bd336e10cf6874889aefa7395.tar.gz deheader-1c50c0216f1f791bd336e10cf6874889aefa7395.tar.bz2 deheader-1c50c0216f1f791bd336e10cf6874889aefa7395.tar.xz deheader-1c50c0216f1f791bd336e10cf6874889aefa7395.zip |
Refactored so building the inclusion map is all sone at once.
-rwxr-xr-x | deheader | 60 |
1 files changed, 11 insertions, 49 deletions
@@ -72,12 +72,17 @@ class InclusionMap: if not os.path.isdir(root): if c_source(root): self.files.append(root) + else: + print >>sys.stderr, "deheader: can't analyze %s" % root else: for root, dirs, files in os.walk(root): dirs = filter(lambda x: not x.startswith("."), dirs) for name in files: - if c_source(name): - self.files.append(os.path.join(root, name)) + path = os.path.join(root, name) + if c_source(path): + self.files.append(path) + else: + print >>sys.stderr, "deheader: can't analyze %s" % parth self.c_to_h = {} for sourcefile in self.files: includes = [] @@ -107,21 +112,6 @@ class InclusionMap: "Undo forgetting of a dependency." self.c_to_h[sourcefile].append(header) -def sourcefiles(roots): - "Get the names of sourcefiles at or beneath specified locations." - files = [] - for root in roots: - if not os.path.isdir(root): - if c_source(root): - files.append(root) - else: - for root, dirs, files in os.walk(root): - dirs = filter(lambda x: not x.startswith("."), dirs) - for name in files: - if c_source(name): - files.append(os.path.join(root, name)) - return files - def trim(line): "Get file reference from an #include, retaining <> if a system header." trimmed = line[9:].strip() @@ -132,29 +122,6 @@ def trim(line): else: return `line` -def extract_includes(sourcefile, ignore, verbosity): - "Extract a list of #include lines used in a given sourcfile." - includes = [] - ifdepth = 0 - for line in open(sourcefile): - if line.startswith("#ifdef") or line.startswith("#ifndef"): - ifdepth += 1 - elif line.startswith("#endif"): - ifdepth -= 1 - elif line.startswith("#include"): - if verbosity: - name = trim(line) - print "deheader: %s requires %s" % (sourcefile, name) - if ignore.search(line): - if verbosity: - print "deheader: ignoring %s (exclusion match with %s)." % (name, ignore.pattern) - continue - if ifdepth == 0: - includes.append(line) - elif verbose: - print "deheader: ignoring %s (conditional inclusion)" % name - return includes - def compile(source, msg="", verbosity=0): "Test-compile a sourcefile. Return the status and the compilation time" (stem, suffix) = os.path.splitext(source) @@ -178,19 +145,15 @@ def compile(source, msg="", verbosity=0): % (sourcefile, msg, explain, end-start) return (status, end - start) -def deheader(sourcefile, ignores, remove, verbose): +def deheader(sourcefile, includes, remove, verbose): # Sanity check against broken sourcefiles; we want this to # complain visibly if the sourcefile won't build at all. (st, t) = compile(sourcefile, min(1, verbose)) if st != 0: return # Now do the analysis - includes = extract_includes(sourcefile, ignores, verbose) if sourcefile.endswith(".c") or sourcefile.endswith(".cpp"): unneeded = c_analyze(sourcefile, includes, verbose) - else: - print >>sys.stderr, "deheader: can't analyze %s" % sourcefile - return if unneeded: for line in unneeded: print "deheader: remove %s from %s" % (trim(line), sourcefile) @@ -253,7 +216,6 @@ if __name__ == "__main__": if not arguments: arguments = ["."] - #for sourcefile in sourcefiles(arguments): - # deheader(sourcefile, ignore, remove, verbose) - imap = InclusionMap(arguments, ignore, verbose) - print imap.c_to_h + inclusion_map = InclusionMap(arguments, ignore, verbose) + for sourcefile in inclusion_map.c_to_h: + deheader(sourcefile, inclusion_map.c_to_h[sourcefile], remove, verbose) |