From 5210aec6df0dd040c7391e8befc337b4c3fa5b0d Mon Sep 17 00:00:00 2001 From: "erg@google.com" Date: Tue, 6 Sep 2011 20:19:05 +0000 Subject: [PATCH] Upstream useful lint check from WebKit's fork of cpplint.py. Checks against "Foo *bar" and "Foo &bar" declarations. WebKit patch was: http://trac.webkit.org/changeset/46856 Credit Torch Mobile, Inc. who have contributed the WebKit patch in question. Patch committed for asvitkine@chromium.org. --- cpplint/cpplint.py | 26 +++++++++++++++++++++++++- cpplint/cpplint_unittest.py | 18 ++++++++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/cpplint/cpplint.py b/cpplint/cpplint.py index 86154ed..d9b3e6b 100755 --- a/cpplint/cpplint.py +++ b/cpplint/cpplint.py @@ -1,6 +1,7 @@ #!/usr/bin/python2.4 # -# Copyright (c) 2009 Google Inc. All rights reserved. +# Copyright (c) 2011 Google Inc. All rights reserved. +# Copyright (c) 2009 Torch Mobile Inc. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are @@ -193,6 +194,7 @@ _ERROR_CATEGORIES = [ 'whitespace/braces', 'whitespace/comma', 'whitespace/comments', + 'whitespace/declaration', 'whitespace/end_of_line', 'whitespace/ending_newline', 'whitespace/indent', @@ -1245,6 +1247,27 @@ def CheckInvalidIncrement(filename, clean_lines, linenum, error): 'Changing pointer instead of value (or unused value of operator*).') +# Matches Foo *foo declarations. +_RE_PATTERN_POINTER_DECLARATION_WHITESPACE = re.compile( + r'\s*\w+(?\*|\&)\w+') + +def CheckPointerDeclarationWhitespace(filename, clean_lines, linenum, error): + """Checks for Foo *foo declarations. + + Args: + filename: The name of the current file. + clean_lines: A CleansedLines instance containing the file. + linenum: The number of the line to check. + error: The function to call with any errors found. + """ + line = clean_lines.elided[linenum] + matched = _RE_PATTERN_POINTER_DECLARATION_WHITESPACE.match(line) + if matched: + error(filename, linenum, 'whitespace/declaration', 3, + 'Declaration has space between type name and %s in %s' % + (matched.group('pointer_operator'), matched.group(0).strip())) + + class _ClassInfo(object): """Stores information about a class.""" @@ -2926,6 +2949,7 @@ def ProcessLine(filename, file_extension, class_state, error) CheckPosixThreading(filename, clean_lines, line, error) CheckInvalidIncrement(filename, clean_lines, line, error) + CheckPointerDeclarationWhitespace(filename, clean_lines, line, error) def ProcessFileData(filename, file_extension, lines, error): diff --git a/cpplint/cpplint_unittest.py b/cpplint/cpplint_unittest.py index ea39f9c..6b0090a 100755 --- a/cpplint/cpplint_unittest.py +++ b/cpplint/cpplint_unittest.py @@ -1,7 +1,8 @@ #!/usr/bin/python2.4 # -*- coding: utf-8; -*- # -# Copyright (c) 2009 Google Inc. All rights reserved. +# Copyright (c) 2011 Google Inc. All rights reserved. +# Copyright (C) 2009 Torch Mobile Inc. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are @@ -348,7 +349,7 @@ class CpplintTest(CpplintTestBase): 'Using C-style cast. Use static_cast(...) instead' ' [readability/casting] [4]') self.TestLint( - 'int *a = (int *)NULL;', + 'int* a = (int *)NULL;', 'Using C-style cast. Use reinterpret_cast(...) instead' ' [readability/casting] [4]') @@ -1646,6 +1647,19 @@ class CpplintTest(CpplintTestBase): self.TestLint('f(a, /* name */ b);', '') self.TestLint('f(a, /* name */b);', '') + def testPointerDeclarationWhitespace(self): + self.TestLint('int* b;', '') + self.TestLint('int *b;', + 'Declaration has space between type name and * in int *b ' + '[whitespace/declaration] [3]') + self.TestLint('return *b;', '') + self.TestLint('delete *b;', '') + self.TestLint('int& b;', '') + self.TestLint('int &b;', + 'Declaration has space between type name and & in int &b ' + '[whitespace/declaration] [3]') + self.TestLint('return &b;', '') + def testIndent(self): self.TestLint('static int noindent;', '') self.TestLint(' int two_space_indent;', '')