Skip to content

[Clang] [Sema] Do not attempt to dump the layout of dependent types when -fdump-record-layouts-complete is passed #83688

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
merged 6 commits into from
Mar 4, 2024

Conversation

Sirraide
Copy link
Member

@Sirraide Sirraide commented Mar 2, 2024

We were crashing on trying to print the layout of an uninstantiated template, which obviously doesn’t make sense.

This fixes #83684.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 2, 2024
@llvmbot
Copy link
Member

llvmbot commented Mar 2, 2024

@llvm/pr-subscribers-clang

Author: None (Sirraide)

Changes

We were crashing on trying to print the layout of an uninstantiated template, which obviously doesn’t make sense.

This fixes #83684.


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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+4)
  • (modified) clang/lib/AST/Decl.cpp (+1-1)
  • (modified) clang/test/Layout/dump-complete.cpp (+28-1)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f44fef28b9f17f..69cf0cb643e6aa 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -308,6 +308,10 @@ Miscellaneous Bug Fixes
 Miscellaneous Clang Crashes Fixed
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
+- Do not attempt to dump the layout of dependent types when ``-fdump-record-layouts-complete``
+  is passed.
+  Fixes (`#83684 <https://github.com/llvm/llvm-project/issues/83684>`_).
+
 OpenACC Specific Changes
 ------------------------
 
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 5d6bb72a208a1a..d069cd65732310 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -5042,7 +5042,7 @@ void RecordDecl::completeDefinition() {
 
   // Layouts are dumped when computed, so if we are dumping for all complete
   // types, we need to force usage to get types that wouldn't be used elsewhere.
-  if (Ctx.getLangOpts().DumpRecordLayoutsComplete)
+  if (Ctx.getLangOpts().DumpRecordLayoutsComplete && !isDependentType())
     (void)Ctx.getASTRecordLayout(this);
 }
 
diff --git a/clang/test/Layout/dump-complete.cpp b/clang/test/Layout/dump-complete.cpp
index 9ccbf477c7052e..0a91d3329e6921 100644
--- a/clang/test/Layout/dump-complete.cpp
+++ b/clang/test/Layout/dump-complete.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -emit-llvm-only -fdump-record-layouts-complete %s | FileCheck %s
+// RUN: %clang_cc1 -fsyntax-only -fdump-record-layouts-complete %s | FileCheck %s
 
 struct a {
   int x;
@@ -12,7 +12,34 @@ class c {};
 
 class d;
 
+template <typename>
+struct s {
+  int x;
+};
+
+template <typename T>
+struct ts {
+  T x;
+};
+
+void f() {
+  ts<int> a;
+  ts<double> b;
+}
+
+namespace gh83684 {
+template <class Pointer>
+struct AllocationResult {
+  Pointer ptr = nullptr;
+  int count = 0;
+};
+}
+
 // CHECK:          0 | struct a
 // CHECK:          0 | struct b
 // CHECK:          0 | class c
+// CHECK:          0 | struct ts<int>
+// CHECK:          0 | struct ts<double>
 // CHECK-NOT:      0 | class d
+// CHECK-NOT:      0 | struct s
+// CHECK-NOT:      0 | struct AllocationResult

@shafik shafik requested a review from Endilll March 3, 2024 06:52
Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

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

I think this makes sense but I would like another set of eyes.

@Endilll
Copy link
Contributor

Endilll commented Mar 3, 2024

This might also fix #83671

@Sirraide
Copy link
Member Author

Sirraide commented Mar 3, 2024

This might also fix #83671

From what I saw, that’s a different problem, and there’s a different pr for that already, but I’ll double-check.

@Sirraide
Copy link
Member Author

Sirraide commented Mar 3, 2024

Oh, apparently, there’s yet another problem here; it seems we should also check for isInvalidDecl().

@bjrjk
Copy link

bjrjk commented Mar 3, 2024

This might also fix #83671

From what I saw, that’s a different problem, and there’s a different pr for that already, but I’ll double-check.

Seems not same problem, root cause are different.

@Sirraide
Copy link
Member Author

Sirraide commented Mar 3, 2024

This might also fix #83671

Actually, it seems to fix your reduced test case, hmm...

@Sirraide
Copy link
Member Author

Sirraide commented Mar 3, 2024

So yeah, this pr indeed also seems to fix #83671 as well.

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

LGTM!

@Sirraide Sirraide merged commit e4882d8 into llvm:main Mar 4, 2024
@Sirraide Sirraide deleted the 83684 branch March 4, 2024 13:04
@Sirraide
Copy link
Member Author

Sirraide commented Mar 4, 2024

Apparently, buildbot is mad at me (https://lab.llvm.org/buildbot/#/builders/165/builds/49741)?

I’m not entirely sure how a fix that affects only -fdump-record-layouts-complete could have caused this. Is this a spurious failure or due to something unrelated, or is it worth investigating this?

@Endilll
Copy link
Contributor

Endilll commented Mar 4, 2024

Apparently, buildbot is mad at me (https://lab.llvm.org/buildbot/#/builders/165/builds/49741)?

I’m not entirely sure how a fix that affects only -fdump-record-layouts-complete could have caused this. Is this a spurious failure or due to something unrelated, or is it worth investigating this?

That builder seems happy testing main after your commit: https://lab.llvm.org/buildbot/#/builders/165, so I'd write it off as spurious.

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
6 participants