MCPcopy Create free account
hub / github.com/WolfireGames/overgrowth / FilesBelongToSameModule

Function FilesBelongToSameModule

Tools/cpplint.py:5341–5393  ·  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

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

Tested by

no test coverage detected