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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ using namespace clang::ast_matchers;

namespace clang::tidy::cppcoreguidelines {

ProBoundsPointerArithmeticCheck::ProBoundsPointerArithmeticCheck(
StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
AllowIncrementDecrementOperators(
Options.get("AllowIncrementDecrementOperators", false)) {}

void ProBoundsPointerArithmeticCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "AllowIncrementDecrementOperators",
AllowIncrementDecrementOperators);
}

void ProBoundsPointerArithmeticCheck::registerMatchers(MatchFinder *Finder) {
const auto AllPointerTypes =
anyOf(hasType(hasUnqualifiedDesugaredType(pointerType())),
Expand All @@ -30,13 +42,14 @@ void ProBoundsPointerArithmeticCheck::registerMatchers(MatchFinder *Finder) {
this);

// Flag all operators ++, -- that result in a pointer
Finder->addMatcher(
unaryOperator(hasAnyOperatorName("++", "--"),
hasType(hasUnqualifiedDesugaredType(pointerType())),
unless(hasUnaryOperand(
ignoringImpCasts(declRefExpr(to(isImplicit()))))))
.bind("expr"),
this);
if (!AllowIncrementDecrementOperators)
Finder->addMatcher(
unaryOperator(hasAnyOperatorName("++", "--"),
hasType(hasUnqualifiedDesugaredType(pointerType())),
unless(hasUnaryOperand(
ignoringImpCasts(declRefExpr(to(isImplicit()))))))
.bind("expr"),
this);

// Array subscript on a pointer (not an array) is also pointer arithmetic
Finder->addMatcher(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ namespace clang::tidy::cppcoreguidelines {
/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/pro-bounds-pointer-arithmetic.html
class ProBoundsPointerArithmeticCheck : public ClangTidyCheck {
public:
ProBoundsPointerArithmeticCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}
ProBoundsPointerArithmeticCheck(StringRef Name, ClangTidyContext *Context);
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
return LangOpts.CPlusPlus;
}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;

private:
const bool AllowIncrementDecrementOperators;
};

} // namespace clang::tidy::cppcoreguidelines
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ Changes in existing checks
<clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>` check to
avoid false positives on inherited members in class templates.

- Improved :doc:`cppcoreguidelines-pro-bounds-pointer-arithmetic
<clang-tidy/checks/cppcoreguidelines/pro-bounds-pointer-arithmetic>` check
adding an option to allow pointer arithmetic via prefix/postfix increment or
decrement operators.

- Improved :doc:`misc-header-include-cycle
<clang-tidy/checks/misc/header-include-cycle>` check performance.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ arrays of data.
This rule is part of the `Bounds safety (Bounds 1)
<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Pro-bounds-arithmetic>`_
profile from the C++ Core Guidelines.

Options
-------

.. option:: AllowIncrementDecrementOperators

When enabled, the check will allow using the prefix/postfix increment or
decrement operators on pointers. Default is ``false``.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// RUN: %check_clang_tidy %s cppcoreguidelines-pro-bounds-pointer-arithmetic %t
// RUN: %check_clang_tidy %s cppcoreguidelines-pro-bounds-pointer-arithmetic -check-suffixes=,DEFAULT %t
// RUN: %check_clang_tidy %s cppcoreguidelines-pro-bounds-pointer-arithmetic %t -- \
// RUN: -config="{CheckOptions: {cppcoreguidelines-pro-bounds-pointer-arithmetic.AllowIncrementDecrementOperators: true}}" --

enum E {
ENUM_LITERAL = 1
Expand Down Expand Up @@ -42,22 +44,22 @@ void fail() {
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic

p++;
// CHECK-MESSAGES: :[[@LINE-1]]:4: warning: do not use pointer arithmetic
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:4: warning: do not use pointer arithmetic
++p;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use pointer arithmetic
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:3: warning: do not use pointer arithmetic

p--;
// CHECK-MESSAGES: :[[@LINE-1]]:4: warning: do not use pointer arithmetic
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:4: warning: do not use pointer arithmetic
--p;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use pointer arithmetic
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:3: warning: do not use pointer arithmetic

i = p[1];
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use pointer arithmetic

p = ip + 1;
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: do not use pointer arithmetic
ip++;
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:5: warning: do not use pointer arithmetic
i = ip[1];
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use pointer arithmetic
}
Expand All @@ -72,7 +74,7 @@ void template_fail() {
q = p - 1;
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic
p++;
// CHECK-MESSAGES: :[[@LINE-1]]:4: warning: do not use pointer arithmetic
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:4: warning: do not use pointer arithmetic
i = p[1];
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use pointer arithmetic
}
Expand Down