diff --git a/addons/misra.py b/addons/misra.py index 59d7ab4f4..3ff77da74 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -808,7 +808,7 @@ def misra_11_4(data): def misra_11_5(data): for token in data.tokenlist: if not isCast(token): - if token.str == "=" and token.next.str != "(": + if token.astOperand1 and token.astOperand2 and token.str == "=" and token.next.str != "(": vt1 = token.astOperand1.valueType vt2 = token.astOperand2.valueType if not vt1 or not vt2: @@ -901,18 +901,19 @@ def misra_11_8(data): def misra_11_9(data): - compiled = re.compile(r'#define ([A-Za-z_][A-Za-z_0-9]*) (.*)') - for directive in data.directives: - res1 = compiled.match(directive.str) - if not res1: - continue - name = res1.group(1) - if name == 'NULL': - continue - value = res1.group(2).replace(' ', '') - if value == '((void*)0)': - reportError(directive, 11, 9) - + for token in data.tokenlist: + if token.astOperand1 and token.astOperand2 and token.str in ["=", "==", "!=", "?", ":"]: + vt1 = token.astOperand1.valueType + vt2 = token.astOperand2.valueType + if not vt1 or not vt2: + continue + if vt1.pointer > 0 and vt2.pointer == 0 and token.astOperand2.str == "NULL": + continue + if (token.astOperand2.values and vt1.pointer > 0 and + vt2.pointer == 0 and token.astOperand2.values): + for val in token.astOperand2.values: + if val.intvalue == 0: + reportError(token, 11, 9) def misra_12_1_sizeof(rawTokens): state = 0 diff --git a/addons/test/misra-test.c b/addons/test/misra-test.c index 377b0be88..3ae674666 100644 --- a/addons/test/misra-test.c +++ b/addons/test/misra-test.c @@ -203,7 +203,20 @@ char * misra_11_8(const char *str) { return (char *)str; // 11.8 } -#define MISRA_11_9 ((void*)0) // 11.9 +#define MISRA_11_9_NULL_1 (1-1) +#define MISRA_11_9_NULL_2 ( void * ) 0 +#define MISRA_11_9_NULL_3 NULL +void misra_11_9(){ + int *p1 = (5-5); //11.9 + int *p2 = MISRA_11_9_NULL_2 ; // no-warning + int *p3 = MISRA_11_9_NULL_3 ; // no-warning + if ( p1 == MISRA_11_9_NULL_1 ) //11.9 + { + ; + } + +} + void misra_12_1() { sz = sizeof x + y; // 12.1