Skip to content

Commit 4b873ed

Browse files
swasti16danmar
authored andcommitted
Rule 5.3 modified along with test suite (danmar#1227)
* Added rule 5.2 * updated 5.2 request-checks: true * Added rule 5.3 * Changed rule 5.4, 5.5 * Updated test suite for Rule 5.2 * Changes in Rule 5.4 and 5.5 * Change in function name in test suite and removed type from class token in cppcheck * Changed the name of function in misra-test.c * Modified rule 5.3 * Modified misra-test.c for rule 5.3
1 parent d2d1bf9 commit 4b873ed

2 files changed

Lines changed: 58 additions & 17 deletions

File tree

addons/misra.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -467,36 +467,56 @@ def misra_5_2(data):
467467
reportError(scopename2.bodyStart, 5, 2)
468468

469469
def misra_5_3(data):
470+
enum = []
470471
scopeVars = {}
471472
for var in data.variables:
472-
if var.isArgument:
473-
# TODO
474-
continue
475473
if var.nameToken.scope not in scopeVars:
476474
scopeVars[var.nameToken.scope] = []
477475
scopeVars[var.nameToken.scope].append(var)
478-
479476
for innerScope in data.scopes:
480-
if innerScope.type == 'Global':
477+
if innerScope.type == "Enum":
478+
enum_token = innerScope.bodyStart.next
479+
while enum_token != innerScope.bodyEnd:
480+
if enum_token.values and enum_token.isName:
481+
enum.append(enum_token.str)
482+
enum_token = enum_token.next
481483
continue
482484
if innerScope not in scopeVars:
483485
continue
486+
if innerScope.type == "Global":
487+
continue
484488
for innerVar in scopeVars[innerScope]:
485489
outerScope = innerScope.nestedIn
486490
while outerScope:
487491
if outerScope not in scopeVars:
488492
outerScope = outerScope.nestedIn
489493
continue
490-
found = False
491494
for outerVar in scopeVars[outerScope]:
492-
if innerVar.nameToken.str == outerVar.nameToken.str:
493-
found = True
494-
break
495-
if found:
496-
reportError(innerVar.nameToken, 5, 3)
497-
break
495+
if innerVar.nameToken.str[:31] == outerVar.nameToken.str[:31]:
496+
if outerVar.isArgument and outerScope.type == "Global" and not innerVar.isArgument:
497+
continue
498+
if int(innerVar.nameToken.linenr) > int(outerVar.nameToken.linenr):
499+
reportError(innerVar.nameToken, 5, 3)
500+
else:
501+
reportError(outerVar.nameToken, 5, 3)
498502
outerScope = outerScope.nestedIn
503+
for scope in data.scopes:
504+
if (scope.className and innerVar.nameToken.str[:31] == scope.className[:31]):
505+
if int(innerVar.nameToken.linenr) > int(scope.bodyStart.linenr):
506+
reportError(innerVar.nameToken, 5, 3)
507+
else:
508+
reportError(scope.bodyStart, 5, 3)
499509

510+
for e in enum:
511+
if scope.className and innerVar.nameToken.str[:31] == e[:31]:
512+
if int(innerVar.nameToken.linenr) > int(innerScope.bodyStart.linenr):
513+
reportError(innerVar.nameToken, 5, 3)
514+
else:
515+
reportError(innerScope.bodyStart, 5, 3)
516+
for e in enum:
517+
for scope in data.scopes:
518+
if (scope.className and scope.className[:31] == e[:31]):
519+
reportError(scope.bodyStart, 5, 3)
500520

501521
def misra_5_4(data):
502522
macro = {}

addons/test/misra-test.c

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,33 @@ void misra_5_1() {
5858
int a1234567890123456789012345678901; // 5.1 ,5.2
5959
}
6060

61-
void misra_5_3() {
62-
u8 x=1;
63-
if (y!=0) {
64-
u8 x=2; // 5.3
65-
} else {}
61+
extern int misra_5_3_var_hides_var______31x;
62+
void misra_5_3_var_hides_function_31x (void) {}
63+
enum misra_5_3_Enum {
64+
misra_5_3_var_hidesenumconst_31x = 2,misra_5_3_enum_hidesfunction_31x = 5
65+
};
66+
void misra_5_3_func1(void)
67+
{
68+
int misra_5_3_var_hides_var______31y; //5.3
69+
int misra_5_3_var_hides_function_31y; //5.3
70+
int misra_5_3_var_hidesenumconst_31y; //5.3
71+
switch(misra_5_3_func2())
72+
{
73+
case 1:
74+
{
75+
do
76+
{
77+
int misra_5_3_var_hides_var_1____31x;
78+
if(misra_5_3_func3())
79+
{
80+
int misra_5_3_var_hides_var_1____31y = 1; //5.3
81+
}
82+
} while(misra_5_3_func2());
83+
}
84+
}
6685
}
86+
void misra_5_3_enum_hidesfunction_31y(void) {} //5.3
87+
6788

6889
#define misra_5_4_macro_hides_macro__31x 1
6990
#define misra_5_4_param_hides_macro__31x 1

0 commit comments

Comments
 (0)