-
Notifications
You must be signed in to change notification settings - Fork 388
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4335 from noraz31/clang_tidy_yaml
Add clang-tidy yaml report converter
- Loading branch information
Showing
29 changed files
with
1,775 additions
and
9 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
tools/report-converter/codechecker_report_converter/analyzers/clang_tidy_yaml/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# ------------------------------------------------------------------------- | ||
# | ||
# Part of the CodeChecker project, under the Apache License v2.0 with | ||
# LLVM Exceptions. See LICENSE for license information. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
# | ||
# ------------------------------------------------------------------------- |
26 changes: 26 additions & 0 deletions
26
...eport-converter/codechecker_report_converter/analyzers/clang_tidy_yaml/analyzer_result.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# ------------------------------------------------------------------------- | ||
# | ||
# Part of the CodeChecker project, under the Apache License v2.0 with | ||
# LLVM Exceptions. See LICENSE for license information. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
# | ||
# ------------------------------------------------------------------------- | ||
|
||
from typing import List | ||
|
||
from codechecker_report_converter.report import Report | ||
|
||
from ..analyzer_result import AnalyzerResultBase | ||
from .parser import Parser | ||
|
||
|
||
class AnalyzerResult(AnalyzerResultBase): | ||
""" Transform analyzer result of Clang Tidy. """ | ||
|
||
TOOL_NAME = 'clang-tidy-yaml' | ||
NAME = 'Clang Tidy' | ||
URL = 'https://clang.llvm.org/extra/clang-tidy' | ||
|
||
def get_reports(self, file_path: str) -> List[Report]: | ||
""" Get reports from the given analyzer result. """ | ||
return Parser().get_reports(file_path) |
125 changes: 125 additions & 0 deletions
125
tools/report-converter/codechecker_report_converter/analyzers/clang_tidy_yaml/parser.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# ------------------------------------------------------------------------- | ||
# | ||
# Part of the CodeChecker project, under the Apache License v2.0 with | ||
# LLVM Exceptions. See LICENSE for license information. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
# | ||
# ------------------------------------------------------------------------- | ||
|
||
import os | ||
import yaml | ||
import logging | ||
from typing import Iterator, List, Tuple | ||
|
||
from codechecker_report_converter.report import BugPathEvent, \ | ||
get_or_create_file, Report | ||
|
||
from ..parser import BaseParser | ||
|
||
LOG = logging.getLogger('report-converter') | ||
|
||
|
||
def get_location_by_offset(filename, offset): | ||
""" | ||
This function returns the line and column number in the given file which | ||
is located at the given offset (i.e. number of characters including new | ||
line characters). None returns when the offset is greater than the file | ||
length. | ||
""" | ||
with open(filename, encoding='utf-8', errors='ignore', newline='') as f: | ||
for row, line in enumerate(f, 1): | ||
length = len(line) | ||
if length < offset: | ||
offset -= length | ||
else: | ||
return row, offset + 1 | ||
|
||
return None | ||
|
||
|
||
class Parser(BaseParser): | ||
"""Parser for clang-tidy YAML output.""" | ||
|
||
def get_reports(self, file_path: str) -> List[Report]: | ||
"""Parse Clang-Tidy's YAML output file.""" | ||
with open(file_path, 'r', encoding='utf-8') as file: | ||
data = yaml.safe_load(file) | ||
|
||
reports = [] | ||
if data: | ||
for diagnostic in data['Diagnostics']: | ||
report = self._parse_diagnostic(diagnostic) | ||
reports.append(report) | ||
return reports | ||
|
||
def _parse_diagnostic(self, diagnostic: dict) -> Report: | ||
"""Parse a Clang-Tidy diagnostic.""" | ||
checker_name = diagnostic.get('DiagnosticName', '') | ||
diagnostic_message = diagnostic.get('DiagnosticMessage', {}) | ||
file_path = os.path.abspath(diagnostic_message['FilePath']) | ||
file_obj = get_or_create_file(file_path, self._file_cache) | ||
line, col = get_location_by_offset( | ||
file_path, diagnostic_message.get('FileOffset')) | ||
|
||
report = Report( | ||
file=file_obj, | ||
line=line, | ||
column=col, | ||
message=diagnostic_message.get('Message', '').strip(), | ||
checker_name=checker_name, | ||
category=self._get_category(checker_name), | ||
bug_path_events=[] | ||
) | ||
|
||
# Parse replacements (fixits) (if any) | ||
for replacement in diagnostic_message.get('Replacements', []): | ||
replacement_path = os.path.abspath(replacement['FilePath']) | ||
replacement_file_obj = get_or_create_file(replacement_path, | ||
self._file_cache) | ||
fixit_line, fixit_col = get_location_by_offset( | ||
replacement_path, replacement['Offset']) | ||
report.notes.append( | ||
BugPathEvent( | ||
f"{replacement['ReplacementText']} (fixit)", | ||
replacement_file_obj, | ||
fixit_line, | ||
fixit_col | ||
) | ||
) | ||
|
||
# Parse notes (if any) | ||
for note in diagnostic.get('Notes', []): | ||
if note['FilePath'] != '': | ||
note_path = os.path.abspath(note['FilePath']) | ||
note_line, note_col = get_location_by_offset( | ||
note_path, note['FileOffset']) | ||
note_file_obj = get_or_create_file(note_path, self._file_cache) | ||
report.bug_path_events.append( | ||
BugPathEvent( | ||
note['Message'].strip(), | ||
note_file_obj, | ||
note_line, | ||
note_col | ||
) | ||
) | ||
|
||
report.bug_path_events.append(BugPathEvent( | ||
report.message, | ||
report.file, | ||
report.line, | ||
report.column)) | ||
|
||
return report | ||
|
||
def _get_category(self, checker_name: str) -> str: | ||
""" Get category for Clang-Tidy checker. """ | ||
parts = checker_name.split('-') | ||
return parts[0] if parts else 'unknown' | ||
|
||
def _parse_line(self, it: Iterator[str], line: str) -> Tuple[ | ||
List[Report], str]: | ||
# FIXME: This method is a placeholder to allow instantiation of the | ||
# Parser class. | ||
# The _parse_line method is required because Parser is an abstract | ||
# class that expects this method to be implemented in subclasses. | ||
return [], "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ pylint==3.2.4 | |
portalocker==2.2.1 | ||
mypy==1.7.1 | ||
setuptools==70.2.0 | ||
PyYAML==6.0.1 | ||
types-PyYAML==6.0.12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
tools/report-converter/tests/unit/analyzers/test_clang_tidy_yaml_parser.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# ------------------------------------------------------------------------- | ||
# | ||
# Part of the CodeChecker project, under the Apache License v2.0 with | ||
# LLVM Exceptions. See LICENSE for license information. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
# | ||
# ------------------------------------------------------------------------- | ||
|
||
""" | ||
This module tests the correctness of the OutputParser and PListConverter, which | ||
used in sequence transform a Clang Tidy output file to a plist file. | ||
""" | ||
|
||
|
||
import os | ||
import plistlib | ||
import shutil | ||
import tempfile | ||
import unittest | ||
|
||
from codechecker_report_converter.analyzers.clang_tidy_yaml import ( | ||
analyzer_result) | ||
from codechecker_report_converter.report.parser import plist | ||
|
||
|
||
OLD_PWD = None | ||
|
||
|
||
def setup_module(): | ||
"""Setup the test tidy reprs for the test classes in the module.""" | ||
global OLD_PWD | ||
OLD_PWD = os.getcwd() | ||
os.chdir(os.path.join(os.path.dirname(__file__), | ||
'tidy_yaml_output_test_files')) | ||
|
||
|
||
def teardown_module(): | ||
"""Restore environment after tests have ran.""" | ||
global OLD_PWD | ||
os.chdir(OLD_PWD) | ||
|
||
|
||
class ClangTidyYamlAnalyzerResultTestCase(unittest.TestCase): | ||
""" Test the output of the ClangTidyYamlAnalyzerResult. """ | ||
|
||
def setUp(self): | ||
""" Setup the test. """ | ||
self.analyzer_result = analyzer_result.AnalyzerResult() | ||
self.cc_result_dir = tempfile.mkdtemp() | ||
|
||
def tearDown(self): | ||
""" Clean temporary directory. """ | ||
shutil.rmtree(self.cc_result_dir) | ||
|
||
def __check_analyzer_result(self, analyzer_result, analyzer_result_plist, | ||
source_files, expected_plist): | ||
""" Check the result of the analyzer transformation. """ | ||
self.analyzer_result.transform( | ||
[analyzer_result], self.cc_result_dir, plist.EXTENSION, | ||
file_name="{source_file}_{analyzer}") | ||
|
||
plist_file = os.path.join(self.cc_result_dir, analyzer_result_plist) | ||
with open(plist_file, mode='rb') as pfile: | ||
res = plistlib.load(pfile) | ||
|
||
# Use relative path for this test. | ||
res['files'] = source_files | ||
|
||
with open(expected_plist, mode='rb') as pfile: | ||
exp = plistlib.load(pfile) | ||
|
||
self.assertTrue(res['metadata']['generated_by']['version']) | ||
res['metadata']['generated_by']['version'] = "x.y.z" | ||
|
||
self.assertEqual(res, exp) | ||
|
||
def test_empty1(self): | ||
""" Test for empty Messages. """ | ||
ret = self.analyzer_result.transform( | ||
['empty1.out'], self.cc_result_dir, plist.EXTENSION) | ||
self.assertFalse(ret) | ||
|
||
def test_empty2(self): | ||
""" Test for empty Messages with multiple line. """ | ||
ret = self.analyzer_result.transform( | ||
['empty2.out'], self.cc_result_dir, plist.EXTENSION) | ||
self.assertFalse(ret) | ||
|
||
def test_tidy1(self): | ||
""" Test for the tidy1.plist file. """ | ||
self.__check_analyzer_result('tidy1.out', | ||
'test.cpp_clang-tidy-yaml.plist', | ||
['files/test.cpp'], 'tidy1.plist') | ||
|
||
def test_tidy2(self): | ||
""" Test for the tidy2.plist file. """ | ||
self.__check_analyzer_result('tidy2.out', | ||
'test2.cpp_clang-tidy-yaml.plist', | ||
['files/test2.cpp'], 'tidy2.plist') | ||
|
||
def test_tidy3(self): | ||
""" Test for the tidy3.plist file. """ | ||
self.__check_analyzer_result('tidy3.out', | ||
'test3.cpp_clang-tidy-yaml.plist', | ||
['files/test3.cpp'], | ||
'tidy3_cpp.plist') | ||
|
||
self.__check_analyzer_result('tidy3.out', | ||
'test3.hh_clang-tidy-yaml.plist', | ||
['files/test3.cpp', 'files/test3.hh'], | ||
'tidy3_hh.plist') | ||
|
||
def test_tidy7(self): | ||
""" Test for the tidy7.plist file. """ | ||
self.__check_analyzer_result('tidy7.out', | ||
'test7.cpp_clang-tidy-yaml.plist', | ||
['files/test7.cpp'], 'tidy7.plist') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
tools/report-converter/tests/unit/analyzers/tidy_output_test_files/files/test7.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <iostream> | ||
|
||
// Unused function declaration | ||
void unusedFunction(); | ||
|
||
// Unused variable | ||
int unusedVar = 42; | ||
|
||
int main() { | ||
// Uninitialized pointer | ||
int* ptr; | ||
|
||
// NULL | ||
int* nullPtr = NULL; | ||
|
||
// Array out-of-bounds access | ||
int arr[5]; | ||
// Out of bounds access | ||
arr[10] = 42; | ||
|
||
// Function call without definition | ||
unusedFunction(); | ||
|
||
// Uninitialized pointer | ||
*ptr = 5; | ||
|
||
// Null pointer dereference | ||
*nullPtr = 10; | ||
|
||
int errVar = 3 | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
tools/report-converter/tests/unit/analyzers/tidy_output_test_files/tidy7.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
files/test7.cpp:14:20: warning: use nullptr [modernize-use-nullptr] | ||
14 | int* nullPtr = NULL; | ||
| ^~~~ | ||
| nullptr | ||
files/test7.cpp:19:5: warning: array index 10 is past the end of the array (that has type 'int[5]') [clang-diagnostic-array-bounds] | ||
19 | arr[10] = 42; | ||
| ^ ~~ | ||
files/test7.cpp:17:5: note: array 'arr' declared here | ||
17 | int arr[5]; | ||
| ^ | ||
files/test7.cpp:30:19: error: expected ';' at end of declaration [clang-diagnostic-error] | ||
30 | int errVar = 3 | ||
| ^ | ||
| ; |
Oops, something went wrong.