Title: [283625] trunk/Tools
Revision
283625
Author
[email protected]
Date
2021-10-06 08:57:51 -0700 (Wed, 06 Oct 2021)

Log Message

Style checker should check spacing inside braces with elements in them
https://bugs.webkit.org/show_bug.cgi?id=231199

In order to automate this and prevent reviewers from having to comment
on it, ensure that spaces are required by webkit-check-style inside of
braces with elements in them (e.g., initializer lists.)

In order to avoid false positives, ignore brace spacing if the braces
are surrounded by double-quotes on the same line, assuming that means
it's likely the braces are inside a string.

Patch by Philip Chimento <[email protected]> on 2021-10-06
Reviewed by Jonathan Bedard.

* Scripts/webkitpy/style/checkers/cpp.py:
(check_spacing):
* Scripts/webkitpy/style/checkers/cpp_unittest.py:
(CppStyleTest):
(NoNonVirtualDestructorsTest):
(WebKitStyleTest.test_indentation):

Modified Paths

Diff

Modified: trunk/Tools/ChangeLog (283624 => 283625)


--- trunk/Tools/ChangeLog	2021-10-06 15:56:21 UTC (rev 283624)
+++ trunk/Tools/ChangeLog	2021-10-06 15:57:51 UTC (rev 283625)
@@ -1,3 +1,25 @@
+2021-10-06  Philip Chimento  <[email protected]>
+
+        Style checker should check spacing inside braces with elements in them
+        https://bugs.webkit.org/show_bug.cgi?id=231199
+
+        In order to automate this and prevent reviewers from having to comment
+        on it, ensure that spaces are required by webkit-check-style inside of
+        braces with elements in them (e.g., initializer lists.)
+
+        In order to avoid false positives, ignore brace spacing if the braces
+        are surrounded by double-quotes on the same line, assuming that means
+        it's likely the braces are inside a string.
+
+        Reviewed by Jonathan Bedard.
+
+        * Scripts/webkitpy/style/checkers/cpp.py:
+        (check_spacing):
+        * Scripts/webkitpy/style/checkers/cpp_unittest.py:
+        (CppStyleTest):
+        (NoNonVirtualDestructorsTest):
+        (WebKitStyleTest.test_indentation):
+
 2021-10-06  Youenn Fablet  <[email protected]>
 
         Implement https://w3c.github.io/push-api/#receiving-a-push-message

Modified: trunk/Tools/Scripts/webkitpy/style/checkers/cpp.py (283624 => 283625)


--- trunk/Tools/Scripts/webkitpy/style/checkers/cpp.py	2021-10-06 15:56:21 UTC (rev 283624)
+++ trunk/Tools/Scripts/webkitpy/style/checkers/cpp.py	2021-10-06 15:57:51 UTC (rev 283625)
@@ -2334,12 +2334,26 @@
             error(line_number, 'whitespace/brackets', 5,
                   'Extra space before [.')
 
-    # There should always be a single space in between braces on the same line.
-    if search(r'\{\}', line):
-        error(line_number, 'whitespace/braces', 5, 'Missing space inside { }.')
-    if search(r'\{\s\s+\}', line):
-        error(line_number, 'whitespace/braces', 5, 'Too many spaces inside { }.')
+    # Try to avoid false positives when braces are inside of a string
+    if not search(r'"[^"]*\{.*\}[^"]*"', line):
+        # There should always be a single space in between braces on the same
+        # line.
+        if search(r'\{\}', line):
+            error(line_number, 'whitespace/braces', 5, 'Missing space inside { }.')
+        if search(r'\{\s\s+\}', line):
+            error(line_number, 'whitespace/braces', 5, 'Too many spaces inside { }.')
 
+        # Also a single space inside of braces with an initializer value inside
+        # of them.
+        if search(r'\{[^\s\}]', line):
+            error(line_number, 'whitespace/braces', 5, 'Missing space after {.')
+        if search(r'[^\s\{]\}', line):
+            error(line_number, 'whitespace/braces', 5, 'Missing space before }.')
+        if search(r'\{\s\s+[^\s\}]', line):
+            error(line_number, 'whitespace/braces', 5, 'Too many spaces after {.')
+        if search(r'[^\s\{]\s\+\}', line):
+            error(line_number, 'whitespace/braces', 5, 'Too many spaces before }.')
+
     # You shouldn't have a space before a semicolon at the end of the line.
     # There's a special case for "for" since the style guide allows space before
     # the semicolon there.

Modified: trunk/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py (283624 => 283625)


--- trunk/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py	2021-10-06 15:56:21 UTC (rev 283624)
+++ trunk/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py	2021-10-06 15:57:51 UTC (rev 283625)
@@ -1844,7 +1844,7 @@
         self.assert_lint(
             '''\
             const int foo[] =
-                {1, 2, 3 };''',
+                { 1, 2, 3 };''',
             '')
         # For single line, unmatched '}' with a ';' is ignored (not enough context)
         self.assert_multi_line_lint(
@@ -2150,6 +2150,24 @@
         self.assert_lint('    { }', '')
         self.assert_lint('    {}', 'Missing space inside { }.  [whitespace/braces] [5]')
         self.assert_lint('    {   }', 'Too many spaces inside { }.  [whitespace/braces] [5]')
+        self.assert_lint('    }', '')  # closing brace by itself is fine
+        self.assert_lint('    int64_t {0xffffffff }', 'Missing space after {.  [whitespace/braces] [5]')
+        self.assert_lint('    int64_t { 0xffffffff}', 'Missing space before }.  [whitespace/braces] [5]')
+        self.assert_lint('    int64_t { 0xffffffff }', '')
+        self.assert_lint('    IntTuple {1, 2 }', 'Missing space after {.  [whitespace/braces] [5]')
+        self.assert_lint('    IntTuple { 1, 2}', 'Missing space before }.  [whitespace/braces] [5]')
+        self.assert_lint('    IntTuple { 1, 2 }', '')
+        self.assert_lint('    int a[2][2] = {{ 1, 2 }, { 3, 4 } };', 'Missing space after {.  [whitespace/braces] [5]')
+        self.assert_lint('    int a[2][2] = { {1, 2 }, { 3, 4 } };', 'Missing space after {.  [whitespace/braces] [5]')
+        self.assert_lint('    int a[2][2] = { { 1, 2 }, {3, 4 } };', 'Missing space after {.  [whitespace/braces] [5]')
+        self.assert_lint('    int a[2][2] = { { 1, 2}, { 3, 4 } };', 'Missing space before }.  [whitespace/braces] [5]')
+        self.assert_lint('    int a[2][2] = { { 1, 2 }, { 3, 4} };', 'Missing space before }.  [whitespace/braces] [5]')
+        self.assert_lint('    int a[2][2] = { { 1, 2 }, { 3, 4 }};', 'Missing space before }.  [whitespace/braces] [5]')
+        self.assert_lint('    int a[2][2] = { { 1, 2 }, { 3, 4 } };', '')
+        self.assert_lint('    StrPair {"strings", "inside" }', 'Missing space after {.  [whitespace/braces] [5]')
+        self.assert_lint('    StrPair { "strings", "inside"}', 'Missing space before }.  [whitespace/braces] [5]')
+        self.assert_lint('    StrPair { "strings", "inside" }', '')
+        self.assert_lint('    foo("{braces in a string}");', '')
 
     def test_spacing_before_brackets(self):
         self.assert_lint('delete [] base;', '')
@@ -4058,7 +4076,7 @@
                     FOO_TWO
                 };
                 enum { FOO_ONE };
-                enum {FooOne, fooTwo};
+                enum { FooOne, fooTwo };
                 enum {
                     FOO_ONE
                 };''',
@@ -4110,7 +4128,7 @@
                     FOO_TWO
                 };
                 enum class Foo { FOO_ONE };
-                enum class Foo {FooOne, fooTwo};
+                enum class Foo { FooOne, fooTwo };
                 enum class Foo {
                     FOO_ONE
                 };''',
@@ -4486,7 +4504,7 @@
             'namespace WebCore {\n'
             '#define abc(x) x; \\\n'
             '    x\n'
-            '    void* x;'
+            '    void* x;\n'
             '}',
             'Code inside a namespace should not be indented.  [whitespace/indent] [4]',
             'foo.cpp')
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to