diff options
author | enne@chromium.org <enne@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-04 06:59:16 +0000 |
---|---|---|
committer | enne@chromium.org <enne@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-04 06:59:16 +0000 |
commit | 49aa76a1085053d4ec0f0beac81638dd7e88fe6a (patch) | |
tree | e8dc8a9e40e0a17f7b9ff24df82c473a9aa8f7b8 /PRESUBMIT.py | |
parent | 7e392f35915f9278e1105ee25eca1c898eb12593 (diff) | |
download | chromium_src-49aa76a1085053d4ec0f0beac81638dd7e88fe6a.zip chromium_src-49aa76a1085053d4ec0f0beac81638dd7e88fe6a.tar.gz chromium_src-49aa76a1085053d4ec0f0beac81638dd7e88fe6a.tar.bz2 |
Add presubmit check for bad anonymous variables
These types are all scoped locks and should never be in an anonymous
variable. A presubmit check is not watertight, but should go a long way
towards avoiding this class of bugs.
I ran this check over Chromium and there were no false positives in the
codebase.
This CL is meant as an alternative to the macro and extra parentheses
approach described in https://codereview.chromium.org/71713004/
BUG=none
Review URL: https://codereview.chromium.org/100743004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@238618 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'PRESUBMIT.py')
-rw-r--r-- | PRESUBMIT.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/PRESUBMIT.py b/PRESUBMIT.py index c046e5f..ef0b814 100644 --- a/PRESUBMIT.py +++ b/PRESUBMIT.py @@ -901,6 +901,62 @@ def _CheckSpamLogging(input_api, output_api): return [] +def _CheckForAnonymousVariables(input_api, output_api): + """These types are all expected to hold locks while in scope and + so should never be anonymous (which causes them to be immediately + destroyed).""" + they_who_must_be_named = [ + 'base::AutoLock', + 'base::AutoReset', + 'base::AutoUnlock', + 'SkAutoAlphaRestore', + 'SkAutoBitmapShaderInstall', + 'SkAutoBlitterChoose', + 'SkAutoBounderCommit', + 'SkAutoCallProc', + 'SkAutoCanvasRestore', + 'SkAutoCommentBlock', + 'SkAutoDescriptor', + 'SkAutoDisableDirectionCheck', + 'SkAutoDisableOvalCheck', + 'SkAutoFree', + 'SkAutoGlyphCache', + 'SkAutoHDC', + 'SkAutoLockColors', + 'SkAutoLockPixels', + 'SkAutoMalloc', + 'SkAutoMaskFreeImage', + 'SkAutoMutexAcquire', + 'SkAutoPathBoundsUpdate', + 'SkAutoPDFRelease', + 'SkAutoRasterClipValidate', + 'SkAutoRef', + 'SkAutoTime', + 'SkAutoTrace', + 'SkAutoUnref', + ] + anonymous = r'(%s)\s*[({]' % '|'.join(they_who_must_be_named) + # bad: base::AutoLock(lock.get()); + # not bad: base::AutoLock lock(lock.get()); + bad_pattern = input_api.re.compile(anonymous) + # good: new base::AutoLock(lock.get()) + good_pattern = input_api.re.compile(r'\bnew\s*' + anonymous) + errors = [] + + for f in input_api.AffectedFiles(): + if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')): + continue + for linenum, line in f.ChangedContents(): + if bad_pattern.search(line) and not good_pattern.search(line): + errors.append('%s:%d' % (f.LocalPath(), linenum)) + + if errors: + return [output_api.PresubmitError( + 'These lines create anonymous variables that need to be named:', + items=errors)] + return [] + + def _CheckCygwinShell(input_api, output_api): source_file_filter = lambda x: input_api.FilterSourceFile( x, white_list=(r'.+\.(gyp|gypi)$',)) @@ -950,6 +1006,7 @@ def _CommonChecks(input_api, output_api): output_api, source_file_filter=lambda x: x.LocalPath().endswith('.grd'))) results.extend(_CheckSpamLogging(input_api, output_api)) + results.extend(_CheckForAnonymousVariables(input_api, output_api)) results.extend(_CheckCygwinShell(input_api, output_api)) if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()): |