Add presubmit check to prevent anonymous namespace usage in headers
Bug: 391905435 Change-Id: I928d282bc3b626addc564889338cb7426e7cbe4c Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6192789 Commit-Queue: Aaron Selya <selya@google.com> Reviewed-by: Andrew Grieve <agrieve@chromium.org> Cr-Commit-Position: refs/heads/main@{#1411842}
This commit is contained in:
30
PRESUBMIT.py
30
PRESUBMIT.py
@ -7806,3 +7806,33 @@ def CheckTodoBugReferences(input_api, output_api):
|
||||
]
|
||||
else:
|
||||
return []
|
||||
|
||||
def CheckForAnonymousNamespaceInHeader(input_api,
|
||||
output_api,
|
||||
allowlist=_HEADER_EXTENSIONS,
|
||||
denylist=None):
|
||||
denylist = tuple(denylist or input_api.DEFAULT_FILES_TO_SKIP)
|
||||
source_file_filter = lambda x: input_api.FilterSourceFile(
|
||||
x, allowlist, denylist)
|
||||
|
||||
def headers(f):
|
||||
return input_api.FilterSourceFile(
|
||||
f, files_to_check=(r'.+%s' % _HEADER_EXTENSIONS, ))
|
||||
|
||||
header_using_anon_namespace_files = []
|
||||
|
||||
for f in input_api.AffectedSourceFiles(headers):
|
||||
contents = input_api.ReadFile(f, 'rb')
|
||||
if input_api.re.search(r"namespace {", contents):
|
||||
header_using_anon_namespace_files.append(f.LocalPath())
|
||||
|
||||
result = []
|
||||
if header_using_anon_namespace_files:
|
||||
result.append(
|
||||
output_api.PresubmitError(
|
||||
'These files have an anonymous namespace which is not '
|
||||
'permitted in header files, for more information see ('
|
||||
'https://google.github.io/styleguide/cppguide.html#Internal_Linkage'
|
||||
'): ',
|
||||
items=header_using_anon_namespace_files))
|
||||
return result
|
||||
|
@ -5705,6 +5705,30 @@ class CheckDeprecatedSyncConsentFunctionsTest(unittest.TestCase):
|
||||
self.assertTrue('chrome/foo/file4.java' in results[0].message),
|
||||
self.assertTrue('chrome/foo/file5.java' in results[0].message),
|
||||
|
||||
class CheckAnonymousNamespacesInHeaderFilesTest(unittest.TestCase):
|
||||
"""Test the presubmit checking for anonymous namespaces in header files."""
|
||||
|
||||
def testAnonymousNamespaceInHeader(self):
|
||||
file_types = ['src/helpers.h', 'src/helpers.hpp', 'src/helpers.hxx']
|
||||
for header_file in file_types:
|
||||
input_api = MockInputApi()
|
||||
input_api.files = [
|
||||
MockAffectedFile(header_file,
|
||||
['namespace {}'])]
|
||||
error = PRESUBMIT.CheckForAnonymousNamespaceInHeader(
|
||||
input_api, MockOutputApi())
|
||||
self.assertEqual(1, len(error))
|
||||
|
||||
def testValidNamespaceInHeader(self):
|
||||
file_types = ['src/helpers.h', 'src/helpers.hpp', 'src/helpers.hxx']
|
||||
for header_file in file_types:
|
||||
input_api = MockInputApi()
|
||||
input_api.files = [
|
||||
MockAffectedFile(header_file,
|
||||
['namespace test {}'])]
|
||||
error = PRESUBMIT.CheckForAnonymousNamespaceInHeader(
|
||||
input_api, MockOutputApi())
|
||||
self.assertEqual(0, len(error))
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
Reference in New Issue
Block a user