-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
misra_9.py
497 lines (406 loc) · 18.5 KB
/
misra_9.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# Holds information about an array, struct or union's element definition.
class ElementDef:
def __init__(self, elementType, name, valueType, dimensions = None):
self.elementType = elementType # 'array', 'record' or 'value'
self.name = str(name)
self.valueType = valueType
self.children = []
self.dimensions = dimensions
self.parent = None
self.isDesignated = False
self.isPositional = False
self.numInits = 0
self.childIndex = -1
self.flexibleToken = None
self.isFlexible = False
self.structureViolationToken = None
def __repr__(self):
inits = ""
if self.isPositional:
inits += 'P'
if self.isDesignated:
inits += 'D'
if not (self.isPositional or self.isDesignated) and self.numInits == 0:
inits += '_'
if self.numInits > 1:
inits += str(self.numInits)
attrs = ["childIndex", "elementType", "valueType"]
return "{}({}, {}, {})".format(
"ED",
self.getLongName(),
inits,
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
)
@property
def isArray(self):
return self.elementType == 'array'
@property
def isRecord(self):
return self.elementType == 'record'
@property
def isValue(self):
return self.elementType == 'value'
def getLongName(self):
return self.parent.getLongName() + "." + self.name if self.parent else self.name
def getInitDump(self):
t = []
if self.isPositional:
t.append('P')
if self.isDesignated:
t.append('D')
if self.numInits == 0:
t.append('_')
if self.numInits > 1:
t.append(str(self.numInits))
myDump = "".join(t)
if len(self.children):
childDumps = []
for c in self.children:
childDumps.append(c.getInitDump())
if self.structureViolationToken is not None:
myDump += "!"
myDump += "{ " + ", ".join(childDumps) + " }"
return myDump
def addChild(self, child):
self.children.append(child)
child.parent = self
def getNextChild(self):
self.childIndex += 1
return self.getChildByIndex(self.childIndex)
def getChildByIndex(self, index):
if self.isFlexible:
while len(self.children) <= index:
createChild(self, self.flexibleToken, len(self.children))
return self.children[index] if index >= 0 and len(self.children) > index else None
def getChildByName(self, name):
for c in self.children:
if c.name == name:
return c
return None
def getNextValueElement(self, root):
current = self
while current != root:
# Get next index of parent
i = current.parent.children.index(current) + 1
# Next index of parent exists
if i < len(current.parent.children):
current = current.parent.children[i]
return current.getFirstValueElement()
# Next index of parent doesn't exist. Move up
current = current.parent
return None
def getFirstValueElement(self):
current = self
# Move to first child as long as children exists
next_child = current.getChildByIndex(0)
while next_child:
current = next_child
next_child = current.getChildByIndex(0)
return current
def getLastValueElement(self):
current = self
# Move to last child as long as children exists
while len(current.children) > 0:
current = current.children[-1]
return current
def getChildByValueElement(self, ed):
potentialChild = ed
while potentialChild and not potentialChild in self.children:
potentialChild = potentialChild.parent
return self.children[self.children.index(potentialChild)] if potentialChild else None
def getEffectiveLevel(self):
if self.parent and self.parent.elementType == "array":
return self.parent.getEffectiveLevel() + 1
else:
return 0
def setInitialized(self, designated=False, positional=False):
if designated:
self.isDesignated = True
if positional or not designated:
self.isPositional = True
self.numInits += 1
def initializeChildren(self):
for child in self.children:
child.setInitialized(positional=True)
child.initializeChildren()
def unset(self):
self.isDesignated = False
self.isPositional = False
# Unset is always recursive
for child in self.children:
child.unset()
def markStuctureViolation(self, token):
if self.name == '->':
self.children[0].markStuctureViolation(token)
elif not self.structureViolationToken:
self.structureViolationToken = token
def markAsFlexibleArray(self, token):
self.flexibleToken = token
self.isFlexible = True
def markAsCurrent(self):
if self.parent:
if self.name == '<-':
self.parent.childIndex = self.parent.children.index(self.children[0])
else:
self.parent.childIndex = self.parent.children.index(self)
self.parent.markAsCurrent()
def isAllChildrenSet(self):
myself = len(self.children) == 0 and (self.isDesignated or self.isPositional)
mychildren = len(self.children) > 0 and all([child.isAllChildrenSet() for child in self.children])
return myself or mychildren
def isAllSet(self):
return all([child.isPositional or child.isDesignated for child in self.children])
def isOnlyDesignated(self):
return all([not child.isPositional for child in self.children])
def isMisra92Compliant(self):
return self.structureViolationToken is None and all([child.isMisra92Compliant() for child in self.children])
def isMisra93Compliant(self):
if self.elementType == 'array':
result = self.isAllChildrenSet() or \
((self.isAllSet() or \
self.isOnlyDesignated()) and \
all([not (child.isDesignated or child.isPositional) or child.isMisra93Compliant() for child in self.children]))
return result
elif self.elementType == 'record':
result = all([child.isMisra93Compliant() for child in self.children])
return result
else:
return True
def isMisra94Compliant(self):
return self.numInits <= 1 and all([child.isMisra94Compliant() for child in self.children])
def isMisra95Compliant(self):
return not self.isFlexible or all([not child.isDesignated for child in self.children])
# Parses the initializers and uodate the ElementDefs status accordingly
class InitializerParser:
def __init__(self):
self.token = None
self.root = None
self.ed = None
self.rootStack = []
def parseInitializer(self, root, token):
self.root = root
self.token = token
dummyRoot = ElementDef('array', '->', self.root.valueType)
dummyRoot.children = [self.root]
self.rootStack = []
self.root = dummyRoot
self.ed = self.root.getFirstValueElement()
isFirstElement = False
isDesignated = False
while self.token:
if self.token.str == ',':
self.token = self.token.astOperand1
isFirstElement = False
# Designated initializer ( [2]=... or .name=... )
elif self.token.isAssignmentOp and not self.token.valueType:
self.popFromStackIfExitElement()
self.ed = getElementByDesignator(self.root, self.token.astOperand1)
if self.ed:
# Update root
self.pushToRootStackAndMarkAsDesignated()
# Make sure ed points to valueElement
self.ed = self.ed.getFirstValueElement()
self.token = self.token.astOperand2
isFirstElement = False
isDesignated = True
elif self.token.str == '{':
nextChild = self.root.getNextChild()
if nextChild:
if nextChild.isArray or nextChild.isRecord:
nextChild.unset()
nextChild.setInitialized(isDesignated)
self.ed = nextChild.getFirstValueElement()
isDesignated = False
elif nextChild.valueType is None:
# No type information available - unable to check structure - assume correct initialization
nextChild.setInitialized(isDesignated)
self.unwindAndContinue()
continue
elif self.token.astOperand1:
# Create dummy nextChild to represent excess levels in initializer
dummyRoot = ElementDef('array', '<-', self.root.valueType)
dummyRoot.parent = self.root
dummyRoot.childIndex = 0
dummyRoot.children = [nextChild]
nextChild.parent = dummyRoot
self.root.markStuctureViolation(self.token)
# Fake dummy as nextChild (of current root)
nextChild = dummyRoot
if self.token.astOperand1:
self.root = nextChild
self.token = self.token.astOperand1
isFirstElement = True
else:
# {}
if self.root.name == '<-':
self.root.parent.markStuctureViolation(self.token)
else:
self.root.markStuctureViolation(self.token)
self.ed = None
self.unwindAndContinue()
else:
if self.ed and self.ed.isValue:
if not isDesignated and len(self.rootStack) > 0 and self.rootStack[-1][1] == self.root:
self.rootStack[-1][0].markStuctureViolation(self.token)
if isFirstElement and self.token.str == '0' and self.token.next.str == '}':
# Zero initializer causes recursive initialization
self.root.initializeChildren()
elif self.token.isString and self.ed.valueType.pointer > 0:
if self.ed.valueType.pointer - self.ed.getEffectiveLevel() == 1:
if self.ed.parent != self.root:
self.root.markStuctureViolation(self.token)
self.ed.setInitialized(isDesignated)
elif self.ed.valueType.pointer == self.ed.getEffectiveLevel():
if(self.root.name != '->' and self.ed.parent.parent != self.root) or (self.root.name == '->' and self.root.children[0] != self.ed.parent):
self.root.markStuctureViolation(self.token)
else:
self.ed.parent.setInitialized(isDesignated)
self.ed.parent.initializeChildren()
else:
if self.ed.parent != self.root:
# Check if token is correct value type for self.root.children[?]
child = self.root.getChildByValueElement(self.ed)
if child.elementType != 'record' or self.token.valueType.type != 'record' or child.valueType.typeScope != self.token.valueType.typeScope:
self.root.markStuctureViolation(self.token)
self.ed.setInitialized(isDesignated)
# Mark all elements up to root with positional or designated
# (for complex designators, or missing structure)
parent = self.ed.parent
while parent and parent != self.root:
parent.isDesignated = isDesignated if isDesignated and not parent.isPositional else parent.isDesignated
parent.isPositional = not isDesignated if not isDesignated and not parent.isDesignated else parent.isPositional
parent = parent.parent
isDesignated = False
self.unwindAndContinue()
def pushToRootStackAndMarkAsDesignated(self):
new = self.ed.parent
if new != self.root:
# Mark all elements up to self.root root as designated
parent = new
while parent and parent != self.root:
parent.isDesignated = True
parent = parent.parent
self.rootStack.append((self.root, new))
new.markAsCurrent()
new.childIndex = new.children.index(self.ed) - 1
self.root = new
def popFromStackIfExitElement(self):
if len(self.rootStack) > 0 and self.rootStack[-1][1] == self.root:
old = self.rootStack.pop()[0]
old.markAsCurrent()
self.root = old
def unwindAndContinue(self):
while self.token:
if self.token.astParent.astOperand1 == self.token and self.token.astParent.astOperand2:
if self.ed:
self.ed.markAsCurrent()
self.ed = self.ed.getNextValueElement(self.root)
self.token = self.token.astParent.astOperand2
break
else:
self.token = self.token.astParent
if self.token.str == '{':
if self.root:
self.ed = self.root.getLastValueElement()
self.ed.markAsCurrent()
# Cleanup if root is dummy node representing excess levels in initializer
if self.root.name == '<-':
self.root.children[0].parent = self.root.parent
self.root = self.root.parent
if self.token.astParent is None:
self.token = None
break
def misra_9_x(self, data, rule, rawTokens = None):
parser = InitializerParser()
for variable in data.variables:
if not variable.nameToken:
continue
nameToken = variable.nameToken
# Check if declaration and initialization is
# split into two separate statements in ast.
if nameToken.next and nameToken.next.isSplittedVarDeclEq:
nameToken = nameToken.next.next
# Find declarations with initializer assignment
eq = nameToken
while not eq.isAssignmentOp and eq.astParent:
eq = eq.astParent
# We are only looking for initializers
if not eq.isAssignmentOp or eq.astOperand2.isName:
continue
if variable.isArray or variable.isClass:
ed = getElementDef(nameToken, rawTokens)
parser.parseInitializer(ed, eq.astOperand2)
# print(rule, nameToken.str + '=', ed.getInitDump())
if rule == 902 and not ed.isMisra92Compliant():
self.reportError(nameToken, 9, 2)
if rule == 903 and not ed.isMisra93Compliant():
self.reportError(nameToken, 9, 3)
if rule == 904 and not ed.isMisra94Compliant():
self.reportError(nameToken, 9, 4)
if rule == 905 and not ed.isMisra95Compliant():
self.reportError(nameToken, 9, 5)
def getElementDef(nameToken, rawTokens = None):
if nameToken.variable.isArray:
ed = ElementDef("array", nameToken.str, nameToken.valueType)
createArrayChildrenDefs(ed, nameToken.astParent, rawTokens)
elif nameToken.variable.isClass:
ed = ElementDef("record", nameToken.str, nameToken.valueType)
createRecordChildrenDefs(ed)
else:
ed = ElementDef("value", nameToken.str, nameToken.valueType)
return ed
def createArrayChildrenDefs(ed, token, rawTokens = None):
if token.str == '[':
if rawTokens is not None:
foundToken = next(rawToken for rawToken in rawTokens if rawToken.file == token.file and rawToken.linenr == token.linenr and rawToken.column == token.column)
if foundToken and foundToken.next and foundToken.next.str == ']':
ed.markAsFlexibleArray(token)
if (token.astOperand2 is not None) and (token.astOperand2.getKnownIntValue() is not None):
for i in range(token.astOperand2.getKnownIntValue()):
createChild(ed, token, i)
else:
ed.markAsFlexibleArray(token)
def createChild(ed, token, name):
if token.astParent and token.astParent.str == '[':
child = ElementDef("array", name, ed.valueType)
createArrayChildrenDefs(child, token.astParent)
else:
if ed.valueType and ed.valueType.type == "record":
child = ElementDef("record", name, ed.valueType)
createRecordChildrenDefs(child)
else:
child = ElementDef("value", name, ed.valueType)
ed.addChild(child)
def createRecordChildrenDefs(ed):
valueType = ed.valueType
if not valueType or not valueType.typeScope:
return
for variable in valueType.typeScope.varlist:
child = getElementDef(variable.nameToken)
ed.addChild(child)
def getElementByDesignator(ed, token):
if not token.str in [ '.', '[' ]:
return None
while token.str in [ '.', '[' ]:
token = token.astOperand1
while ed and not token.isAssignmentOp:
token = token.astParent
if token.str == '[':
if not ed.isArray:
ed.markStuctureViolation(token)
chIndex = -1
if token.astOperand2 is not None:
chIndex = token.astOperand2.getKnownIntValue()
elif token.astOperand1 is not None:
chIndex = token.astOperand1.getKnownIntValue()
ed = ed.getChildByIndex(chIndex) if chIndex is not None else None
elif token.str == '.':
if not ed.isRecord:
ed.markStuctureViolation(token)
name = ""
if token.astOperand2 is not None:
name = token.astOperand2.str
elif token.astOperand1 is not None:
name = token.astOperand1.str
ed = ed.getChildByName(name)
return ed