-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgcc_copt_inclusions.py
executable file
·234 lines (205 loc) · 7.5 KB
/
gcc_copt_inclusions.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env python3
# https://gcc.gnu.org/onlinedocs/gccint/Option-file-format.html#Option-file-format
import argparse
import sys
import logging
import re
from enum import Enum
languages = []
class State(Enum):
INIT = 1
LANGUAGE = 2
ENUM = 3
ENUM_VALUE = 4
OPTION = 5
OPTION_HELP = 6
IGNORE = 1000
def parse_properties_string(s):
res = {}
r = re.compile(r"([^( ]+(?:\(.*?\))?)")
name_val_r = re.compile(r"([^( ]+)(\(.*?\))?")
try:
for v in r.findall(s):
k, v = name_val_r.search(v).groups()
if v:
res[k] = v[1:-1]
else:
res[k] = None
except TypeError as e:
raise RuntimeError("Invalid properties string: "+s) from e
return res
class GCCOption():
def __init__(self, name, props):
self.name = name
self.raw_props = props.strip("\n ")
self.props = parse_properties_string(props)
self.aliases = []
self.enabled_by = []
self.enables = []
self.help = ""
self.langs = self.props.get("LangEnabledBy", "").split(',')[0].split(' ') or []
def __str__(self):
return "-%s {%r}" % (self.name, self.props)
def __repr__(self):
return str(self)
def is_valid_for_lang(self, lang):
return "Common" in self.props.keys() or lang in self.langs
def is_warning(self):
return not self.is_alias() and "Warning" in self.props.keys()
def is_alias(self):
return "Alias" in self.props.keys()
def get_alias_target(self):
if self.is_alias():
return self.props['Alias'].split(',')[0]
return None
def is_enabled_by(self):
keys = self.props.keys()
return "EnabledBy" in keys or "LangEnabledBy" in keys
def is_by_default(self):
# TODO: less hackish
return "Var(" in self.raw_props and "Init(1)" in self.raw_props and "Range" not in self.raw_props
def get_enabled_by(self):
# TODO: handle && and ||
res = []
if "EnabledBy" in self.props.keys():
res.append(self.props['EnabledBy'])
if "LangEnabledBy" in self.props.keys():
lang_args = self.props['LangEnabledBy'].split(',')
if len(lang_args) > 2:
lang_args = lang_args[0:2]
if len(lang_args) > 1:
langs, opt = lang_args
res.append(opt.strip(' '))
if res:
return res
return None
def pretty_print(self):
print("Option:", self.name, "[DEFAULT ON]" if self.is_by_default() else "")
if self.is_alias():
print("\tAlias:", self.props["Alias"])
if self.is_enabled_by():
e = self.props.get('EnabledBy', None)
if e:
print("\tEnabledBy", e)
e = self.props.get('LangEnabledBy', None)
if e:
print("\tLangEnabledBy", e)
if self.enables:
print("\tEnables:", ", ".join(self.enables))
print("\tHelp:", self.help)#.rstrip())
print("\t"+self.raw_props)
class GCCEnum():
def __init__(self, s):
enum_info = parse_properties_string(s)
self.__name__ = enum_info['Name']
self.__type__ = enum_info['Type']
self.values = {}
def __str__(self):
return "Enum: %s / %s {%r}" % (self.__name__, self.__type__, self.values)
def __repr__(self):
return str(self)
parser = argparse.ArgumentParser(description='Parse GCC option definition file (.opt)')
parser.add_argument('file', help='The file to parse')
parser.add_argument('arg', nargs='*', help='Arg to display details of')
parser.add_argument('--warn-not-enabled', action='store_true', help="List warnings not enabled by -Wall and -Wextra")
parser.add_argument('--lang', help="Restrict to this language")
parser.add_argument('-v', '--verbose', action='store_true', help='verbose operations')
args = parser.parse_args()
if args.verbose:
logging.basicConfig(level=logging.DEBUG)
state = State.INIT
current_option = None
Ignored_options = ['TargetSave', 'Variable', 'TargetVariable', 'HeaderInclude', 'SourceInclude']
enums = {}
options = {}
with open(args.file, "r") as f:
for l in f.readlines():
l = l.rstrip("\n")
logging.debug("State : %r, current_option: '%s', line: '%s'", state, current_option, l)
# Skip comment
if len(l) and l[0] == ";":
continue
# Empty line, reset State
if l == "":
state = State.INIT
current_option = None
continue
if state == State.INIT:
if l in Ignored_options:
state = State.IGNORE
elif l == "Language":
state = State.LANGUAGE
elif l == "Enum":
state = State.ENUM
elif l == "EnumValue":
state = State.ENUM_VALUE
else:
state = State.OPTION
current_option = l
elif state in (State.IGNORE, ):
logging.debug("Ignoring line")
# Ignore line
continue
elif state == State.OPTION_HELP:
options[current_option].help += l
elif state == State.LANGUAGE:
logging.debug('New language: %s',l)
languages.append(l)
elif state == State.ENUM:
new_enum = GCCEnum(l)
logging.debug('New Enum: %s',new_enum)
enums[new_enum.__name__] = new_enum
elif state == State.ENUM_VALUE:
enum_value_info = parse_properties_string(l)
enum_name = enum_value_info['Enum']
enums[enum_name].values[enum_value_info['String']] = enum_value_info['Value']
elif state == State.OPTION:
# Skip already defined options
# TODO: check which definition is the best ?
if current_option not in options:
opt = GCCOption(current_option, l)
logging.debug("%r", opt)
options[current_option] = opt
state = State.OPTION_HELP
else:
state = State.IGNORE
else:
raise RuntimeError("Invalid STATE "+str(state))
# Consolidate options
for name, opt in options.items():
# Aliases are added to the real option, then deleted
alias_target = opt.get_alias_target()
if alias_target:
try:
options[alias_target].aliases.append(name)
except KeyError:
print(f"Error: could not find Alias target '{alias_target}', check for typo")
sys.exit(1)
continue
enabled_by = opt.get_enabled_by()
if enabled_by:
for en in enabled_by:
if "&&" not in en and "||" not in en:
options[en].enables.append(name)
def get_enabled_by_recursive(opt, res=[]):
if opt.is_enabled_by():
en_by = opt.get_enabled_by()
for o in en_by:
res.append(o)
if "&&" not in o and "||" not in o:
get_enabled_by_recursive(options[o], res)
return res
return res
if args.warn_not_enabled:
for name, opt in options.items():
if opt.is_warning() and not opt.is_by_default() and name not in ("Wextra", "Wall"):
if opt.is_enabled_by():
en_by = get_enabled_by_recursive(opt)
if "Wextra" in en_by or "Wall" in en_by:
continue
opt.pretty_print()
else:
for arg in args.arg:
p = re.compile(arg)
for found_opt in filter(lambda x: p.match(x), options.keys()):
options[found_opt].pretty_print()