Skip to content

Commit 53548df

Browse files
committed
json: add support for 'output' argument
1 parent 53ae9ee commit 53548df

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

compdb/backend/json.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ def _dict_to_compile_command(d):
4343
# PERFORMANCE: I think shlex is inherently slow,
4444
# something performing better may be necessary
4545
arguments = shlex.split(d['command'])
46-
return CompileCommand(d['directory'], d['file'], arguments)
46+
return CompileCommand(d['directory'], d['file'], arguments,
47+
d.get('output'))
4748

4849
@property
4950
def _data(self):
@@ -75,14 +76,18 @@ def str_to_json(s):
7576

7677

7778
def compile_command_to_json(compile_command):
79+
output_str = ""
80+
if compile_command.output:
81+
output_str = ',\n "output": {}'.format(
82+
str_to_json(compile_command.output))
7883
return r'''{{
7984
"directory": {},
8085
"command": {},
81-
"file": {}
86+
"file": {}{}
8287
}}'''.format(
8388
str_to_json(compile_command.directory),
8489
arguments_to_json(compile_command.arguments),
85-
str_to_json(compile_command.file))
90+
str_to_json(compile_command.file), output_str)
8691

8792

8893
class JSONCompileCommandSerializer(object):

compdb/models.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,26 @@ def __init__(self, message, cause=None):
1515

1616

1717
class CompileCommand:
18-
def __init__(self, directory, file, arguments):
18+
def __init__(self, directory, file, arguments, output=None):
1919
self.directory = directory
2020
self.file = file
2121
self.arguments = arguments
22+
self.output = output
2223

2324
@property
2425
def normfile(self):
2526
return os.path.normpath(os.path.join(self.directory, self.file))
2627

2728
def __repr__(self):
28-
return "{{directory: {},\nfile: {},\n arguments: {}}}\n\n".format(
29-
self.directory, self.file, pprint.pformat(self.arguments))
29+
return "{{directory: {}, file: {}, arguments: {}, output: {}}}".format(
30+
repr(self.directory),
31+
repr(self.file), pprint.pformat(self.arguments), repr(self.output))
3032

3133
def __str__(self):
3234
return self.__repr__()
3335

3436
def _as_tuple(self):
35-
return (self.directory, self.file, self.arguments)
37+
return (self.directory, self.file, self.arguments, self.output)
3638

3739
def __eq__(self, other):
3840
if isinstance(other, self.__class__):

tests/unit/backend/test_json.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ def test_get_compile_commands(self):
3131
self.assertEqual(b_commands[1],
3232
CompileCommand("/tmp/", "/tmp/b.cpp",
3333
["clang", "-DB=2"]))
34+
c_commands = list(self.db.get_compile_commands("/tmp/c.cpp"))
35+
self.assertEqual(len(c_commands), 1)
36+
self.assertEqual(c_commands[0],
37+
CompileCommand("/tmp/", "/tmp/c.cpp",
38+
["clang", "-DC=1"], "c.o"))
3439

3540
def test_get_all_files(self):
3641
files = list(sorted(self.db.get_all_files()))
@@ -41,4 +46,5 @@ def test_get_all_files(self):
4146
'/tmp/b.cpp',
4247
# note: it's debatable whether duplicates should be present
4348
'/tmp/b.cpp',
49+
'/tmp/c.cpp',
4450
])

tests/unit/backend/test_json_data/compile_commands.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,11 @@
1313
"directory": "/tmp/",
1414
"arguments": [ "clang", "-DB=2" ],
1515
"file": "/tmp/b.cpp"
16+
},
17+
{
18+
"directory": "/tmp/",
19+
"arguments": [ "clang", "-DC=1" ],
20+
"file": "/tmp/c.cpp",
21+
"output": "c.o"
1622
}
1723
]

tests/unit/test_dump.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
COMPILE_COMMANDS_TO_JSON_DATA = ([
3636
CompileCommand("/tmp", "foo.cpp", ["clang++"]),
3737
CompileCommand("/tmp/bar", "bar.cpp", ["clang++", "-std=c++11"]),
38+
CompileCommand("/tmp/foo", "foo.cpp", ["clang++", "-std=c++11"], "foo.o"),
3839
], r"""[
3940
{
4041
"directory": "/tmp",
@@ -46,6 +47,13 @@
4647
"directory": "/tmp/bar",
4748
"command": "clang++ -std=c++11",
4849
"file": "bar.cpp"
50+
},
51+
52+
{
53+
"directory": "/tmp/foo",
54+
"command": "clang++ -std=c++11",
55+
"file": "foo.cpp",
56+
"output": "foo.o"
4957
}
5058
]
5159
""")

0 commit comments

Comments
 (0)