Skip to content

[clang][dataflow] Handle this-capturing lambdas in field initializers. #99519

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

Merged
merged 1 commit into from
Jul 22, 2024
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
21 changes: 15 additions & 6 deletions clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,12 +518,21 @@ void Environment::initialize() {
assert(VarDecl != nullptr);
setStorageLocation(*VarDecl, createObject(*VarDecl, nullptr));
} else if (Capture.capturesThis()) {
const auto *SurroundingMethodDecl =
cast<CXXMethodDecl>(InitialTargetFunc->getNonClosureAncestor());
QualType ThisPointeeType =
SurroundingMethodDecl->getFunctionObjectParameterType();
setThisPointeeStorageLocation(
cast<RecordStorageLocation>(createObject(ThisPointeeType)));
if (auto *Ancestor = InitialTargetFunc->getNonClosureAncestor()) {
const auto *SurroundingMethodDecl = cast<CXXMethodDecl>(Ancestor);
QualType ThisPointeeType =
SurroundingMethodDecl->getFunctionObjectParameterType();
setThisPointeeStorageLocation(
cast<RecordStorageLocation>(createObject(ThisPointeeType)));
} else if (auto *FieldBeingInitialized =
dyn_cast<FieldDecl>(Parent->getLambdaContextDecl())) {
// This is in a field initializer, rather than a method.
setThisPointeeStorageLocation(
cast<RecordStorageLocation>(createObject(QualType(
FieldBeingInitialized->getParent()->getTypeForDecl(), 0))));
} else {
assert(false && "Unexpected this-capturing lambda context.");
}
}
}
} else if (MethodDecl->isImplicitObjectMemberFunction()) {
Expand Down
28 changes: 28 additions & 0 deletions clang/unittests/Analysis/FlowSensitive/DataflowEnvironmentTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,32 @@ TEST_F(EnvironmentTest, Stmt) {
Env.getResultObjectLocation(*Init);
}

// This is a crash repro.
TEST_F(EnvironmentTest, LambdaCapturingThisInFieldInitializer) {
using namespace ast_matchers;
std::string Code = R"cc(
struct S {
int f{[this]() { return 1; }()};
};
)cc";

auto Unit =
tooling::buildASTFromCodeWithArgs(Code, {"-fsyntax-only", "-std=c++11"});
auto &Context = Unit->getASTContext();

ASSERT_EQ(Context.getDiagnostics().getClient()->getNumErrors(), 0U);

auto *LambdaCallOperator = selectFirst<CXXMethodDecl>(
"method", match(cxxMethodDecl(hasName("operator()"),
ofClass(cxxRecordDecl(isLambda())))
.bind("method"),
Context));

Environment Env(DAContext, *LambdaCallOperator);
// Don't crash when initializing.
Env.initialize();
// And initialize the captured `this` pointee.
ASSERT_NE(nullptr, Env.getThisPointeeStorageLocation());
}

} // namespace
Loading