Skip to content

Commit

Permalink
final
Browse files Browse the repository at this point in the history
  • Loading branch information
Szelethus committed Oct 4, 2023
1 parent c9db90c commit 8a0b0de
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


class AnalyzerResult(AnalyzerResultBase):
""" Transform analyzer result of the FB Infer. """
""" Transform analyzer result of the GCC Static Analyzer. """

TOOL_NAME = 'gcc'
NAME = 'GNU Compiler Collection Static Analyzer'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,27 @@ def load_json(path: str):
return ret


def get_tool_info() -> Tuple[str, str]:
""" Get tool info.
If this was called through CodeChecker, this function will return
CodeChecker information, otherwise this tool (report-converter)
information.
"""
data_files_dir_path = os.environ.get('CC_DATA_FILES_DIR')
if data_files_dir_path:
analyzer_version_file_path = os.path.join(
data_files_dir_path, 'config', 'analyzer_version.json')
if os.path.exists(analyzer_version_file_path):
data = load_json(analyzer_version_file_path)
version = data.get('version')
if version:
return 'CodeChecker', f"{version['major']}." \
f"{version['minor']}.{version['revision']}"

return __title__, __version__


class AnalyzerInfo:
""" Hold information about the analyzer. """
def __init__(self, name: str):
Expand All @@ -73,26 +94,6 @@ def get_severity(self, checker_name: str) -> Optional[str]:
return self._checker_labels.severity(checker_name)
return None

def get_tool_info(self) -> Tuple[str, str]:
""" Get tool info.
If this was called through CodeChecker, this function will return
CodeChecker information, otherwise this tool (report-converter)
information.
"""
data_files_dir_path = os.environ.get('CC_DATA_FILES_DIR')
if data_files_dir_path:
analyzer_version_file_path = os.path.join(
data_files_dir_path, 'config', 'analyzer_version.json')
if os.path.exists(analyzer_version_file_path):
data = load_json(analyzer_version_file_path)
version = data.get('version')
if version:
return 'CodeChecker', f"{version['major']}." \
f"{version['minor']}.{version['revision']}"

return __title__, __version__

@abstractmethod
def get_reports(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
BugPathPosition, File, get_or_create_file, MacroExpansion, Range, Report
from codechecker_report_converter.report.hash import get_report_hash, HashType
from codechecker_report_converter.report.parser.base import AnalyzerInfo, \
BaseParser
BaseParser, get_tool_info


LOG = logging.getLogger('report-converter')
Expand Down Expand Up @@ -426,7 +426,7 @@ def convert(
analyzer_info: Optional[AnalyzerInfo] = None
):
""" Converts the given reports. """
tool_name, tool_version = self.get_tool_info()
tool_name, tool_version = get_tool_info()

data: Dict[str, Any] = {
'files': [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
BugPathPosition, MacroExpansion, File, get_or_create_file, Range, Report
from codechecker_report_converter.report.hash import get_report_hash, HashType
from codechecker_report_converter.report.parser.base import AnalyzerInfo, \
BaseParser, load_json
BaseParser, load_json, get_tool_info


EXTENSION = 'sarif'
Expand All @@ -30,7 +30,7 @@
LOG = logging.getLogger('report-converter')


# $3.37
# §3.37
class ThreadFlowInfo:
def __init__(self):
self.bug_path_events: List[BugPathEvent] = []
Expand All @@ -39,6 +39,12 @@ def __init__(self):
self.macro_expansions: List[MacroExpansion] = []


# Parse to/from sarif formats.
# Sarif has an extensive documentation, and supports a grand number of fields
# that CodeChecker has no use for as of writing this comment (e.g. §3.39 graph,
# §3.30 region), leading to these fields to be ignored.
# This is NOT (and will likely never be) a full fledged sarif parser, only what
# we need from it at the time.
class Parser(BaseParser):
def get_reports(
self,
Expand Down Expand Up @@ -110,7 +116,7 @@ def has_any_runs(self, result_file_path: str) -> bool:
check it.
"""
ret = load_json(result_file_path)
return ret and ret["runs"] is not None
return ret and ret["runs"]

def _get_rules(self, data: Dict) -> Dict[str, Dict]:
""" """
Expand Down Expand Up @@ -198,24 +204,24 @@ def _resolve_uri_base_id(self, uri_base_id: str, postfix: str):
absolute path of the file.
"""
error_str = \
f"Failed to assemble the absolute path for '{postfix}'. sarif " \
f"Failed to fully resolve the path for '{postfix}'. sarif " \
"v2.1.0 permits this, but CodeChecker requires it to be " \
"available. If you intend to hide the absolute path before " \
"storing the results to a CodeChecker server, use " \
"--trim-path-prefix."

if not self.original_uri_base_ids:
LOG.warning("Missing 'originalUriBaseIds' (sarif v2.1.0 §3.14.14) "
f"in '{self.result_file_path}'.")
LOG.error("Missing 'originalUriBaseIds' (sarif v2.1.0 §3.14.14) "
f"in '{self.result_file_path}'.")
LOG.error(error_str)
self.had_error = True
return ""

original = self.original_uri_base_ids.get(uri_base_id)
if not original:
LOG.warning(f"Missing entry for '{uri_base_id} in"
"'originalUriBaseIds' (sarif v2.1.0 §3.14.14)"
f"in '{self.result_file_path}'.")
LOG.error(f"Missing entry for '{uri_base_id} in"
"'originalUriBaseIds' (sarif v2.1.0 §3.14.14)"
f"in '{self.result_file_path}'.")
LOG.error(error_str)
self.had_error = True
return ""
Expand Down Expand Up @@ -281,7 +287,7 @@ def convert(
analyzer_info: Optional[AnalyzerInfo] = None
):
""" Converts the given reports to sarif format. """
tool_name, tool_version = self.get_tool_info()
tool_name, tool_version = get_tool_info()

rules = {}
results = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
LOG = logging.getLogger('report-converter')


SUPPORTED_ANALYZER_FORMATS = [plist, sarif]

SUPPORTED_ANALYZER_EXTENSIONS = tuple([plist.EXTENSION, sarif.EXTENSION])


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ def setup_class(cls):
cls.__sarif_test_files = os.path.join(
cls.__this_dir, 'sarif_test_files')

def test_no_bug_file(self):
def test_no_crash(self):
"""There was no bug in the checked file."""
for root, _, files in os.walk(self.__sarif_test_files):
for filename in files:
if not filename.endswith(".sarif"):
continue
abs_filename = os.path.join(root, filename)
# We test for no crashes.
print(abs_filename)
report_file.get_reports(abs_filename)

0 comments on commit 8a0b0de

Please sign in to comment.