Skip to content

Commit 6f4c13e

Browse files
committed
update compilation db logic
1 parent 838ac87 commit 6f4c13e

File tree

2 files changed

+16
-37
lines changed

2 files changed

+16
-37
lines changed

bindings/python/scripts/parse.py

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def __init__(self, file, compiler_arguments):
3333
}
3434

3535
@staticmethod
36-
def is_valid_child(parent_node, child_node):
36+
def _is_valid_child(parent_node, child_node):
3737
child = child_node.get("cursor")
3838
parent_filename = parent_node.get("filename")
3939

@@ -117,7 +117,7 @@ def parse_node_recursive(cls, node):
117117
# Get cursor's children and recursively add their info to a dictionary, as members of the parent
118118
for child in cursor.get_children():
119119
child_node = {"cursor": child, "filename": filename, "depth": depth + 1}
120-
if cls.is_valid_child(node, child_node):
120+
if cls._is_valid_child(node, child_node):
121121
child_parsed_info = cls.parse_node_recursive(child_node)
122122
parsed_info["members"].append(child_parsed_info)
123123

@@ -133,30 +133,3 @@ def get_parsed_info(self):
133133
- The key 'members' contains the node's children's `parsed_info`
134134
"""
135135
return self.parse_node_recursive(self.root_node)
136-
137-
# @TODO: Move the function out of this file in a separate PR
138-
@staticmethod
139-
def get_compilation_arguments(compilation_database_path, filename):
140-
"""
141-
Yields the compilation commands extracted from the compilation database
142-
143-
Parameters:
144-
- compilation_database_path: The path to `compile_commands.json`
145-
- filename: The file's name to get its compilation commands
146-
147-
Yields:
148-
- compilation commands (list): The arguments passed to the compiler
149-
"""
150-
151-
# Build a compilation database found in the given directory
152-
compilation_database = clang.CompilationDatabase.fromDirectory(
153-
buildDir=compilation_database_path
154-
)
155-
# Get compilation commands from the compilation database for the given file
156-
compilation_commands = compilation_database.getCompileCommands(
157-
filename=filename
158-
)
159-
160-
for compilation_command in compilation_commands:
161-
# Extract compiler arguments, excluding compiler and filename
162-
yield list(compilation_command.arguments)[1:-1]

bindings/python/tests/test_parse.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from context import scripts
22
import clang.cindex as clang
33
from scripts.parse import Parse
4+
from scripts.compilation_database import CompilationDatabase
45

56

6-
def create_compilation_database(tmp_path, filepath):
7+
def get_compilation_database_path(tmp_path, filepath):
78
input = tmp_path / "compile_commands.json"
89
x = [
910
{
@@ -25,15 +26,20 @@ def get_parsed_info(tmp_path, file_contents):
2526
with open(source_path, "w") as f:
2627
f.write(str(file_contents))
2728

28-
compiler_arguments = next(
29-
Parse.get_compilation_arguments(
30-
compilation_database_path=create_compilation_database(
31-
tmp_path=tmp_path, filepath=source_path
32-
),
33-
filename=source_path,
34-
)
29+
compilation_database_path = get_compilation_database_path(
30+
tmp_path=tmp_path, filepath=source_path
3531
)
3632

33+
compilation_database = CompilationDatabase(
34+
compilation_database_path=compilation_database_path
35+
)
36+
37+
compilation_arguments = compilation_database.get_compilation_arguments(
38+
filename=source_path
39+
)
40+
41+
compiler_arguments = compilation_arguments.get(source_path)
42+
3743
parsed_info = Parse(source_path, compiler_arguments).get_parsed_info()
3844

3945
return parsed_info

0 commit comments

Comments
 (0)