Skip to content

Instantly share code, notes, and snippets.

@agscala
Created August 8, 2013 20:24
Show Gist options
  • Save agscala/6188347 to your computer and use it in GitHub Desktop.
Save agscala/6188347 to your computer and use it in GitHub Desktop.
Search directories for unused header files
import os
import re
from collections import defaultdict
# Directories to inspect all .h, .c, .cpp files inside
TARGET_DIRECTORIES = ["source", "include"]
def getfilepaths(target_directory):
all_files = []
for root, dirs, files in os.walk(target_directory):
for file in files:
fullpath = os.path.join(root, file)
all_files.append(fullpath)
# file_name, file_ext = os.path.splitext(file)
# print file_ext
return all_files
def get_included_files(filepath):
included_files = []
for line in open(filepath):
if "#include" in line:
result = re.search("#include \"(?P<filename>[A-Za-z0-9\._]+)\".*", line)
if result:
filename = result.group("filename")
included_files.append(filename)
return included_files
if __name__ == "__main__":
all_filepaths = []
# Get a list of all file paths we're targetting.
for directory in TARGET_DIRECTORIES:
all_filepaths.extend(getfilepaths(directory))
# A dict of {header: [file paths that include this header]}
header_includers = defaultdict(set)
for filepath in all_filepaths:
included_files = get_included_files(filepath)
for included_file in included_files:
header_includers[included_file].add(filepath)
# Print the include graph nicely.
for file, callers in header_includers.items():
print file
for caller in callers:
print "\t%s" % caller
print "\n"
# Find the headers that were never used
used_headers = header_includers.keys()
unused_headers = [filepath for filepath in all_filepaths if ".h" in filepath and os.path.basename(filepath) not in used_headers]
print "UNUSED HEADER FILES"
for header in unused_headers:
print "\t%s" % header
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment