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)
| 4397 | |
| 4398 | |
| 4399 | def FilesBelongToSameModule(filename_cc, filename_h): |
| 4400 | """Check if these two filenames belong to the same module. |
| 4401 | |
| 4402 | The concept of a 'module' here is a as follows: |
| 4403 | foo.h, foo-inl.h, foo.cc, foo_test.cc and foo_unittest.cc belong to the |
| 4404 | same 'module' if they are in the same directory. |
| 4405 | some/path/public/xyzzy and some/path/internal/xyzzy are also considered |
| 4406 | to belong to the same module here. |
| 4407 | |
| 4408 | If the filename_cc contains a longer path than the filename_h, for example, |
| 4409 | '/absolute/path/to/base/sysinfo.cc', and this file would include |
| 4410 | 'base/sysinfo.h', this function also produces the prefix needed to open the |
| 4411 | header. This is used by the caller of this function to more robustly open the |
| 4412 | header file. We don't have access to the real include paths in this context, |
| 4413 | so we need this guesswork here. |
| 4414 | |
| 4415 | Known bugs: tools/base/bar.cc and base/bar.h belong to the same module |
| 4416 | according to this implementation. Because of this, this function gives |
| 4417 | some false positives. This should be sufficiently rare in practice. |
| 4418 | |
| 4419 | Args: |
| 4420 | filename_cc: is the path for the .cc file |
| 4421 | filename_h: is the path for the header path |
| 4422 | |
| 4423 | Returns: |
| 4424 | Tuple with a bool and a string: |
| 4425 | bool: True if filename_cc and filename_h belong to the same module. |
| 4426 | string: the additional prefix needed to open the header file. |
| 4427 | """ |
| 4428 | |
| 4429 | if not filename_cc.endswith('.cc'): |
| 4430 | return (False, '') |
| 4431 | filename_cc = filename_cc[:-len('.cc')] |
| 4432 | if filename_cc.endswith('_unittest'): |
| 4433 | filename_cc = filename_cc[:-len('_unittest')] |
| 4434 | elif filename_cc.endswith('_test'): |
| 4435 | filename_cc = filename_cc[:-len('_test')] |
| 4436 | filename_cc = filename_cc.replace('/public/', '/') |
| 4437 | filename_cc = filename_cc.replace('/internal/', '/') |
| 4438 | |
| 4439 | if not filename_h.endswith('.h'): |
| 4440 | return (False, '') |
| 4441 | filename_h = filename_h[:-len('.h')] |
| 4442 | if filename_h.endswith('-inl'): |
| 4443 | filename_h = filename_h[:-len('-inl')] |
| 4444 | filename_h = filename_h.replace('/public/', '/') |
| 4445 | filename_h = filename_h.replace('/internal/', '/') |
| 4446 | |
| 4447 | files_belong_to_same_module = filename_cc.endswith(filename_h) |
| 4448 | common_path = '' |
| 4449 | if files_belong_to_same_module: |
| 4450 | common_path = filename_cc[:-len(filename_h)] |
| 4451 | return files_belong_to_same_module, common_path |
| 4452 | |
| 4453 | |
| 4454 | def UpdateIncludeState(filename, include_state, io=codecs): |
no outgoing calls
no test coverage detected