diff --git a/addons/misra.py b/addons/misra.py index 5fd5876b8..e2b9a4e98 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -420,9 +420,23 @@ def misra_4_1(rawTokens): def misra_5_1(data): - for token in data.tokenlist: - if token.isName and len(token.str) > 31: - reportError(token, 5, 1) + scopeVars = {} + for var in data.variables: + if var.isArgument: + continue + if var.nameToken.scope not in scopeVars: + scopeVars[var.nameToken.scope] = [] + scopeVars[var.nameToken.scope].append(var) + for scope in scopeVars: + for i, variable1 in enumerate(scopeVars[scope]): + for variable2 in scopeVars[scope][i + 1:]: + if (variable1.isExtern and variable2.isExtern and + variable1.nameToken.str[:31] == variable2.nameToken.str[:31] and + variable1.Id != variable2.Id): + if int(variable1.nameToken.linenr) > int(variable2.nameToken.linenr): + reportError(variable1.nameToken, 5, 1) + else: + reportError(variable2.nameToken, 5, 1) def misra_5_2(data): scopeVars = {} @@ -444,6 +458,8 @@ def misra_5_2(data): for variable2 in scopeVars[scope]["varlist"][i + 1:]: if variable1.isArgument and variable2.isArgument: continue + if variable1.isExtern and variable2.isExtern: + continue if (variable1.nameToken.str[:31] == variable2.nameToken.str[:31] and variable1.Id != variable2.Id): if int(variable1.nameToken.linenr) > int(variable2.nameToken.linenr): @@ -466,6 +482,8 @@ def misra_5_2(data): else: reportError(scopename2.bodyStart, 5, 2) + + def misra_5_3(data): enum = [] scopeVars = {} diff --git a/addons/test/misra-test.c b/addons/test/misra-test.c index 134ff188f..b5952ab6e 100644 --- a/addons/test/misra-test.c +++ b/addons/test/misra-test.c @@ -14,14 +14,17 @@ typedef unsigned long long u64; //// 3.1 -extern int n01_var_hides_var____________________________________________63x; -static int n01_var_hides_var____________________________________________63y;//5.2 -static int n02_function_hides_var_______________________________________63x; -void n02_function_hides_var_______________________________________63y(void) {}//5.2 +extern int misra_5_1_extern_var_hides_var_x; +extern int misra_5_1_extern_var_hides_var_y; //5.1 + +extern int misra_5_2_var_hides_var______31x; +static int misra_5_2_var_hides_var______31y;//5.2 +static int misra_5_2_function_hides_var_31x; +void misra_5_2_function_hides_var_31y(void) {}//5.2 void foo(void) { int i; - switch(f1()) + switch(misra_5_2_func1()) //16.4 16.6 { case 1: { @@ -29,35 +32,28 @@ void foo(void) { for(i = 0; i < 10; i++) { - if(f3()) + if(misra_5_2_func3()) //14.4 { - int - n03_var_hides_var____________________________________________63x; - int - n03_var_hides_var____________________________________________63y;//5.2 + int misra_5_2_var_hides_var_1____31x; + int misra_5_2_var_hides_var_1____31y;//5.2 } } - } while(f2()); + } while(misra_5_2_func2()); //14.4 } } } -union n06_field_hides_field________________________________________63x { -int n04_field_hides_field________________________________________63x; -int n04_field_hides_field________________________________________63y;//5.2 +union misra_5_2_field_hides_field__63x { //19.2 +int misra_5_2_field_hides_field__31x; +int misra_5_2_field_hides_field__31y;//5.2 }; -struct n06_field_hides_field________________________________________63y { //5.2 -int n05_field_hides_field________________________________________63x; -int n05_field_hides_field________________________________________63y;//5.2 +struct misra_5_2_field_hides_field__63y { //5.2 +int misra_5_2_field_hides_field1_31x; +int misra_5_2_field_hides_field1_31y;//5.2 }; const char *s41_1 = "\x41g"; // 4.1 const char *s41_2 = "\x41\x42"; -void misra_5_1() { - int a123456789012345678901234567890; // no-warning - int a1234567890123456789012345678901; // 5.1 5.2 -} - extern int misra_5_3_var_hides_var______31x; void misra_5_3_var_hides_function_31x (void) {} enum misra_5_3_Enum { @@ -68,18 +64,18 @@ void misra_5_3_func1(void) int misra_5_3_var_hides_var______31y; //5.3 int misra_5_3_var_hides_function_31y; //5.3 int misra_5_3_var_hidesenumconst_31y; //5.3 - switch(misra_5_3_func2()) + switch(misra_5_3_func2()) //16.4 16.6 { case 1: { do { int misra_5_3_var_hides_var_1____31x; - if(misra_5_3_func3()) + if(misra_5_3_func3()) //14.4 { int misra_5_3_var_hides_var_1____31y = 1; //5.3 } - } while(misra_5_3_func2()); + } while(misra_5_3_func2()); //14.4 } } } @@ -105,17 +101,17 @@ int x; }; void misra_5_5_func1() { - switch(misra_5_5_func2()) + switch(misra_5_5_func2()) //16.4 16.6 { case 1: { do { - if(misra_5_5_func3()) + if(misra_5_5_func3()) //14.4 { int misra_5_5_hides_macro________31y; //5.5 } - } while(misra_5_5_func2()); + } while(misra_5_5_func2()); //14.4 } } }