From 74397abed84101f8af5228586f9b23f681ad65c3 Mon Sep 17 00:00:00 2001 From: Aaron Liu Date: Sat, 1 Feb 2025 16:07:45 -0500 Subject: [PATCH 1/4] suppress C++-only categories on C file extensions ".h" files don't, because many C++ headers also use this extension --- CHANGELOG.rst | 3 ++- cpplint.py | 11 ++++------- cpplint_unittest.py | 10 +++++++++- samples/vlc-sample/simple.def | 4 +--- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index bd67068..6c0d9b8 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -7,7 +7,8 @@ Changelog Yet another overdue... hotfix. Sorry this took so long. -* The false positive for indented function parameters in namespaces was eradicated. +* The false positive for indented function parameters in namespaces was eradicated. (https://github.com/cpplint/cpplint/pull/304) +* Files that end in ".c", ".C", or ".cu" will now also automatically suppress C++-only categories. Previously, `// NO_LINT_C` was required. (https://github.com/cpplint/cpplint/pull/308) 2.0 (2024-10-06) ================ diff --git a/cpplint.py b/cpplint.py index 7e8470a..494d94e 100755 --- a/cpplint.py +++ b/cpplint.py @@ -1111,21 +1111,18 @@ def ProcessCategory(category): error(filename, linenum, 'readability/nolint', 5, f'Unknown NOLINT error category: {category}') -def ProcessGlobalSuppresions(lines): - """Deprecated; use ProcessGlobalSuppressions.""" - ProcessGlobalSuppressions(lines) - -def ProcessGlobalSuppressions(lines): +def ProcessGlobalSuppressions(filename, lines): """Updates the list of global error suppressions. Parses any lint directives in the file that have global effect. Args: + filename: str, the name of the input file. lines: An array of strings, each representing a line of the file, with the last element being empty if the file is terminated with a newline. """ for line in lines: - if _SEARCH_C_FILE.search(line): + if _SEARCH_C_FILE.search(line) or filename.endswith(('.c', '.cu', '.C')): for category in _DEFAULT_C_SUPPRESSED_CATEGORIES: _error_suppressions.AddGlobalSuppression(category) if _SEARCH_KERNEL_FILE.search(line): @@ -6522,7 +6519,7 @@ def ProcessFileData(filename, file_extension, lines, error, ResetNolintSuppressions() CheckForCopyright(filename, lines, error) - ProcessGlobalSuppressions(lines) + ProcessGlobalSuppressions(filename, lines) RemoveMultiLineComments(filename, lines, error) clean_lines = CleansedLines(lines) diff --git a/cpplint_unittest.py b/cpplint_unittest.py index d5f126d..7a8f9f5 100755 --- a/cpplint_unittest.py +++ b/cpplint_unittest.py @@ -477,10 +477,18 @@ def testErrorSuppression(self): self.TestLint( 'long a = (int64_t) 65; // NOLINT(runtime/int,readability/casting)', '') - # All categories suppressed: (two aliases) self.TestLint('long a = (int64_t) 65; // NOLINT', '') self.TestLint('long a = (int64_t) 65; // NOLINT(*)', '') + # Linting a C file + error_collector = ErrorCollector(self.assertTrue) + cpplint.ProcessFileData('test.c', 'c', + ['// Copyright 2014 Your Majesty.', + 'int64_t a = (int64_t) 65;', + ''], + error_collector) + self.assertEqual('', error_collector.Results()) + # Malformed NOLINT directive: self.TestLint( 'long a = 65; // NOLINT(foo)', diff --git a/samples/vlc-sample/simple.def b/samples/vlc-sample/simple.def index 19c9563..e3e415e 100644 --- a/samples/vlc-sample/simple.def +++ b/samples/vlc-sample/simple.def @@ -4,7 +4,7 @@ src/* Done processing src/libvlc.c Done processing src/libvlc.h Done processing src/missing.c -Total errors found: 602 +Total errors found: 600 src/libvlc.c:41: Found C system header after other header. Should be: libvlc.h, c system, c++ system, other. [build/include_order] [4] src/libvlc.c:47: Found C system header after other header. Should be: libvlc.h, c system, c++ system, other. [build/include_order] [4] @@ -19,7 +19,6 @@ src/libvlc.c:86: Extra space before ( in function call [whitespace/parens] [4] src/libvlc.c:86: Extra space before ) [whitespace/parens] [2] src/libvlc.c:92: Extra space after ( in function call [whitespace/parens] [4] src/libvlc.c:93: { should almost always be at the end of the previous line [whitespace/braces] [4] -src/libvlc.c:98: Using C-style cast. Use reinterpret_cast(...) instead [readability/casting] [4] src/libvlc.c:99: Extra space before ) [whitespace/parens] [2] src/libvlc.c:100: Missing space before ( in if( [whitespace/parens] [5] src/libvlc.c:103: Extra space before ( in function call [whitespace/parens] [4] @@ -74,7 +73,6 @@ src/libvlc.c:219: Extra space before ) [whitespace/parens] [2] src/libvlc.c:220: Missing space before ( in if( [whitespace/parens] [5] src/libvlc.c:221: { should almost always be at the end of the previous line [whitespace/braces] [4] src/libvlc.c:222: Extra space after ( in function call [whitespace/parens] [4] -src/libvlc.c:222: Using C-style cast. Use static_cast(...) instead [readability/casting] [4] src/libvlc.c:223: Extra space after ( in function call [whitespace/parens] [4] src/libvlc.c:223: Extra space before ) [whitespace/parens] [2] src/libvlc.c:224: Extra space after ( in function call [whitespace/parens] [4] From 2ecc6a400a750a5c923d0401705a6f700de03d55 Mon Sep 17 00:00:00 2001 From: Aaron Liu Date: Tue, 4 Mar 2025 18:44:46 -0500 Subject: [PATCH 2/4] case-insensitive extension matching Co-authored-by: Christian Clauss --- cpplint.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpplint.py b/cpplint.py index 494d94e..8bab755 100755 --- a/cpplint.py +++ b/cpplint.py @@ -1122,7 +1122,7 @@ def ProcessGlobalSuppressions(filename, lines): last element being empty if the file is terminated with a newline. """ for line in lines: - if _SEARCH_C_FILE.search(line) or filename.endswith(('.c', '.cu', '.C')): + if _SEARCH_C_FILE.search(line) or filename.lower().endswith(('.c', '.cu')): for category in _DEFAULT_C_SUPPRESSED_CATEGORIES: _error_suppressions.AddGlobalSuppression(category) if _SEARCH_KERNEL_FILE.search(line): From 4cc5ec6ef0f10789134f9a661a77ef1d416cdd7c Mon Sep 17 00:00:00 2001 From: Aaron Liu Date: Wed, 5 Mar 2025 06:58:37 -0500 Subject: [PATCH 3/4] Type hints Co-authored-by: Christian Clauss --- cpplint.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpplint.py b/cpplint.py index 8bab755..87d5f9d 100755 --- a/cpplint.py +++ b/cpplint.py @@ -1111,7 +1111,7 @@ def ProcessCategory(category): error(filename, linenum, 'readability/nolint', 5, f'Unknown NOLINT error category: {category}') -def ProcessGlobalSuppressions(filename, lines): +def ProcessGlobalSuppressions(filename: str, lines: list[str]) -> None: """Updates the list of global error suppressions. Parses any lint directives in the file that have global effect. From eaf75efc2ac7f4212caccce9db5e5be26519f8f9 Mon Sep 17 00:00:00 2001 From: Aaron Liu Date: Mon, 24 Mar 2025 17:08:26 -0400 Subject: [PATCH 4/4] misc unchanges, fix mypy weirdness --- cpplint.py | 12 +++++------- cpplint_unittest.py | 2 ++ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cpplint.py b/cpplint.py index d3727ac..2670796 100755 --- a/cpplint.py +++ b/cpplint.py @@ -934,10 +934,6 @@ "Missing space after ,": r"s/,\([^ ]\)/, \1/g", } -# {str, set(int)}: a map from error categories to sets of linenumbers -# on which those errors are expected and should be suppressed. -_error_suppressions: dict[str, set[int]] = {} - # The root directory used for deriving header guard CPP variable. # This is set by --root flag. _root = None @@ -1036,7 +1032,9 @@ def Clear(self): self._open_block_suppression = None -_error_suppressions = ErrorSuppressions() # type: ignore[assignment] +# {str, set(int)}: a map from error categories to sets of linenumbers +# on which those errors are expected and should be suppressed. +_error_suppressions = ErrorSuppressions() def ProcessHppHeadersOption(val): @@ -1172,9 +1170,9 @@ def ProcessGlobalSuppressions(filename: str, lines: list[str]) -> None: Parses any lint directives in the file that have global effect. Args: + lines: An array of strings, each representing a line of the file, with the + last element being empty if the file is terminated with a newline. filename: str, the name of the input file. - lines: An array of strings, each representing a line of the file, with the - last element being empty if the file is terminated with a newline. """ for line in lines: if _SEARCH_C_FILE.search(line) or filename.lower().endswith((".c", ".cu")): diff --git a/cpplint_unittest.py b/cpplint_unittest.py index 9f92f40..241c131 100755 --- a/cpplint_unittest.py +++ b/cpplint_unittest.py @@ -466,9 +466,11 @@ def testErrorSuppression(self): ) # Two categories of errors suppressed: self.TestLint("long a = (int64_t) 65; // NOLINT(runtime/int,readability/casting)", "") + # All categories suppressed: (two aliases) self.TestLint("long a = (int64_t) 65; // NOLINT", "") self.TestLint("long a = (int64_t) 65; // NOLINT(*)", "") + # Linting a C file error_collector = ErrorCollector(self.assertTrue) cpplint.ProcessFileData(