Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for disabling cppclean for a file or a single line #162

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ Multiple include paths can be specified::

$ cppclean --include-path=directory1 --include-path=directory2 <path>

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
==============
Expand Down
20 changes: 20 additions & 0 deletions cpp/comments.py
Original file line number Diff line number Diff line change
@@ -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])
)
17 changes: 8 additions & 9 deletions cpp/find_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,6 +29,7 @@
from . import symbols
from . import tokenize
from . import utils
from . import comments


try:
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 4 additions & 0 deletions test/disable-line.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "bar.h"
// cppclean-disable-next-line
#include "bar.h"
#include "bar.h"
3 changes: 3 additions & 0 deletions test/disable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// cppclean-disable
#include "bar.h"
#include "bar.h"
2 changes: 2 additions & 0 deletions test/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down