MCPcopy Create free account
hub / github.com/OpenMS/OpenMS / FilesBelongToSameModule

Function FilesBelongToSameModule

src/tests/coding/cpplint.py:5967–6022  ·  view source on GitHub ↗

Check if these two filenames belong to the same module. The concept of a 'module' here is a as follows: foo.h, foo-inl.h, foo.cc, foo_test.cc and foo_unittest.cc belong to the same 'module' if they are in the same directory. some/path/public/xyzzy and some/path/internal/xyzzy are also consi

(filename_cc, filename_h)

Source from the content-addressed store, hash-verified

5965
5966
5967def FilesBelongToSameModule(filename_cc, filename_h):
5968 """Check if these two filenames belong to the same module.
5969
5970 The concept of a 'module' here is a as follows:
5971 foo.h, foo-inl.h, foo.cc, foo_test.cc and foo_unittest.cc belong to the
5972 same 'module' if they are in the same directory.
5973 some/path/public/xyzzy and some/path/internal/xyzzy are also considered
5974 to belong to the same module here.
5975
5976 If the filename_cc contains a longer path than the filename_h, for example,
5977 '/absolute/path/to/base/sysinfo.cc', and this file would include
5978 'base/sysinfo.h', this function also produces the prefix needed to open the
5979 header. This is used by the caller of this function to more robustly open the
5980 header file. We don't have access to the real include paths in this context,
5981 so we need this guesswork here.
5982
5983 Known bugs: tools/base/bar.cc and base/bar.h belong to the same module
5984 according to this implementation. Because of this, this function gives
5985 some false positives. This should be sufficiently rare in practice.
5986
5987 Args:
5988 filename_cc: is the path for the source (e.g. .cc) file
5989 filename_h: is the path for the header path
5990
5991 Returns:
5992 Tuple with a bool and a string:
5993 bool: True if filename_cc and filename_h belong to the same module.
5994 string: the additional prefix needed to open the header file.
5995 """
5996 fileinfo_cc = FileInfo(filename_cc)
5997 if not fileinfo_cc.Extension().lstrip('.') in GetNonHeaderExtensions():
5998 return (False, '')
5999
6000 fileinfo_h = FileInfo(filename_h)
6001 if not IsHeaderExtension(fileinfo_h.Extension().lstrip('.')):
6002 return (False, '')
6003
6004 filename_cc = filename_cc[:-(len(fileinfo_cc.Extension()))]
6005 matched_test_suffix = Search(_TEST_FILE_SUFFIX, fileinfo_cc.BaseName())
6006 if matched_test_suffix:
6007 filename_cc = filename_cc[:-len(matched_test_suffix.group(1))]
6008
6009 filename_cc = filename_cc.replace('/public/', '/')
6010 filename_cc = filename_cc.replace('/internal/', '/')
6011
6012 filename_h = filename_h[:-(len(fileinfo_h.Extension()))]
6013 if filename_h.endswith('-inl'):
6014 filename_h = filename_h[:-len('-inl')]
6015 filename_h = filename_h.replace('/public/', '/')
6016 filename_h = filename_h.replace('/internal/', '/')
6017
6018 files_belong_to_same_module = filename_cc.endswith(filename_h)
6019 common_path = ''
6020 if files_belong_to_same_module:
6021 common_path = filename_cc[:-len(filename_h)]
6022 return files_belong_to_same_module, common_path
6023
6024

Callers 1

Calls 8

ExtensionMethod · 0.95
BaseNameMethod · 0.95
FileInfoClass · 0.85
GetNonHeaderExtensionsFunction · 0.85
IsHeaderExtensionFunction · 0.85
SearchFunction · 0.85
groupMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected