Skip to content

Instantly share code, notes, and snippets.

@rpavlik
Created November 20, 2012 21:12
Show Gist options
  • Save rpavlik/4121171 to your computer and use it in GitHub Desktop.
Save rpavlik/4121171 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 subprocess
import sys
def run(cmd):
# print(cmd)
stringCmd = u" ".join(cmd)
subprocess.call(stringCmd, shell=True)
def runIWYUGDB(wd, args):
cmd =[u"gdb", u"--args"]
cmd.extend(basecmd)
cmd.extend(args)
run(cmd)
def runIWYU(wd, args):
cmd = basecmd[:]
cmd.extend(args)
run(cmd)
f = open("compile_commands.json", "r")
tunits = json.load(f)
extraArg = u" ".join([u"-Xiwyu " + x for x in sys.argv[1:]])
basedir = u"/home/rpavlik/llvm-trunk/"
basecmd = [basedir + u"bin/include-what-you-use", u"-Xiwyu", u"--verbose=2", extraArg] # "-working-directory", wd]
srcdir = "/home/rpavlik/src/third-party/llvm/tools/clang/tools/include-what-you-use/"
mappingFiles = ["gcc.libc.imp", "gcc.stl.headers.imp", "gcc.symbols.imp", "google.imp", "third_party.imp"] # "iwyu.gcc.imp"
for mapping in mappingFiles:
basecmd.extend(["-Xiwyu", "--mapping_file="+srcdir+mapping])
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