[clang-tools-extra] Add option to allow pre/post increment/decrement operator in cppcoreg… (PR #155015)
Carlos Galvez via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 22 12:17:30 PDT 2025
https://github.com/carlosgalvezp created https://github.com/llvm/llvm-project/pull/155015
…uidelines-pro-bounds-pointer-arithmetic
Fixes #154907
>From c4f59473a6bac427e393cbc19aee9d485348acee Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20G=C3=A1lvez?= <carlos.galvez at zenseact.com>
Date: Fri, 22 Aug 2025 19:13:18 +0000
Subject: [PATCH] Add option to allow pre/post increment/decrement operator in
cppcoreguidelines-pro-bounds-pointer-arithmetic
Fixes #154907
---
.../ProBoundsPointerArithmeticCheck.cpp | 27 ++++++++++++++-----
.../ProBoundsPointerArithmeticCheck.h | 7 +++--
clang-tools-extra/docs/ReleaseNotes.rst | 5 ++++
.../pro-bounds-pointer-arithmetic.rst | 8 ++++++
.../pro-bounds-pointer-arithmetic.cpp | 16 ++++++-----
5 files changed, 47 insertions(+), 16 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.cpp
index 9ac7b9e057e35..51995c5f64ef6 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.cpp
@@ -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())),
@@ -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(
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.h b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.h
index 3466c72a769e9..785f754055fb8 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.h
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.h
@@ -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
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 383286eb0c5a3..780e5b3fc21cf 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -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.
diff --git a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/pro-bounds-pointer-arithmetic.rst b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/pro-bounds-pointer-arithmetic.rst
index 12a8f60184fe5..a3f13714e809c 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/pro-bounds-pointer-arithmetic.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/pro-bounds-pointer-arithmetic.rst
@@ -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``.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic.cpp
index aed6080471e1f..fa81c135a1803 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic.cpp
@@ -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
@@ -42,14 +44,14 @@ 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
@@ -57,7 +59,7 @@ void fail() {
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
}
@@ -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
}
More information about the cfe-commits
mailing list