Changeset 53989 in webkit


Ignore:
Timestamp:
Jan 28, 2010 12:29:05 AM (14 years ago)
Author:
eric@webkit.org
Message:

2010-01-28 Anton Muhin <antonm@chromium.org>

Reviewed by Shinichiro Hamaji.

Improve treatment of conditions and rest of the line for if, else, switch and alikes
https://bugs.webkit.org/show_bug.cgi?id=34173

  • Scripts/webkitpy/style/cpp_style.py:
  • Scripts/webkitpy/style/cpp_style_unittest.py:
Location:
trunk/WebKitTools
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebKitTools/ChangeLog

    r53976 r53989  
     12010-01-28  Anton Muhin  <antonm@chromium.org>
     2
     3        Reviewed by Shinichiro Hamaji.
     4
     5        Improve treatment of conditions and rest of the line for if, else, switch and alikes
     6        https://bugs.webkit.org/show_bug.cgi?id=34173
     7
     8        * Scripts/webkitpy/style/cpp_style.py:
     9        * Scripts/webkitpy/style/cpp_style_unittest.py:
     10
    1112010-01-28  Joe Mason  <jmason@rim.com>
    212
  • trunk/WebKitTools/Scripts/webkitpy/style/processors/cpp.py

    r53882 r53989  
    152152    return _regexp_compile_cache[pattern].subn(replacement, s)
    153153
     154
     155def up_to_unmatched_closing_paren(s):
     156    """Splits a string into two parts up to first unmatched ')'.
     157
     158    Args:
     159      s: a string which is a substring of line after '('
     160      (e.g., "a == (b + c))").
     161
     162    Returns:
     163      A pair of strings (prefix before first unmatched ')',
     164      reminder of s after first unmatched ')'), e.g.,
     165      up_to_unmatched_closing_paren("a == (b + c)) { ")
     166      returns "a == (b + c)", " {".
     167      Returns None, None if there is no unmatched ')'
     168
     169    """
     170    i = 1
     171    for pos, c in enumerate(s):
     172      if c == '(':
     173        i += 1
     174      elif c == ')':
     175        i -= 1
     176        if i == 0:
     177          return s[:pos], s[pos + 1:]
     178    return None, None
    154179
    155180class _IncludeState(dict):
     
    13101335    # We don't want: "if ( foo)" or "if ( foo   )".
    13111336    # Exception: "for ( ; foo; bar)" and "for (foo; bar; )" are allowed.
    1312     matched = search(r'\b(if|for|foreach|while|switch)\s*\(([ ]*)(.).*[^ ]+([ ]*)\)\s*{\s*$',
    1313                      line)
     1337    matched = search(r'\b(?P<statement>if|for|foreach|while|switch)\s*\((?P<reminder>.*)$', line)
    13141338    if matched:
    1315         if len(matched.group(2)) != len(matched.group(4)):
    1316             if not (matched.group(3) == ';'
    1317                     and len(matched.group(2)) == 1 + len(matched.group(4))
    1318                     or not matched.group(2) and search(r'\bfor\s*\(.*; \)', line)):
    1319                 error(line_number, 'whitespace/parens', 5,
    1320                       'Mismatching spaces inside () in %s' % matched.group(1))
    1321         if not len(matched.group(2)) in [0, 1]:
    1322             error(line_number, 'whitespace/parens', 5,
    1323                   'Should have zero or one spaces inside ( and ) in %s' %
    1324                   matched.group(1))
     1339        statement = matched.group('statement')
     1340        condition, rest = up_to_unmatched_closing_paren(matched.group('reminder'))
     1341        if condition is not None:
     1342            condition_match = search(r'(?P<leading>[ ]*)(?P<separator>.).*[^ ]+(?P<trailing>[ ]*)', condition)
     1343            if condition_match:
     1344                n_leading = len(condition_match.group('leading'))
     1345                n_trailing = len(condition_match.group('trailing'))
     1346                if n_leading != n_trailing:
     1347                    for_exception = statement == 'for' and (
     1348                        (condition.startswith(' ;') and n_trailing == 0) or
     1349                        (condition.endswith('; ')   and n_leading == 0))
     1350                    if not for_exception:
     1351                        error(line_number, 'whitespace/parens', 5,
     1352                              'Mismatching spaces inside () in %s' % statement)
     1353                if n_leading > 1:
     1354                    error(line_number, 'whitespace/parens', 5,
     1355                          'Should have zero or one spaces inside ( and ) in %s' %
     1356                          statement)
     1357
     1358            # Do not check for more than one command in macros
     1359            in_macro = match(r'\s*#define', line)
     1360            if not in_macro and not match(r'((\s*{\s*}?)|(\s*;?))\s*\\?$', rest):
     1361                error(line_number, 'whitespace/parens', 4,
     1362                      'More than one command on the same line in %s' % statement)
    13251363
    13261364    # You should always have a space after a comma (either as fn arg or operator)
  • trunk/WebKitTools/Scripts/webkitpy/style/processors/cpp_unittest.py

    r53882 r53989  
    11681168        self.assert_lint('for (foo; ba; bar ) {', 'Mismatching spaces inside () in for'
    11691169                         '  [whitespace/parens] [5]')
     1170        self.assert_lint('for ((foo); (ba); (bar) ) {', 'Mismatching spaces inside () in for'
     1171                         '  [whitespace/parens] [5]')
    11701172        self.assert_lint('for (; foo; bar) {', '')
     1173        self.assert_lint('for (; (foo); (bar)) {', '')
    11711174        self.assert_lint('for ( ; foo; bar) {', '')
     1175        self.assert_lint('for ( ; (foo); (bar)) {', '')
    11721176        self.assert_lint('for ( ; foo; bar ) {', '')
     1177        self.assert_lint('for ( ; (foo); (bar) ) {', '')
    11731178        self.assert_lint('for (foo; bar; ) {', '')
     1179        self.assert_lint('for ((foo); (bar); ) {', '')
    11741180        self.assert_lint('foreach (foo, foos ) {', 'Mismatching spaces inside () in foreach'
    11751181                         '  [whitespace/parens] [5]')
     
    29993005            '')
    30003006        self.assert_multi_line_lint(
     3007            '    if (condition) \\\n'
     3008            '        doIt();\n',
     3009            '')
     3010        self.assert_multi_line_lint(
    30013011            '    x++; y++;',
    30023012            'More than one command on the same line  [whitespace/newline] [4]')
    3003         # FIXME: Make this fail.
    3004         # self.assert_multi_line_lint(
    3005         #     '    if (condition) doIt();\n',
    3006         #     '')
     3013        self.assert_multi_line_lint(
     3014            '    if (condition) doIt();\n',
     3015            'More than one command on the same line in if  [whitespace/parens] [4]')
    30073016
    30083017        # 2. An else statement should go on the same line as a preceding
     
    30353044            '#define TEST_ASSERT(expression) do { if (!(expression)) { TestsController::shared().testFailed(__FILE__, __LINE__, #expression); return; } } while (0)\n',
    30363045            '')
     3046        self.assert_multi_line_lint(
     3047            '#define TEST_ASSERT(expression) do { if ( !(expression)) { TestsController::shared().testFailed(__FILE__, __LINE__, #expression); return; } } while (0)\n',
     3048            'Mismatching spaces inside () in if  [whitespace/parens] [5]')
     3049        # FIXME: currently we only check first conditional, so we cannot detect errors in next ones.
     3050        # self.assert_multi_line_lint(
     3051        #     '#define TEST_ASSERT(expression) do { if (!(expression)) { TestsController::shared().testFailed(__FILE__, __LINE__, #expression); return; } } while (0 )\n',
     3052        #     'Mismatching spaces inside () in if  [whitespace/parens] [5]')
    30373053        self.assert_multi_line_lint(
    30383054            'if (condition) {\n'
     
    30483064            'if (condition) doSomething(); else doSomethingElse();\n',
    30493065            ['More than one command on the same line  [whitespace/newline] [4]',
    3050              'Else clause should never be on same line as else (use 2 lines)  [whitespace/newline] [4]'])
    3051         # FIXME: Make this fail.
    3052         # self.assert_multi_line_lint(
    3053         #     'if (condition) doSomething(); else {\n'
    3054         #     '    doSomethingElse();\n'
    3055         #     '}\n',
    3056         #     '')
     3066             'Else clause should never be on same line as else (use 2 lines)  [whitespace/newline] [4]',
     3067             'More than one command on the same line in if  [whitespace/parens] [4]'])
     3068        self.assert_multi_line_lint(
     3069            'if (condition) doSomething(); else {\n'
     3070            '    doSomethingElse();\n'
     3071            '}\n',
     3072            ['More than one command on the same line in if  [whitespace/parens] [4]',
     3073             'One line control clauses should not use braces.  [whitespace/braces] [4]'])
    30573074
    30583075        # 3. An else if statement should be written as an if statement
Note: See TracChangeset for help on using the changeset viewer.