Skip to content

Commit efed984

Browse files
carlosgalvezpCarlos Gálvez
andauthored
Add option to allow pre/post increment/decrement operator in cppcoreg… (llvm#155015)
…uidelines-pro-bounds-pointer-arithmetic Fixes llvm#154907 Co-authored-by: Carlos Gálvez <[email protected]>
1 parent 16deba3 commit efed984

File tree

5 files changed

+47
-16
lines changed

5 files changed

+47
-16
lines changed

clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ using namespace clang::ast_matchers;
1414

1515
namespace clang::tidy::cppcoreguidelines {
1616

17+
ProBoundsPointerArithmeticCheck::ProBoundsPointerArithmeticCheck(
18+
StringRef Name, ClangTidyContext *Context)
19+
: ClangTidyCheck(Name, Context),
20+
AllowIncrementDecrementOperators(
21+
Options.get("AllowIncrementDecrementOperators", false)) {}
22+
23+
void ProBoundsPointerArithmeticCheck::storeOptions(
24+
ClangTidyOptions::OptionMap &Opts) {
25+
Options.store(Opts, "AllowIncrementDecrementOperators",
26+
AllowIncrementDecrementOperators);
27+
}
28+
1729
void ProBoundsPointerArithmeticCheck::registerMatchers(MatchFinder *Finder) {
1830
const auto AllPointerTypes =
1931
anyOf(hasType(hasUnqualifiedDesugaredType(pointerType())),
@@ -30,13 +42,14 @@ void ProBoundsPointerArithmeticCheck::registerMatchers(MatchFinder *Finder) {
3042
this);
3143

3244
// Flag all operators ++, -- that result in a pointer
33-
Finder->addMatcher(
34-
unaryOperator(hasAnyOperatorName("++", "--"),
35-
hasType(hasUnqualifiedDesugaredType(pointerType())),
36-
unless(hasUnaryOperand(
37-
ignoringImpCasts(declRefExpr(to(isImplicit()))))))
38-
.bind("expr"),
39-
this);
45+
if (!AllowIncrementDecrementOperators)
46+
Finder->addMatcher(
47+
unaryOperator(hasAnyOperatorName("++", "--"),
48+
hasType(hasUnqualifiedDesugaredType(pointerType())),
49+
unless(hasUnaryOperand(
50+
ignoringImpCasts(declRefExpr(to(isImplicit()))))))
51+
.bind("expr"),
52+
this);
4053

4154
// Array subscript on a pointer (not an array) is also pointer arithmetic
4255
Finder->addMatcher(

clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ namespace clang::tidy::cppcoreguidelines {
2121
/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/pro-bounds-pointer-arithmetic.html
2222
class ProBoundsPointerArithmeticCheck : public ClangTidyCheck {
2323
public:
24-
ProBoundsPointerArithmeticCheck(StringRef Name, ClangTidyContext *Context)
25-
: ClangTidyCheck(Name, Context) {}
24+
ProBoundsPointerArithmeticCheck(StringRef Name, ClangTidyContext *Context);
2625
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
2726
return LangOpts.CPlusPlus;
2827
}
2928
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
29+
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
3030
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
31+
32+
private:
33+
const bool AllowIncrementDecrementOperators;
3134
};
3235

3336
} // namespace clang::tidy::cppcoreguidelines

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ Changes in existing checks
180180
<clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>` check to
181181
avoid false positives on inherited members in class templates.
182182

183+
- Improved :doc:`cppcoreguidelines-pro-bounds-pointer-arithmetic
184+
<clang-tidy/checks/cppcoreguidelines/pro-bounds-pointer-arithmetic>` check
185+
adding an option to allow pointer arithmetic via prefix/postfix increment or
186+
decrement operators.
187+
183188
- Improved :doc:`misc-header-include-cycle
184189
<clang-tidy/checks/misc/header-include-cycle>` check performance.
185190

clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/pro-bounds-pointer-arithmetic.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,11 @@ arrays of data.
1313
This rule is part of the `Bounds safety (Bounds 1)
1414
<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Pro-bounds-arithmetic>`_
1515
profile from the C++ Core Guidelines.
16+
17+
Options
18+
-------
19+
20+
.. option:: AllowIncrementDecrementOperators
21+
22+
When enabled, the check will allow using the prefix/postfix increment or
23+
decrement operators on pointers. Default is ``false``.

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// RUN: %check_clang_tidy %s cppcoreguidelines-pro-bounds-pointer-arithmetic %t
1+
// RUN: %check_clang_tidy %s cppcoreguidelines-pro-bounds-pointer-arithmetic -check-suffixes=,DEFAULT %t
2+
// RUN: %check_clang_tidy %s cppcoreguidelines-pro-bounds-pointer-arithmetic %t -- \
3+
// RUN: -config="{CheckOptions: {cppcoreguidelines-pro-bounds-pointer-arithmetic.AllowIncrementDecrementOperators: true}}" --
24

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

4446
p++;
45-
// CHECK-MESSAGES: :[[@LINE-1]]:4: warning: do not use pointer arithmetic
47+
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:4: warning: do not use pointer arithmetic
4648
++p;
47-
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use pointer arithmetic
49+
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:3: warning: do not use pointer arithmetic
4850

4951
p--;
50-
// CHECK-MESSAGES: :[[@LINE-1]]:4: warning: do not use pointer arithmetic
52+
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:4: warning: do not use pointer arithmetic
5153
--p;
52-
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use pointer arithmetic
54+
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:3: warning: do not use pointer arithmetic
5355

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

5759
p = ip + 1;
5860
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: do not use pointer arithmetic
5961
ip++;
60-
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic
62+
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:5: warning: do not use pointer arithmetic
6163
i = ip[1];
6264
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use pointer arithmetic
6365
}
@@ -72,7 +74,7 @@ void template_fail() {
7274
q = p - 1;
7375
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic
7476
p++;
75-
// CHECK-MESSAGES: :[[@LINE-1]]:4: warning: do not use pointer arithmetic
77+
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:4: warning: do not use pointer arithmetic
7678
i = p[1];
7779
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use pointer arithmetic
7880
}

0 commit comments

Comments
 (0)