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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ Miscellaneous Bug Fixes
Miscellaneous Clang Crashes Fixed
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

- Do not attempt to dump the layout of dependent types or invalid declarations
when ``-fdump-record-layouts-complete`` is passed.
Fixes (`#83684 <https://github.com/llvm/llvm-project/issues/83684>`_).

OpenACC Specific Changes
------------------------

Expand Down
8 changes: 7 additions & 1 deletion clang/lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5042,7 +5042,13 @@ 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 the type is dependent, then we can't compute its layout because there
// is no way for us to know the size or alignment of a dependent type. Also
// ignore declarations marked as invalid since 'getASTRecordLayout()' asserts
// on that.
if (Ctx.getLangOpts().DumpRecordLayoutsComplete && !isDependentType() &&
!isInvalidDecl())
(void)Ctx.getASTRecordLayout(this);
}

Expand Down
6 changes: 6 additions & 0 deletions clang/test/Layout/dump-complete-invalid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// RUN: %clang_cc1 -verify -fsyntax-only -fdump-record-layouts-complete %s

struct Incomplete; // expected-note {{forward declaration}}

// Check we don't crash on trying to print out an invalid declaration.
struct Invalid : Incomplete {}; // expected-error {{base class has incomplete type}}
57 changes: 56 additions & 1 deletion clang/test/Layout/dump-complete.cpp
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,7 +12,62 @@ class c {};

class d;

template <typename>
struct s {
int x;
};

template <typename T>
struct ts {
T x;
};

template <>
struct ts<void> {
float f;
};

void f() {
ts<int> a;
ts<double> b;
ts<void> c;
}

namespace gh83671 {
template <class _Tp, _Tp __v>
struct integral_constant {
static constexpr const _Tp value = __v;
typedef integral_constant type;
};

template <bool _Val>
using _BoolConstant = integral_constant<bool, _Val>;

template <class _Tp, class _Up>
struct is_same : _BoolConstant<__is_same(_Tp, _Up)> {};

template < class _Tp >
class numeric_limits {};

template < class _Tp >
class numeric_limits< const _Tp > : public numeric_limits< _Tp > {};
}

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<void>
// CHECK-NEXT: 0 | float
// 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