[PATCH] D44100: [ASTImporter] Reorder fields after structure import is finished

Aleksei Sidorin via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 13 15:57:36 PDT 2018


a_sidorin updated this revision to Diff 160477.
a_sidorin edited the summary of this revision.
a_sidorin added a comment.

All declarations are reordered now, not only fields. Also some review comments were addressed.


Repository:
  rC Clang

https://reviews.llvm.org/D44100

Files:
  lib/AST/ASTImporter.cpp
  unittests/AST/ASTImporterTest.cpp


Index: unittests/AST/ASTImporterTest.cpp
===================================================================
--- unittests/AST/ASTImporterTest.cpp
+++ unittests/AST/ASTImporterTest.cpp
@@ -1428,7 +1428,7 @@
 }
 
 TEST_P(ASTImporterTestBase,
-       DISABLED_CXXRecordDeclFieldOrderShouldNotDependOnImportOrder) {
+       CXXRecordDeclFieldOrderShouldNotDependOnImportOrder) {
   Decl *From, *To;
   std::tie(From, To) = getImportedDecl(
       // The original recursive algorithm of ASTImporter first imports 'c' then
@@ -2995,5 +2995,16 @@
                         ImportFunctionTemplateSpecializations,
                         DefaultTestValuesForRunOptions, );
 
+TEST_P(ImportDecl, ImportFieldOrder) {
+  MatchVerifier<Decl> Verifier;
+  testImport("struct declToImport {"
+             "  int b = a + 2;"
+             "  int a = 5;"
+             "};",
+             Lang_CXX11, "", Lang_CXX11, Verifier,
+             recordDecl(hasFieldOrder({"b", "a"})));
+}
+
+
 } // end namespace ast_matchers
 } // end namespace clang
Index: lib/AST/ASTImporter.cpp
===================================================================
--- lib/AST/ASTImporter.cpp
+++ lib/AST/ASTImporter.cpp
@@ -1299,6 +1299,36 @@
     if (!Importer.Import(From))
       return true;
 
+  // Reorder declarations in RecordDecls because they may have another
+  // order. Keeping field order is vitable because it determines structure
+  // layout.
+  // FIXME: This is an ugly fix. Unfortunately, I cannot come with better
+  // solution for this issue. We cannot defer expression import here because
+  // type import can depend on them.
+  const auto *FromRD = dyn_cast<RecordDecl>(FromDC);
+  if (!FromRD)
+    return false;
+
+  auto ImportedDC = Importer.Import(cast<Decl>(FromDC));
+  assert(ImportedDC);
+  auto *ToRD = cast<RecordDecl>(*ImportedDC);
+
+  for (auto *D : FromRD->decls()) {
+    Decl *ToD = Importer.GetAlreadyImportedOrNull(D);
+    assert(ToRD == ToD->getDeclContext() && ToRD->containsDecl(ToD));
+    ToRD->removeDecl(ToD);
+  }
+
+  assert(ToRD->decls_empty());
+
+  for (auto *D : FromRD->decls()) {
+    Decl *ToD = Importer.GetAlreadyImportedOrNull(D);
+    assert(ToRD == ToD->getDeclContext());
+    assert(ToRD == ToD->getLexicalDeclContext());
+    assert(!ToRD->containsDecl(ToD));
+    ToRD->addDeclInternal(ToD);
+  }
+
   return false;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D44100.160477.patch
Type: text/x-patch
Size: 2359 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180813/20a9023f/attachment.bin>


More information about the cfe-commits mailing list