Skip to content

Commit

Permalink
Support 'arguments' key in compilation databases
Browse files Browse the repository at this point in the history
Move things around a little to make this easier to implement -- run_iwyu
now translates from a compilation database entry to a command-line.

This should fix issue #456.
  • Loading branch information
Kim Gräsman authored and kimgr committed Sep 18, 2017
1 parent 37670dd commit fbcc7e3
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions iwyu_tool.py
Expand Up @@ -96,18 +96,30 @@ def get_output(cwd, command):
return process.communicate()[0].decode("utf-8").splitlines()


def run_iwyu(cwd, compile_command, iwyu_args, verbose):
def run_iwyu(dbentry, iwyu_args, verbose):
""" Rewrite compile_command to an IWYU command, and run it. """
compiler, _, args = compile_command.partition(' ')
if compiler.endswith('cl.exe'):
cwd = dbentry['directory']

if 'arguments' in dbentry:
# arguments is a command-line in list form
arguments = dbentry['arguments']
compile_command, compile_args = arguments[0], arguments[1:]
compile_args = ' '.join(compile_args)
elif 'command' in dbentry:
# command is a command-line in string form
compile_command, _, compile_args = dbentry['command'].partition(' ')
else:
raise ValueError('Invalid compilation database entry: %s' % dbentry)

if compile_command.endswith('cl.exe'):
# If the compiler name is cl.exe, let IWYU be cl-compatible
clang_args = ['--driver-mode=cl']
else:
clang_args = []

iwyu_args = ['-Xiwyu ' + a for a in iwyu_args]
command = ['include-what-you-use'] + clang_args + iwyu_args
command = '%s %s' % (' '.join(command), args.strip())
command = '%s %s' % (' '.join(command), compile_args.strip())

if verbose:
print('%s:' % command)
Expand Down Expand Up @@ -160,9 +172,8 @@ def main(compilation_db_path, source_files, verbose, formatter, jobs, iwyu_args)
# Details here: https://stackoverflow.com/a/28660669.
results = []
for entry in entries:
cwd, compile_command = entry['directory'], entry['command']
results.append(pool.apply_async(run_iwyu,
(cwd, compile_command, iwyu_args, verbose),
(entry, iwyu_args, verbose),
callback=formatter))
pool.close()
pool.join()
Expand Down

0 comments on commit fbcc7e3

Please sign in to comment.