jankratochvil updated this revision to Diff 342249.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D101236/new/

https://reviews.llvm.org/D101236

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
@@ -6323,6 +6323,105 @@
             ToD->getTemplateSpecializationKind());
 }
 
+TEST_P(ASTImporterOptionSpecificTestBase, ImportNoUniqueAddress) {
+  Decl *FromTU = getTuDecl(
+      R"(
+      struct A {};
+      struct B {
+        [[no_unique_address]] A a;
+      };
+      struct C : B {
+        char c;
+      } c;
+      )", Lang_CXX20);
+
+  auto *BFromD = FirstDeclMatcher<CXXRecordDecl>().match(
+      FromTU, cxxRecordDecl(hasName("B")));
+  ASSERT_TRUE(BFromD);
+  auto *BToD = Import(BFromD, Lang_CXX20);
+  EXPECT_TRUE(BToD);
+
+  auto *CFromD = FirstDeclMatcher<CXXRecordDecl>().match(
+      FromTU, cxxRecordDecl(hasName("C")));
+  ASSERT_TRUE(CFromD);
+  auto *CToD = Import(CFromD, Lang_CXX20);
+  EXPECT_TRUE(CToD);
+
+  QualType CFType = FromTU->getASTContext().getRecordType(CFromD);
+  TypeInfo TFI = FromTU->getASTContext().getTypeInfo(CFType);
+  ASSERT_EQ(TFI.Width, 8U);
+  QualType CTType = FromTU->getASTContext().getRecordType(CToD);
+  TypeInfo TTI = FromTU->getASTContext().getTypeInfo(CTType);
+  // Here we test the NoUniqueAddressAttr import.
+  // But the Record definition part is not tested here.
+  ASSERT_EQ(TTI.Width, TFI.Width);
+}
+
+struct LLDBMinimalImport : ASTImporterOptionSpecificTestBase {
+  LLDBMinimalImport() {
+    Creator = [](ASTContext &ToContext, FileManager &ToFileManager,
+                 ASTContext &FromContext, FileManager &FromFileManager,
+                 bool MinimalImport,
+                 const std::shared_ptr<ASTImporterSharedState> &SharedState) {
+      return new ASTImporter(ToContext, ToFileManager, FromContext,
+                             FromFileManager, true /*MinimalImport*/,
+                             // We use the regular lookup.
+                             /*SharedState=*/nullptr);
+    };
+  }
+};
+
+TEST_P(LLDBMinimalImport, LLDBImportNoUniqueAddress) {
+  TranslationUnitDecl *ToTU = getToTuDecl(
+      R"(
+      struct A {};
+      struct B {
+        [[no_unique_address]] A a;
+      };
+      struct C : B {
+        char c;
+      } c;
+      )", Lang_CXX20);
+  auto *ToC = FirstDeclMatcher<CXXRecordDecl>().match(
+      ToTU, cxxRecordDecl(hasName("C")));
+
+  // Set up a stub external storage.
+  ToTU->setHasExternalLexicalStorage(true);
+  // Set up DeclContextBits.HasLazyExternalLexicalLookups to true.
+  ToTU->setMustBuildLookupTable();
+  struct TestExternalASTSource : ExternalASTSource {};
+  ToTU->getASTContext().setExternalSource(new TestExternalASTSource());
+
+  Decl *FromTU = getTuDecl(
+      R"(
+        struct C;
+      )",
+      Lang_CXX20);
+  auto *FromC = FirstDeclMatcher<CXXRecordDecl>().match(
+      FromTU, cxxRecordDecl(hasName("C")));
+  auto *ImportedC = Import(FromC, Lang_CXX20);
+  // The lookup must find the existing class definition in the LinkageSpecDecl.
+  // Then the importer renders the existing and the new decl into one chain.
+  EXPECT_EQ(ImportedC->getCanonicalDecl(), ToC->getCanonicalDecl());
+
+  // Import the definition of the created class.
+  llvm::Error Err = findFromTU(FromC)->Importer->ImportDefinition(ToC);
+  EXPECT_FALSE((bool)Err);
+  consumeError(std::move(Err));
+
+  QualType CTType = ToTU->getASTContext().getRecordType(ToC);
+  TypeInfo TTI = ToTU->getASTContext().getTypeInfo(CTType);
+  fprintf(stderr,"LLDB To Width=%lu\n",TTI.Width);
+#if 0 // getASTRecordLayout(const clang::RecordDecl *) const: Assertion `D && "Cannot get layout of forward declarations!"' failed.
+  QualType CFType = FromTU->getASTContext().getRecordType(FromC);
+  TypeInfo TFI = FromTU->getASTContext().getTypeInfo(CFType);
+  fprintf(stderr,"LLDB From Width=%lu\n",TFI.Width);
+#endif
+  QualType CIType = FromTU->getASTContext().getRecordType(ImportedC);
+  TypeInfo TII = FromTU->getASTContext().getTypeInfo(CIType);
+  fprintf(stderr,"LLDB Imported Width=%lu\n",TII.Width);
+}
+
 INSTANTIATE_TEST_CASE_P(ParameterizedTests, ASTImporterLookupTableTest,
                         DefaultTestValuesForRunOptions, );
 
@@ -6397,5 +6496,8 @@
 INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportWithExternalSource,
                         DefaultTestValuesForRunOptions, );
 
+INSTANTIATE_TEST_CASE_P(ParameterizedTests, LLDBMinimalImport,
+                        DefaultTestValuesForRunOptions, );
+
 } // end namespace ast_matchers
 } // end namespace clang
Index: clang/lib/AST/ASTImporter.cpp
===================================================================
--- clang/lib/AST/ASTImporter.cpp
+++ clang/lib/AST/ASTImporter.cpp
@@ -3673,6 +3673,30 @@
                               D->getInClassInitStyle()))
     return ToField;
 
+  // FieldDecl::isZeroSize may need to check these.
+  if (const Attr *FromAttr = D->getAttr<NoUniqueAddressAttr>()) {
+    if (auto ToAttrOrErr = Importer.Import(FromAttr))
+      ToField->addAttr(*ToAttrOrErr);
+    else
+      return ToAttrOrErr.takeError();
+#if 0
+    RecordType *FromRT =
+        const_cast<RecordType *>(D->getType()->getAs<RecordType>());
+    // Is this field a struct? FieldDecl::isZeroSize will need its definition.
+    if (FromRT) {
+      RecordDecl *FromRD = FromRT->getDecl();
+      RecordType *ToRT =
+          const_cast<RecordType *>(ToField->getType()->getAs<RecordType>());
+      assert(ToRT && "RecordType import has different type");
+      RecordDecl *ToRD = ToRT->getDecl();
+      if (FromRD->isCompleteDefinition() && !ToRD->isCompleteDefinition()) {
+        if (Error Err = ImportDefinition(FromRD, ToRD, IDK_Basic))
+          return std::move(Err);
+      }
+    }
+#endif
+  }
+
   ToField->setAccess(D->getAccess());
   ToField->setLexicalDeclContext(LexicalDC);
   if (ToInitializer)
@@ -8445,6 +8469,8 @@
   // Make sure that ImportImpl registered the imported decl.
   assert(ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?");
 
+  // There may have been NoUniqueAddressAttr for FieldDecl::isZeroSize.
+  ToD->dropAttrs();
   if (FromD->hasAttrs())
     for (const Attr *FromAttr : FromD->getAttrs()) {
       auto ToAttrOrErr = Import(FromAttr);
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to