diff --git a/addons/cppcheckdata.py b/addons/cppcheckdata.py index 0e81f1f31..7f54e11ea 100644 --- a/addons/cppcheckdata.py +++ b/addons/cppcheckdata.py @@ -73,6 +73,7 @@ class Token: function If this token points at a function call, this attribute has the Function information. See the Function class. values Possible values of token + valueType type information typeScope type scope (token->type()->classScope) astParent ast parent astOperand1 ast operand1 @@ -121,6 +122,7 @@ class Token: function = None valuesId = None values = None + valueType = None typeScopeId = None typeScope = None @@ -179,6 +181,7 @@ class Token: self.function = None self.valuesId = element.get('values') self.values = None + self.valueType = element.get('valueType') self.typeScopeId = element.get('type-scope') self.typeScope = None self.astParentId = element.get('astParent') diff --git a/addons/misra-test.c b/addons/misra-test.c index 23ff2446e..e4fb73230 100644 --- a/addons/misra-test.c +++ b/addons/misra-test.c @@ -23,6 +23,10 @@ void misra_7_3() { int x = 12lu; // 7.3 } +char * misra_11_8(const char *str) { + return (char *)str; // 11.8 +} + #define MISRA_11_9 ((void*)0) // 11.9 void misra_12_1() { diff --git a/addons/misra.py b/addons/misra.py index 29411779e..8659c77fd 100644 --- a/addons/misra.py +++ b/addons/misra.py @@ -96,6 +96,9 @@ def bitsOfEssentialType(expr): return INT_BIT return 0 +def isCast(expr): + return expr and expr.str == '(' and expr.astOperand1 == expr.link.next + def isFunctionCall(expr): if not expr: return False @@ -263,6 +266,18 @@ def misra_12_1_sizeof(rawTokens): else: state = 0 +def misra_11_8(data): + for token in data.tokenlist: + if not isCast(token): + continue + if not token.valueType or not token.astOperand1.valueType: + continue + if token.valueType.startswith('const') or not token.astOperand1.valueType.startswith('const'): + continue + if token.valueType.find('*')<0 or token.astOperand1.valueType.find('*')<0: + continue + reportError(token, 11, 8) + def misra_11_9(data): for directive in data.directives: res1 = re.match(r'#define ([A-Za-z_][A-Za-z_0-9]*) (.*)', directive.str) @@ -716,7 +731,8 @@ for arg in sys.argv[1:]: if cfgNumber == 1: misra_7_1(data.rawTokens) misra_7_3(data.rawTokens) - misra_11_9(cfg); + misra_11_8(cfg) + misra_11_9(cfg) if cfgNumber == 1: misra_12_1_sizeof(data.rawTokens) misra_12_1(cfg)