MCPcopy Create free account
hub / github.com/alibaba/GraphScope / FilesBelongToSameModule

Function FilesBelongToSameModule

analytical_engine/misc/cpplint.py:5970–6025  ·  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

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

Callers 1

Calls 7

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

Tested by

no test coverage detected