[clang] [clang] Fix crash when #embed used in a compound literal (PR #102304)
Mariya Podchishchaeva via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 7 04:06:51 PDT 2024
https://github.com/Fznamznon created https://github.com/llvm/llvm-project/pull/102304
Fixes https://github.com/llvm/llvm-project/issues/102248
>From ebeb9264e8a4e6d57a4573376248191f64ad99e7 Mon Sep 17 00:00:00 2001
From: "Podchishchaeva, Mariya" <mariya.podchishchaeva at intel.com>
Date: Wed, 7 Aug 2024 04:04:09 -0700
Subject: [PATCH] [clang] Fix crash when #embed used in a compound literal
Fixes https://github.com/llvm/llvm-project/issues/102248
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Sema/SemaInit.cpp | 4 ++--
clang/test/Preprocessor/embed_codegen.cpp | 6 ++++++
3 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 829a759ca4a0be..946d09b0215e5b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -167,6 +167,7 @@ Bug Fixes in This Version
be used in C++.
- Fixed a failed assertion when checking required literal types in C context. (#GH101304).
- Fixed a crash when trying to transform a dependent address space type. Fixes #GH101685.
+- Fixed crash when #embed is used in a compound literal Fixes #GH102248.
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 90fd6df782f095..abd4401e029817 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -515,8 +515,8 @@ class InitListChecker {
uint64_t ElsCount = 1;
// Otherwise try to fill whole array with embed data.
if (Entity.getKind() == InitializedEntity::EK_ArrayElement) {
- ValueDecl *ArrDecl = Entity.getParent()->getDecl();
- auto *AType = SemaRef.Context.getAsArrayType(ArrDecl->getType());
+ auto *AType =
+ SemaRef.Context.getAsArrayType(Entity.getParent()->getType());
assert(AType && "expected array type when initializing array");
ElsCount = Embed->getDataElementCount();
if (const auto *CAType = dyn_cast<ConstantArrayType>(AType))
diff --git a/clang/test/Preprocessor/embed_codegen.cpp b/clang/test/Preprocessor/embed_codegen.cpp
index 5baab9b59a9c16..d9624306dc828d 100644
--- a/clang/test/Preprocessor/embed_codegen.cpp
+++ b/clang/test/Preprocessor/embed_codegen.cpp
@@ -1,9 +1,15 @@
// RUN: %clang_cc1 %s -triple x86_64 --embed-dir=%S/Inputs -emit-llvm -o - | FileCheck %s
+// CHECK: @.compoundliteral = internal global [2 x i8] c"jk"
// CHECK: @__const._Z3fooi.ca = private unnamed_addr constant [3 x i32] [i32 0, i32 106, i32 107], align 4
// CHECK: @__const._Z3fooi.sc = private unnamed_addr constant %struct.S1 { i32 106, i32 107, i32 0 }, align 4
// CHECK: @__const._Z3fooi.t = private unnamed_addr constant [3 x %struct.T] [%struct.T { [2 x i32] [i32 48, i32 49], %struct.S1 { i32 50, i32 51, i32 52 } }, %struct.T { [2 x i32] [i32 53, i32 54], %struct.S1 { i32 55, i32 56, i32 57 } }, %struct.T { [2 x i32] [i32 10, i32 0], %struct.S1 zeroinitializer }], align 16
// CHECK: @__const._Z3fooi.W = private unnamed_addr constant %struct.Wrapper { i32 48, %struct.HasCharArray { [10 x i8] c"123456789\0A" } }, align 4
+
+char *p = (char[]){
+#embed "jk.txt"
+};
+
void foo(int a) {
// CHECK: %a.addr = alloca i32, align 4
// CHECK: store i32 %a, ptr %a.addr, align 4
More information about the cfe-commits
mailing list