Skip to content

Commit 326ee7a

Browse files
authored
[clang-tidy] Fix false positive in misc-const-correctness (#170065)
Closes llvm/llvm-project#131599 and llvm/llvm-project#170033
1 parent 91e8780 commit 326ee7a

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,8 @@ Changes in existing checks
447447
positives when pointers is transferred to non-const references
448448
and avoid false positives of function pointer and fix false
449449
positives on return of non-const pointer and fix false positives on
450-
pointer-to-member operator.
450+
pointer-to-member operator and avoid false positives when the address
451+
of a variable is taken to be passed to a function.
451452

452453
- Improved :doc:`misc-coroutine-hostile-raii
453454
<clang-tidy/checks/misc/coroutine-hostile-raii>` check by adding the option

clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,18 @@ void ignoreNonConstRefOps() {
7373
int* p2 {nullptr};
7474
int*& r2 = (int*&)p2;
7575
}
76+
77+
void pointer_to_pointer_param(int**);
78+
void pass_address_to_pointer_to_pointer() {
79+
int i = 0;
80+
int* ip = &i;
81+
// CHECK-NOT: warning
82+
pointer_to_pointer_param(&ip);
83+
}
84+
85+
void void_pointer_to_pointer_param(void**);
86+
void pass_address_to_void_pointer_to_pointer() {
87+
void* ptr = nullptr;
88+
// CHECK-NOT: warning
89+
void_pointer_to_pointer_param(&ptr);
90+
}

clang/lib/Analysis/ExprMutationAnalyzer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ class ExprPointeeResolve {
135135
if (const auto *PE = dyn_cast<ParenExpr>(E))
136136
return resolveExpr(PE->getSubExpr());
137137

138+
if (const auto *UO = dyn_cast<UnaryOperator>(E)) {
139+
if (UO->getOpcode() == UO_AddrOf)
140+
return resolveExpr(UO->getSubExpr());
141+
}
142+
138143
if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) {
139144
// only implicit cast needs to be treated as resolvable.
140145
// explicit cast will be checked in `findPointeeToNonConst`

clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,4 +2091,21 @@ TEST(ExprMutationAnalyzerTest, PointeeMutatedByPointerToMemberOperator) {
20912091
EXPECT_TRUE(isPointeeMutated(Results, AST.get()));
20922092
}
20932093

2094+
TEST(ExprMutationAnalyzerTest, PointeeMutatedByPassAsPointerToPointer) {
2095+
{
2096+
const std::string Code = "void f(int**); void g() { int* ip; f(&ip); }";
2097+
auto AST = buildASTFromCode(Code);
2098+
auto Results =
2099+
match(withEnclosingCompound(declRefTo("ip")), AST->getASTContext());
2100+
EXPECT_TRUE(isPointeeMutated(Results, AST.get()));
2101+
}
2102+
{
2103+
const std::string Code = "void f(void**); void g() { void* ip; f(&ip); }";
2104+
auto AST = buildASTFromCode(Code);
2105+
auto Results =
2106+
match(withEnclosingCompound(declRefTo("ip")), AST->getASTContext());
2107+
EXPECT_TRUE(isPointeeMutated(Results, AST.get()));
2108+
}
2109+
}
2110+
20942111
} // namespace clang

0 commit comments

Comments
 (0)