Skip to content

Commit

Permalink
[analyzer] Fix parsing clangsa analyze help
Browse files Browse the repository at this point in the history
  • Loading branch information
csordasmarton committed Feb 23, 2021
1 parent 4a1fcdf commit 33c16d4
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
36 changes: 34 additions & 2 deletions analyzer/codechecker_analyzer/analyzers/clangsa/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,40 @@ def parse_clang_help_page(command, start_label, environ):
return []

help_page = help_page[help_page.index(start_label) + len(start_label):]
reg = re.compile(r'(\S+)\s+([^\n]+)')
return re.findall(reg, help_page)

# This regex will match lines which contain only a flag or a flag and a
# description: ' <flag>', ' <flag> <description>'.
start_new_option_rgx = \
re.compile(r"^\s{2}(?P<flag>\S+)(\s(?P<desc>[^\n]+))?$")

# This regex will match lines which contain description for the previous
# flag: ' <description>'
prev_help_desc_rgx = \
re.compile(r"^\s{3,}(?P<desc>[^\n]+)$")

res = []

flag = None
desc = []
for line in help_page.splitlines():
m = start_new_option_rgx.match(line)
if m:
if flag and desc:
res.append((flag, ' '.join(desc)))
flag = None
desc = []

flag = m.group("flag")
else:
m = prev_help_desc_rgx.match(line)

if m and m.group("desc"):
desc.append(m.group("desc").strip())

if flag and desc:
res.append((flag, ' '.join(desc)))

return res


class ClangSA(analyzer_base.SourceAnalyzer):
Expand Down
50 changes: 50 additions & 0 deletions analyzer/tests/functional/cmdline/test_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,53 @@ def test_checkers_warnings(self):
self.assertIn('clang-diagnostic-vla', out)
# Make sure the header from `diagtool --tree` is ignored.
self.assertNotIn('EEN', out)

def test_clangsa_checkers_description(self):
"""
Test that descriptions for clangsa checkers are parsed properly.
"""
checkers_cmd = [env.codechecker_cmd(), 'checkers',
'--analyzers', 'clangsa',
'-o', 'json', '--details']

_, out, _ = run_cmd(checkers_cmd)
checkers = json.loads(out)

for checker in checkers:
desc = checker['description']
self.assertTrue(desc)
self.assertFalse(desc[0].islower())

def test_checker_config(self):
"""
Test that descriptions for clangsa checkers configs are parsed
properly.
"""
checker_cfg_cmd = [env.codechecker_cmd(), 'checkers',
'--analyzers', 'clangsa', '--checker-config',
'-o', 'json', '--details']

_, out, _ = run_cmd(checker_cfg_cmd)
checker_cfg = json.loads(out)

for cfg in checker_cfg:
desc = cfg['description']
self.assertTrue(desc)
self.assertFalse(desc[0].islower())

def test_analyzer_config(self):
"""
Test that descriptions for clangsa analyzer configs are parsed
properly.
"""
analyzer_cfg_cmd = [env.codechecker_cmd(), 'analyzers',
'--analyzer-config', 'clangsa',
'-o', 'json', '--details']

_, out, _ = run_cmd(analyzer_cfg_cmd)
analyzer_cfg = json.loads(out)

for cfg in analyzer_cfg:
desc = cfg['description']
self.assertTrue(desc)
self.assertFalse(desc[0].islower())

0 comments on commit 33c16d4

Please sign in to comment.