Skip to content

Commit e4882d8

Browse files
authored
[Clang] [Sema] Do not crash on dependent or invalid record decls when -fdump-record-layouts-complete is passed (#83688)
We no longer try to compute and dump the layout of types in cases where that isn’t possible. This fixes #83684 and #83671.
1 parent c089fa5 commit e4882d8

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,10 @@ Miscellaneous Bug Fixes
321321
Miscellaneous Clang Crashes Fixed
322322
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
323323

324+
- Do not attempt to dump the layout of dependent types or invalid declarations
325+
when ``-fdump-record-layouts-complete`` is passed.
326+
Fixes (`#83684 <https://github.com/llvm/llvm-project/issues/83684>`_).
327+
324328
OpenACC Specific Changes
325329
------------------------
326330

clang/lib/AST/Decl.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5042,7 +5042,13 @@ void RecordDecl::completeDefinition() {
50425042

50435043
// Layouts are dumped when computed, so if we are dumping for all complete
50445044
// types, we need to force usage to get types that wouldn't be used elsewhere.
5045-
if (Ctx.getLangOpts().DumpRecordLayoutsComplete)
5045+
//
5046+
// If the type is dependent, then we can't compute its layout because there
5047+
// is no way for us to know the size or alignment of a dependent type. Also
5048+
// ignore declarations marked as invalid since 'getASTRecordLayout()' asserts
5049+
// on that.
5050+
if (Ctx.getLangOpts().DumpRecordLayoutsComplete && !isDependentType() &&
5051+
!isInvalidDecl())
50465052
(void)Ctx.getASTRecordLayout(this);
50475053
}
50485054

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %clang_cc1 -verify -fsyntax-only -fdump-record-layouts-complete %s
2+
3+
struct Incomplete; // expected-note {{forward declaration}}
4+
5+
// Check we don't crash on trying to print out an invalid declaration.
6+
struct Invalid : Incomplete {}; // expected-error {{base class has incomplete type}}

clang/test/Layout/dump-complete.cpp

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -emit-llvm-only -fdump-record-layouts-complete %s | FileCheck %s
1+
// RUN: %clang_cc1 -fsyntax-only -fdump-record-layouts-complete %s | FileCheck %s
22

33
struct a {
44
int x;
@@ -12,7 +12,62 @@ class c {};
1212

1313
class d;
1414

15+
template <typename>
16+
struct s {
17+
int x;
18+
};
19+
20+
template <typename T>
21+
struct ts {
22+
T x;
23+
};
24+
25+
template <>
26+
struct ts<void> {
27+
float f;
28+
};
29+
30+
void f() {
31+
ts<int> a;
32+
ts<double> b;
33+
ts<void> c;
34+
}
35+
36+
namespace gh83671 {
37+
template <class _Tp, _Tp __v>
38+
struct integral_constant {
39+
static constexpr const _Tp value = __v;
40+
typedef integral_constant type;
41+
};
42+
43+
template <bool _Val>
44+
using _BoolConstant = integral_constant<bool, _Val>;
45+
46+
template <class _Tp, class _Up>
47+
struct is_same : _BoolConstant<__is_same(_Tp, _Up)> {};
48+
49+
template < class _Tp >
50+
class numeric_limits {};
51+
52+
template < class _Tp >
53+
class numeric_limits< const _Tp > : public numeric_limits< _Tp > {};
54+
}
55+
56+
namespace gh83684 {
57+
template <class Pointer>
58+
struct AllocationResult {
59+
Pointer ptr = nullptr;
60+
int count = 0;
61+
};
62+
}
63+
1564
// CHECK: 0 | struct a
1665
// CHECK: 0 | struct b
1766
// CHECK: 0 | class c
67+
// CHECK: 0 | struct ts<void>
68+
// CHECK-NEXT: 0 | float
69+
// CHECK: 0 | struct ts<int>
70+
// CHECK: 0 | struct ts<double>
1871
// CHECK-NOT: 0 | class d
72+
// CHECK-NOT: 0 | struct s
73+
// CHECK-NOT: 0 | struct AllocationResult

0 commit comments

Comments
 (0)