Skip to content

Commit d4e1dd4

Browse files
committed
Fix 206: false positive: misra-c2012-2.5
1 parent 92176f7 commit d4e1dd4

3 files changed

Lines changed: 27 additions & 11 deletions

File tree

addons/misra.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,16 +1588,23 @@ 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+'_define'] = {'name': macro_name+'_define', '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+
unused_macro[macro_name+'_undef'] = {'name': macro_name + '_undef', 'used': (macro_name in used_macros),
1604+
'file': directive.file,
1605+
'line': directive.linenr, 'column': directive.column}
1606+
if unused_macro:
1607+
cppcheckdata.reportSummary(dumpfile, 'MisraMacro', list(unused_macro.values()))
16011608

16021609
def misra_2_7(self, data):
16031610
for func in data.functions:
@@ -4801,10 +4808,15 @@ def is_different_file(loc1, loc2):
48014808
unused_tags = [tag for tag in all_tagname_info.values() if not tag['used']]
48024809
for tag in unused_tags:
48034810
self.reportError(Location(tag), 2, 4)
4804-
4805-
unused_macros = [m for m in all_macro_info.values() if not m['used']]
4806-
for m in unused_macros:
4807-
self.reportError(Location(m), 2, 5)
4811+
for m_key,m_value in all_macro_info.items():
4812+
i = m_key.rfind('_')
4813+
m_key_name,m_directive = m_key[:i], m_key[i+1: ]
4814+
if m_directive == 'undef' and m_key_name + '_define' in all_macro_info:
4815+
continue
4816+
if m_directive == 'define' and m_key_name + '_undef' in all_macro_info:
4817+
continue
4818+
if not m_value['used']:
4819+
self.reportError(Location(m_value), 2, 5)
48084820

48094821
all_external_identifiers = all_external_identifiers_decl
48104822
all_external_identifiers.update(all_external_identifiers_def)

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)