Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ A bunch of long-overdue modernizations of the codebase!
* Python 2 is no longer supported. Python 3.7 and 3.12 support was added, courtesy of @jayvdb
* As a result of all this, setup.py's lint subcommand was removed. Please run the commands directly instead.
* You can now specify blocks of code that exclude linting with NOLINTBEGIN and NOLINTEND, courtesy of @n3world (https://github.com/cpplint/cpplint/pull/213)
* NOLINT and NOLINTNEXTLINE comments now support a comma-separated list of categories, courtesy of @n3world (https://github.com/cpplint/cpplint/pull/220)
* The `--filter` option can now be only applied to a specific file or even a specific line through utilizing colons, e.g. `-filter=-whitespace:foo.h,+whitespace/braces:foo.h:418`. Courtesy of @PhilLab (https://github.com/cpplint/cpplint/pull/171)
* NOLINT and NOLINTNEXTLINE comments now support a comma-separated list of categories, courtesy of @n3world (https://github.com/cpplint/cpplint/pull/220)
* NOLINT and NOLINTNEXTLINE will now ignore categories known to be from clang-tidy thanks to @xatier (https://github.com/cpplint/cpplint/pull/231)
* Fix behavior with nested source repositories by @groegeorg (https://github.com/cpplint/cpplint/pull/78)
* build/include-what-you-use no longer supports transitive headers from the header for the current module for parity with the style guide by @aaronliu0130
* build/include-what-you-use now supports a plethora of new functions, courtesy of @geoffviola (https://github.com/cpplint/cpplint/pull/94)
* Indented functions inside namespaces will now be correctly erred on, courtesy of @Yujinmon (https://github.com/cpplint/cpplint/pull/235)
* C++20 headers will no longer be flagged as C headers thanks to @miker2 (https://github.com/cpplint/cpplint/pull/216)
* Same goes for C++23 and C23 headers, thanks to @aaronliu0130 (https://github.com/cpplint/cpplint/pull/239)
* "complex.h" will be treated as the C99 header instead of the legacy C++ header by @tkruse (https://github.com/cpplint/cpplint/pull/219)
Expand All @@ -25,7 +26,7 @@ A bunch of long-overdue modernizations of the codebase!
* We will no longer bother you if you mark a no-arg constructor as explicit thanks to @markww (https://github.com/cpplint/cpplint/pull/227)
* In the same PR, @aaronliu0130 also decreased the verbosity of nagging to mark single-arg constructors as explicit to 4, as the styleguide includes a major exception to this rule that would be very hard to detect.
* You can now specify the name of the CPPLINT.cfg file through `--config` as long as it is in the same directory, thanks to @gedankenexperimenter (https://github.com/cpplint/cpplint/pull/198)
* The new `__VA_OPT__(,)` will now be recognized by the Whitespace linter as a function, courtesy of @elrinor (https://github.com/cpplint/cpplint/pull/237)
* The new __VA_OPT__(,) will now be recognized by the Whitespace linter as a function thanks to @elrinor (https://github.com/cpplint/cpplint/pull/237)
* The check for including a source file's header file will now scan all files with the same base name. Thanks to @crogre for figuring out what code needed to be changed and @aaronliu0130 for fixing it (https://github.com/cpplint/cpplint/pull/104)
* Usages of the deprecated sre_compile were refectored by @jspricke (https://github.com/cpplint/cpplint/pull/214)
* Usages of deprecated unittest aliases were refactored by @tirkarthi (https://github.com/cpplint/cpplint/pull/182), @aaronliu0130 and @jayvdb
Expand Down
26 changes: 15 additions & 11 deletions cpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,6 @@
'runtime/invalid_increment',
'runtime/member_string_references',
'runtime/memset',
'runtime/indentation_namespace',
'runtime/operator',
'runtime/printf',
'runtime/printf_format',
Expand All @@ -354,6 +353,7 @@
'whitespace/ending_newline',
'whitespace/forcolon',
'whitespace/indent',
'whitespace/indent_namespace',
'whitespace/line_length',
'whitespace/newline',
'whitespace/operators',
Expand Down Expand Up @@ -3638,10 +3638,10 @@ def IsBlankLine(line):
def CheckForNamespaceIndentation(filename, nesting_state, clean_lines, line,
error):
is_namespace_indent_item = (
len(nesting_state.stack) > 1 and
nesting_state.stack[-1].check_namespace_indentation and
isinstance(nesting_state.previous_stack_top, _NamespaceInfo) and
nesting_state.previous_stack_top == nesting_state.stack[-2])
len(nesting_state.stack) >= 1 and
(isinstance(nesting_state.stack[-1], _NamespaceInfo) or
(isinstance(nesting_state.previous_stack_top, _NamespaceInfo)))
)

if ShouldCheckNamespaceIndentation(nesting_state, is_namespace_indent_item,
clean_lines.elided, line):
Expand Down Expand Up @@ -6370,10 +6370,14 @@ def IsBlockInNameSpace(nesting_state, is_forward_declaration):
return len(nesting_state.stack) >= 1 and (
isinstance(nesting_state.stack[-1], _NamespaceInfo))


return (len(nesting_state.stack) > 1 and
nesting_state.stack[-1].check_namespace_indentation and
isinstance(nesting_state.stack[-2], _NamespaceInfo))
if len(nesting_state.stack) >= 1:
if isinstance(nesting_state.stack[-1], _NamespaceInfo):
return True
elif (len(nesting_state.stack) > 1 and
isinstance(nesting_state.previous_stack_top, _NamespaceInfo) and
isinstance(nesting_state.stack[-2], _NamespaceInfo)):
return True
return False


def ShouldCheckNamespaceIndentation(nesting_state, is_namespace_indent_item,
Expand Down Expand Up @@ -6413,8 +6417,8 @@ def CheckItemIndentationInNamespace(filename, raw_lines_no_comments, linenum,
error):
line = raw_lines_no_comments[linenum]
if re.match(r'^\s+', line):
error(filename, linenum, 'runtime/indentation_namespace', 4,
'Do not indent within a namespace')
error(filename, linenum, 'whitespace/indent_namespace', 4,
'Do not indent within a namespace.')


def ProcessLine(filename, file_extension, clean_lines, line,
Expand Down
29 changes: 13 additions & 16 deletions cpplint_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,48 +283,45 @@ def GetNamespaceResults(self, lines):

return error_collector.Results()

def testForwardDeclarationNameSpaceIndentation(self):
def testForwardDeclarationNamespaceIndentation(self):
lines = ['namespace Test {',
' class ForwardDeclaration;',
'} // namespace Test']

results = self.GetNamespaceResults(lines)
self.assertEqual(results, 'Do not indent within a namespace '
' [runtime/indentation_namespace] [4]')
self.assertEqual(results, 'Do not indent within a namespace. '
' [whitespace/indent_namespace] [4]')

def testNameSpaceIndentationForClass(self):
def testNamespaceIndentationForClass(self):
lines = ['namespace Test {',
'void foo() { }',
' class Test {',
' };',
'} // namespace Test']

results = self.GetNamespaceResults(lines)
self.assertEqual(results, 'Do not indent within a namespace '
' [runtime/indentation_namespace] [4]')
self.assertEqual(results, ['Do not indent within a namespace. '
' [whitespace/indent_namespace] [4]',
'Do not indent within a namespace. '
' [whitespace/indent_namespace] [4]'])

def testNameSpaceIndentationNoError(self):
def testNamespaceIndentationNoError(self):
lines = ['namespace Test {',
'void foo() { }',
'} // namespace Test']

results = self.GetNamespaceResults(lines)
self.assertEqual(results, '')

def testWhitespaceBeforeNamespace(self):
lines = [' namespace Test {',
' void foo() { }',
' } // namespace Test']

results = self.GetNamespaceResults(lines)
self.assertEqual(results, '')

def testFalsePositivesNoError(self):
def testNestingInNamespace(self):
lines = ['namespace Test {',
'struct OuterClass {',
' struct NoFalsePositivesHere;',
' struct NoFalsePositivesHere member_variable;',
'};',
'void foo() {',
' const int no_positives_eh = 418;',
'}',
'} // namespace Test']

results = self.GetNamespaceResults(lines)
Expand Down
23 changes: 21 additions & 2 deletions samples/boost-sample/exclude.def
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
4
Done processing src/inspect/unnamed_namespace_check.hpp
Done processing src/tr1/c_policy.hpp
Total errors found: 107
Total errors found: 126

src/inspect/unnamed_namespace_check.hpp:0: No #ifndef header guard found, suggested CPP variable is: SAMPLES_BOOST_SAMPLE_SRC_INSPECT_UNNAMED_NAMESPACE_CHECK_HPP_ [build/header_guard] [5]
src/inspect/unnamed_namespace_check.hpp:11: Include the directory when naming header files [build/include_subdir] [4]
src/inspect/unnamed_namespace_check.hpp:14: Do not use unnamed namespaces in header files. See https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces for more information. [build/namespaces_headers] [4]
src/inspect/unnamed_namespace_check.hpp:17: Do not indent within a namespace. [whitespace/indent_namespace] [4]
src/inspect/unnamed_namespace_check.hpp:18: Do not indent within a namespace. [whitespace/indent_namespace] [4]
src/inspect/unnamed_namespace_check.hpp:18: At least two spaces is best between code and comments [whitespace/comments] [2]
src/inspect/unnamed_namespace_check.hpp:19: Do not indent within a namespace. [whitespace/indent_namespace] [4]
src/inspect/unnamed_namespace_check.hpp:19: Closing ) should be moved to the previous line [whitespace/parens] [2]
src/inspect/unnamed_namespace_check.hpp:21: Anonymous namespace should be terminated with "// namespace" [readability/namespace] [5]
src/inspect/unnamed_namespace_check.hpp:21: At least two spaces is best between code and comments [whitespace/comments] [2]
src/inspect/unnamed_namespace_check.hpp:26: { should almost always be at the end of the previous line [whitespace/braces] [4]
src/inspect/unnamed_namespace_check.hpp:27: Do not indent within a namespace [runtime/indentation_namespace] [4]
src/inspect/unnamed_namespace_check.hpp:27: Do not indent within a namespace. [whitespace/indent_namespace] [4]
src/inspect/unnamed_namespace_check.hpp:28: Do not indent within a namespace. [whitespace/indent_namespace] [4]
src/inspect/unnamed_namespace_check.hpp:28: { should almost always be at the end of the previous line [whitespace/braces] [4]
src/inspect/unnamed_namespace_check.hpp:29: Do not indent within a namespace. [whitespace/indent_namespace] [4]
src/inspect/unnamed_namespace_check.hpp:29: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
src/inspect/unnamed_namespace_check.hpp:30: Do not indent within a namespace. [whitespace/indent_namespace] [4]
src/inspect/unnamed_namespace_check.hpp:30: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
src/inspect/unnamed_namespace_check.hpp:30: { should almost always be at the end of the previous line [whitespace/braces] [4]
src/inspect/unnamed_namespace_check.hpp:31: Extra space after ( in function call [whitespace/parens] [4]
Expand All @@ -34,15 +40,23 @@ src/inspect/unnamed_namespace_check.hpp:36: Extra space after ( in function cal
src/inspect/unnamed_namespace_check.hpp:36: Extra space before ) [whitespace/parens] [2]
src/inspect/unnamed_namespace_check.hpp:37: Extra space after ( in function call [whitespace/parens] [4]
src/inspect/unnamed_namespace_check.hpp:37: Extra space before ) [whitespace/parens] [2]
src/inspect/unnamed_namespace_check.hpp:38: Do not indent within a namespace. [whitespace/indent_namespace] [4]
src/inspect/unnamed_namespace_check.hpp:38: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
src/inspect/unnamed_namespace_check.hpp:40: Do not indent within a namespace. [whitespace/indent_namespace] [4]
src/inspect/unnamed_namespace_check.hpp:40: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
src/inspect/unnamed_namespace_check.hpp:41: Do not indent within a namespace. [whitespace/indent_namespace] [4]
src/inspect/unnamed_namespace_check.hpp:42: Do not indent within a namespace. [whitespace/indent_namespace] [4]
src/inspect/unnamed_namespace_check.hpp:43: Do not indent within a namespace. [whitespace/indent_namespace] [4]
src/inspect/unnamed_namespace_check.hpp:44: Do not indent within a namespace. [whitespace/indent_namespace] [4]
src/inspect/unnamed_namespace_check.hpp:44: { should almost always be at the end of the previous line [whitespace/braces] [4]
src/inspect/unnamed_namespace_check.hpp:48: Lines should be <= 80 characters long [whitespace/line_length] [2]
src/inspect/unnamed_namespace_check.hpp:49: Missing space before ( in for( [whitespace/parens] [5]
src/inspect/unnamed_namespace_check.hpp:50: { should almost always be at the end of the previous line [whitespace/braces] [4]
src/inspect/unnamed_namespace_check.hpp:54: Extra space after ( in function call [whitespace/parens] [4]
src/inspect/unnamed_namespace_check.hpp:54: Extra space before ) [whitespace/parens] [2]
src/inspect/unnamed_namespace_check.hpp:57: Redundant blank line at the end of a code block should be deleted. [whitespace/blank_line] [3]
src/inspect/unnamed_namespace_check.hpp:58: Do not indent within a namespace. [whitespace/indent_namespace] [4]
src/inspect/unnamed_namespace_check.hpp:59: Do not indent within a namespace. [whitespace/indent_namespace] [4]
src/inspect/unnamed_namespace_check.hpp:59: At least two spaces is best between code and comments [whitespace/comments] [2]
src/inspect/unnamed_namespace_check.hpp:60: At least two spaces is best between code and comments [whitespace/comments] [2]
src/inspect/unnamed_namespace_check.hpp:51: Add #include <string> for string [build/include_what_you_use] [4]
Expand Down Expand Up @@ -111,5 +125,10 @@ src/tr1/c_policy.hpp:109: Namespace should be terminated with "// namespace mat
src/tr1/c_policy.hpp:109: Namespace should be terminated with "// namespace boost" [readability/namespace] [5]
src/tr1/c_policy.hpp:109: At least two spaces is best between code and comments [whitespace/comments] [2]
src/tr1/c_policy.hpp:111: Missing space before { [whitespace/braces] [5]
src/tr1/c_policy.hpp:122: Do not indent within a namespace. [whitespace/indent_namespace] [4]
src/tr1/c_policy.hpp:123: Do not indent within a namespace. [whitespace/indent_namespace] [4]
src/tr1/c_policy.hpp:124: Do not indent within a namespace. [whitespace/indent_namespace] [4]
src/tr1/c_policy.hpp:125: Do not indent within a namespace. [whitespace/indent_namespace] [4]
src/tr1/c_policy.hpp:126: Do not indent within a namespace. [whitespace/indent_namespace] [4]
src/tr1/c_policy.hpp:131: Namespace should be terminated with "// namespace c_policies" [readability/namespace] [5]

Loading