MCPcopy Index your code
hub / github.com/nodejs/node / FilesBelongToSameModule

Function FilesBelongToSameModule

tools/cpplint.py:7052–7105  ·  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 al

(filename_cc, filename_h)

Source from the content-addressed store, hash-verified

7050
7051
7052def FilesBelongToSameModule(filename_cc, filename_h):
7053 """Check if these two filenames belong to the same module.
7054
7055 The concept of a 'module' here is a as follows:
7056 foo.h, foo-inl.h, foo.cc, foo_test.cc and foo_unittest.cc belong to the
7057 same 'module' if they are in the same directory.
7058 some/path/public/xyzzy and some/path/internal/xyzzy are also considered
7059 to belong to the same module here.
7060
7061 If the filename_cc contains a longer path than the filename_h, for example,
7062 '/absolute/path/to/base/sysinfo.cc', and this file would include
7063 'base/sysinfo.h', this function also produces the prefix needed to open the
7064 header. This is used by the caller of this function to more robustly open the
7065 header file. We don't have access to the real include paths in this context,
7066 so we need this guesswork here.
7067
7068 Known bugs: tools/base/bar.cc and base/bar.h belong to the same module
7069 according to this implementation. Because of this, this function gives
7070 some false positives. This should be sufficiently rare in practice.
7071
7072 Args:
7073 filename_cc: is the path for the source (e.g. .cc) file
7074 filename_h: is the path for the header path
7075
7076 Returns:
7077 Tuple with a bool and a string:
7078 bool: True if filename_cc and filename_h belong to the same module.
7079 string: the additional prefix needed to open the header file.
7080 """
7081 fileinfo_cc = FileInfo(filename_cc)
7082 if fileinfo_cc.Extension().lstrip(".") not in GetNonHeaderExtensions():
7083 return (False, "")
7084
7085 fileinfo_h = FileInfo(filename_h)
7086 if not IsHeaderExtension(fileinfo_h.Extension().lstrip(".")):
7087 return (False, "")
7088
7089 filename_cc = filename_cc[: -(len(fileinfo_cc.Extension()))]
7090 if matched_test_suffix := re.search(_TEST_FILE_SUFFIX, fileinfo_cc.BaseName()):
7091 filename_cc = filename_cc[: -len(matched_test_suffix.group(1))]
7092
7093 filename_cc = filename_cc.replace("/public/", "/")
7094 filename_cc = filename_cc.replace("/internal/", "/")
7095
7096 filename_h = filename_h[: -(len(fileinfo_h.Extension()))]
7097 filename_h = filename_h.removesuffix("-inl")
7098 filename_h = filename_h.replace("/public/", "/")
7099 filename_h = filename_h.replace("/internal/", "/")
7100
7101 files_belong_to_same_module = filename_cc.endswith(filename_h)
7102 common_path = ""
7103 if files_belong_to_same_module:
7104 common_path = filename_cc[: -len(filename_h)]
7105 return files_belong_to_same_module, common_path
7106
7107
7108def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error, io=codecs):

Callers

nothing calls this directly

Calls 6

ExtensionMethod · 0.95
BaseNameMethod · 0.95
FileInfoClass · 0.85
GetNonHeaderExtensionsFunction · 0.85
IsHeaderExtensionFunction · 0.85
searchMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…