Added rule 5.2 (#1212)

* Added rule 5.2

* updated 5.2

request-checks: true

* Updated test suite for Rule 5.2
This commit is contained in:
swasti16 2018-05-09 00:11:45 +05:30 committed by Daniel Marjamäki
parent c266688784
commit d934065d21
2 changed files with 80 additions and 2 deletions

View File

@ -424,6 +424,47 @@ def misra_5_1(data):
if token.isName and len(token.str) > 31:
reportError(token, 5, 1)
def misra_5_2(data):
scopeVars = {}
for var in data.variables:
if var.nameToken.scope not in scopeVars:
scopeVars.setdefault(var.nameToken.scope, {})["varlist"] = []
scopeVars.setdefault(var.nameToken.scope, {})["scopelist"] = []
scopeVars[var.nameToken.scope]["varlist"].append(var)
for scope in data.scopes:
if scope.nestedIn and scope.className:
if scope.nestedIn not in scopeVars:
scopeVars.setdefault(scope.nestedIn, {})["varlist"] = []
scopeVars.setdefault(scope.nestedIn, {})["scopelist"] = []
scopeVars[scope.nestedIn]["scopelist"].append(scope)
for scope in scopeVars:
if len(scopeVars[scope]["varlist"]) <= 1:
continue
for i, variable1 in enumerate(scopeVars[scope]["varlist"]):
for variable2 in scopeVars[scope]["varlist"][i + 1:]:
if variable1.isArgument and variable2.isArgument:
continue
if (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, 2)
else:
reportError(variable2.nameToken, 5, 2)
for innerscope in scopeVars[scope]["scopelist"]:
if (variable1.nameToken.str[:31] == innerscope.className[:31]):
if int(variable1.nameToken.linenr) > int(innerscope.bodyStart.linenr):
reportError(variable1.nameToken, 5, 2)
else:
reportError(innerscope.bodyStart, 5, 2)
if len(scopeVars[scope]["scopelist"]) <= 1:
continue
for i, scopename1 in enumerate(scopeVars[scope]["scopelist"]):
for scopename2 in scopeVars[scope]["scopelist"][i + 1:]:
if (scopename1.className[:31] == scopename2.className[:31]):
if int(scopename1.bodyStart.linenr) > int(scopename2.bodyStart.linenr):
reportError(scopename1.bodyStart, 5, 2)
else:
reportError(scopename2.bodyStart, 5, 2)
def misra_5_3(data):
scopeVars = {}
@ -1420,6 +1461,7 @@ for arg in sys.argv[1:]:
misra_3_1(data.rawTokens)
misra_4_1(data.rawTokens)
misra_5_1(cfg)
misra_5_2(cfg)
misra_5_3(cfg)
misra_5_4(cfg)
misra_5_5(cfg)

View File

@ -14,12 +14,48 @@ 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
void foo(void)
{
int i;
switch(f1())
{
case 1:
{
do
{
for(i = 0; i < 10; i++)
{
if(f3())
{
int
n03_var_hides_var____________________________________________63x;
int
n03_var_hides_var____________________________________________63y;//5.2
}
}
} while(f2());
}
}
}
union n06_field_hides_field________________________________________63x {
int n04_field_hides_field________________________________________63x;
int n04_field_hides_field________________________________________63y;//5.2
};
struct n06_field_hides_field________________________________________63y { //5.2
int n05_field_hides_field________________________________________63x;
int n05_field_hides_field________________________________________63y;//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
int a1234567890123456789012345678901; // 5.1 ,5.2
}
void misra_5_3() {
@ -41,7 +77,7 @@ void misra_7_1() {
void misra_7_3() {
int x = 12l; // 7.3
int x = 12lu; // 7.3
int x = 12lu; // 7.3, 5.2
}
extern int a811[]; // 8.11