Skip to content

Instantly share code, notes, and snippets.

@bbannier
Forked from rpavlik/iwyu-on-json.py
Last active October 6, 2015 15:02
Show Gist options
  • Save bbannier/073c5efdaaf88f95d16d to your computer and use it in GitHub Desktop.
Save bbannier/073c5efdaaf88f95d16d to your computer and use it in GitHub Desktop.
Run iwyu on compile_commands.json file created by CMake with CMAKE_EXPORT_COMPILE_COMMANDS
#!/usr/bin/env python
import json
import os
import subprocess
import sys
def run(cmd):
string_cmd = u" ".join(cmd)
subprocess.call(string_cmd, shell=True)
def find_here_or_above(path, fname):
rpath = os.path.realpath(path)
if os.path.isfile(os.path.join(rpath, fname)):
return os.path.join(rpath, fname)
else:
spath = os.path.split(rpath)
if spath[1] == '': # already reached root directory
return None
else:
return find_here_or_above(spath[0], fname)
def run_iwyu(wd, args):
os.chdir(wd)
cmd = basecmd[:]
cmd.extend(args)
run(cmd)
if __name__ == '__main__':
fname = find_here_or_above(os.path.curdir, 'compile_commands.json')
if not fname:
sys.exit()
f = open(fname, 'r')
tunits = json.load(f)
llvmdir = \
subprocess.Popen(
'llvm-config --src-root',
shell=True, stdout=subprocess.PIPE).communicate()[0].strip()
srcdir = os.path.join(
llvmdir, 'tools', 'clang', 'tools', 'include-what-you-use', '')
mapping_files = ["gcc.libc.imp", "gcc.stl.headers.imp", "gcc.symbols.imp",
"third_party.imp"]
basedir = os.path.join(llvmdir, 'Release+Asserts', '')
# basecmd = [basedir + u"bin/include-what-you-use", u"-Xiwyu",
# u"--verbose=2"]
basecmd = [u"include-what-you-use", u"-Xiwyu",
u"--verbose=2"]
for mapping in mapping_files:
basecmd.extend(["-Xiwyu", "--mapping_file=" + srcdir + mapping])
d = {}
for tu in tunits:
d[tu['file']] = {'directory' : tu['directory'],
'command': tu['command']}
j = d[os.path.realpath(os.path.join(os.path.curdir, sys.argv[1]))]
old_driver = j['command'].split()[0]
cmd = j['command'].replace(old_driver, u'', 1)
run_iwyu(j['directory'], [cmd])
# for tu in tunits:
# oldDriver = tu["command"].split(None)[0]
# cmd = tu["command"].replace(oldDriver, u"")
# runIWYU(tu["directory"], [cmd])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment