Skip to content

Commit 7b80e44

Browse files
committed
Fix #13957 (False positive: Misra C 2012 2.5: Macro usage not seen in code that is ifeffed out)
1 parent c191f3b commit 7b80e44

3 files changed

Lines changed: 516 additions & 1 deletion

File tree

‎addons/misra.py‎

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1765,12 +1765,132 @@ def misra_2_3(self, dumpfile, typedefInfo):
17651765
def misra_2_4(self, dumpfile, cfg):
17661766
self._save_ctu_summary_tagnames(dumpfile, cfg)
17671767

1768+
@staticmethod
1769+
def _get_active_directives(cfg):
1770+
# a condition evaluated in several inclusions of a file can have both results
1771+
results = {}
1772+
for cond in cfg.preprocessor_if_conditions:
1773+
results.setdefault((cond.file, cond.linenr), set()).add(cond.result != 0)
1774+
1775+
directives_of_file = {}
1776+
for directive in cfg.directives:
1777+
directives_of_file.setdefault(directive.file, []).append(directive)
1778+
1779+
in_active_branch = set()
1780+
for directives in directives_of_file.values():
1781+
stack = []
1782+
active = True
1783+
for directive in directives:
1784+
res = re.match(r'#\s*([a-z]*)', directive.str)
1785+
kind = res.group(1) if res else ''
1786+
if kind in ('if', 'ifdef', 'ifndef'):
1787+
values = results.get((directive.file, directive.linenr), {False, True})
1788+
stack.append([active, active and False in values])
1789+
active = active and True in values
1790+
elif kind in ('elif', 'elifdef', 'elifndef', 'else') and stack:
1791+
fallthrough = stack[-1][1]
1792+
values = {True} if kind == 'else' else results.get((directive.file, directive.linenr), {False, True})
1793+
active = fallthrough and True in values
1794+
stack[-1][1] = fallthrough and False in values
1795+
elif kind == 'endif' and stack:
1796+
active = stack.pop()[0]
1797+
elif active:
1798+
in_active_branch.add(id(directive))
1799+
1800+
def file_name(path):
1801+
return os.path.normcase(os.path.basename(path))
1802+
1803+
def included_path(directive):
1804+
# '' for an #include that does not name its file (#include MACRO)
1805+
res = re.match(r'#\s*include\b\s*(?:["<]([^">]+)[">])?', directive.str)
1806+
if not res:
1807+
return None
1808+
return res.group(1) or ''
1809+
1810+
def included_name(directive):
1811+
path = included_path(directive)
1812+
return file_name(path) if path else path
1813+
1814+
def components(path):
1815+
return os.path.normcase(os.path.normpath(path)).replace('\\', '/').split('/')
1816+
1817+
def same_end(file, path):
1818+
# the path of the dump and the path of the #include end with the same components ('..' excluded)
1819+
a = components(file)
1820+
b = [part for part in components(path) if part != '..']
1821+
n = min(len(a), len(b))
1822+
return a[-n:] == b[-n:]
1823+
1824+
found_paths = {}
1825+
1826+
def is_named(file, name):
1827+
# the dump names a header after its first inclusion, another name can reach it (hard link)
1828+
path = os.path.join(os.path.dirname(file), name)
1829+
if path not in found_paths:
1830+
found_paths[path] = os.path.exists(path)
1831+
try:
1832+
return found_paths[path] and os.path.samefile(path, file)
1833+
except OSError:
1834+
return False
1835+
1836+
dirs_of_entry = {}
1837+
1838+
def dirs_having(name):
1839+
if not dirs_of_entry:
1840+
for directory in files_of_dir:
1841+
try:
1842+
for entry in os.listdir(directory or '.'):
1843+
dirs_of_entry.setdefault(os.path.normcase(entry), []).append(directory)
1844+
except OSError:
1845+
pass
1846+
return dirs_of_entry.get(name, [])
1847+
1848+
files_of_name = {}
1849+
files_named = {}
1850+
files_of_dir = {}
1851+
for file in directives_of_file:
1852+
files_of_name.setdefault(file_name(file), []).append(file)
1853+
files_of_dir.setdefault(os.path.dirname(file), []).append(file)
1854+
included_names = set(filter(None, (included_name(directive) for directive in cfg.directives)))
1855+
active_files = set(file for file in directives_of_file if file_name(file) not in included_names)
1856+
# the directives of the source file come first
1857+
active_files.update(directive.file for directive in cfg.directives[:1])
1858+
pending = list(active_files)
1859+
while pending:
1860+
for directive in directives_of_file[pending.pop()]:
1861+
name = included_name(directive) if id(directive) in in_active_branch else None
1862+
if name == '':
1863+
files = list(directives_of_file)
1864+
elif name:
1865+
files = files_of_name.get(name)
1866+
if not files:
1867+
# a name that no file of the dump has: look once per name, in the directories of the dump only
1868+
if name not in files_named:
1869+
files_named[name] = [file for directory in dirs_having(name)
1870+
for file in files_of_dir[directory] if is_named(file, name)]
1871+
files = files_named[name]
1872+
if len(files) > 1:
1873+
# several headers of this file name: the one next to the including file for #include "...",
1874+
# else those whose path ends like the path of the #include
1875+
path = included_path(directive)
1876+
here = components(os.path.join(os.path.dirname(directive.file), path))
1877+
files = ([file for file in files if '"' in directive.str and components(file) == here] or
1878+
[file for file in files if same_end(file, path)] or files)
1879+
else:
1880+
files = []
1881+
for file in files:
1882+
if file not in active_files:
1883+
active_files.add(file)
1884+
pending.append(file)
1885+
1886+
return [directive for directive in cfg.directives if directive.file in active_files and id(directive) in in_active_branch]
1887+
17681888
def misra_2_5(self, dumpfile, cfg):
17691889
used_macros = []
17701890
unused_macro = {}
17711891
for m in cfg.macro_usage:
17721892
used_macros.append(m.name)
1773-
for directive in cfg.directives:
1893+
for directive in self._get_active_directives(cfg):
17741894
res_define = re.match(r'#define[ \t]+([a-zA-Z_][a-zA-Z_0-9]*).*', directive.str)
17751895
res_undef = re.match(r'#undef[ \t]+([a-zA-Z_][a-zA-Z_0-9]*).*', directive.str)
17761896
if res_define:

0 commit comments

Comments
 (0)