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)
| 5414 | |
| 5415 | |
| 5416 | def FilesBelongToSameModule(filename_cc, filename_h): |
| 5417 | """Check if these two filenames belong to the same module. |
| 5418 | |
| 5419 | The concept of a 'module' here is a as follows: |
| 5420 | foo.h, foo-inl.h, foo.cc, foo_test.cc and foo_unittest.cc belong to the |
| 5421 | same 'module' if they are in the same directory. |
| 5422 | some/path/public/xyzzy and some/path/internal/xyzzy are also considered |
| 5423 | to belong to the same module here. |
| 5424 | |
| 5425 | If the filename_cc contains a longer path than the filename_h, for example, |
| 5426 | '/absolute/path/to/base/sysinfo.cc', and this file would include |
| 5427 | 'base/sysinfo.h', this function also produces the prefix needed to open the |
| 5428 | header. This is used by the caller of this function to more robustly open the |
| 5429 | header file. We don't have access to the real include paths in this context, |
| 5430 | so we need this guesswork here. |
| 5431 | |
| 5432 | Known bugs: tools/base/bar.cc and base/bar.h belong to the same module |
| 5433 | according to this implementation. Because of this, this function gives |
| 5434 | some false positives. This should be sufficiently rare in practice. |
| 5435 | |
| 5436 | Args: |
| 5437 | filename_cc: is the path for the .cc file |
| 5438 | filename_h: is the path for the header path |
| 5439 | |
| 5440 | Returns: |
| 5441 | Tuple with a bool and a string: |
| 5442 | bool: True if filename_cc and filename_h belong to the same module. |
| 5443 | string: the additional prefix needed to open the header file. |
| 5444 | """ |
| 5445 | |
| 5446 | fileinfo = FileInfo(filename_cc) |
| 5447 | if not fileinfo.IsSource(): |
| 5448 | return (False, '') |
| 5449 | filename_cc = filename_cc[:-len(fileinfo.Extension())] |
| 5450 | matched_test_suffix = Search(_TEST_FILE_SUFFIX, fileinfo.BaseName()) |
| 5451 | if matched_test_suffix: |
| 5452 | filename_cc = filename_cc[:-len(matched_test_suffix.group(1))] |
| 5453 | filename_cc = filename_cc.replace('/public/', '/') |
| 5454 | filename_cc = filename_cc.replace('/internal/', '/') |
| 5455 | |
| 5456 | if not filename_h.endswith('.h'): |
| 5457 | return (False, '') |
| 5458 | filename_h = filename_h[:-len('.h')] |
| 5459 | if filename_h.endswith('-inl'): |
| 5460 | filename_h = filename_h[:-len('-inl')] |
| 5461 | filename_h = filename_h.replace('/public/', '/') |
| 5462 | filename_h = filename_h.replace('/internal/', '/') |
| 5463 | |
| 5464 | files_belong_to_same_module = filename_cc.endswith(filename_h) |
| 5465 | common_path = '' |
| 5466 | if files_belong_to_same_module: |
| 5467 | common_path = filename_cc[:-len(filename_h)] |
| 5468 | return files_belong_to_same_module, common_path |
| 5469 | |
| 5470 | |
| 5471 | def UpdateIncludeState(filename, include_dict, io=codecs): |