4 This produces a structured normalized report json file based on warnings generated by another tool.
5 Currently implemented is clang-tidy warnings.
10 from collections
import namedtuple
11 from itertools
import groupby
14 from fnmatch
import fnmatch
19 from codereport
import CodeReport, ReportItem
24 m = re.match(
r"(?P<file>[/.\-+\w]+):(?P<line>\d+):(?P<col>\d+): (?P<sev>.*?):(?P<msg>[\s\S]*?)\[(?P<code>.*)\]\n(?P<info>[\s\S]*)", itemstr)
28 line=
int(m.group(
"line")),
29 col=
int(m.group(
"col")),
30 message=m.group(
"msg").strip(),
32 severity=m.group(
"sev")
39 print(
"Failed parsing clang-tidy item:")
49 itemstr = re.sub(
r"Enabled checks:\n[\S\s]+?\n\n",
"", itemstr)
50 itemstr = re.sub(
r"clang-tidy-\d\.\d.*\n?",
"", itemstr)
51 itemstr = re.sub(
r"clang-apply-.*",
"", itemstr)
52 itemstr = re.sub(
r".*-header-filter.*",
"", itemstr)
57 matches = list(re.finditer(
r"([\w/.\-+]+):(\d+):(\d+): (?:(?:warning)|(?:error)):", itemstr))
58 for idx, m
in enumerate(matches):
62 item = itemstr[prevstart:start]
64 if idx+1 == len(matches):
65 item = itemstr[start:]
69 items = set(map(parse_clang_tidy_item, sorted(items)))
75 p = argparse.ArgumentParser(description=__doc__)
76 p.add_argument(
"mode", choices=(
"clang-tidy",),
77 help=
"Type of input warnings")
78 p.add_argument(
"inputfile",
79 help=
"The input file containing the warnings")
80 p.add_argument(
"output", default=
"codereport_clang_tidy.json",
81 help=
"The resulting JSON file")
82 p.add_argument(
"--exclude",
"-e", action=
"append", default=[],
83 help=
"Exclude files that match any of these patterns")
84 p.add_argument(
"--filter", action=
"append", default=[],
85 help=
"Only include files that match any of these patterns")
89 if args.mode ==
"clang-tidy":
90 with
open(args.inputfile,
"r", encoding="utf-8") as f:
96 if len(args.filter) > 0:
97 accept = accept
and all(fnmatch(item.path, e)
for e
in args.filter)
99 accept = accept
and not any(fnmatch(item.path, e)
for e
in args.exclude)
102 items =
filter(select, items)
105 data = [i.dict()
for i
in items]
106 print(
"Write to", args.output)
107 with
open(args.output,
"w+")
as jf:
108 json.dump(data, jf, indent=2)
111 if "__main__" == __name__: