Skip to content

Commit 5a9e6ba

Browse files
authored
[clang-tidy] fix false negatives with type aliases in cppcoreguidlines-pro-bounds-pointer-arithmetic check (#139430)
Fixed false negatives with type aliases in `cppcoreguidlines-pro-bounds-pointer-arithmetic` check. Added tests with pointer arithmetic in template functions to make test cases more robust. Closes #139241.
1 parent 59e812f commit 5a9e6ba

File tree

5 files changed

+50
-7
lines changed

5 files changed

+50
-7
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,26 @@ using namespace clang::ast_matchers;
1515
namespace clang::tidy::cppcoreguidelines {
1616

1717
void ProBoundsPointerArithmeticCheck::registerMatchers(MatchFinder *Finder) {
18-
if (!getLangOpts().CPlusPlus)
19-
return;
20-
2118
const auto AllPointerTypes =
22-
anyOf(hasType(pointerType()),
19+
anyOf(hasType(hasUnqualifiedDesugaredType(pointerType())),
2320
hasType(autoType(
2421
hasDeducedType(hasUnqualifiedDesugaredType(pointerType())))),
2522
hasType(decltypeType(hasUnderlyingType(pointerType()))));
2623

27-
// Flag all operators +, -, +=, -=, ++, -- that result in a pointer
24+
// Flag all operators +, -, +=, -= that result in a pointer
2825
Finder->addMatcher(
2926
binaryOperator(
3027
hasAnyOperatorName("+", "-", "+=", "-="), AllPointerTypes,
3128
unless(hasLHS(ignoringImpCasts(declRefExpr(to(isImplicit()))))))
3229
.bind("expr"),
3330
this);
3431

32+
// Flag all operators ++, -- that result in a pointer
3533
Finder->addMatcher(
36-
unaryOperator(hasAnyOperatorName("++", "--"), hasType(pointerType()))
34+
unaryOperator(hasAnyOperatorName("++", "--"),
35+
hasType(hasUnqualifiedDesugaredType(pointerType())),
36+
unless(hasUnaryOperand(
37+
ignoringImpCasts(declRefExpr(to(isImplicit()))))))
3738
.bind("expr"),
3839
this);
3940

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class ProBoundsPointerArithmeticCheck : public ClangTidyCheck {
2323
public:
2424
ProBoundsPointerArithmeticCheck(StringRef Name, ClangTidyContext *Context)
2525
: ClangTidyCheck(Name, Context) {}
26+
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
27+
return LangOpts.CPlusPlus;
28+
}
2629
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
2730
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
2831
};

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ Changes in existing checks
219219
- Improved :doc:`cppcoreguidelines-pro-bounds-pointer-arithmetic
220220
<clang-tidy/checks/cppcoreguidelines/pro-bounds-pointer-arithmetic>` check by
221221
fixing false positives when calling indexing operators that do not perform
222-
pointer arithmetic in template, for example ``std::map::operator[]``.
222+
pointer arithmetic in template, for example ``std::map::operator[]`` and
223+
when pointer arithmetic was used through type aliases.
223224

224225
- Improved :doc:`cppcoreguidelines-rvalue-reference-param-not-moved
225226
<clang-tidy/checks/cppcoreguidelines/rvalue-reference-param-not-moved>` check

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
// RUN: %check_clang_tidy -std=c++14-or-later %s cppcoreguidelines-pro-bounds-pointer-arithmetic %t
22

33
// Fix PR36489 and detect auto-deduced value correctly.
4+
typedef char* charPtr;
5+
46
char *getPtr();
57
auto getPtrAuto() { return getPtr(); }
68
decltype(getPtr()) getPtrDeclType();
79
decltype(auto) getPtrDeclTypeAuto() { return getPtr(); }
810
auto getPtrWithTrailingReturnType() -> char *;
11+
charPtr getCharPtr() { return getPtr(); }
912

1013
void auto_deduction_binary() {
1114
auto p1 = getPtr() + 1;
@@ -28,6 +31,10 @@ void auto_deduction_binary() {
2831
// CHECK-MESSAGES: :[[@LINE-1]]:31: warning: do not use pointer arithmetic
2932
auto *p9 = getPtrDeclTypeAuto() + 1;
3033
// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: do not use pointer arithmetic
34+
auto p10 = getCharPtr() + 1;
35+
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: do not use pointer
36+
auto* p11 = getCharPtr() + 1;
37+
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: do not use pointer arithmetic
3138
}
3239

3340
void auto_deduction_subscript() {
@@ -50,4 +57,6 @@ void auto_deduction_subscript() {
5057
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not use pointer arithmetic
5158
auto p8 = getPtrDeclTypeAuto()[9];
5259
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not use pointer arithmetic
60+
auto p9 = getCharPtr()[10];
61+
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not use pointer arithmetic
5362
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ enum E {
44
ENUM_LITERAL = 1
55
};
66

7+
typedef int* IntPtr;
8+
79
int i = 4;
810
int j = 1;
911
int *p = 0;
1012
int *q = 0;
13+
IntPtr ip = 0;
1114

1215
void fail() {
1316
q = p + 4;
@@ -50,6 +53,32 @@ void fail() {
5053

5154
i = p[1];
5255
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use pointer arithmetic
56+
57+
p = ip + 1;
58+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: do not use pointer arithmetic
59+
ip++;
60+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic
61+
i = ip[1];
62+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use pointer arithmetic
63+
}
64+
65+
template <typename T>
66+
void template_fail() {
67+
T* p;
68+
T* q;
69+
70+
p = q + 1;
71+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic
72+
q = p - 1;
73+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic
74+
p++;
75+
// CHECK-MESSAGES: :[[@LINE-1]]:4: warning: do not use pointer arithmetic
76+
i = p[1];
77+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use pointer arithmetic
78+
}
79+
80+
void instantiate() {
81+
template_fail<int>();
5382
}
5483

5584
struct S {

0 commit comments

Comments
 (0)