From: llango.u-szeged@partner.samsung.com Date: Tue, 11 Mar 2014 14:56:48 +0000 (+0000) Subject: check-webkit-style failed to complain about missing braces X-Git-Url: http://git.webkit.org/?p=WebKit-https.git;a=commitdiff_plain;h=0fccabfb09b38710d64e143ab30ae139efa55b52 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): git-svn-id: https://svn.webkit.org/repository/webkit/trunk@165451 268f45cc-cd09-0410-ab3c-d52691b4dbfc --- diff --git a/Tools/ChangeLog b/Tools/ChangeLog index 60f8145..450433e 100644 --- a/Tools/ChangeLog +++ b/Tools/ChangeLog @@ -1,5 +1,17 @@ 2014-03-11 László Langó + 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ó + Remove Qt cruft from style checker. https://bugs.webkit.org/show_bug.cgi?id=130085 diff --git a/Tools/Scripts/webkitpy/style/checkers/cpp.py b/Tools/Scripts/webkitpy/style/checkers/cpp.py index 87649ad..3026472 100644 --- a/Tools/Scripts/webkitpy/style/checkers/cpp.py +++ b/Tools/Scripts/webkitpy/style/checkers/cpp.py @@ -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\s*).*', line).group('indentation') + next_line_indent = search(r'^(?P\s*).*', next_line).group('indentation') + after_next_line_indent = search(r'^(?P\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. diff --git a/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py b/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py index 8242baf..4c32876 100644 --- a/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py +++ b/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py @@ -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,