Skip to content

Commit d7c8ad3

Browse files
authored
Fix 206: false positive: misra-c2012-2.5 (danmar#7081)
1 parent 3e34495 commit d7c8ad3

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

addons/misra.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,16 +1588,28 @@ def misra_2_4(self, dumpfile, cfg):
15881588

15891589
def misra_2_5(self, dumpfile, cfg):
15901590
used_macros = []
1591+
unused_macro = {}
15911592
for m in cfg.macro_usage:
15921593
used_macros.append(m.name)
1593-
summary = []
15941594
for directive in cfg.directives:
1595-
res = re.match(r'#define[ \t]+([a-zA-Z_][a-zA-Z_0-9]*).*', directive.str)
1596-
if res:
1597-
macro_name = res.group(1)
1598-
summary.append({'name': macro_name, 'used': (macro_name in used_macros), 'file': directive.file, 'line': directive.linenr, 'column': directive.column})
1599-
if len(summary) > 0:
1600-
cppcheckdata.reportSummary(dumpfile, 'MisraMacro', summary)
1595+
res_define = re.match(r'#define[ \t]+([a-zA-Z_][a-zA-Z_0-9]*).*', directive.str)
1596+
res_undef = re.match(r'#undef[ \t]+([a-zA-Z_][a-zA-Z_0-9]*).*', directive.str)
1597+
if res_define:
1598+
macro_name = res_define.group(1)
1599+
unused_macro[macro_name] = {'name': macro_name, 'used': (macro_name in used_macros),
1600+
'file': directive.file, 'line': directive.linenr, 'column': directive.column}
1601+
elif res_undef:
1602+
macro_name = res_undef.group(1)
1603+
# assuming that if we have #undef, we also have #define somewhere
1604+
if macro_name in unused_macro:
1605+
unused_macro[macro_name]['used'] = True
1606+
else:
1607+
unused_macro[macro_name] = {'name': macro_name, 'used': True, 'file': directive.file,
1608+
'line': directive.linenr, 'column': directive.column}
1609+
used_macros.append(macro_name)
1610+
1611+
if unused_macro:
1612+
cppcheckdata.reportSummary(dumpfile, 'MisraMacro', list(unused_macro.values()))
16011613

16021614
def misra_2_7(self, data):
16031615
for func in data.functions:

addons/test/misra/misra-ctu-1-test.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ extern MISRA_2_3_A misra_2_3_a;
77

88
x = MISRA_2_5_OK_1;
99

10+
// cppcheck-suppress misra-c2012-20.5
11+
#undef MISRA_2_5_OK_3
12+
1013
// cppcheck-suppress misra-c2012-2.3
1114
// cppcheck-suppress misra-c2012-5.6
1215
typedef int MISRA_5_6_VIOLATION;

addons/test/misra/misra-ctu-test.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ void misra_8_7_external(void);
2121
// #12362
2222
extern void misra_8_7_compliant( void );
2323

24+
#define MISRA_2_5_OK_3 This

0 commit comments

Comments
 (0)