Skip to content

[clang][ASTImporter] Only reorder fields of RecordDecls #77079

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

Conversation

Michael137
Copy link
Member

@Michael137 Michael137 commented Jan 5, 2024

Prior to e9536698720ec524cc8b72599363622bc1a31558 (https://reviews.llvm.org/D154764) we only re-ordered the fields of RecordDecls. The change refactored this logic to make sure FieldDecls are imported before other member decls. However, this change also widened the types of DeclContexts we consider for re-ordering from RecordDecl to anything that's a DeclContext. This seems to have been just a drive-by cleanup.

Internally we've seen numerous crashes in LLDB where we try to perform this re-ordering on fields of ObjCInterfaceDecls.

This patch restores old behaviour where we limit the re-ordering to just RecordDecls.

rdar://119343184
rdar://119636274
rdar://119832131

Prior to `e9536698720ec524cc8b72599363622bc1a31558`
(https://reviews.llvm.org/D154764) we only re-ordered
the fields of `RecordDecl`s. The change refactored this
logic to make sure `FieldDecl`s are imported before other
member decls. However, this change also widened the types
of `DeclContext`s we consider for re-ordering from `RecordDecl`
to anything that's a `DeclContext`. This seems to have been
just a drive-by cleanup.

Internally we've seen numerous crashes in LLDB where we try to
perform this re-ordering fields of `ObjCInterfaceDecl`s.

This patch restores old behaviour where we limit the re-ordering
to just `RecordDecl`s.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jan 5, 2024
@llvmbot
Copy link
Member

llvmbot commented Jan 5, 2024

@llvm/pr-subscribers-clang

Author: Michael Buch (Michael137)

Changes

Prior to e9536698720ec524cc8b72599363622bc1a31558 (https://reviews.llvm.org/D154764) we only re-ordered the fields of RecordDecls. The change refactored this logic to make sure FieldDecls are imported before other member decls. However, this change also widened the types of DeclContexts we consider for re-ordering from RecordDecl to anything that's a DeclContext. This seems to have been just a drive-by cleanup.

Internally we've seen numerous crashes in LLDB where we try to perform this re-ordering fields of ObjCInterfaceDecls.

This patch restores old behaviour where we limit the re-ordering to just RecordDecls.


Full diff: https://github.com/llvm/llvm-project/pull/77079.diff

1 Files Affected:

  • (modified) clang/lib/AST/ASTImporter.cpp (+6-2)
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index 9ffae72346f2af..f03ea6e09e8b7e 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -2034,10 +2034,14 @@ ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
     return ToDCOrErr.takeError();
   }
 
+  const auto *FromRD = dyn_cast<RecordDecl>(FromDC);
+  if (!FromRD)
+    return ChildErrors;
+
   DeclContext *ToDC = *ToDCOrErr;
   // Remove all declarations, which may be in wrong order in the
   // lexical DeclContext and then add them in the proper order.
-  for (auto *D : FromDC->decls()) {
+  for (auto *D : FromRD->decls()) {
     if (!MightNeedReordering(D))
       continue;
 
@@ -2055,7 +2059,7 @@ ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
   }
 
   // Import everything else.
-  for (auto *From : FromDC->decls()) {
+  for (auto *From : FromRD->decls()) {
     if (MightNeedReordering(From))
       continue;
 

@Michael137
Copy link
Member Author

Need to sort out some test failures

@balazske
Copy link
Collaborator

balazske commented Jan 5, 2024

I can not check what caused exactly the problems but the change looks correct and in change D154764 there seems to be no reason for re-ordering at non-record objects. As an improvement, some tests could be added that check for re-ordering at RecordDecl and (this is the more important) no re-ordering at ObjCInterfaceDecl.

@Michael137
Copy link
Member Author

Michael137 commented Jan 5, 2024

I can not check what caused exactly the problems but the change looks correct and in change D154764 there seems to be no reason for re-ordering at non-record objects. As an improvement, some tests could be added that check for re-ordering at RecordDecl and (this is the more important) no re-ordering at ObjCInterfaceDecl.

Agreed it would be nice to get a test for this. We already have a test that checks re-ordering of RecordDecls (CXXRecordDeclFieldOrderShouldNotDependOnImportOrder). For ObjCInterfaceDecls it's trickier because, AFAIK, they neither support in-class member initialisers nor function bodies that could trigger a different import order. Any ideas?

@Michael137
Copy link
Member Author

I guess if there's some other type of ObjC decl that could trigger the re-ordering that would be useful.

Copy link
Collaborator

@adrian-prantl adrian-prantl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test would be nice, but generally this looks like a reasonable modification.

@Michael137
Copy link
Member Author

Michael137 commented Jan 8, 2024

Tried to find a way to test the "objective-c fields aren't reordered" case. But after talking to some objective-c experts, there doesn't seem to be a good way to force such "incorrect field order", because it's strictly an in-order language. Reducing the LLDB crashes has been tricky because the reproducers require a somewhat complex setup. @balazske If you're ok with it, I'll go ahead and merge it like this, given it's essentially a minor revert.

Separately I'll try to see if I can simplify the LLDB reproducer, but that'll take some time

Copy link
Collaborator

@balazske balazske left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reordering is not a significant feature (and may change in the future) and no test existed before, it is enough (if this exists) if there is a GDB test for the crash.

@Michael137 Michael137 merged commit 34dbadd into llvm:main Jan 8, 2024
Michael137 added a commit to Michael137/llvm-project that referenced this pull request Jan 8, 2024
Prior to `e9536698720ec524cc8b72599363622bc1a31558`
(https://reviews.llvm.org/D154764) we only re-ordered the fields of
`RecordDecl`s. The change refactored this logic to make sure
`FieldDecl`s are imported before other member decls. However, this
change also widened the types of `DeclContext`s we consider for
re-ordering from `RecordDecl` to anything that's a `DeclContext`. This
seems to have been just a drive-by cleanup.

Internally we've seen numerous crashes in LLDB where we try to perform
this re-ordering on fields of `ObjCInterfaceDecl`s.

This patch restores old behaviour where we limit the re-ordering to just
`RecordDecl`s.

rdar://119343184
rdar://119636274
rdar://119832131
(cherry picked from commit 34dbadd)
justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this pull request Jan 28, 2024
Prior to `e9536698720ec524cc8b72599363622bc1a31558`
(https://reviews.llvm.org/D154764) we only re-ordered the fields of
`RecordDecl`s. The change refactored this logic to make sure
`FieldDecl`s are imported before other member decls. However, this
change also widened the types of `DeclContext`s we consider for
re-ordering from `RecordDecl` to anything that's a `DeclContext`. This
seems to have been just a drive-by cleanup.

Internally we've seen numerous crashes in LLDB where we try to perform
this re-ordering on fields of `ObjCInterfaceDecl`s.

This patch restores old behaviour where we limit the re-ordering to just
`RecordDecl`s.

rdar://119343184
rdar://119636274
rdar://119832131
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants