MCPcopy Create free account
hub / github.com/4paradigm/OpenMLDB / FilesBelongToSameModule

Function FilesBelongToSameModule

steps/cpplint.py:5980–6035  ·  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

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

Callers 1

Calls 6

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

Tested by

no test coverage detected