[clang] [AST] Make err_struct_too_large check target-aware (PR #218749)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 25 11:50:22 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Akira Hatanaka (ahatanak)
<details>
<summary>Changes</summary>
ASTContext::getASTRecordLayout used a fixed 1ULL << 60 threshold for err_struct_too_large, regardless of the target's size_t width.
Scale the threshold to the target's size_t width instead, so it is below (1 << 32) on 32-bit architectures. Diagnosing the overflow in Sema avoids the crash in codegen.
rdar://183351516
---
Full diff: https://github.com/llvm/llvm-project/pull/218749.diff
2 Files Affected:
- (modified) clang/lib/AST/RecordLayoutBuilder.cpp (+4-1)
- (modified) clang/test/AST/absurdly_big_struct.cpp (+13-4)
``````````diff
diff --git a/clang/lib/AST/RecordLayoutBuilder.cpp b/clang/lib/AST/RecordLayoutBuilder.cpp
index e6da6c78238c1..45636457b4e8b 100644
--- a/clang/lib/AST/RecordLayoutBuilder.cpp
+++ b/clang/lib/AST/RecordLayoutBuilder.cpp
@@ -3511,7 +3511,10 @@ ASTContext::getASTRecordLayout(const RecordDecl *D) const {
ASTRecordLayouts[D] = NewEntry;
- constexpr uint64_t MaxStructSizeInBytes = 1ULL << 60;
+ // Cap at the target's size_t width (up to 60 bits) so oversized layouts on
+ // narrow targets are diagnosed instead of overflowing size_t in codegen.
+ uint64_t MaxStructSizeInBytes =
+ 1ULL << std::min<unsigned>(getTypeSize(getSizeType()), 60);
CharUnits StructSize = NewEntry->getSize();
if (static_cast<uint64_t>(StructSize.getQuantity()) >= MaxStructSizeInBytes) {
getDiagnostics().Report(D->getLocation(), diag::err_struct_too_large)
diff --git a/clang/test/AST/absurdly_big_struct.cpp b/clang/test/AST/absurdly_big_struct.cpp
index c17274343d57a..2677f7c1e3b60 100644
--- a/clang/test/AST/absurdly_big_struct.cpp
+++ b/clang/test/AST/absurdly_big_struct.cpp
@@ -1,8 +1,9 @@
-// RUN: %clang_cc1 -fsyntax-only -verify %s -triple x86_64-linux-gnu
+// RUN: %clang_cc1 -fsyntax-only -verify=bit64 %s -triple x86_64-linux-gnu
+// RUN: %clang_cc1 -fsyntax-only -verify=bit32 %s -triple armv7-unknown-linux-gnueabi
-struct a { // expected-error {{structure 'a' is too large, which exceeds maximum allowed size of 1152921504606846976 bytes}}
- char x[1ull<<60];
- char x2[1ull<<60];
+struct a { // bit64-error {{structure 'a' is too large, which exceeds maximum allowed size of 1152921504606846976 bytes}}
+ char x[1ull<<60]; // bit32-error {{array is too large}}
+ char x2[1ull<<60]; // bit32-error {{array is too large}}
};
a z[1];
@@ -11,3 +12,11 @@ long long x2() { return sizeof(a::x); }
long long x3() { return sizeof(a::x2); }
long long x4() { return sizeof(z); }
+// On 32-bit architectures, the struct size must be below (1 << 32).
+struct b { // bit32-error {{structure 'b' is too large, which exceeds maximum allowed size of 4294967296 bytes}}
+ char c[0xFFFFFFFF];
+ char c2[1];
+};
+
+long long y() { return sizeof(b); }
+
``````````
</details>
https://github.com/llvm/llvm-project/pull/218749
More information about the cfe-commits
mailing list