MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / misra_9_x

Function misra_9_x

addons/misra_9.py:396–443  ·  view source on GitHub ↗
(self, data, rule, rawTokens = None)

Source from the content-addressed store, hash-verified

394 break
395
396def misra_9_x(self, data, rule, rawTokens = None):
397
398 parser = InitializerParser()
399
400 for variable in data.variables:
401 if variable.nameToken is None:
402 continue
403
404 nameToken = variable.nameToken
405
406 # Check if declaration and initialization is
407 # split into two separate statements in ast.
408 if nameToken.next and nameToken.next.isSplittedVarDeclEq:
409 nameToken = nameToken.next.next
410
411 # Find declarations with initializer assignment
412 eq = nameToken
413 while not eq.isAssignmentOp and eq.astParent:
414 eq = eq.astParent
415
416 # We are only looking for initializers
417 if not eq.isAssignmentOp or eq.astOperand2.isName:
418 continue
419
420 if variable.isArray or variable.isClass:
421 ed = getElementDef(nameToken, rawTokens)
422 # No need to check non-arrays if valueType is missing,
423 # since we can't say anything useful about the structure
424 # without it.
425 if ed.valueType is None and not variable.isArray:
426 continue
427 parser.parseInitializer(ed, eq.astOperand2)
428 # print(rule, nameToken.str + '=', ed.getInitDump())
429 if rule == 902 and not ed.isMisra92Compliant():
430 self.reportError(nameToken, 9, 2)
431 if rule == 903 and not ed.isMisra93Compliant():
432 # Do not check when variable is pointer type
433 type_token = variable.nameToken
434 while type_token and type_token.isName:
435 type_token = type_token.previous
436 if type_token and type_token.str == '*':
437 continue
438
439 self.reportError(nameToken, 9, 3)
440 if rule == 904 and not ed.isMisra94Compliant():
441 self.reportError(nameToken, 9, 4)
442 if rule == 905 and not ed.isMisra95Compliant():
443 self.reportError(nameToken, 9, 5)
444
445def getElementDef(nameToken, rawTokens = None):
446 if nameToken.variable.isArray:

Callers

nothing calls this directly

Calls 8

parseInitializerMethod · 0.95
InitializerParserClass · 0.85
getElementDefFunction · 0.85
isMisra92CompliantMethod · 0.80
isMisra93CompliantMethod · 0.80
isMisra94CompliantMethod · 0.80
isMisra95CompliantMethod · 0.80
reportErrorMethod · 0.45

Tested by

no test coverage detected