blob: 5cb31aafb17ecff696f1074f485377c2559ed796 [file] [log] [blame]
Fathi Boudra422bf772019-12-02 11:10:16 +02001#!/usr/bin/env python3
2#
3# Copyright (c) 2019, Arm Limited. All rights reserved.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7
8import argparse
9import json
10import re
11import shutil
12import sys
13
14
15_rule_exclusions = [
16 "MISRA C-2012 Rule 8.6",
17 "MISRA C-2012 Rule 5.1"
18]
19
20# The following classification of rules and directives include 'MISRA C:2012
21# Amendment 1'
22
23# Directives
24_dir_required = set(["1.1", "2.1", "3.1", "4.1", "4.3", "4.7", "4.10", "4.11",
25 "4.12", "4.14"])
26
27_dir_advisory = set(["4.2", "4.4", "4.5", "4.6", "4.8", "4.9", "4.13"])
28
29# Rules
30_rule_mandatory = set(["9.1", "9.2", "9.3", "12.5", "13.6", "17.3", "17.4",
31 "17.6", "19.1", "21.13", "21.17", "21.18", "21.19", "21.20", "22.2", "22.5",
32 "22.6"])
33
34_rule_required = set(["1.1", "1.3", "2.1", "2.2", "3.1", "3.2", "4.1", "5.1",
35 "5.2", "5.3", "5.4", "5.5", "5.6", "5.7", "5.8", "6.1", "6.2", "7.1", "7.2",
36 "7.3", "7.4", "8.1", "8.2", "8.3", "8.4", "8.5", "8.6", "8.7", "8.8",
37 "8.10", "8.12", "8.14", "9.2", "9.3", "9.4", "9.5", "10.1", "10.2", "10.3",
38 "10.4", "10.6", "10.7", "10.8", "11.1", "11.2", "11.3", "11.6", "11.7",
39 "11.8", "11.9", "12.2", "13.1", "13.2", "13.5", "14.1", "14.2", "14.3",
40 "14.4", "15.2", "15.3", "15.6", "15.7", "16.1", "16.2", "16.3", "16.4",
41 "16.5", "16.6", "16.7", "17.1", "17.2", "17.7", "18.1", "18.2", "18.3",
42 "18.6", "18.7", "18.8", "20.3", "20.4", "20.6", "20.7", "20.8", "20.9",
43 "20.11", "20.12", "20.13", "20.14", "21.1", "21.2", "21.3", "21.4", "21.5",
44 "21.6", "21.7", "21.8", "21.9", "21.10", "21.11", "21.14", "21.15", "21.16",
45 "22.1", "22.3", "22.4", "22.7", "22.8", "22.9", "22.10"])
46
47_rule_advisory = set(["1.2", "2.3", "2.4", "2.5", "2.6", "2.7", "4.2", "5.9",
48 "8.9", "8.11", "8.13", "10.5", "11.4", "11.5", "12.1", "12.3", "12.4",
49 "13.3", "13.4", "15.1", "15.4", "15.5", "17.5", "17.8", "18.4", "18.5",
50 "19.2", "20.1", "20.2", "20.5", "20.10", "21.12"])
51
52
53_checker_lookup = {
54 "Directive": {
55 "required": _dir_required,
56 "advisory": _dir_advisory
57 },
58 "Rule": {
59 "mandatory": _rule_mandatory,
60 "required": _rule_required,
61 "advisory": _rule_advisory
62 }
63 }
64
65_checker_re = re.compile(r"""(?P<kind>\w+) (?P<number>[\d\.]+)$""")
66
67
68def _classify_checker(checker):
69 match = _checker_re.search(checker)
70 if match:
71 kind, number = match.group("kind"), match.group("number")
72 for classification, class_set in _checker_lookup[kind].items():
73 if number in class_set:
74 return classification
75
76 return "unknown"
77
78
79# Return a copy of the original issue description. Update file path to strip
80# heading '/', and also insert CID.
81def _new_issue(cid, orig_issue):
82 checker = orig_issue["checker"]
83 classification = _classify_checker(checker)
84
85 return {
86 "cid": cid,
87 "file": orig_issue["file"].lstrip("/"),
88 "line": orig_issue["mainEventLineNumber"],
89 "checker": checker,
90 "classification": classification,
91 "description": orig_issue["mainEventDescription"]
92 }
93
94
95def _cls_string(issue):
96 cls = issue["classification"]
97
98 return " (" + cls + ")" if cls != "unknown" else ""
99
100
101# Given an issue, make a string formed of file name, line number, checker, and
102# the CID. This could be used as a dictionary key to identify unique defects
103# across the scan. Convert inegers to zero-padded strings for proper sorting.
104def make_key(i):
105 return (i["file"] + str(i["line"]).zfill(5) + i["checker"] +
106 str(i["cid"]).zfill(5))
107
108
109# Iterate through all issues that are not ignored. If show_all is set, only
110# issues that are not in the comparison snapshot are returned.
111def iter_issues(path, show_all=False):
112 with open(path, encoding="utf-8") as fd:
113 report = json.load(fd)
114
115 # Unconditional filter
116 filters = [lambda i: ((i["triage"]["action"] != "Ignore") and
117 (i["occurrences"][0]["checker"] not in _rule_exclusions))]
118
119 # Whether we need diffs only
120 if not show_all:
121 # Pick only issues that are not present in comparison snapshot
122 filters.append(lambda i: not i["presentInComparisonSnapshot"])
123
124 # Pick issue when all filters are true
125 filter_func = lambda i: all([f(i) for f in filters])
126
127 # Top-level is a group of issues, all sharing a common CID
128 for issue_group in filter(filter_func, report["issueInfo"]):
129 # Pick up individual occurrence of the CID
130 for occurrence in issue_group["occurrences"]:
131 yield _new_issue(issue_group["cid"], occurrence)
132
133
134# Format issue (returned from iter_issues()) as text.
135def format_issue(issue):
136 return ("{file}:{line}:[{checker}{cls}]<{cid}> {description}").format_map(
137 dict(issue, cls=_cls_string(issue)))
138
139
140# Format issue (returned from iter_issues()) as HTML table row.
141def format_issue_html(issue):
142 cls = _cls_string(issue)
143 cov_class = "cov-" + issue["classification"]
144
145 return """\
146<tr class="{cov_class}">
147 <td class="cov-file">{file}</td>
148 <td class="cov-line">{line}</td>
149 <td class="cov-checker">{checker}{cls}</td>
150 <td class="cov-cid">{cid}</td>
151 <td class="cov-description">{description}</td>
152</tr>""".format_map(dict(issue, cls=cls, cov_class=cov_class))
153
154
155if __name__ == "__main__":
156 parser = argparse.ArgumentParser()
157
158 parser.add_argument("--all", default=False, dest="show_all",
159 action="store_const", const=True, help="List all issues")
160 parser.add_argument("--output",
161 help="File to output filtered defects to in JSON")
162 parser.add_argument("json_report")
163
164 opts = parser.parse_args()
165
166 issues = []
167 for issue in sorted(iter_issues(opts.json_report, opts.show_all),
168 key=lambda i: make_key(i)):
169 print(format_issue(issue))
170 issues.append(issue)
171
172 if opts.output:
173 # Dump selected issues
174 with open(opts.output, "wt") as fd:
175 fd.write(json.dumps(issues))
176
177 sys.exit(int(len(issues) > 0))