-
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 #2968 from jay24rajput/smatch_parser
[report-converter] Smatch Parser
- Loading branch information
Showing
11 changed files
with
293 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
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
7 changes: 7 additions & 0 deletions
7
tools/report-converter/codechecker_report_converter/smatch/__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 | ||
# | ||
# ------------------------------------------------------------------------- |
36 changes: 36 additions & 0 deletions
36
tools/report-converter/codechecker_report_converter/smatch/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,36 @@ | ||
# ------------------------------------------------------------------------- | ||
# | ||
# 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 codechecker_report_converter.analyzer_result import AnalyzerResult | ||
|
||
from .output_parser import SmatchParser | ||
from ..plist_converter import PlistConverter | ||
|
||
|
||
class SmatchAnalyzerResult(AnalyzerResult): | ||
""" Transform analyzer result of Smatch. """ | ||
|
||
TOOL_NAME = 'smatch' | ||
NAME = 'Smatch' | ||
URL = 'https://repo.or.cz/w/smatch.git' | ||
|
||
def parse(self, analyzer_result): | ||
""" Creates plist files from the given analyzer result to the given | ||
output directory. | ||
""" | ||
parser = SmatchParser(analyzer_result) | ||
|
||
content = self._get_analyzer_result_file_content(analyzer_result) | ||
if not content: | ||
return | ||
|
||
messages = parser.parse_messages(content) | ||
|
||
plist_converter = PlistConverter(self.TOOL_NAME) | ||
plist_converter.add_messages(messages) | ||
return plist_converter.get_plist_results() |
64 changes: 64 additions & 0 deletions
64
tools/report-converter/codechecker_report_converter/smatch/output_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,64 @@ | ||
# ------------------------------------------------------------------------- | ||
# | ||
# 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 logging | ||
import os | ||
import re | ||
|
||
from ..output_parser import BaseParser, Message | ||
LOG = logging.getLogger('ReportConverter') | ||
|
||
|
||
class SmatchParser(BaseParser): | ||
""" | ||
Parser for Smatch Output | ||
""" | ||
|
||
def __init__(self, analyzer_result): | ||
super(SmatchParser, self).__init__() | ||
|
||
self.analyzer_result = analyzer_result | ||
|
||
self.message_line_re = re.compile( | ||
# File path followed by a ':'. | ||
r'^(?P<path>[\S ]+?):' | ||
# Line number followed by a whitespace. | ||
r'(?P<line>\d+?)\s' | ||
# Function name followed by a whitespace. | ||
r'(?P<function_name>\S+)\s' | ||
# Checker name followed by a whitespace | ||
r'\[smatch\.(?P<checker_name>\S+)\]\s' | ||
# Message. | ||
r'(?P<message>[\S \t]+)\s*') | ||
|
||
def parse_message(self, it, line): | ||
""" | ||
Actual Parsing function for the given line | ||
It is expected that each line contains a seperate report | ||
""" | ||
match = self.message_line_re.match(line) | ||
if match is None: | ||
return None, next(it) | ||
|
||
file_path = os.path.normpath( | ||
os.path.join(os.path.dirname(self.analyzer_result), | ||
match.group('path'))) | ||
|
||
column = 0 | ||
|
||
message = Message( | ||
file_path, | ||
int(match.group('line')), | ||
column, | ||
match.group('message').strip(), | ||
match.group('checker_name')) | ||
|
||
try: | ||
return message, next(it) | ||
except StopIteration: | ||
return message, '' |
5 changes: 5 additions & 0 deletions
5
tools/report-converter/tests/unit/smatch_output_test_files/files/sample.c
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,5 @@ | ||
static struct parse_smatch *check_sample() { | ||
int res; | ||
res = 0; | ||
return ERR_PTR(res); | ||
} |
69 changes: 69 additions & 0 deletions
69
tools/report-converter/tests/unit/smatch_output_test_files/sample.expected.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>diagnostics</key> | ||
<array> | ||
<dict> | ||
<key>category</key> | ||
<string>unknown</string> | ||
<key>check_name</key> | ||
<string>check_zero_to_err_ptr</string> | ||
<key>description</key> | ||
<string>warn: passing zero to 'ERR_PTR'</string> | ||
<key>issue_hash_content_of_line_in_context</key> | ||
<string>2d8b9711e5b334b518d201a8a6799fdb</string> | ||
<key>location</key> | ||
<dict> | ||
<key>col</key> | ||
<integer>0</integer> | ||
<key>file</key> | ||
<integer>0</integer> | ||
<key>line</key> | ||
<integer>4</integer> | ||
</dict> | ||
<key>path</key> | ||
<array> | ||
<dict> | ||
<key>depth</key> | ||
<integer>0</integer> | ||
<key>kind</key> | ||
<string>event</string> | ||
<key>location</key> | ||
<dict> | ||
<key>col</key> | ||
<integer>0</integer> | ||
<key>file</key> | ||
<integer>0</integer> | ||
<key>line</key> | ||
<integer>4</integer> | ||
</dict> | ||
<key>message</key> | ||
<string>warn: passing zero to 'ERR_PTR'</string> | ||
</dict> | ||
</array> | ||
<key>type</key> | ||
<string>smatch</string> | ||
</dict> | ||
</array> | ||
<key>files</key> | ||
<array> | ||
<string>files/sample.c</string> | ||
</array> | ||
<key>metadata</key> | ||
<dict> | ||
<key>analyzer</key> | ||
<dict> | ||
<key>name</key> | ||
<string>smatch</string> | ||
</dict> | ||
<key>generated_by</key> | ||
<dict> | ||
<key>name</key> | ||
<string>report-converter</string> | ||
<key>version</key> | ||
<string>x.y.z</string> | ||
</dict> | ||
</dict> | ||
</dict> | ||
</plist> |
1 change: 1 addition & 0 deletions
1
tools/report-converter/tests/unit/smatch_output_test_files/sample.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 @@ | ||
files/sample.c:4 check_sample() [smatch.check_zero_to_err_ptr] warn: passing zero to 'ERR_PTR' |
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,79 @@ | ||
# ------------------------------------------------------------------------- | ||
# | ||
# 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 SmatchAnalyzerResult, which | ||
used in sequence transform Smatch output to a plist file. | ||
""" | ||
|
||
|
||
import os | ||
import plistlib | ||
import shutil | ||
import tempfile | ||
import unittest | ||
|
||
|
||
from codechecker_report_converter.smatch.analyzer_result import \ | ||
SmatchAnalyzerResult | ||
|
||
|
||
class SmatchAnalyzerResultTestCase(unittest.TestCase): | ||
""" Test the output of the SmatchAnalyzerResult. """ | ||
|
||
def setUp(self): | ||
""" Setup the test. """ | ||
self.analyzer_result = SmatchAnalyzerResult() | ||
self.cc_result_dir = tempfile.mkdtemp() | ||
self.test_files = os.path.join(os.path.dirname(__file__), | ||
'smatch_output_test_files') | ||
|
||
def tearDown(self): | ||
""" Clean temporary directory. """ | ||
shutil.rmtree(self.cc_result_dir) | ||
|
||
def test_no_smatch_output_file(self): | ||
""" Test transforming single C file. """ | ||
analyzer_result = os.path.join(self.test_files, 'files', | ||
'sample.c') | ||
|
||
ret = self.analyzer_result.transform(analyzer_result, | ||
self.cc_result_dir) | ||
self.assertFalse(ret) | ||
|
||
def test_transform_dir(self): | ||
""" Test transforming a directory. """ | ||
analyzer_result = os.path.join(self.test_files) | ||
|
||
ret = self.analyzer_result.transform(analyzer_result, | ||
self.cc_result_dir) | ||
self.assertFalse(ret) | ||
|
||
def test_transform_single_file(self): | ||
""" Test transforming single output file. """ | ||
analyzer_result = os.path.join(self.test_files, 'sample.out') | ||
self.analyzer_result.transform(analyzer_result, self.cc_result_dir) | ||
|
||
plist_file = os.path.join(self.cc_result_dir, | ||
'sample.c_smatch.plist') | ||
|
||
with open(plist_file, mode='rb') as pfile: | ||
res = plistlib.load(pfile) | ||
|
||
# Use relative path for this test. | ||
res['files'][0] = os.path.join('files', 'sample.c') | ||
|
||
self.assertTrue(res['metadata']['generated_by']['version']) | ||
res['metadata']['generated_by']['version'] = "x.y.z" | ||
|
||
plist_file = os.path.join(self.test_files, | ||
'sample.expected.plist') | ||
with open(plist_file, mode='rb') as pfile: | ||
exp = plistlib.load(pfile) | ||
|
||
self.assertEqual(res, exp) |