From dc0e2f067f49dcb2afb6cc740702a0e2757a76d4 Mon Sep 17 00:00:00 2001 From: Kevin Powell Date: Thu, 6 Oct 2016 14:05:18 -0400 Subject: [PATCH] [Fix] Improved Indent testing Improved indent testing to test for more than 4 leading spaces as well as improved continuation line testing/exceptions. Added Unit test for valid Boolean Comparators that we consider to be valid line continuations. --- cpplint/cpplint.py | 31 +++++++-- cpplint/cpplint_unittest.py | 127 ++++++++++++++++++++++++++++++++++-- 2 files changed, 148 insertions(+), 10 deletions(-) diff --git a/cpplint/cpplint.py b/cpplint/cpplint.py index 3366b39..60d7226 100755 --- a/cpplint/cpplint.py +++ b/cpplint/cpplint.py @@ -4275,22 +4275,43 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state, scope_or_label_pattern = r'\s*\w+\s*:\s*\\?$' classinfo = nesting_state.InnermostClass() initial_spaces = 0 + complain = 1 cleansed_line = clean_lines.elided[linenum] - while initial_spaces < len(line) and line[initial_spaces] == ' ': - initial_spaces += 1 + initial_spaces = GetIndentLevel(line) # There are certain situations we allow one space, notably for # section labels, and also lines containing multi-line raw strings. # We also don't check for lines that look like continuation lines # (of lines ending in double quotes, commas, equals, or angle brackets) # because the rules for how to indent those are non-trivial. - if (not Search(r'[",=><] *$', prev) and - (initial_spaces == 1 or initial_spaces == 3) and + # We also don't check for lines that have a number of leading spaces + # greater than 1/4 of our line length. + if (initial_spaces > (_line_length / 4)): + complain = 0 + if (Search(r' +(error|private|public|protected):', prev)): + complain = 0 + if (Search(r'&& *$', prev)) or (Search(r'^ *&&', line)): + complain = 0 + if (Search(r'\|\| *$', prev)) or (Search(r'^ *\|\|', line)): + complain = 0 + if (Search(r'[=> 0) and not Match(scope_or_label_pattern, cleansed_line) and not (clean_lines.raw_lines[linenum] != line and Match(r'^\s*""', line))): error(filename, linenum, 'whitespace/indent', 3, 'Weird number of spaces at line-start. ' - 'Are you using a 2-space indent?') if line and line[-1].isspace(): error(filename, linenum, 'whitespace/end_of_line', 4, diff --git a/cpplint/cpplint_unittest.py b/cpplint/cpplint_unittest.py index b8b8319..85e523f 100755 --- a/cpplint/cpplint_unittest.py +++ b/cpplint/cpplint_unittest.py @@ -3459,12 +3459,19 @@ class CpplintTest(CpplintTestBase): self.TestLint('static int noindent;', '') self.TestLint(' int two_space_indent;', '') self.TestLint(' int four_space_indent;', '') + self.TestLint(' int six_space_indent;', '') self.TestLint(' int one_space_indent;', 'Weird number of spaces at line-start. ' 'Are you using a 2-space indent? [whitespace/indent] [3]') self.TestLint(' int three_space_indent;', 'Weird number of spaces at line-start. ' 'Are you using a 2-space indent? [whitespace/indent] [3]') + self.TestLint(' int five_space_indent;', + 'Weird number of spaces at line-start. ' + 'Are you using a 2-space indent? [whitespace/indent] [3]') + self.TestLint(' int seven_space_indent;', + 'Weird number of spaces at line-start. ' + 'Are you using a 2-space indent? [whitespace/indent] [3]') self.TestLint(' char* one_space_indent = "public:";', 'Weird number of spaces at line-start. ' 'Are you using a 2-space indent? [whitespace/indent] [3]') @@ -3496,6 +3503,114 @@ class CpplintTest(CpplintTestBase): ' static const char kSingleLineRawString[] = R"(...)";', 'Weird number of spaces at line-start. ' 'Are you using a 2-space indent? [whitespace/indent] [3]') + self.TestMultiLineLint( + TrimExtraIndent(''' + if (foo && + bar) + goto pass;'''), + '') + self.TestMultiLineLint( + TrimExtraIndent(''' + if (foo + && bar) + goto pass;'''), + '') + self.TestMultiLineLint( + TrimExtraIndent(''' + if (foo || + bar) + goto pass;'''), + '') + self.TestMultiLineLint( + TrimExtraIndent(''' + if (foo + || bar) + goto pass;'''), + '') + self.TestMultiLineLint( + TrimExtraIndent(''' + if (foo == + bar) + goto pass;'''), + '') + self.TestMultiLineLint( + TrimExtraIndent(''' + if (foo + == bar) + goto pass;'''), + '') + self.TestMultiLineLint( + TrimExtraIndent(''' + if (foo != + bar) + goto pass;'''), + '') + self.TestMultiLineLint( + TrimExtraIndent(''' + if (foo + != bar) + goto pass;'''), + '') + self.TestMultiLineLint( + TrimExtraIndent(''' + if (foo <= + bar) + goto pass;'''), + '') + self.TestMultiLineLint( + TrimExtraIndent(''' + if (foo + <= bar) + goto pass;'''), + '') + self.TestMultiLineLint( + TrimExtraIndent(''' + if (foo >= + bar) + goto pass;'''), + '') + self.TestMultiLineLint( + TrimExtraIndent(''' + if (foo + >= bar) + goto pass;'''), + '') + self.TestMultiLineLint( + TrimExtraIndent(''' + if (foo < + bar) + goto pass;'''), + '') + self.TestMultiLineLint( + TrimExtraIndent(''' + if (foo + < bar) + goto pass;'''), + '') + self.TestMultiLineLint( + TrimExtraIndent(''' + if (foo > + bar) + goto pass;'''), + '') + self.TestMultiLineLint( + TrimExtraIndent(''' + if (foo + > bar) + goto pass;'''), + '') + self.TestMultiLineLint( + TrimExtraIndent(''' + if (foo && ! + bar) + goto pass;'''), + '') + self.TestMultiLineLint( + TrimExtraIndent(''' + if (foo && + !(bar)) + goto pass;'''), + '') def testSectionIndent(self): self.TestMultiLineLint( @@ -3520,12 +3635,12 @@ class CpplintTest(CpplintTestBase): self.TestMultiLineLint( """ struct D { - };""", + };""", 'Closing brace should be aligned with beginning of struct D' ' [whitespace/indent] [3]') self.TestMultiLineLint( """ - template class F { + template class F { };""", 'Closing brace should be aligned with beginning of class F' ' [whitespace/indent] [3]') @@ -3960,8 +4075,9 @@ class CpplintTest(CpplintTestBase): 'class Foo;', '') self.TestMultiLineLint( - """struct Foo* - foo = NewFoo();""", + """ + struct Foo* + foo = NewFoo();""", '') # Test preprocessor. self.TestMultiLineLint( @@ -3992,7 +4108,8 @@ class CpplintTest(CpplintTestBase): # The crosstool compiler we currently use will fail to compile the # code in this test, so we might consider removing the lint check. self.TestMultiLineLint( - """#if 0 + """ + #if 0 #endif Not a comment""", 'Uncommented text after #endif is non-standard. Use a comment.' ' [build/endif_comment] [5]')