From 1ad22278e6e505b1b6fcfa59521e53ecc8a63a7e Mon Sep 17 00:00:00 2001 From: Joel Lefkowitz Date: Sat, 30 Apr 2022 14:13:33 +0100 Subject: [PATCH 1/2] Add support for cppclean-disable and cppclean-disable-next-line --- cpp/comments.py | 20 ++++++++++++++++++++ cpp/find_warnings.py | 17 ++++++++--------- test/disable-line.h | 4 ++++ test/disable.h | 3 +++ test/expected.txt | 2 ++ 5 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 cpp/comments.py create mode 100644 test/disable-line.h create mode 100644 test/disable.h diff --git a/cpp/comments.py b/cpp/comments.py new file mode 100644 index 0000000..82578f1 --- /dev/null +++ b/cpp/comments.py @@ -0,0 +1,20 @@ +import re +from . import utils + + +def line_comment_regex(comment): + return re.compile(rf"^\s*//\s*{comment}\s*$") + + +def file_disabled(filename): + lines = utils.read_file(filename).split("\n") + return len(lines) >= 1 and line_comment_regex("cppclean-disable").match(lines[0]) + + +def line_disabled(filename, line_number): + lines = utils.read_file(filename).split("\n") + return ( + line_number >= 2 + and line_number <= len(lines) + 2 + and line_comment_regex("cppclean-disable-next-line").match(lines[line_number - 2]) + ) diff --git a/cpp/find_warnings.py b/cpp/find_warnings.py index 35e05b9..25ba6e3 100644 --- a/cpp/find_warnings.py +++ b/cpp/find_warnings.py @@ -13,14 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Find warnings for C++ code. - -TODO(nnorwitz): provide a mechanism to configure which warnings should -be generated and which should be suppressed. Currently, all possible -warnings will always be displayed. There is no way to suppress any. -There also needs to be a way to use annotations in the source code to -suppress warnings. -""" +"""Find warnings for C++ code.""" from __future__ import absolute_import from __future__ import print_function @@ -36,6 +29,7 @@ from . import symbols from . import tokenize from . import utils +from . import comments try: @@ -113,7 +107,12 @@ def _add_warning(self, msg, node, filename=None): filename = self.filename src_metrics = self.metrics line_number = get_line_number(src_metrics, node) - self.warnings.add((filename, line_number, msg)) + + if not ( + comments.file_disabled(filename) + or comments.line_disabled(filename, line_number) + ): + self.warnings.add((filename, line_number, msg)) def show_warnings(self): for filename, line_num, msg in sorted(self.warnings): diff --git a/test/disable-line.h b/test/disable-line.h new file mode 100644 index 0000000..37ac98e --- /dev/null +++ b/test/disable-line.h @@ -0,0 +1,4 @@ +#include "bar.h" +// cppclean-disable-next-line +#include "bar.h" +#include "bar.h" diff --git a/test/disable.h b/test/disable.h new file mode 100644 index 0000000..820aed8 --- /dev/null +++ b/test/disable.h @@ -0,0 +1,3 @@ +// cppclean-disable +#include "bar.h" +#include "bar.h" diff --git a/test/expected.txt b/test/expected.txt index 525f5dd..4835c5f 100644 --- a/test/expected.txt +++ b/test/expected.txt @@ -7,6 +7,8 @@ test/define/d1.cc:15: expected to find 'SomeOtherFunction1' in 'test/define/d1.h test/define/d1.h:4: 'OnlyDeclared' declared but not defined test/define/d2.cc:2: 'SomeFunction' not found in any directly #included header test/define/d7.h:4 'Bar' has virtual methods without a virtual dtor +test/disable-line.h:4: 'bar.h' already #included on line 1 +test/disable-line.h:4: 'bar.h' does not need to be #included test/dup.h:3: 'Bar' forward declared, but already #included in 'test/bar.h' test/enum.h:14: static data 'color' test/external/foo.h:1: 'Unused' not used From c04144082d68f2c5d247a478daaee74e3914b041 Mon Sep 17 00:00:00 2001 From: Joel Lefkowitz Date: Sat, 30 Apr 2022 14:18:34 +0100 Subject: [PATCH 2/2] Add disabling checks instructions to README --- README.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.rst b/README.rst index 26ba79a..3374b4f 100644 --- a/README.rst +++ b/README.rst @@ -66,6 +66,20 @@ Multiple include paths can be specified:: $ cppclean --include-path=directory1 --include-path=directory2 +Disabling checks +================ + +To disable checking for a whole file: +:: + + // cppclean-disable + # include "foo.h" + +To disable checking for a single line: +:: + + // cppclean-disable-next-line + # include "foo.h" Current status ==============