Teach the style checker about ENUM_CLASS
authorap@apple.com <ap@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 31 Oct 2013 04:11:02 +0000 (04:11 +0000)
committerap@apple.com <ap@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 31 Oct 2013 04:11:02 +0000 (04:11 +0000)
https://bugs.webkit.org/show_bug.cgi?id=123528

Reviewed by Andy Estes.

* Scripts/webkitpy/style/checkers/cpp.py:
(_EnumState.process_clean_line): Make content of an ENUM_CLASS declaration
equivalent to enum content for checking.
(check_braces): Allow semicolon at the end.

* Scripts/webkitpy/style/checkers/cpp_unittest.py:
(NoNonVirtualDestructorsTest.test_enum_casing):
(NoNonVirtualDestructorsTest.test_enum_trailing_semicolon):
(WebKitStyleTest.test_braces):
Test it.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@158342 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Tools/ChangeLog
Tools/Scripts/webkitpy/style/checkers/cpp.py
Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py

index cee6ebe..18b4a06 100644 (file)
@@ -1,3 +1,21 @@
+2013-10-30  Alexey Proskuryakov  <ap@apple.com>
+
+        Teach the style checker about ENUM_CLASS
+        https://bugs.webkit.org/show_bug.cgi?id=123528
+
+        Reviewed by Andy Estes.
+
+        * Scripts/webkitpy/style/checkers/cpp.py:
+        (_EnumState.process_clean_line): Make content of an ENUM_CLASS declaration
+        equivalent to enum content for checking.
+        (check_braces): Allow semicolon at the end.
+
+        * Scripts/webkitpy/style/checkers/cpp_unittest.py:
+        (NoNonVirtualDestructorsTest.test_enum_casing):
+        (NoNonVirtualDestructorsTest.test_enum_trailing_semicolon):
+        (WebKitStyleTest.test_braces):
+        Test it.
+
 2013-10-30  Ryosuke Niwa  <rniwa@webkit.org>
 
         kill-old-processes should kill Web Socket script
index 435c7a4..5e0fd01 100644 (file)
@@ -1193,7 +1193,7 @@ class _EnumState(object):
         expr_all_uppercase = r'\s*[A-Z0-9_]+\s*(?:=\s*[a-zA-Z0-9]+\s*)?,?\s*$'
         expr_starts_lowercase = r'\s*[a-z]'
         expr_enum_end = r'}\s*(?:[a-zA-Z0-9]+\s*(?:=\s*[a-zA-Z0-9]+)?)?\s*;\s*'
-        expr_enum_start = r'\s*enum(?:\s+[a-zA-Z0-9]+)?\s*\{?\s*'
+        expr_enum_start = r'\s*(?:enum(?:\s+[a-zA-Z0-9]+)?|ENUM_CLASS\s*\([a-zA-Z0-9]+\))\s*\{?\s*'
         if self.in_enum_decl:
             if match(r'\s*' + expr_enum_end + r'$', line):
                 self.in_enum_decl = False
@@ -2291,13 +2291,13 @@ def check_braces(clean_lines, line_number, error):
         # We also allow '#' for #endif and '=' for array initialization.
         previous_line = get_previous_non_blank_line(clean_lines, line_number)[0]
         if ((not search(r'[;:}{)=]\s*$|\)\s*((const|OVERRIDE)\s*)*\s*$', previous_line)
-             or search(r'\b(if|for|foreach|while|switch|else|NS_ENUM)\b', previous_line))
+             or search(r'\b(if|for|foreach|while|switch|else|NS_ENUM|ENUM_CLASS)\b', previous_line))
             and previous_line.find('#') < 0):
             error(line_number, 'whitespace/braces', 4,
                   'This { should be at the end of the previous line')
     elif (search(r'\)\s*(((const|OVERRIDE)\s*)*\s*)?{\s*$', line)
           and line.count('(') == line.count(')')
-          and not search(r'\b(if|for|foreach|while|switch|NS_ENUM)\b', line)
+          and not search(r'\b(if|for|foreach|while|switch|NS_ENUM|ENUM_CLASS)\b', line)
           and not match(r'\s+[A-Z_][A-Z_0-9]+\b', line)):
         error(line_number, 'whitespace/braces', 4,
               'Place brace on its own line for function definitions.')
@@ -2341,7 +2341,7 @@ def check_braces(clean_lines, line_number, error):
             break
     if (search(r'{.*}\s*;', line)
         and line.count('{') == line.count('}')
-        and not search(r'struct|class|enum|\s*=\s*{', line)):
+        and not search(r'struct|class|enum|ENUM_CLASS|\s*=\s*{', line)):
         error(line_number, 'readability/braces', 4,
               "You don't need a ; after a }")
 
index fa8d059..079a469 100644 (file)
@@ -3277,6 +3277,39 @@ class NoNonVirtualDestructorsTest(CppStyleTestBase):
 
         self.assert_multi_line_lint(
             '''\
+                ENUM_CLASS(Foo) {
+                    FOO_ONE = 1,
+                    FOO_TWO
+                };
+                ENUM_CLASS(Foo) { FOO_ONE };
+                ENUM_CLASS(Foo) {FooOne, fooTwo};
+                ENUM_CLASS(Foo) {
+                    FOO_ONE
+                };''',
+            ['enum members should use InterCaps with an initial capital letter.  [readability/enum_casing] [4]'] * 5)
+
+        self.assert_multi_line_lint(
+            '''\
+                ENUM_CLASS(Foo) {
+                    fooOne = 1,
+                    FooTwo = 2
+                };''',
+            'enum members should use InterCaps with an initial capital letter.  [readability/enum_casing] [4]')
+
+        self.assert_multi_line_lint(
+            '''\
+                ENUM_CLASS(Foo) {
+                    FooOne = 1,
+                    FooTwo
+                } fooVar = FooOne;
+                ENUM_CLASS(Enum123) {
+                    FooOne,
+                    FooTwo = FooOne,
+                };''',
+            '')
+
+        self.assert_multi_line_lint(
+            '''\
                 // WebIDL enum
                 enum Foo {
                     FOO_ONE = 1,
@@ -3290,6 +3323,26 @@ class NoNonVirtualDestructorsTest(CppStyleTestBase):
                 enum Foo { FOO_ONE, FOO_TWO };''',
             '')
 
+    def test_enum_trailing_semicolon(self):
+        self.assert_lint(
+            'enum MyEnum { Value1, Value2 };',
+            '')
+        self.assert_lint(
+            'enum MyEnum {\n'
+            '    Value1,\n'
+            '    Value2\n'
+            '};',
+            '')
+        self.assert_lint(
+            'ENUM_CLASS(CPP11EnumClass) { Value1, Value2 };',
+            '')
+        self.assert_lint(
+            'ENUM_CLASS(MyEnum) {\n'
+            '    Value1,\n'
+            '    Value2\n'
+            '};',
+            '')
+
     def test_destructor_non_virtual_when_virtual_needed(self):
         self.assert_multi_line_lint_re(
             '''\
@@ -4213,6 +4266,18 @@ class WebKitStyleTest(CppStyleTestBase):
             '    0,\n'
             '    1\n'
             '};', '')
+        self.assert_multi_line_lint(
+            'ENUM_CLASS(CPP11EnumClass)\n'
+            '{\n'
+            '    Value1,\n'
+            '    Value2\n'
+            '};',
+            'This { should be at the end of the previous line  [whitespace/braces] [4]')
+        self.assert_multi_line_lint(
+            'ENUM_CLASS(CPP11EnumClass) {\n'
+            '    Value1,\n'
+            '    Value2\n'
+            '};', '')
 
         # 3. One-line control clauses should not use braces unless
         #    comments are included or a single statement spans multiple