[PATCH] D64073: [ASTImporter] Fix import of lambda in function param
Gabor Marton via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 3 06:29:41 PDT 2019
martong updated this revision to Diff 207776.
martong marked an inline comment as done.
martong added a comment.
- 'the ' -> 'the '
- Use std::distance()
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D64073/new/
https://reviews.llvm.org/D64073
Files:
clang/lib/AST/ASTImporter.cpp
clang/unittests/AST/ASTImporterTest.cpp
Index: clang/unittests/AST/ASTImporterTest.cpp
===================================================================
--- clang/unittests/AST/ASTImporterTest.cpp
+++ clang/unittests/AST/ASTImporterTest.cpp
@@ -5083,6 +5083,45 @@
INSTANTIATE_TEST_CASE_P(ParameterizedTests, CanonicalRedeclChain,
::testing::Values(ArgVector()), );
+TEST_P(ASTImporterOptionSpecificTestBase, LambdaInFunctionBody) {
+ Decl *FromTU = getTuDecl(
+ R"(
+ void f() {
+ auto L = [](){};
+ }
+ )",
+ Lang_CXX11, "input0.cc");
+ auto Pattern = lambdaExpr();
+ CXXRecordDecl *FromL =
+ FirstDeclMatcher<LambdaExpr>().match(FromTU, Pattern)->getLambdaClass();
+
+ auto ToL = Import(FromL, Lang_CXX11);
+ unsigned ToLSize = std::distance(ToL->decls().begin(), ToL->decls().end());
+ unsigned FromLSize =
+ std::distance(FromL->decls().begin(), FromL->decls().end());
+ EXPECT_NE(ToLSize, 0u);
+ EXPECT_EQ(ToLSize, FromLSize);
+}
+
+TEST_P(ASTImporterOptionSpecificTestBase, LambdaInFunctionParam) {
+ Decl *FromTU = getTuDecl(
+ R"(
+ template <typename F>
+ void f(F L = [](){}) {}
+ )",
+ Lang_CXX11, "input0.cc");
+ auto Pattern = lambdaExpr();
+ CXXRecordDecl *FromL =
+ FirstDeclMatcher<LambdaExpr>().match(FromTU, Pattern)->getLambdaClass();
+
+ auto ToL = Import(FromL, Lang_CXX11);
+ unsigned ToLSize = std::distance(ToL->decls().begin(), ToL->decls().end());
+ unsigned FromLSize =
+ std::distance(FromL->decls().begin(), FromL->decls().end());
+ EXPECT_NE(ToLSize, 0u);
+ EXPECT_EQ(ToLSize, FromLSize);
+}
+
INSTANTIATE_TEST_CASE_P(ParameterizedTests, ASTImporterLookupTableTest,
DefaultTestValuesForRunOptions, );
Index: clang/lib/AST/ASTImporter.cpp
===================================================================
--- clang/lib/AST/ASTImporter.cpp
+++ clang/lib/AST/ASTImporter.cpp
@@ -1708,8 +1708,18 @@
Error ASTNodeImporter::ImportDefinition(
RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) {
if (To->getDefinition() || To->isBeingDefined()) {
- if (Kind == IDK_Everything)
- return ImportDeclContext(From, /*ForceImport=*/true);
+ if (Kind == IDK_Everything ||
+ // In case of lambdas, the class already has a definition ptr set, but
+ // the contained decls are not imported yet. Also, isBeingDefined was
+ // set in CXXRecordDecl::CreateLambda. We must import the contained
+ // decls here and finish the definition.
+ (To->isLambda() && shouldForceImportDeclContext(Kind))) {
+ Error Result = ImportDeclContext(From, /*ForceImport=*/true);
+ // Finish the definition of the lambda, set isBeingDefined to false.
+ if (To->isLambda())
+ To->completeDefinition();
+ return Result;
+ }
return Error::success();
}
@@ -7422,19 +7432,10 @@
return ToClassOrErr.takeError();
CXXRecordDecl *ToClass = *ToClassOrErr;
- // NOTE: lambda classes are created with BeingDefined flag set up.
- // It means that ImportDefinition doesn't work for them and we should fill it
- // manually.
- if (ToClass->isBeingDefined())
- if (Error Err = ImportDeclContext(FromClass, /*ForceImport = */ true))
- return std::move(Err);
-
auto ToCallOpOrErr = import(E->getCallOperator());
if (!ToCallOpOrErr)
return ToCallOpOrErr.takeError();
- ToClass->completeDefinition();
-
SmallVector<LambdaCapture, 8> ToCaptures;
ToCaptures.reserve(E->capture_size());
for (const auto &FromCapture : E->captures()) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D64073.207776.patch
Type: text/x-patch
Size: 3566 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190703/f669c2b4/attachment.bin>
More information about the cfe-commits
mailing list