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

Method misra_7_4

addons/misra.py:2068–2105  ·  view source on GitHub ↗
(self, data)

Source from the content-addressed store, hash-verified

2066 self.reportError(tok, 7, 3)
2067
2068 def misra_7_4(self, data):
2069 # A string literal shall not be assigned to an object unless the object's type
2070 # is constant.
2071 def reportErrorIfVariableIsNotConst(variable, stringLiteral):
2072 if variable.valueType:
2073 if (variable.valueType.constness % 2) != 1:
2074 self.reportError(stringLiteral, 7, 4)
2075
2076 for token in data.tokenlist:
2077 if token.isString:
2078 # Check normal variable assignment
2079 variable = getAssignedVariableToken(token)
2080 if variable:
2081 reportErrorIfVariableIsNotConst(variable, token)
2082
2083 # Check use as return value
2084 function = getFunctionUsingReturnValue(token)
2085 if function:
2086 # "Primitive" test since there is no info available on return value type
2087 if not tokenFollowsSequence(function.tokenDef, ['const', 'char', '*']):
2088 self.reportError(token, 7, 4)
2089
2090 # Check use as function parameter
2091 if isFunctionCall(token, data.standards.c) and token.astOperand1 and token.astOperand1.function:
2092 functionDeclaration = token.astOperand1.function
2093
2094 if functionDeclaration.tokenDef:
2095 if functionDeclaration.tokenDef is token.astOperand1:
2096 # Token is not a function call, but it is the definition of the function
2097 continue
2098
2099 parametersUsed = getArguments(token)
2100 for i in range(len(parametersUsed)):
2101 usedParameter = parametersUsed[i]
2102 parameterDefinition = functionDeclaration.argument.get(i+1)
2103
2104 if usedParameter.isString and parameterDefinition and parameterDefinition.nameToken:
2105 reportErrorIfVariableIsNotConst(parameterDefinition.nameToken, usedParameter)
2106
2107 def misra_8_1(self, cfg):
2108 for token in cfg.tokenlist:

Callers

nothing calls this directly

Calls 7

reportErrorMethod · 0.95
getAssignedVariableTokenFunction · 0.85
tokenFollowsSequenceFunction · 0.85
isFunctionCallFunction · 0.70
getArgumentsFunction · 0.70
getMethod · 0.45

Tested by

no test coverage detected