Title: [156105] trunk
Revision
156105
Author
[email protected]
Date
2013-09-19 11:13:19 -0700 (Thu, 19 Sep 2013)

Log Message

Add a style guideline regarding spacing in range-based for loops
https://bugs.webkit.org/show_bug.cgi?id=121620

Reviewed by Anders Carlsson.

Tools: 

* Scripts/webkitpy/style/checkers/cpp.py:
(check_spacing): Added checking that there are spaces around the colon in a range-based for
loop.
(CppChecker): Added whitespace/colon to the categories set.
* Scripts/webkitpy/style/checkers/cpp_unittest.py:
(WebKitStyleTest.test_spacing): Added two tests for the new check.

Websites/webkit.org: 

* coding/coding-style.html: Added the guideline that spaces should be placed
around the colon in a range-based for loop.

Modified Paths

Diff

Modified: trunk/Tools/ChangeLog (156104 => 156105)


--- trunk/Tools/ChangeLog	2013-09-19 17:57:47 UTC (rev 156104)
+++ trunk/Tools/ChangeLog	2013-09-19 18:13:19 UTC (rev 156105)
@@ -1,3 +1,17 @@
+2013-09-19  Dan Bernstein  <[email protected]>
+
+        Add a style guideline regarding spacing in range-based for loops
+        https://bugs.webkit.org/show_bug.cgi?id=121620
+
+        Reviewed by Anders Carlsson.
+
+        * Scripts/webkitpy/style/checkers/cpp.py:
+        (check_spacing): Added checking that there are spaces around the colon in a range-based for
+        loop.
+        (CppChecker): Added whitespace/colon to the categories set.
+        * Scripts/webkitpy/style/checkers/cpp_unittest.py:
+        (WebKitStyleTest.test_spacing): Added two tests for the new check.
+
 2013-09-18  Sam Weinig  <[email protected]>
 
         Replace use of OwnArrayPtr<Foo> with std::unique_ptr<Foo[]> in Tools

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


--- trunk/Tools/Scripts/webkitpy/style/checkers/cpp.py	2013-09-19 17:57:47 UTC (rev 156104)
+++ trunk/Tools/Scripts/webkitpy/style/checkers/cpp.py	2013-09-19 18:13:19 UTC (rev 156105)
@@ -1907,6 +1907,8 @@
         statement = matched.group('statement')
         condition, rest = up_to_unmatched_closing_paren(matched.group('remainder'))
         if condition is not None:
+            if statement == 'for' and search(r'(?:[^ :]:[^:]|[^:]:[^ :])', condition):
+                error(line_number, 'whitespace/colon', 4, 'Missing space around : in range-based for statement')
             condition_match = search(r'(?P<leading>[ ]*)(?P<separator>.).*[^ ]+(?P<trailing>[ ]*)', condition)
             if condition_match:
                 n_leading = len(condition_match.group('leading'))
@@ -3699,6 +3701,7 @@
         'runtime/virtual',
         'whitespace/blank_line',
         'whitespace/braces',
+        'whitespace/colon',
         'whitespace/comma',
         'whitespace/comments',
         'whitespace/declaration',

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


--- trunk/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py	2013-09-19 17:57:47 UTC (rev 156104)
+++ trunk/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py	2013-09-19 18:13:19 UTC (rev 156105)
@@ -3849,8 +3849,19 @@
         #     'return condition ? 1:0;',
         #     '')
 
-        # 3. Place spaces between control statements and their parentheses.
+        # 3. Place spaces around the colon in a range-based for loop.
         self.assert_multi_line_lint(
+            '    for (const WTF::Vector& vector : vectors)\n'
+            '        process(vector);\n',
+            '')
+
+        self.assert_multi_line_lint(
+            '    for (const Vector& vector: vectors)\n'
+            '        process(vector);\n',
+            'Missing space around : in range-based for statement  [whitespace/colon] [4]')
+
+        # 5. Place spaces between control statements and their parentheses.
+        self.assert_multi_line_lint(
             '    if (condition)\n'
             '        doIt();\n',
             '')
@@ -3859,7 +3870,7 @@
             '        doIt();\n',
             'Missing space before ( in if(  [whitespace/parens] [5]')
 
-        # 4. Do not place spaces between a function and its parentheses,
+        # 6. Do not place spaces between a function and its parentheses,
         #    or between a parenthesis and its content.
         self.assert_multi_line_lint(
             'f(a, b);',

Modified: trunk/Websites/webkit.org/ChangeLog (156104 => 156105)


--- trunk/Websites/webkit.org/ChangeLog	2013-09-19 17:57:47 UTC (rev 156104)
+++ trunk/Websites/webkit.org/ChangeLog	2013-09-19 18:13:19 UTC (rev 156105)
@@ -1,3 +1,13 @@
+2013-09-19  Dan Bernstein  <[email protected]>
+
+        Add a style guideline regarding spacing in range-based for loops
+        https://bugs.webkit.org/show_bug.cgi?id=121620
+
+        Reviewed by Anders Carlsson.
+
+        * coding/coding-style.html: Added the guideline that spaces should be placed
+        around the colon in a range-based for loop.
+
 2013-09-10  Beth Dakin  <[email protected]>
 
         Adding another screenshot for a potential blog post.

Modified: trunk/Websites/webkit.org/coding/coding-style.html (156104 => 156105)


--- trunk/Websites/webkit.org/coding/coding-style.html	2013-09-19 17:57:47 UTC (rev 156104)
+++ trunk/Websites/webkit.org/coding/coding-style.html	2013-09-19 18:13:19 UTC (rev 156105)
@@ -188,6 +188,22 @@
 </pre>
 </li>
 
+<li id="spacing-for-colon">Place spaces around the colon in a range-based for loop.
+<h4 class="right">Right:</h4>
+<pre class="code">
+Vector&lt;PluginModuleInfo> plugins;
+for (const auto&amp; plugin : plugins)
+    registerPlugin(plugin);
+</pre>
+
+<h4 class="wrong">Wrong:</h4>
+<pre class="code">
+Vector&lt;PluginModuleInfo> plugins;
+for (const auto&amp; plugin: plugins)
+    registerPlugin(plugin);
+</pre>
+</li>
+
 <li id="spacing-comma-semicolon">Do not place spaces before comma and semicolon.
 <h4 class="right">Right:</h4>
 <pre class="code">
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to