Skip to content

Commit

Permalink
Fix 206: false positive: misra-c2012-2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
swasti16 committed Dec 13, 2024
1 parent 92176f7 commit c712b5e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
64 changes: 46 additions & 18 deletions addons/misra.py
Original file line number Diff line number Diff line change
Expand Up @@ -1588,16 +1588,33 @@ def misra_2_4(self, dumpfile, cfg):

def misra_2_5(self, dumpfile, cfg):
used_macros = []
unused_macro = {}
for m in cfg.macro_usage:
used_macros.append(m.name)
summary = []
for directive in cfg.directives:
res = re.match(r'#define[ \t]+([a-zA-Z_][a-zA-Z_0-9]*).*', directive.str)
if res:
macro_name = res.group(1)
summary.append({'name': macro_name, 'used': (macro_name in used_macros), 'file': directive.file, 'line': directive.linenr, 'column': directive.column})
if len(summary) > 0:
cppcheckdata.reportSummary(dumpfile, 'MisraMacro', summary)
res_define = re.match(r'#define[ \t]+([a-zA-Z_][a-zA-Z_0-9]*).*', directive.str)
res_undef = re.match(r'#undef[ \t]+([a-zA-Z_][a-zA-Z_0-9]*).*', directive.str)
if res_define:
macro_name = res_define.group(1)
macro_dict = {'name': macro_name, 'used': (macro_name in used_macros),
'file': directive.file, 'line': directive.linenr, 'column': directive.column}
if macro_name in unused_macro:
unused_macro[macro_name].append(macro_dict)
else:
unused_macro[macro_name] = [macro_dict]
elif res_undef:
macro_name = res_undef.group(1)
# assuming that if we have #undef, we also have #define somewhere
macro_dict = {'name': macro_name, 'used': True, 'file': directive.file,
'line': directive.linenr, 'column': directive.column}
if macro_name in unused_macro:
unused_macro[macro_name].append(macro_dict)
else:
unused_macro[macro_name] = [macro_dict]
used_macros.append(macro_name)

if unused_macro:
cppcheckdata.reportSummary(dumpfile, 'MisraMacro', list(unused_macro.values()))

def misra_2_7(self, data):
for func in data.functions:
Expand Down Expand Up @@ -4746,13 +4763,17 @@ def is_different_file(loc1, loc2):
all_tagname_info[key] = new_tagname_info

if summary_type == 'MisraMacro':
for new_macro in summary_data:
key = new_macro['name']
existing_macro = all_macro_info.get(key, None)
if existing_macro:
existing_macro['used'] = existing_macro['used'] or new_macro['used']
else:
all_macro_info[key] = new_macro
for _list in summary_data:
for new_macro in _list:
key = new_macro['name']
key_loc = (new_macro['file'], new_macro['line'], new_macro['column'])
macro_dict = {key_loc: new_macro['used']}
if key not in all_macro_info:
all_macro_info[key] = macro_dict
elif key_loc not in all_macro_info[key]:
all_macro_info[key][key_loc] = new_macro['used']
else:
all_macro_info[key][key_loc] = all_macro_info[key][key_loc] or new_macro['used']

if summary_type == 'MisraExternalIdentifiers':
for s in sorted(summary_data, key=lambda d: "%s %s %s" %(d['file'],d['line'], d['column'] )):
Expand Down Expand Up @@ -4801,10 +4822,17 @@ def is_different_file(loc1, loc2):
unused_tags = [tag for tag in all_tagname_info.values() if not tag['used']]
for tag in unused_tags:
self.reportError(Location(tag), 2, 4)

unused_macros = [m for m in all_macro_info.values() if not m['used']]
for m in unused_macros:
self.reportError(Location(m), 2, 5)
for m_dict in all_macro_info.values():
used_cnt = 0
last_unused = None
for m_loc, m_used in m_dict.items():
if m_used:
used_cnt += 1
else:
used_cnt -= 1
last_unused = {'file': m_loc[0], 'line': m_loc[1], 'column': m_loc[2]}
if used_cnt != 0 and last_unused:
self.reportError(Location(last_unused), 2, 5)

all_external_identifiers = all_external_identifiers_decl
all_external_identifiers.update(all_external_identifiers_def)
Expand Down
6 changes: 6 additions & 0 deletions addons/test/misra/misra-ctu-1-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ extern MISRA_2_3_A misra_2_3_a;

x = MISRA_2_5_OK_1;

// cppcheck-suppress misra-c2012-20.5
#undef MISRA_2_5_OK_3

// cppcheck-suppress misra-c2012-20.5
#undef MISRA_2_5_warn_3

// cppcheck-suppress misra-c2012-2.3
// cppcheck-suppress misra-c2012-5.6
typedef int MISRA_5_6_VIOLATION;
Expand Down
3 changes: 3 additions & 0 deletions addons/test/misra/misra-ctu-2-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

extern MISRA_2_3_B misra_2_3_b;

// cppcheck-suppress misra-c2012-2.5
#define MISRA_2_5_warn_3 This

x = MISRA_2_5_OK_2;

// cppcheck-suppress misra-c2012-5.6
Expand Down
3 changes: 3 additions & 0 deletions addons/test/misra/misra-ctu-test.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ void misra_8_7_external(void);
// #12362
extern void misra_8_7_compliant( void );

#define MISRA_2_5_OK_3 This

#define MISRA_2_5_warn_3 This

0 comments on commit c712b5e

Please sign in to comment.