MCPcopy Create free account
hub / github.com/BVLC/caffe / FilesBelongToSameModule

Function FilesBelongToSameModule

scripts/cpp_lint.py:4403–4455  ·  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

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

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected