Skip to content

Commit 838ac87

Browse files
committed
[compilation_database.py] add compilation_database.py
1 parent f0edede commit 838ac87

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import clang.cindex as clang
2+
3+
4+
class CompilationDatabase:
5+
"""
6+
Build a compilation database from a given directory
7+
"""
8+
9+
def __init__(self, compilation_database_path):
10+
self.compilation_database = clang.CompilationDatabase.fromDirectory(
11+
buildDir=compilation_database_path
12+
)
13+
14+
def get_compilation_arguments(self, filename=None):
15+
"""
16+
Returns the compilation commands extracted from the compilation database
17+
18+
Parameters:
19+
- compilation_database_path: The path to `compile_commands.json`
20+
- filename (optional): To get compilaton commands of a file
21+
22+
Returns:
23+
- compilation_arguments (dict): {filename: compiler arguments}
24+
"""
25+
26+
if filename:
27+
# Get compilation commands from the compilation database for the given file
28+
compilation_commands = self.compilation_database.getCompileCommands(
29+
filename=filename
30+
)
31+
else:
32+
# Get all compilation commands from the compilation database
33+
compilation_commands = self.compilation_database.getAllCompileCommands()
34+
35+
# {file: compiler arguments}
36+
compilation_arguments = {
37+
command.filename: list(command.arguments)[1:-1]
38+
for command in compilation_commands
39+
}
40+
return compilation_arguments

0 commit comments

Comments
 (0)