/
gcc.py
executable file
·153 lines (128 loc) · 5.44 KB
/
gcc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python
# Copyright 2013 Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
# USA
import re
import sys
from firehose.model import Message, Function, Point, \
File, Location, Metadata, Generator, Issue, Analysis
# Parser for warnings emitted by GCC
# The code that generates these warnings can be seen within gcc's own
# sources within:
# gcc/diagnostic.c
# gcc/langhooks.c: lhd_print_error_function
# (as of gcc-4.7.2)
# See e.g.:
# http://gcc.gnu.org/viewcvs/trunk/gcc/diagnostic.c?revision=195098&view=markup
# http://gcc.gnu.org/viewcvs/trunk/gcc/langhooks.c?revision=195098&view=markup
# This parser is only intended to be run with the C locale
# column is optional
# switch is optional
GCC_PATTERN = re.compile("^(?P<path>\S.*?):(?P<line>\d+):(?P<column>\d*):? (?P<type>warning|note): (?P<message>.*?)(?P<switch> \[\-W.+\])?$")
SWITCH_SUB_PATTERN = re.compile("^ \[\-W(?P<name>.*)\]$")
CWE_SUB_PATTERN = re.compile("^(?P<message>.*) \[CWE-(?P<cwe>[0-9]+)\]$")
# single quotes may not match locales that are not C
FUNCTION_PATTERN = re.compile(".*: In (?:member )?function '(?P<func>.*)':")
# match when gcc issues a warning for a location it thinks is in global scope
GLOBAL_PATTERN = re.compile(".*: At global scope:$")
# When gcc issues a warning at spot it thinks is in global scope, use this
# as the function name
GLOBAL_FUNC_NAME = '::'
def parse_file(data_file, gccversion=None, sut=None, file_=None, stats=None):
"""
looks for groups of lines that start with a line identifying a function
name, followed by one or more lines with a warning or note
:param data_file: file object containing build log
:type data_file: file
:param gccversion: version of GCC that generated this report
:type gccversion: str
:return: Analysis instance
"""
# has a value only when in a block of lines where the first line identifies
# a function and is followed by 0 or more warning lines
generator = Generator(name='gcc',
version=gccversion)
metadata = Metadata(generator, sut, file_, stats)
analysis = Analysis(metadata, [])
current_func_name = None
for line in data_file.readlines():
match_func = FUNCTION_PATTERN.match(line)
match_global = GLOBAL_PATTERN.match(line)
# if we found a line that describes a function name
if match_func:
current_func_name = match_func.group('func')
elif match_global:
current_func_name = GLOBAL_FUNC_NAME
# if we think the next line might describe a warning
elif current_func_name is not None:
issue = parse_warning(line, current_func_name)
if issue:
analysis.results.append(issue)
else:
# reset this when we run out of warnings associated with it
current_func_name = None
return analysis
def parse_warning(line, func_name):
"""
:param line: current line read from file
:type line: basestring
:param func_name: name of the current function
:type func_name: basestring
:param gccversion: version of GCC that generated this report
:type gccversion: str
:param sut: metadata about the software-under-test
:type sut: Sut
:return: Issue if match, else None
"""
match = GCC_PATTERN.match(line)
if match:
text = match.group('message')
# GCC 10 onwards can (optionally) append a CWE id to the message.
# Extract it if it is present.
cwe_match = CWE_SUB_PATTERN.match(text)
if cwe_match:
message = Message(cwe_match.group('message'))
cwe = int(cwe_match.group('cwe'))
else:
message = Message(text)
cwe = None
func = Function(func_name)
try:
column = int(match.group('column'))
except ValueError:
if match.group('column') == '':
column = 0
else:
raise
except TypeError:
column = None
switch_match = SWITCH_SUB_PATTERN.match(match.group('switch') or '')
if switch_match:
switch = switch_match.group('name')
else:
switch = None
point = Point(int(match.group('line')), column)
path = File(match.group('path'), None)
location = Location(path, func, point)
return Issue(cwe, switch, location, message, None, None)
if __name__ == '__main__':
if len(sys.argv) != 2:
print("provide a build log file path as the only argument")
else:
with open(sys.argv[1]) as data_file:
analysis = parse_file(data_file)
sys.stdout.write(str(analysis.to_xml()))
sys.stdout.write('\n')