Skip to content

Commit 40b9131

Browse files
committed
refresh.template.py: changed the _convert_compile_commands to return a list of SimpleNamespace objects instead of yielding a dictionary & Removal type cast
1 parent 9ce1716 commit 40b9131

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

refresh.template.py

+11-16
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ def _convert_compile_commands(aquery_output, focused_on_file: str = None):
810810
"""Converts from Bazel's aquery format to de-Bazeled compile_commands.json entries.
811811
812812
Input: jsonproto output from aquery, pre-filtered to (Objective-)C(++) compile actions for a given build.
813-
Yields: Corresponding entries for a compile_commands.json, with commas after each entry, describing all ways every file is being compiled.
813+
Returns: Corresponding entries for a compile_commands.json, with commas after each entry, describing all ways every file is being compiled.
814814
Also includes one entry per header, describing one way it is compiled (to work around https://github.com/clangd/clangd/issues/123).
815815
816816
Crucially, this de-Bazels the compile commands it takes as input, leaving something clangd can understand. The result is a command that could be run from the workspace root directly, with no bazel-specific environment variables, etc.
@@ -857,7 +857,8 @@ def _convert_compile_commands(aquery_output, focused_on_file: str = None):
857857
If that's a source file, please report this. We should work to improve the performance.
858858
If that's a header file, we should probably do the same, but it may be unavoidable.""")
859859

860-
# Yield as compile_commands.json entries
860+
# Return as compile_commands.json entries
861+
output_list = []
861862
header_files_already_written = set()
862863
for source_files, header_files, compile_command_args in outputs:
863864
# Only emit one entry per header
@@ -871,14 +872,8 @@ def _convert_compile_commands(aquery_output, focused_on_file: str = None):
871872

872873
for file in itertools.chain(source_files, header_files_not_already_written):
873874
if file == 'external/bazel_tools/src/tools/launcher/dummy.cc': continue # Suppress Bazel internal files leaking through. Hopefully will prevent issues like https://github.com/hedronvision/bazel-compile-commands-extractor/issues/77
874-
yield { # TODO for consistency, let's have this return a list if its parent is returning a list. That'd also let us strip the list() cast in the found = True block, below
875-
# Docs about compile_commands.json format: https://clang.llvm.org/docs/JSONCompilationDatabase.html#format
876-
'file': file,
877-
# Using `arguments' instead of 'command' because it's now preferred by clangd. Heads also that shlex.join doesn't work for windows cmd, so you'd need to use windows_list2cmdline if we ever switched back. For more, see https://github.com/hedronvision/bazel-compile-commands-extractor/issues/8#issuecomment-1090262263
878-
'arguments': compile_command_args,
879-
# Bazel gotcha warning: If you were tempted to use `bazel info execution_root` as the build working directory for compile_commands...search ImplementationReadme.md to learn why that breaks.
880-
'directory': os.environ["BUILD_WORKSPACE_DIRECTORY"],
881-
}
875+
output_list.append(types.SimpleNamespace(file=file, arguments=compile_command_args, directory=os.environ["BUILD_WORKSPACE_DIRECTORY"]))
876+
return output_list
882877

883878

884879
def _get_compile_commands_for_aquery(aquery_target_statement: str, additional_aquery_flags: typing.List[str], focused_on_file: str = None):
@@ -1011,10 +1006,10 @@ def _get_commands(target: str, flags: str):
10111006

10121007
found = False
10131008
for target_statement in target_statement_candidates:
1014-
commands = list(_get_compile_commands_for_aquery(target_statement, additional_flags, file_path))
1009+
commands = _get_compile_commands_for_aquery(target_statement, additional_flags, file_path)
10151010
compile_commands.extend(commands) # If we did the work to generate a command, we'll update it, whether it's for the requested file or not.
10161011
if file_flags:
1017-
if any(command['file'].endswith(file_path) for command in commands):
1012+
if any(command.file.endswith(file_path) for command in commands):
10181013
found = True
10191014
break
10201015
log_info(f""">>> Couldn't quickly find a compile command for {file_path} in {target} under {target_statement}
@@ -1176,16 +1171,16 @@ def _ensure_cwd_is_workspace_root():
11761171
previous_compile_command_entries = []
11771172
try:
11781173
with open('compile_commands.json') as compile_commands_file:
1179-
previous_compile_command_entries = json.load(compile_commands_file)
1174+
previous_compile_command_entries = [types.SimpleNamespace(**d) for d in json.load(compile_commands_file)]
11801175
except:
11811176
log_warning(">>> Couldn't read previous compile_commands.json. Overwriting instead of merging...")
11821177
else:
1183-
updated_files = set(entry['file'] for entry in compile_command_entries)
1184-
compile_command_entries += [entry for entry in previous_compile_command_entries if entry['file'] not in updated_files]
1178+
updated_files = set(entry.file for entry in compile_command_entries)
1179+
compile_command_entries += [entry for entry in previous_compile_command_entries if entry.file not in updated_files]
11851180

11861181
with open('compile_commands.json', 'w') as output_file:
11871182
json.dump(
1188-
compile_command_entries,
1183+
[vars(entry) for entry in compile_command_entries],
11891184
output_file,
11901185
indent=2, # Yay, human readability!
11911186
check_circular=False # For speed.

0 commit comments

Comments
 (0)