Skip to content

Commit

Permalink
Use sarif-stderr instead of sarif-file for gcc's output
Browse files Browse the repository at this point in the history
  • Loading branch information
Szelethus committed Nov 8, 2023
1 parent 82f6f66 commit 61d46a0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
2 changes: 1 addition & 1 deletion analyzer/codechecker_analyzer/analyzers/gcc/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def construct_analyzer_cmd(self, result_handler):

analyzer_cmd.extend(self.buildaction.analyzer_options)

analyzer_cmd.append('-fdiagnostics-format=sarif-file')
analyzer_cmd.append('-fdiagnostics-format=sarif-stderr')

for checker_name, value in config.checks().items():
if value[0] == CheckerState.disabled:
Expand Down
45 changes: 23 additions & 22 deletions analyzer/codechecker_analyzer/analyzers/gcc/result_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,35 @@ def postprocess_result(self, skip_handlers: Optional[SkipListHandlers]):
into the database.
"""
LOG.debug_analyzer(self.analyzer_stdout)
gcc_stderr = self.analyzer_stderr
assert gcc_stderr, "Even in the event of no reported diagnostics, " \
"stderr mustn't be empty!"

# GCC places sarif files to the "directory" entry found in the
# compilation database. As of writing this comment, there is no way to
# tell GCC to place is elsewhere, so we need to find it, move it and
# rename it.
file_name = os.path.basename(self.analyzed_source_file)
gcc_output_file = \
str(Path(self.buildaction.directory, file_name)) + ".sarif"
# report-converter needs a file to parse, let's dump the content of
# stderr to one.
gcc_out_folder = Path(self.workspace, "gcc")
gcc_out_folder.mkdir(exist_ok=True)
gcc_dest_file_name = \
str(Path(gcc_out_folder,
os.path.basename(self.analyzed_source_file) +
self.buildaction_hash + ".sarif"))

assert os.path.exists(gcc_output_file), \
"Faile to find the sarif file for GCC analysis!"
assert not os.path.exists(gcc_dest_file_name)
with open(gcc_dest_file_name, 'w') as f:
f.write(gcc_stderr)
assert os.path.exists(gcc_dest_file_name)

reports = report_file.get_reports(
gcc_output_file, self.checker_labels,
gcc_dest_file_name, self.checker_labels,
source_dir_path=self.source_dir_path)

# If we were to leave sarif files in the repoort directory, we would
# unintentionally parse them, so we rename them.
try:
shutil.move(gcc_dest_file_name, gcc_dest_file_name + '.bak')
except(OSError) as e:
LOG.error(e)

reports = [r for r in reports if not r.skip(skip_handlers)]
for report in reports:
report.checker_name = \
Expand All @@ -92,15 +105,3 @@ def postprocess_result(self, skip_handlers: Optional[SkipListHandlers]):
report_file.create(
self.analyzer_result_file, reports, self.checker_labels,
self.analyzer_info)

# TODO Maybe move this to post_analyze?
gcc_out_folder = Path(self.workspace, "gcc")
gcc_out_folder.mkdir(exist_ok=True)
gcc_dest_file_name = \
Path(gcc_out_folder, os.path.basename(self.analyzed_source_file) +
self.buildaction_hash + ".sarif.bak")
try:
shutil.move(gcc_output_file, gcc_dest_file_name)
except(OSError) as e:
LOG.error(f"Failed to move '{gcc_output_file}' to "
f"'{gcc_out_folder}': {e}")

0 comments on commit 61d46a0

Please sign in to comment.