diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -2302,6 +2302,11 @@ if (ToND) return ToND; + BindingDecl *ToD; + if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, Loc, + Name.getAsIdentifierInfo())) + return ToD; + Error Err = Error::success(); QualType ToType = importChecked(Err, D->getType()); Expr *ToBinding = importChecked(Err, D->getBinding()); @@ -2309,11 +2314,6 @@ if (Err) return std::move(Err); - BindingDecl *ToD; - if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, Loc, - Name.getAsIdentifierInfo())) - return ToD; - ToD->setBinding(ToType, ToBinding); ToD->setDecomposedDecl(ToDecomposedDecl); addDeclToContexts(D, ToD); @@ -4098,14 +4098,26 @@ if (Err) return std::move(Err); - // Create the imported variable. VarDecl *ToVar; - if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC, - ToInnerLocStart, Loc, - Name.getAsIdentifierInfo(), - ToType, ToTypeSourceInfo, - D->getStorageClass())) - return ToVar; + if (auto *FromDecomp = dyn_cast(D)) { + SmallVector Bindings(FromDecomp->bindings().size()); + if (Error Err = + ImportArrayChecked(FromDecomp->bindings(), Bindings.begin())) + return std::move(Err); + DecompositionDecl *ToDecomp; + if (GetImportedOrCreateDecl( + ToDecomp, FromDecomp, Importer.getToContext(), DC, ToInnerLocStart, + Loc, ToType, ToTypeSourceInfo, D->getStorageClass(), Bindings)) + return ToDecomp; + ToVar = ToDecomp; + } else { + // Create the imported variable. + if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC, + ToInnerLocStart, Loc, + Name.getAsIdentifierInfo(), ToType, + ToTypeSourceInfo, D->getStorageClass())) + return ToVar; + } ToVar->setTSCSpec(D->getTSCSpec()); ToVar->setQualifierInfo(ToQualifierLoc); diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp --- a/clang/unittests/AST/ASTImporterTest.cpp +++ b/clang/unittests/AST/ASTImporterTest.cpp @@ -3725,6 +3725,32 @@ VerifyImport("y3"); } +TEST_P(ImportVariables, ImportDecompositionDeclArray) { + Decl *From, *To; + std::tie(From, To) = getImportedDecl( + R"( + void declToImport() { + int a[2] = {1,2}; + auto [x1,y1] = a; + }; + )", + Lang_CXX17, "", Lang_CXX17); + + TranslationUnitDecl *FromTU = From->getTranslationUnitDecl(); + auto *FromDecomp = + FirstDeclMatcher().match(FromTU, decompositionDecl()); + auto *ToDecomp = Import(FromDecomp, Lang_CXX17); + EXPECT_TRUE(ToDecomp); + + ArrayRef FromB = FromDecomp->bindings(); + ArrayRef ToB = ToDecomp->bindings(); + EXPECT_EQ(FromB.size(), ToB.size()); + for (unsigned int I = 0; I < FromB.size(); ++I) { + auto *ToBI = Import(FromB[I], Lang_CXX17); + EXPECT_EQ(ToBI, ToB[I]); + } +} + struct ImportClasses : ASTImporterOptionSpecificTestBase {}; TEST_P(ImportClasses, ImportDefinitionWhenProtoIsInNestedToContext) {