All pastes #1506243 Raw Edit

Unnamed

public text v1 · immutable
#1506243 ·published 2009-07-24 21:39 UTC
rendered paste body
commit 842f39cd813a08eb8272b1af351a4efd21a9db5f
Author: Kenneth Christiansen <kenneth.christiansen@openbossa.org>
Date:   Mon Jul 20 17:48:41 2009 -0300

    The Qt coding style differs from that of WebKit with regard to braces.
    
    This is in the case of namespaces, classes and structs.
    This change fixes cpplint to adhere to this rule and updates all unit tests.
    
       Braces:
    
       Exception: Function implementations and class declarations always have
       the left brace on the start of a line:
    
       class Moo
       {
       };
    
       Correct the use of braces after classes, structs and namespaces.

diff --git a/cpplint.py b/cpplint.py
old mode 100644
new mode 100755
index 8997444..71f26f2
--- a/cpplint.py
+++ b/cpplint.py
@@ -1681,9 +1681,15 @@ def check_braces(filename, clean_lines, line_number, error):
         previous_line = get_previous_non_blank_line(clean_lines, line_number)[0]
         if ((not search(r'[;:}{)=]\s*$|\)\s*const\s*$', previous_line)
              or search(r'\b(if|for|while|switch|else)\b', previous_line))
-            and previous_line.find('#') < 0):
+            and previous_line.find('#') < 0
+            and not search(r'\s*(struct|class|namespace)\s+\w+\s*', previous_line)
+            and not search(r'\s*(public|protected|private)\s+\w+\s*', previous_line)):
             error(filename, line_number, 'whitespace/braces', 4,
                   'This { should be at the end of the previous line')
+    elif (search(r'\s*(struct|class|namespace)\s+\w+\s*{\s*$', line)
+        or search(r'\s*(public|protected|private)\s+\w+\s*{\s*$', line)):
+        error(filename, line_number, 'whitespace/braces', 4,
+              'Place brace on its own line for class/struct/namespace definitions.')
     elif (search(r'\)\s*(const\s*)?{\s*$', line)
           and line.count('(') == line.count(')')
           and not search(r'\b(if|for|foreach|while|switch)\b', line)):
diff --git a/cpplint_unittest.py b/cpplint_unittest.py
old mode 100644
new mode 100755
index f6e4878..6eac4c0
--- a/cpplint_unittest.py
+++ b/cpplint_unittest.py
@@ -372,7 +372,7 @@ class CpplintTest(CpplintTestBase):
     #   self.assert_lint('void Method(char* /*x*/);', message)
     #   self.assert_lint('typedef void (*Method)(int32);', message)
     #   self.assert_lint('static void operator delete[](void*) throw();', message)
-    # 
+    #
     #   self.assert_lint('virtual void D(int* p);', '')
     #   self.assert_lint('void operator delete(void* x) throw();', '')
     #   self.assert_lint('void Method(char* x)\n{', '')
@@ -381,7 +381,7 @@ class CpplintTest(CpplintTestBase):
     #   self.assert_lint('typedef void (*Method)(int32 x);', '')
     #   self.assert_lint('static void operator delete[](void* x) throw();', '')
     #   self.assert_lint('static void operator delete[](void* /*x*/) throw();', '')
-    # 
+    #
     #   # This one should technically warn, but doesn't because the function
     #   # pointer is confusing.
     #   self.assert_lint('virtual void E(void (*fn)(int* p));', '')
@@ -713,14 +713,16 @@ class CpplintTest(CpplintTestBase):
     def test_explicit_single_argument_constructors(self):
         # missing explicit is bad
         self.assert_multi_line_lint(
-            '''class Foo {
+            '''class Foo
+               {
                  Foo(int f);
                };''',
             'Single-argument constructors should be marked explicit.'
             '  [runtime/explicit] [5]')
         # missing explicit is bad, even with whitespace
         self.assert_multi_line_lint(
-            '''class Foo {
+            '''class Foo
+               {
                  Foo (int f);
                };''',
             ['Extra space before ( in function call  [whitespace/parens] [4]',
@@ -728,79 +730,91 @@ class CpplintTest(CpplintTestBase):
              '  [runtime/explicit] [5]'])
         # missing explicit, with distracting comment, is still bad
         self.assert_multi_line_lint(
-            '''class Foo {
+            '''class Foo
+               {
                  Foo(int f);  // simpler than Foo(blargh, blarg)
                };''',
             'Single-argument constructors should be marked explicit.'
             '  [runtime/explicit] [5]')
         # missing explicit, with qualified classname
         self.assert_multi_line_lint(
-            '''class Qualifier::AnotherOne::Foo {
+            '''class Qualifier::AnotherOne::Foo
+               {
                  Foo(int f);
                };''',
             'Single-argument constructors should be marked explicit.'
             '  [runtime/explicit] [5]')
         # structs are caught as well.
         self.assert_multi_line_lint(
-            '''struct Foo {
+            '''struct Foo
+               {
                  Foo(int f);
                };''',
             'Single-argument constructors should be marked explicit.'
             '  [runtime/explicit] [5]')
         # Templatized classes are caught as well.
         self.assert_multi_line_lint(
-            '''template<typename T> class Foo {
+            '''template<typename T> class Foo
+               {
                  Foo(int f);
                };''',
             'Single-argument constructors should be marked explicit.'
             '  [runtime/explicit] [5]')
         # proper style is okay
         self.assert_multi_line_lint(
-            '''class Foo {
+            '''class Foo
+               {
                  explicit Foo(int f);
                };''',
             '')
         # two argument constructor is okay
         self.assert_multi_line_lint(
-            '''class Foo {
+            '''class Foo
+               {
                  Foo(int f, int b);
                };''',
             '')
         # two argument constructor, across two lines, is okay
         self.assert_multi_line_lint(
-            '''class Foo {
+            '''class Foo
+               {
                  Foo(int f,
                      int b);
                };''',
             '')
         # non-constructor (but similar name), is okay
         self.assert_multi_line_lint(
-            '''class Foo {
+            '''class Foo
+               {
                  aFoo(int f);
                };''',
             '')
         # constructor with void argument is okay
         self.assert_multi_line_lint(
-            '''class Foo {
+            '''class Foo
+               {
                  Foo(void);
                };''',
             '')
         # single argument method is okay
         self.assert_multi_line_lint(
-            '''class Foo {
+            '''class Foo
+               {
                  Bar(int b);
                };''',
             '')
         # comments should be ignored
         self.assert_multi_line_lint(
-            '''class Foo {
+            '''class Foo
+               {
                // Foo(int f);
                };''',
             '')
         # single argument function following class definition is okay
         # (okay, it's not actually valid, but we don't want a false positive)
         self.assert_multi_line_lint(
-            '''class Foo {
+            '''class Foo
+               {
                  Foo(int f, int b);
                };
                Foo(int f);''',
@@ -811,12 +825,14 @@ class CpplintTest(CpplintTestBase):
             '')
         # single argument copy constructor is okay.
         self.assert_multi_line_lint(
-            '''class Foo {
+            '''class Foo
+               {
                  Foo(const Foo&);
                };''',
             '')
         self.assert_multi_line_lint(
-            '''class Foo {
+            '''class Foo
+               {
                  Foo(Foo&);
                };''',
             '')
@@ -1604,7 +1620,8 @@ class CpplintTest(CpplintTestBase):
         # and that it will report when it can't.
         # Use multi-line linter because it performs the ClassState check.
         self.assert_multi_line_lint(
-            'class Foo {',
+            'class Foo\n'
+            '{',
             'Failed to find complete declaration of class Foo'
             '  [build/class] [5]')
         # Don't warn on forward declarations of various types.
@@ -1619,14 +1636,15 @@ class CpplintTest(CpplintTestBase):
         # the code doesn't violate the style guide.
         self.assert_multi_line_lint(
             '''class Foo
-            #ifdef DERIVE_FROM_GOO
-              : public Goo {
-            #else
-              : public Hoo {
-            #endif
-              };''',
-            'Failed to find complete declaration of class Foo'
-            '  [build/class] [5]')
+               #ifdef DERIVE_FROM_GOO
+                   : public Goo
+               {
+               #else
+                   : public Hoo
+               {
+               #endif
+               };''',
+            'Failed to find complete declaration of class Foo  [build/class] [5]')
 
     def test_build_end_comment(self):
         # The crosstool compiler we currently use will fail to compile the
@@ -2534,28 +2552,32 @@ class NoNonVirtualDestructorsTest(CpplintTestBase):
 
     def test_no_error(self):
         self.assert_multi_line_lint(
-            '''class Foo {
+            '''class Foo
+               {
                    virtual ~Foo();
                    virtual void foo();
                };''',
             '')
 
         self.assert_multi_line_lint(
-            '''class Foo {
+            '''class Foo
+               {
                    virtual inline ~Foo();
                    virtual void foo();
                };''',
             '')
 
         self.assert_multi_line_lint(
-            '''class Foo {
+            '''class Foo
+               {
                    inline virtual ~Foo();
                    virtual void foo();
                };''',
             '')
 
         self.assert_multi_line_lint(
-            '''class Foo::Goo {
+            '''class Foo::Goo
+               {
                    virtual ~Goo();
                    virtual void goo();
                };''',
@@ -2565,7 +2587,8 @@ class NoNonVirtualDestructorsTest(CpplintTestBase):
             'More than one command on the same line  [whitespace/newline] [4]')
 
         self.assert_multi_line_lint(
-            '''class Qualified::Goo : public Foo {
+            '''class Qualified::Goo : public Foo
+               {
                    virtual void goo();
                };''',
             '')
@@ -2573,7 +2596,8 @@ class NoNonVirtualDestructorsTest(CpplintTestBase):
         self.assert_multi_line_lint(
             # Line-ending :
             '''class Goo :
-               public Foo {
+               public Foo
+               {
                     virtual void goo();
                };''',
             'Labels should always be indented at least one space.  If this is a '
@@ -2582,14 +2606,16 @@ class NoNonVirtualDestructorsTest(CpplintTestBase):
 
     def test_no_destructor_when_virtual_needed(self):
         self.assert_multi_line_lint_re(
-            '''class Foo {
+            '''class Foo
+               {
                    virtual void foo();
                };''',
             'The class Foo probably needs a virtual destructor')
 
     def test_destructor_non_virtual_when_virtual_needed(self):
         self.assert_multi_line_lint_re(
-            '''class Foo {
+            '''class Foo
+               {
                    ~Foo();
                    virtual void foo();
                };''',
@@ -2597,14 +2623,16 @@ class NoNonVirtualDestructorsTest(CpplintTestBase):
 
     def test_no_warn_when_derived(self):
         self.assert_multi_line_lint(
-            '''class Foo : public Goo {
+            '''class Foo : public Goo
+               {
                    virtual void foo();
                };''',
             '')
 
     def test_internal_braces(self):
         self.assert_multi_line_lint_re(
-            '''class Foo {
+            '''class Foo
+               {
                    enum Goo {
                        GOO
                    };
@@ -2614,8 +2642,10 @@ class NoNonVirtualDestructorsTest(CpplintTestBase):
 
     def test_inner_class_needs_virtual_destructor(self):
         self.assert_multi_line_lint_re(
-            '''class Foo {
-                   class Goo {
+            '''class Foo
+               {
+                   class Goo
+                   {
                        virtual void goo();
                    };
                };''',
@@ -2623,8 +2653,10 @@ class NoNonVirtualDestructorsTest(CpplintTestBase):
 
     def test_outer_class_needs_virtual_destructor(self):
         self.assert_multi_line_lint_re(
-            '''class Foo {
-                   class Goo {
+            '''class Foo
+               {
+                   class Goo
+                   {
                    };
                    virtual void foo();
                };''',
@@ -2632,29 +2664,29 @@ class NoNonVirtualDestructorsTest(CpplintTestBase):
 
     def test_qualified_class_needs_virtual_destructor(self):
         self.assert_multi_line_lint_re(
-            '''class Qualified::Foo {
+            '''class Qualified::Foo
+               {
                    virtual void foo();
                };''',
             'The class Qualified::Foo probably needs a virtual destructor')
 
     def test_multi_line_declaration_no_error(self):
-        self.assert_multi_line_lint_re(
-            '''class Foo
-                   : public Goo {
-                   virtual void foo();
-               };''',
+        self.assert_multi_line_lint(
+            'class Foo\n'
+            '    : public Goo\n'
+            '{\n'
+            '    virtual void foo();\n'
+            '};',
             '')
 
     def test_multi_line_declaration_with_error(self):
         self.assert_multi_line_lint(
-            '''class Foo
-               {
-                   virtual void foo();
-               };''',
-            ['This { should be at the end of the previous line  '
-             '[whitespace/braces] [4]',
-             'The class Foo probably needs a virtual destructor due to having '
-             'virtual method(s), one declared at line 2.  [runtime/virtual] [4]'])
+            'class Foo\n'
+            '{\n'
+            '    virtual void foo();\n'
+            '};',
+            'The class Foo probably needs a virtual destructor due to having '
+            'virtual method(s), one declared at line 2.  [runtime/virtual] [4]')
 
 
 class WebKitStyleTest(CpplintTestBase):
@@ -2664,24 +2696,28 @@ class WebKitStyleTest(CpplintTestBase):
         # 1. Use spaces, not tabs. Tabs should only appear in files that
         #    require them for semantic meaning, like Makefiles.
         self.assert_multi_line_lint(
-            'class Foo {\n'
+            'class Foo\n'
+            '{\n'
             '    int goo;\n'
             '};',
             '')
         self.assert_multi_line_lint(
-            'class Foo {\n'
+            'class Foo\n'
+            '{\n'
             '\tint goo;\n'
             '};',
             'Tab found; better to use spaces  [whitespace/tab] [1]')
 
         # 2. The indent size is 4 spaces.
         self.assert_multi_line_lint(
-            'class Foo {\n'
+            'class Foo\n'
+            '{\n'
             '    int goo;\n'
             '};',
             '')
         self.assert_multi_line_lint(
-            'class Foo {\n'
+            'class Foo\n'
+            '{\n'
             '   int goo;\n'
             '};',
             'Weird number of spaces at line-start.  Are you using a 4-space indent?  [whitespace/indent] [3]')
@@ -2811,9 +2847,9 @@ class WebKitStyleTest(CpplintTestBase):
             '    doSomethingElse();\n',
             '')
         self.assert_multi_line_lint(
-            'if (condition)\n'
+            'if (condition) {\n'
             '    doSomething();\n'
-            'else {\n'
+            '} else {\n'
             '    doSomethingElse();\n'
             '    doSomethingElseAgain();\n'
             '}\n',
@@ -2845,7 +2881,8 @@ class WebKitStyleTest(CpplintTestBase):
         # FIXME: No tests for this.
 
     def test_braces(self):
-        # 1. Function definitions: place each brace on its own line.
+        # 1. Function definitions, classes, structs and namespaces: place
+        # each brace on its own line.
         self.assert_multi_line_lint(
             'int main()\n'
             '{\n'
@@ -2857,31 +2894,37 @@ class WebKitStyleTest(CpplintTestBase):
             '    doSomething();\n'
             '}\n',
             'Place brace on its own line for function definitions.  [whitespace/braces] [4]')
-
-        # 2. Other braces: place the open brace on the line preceding the
-        #    code block; place the close brace on its own line.
+        self.assert_multi_line_lint(
+            'class MyClass\n'
+            '{\n'
+            '    int foo;\n'
+            '};\n',
+            '')
         self.assert_multi_line_lint(
             'class MyClass {\n'
             '    int foo;\n'
             '};\n',
+            'Place brace on its own line for class/struct/namespace definitions.  [whitespace/braces] [4]')
+        self.assert_multi_line_lint(
+            'namespace WebCore\n'
+            '{\n'
+            '    int foo;\n'
+            '};\n',
             '')
         self.assert_multi_line_lint(
             'namespace WebCore {\n'
             '    int foo;\n'
             '};\n',
-            '')
+            'Place brace on its own line for class/struct/namespace definitions.  [whitespace/braces] [4]')
+
+        # 2. Other braces: place the open brace on the line preceding the
+        #    code block; place the close brace on its own line.
         self.assert_multi_line_lint(
             'for (int i = 0; i < 10; i++) {\n'
             '    DoSomething();\n'
             '};\n',
             '')
         self.assert_multi_line_lint(
-            'class MyClass\n'
-            '{\n'
-            '    int foo;\n'
-            '};\n',
-            'This { should be at the end of the previous line  [whitespace/braces] [4]')
-        self.assert_multi_line_lint(
             'if (condition)\n'
             '{\n'
             '    int foo;\n'