[clang] [Clang] Fix assertion failure in getASTRecordLayout for invalid declarations (PR #208153)
Aditi Medhane via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 8 00:54:03 PDT 2026
https://github.com/AditiRM updated https://github.com/llvm/llvm-project/pull/208153
>From 78b861d62b432d17881243a6eb3854a7de23258f Mon Sep 17 00:00:00 2001
From: AditiRM <aditimedhane73 at gmail.com>
Date: Wed, 8 Jul 2026 07:09:35 +0000
Subject: [PATCH] [Clang] Fix assertion failure in getASTRecordLayout
---
clang/lib/AST/RecordLayoutBuilder.cpp | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/clang/lib/AST/RecordLayoutBuilder.cpp b/clang/lib/AST/RecordLayoutBuilder.cpp
index 854f88f20b2b6..6b5027772885d 100644
--- a/clang/lib/AST/RecordLayoutBuilder.cpp
+++ b/clang/lib/AST/RecordLayoutBuilder.cpp
@@ -3384,6 +3384,32 @@ ASTContext::getASTRecordLayout(const RecordDecl *D) const {
// not a complete definition (which is what isCompleteDefinition() tests)
// until we *finish* parsing the definition.
D = D->getDefinition();
+
+ // Handle invalid declarations gracefully during error recovery
+ // This can happen when there are template specialization errors
+ if (!D || D->isInvalidDecl() || !D->isCompleteDefinition()) {
+ // Check if we already have a cached layout
+ const ASTRecordLayout *Entry = ASTRecordLayouts[D];
+ if (Entry)
+ return *Entry;
+
+ // Create a minimal safe layout for error recovery
+ // Use 1-byte size and alignment to avoid division by zero or other issues
+ ASTRecordLayout *NewEntry = new (*this)
+ ASTRecordLayout(*this,
+ CharUnits::One(), // Size = 1 byte
+ CharUnits::One(), // Alignment = 1 byte
+ CharUnits::One(), // PreferredAlignment = 1 byte
+ CharUnits::One(), // UnadjustedAlignment = 1 byte
+ CharUnits::One(), // RequiredAlignment = 1 byte
+ CharUnits::One(), // DataSize = 1 byte
+ ArrayRef<uint64_t>() // Empty field offsets
+ );
+
+ ASTRecordLayouts[D] = NewEntry;
+ return *NewEntry;
+ }
+
assert(D && "Cannot get layout of forward declarations!");
assert(!D->isInvalidDecl() && "Cannot get layout of invalid decl!");
assert(D->isCompleteDefinition() && "Cannot layout type before complete!");
More information about the cfe-commits
mailing list