Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions addons/misra.py
Original file line number Diff line number Diff line change
Expand Up @@ -1588,16 +1588,28 @@ 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)
unused_macro[macro_name] = {'name': macro_name, 'used': (macro_name in used_macros),
'file': directive.file, 'line': directive.linenr, 'column': directive.column}
elif res_undef:
macro_name = res_undef.group(1)
# assuming that if we have #undef, we also have #define somewhere
if macro_name in unused_macro:
unused_macro[macro_name]['used'] = True
else:
unused_macro[macro_name] = {'name': macro_name, 'used': True, 'file': directive.file,
'line': directive.linenr, 'column': directive.column}
used_macros.append(macro_name)
Comment thread
danmar marked this conversation as resolved.
Comment thread
danmar marked this conversation as resolved.

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

def misra_2_7(self, data):
for func in data.functions:
Expand Down
3 changes: 3 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,9 @@ 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-2.3
// cppcheck-suppress misra-c2012-5.6
typedef int MISRA_5_6_VIOLATION;
Expand Down
1 change: 1 addition & 0 deletions addons/test/misra/misra-ctu-test.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ void misra_8_7_external(void);
// #12362
extern void misra_8_7_compliant( void );

#define MISRA_2_5_OK_3 This