Skip to content
Permalink
Browse files
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 <pchimento@igalia.com> 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):

Canonical link: https://commits.webkit.org/242577@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@283625 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
ptomato authored and webkit-commit-queue committed Oct 6, 2021
1 parent 9c803db commit 25821a95c581cac0222513294da89e06b9cd25ab
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
@@ -1,3 +1,25 @@
2021-10-06 Philip Chimento <pchimento@igalia.com>

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 <youenn@apple.com>

Implement https://w3c.github.io/push-api/#receiving-a-push-message
@@ -2334,11 +2334,25 @@ def check_spacing(file_extension, clean_lines, line_number, file_state, error):
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
@@ -1844,7 +1844,7 @@ def test_braces(self):
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 @@ def test_spacing_between_braces(self):
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 @@ def test_enum_casing(self):
FOO_TWO
};
enum { FOO_ONE };
enum {FooOne, fooTwo};
enum { FooOne, fooTwo };
enum {
FOO_ONE
};''',
@@ -4110,7 +4128,7 @@ def test_enum_casing(self):
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 @@ def test_indentation(self):
'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')

0 comments on commit 25821a9

Please sign in to comment.