-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Add option to allow pre/post increment/decrement operator in cppcoreg… #155015
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
Conversation
…uidelines-pro-bounds-pointer-arithmetic Fixes llvm#154907
@llvm/pr-subscribers-clang-tools-extra @llvm/pr-subscribers-clang-tidy Author: Carlos Galvez (carlosgalvezp) Changes…uidelines-pro-bounds-pointer-arithmetic Fixes #154907 Full diff: https://github.com/llvm/llvm-project/pull/155015.diff 5 Files Affected:
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
}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/143/builds/10225 Here is the relevant piece of the build log for the reference
|
…uidelines-pro-bounds-pointer-arithmetic
Fixes #154907