Commit 4cea01ef authored by bauerb@chromium.org's avatar bauerb@chromium.org

Allow 'class Singleton<T>' in header files.


BUG=none
TEST='friend class Singleton<T>' is allowed.


Review URL: http://codereview.chromium.org/9756001

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@127751 0039d316-1c4b-4281-b951-d872f2087c98
parent eebfb754
......@@ -824,17 +824,17 @@ def _CheckConstNSObject(input_api, output_api, source_file_filter):
return []
def _CheckSingletonInHeaders(input_api, output_api, source_file_filter):
def CheckSingletonInHeaders(input_api, output_api, source_file_filter=None):
"""Checks to make sure no header files have |Singleton<|."""
pattern = input_api.re.compile(r'Singleton\s*<')
pattern = input_api.re.compile(r'(?<!class\s)Singleton\s*<')
files = []
for f in input_api.AffectedSourceFiles(source_file_filter):
if (f.LocalPath().endswith('.h') or f.LocalPath().endswith('.hxx') or
f.LocalPath().endswith('.hpp') or f.LocalPath().endswith('.inl')):
contents = input_api.ReadFile(f)
for line in contents.splitlines(False):
line = input_api.re.sub(r'//.*$', '', line) # Strip C++ comment.
if pattern.search(line):
if (not input_api.re.match(r'//', line) and # Strip C++ comment.
pattern.search(line)):
files.append(f)
break
......@@ -924,7 +924,7 @@ def PanProjectChecks(input_api, output_api,
results.extend(_CheckConstNSObject(
input_api, output_api, source_file_filter=sources))
snapshot("checking singletons")
results.extend(_CheckSingletonInHeaders(
results.extend(CheckSingletonInHeaders(
input_api, output_api, source_file_filter=sources))
# The following checks are only done on commit, since the commit bot will
......
......@@ -1493,6 +1493,7 @@ class CannedChecksUnittest(PresubmitTestsBase):
'CheckLicense',
'CheckOwners',
'CheckRietveldTryJobExecution',
'CheckSingletonInHeaders',
'CheckSvnModifiedDirectories',
'CheckSvnForCommonMimeTypes', 'CheckSvnProperty',
'RunPythonUnitTests', 'RunPylint',
......@@ -1697,6 +1698,41 @@ class CannedChecksUnittest(PresubmitTestsBase):
'Foo', None, 'Foo ', None,
presubmit.OutputApi.PresubmitPromptWarning)
def testCheckSingletonInHeaders(self):
change1 = presubmit.Change(
'foo1', 'foo1\n', self.fake_root_dir, None, 0, 0, None)
input_api1 = self.MockInputApi(change1, False)
affected_file1 = self.mox.CreateMock(presubmit.SvnAffectedFile)
affected_file2 = self.mox.CreateMock(presubmit.SvnAffectedFile)
input_api1.AffectedSourceFiles(None).AndReturn(
[affected_file1, affected_file2])
affected_file1.LocalPath().AndReturn('foo.h')
input_api1.ReadFile(affected_file1).AndReturn(
'// Comment mentioning Singleton<Foo>.\n' +
'friend class Singleton<Foo>;')
for _ in range(4):
affected_file2.LocalPath().AndReturn('foo.cc')
change2 = presubmit.Change(
'foo2', 'foo2\n', self.fake_root_dir, None, 0, 0, None)
input_api2 = self.MockInputApi(change2, False)
affected_file3 = self.mox.CreateMock(presubmit.SvnAffectedFile)
input_api2.AffectedSourceFiles(None).AndReturn([affected_file3])
affected_file3.LocalPath().AndReturn('foo.h')
input_api2.ReadFile(affected_file3).AndReturn(
'Foo* foo = Singleton<Foo>::get();')
self.mox.ReplayAll()
results1 = presubmit_canned_checks.CheckSingletonInHeaders(
input_api1, presubmit.OutputApi)
self.assertEquals(results1, [])
results2 = presubmit_canned_checks.CheckSingletonInHeaders(
input_api2, presubmit.OutputApi)
self.assertEquals(len(results2), 1)
self.assertEquals(results2[0].__class__, presubmit.OutputApi.PresubmitError)
def testCheckChangeHasOnlyOneEol(self):
self.ReadFileTest(presubmit_canned_checks.CheckChangeHasOnlyOneEol,
"Hey!\nHo!\n", "Hey!\nHo!\n\n",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment