Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,8 @@ Changes in existing checks
- Improved :doc:`misc-const-correctness
<clang-tidy/checks/misc/const-correctness>` check to avoid false
positives when pointers is tranferred to non-const references
and avoid false positives of function pointer.
and avoid false positives of function pointer and fix false
positives on return of non-const pointer.

- Improved :doc:`misc-header-include-cycle
<clang-tidy/checks/misc/header-include-cycle>` check performance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ void ignore_const_alias() {
p_local0 = &a[1];
}

void *return_non_const() {
void *const a = nullptr;
return a;
}

void function_pointer_basic() {
void (*const fp)() = nullptr;
fp();
Expand Down
20 changes: 12 additions & 8 deletions clang/lib/Analysis/ExprMutationAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ class ExprPointeeResolve {
// explicit cast will be checked in `findPointeeToNonConst`
const CastKind kind = ICE->getCastKind();
if (kind == CK_LValueToRValue || kind == CK_DerivedToBase ||
kind == CK_UncheckedDerivedToBase)
kind == CK_UncheckedDerivedToBase ||
(kind == CK_NoOp && (ICE->getType() == ICE->getSubExpr()->getType())))
return resolveExpr(ICE->getSubExpr());
return false;
}
Expand Down Expand Up @@ -788,13 +789,16 @@ ExprMutationAnalyzer::Analyzer::findPointeeToNonConst(const Expr *Exp) {
// FIXME: false positive if the pointee does not change in lambda
const auto CaptureNoConst = lambdaExpr(hasCaptureInit(Exp));

const auto Matches =
match(stmt(anyOf(forEachDescendant(
stmt(anyOf(AssignToNonConst, PassAsNonConstArg,
CastToNonConst, CaptureNoConst))
.bind("stmt")),
forEachDescendant(InitToNonConst))),
Stm, Context);
const auto ReturnNoConst =
returnStmt(hasReturnValue(canResolveToExprPointee(Exp)));

const auto Matches = match(
stmt(anyOf(forEachDescendant(
stmt(anyOf(AssignToNonConst, PassAsNonConstArg,
CastToNonConst, CaptureNoConst, ReturnNoConst))
.bind("stmt")),
forEachDescendant(InitToNonConst))),
Stm, Context);
return selectFirst<Stmt>("stmt", Matches);
}

Expand Down
38 changes: 38 additions & 0 deletions clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2038,4 +2038,42 @@ TEST(ExprMutationAnalyzerTest, PointeeMutatedByConditionOperator) {
EXPECT_TRUE(isPointeeMutated(Results, AST.get()));
}

TEST(ExprMutationAnalyzerTest, PointeeMutatedByReturn) {
{
const std::string Code = R"(
int * f() {
int *const x = nullptr;
return x;
})";
auto AST = buildASTFromCodeWithArgs(Code, {"-Wno-everything"});
auto Results =
match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
EXPECT_TRUE(isPointeeMutated(Results, AST.get()));
}
{
const std::string Code = R"(
int * f() {
int *const x = nullptr;
return x;
})";
// in C++23, AST will have NoOp cast.
auto AST =
buildASTFromCodeWithArgs(Code, {"-Wno-everything", "-std=c++23"});
auto Results =
match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
EXPECT_TRUE(isPointeeMutated(Results, AST.get()));
}
{
const std::string Code = R"(
int const* f() {
int *const x = nullptr;
return x;
})";
auto AST = buildASTFromCodeWithArgs(Code, {"-Wno-everything"});
auto Results =
match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
EXPECT_FALSE(isPointeeMutated(Results, AST.get()));
}
}

} // namespace clang
Loading