@@ -1765,12 +1765,134 @@ 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+ # a file that defines an expanded macro was read, also when no #include reaches it (--include)
1859+ active_files .update (m .file for m in cfg .macro_usage if m .file in directives_of_file )
1860+ pending = list (active_files )
1861+ while pending :
1862+ for directive in directives_of_file [pending .pop ()]:
1863+ name = included_name (directive ) if id (directive ) in in_active_branch else None
1864+ if name == '' :
1865+ files = list (directives_of_file )
1866+ elif name :
1867+ files = files_of_name .get (name )
1868+ if not files :
1869+ # a name that no file of the dump has: look once per name, in the directories of the dump only
1870+ if name not in files_named :
1871+ files_named [name ] = [file for directory in dirs_having (name )
1872+ for file in files_of_dir [directory ] if is_named (file , name )]
1873+ files = files_named [name ]
1874+ if len (files ) > 1 :
1875+ # several headers of this file name: the one next to the including file for #include "...",
1876+ # else those whose path ends like the path of the #include
1877+ path = included_path (directive )
1878+ here = components (os .path .join (os .path .dirname (directive .file ), path ))
1879+ files = ([file for file in files if '"' in directive .str and components (file ) == here ] or
1880+ [file for file in files if same_end (file , path )] or files )
1881+ else :
1882+ files = []
1883+ for file in files :
1884+ if file not in active_files :
1885+ active_files .add (file )
1886+ pending .append (file )
1887+
1888+ return [directive for directive in cfg .directives if directive .file in active_files and id (directive ) in in_active_branch ]
1889+
17681890 def misra_2_5 (self , dumpfile , cfg ):
17691891 used_macros = []
17701892 unused_macro = {}
17711893 for m in cfg .macro_usage :
17721894 used_macros .append (m .name )
1773- for directive in cfg . directives :
1895+ for directive in self . _get_active_directives ( cfg ) :
17741896 res_define = re .match (r'#define[ \t]+([a-zA-Z_][a-zA-Z_0-9]*).*' , directive .str )
17751897 res_undef = re .match (r'#undef[ \t]+([a-zA-Z_][a-zA-Z_0-9]*).*' , directive .str )
17761898 if res_define :
0 commit comments