forked from dhh1128/const_fix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparam.py
More file actions
183 lines (167 loc) · 5.68 KB
/
param.py
File metadata and controls
183 lines (167 loc) · 5.68 KB
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
import re
array_spec_pat = re.compile('.*(\[^]]\])$')
const_prefix_pat = re.compile('^const ([a-zA-Z0-9_]+)(.*)$')
moab_type_pat = re.compile('.*\Wm[a-z_0-9]+_t$')
datatype_names = 'int|short|long|double|float|char|bool'.split('|')
moab_struct_naming_pat = re.compile(r'm([a-z_]+)_t(?=$|\W)')
moab_common_out_pat = re.compile(r'(m?u?(long|int)|mbool_t)\s*\*\s*$')
splittable = [
'table',
'info',
'mdata',
'data',
'grp',
'req',
'vm',
'grid',
'stats',
'list',
'array',
'node'
]
abbreviatable = {
'request': 'req',
'response': 'resp',
'constraint': 'cons',
'policy': 'pol',
'partition': 'par',
'group': 'grp',
'threadpool': 'thpool',
'trigger': 'trig',
'resource': 'res',
'reservation': 'rsv',
'class': 'cls'
}
def _squeeze(txt):
'''Replace all runs of whitepace with a single space, and trim front and back.'''
return re.sub(r'\s{2,}', ' ', txt).strip()
def normalize_type(typ):
'''
Put the type portion of a parameter declaration into normalized
form so it can be compared reliably:
const char* --> char const *
mjob_t & --> mjob_t &
'''
typ = _squeeze(typ.replace('*', ' * ').replace('&', ' & ')).replace('* *', '**')
m = const_prefix_pat.match(typ)
if m:
typ = '%s const%s' % (m.group(1), m.group(2))
return typ
class Param:
def __init__(self, begin, decl):
self.begin = begin
self.decl = decl
self.array_spec = ''
self.data_type = None
self.name = None
self.new_name = None
self._parse()
def propose_name(self):
m = moab_struct_naming_pat.match(self.data_type)
if m:
base = m.group(1)
'''
r -> rm, resource, rsv, req
p -> policy, partition
'''
for key in abbreviatable:
if base.endswith(key):
base = base[0:len(base) - len(key)] + '_' + abbreviatable[key]
break
for x in splittable:
if base.endswith(x):
i = len(base) - len(x)
if base[i - 1] != '_':
base = base[0:i] + '_' + base[i:]
cap_next = False
proposed = ''
for c in base:
if c == '_':
cap_next = True
else:
if cap_next:
proposed += c.upper()
cap_next = False
else:
proposed += c
return proposed
def is_const_candidate(self):
dt = self.data_type
if 'void' in dt:
return False
i = dt.find('*')
j = dt.find('&')
# Params that are not pointers or references are passed by value,
# so their constness is irrelevant.
if i == -1 and j == -1:
return False
# Just some optimizations that applies to moab specifically.
# 1. EMsg bufs are passed around to accumulate error messages; we
# know they are modified.
# 2. It's common in the codebase to see pointers to numeric types
# passed for OUT params: int * size, long * count, etc. These
# are also not worth checking.
if i > -1 and self.data_type == 'char *' and self.name == 'EMsg':
return False
if i > -1 and moab_common_out_pat.match(self.data_type):
return False
# Params that are *& are virtually guaranteed to be OUT params,
# so their constness should not be adjusted.
if i > -1 and j > -1:
return False
# Same for **.
if i > -1 and i < dt.rfind('*'):
return False
return True
def is_const(self):
return 'const' in self.decl
def get_pivot_point(self):
i = self.data_type.find('*')
j = self.data_type.find('&')
if i > -1:
if j > -1:
return min(i, j)
return i
elif j > -1:
return j
def set_const(self, value):
i = self.get_pivot_point()
if value:
if not self.is_const():
self.data_type = _squeeze(self.data_type[:i].rstrip() + ' const ' + self.data_type[i:])
elif self.is_const():
self.data_type = _squeeze(self.data_type.replace('const', ''))
def _parse(self):
decl = _squeeze(self.decl)
m = array_spec_pat.match(decl)
if m:
self.array_spec = m.group(1).replace(' ', '')
decl = decl[0:m.start(1)]
name_idx = -1
if not (decl.endswith('*') or decl.endswith('&') or moab_type_pat.match(decl)):
i = decl.rfind(' ')
if i > -1:
# Make sure we're not hitting a C-ism that confuses us.
prefix = decl[:i]
if (prefix != 'enum') and (prefix != 'struct'):
name_idx = i + 1
while decl[name_idx] == '*' or decl[name_idx] == '&':
name_idx += 1
name = decl[name_idx:]
if name in datatype_names:
name_idx = -1
else:
pass
if name_idx > -1:
self.data_type = decl[0:name_idx].rstrip()
self.name = decl[name_idx:]
else:
self.data_type = decl
self.data_type = normalize_type(self.data_type)
def __str__(self):
name = self.new_name
if not name:
name = self.name
if name:
return self.data_type + ' ' + name + self.array_spec
return self.data_type + self.array_spec