Skip to content

Commit

Permalink
false positive: misra-c2012-8.4
Browse files Browse the repository at this point in the history
  • Loading branch information
swasti16 committed Feb 28, 2024
1 parent 91f15aa commit 74cdc12
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
40 changes: 29 additions & 11 deletions addons/misra.py
Original file line number Diff line number Diff line change
Expand Up @@ -2072,7 +2072,10 @@ def checkDefinitionArgumentsViolations(func, startCall, endCall):
rawTokensFollowingPtr[2].str == ')'):
self.reportError(var.nameToken, 8, 2)


def insert_in_dict(self, dict_name,key, value):
if key not in dict_name:
dict_name[key] = []
dict_name[key].append(value)
def misra_8_4(self, cfg):
for func in cfg.functions:
if func.isStatic:
Expand All @@ -2086,24 +2089,39 @@ def misra_8_4(self, cfg):
if func.tokenDef.str == 'main':
continue
self.reportError(func.tokenDef, 8, 4)

extern_vars = []
var_defs = []

extern_var_with_def = {}
extern_var_without_def = {}
for var in cfg.variables:
if not var.isGlobal:
continue
if var.isStatic:
continue
if var.nameToken is None:
continue
if var.isExtern:
extern_vars.append(var.nameToken.str)
tok = var.nameToken
if tok.next.str == ";":
if tok.next.isSplittedVarDeclEq or (tok.valueType and tok.valueType.type == "record"):
self.insert_in_dict(extern_var_with_def, tok.str, tok)
else:
self.insert_in_dict(extern_var_without_def, tok.str, tok)
else:
var_defs.append(var.nameToken)
for vartok in var_defs:
if vartok.str not in extern_vars:
self.reportError(vartok, 8, 4)
self.insert_in_dict(extern_var_without_def, var.nameToken.str, var.nameToken)

for var in extern_var_with_def:
if var not in extern_var_without_def:
for t in extern_var_with_def[var]:
self.reportError(t, 8, 4)

for var_str, var_tok in extern_var_without_def.items():
warn = True
if var_str not in extern_var_with_def:
for t in var_tok:
if t.variable.isExtern:
warn = False
break
if warn:
for t in var_tok:
self.reportError(t, 8, 4)

def misra_8_5(self, dumpfile, cfg):
self._save_ctu_summary_identifiers(dumpfile, cfg)
Expand Down
2 changes: 0 additions & 2 deletions addons/test/misra/misra-ctu-2-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ extern int misra_8_5;
// cppcheck-suppress misra-c2012-8.4
// cppcheck-suppress misra-c2012-8.6
int32_t misra_8_6 = 2;
// cppcheck-suppress misra-c2012-8.4
int32_t misra_8_6_1;
// cppcheck-suppress misra-c2012-8.7
// cppcheck-suppress misra-c2012-8.4
int32_t misra_8_6_1 = 2;
// cppcheck-suppress misra-c2012-8.4
// cppcheck-suppress misra-c2012-8.7
Expand Down
5 changes: 5 additions & 0 deletions addons/test/misra/misra-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,11 @@ uint8_t misra_8_4_buf2[24]; // 8.4
typedef struct { uint16_t a; uint16_t b; } misra_8_4_struct;
extern misra_8_4_struct bar[42];
misra_8_4_struct bar[42]; // compliant
extern uint16_t misra_8_4_speed = 6000u; //8.4
uint8_t misra_8_4_pressure = 101u; //8.4
int32_t misra_8_4_ext_val2;
int32_t misra_8_4_ext_val2 = 3; // compliant
int32_t misra_8_4_ext_val4; //8.4

static int32_t misra_8_8 = 123;
extern int32_t misra_8_8; // 8.8
Expand Down

0 comments on commit 74cdc12

Please sign in to comment.