Changeset 160084 in webkit


Ignore:
Timestamp:
Dec 4, 2013 3:11:33 AM (10 years ago)
Author:
commit-queue@webkit.org
Message:

check-webkit-style should check member initialization indentation.
https://bugs.webkit.org/show_bug.cgi?id=124820

Patch by László Langó <lango@inf.u-szeged.hu> on 2013-12-04
Reviewed by Zoltan Herczeg.

check-webkit-style should check member initialization indentation
belongs to webkit coding style:
http://www.webkit.org/coding/coding-style.html#punctuation-member-init

  • Scripts/webkitpy/style/checkers/cpp.py:

(check_member_initialization_list): Add new method to check member
initialization list.
(check_style): Add the call of the new method.
(check_language): Move self initialization checking into the new method.
(CppChecker): Add a new category for initialization list.

  • Scripts/webkitpy/style/checkers/cpp_unittest.py:

(CppStyleTest.test_runtime_selfinit):
(CppStyleTest.test_deprecated_cast):
(WebKitStyleTest.test_member_initialization_list): Add new testcases for
the new feature.

Location:
trunk/Tools
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Tools/ChangeLog

    r160083 r160084  
     12013-12-04  László Langó  <lango@inf.u-szeged.hu>
     2
     3        check-webkit-style should check member initialization indentation.
     4        https://bugs.webkit.org/show_bug.cgi?id=124820
     5
     6        Reviewed by Zoltan Herczeg.
     7
     8        check-webkit-style should check member initialization indentation
     9        belongs to webkit coding style:
     10        http://www.webkit.org/coding/coding-style.html#punctuation-member-init
     11
     12        * Scripts/webkitpy/style/checkers/cpp.py:
     13        (check_member_initialization_list): Add new method to check member
     14        initialization list.
     15        (check_style): Add the call of the new method.
     16        (check_language): Move self initialization checking into the new method.
     17        (CppChecker): Add a new category for initialization list.
     18        * Scripts/webkitpy/style/checkers/cpp_unittest.py:
     19        (CppStyleTest.test_runtime_selfinit):
     20        (CppStyleTest.test_deprecated_cast):
     21        (WebKitStyleTest.test_member_initialization_list): Add new testcases for
     22        the new feature.
     23
    1242013-12-04  Tamas Gergely  <gertom@inf.u-szeged.hu>
    225
  • trunk/Tools/Scripts/webkitpy/style/checkers/cpp.py

    r159872 r160084  
    19651965
    19661966
     1967def check_member_initialization_list(clean_lines, line_number, error):
     1968    """ Look for style errors in member initialization list of classes.
     1969
     1970    Args:
     1971      clean_lines: A CleansedLines instance containing the file.
     1972      line_number: The number of the line to check.
     1973      error: The function to call with any errors found.
     1974    """
     1975
     1976    raw = clean_lines.raw_lines
     1977    line = raw[line_number]
     1978
     1979    if search(r'\b([A-Za-z0-9_]*_)\(\1\)', line):
     1980        error(line_number, 'runtime/init', 4,
     1981              'You seem to be initializing a member variable with itself.')
     1982
     1983    # Check the style of the initializer list.
     1984    # Each member (and superclass) should be indented on a separate line,
     1985    # with the colon or comma preceding the member on that line.
     1986    begin_line = line
     1987    if search(r'(?P<indentation>\s*)([^\s]\(.*\)\s?\:|^\s*\:).*[^;]*$', line):
     1988        if search(r'[^:]\:[^\:\s]+', line):
     1989            error(line_number, 'whitespace/init', 4,
     1990                'Missing spaces around :')
     1991        if search(r'[^\s]\(.*\)\s?\:.*[^;]*$', line):
     1992            error(line_number, 'whitespace/indent', 4,
     1993                'Should be indented on a separate line, with the colon or comma first on that line.')
     1994        else:
     1995            begin_line, begin_line_number = get_previous_non_blank_line(clean_lines, line_number)
     1996
     1997        matched = search(r'(?P<indentation>\s*).*', begin_line)
     1998        indentation = matched.group('indentation')
     1999        inner_indentation = indentation + ' ' * 4
     2000
     2001        while(not search(r'{', line)):
     2002            # Don't check inheritance style
     2003            if search(r'\S\(.*\)', line):
     2004                if not line.startswith(inner_indentation) and begin_line != line:
     2005                    error(line_number, 'whitespace/indent', 4,
     2006                        'Wrong number of spaces before statement. (expected: %d)' % len(inner_indentation))
     2007                if search(r'\S\s*,', line):
     2008                    error(line_number, 'whitespace/init', 4,
     2009                        'Comma should be at the beggining of the line in a member initialization list.')
     2010
     2011            # To avoid infinite loop, if can't find the end of member initialization list
     2012            if line_number < len(raw) - 1:
     2013                line_number = line_number + 1
     2014                line = raw[line_number]
     2015            else:
     2016                break
     2017
    19672018def get_previous_non_blank_line(clean_lines, line_number):
    19682019    """Return the most recent non-blank line and its line number.
     
    26402691    check_exit_statement_simplifications(clean_lines, line_number, error)
    26412692    check_spacing(file_extension, clean_lines, line_number, error)
     2693    check_member_initialization_list(clean_lines, line_number, error)
    26422694    check_check(clean_lines, line_number, error)
    26432695    check_for_comparisons_to_zero(clean_lines, line_number, error)
     
    29472999              "hierarchy, use static_cast<> to upcast.  Google doesn't support "
    29483000              'RTTI.')
    2949 
    2950     if search(r'\b([A-Za-z0-9_]*_)\(\1\)', line):
    2951         error(line_number, 'runtime/init', 4,
    2952               'You seem to be initializing a member variable with itself.')
    29533001
    29543002    if file_extension == 'h':
     
    36623710        'whitespace/ending_newline',
    36633711        'whitespace/indent',
     3712        'whitespace/init',
    36643713        'whitespace/line_length',
    36653714        'whitespace/newline',
  • trunk/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py

    r159872 r160084  
    749749
    750750    def test_runtime_selfinit(self):
    751         self.assert_lint(
    752             'Foo::Foo(Bar r, Bel l) : r_(r_), l_(l_) { }',
     751        self.assert_multi_line_lint(
     752            '''\
     753            Foo::Foo(Bar r, Bel l)
     754                : r_(r_)
     755                , l_(l_) { }''',
     756            ['You seem to be initializing a member variable with itself.'
     757            '  [runtime/init] [4]',
    753758            'You seem to be initializing a member variable with itself.'
    754             '  [runtime/init] [4]')
    755         self.assert_lint(
    756             'Foo::Foo(Bar r, Bel l) : r_(r), l_(l) { }',
    757             '')
    758         self.assert_lint(
    759             'Foo::Foo(Bar r) : r_(r), l_(r_), ll_(l_) { }',
     759            '  [runtime/init] [4]'])
     760        self.assert_multi_line_lint(
     761            '''\
     762            Foo::Foo(Bar r, Bel l)
     763                : r_(r)
     764                , l_(l) { }''',
     765            '')
     766        self.assert_multi_line_lint(
     767            '''\
     768            Foo::Foo(Bar r)
     769                : r_(r)
     770                , l_(r_)
     771                , ll_(l_) { }''',
    760772            '')
    761773
     
    819831            'int a = int(); // Constructor, o.k.',
    820832            '')
    821         self.assert_lint(
    822             'X::X() : a(int()) { } // default Constructor, o.k.',
     833        self.assert_multi_line_lint(
     834            '''\
     835            X::X()
     836                : a(int()) { } // default Constructor, o.k.''',
    823837            '')
    824838        self.assert_lint(
     
    48984912                webkit_export_error_rules))
    48994913
     4914    def test_member_initialization_list(self):
     4915        self.assert_lint('MyClass::MyClass(Document* doc) : MySuperClass() { }',
     4916        'Should be indented on a separate line, with the colon or comma first on that line.'
     4917        '  [whitespace/indent] [4]')
     4918        self.assert_multi_line_lint('''\
     4919        MyClass::MyClass(Document* doc) : MySuperClass()
     4920        { }''',
     4921        'Should be indented on a separate line, with the colon or comma first on that line.'
     4922        '  [whitespace/indent] [4]')
     4923        self.assert_multi_line_lint('''\
     4924        MyClass::MyClass(Document* doc)
     4925        : MySuperClass()
     4926        { }''',
     4927        'Wrong number of spaces before statement. (expected: 12)'
     4928        '  [whitespace/indent] [4]')
     4929        self.assert_multi_line_lint('''\
     4930        MyClass::MyClass(Document* doc) :
     4931            MySuperClass(),
     4932            m_doc(0)
     4933        { }''',
     4934        ['Should be indented on a separate line, with the colon or comma first on that line.'
     4935         '  [whitespace/indent] [4]',
     4936         'Comma should be at the beggining of the line in a member initialization list.'
     4937         '  [whitespace/init] [4]'])
     4938        self.assert_multi_line_lint('''\
     4939        MyClass::MyClass(Document* doc) :MySuperClass()
     4940        { }''',
     4941        ['Missing spaces around :  [whitespace/init] [4]',
     4942         'Should be indented on a separate line, with the colon or comma first on that line.'
     4943         '  [whitespace/indent] [4]'])
     4944        self.assert_multi_line_lint('''\
     4945        MyClass::MyClass(Document* doc):MySuperClass()
     4946        { }''',
     4947        ['Missing spaces around :  [whitespace/init] [4]',
     4948         'Should be indented on a separate line, with the colon or comma first on that line.'
     4949         '  [whitespace/indent] [4]'])
     4950        self.assert_multi_line_lint('''\
     4951        MyClass::MyClass(Document* doc) : MySuperClass()
     4952        ,MySuperClass()
     4953        , m_doc(0)
     4954            , m_myMember(0)
     4955        { }''',
     4956        ['Should be indented on a separate line, with the colon or comma first on that line.'
     4957         '  [whitespace/indent] [4]',
     4958         'Wrong number of spaces before statement. (expected: 12)'
     4959         '  [whitespace/indent] [4]',
     4960         'Wrong number of spaces before statement. (expected: 12)'
     4961         '  [whitespace/indent] [4]',
     4962         'Missing space after ,  [whitespace/comma] [3]'])
     4963        self.assert_multi_line_lint('''\
     4964        MyClass::MyClass(Document* doc)
     4965            :MySuperClass()
     4966        { }''',
     4967        'Missing spaces around :  [whitespace/init] [4]')
     4968        self.assert_multi_line_lint('''\
     4969        MyClass::MyClass(Document* doc)
     4970            : MySuperClass() , m_doc(0)
     4971        { }''',
     4972        'Comma should be at the beggining of the line in a member initialization list.'
     4973        '  [whitespace/init] [4]')
     4974        self.assert_multi_line_lint('''\
     4975        class MyClass : public Goo {
     4976        };''',
     4977        '')
     4978        self.assert_multi_line_lint('''\
     4979        class MyClass
     4980        : public Goo
     4981        , public foo {
     4982        };''',
     4983        '')
     4984
    49004985    def test_other(self):
    49014986        # FIXME: Implement this.
Note: See TracChangeset for help on using the changeset viewer.