diff options
author | Miklos Vajna <vmiklos@collabora.com> | 2019-10-04 21:08:20 +0200 |
---|---|---|
committer | Kim Gräsman <kim.grasman@gmail.com> | 2019-10-06 10:46:19 +0200 |
commit | 38dba882612bfb305d7ac5cce83e1e85f2fcb5e9 (patch) | |
tree | a2682c07429935a6e3fe434d83e190126cfb3464 | |
parent | 3776ea410e00e83e4d7d29166efc8e23e5bf20a3 (diff) |
[iwyu_tool] Use directory from compilation database if availabl
If the entry looks like this:
{
"arguments": [
....
],
"directory": "/path/to/test",
"file": "Test.cpp"
},
And we invoked iwyu_tool.py in /path/to, then the constructed abs path was
/path/to/Test.cpp, not /path/to/test/Test.cpp, fix this.
-rwxr-xr-x | iwyu_tool.py | 4 | ||||
-rwxr-xr-x | iwyu_tool_test.py | 16 |
2 files changed, 19 insertions, 1 deletions
diff --git a/iwyu_tool.py b/iwyu_tool.py index 94f2fdb..c2571d9 100755 --- a/iwyu_tool.py +++ b/iwyu_tool.py @@ -298,6 +298,10 @@ class Invocation(object): def fixup_compilation_db(compilation_db): """ Canonicalize paths in JSON compilation database. """ for entry in compilation_db: + # Convert relative paths to absolute ones if possible, based on the entry's directory. + if 'directory' in entry and not os.path.isabs(entry['file']): + entry['file'] = os.path.join(entry['directory'], entry['file']) + # Expand relative paths and symlinks entry['file'] = os.path.realpath(entry['file']) diff --git a/iwyu_tool_test.py b/iwyu_tool_test.py index 2d2f0a9..5979925 100755 --- a/iwyu_tool_test.py +++ b/iwyu_tool_test.py @@ -281,7 +281,6 @@ class CompilationDBTests(unittest.TestCase): """ Compilation database path canonicalization. """ compilation_db = [ { - "directory": "/home/user/llvm/build/test", "file": "Test.cpp" } ] @@ -292,6 +291,21 @@ class CompilationDBTests(unittest.TestCase): entry = canonical[0] self.assertEqual(os.path.join(self.cwd, 'Test.cpp'), entry['file']) + def test_fixup_from_entry_dir(self): + """ Compilation database abs path is based on an entry's directory. """ + compilation_db = [ + { + "directory": "/home/user/foobar", + "file": "Test.cpp" + } + ] + + canonical = iwyu_tool.fixup_compilation_db(compilation_db) + + # Check that the file path is relative to the directory entry, not to the current directory. + entry = canonical[0] + self.assertEqual('/home/user/foobar/Test.cpp', entry['file']) + if __name__ == '__main__': unittest.main() |