MCPcopy Create free account
hub / github.com/dmtcp/dmtcp / FilesBelongToSameModule

Function FilesBelongToSameModule

util/cpplint.py:5338–5390  ·  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

5336
5337
5338def FilesBelongToSameModule(filename_cc, filename_h):
5339 """Check if these two filenames belong to the same module.
5340
5341 The concept of a 'module' here is a as follows:
5342 foo.h, foo-inl.h, foo.cc, foo_test.cc and foo_unittest.cc belong to the
5343 same 'module' if they are in the same directory.
5344 some/path/public/xyzzy and some/path/internal/xyzzy are also considered
5345 to belong to the same module here.
5346
5347 If the filename_cc contains a longer path than the filename_h, for example,
5348 '/absolute/path/to/base/sysinfo.cc', and this file would include
5349 'base/sysinfo.h', this function also produces the prefix needed to open the
5350 header. This is used by the caller of this function to more robustly open the
5351 header file. We don't have access to the real include paths in this context,
5352 so we need this guesswork here.
5353
5354 Known bugs: tools/base/bar.cc and base/bar.h belong to the same module
5355 according to this implementation. Because of this, this function gives
5356 some false positives. This should be sufficiently rare in practice.
5357
5358 Args:
5359 filename_cc: is the path for the .cc file
5360 filename_h: is the path for the header path
5361
5362 Returns:
5363 Tuple with a bool and a string:
5364 bool: True if filename_cc and filename_h belong to the same module.
5365 string: the additional prefix needed to open the header file.
5366 """
5367
5368 fileinfo = FileInfo(filename_cc)
5369 if not fileinfo.IsSource():
5370 return (False, '')
5371 filename_cc = filename_cc[:-len(fileinfo.Extension())]
5372 matched_test_suffix = Search(_TEST_FILE_SUFFIX, fileinfo.BaseName())
5373 if matched_test_suffix:
5374 filename_cc = filename_cc[:-len(matched_test_suffix.group(1))]
5375 filename_cc = filename_cc.replace('/public/', '/')
5376 filename_cc = filename_cc.replace('/internal/', '/')
5377
5378 if not filename_h.endswith('.h'):
5379 return (False, '')
5380 filename_h = filename_h[:-len('.h')]
5381 if filename_h.endswith('-inl'):
5382 filename_h = filename_h[:-len('-inl')]
5383 filename_h = filename_h.replace('/public/', '/')
5384 filename_h = filename_h.replace('/internal/', '/')
5385
5386 files_belong_to_same_module = filename_cc.endswith(filename_h)
5387 common_path = ''
5388 if files_belong_to_same_module:
5389 common_path = filename_cc[:-len(filename_h)]
5390 return files_belong_to_same_module, common_path
5391
5392
5393def UpdateIncludeState(filename, include_dict, io=codecs):

Callers 1

Calls 6

IsSourceMethod · 0.95
ExtensionMethod · 0.95
BaseNameMethod · 0.95
FileInfoClass · 0.85
SearchFunction · 0.85
replaceMethod · 0.80

Tested by

no test coverage detected