@@ -1775,20 +1775,24 @@ def misra_2_5(self, dumpfile, cfg):
17751775 res_undef = re .match (r'#undef[ \t]+([a-zA-Z_][a-zA-Z_0-9]*).*' , directive .str )
17761776 if res_define :
17771777 macro_name = res_define .group (1 )
1778- unused_macro [ macro_name ] = {'name' : macro_name , 'used' : (macro_name in used_macros ),
1779- 'file' : directive .file , 'line' : directive .linenr , 'column' : directive .column }
1778+ unused_macro . setdefault ( macro_name , []). append ( {'name' : macro_name , 'used' : (macro_name in used_macros ),
1779+ 'file' : directive .file , 'line' : directive .linenr , 'column' : directive .column })
17801780 elif res_undef :
17811781 macro_name = res_undef .group (1 )
17821782 # assuming that if we have #undef, we also have #define somewhere
17831783 if macro_name in unused_macro :
1784- unused_macro [macro_name ]['used' ] = True
1784+ # The definitions before an already used one are used too
1785+ for macro in reversed (unused_macro [macro_name ]):
1786+ if macro ['used' ]:
1787+ break
1788+ macro ['used' ] = True
17851789 else :
1786- unused_macro [macro_name ] = {'name' : macro_name , 'used' : True , 'file' : directive .file ,
1787- 'line' : directive .linenr , 'column' : directive .column }
1790+ unused_macro [macro_name ] = [ {'name' : macro_name , 'used' : True , 'file' : directive .file ,
1791+ 'line' : directive .linenr , 'column' : directive .column }]
17881792 used_macros .append (macro_name )
17891793
17901794 if unused_macro :
1791- cppcheckdata .reportSummary (dumpfile , 'MisraMacro' , list ( unused_macro .values ()) )
1795+ cppcheckdata .reportSummary (dumpfile , 'MisraMacro' , [ m for ms in unused_macro .values () for m in ms ] )
17921796
17931797 def misra_2_7 (self , data ):
17941798 for func in data .functions :
@@ -4898,6 +4902,7 @@ def analyse_ctu_info(self, ctu_info_files):
48984902 all_typedef_info = {}
48994903 all_tagname_info = {}
49004904 all_macro_info = {}
4905+ last_macros = {}
49014906 all_external_identifiers_decl = {}
49024907 all_external_identifiers_def = {}
49034908 all_internal_identifiers = {}
@@ -4909,19 +4914,72 @@ def analyse_ctu_info(self, ctu_info_files):
49094914 def is_different_location (loc1 , loc2 ):
49104915 return loc1 ['file' ] != loc2 ['file' ] or loc1 ['line' ] != loc2 ['line' ]
49114916
4917+ real_paths = {}
4918+
4919+ def real_path (path ):
4920+ if path not in real_paths :
4921+ real_paths [path ] = os .path .realpath (path )
4922+ return real_paths [path ]
4923+
49124924 def is_different_file (loc1 , loc2 ):
49134925 return loc1 ['file' ] != loc2 ['file' ]
49144926
4915- def add_declaration (all_info , new_info , num1 , num2 ):
4916- declarations = all_info .setdefault (new_info ['name' ], [])
4917- for info in declarations :
4918- if not is_different_location (info , new_info ):
4919- info ['used' ] = info ['used' ] or new_info ['used' ]
4920- return
4921- if declarations :
4922- self .reportError (Location (declarations [0 ]), num1 , num2 )
4923- self .reportError (Location (new_info ), num1 , num2 )
4924- declarations .append (new_info )
4927+ def links (path ):
4928+ # Numbers of components of the absolute path that are links or Windows 8.3 names, and that are in another case
4929+ count = cases = 0
4930+ p = os .path .abspath (path )
4931+ parent , name = os .path .split (p )
4932+ while name :
4933+ real = real_path (p )
4934+ if os .path .normcase (real ) != os .path .normcase (os .path .join (real_path (parent ), name )):
4935+ count += 1
4936+ try :
4937+ entries = os .listdir (parent )
4938+ except OSError :
4939+ entries = [name ]
4940+ if name not in entries and os .path .normcase (name ) in map (os .path .normcase , entries ):
4941+ cases += 1
4942+ elif os .path .basename (real ) != name :
4943+ cases += 1
4944+ p = parent
4945+ parent , name = os .path .split (p )
4946+ return count , cases
4947+
4948+ spelling_orders = {}
4949+
4950+ def spelling_order (path ):
4951+ # The spelling with the fewest links first, then an absolute one, then the shortest, then the fewest components in another case
4952+ if path not in spelling_orders :
4953+ count , cases = links (path )
4954+ spelling_orders [path ] = (count , not os .path .isabs (path ), len (path ), cases , path )
4955+ return spelling_orders [path ]
4956+
4957+ def add_declarations (all_info , summary_data ):
4958+ path = real = None
4959+ for new_info in summary_data :
4960+ # The declarations of a summary are usually grouped by file
4961+ if new_info ['file' ] != path :
4962+ path = new_info ['file' ]
4963+ real = real_path (path )
4964+ key = (real , new_info ['line' ])
4965+ declarations = all_info .get (new_info ['name' ])
4966+ if declarations is None :
4967+ all_info [new_info ['name' ]] = {key : new_info }
4968+ continue
4969+ info = declarations .get (key )
4970+ if info is None :
4971+ declarations [key ] = new_info
4972+ continue
4973+ if new_info ['used' ]:
4974+ info ['used' ] = True
4975+ if info ['file' ] != new_info ['file' ] and spelling_order (new_info ['file' ]) < spelling_order (info ['file' ]):
4976+ info ['file' ] = new_info ['file' ]
4977+
4978+ def report_declarations (all_info , num1 , num2 ):
4979+ for declarations in all_info .values ():
4980+ if len (declarations ) > 1 :
4981+ for info in declarations .values ():
4982+ self .reportError (Location (info ), num1 , num2 )
49254983
49264984 def read_lines (filename ):
49274985 with open (filename , 'rt' ) as f :
@@ -4937,21 +4995,16 @@ def read_lines(filename):
49374995 summary_data = s .get ('data' , None )
49384996
49394997 if summary_type == 'MisraTypedefInfo' :
4940- for new_typedef_info in summary_data :
4941- add_declaration (all_typedef_info , new_typedef_info , 5 , 6 )
4998+ add_declarations (all_typedef_info , summary_data )
49424999
49435000 if summary_type == 'MisraTagName' :
4944- for new_tagname_info in summary_data :
4945- add_declaration (all_tagname_info , new_tagname_info , 5 , 7 )
5001+ add_declarations (all_tagname_info , summary_data )
49465002
49475003 if summary_type == 'MisraMacro' :
4948- for new_macro in summary_data :
4949- key = new_macro ['name' ]
4950- existing_macro = all_macro_info .get (key , None )
4951- if existing_macro :
4952- existing_macro ['used' ] = existing_macro ['used' ] or new_macro ['used' ]
4953- else :
4954- all_macro_info [key ] = new_macro
5004+ # The last definition of each name in a summary, as they were grouped by name before
5005+ for m in {m ['name' ]: m for m in summary_data }.values ():
5006+ last_macros .setdefault (m ['name' ], []).append (dict (m ))
5007+ add_declarations (all_macro_info , summary_data )
49555008
49565009 if summary_type == 'MisraExternalIdentifiers' :
49575010 for s in sorted (summary_data , key = lambda d : "%s %s %s" % (d ['file' ],d ['line' ], d ['column' ] )):
@@ -4992,16 +5045,32 @@ def read_lines(filename):
49925045
49935046 except FileNotFoundError :
49945047 return
5048+ finally :
5049+ report_declarations (all_typedef_info , 5 , 6 )
5050+ report_declarations (all_tagname_info , 5 , 7 )
49955051
4996- unused_typedefs = [tdi for tdis in all_typedef_info .values () for tdi in tdis if not tdi ['used' ]]
5052+ unused_typedefs = [tdi for tdis in all_typedef_info .values () for tdi in tdis . values () if not tdi ['used' ]]
49975053 for tdi in unused_typedefs :
49985054 self .reportError (Location (tdi ), 2 , 3 )
49995055
5000- unused_tags = [tag for tags in all_tagname_info .values () for tag in tags if not tag ['used' ]]
5056+ unused_tags = [tag for tags in all_tagname_info .values () for tag in tags . values () if not tag ['used' ]]
50015057 for tag in unused_tags :
50025058 self .reportError (Location (tag ), 2 , 4 )
50035059
5004- unused_macros = [m for m in all_macro_info .values () if not m ['used' ]]
5060+ found_paths = {}
5061+
5062+ def path_found (path ):
5063+ if path not in found_paths :
5064+ found_paths [path ] = os .path .exists (path )
5065+ return found_paths [path ]
5066+
5067+ unused_macros = []
5068+ for name , macros in all_macro_info .items ():
5069+ if all (path_found (real ) for real , _ in macros ):
5070+ unused_macros += [m for m in macros .values () if not m ['used' ]]
5071+ elif not any (m ['used' ] for m in last_macros [name ]):
5072+ # A path not found from here, for example with --relative-paths: the definitions are grouped by name as before
5073+ unused_macros .append (min (last_macros [name ], key = lambda m : (spelling_order (m ['file' ]), m ['line' ])))
50055074 for m in unused_macros :
50065075 self .reportError (Location (m ), 2 , 5 )
50075076
0 commit comments