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)
| 6906 | |
| 6907 | |
| 6908 | def FilesBelongToSameModule(filename_cc, filename_h): |
| 6909 | """Check if these two filenames belong to the same module. |
| 6910 | |
| 6911 | The concept of a 'module' here is a as follows: |
| 6912 | foo.h, foo-inl.h, foo.cc, foo_test.cc and foo_unittest.cc belong to the |
| 6913 | same 'module' if they are in the same directory. |
| 6914 | some/path/public/xyzzy and some/path/internal/xyzzy are also considered |
| 6915 | to belong to the same module here. |
| 6916 | |
| 6917 | If the filename_cc contains a longer path than the filename_h, for example, |
| 6918 | '/absolute/path/to/base/sysinfo.cc', and this file would include |
| 6919 | 'base/sysinfo.h', this function also produces the prefix needed to open the |
| 6920 | header. This is used by the caller of this function to more robustly open the |
| 6921 | header file. We don't have access to the real include paths in this context, |
| 6922 | so we need this guesswork here. |
| 6923 | |
| 6924 | Known bugs: tools/base/bar.cc and base/bar.h belong to the same module |
| 6925 | according to this implementation. Because of this, this function gives |
| 6926 | some false positives. This should be sufficiently rare in practice. |
| 6927 | |
| 6928 | Args: |
| 6929 | filename_cc: is the path for the source (e.g. .cc) file |
| 6930 | filename_h: is the path for the header path |
| 6931 | |
| 6932 | Returns: |
| 6933 | Tuple with a bool and a string: |
| 6934 | bool: True if filename_cc and filename_h belong to the same module. |
| 6935 | string: the additional prefix needed to open the header file. |
| 6936 | """ |
| 6937 | fileinfo_cc = FileInfo(filename_cc) |
| 6938 | if fileinfo_cc.Extension().lstrip(".") not in GetNonHeaderExtensions(): |
| 6939 | return (False, "") |
| 6940 | |
| 6941 | fileinfo_h = FileInfo(filename_h) |
| 6942 | if not IsHeaderExtension(fileinfo_h.Extension().lstrip(".")): |
| 6943 | return (False, "") |
| 6944 | |
| 6945 | filename_cc = filename_cc[: -(len(fileinfo_cc.Extension()))] |
| 6946 | if matched_test_suffix := re.search(_TEST_FILE_SUFFIX, fileinfo_cc.BaseName()): |
| 6947 | filename_cc = filename_cc[: -len(matched_test_suffix.group(1))] |
| 6948 | |
| 6949 | filename_cc = filename_cc.replace("/public/", "/") |
| 6950 | filename_cc = filename_cc.replace("/internal/", "/") |
| 6951 | |
| 6952 | filename_h = filename_h[: -(len(fileinfo_h.Extension()))] |
| 6953 | filename_h = filename_h.removesuffix("-inl") |
| 6954 | filename_h = filename_h.replace("/public/", "/") |
| 6955 | filename_h = filename_h.replace("/internal/", "/") |
| 6956 | |
| 6957 | files_belong_to_same_module = filename_cc.endswith(filename_h) |
| 6958 | common_path = "" |
| 6959 | if files_belong_to_same_module: |
| 6960 | common_path = filename_cc[: -len(filename_h)] |
| 6961 | return files_belong_to_same_module, common_path |
| 6962 | |
| 6963 | |
| 6964 | def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error, io=codecs): |
nothing calls this directly
no test coverage detected