check-webkit-style failed to complain about missing braces
authorllango.u-szeged@partner.samsung.com <llango.u-szeged@partner.samsung.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 11 Mar 2014 14:56:48 +0000 (14:56 +0000)
committerllango.u-szeged@partner.samsung.com <llango.u-szeged@partner.samsung.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 11 Mar 2014 14:56:48 +0000 (14:56 +0000)
https://bugs.webkit.org/show_bug.cgi?id=34189

Reviewed by Ryosuke Niwa.

* Scripts/webkitpy/style/checkers/cpp.py:
(check_braces):
* Scripts/webkitpy/style/checkers/cpp_unittest.py:
(WebKitStyleTest.test_braces):

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

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

index 60f8145..450433e 100644 (file)
@@ -1,5 +1,17 @@
 2014-03-11  László Langó  <llango.u-szeged@partner.samsung.com>
 
+        check-webkit-style failed to complain about missing braces
+        https://bugs.webkit.org/show_bug.cgi?id=34189
+
+        Reviewed by Ryosuke Niwa.
+
+        * Scripts/webkitpy/style/checkers/cpp.py:
+        (check_braces):
+        * Scripts/webkitpy/style/checkers/cpp_unittest.py:
+        (WebKitStyleTest.test_braces):
+
+2014-03-11  László Langó  <llango.u-szeged@partner.samsung.com>
+
         Remove Qt cruft from style checker.
         https://bugs.webkit.org/show_bug.cgi?id=130085
 
index 87649ad..3026472 100644 (file)
@@ -2401,6 +2401,33 @@ def check_braces(clean_lines, line_number, error):
         error(line_number, 'whitespace/newline', 4,
               'do/while clauses should not be on a single line')
 
+    # Multi line control clauses should use braces. We check the
+    # indentation level of the statements.
+    if (match(r'^\s*\b(if|for|while|else)\b\s', line)
+        and match(r'.*[^{]$', line)
+        and len(clean_lines.elided) > line_number + 2):
+        has_braces = False
+        begin_line = line
+        begin_line_number = line_number
+        while (clean_lines.elided[begin_line_number + 1].strip().startswith("&&")
+            or clean_lines.elided[begin_line_number + 1].strip().startswith("||")
+            or search(r'^#\S*', clean_lines.elided[begin_line_number + 1])):
+            begin_line_number = begin_line_number + 1
+            begin_line = clean_lines.elided[begin_line_number]
+            if search(r'.*{$', begin_line):
+                has_braces = True
+
+        next_line = clean_lines.elided[begin_line_number + 1]
+        after_next_line = clean_lines.elided[begin_line_number + 2]
+        control_indent = search(r'^(?P<indentation>\s*).*', line).group('indentation')
+        next_line_indent = search(r'^(?P<indentation>\s*).*', next_line).group('indentation')
+        after_next_line_indent = search(r'^(?P<indentation>\s*).*', after_next_line).group('indentation')
+        if (after_next_line != ''
+            and not has_braces
+            and control_indent < next_line_indent
+            and control_indent < after_next_line_indent):
+            error(line_number, 'whitespace/braces', 4, 'Multi line control clauses should use braces.')
+
     # Braces shouldn't be followed by a ; unless they're defining a struct
     # or initializing an array.
     # We can't tell in general, but we can for some common cases.
index 8242baf..4c32876 100644 (file)
@@ -4386,6 +4386,56 @@ class WebKitStyleTest(CppStyleTestBase):
         self.assert_multi_line_lint(
             '} while (true);\n',
             '')
+        # 5. Multi line control clauses should use braces.
+        self.assert_multi_line_lint(
+            'for ( ; c1 && c2 ; )\n'
+            '    if (condition1 && condition2)\n'
+            '        i = 1;\n',
+            'Multi line control clauses should use braces.  [whitespace/braces] [4]')
+        self.assert_multi_line_lint(
+            '    if (condition)\n'
+            '        i = 1;\n'
+            '        j = 1;\n',
+            'Multi line control clauses should use braces.  [whitespace/braces] [4]')
+        self.assert_multi_line_lint(
+            '    if (condition1\n'
+            '        || condition2\n'
+            '        && condition3)\n'
+            '        i = 1;\n'
+            '        j = 1;\n',
+            'Multi line control clauses should use braces.  [whitespace/braces] [4]')
+        self.assert_multi_line_lint(
+            '    if (condition1\n'
+            '        || condition2\n'
+            '        && condition3) {\n'
+            '        i = 1;\n'
+            '        j = 1;\n'
+            '    }\n',
+            '')
+        self.assert_multi_line_lint(
+            'if (condition)\n'
+            '#ifdef SOMETHING\n'
+            '    i = 1;\n'
+            '#endif\n',
+            '')
+        self.assert_multi_line_lint(
+            '#ifdef SOMETHING\n'
+            'if (condition)\n'
+            '#endif\n'
+            '    // Some comment\n'
+            '    i = 1;\n',
+            'Multi line control clauses should use braces.  [whitespace/braces] [4]')
+        self.assert_multi_line_lint(
+            'if (condition)\n'
+            '    myFunction(reallyLongParam1, reallyLongParam2, ...\n'
+            '        reallyLongParam5);\n',
+            'Multi line control clauses should use braces.  [whitespace/braces] [4]')
+        self.assert_multi_line_lint(
+            'if (condition) {\n'
+            '    myFunction(reallyLongParam1, reallyLongParam2, ...\n'
+            '        reallyLongParam5);\n'
+            '    }\n',
+            '')
 
     def test_null_false_zero(self):
         # 1. In C++, the null pointer value should be written as 0. In C,