[llvm-branch-commits] [clang] release/19.x: [clang] Fix crash when #embed used in a compound literal (#102304) (PR #102428)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Aug 8 00:01:40 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: None (llvmbot)

<details>
<summary>Changes</summary>

Backport 3606d69d0b57dc1d23a4362e376e7ad27f650c27

Requested by: @<!-- -->Fznamznon

---
Full diff: https://github.com/llvm/llvm-project/pull/102428.diff


2 Files Affected:

- (modified) clang/lib/Sema/SemaInit.cpp (+2-2) 
- (added) clang/test/Sema/embed_compound_literal.c (+15) 


``````````diff
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index dc2ba039afe7f..eea4bdfa68b52 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/Sema/embed_compound_literal.c b/clang/test/Sema/embed_compound_literal.c
new file mode 100644
index 0000000000000..7ba6fd8e64968
--- /dev/null
+++ b/clang/test/Sema/embed_compound_literal.c
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -std=c23 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -verify -x c++ -Wno-c23-extensions %s
+// expected-no-diagnostics
+
+char *p1 = (char[]){
+#embed __FILE__
+};
+
+int *p2 = (int[]){
+#embed __FILE__
+};
+
+int *p3 = (int[30]){
+#embed __FILE__ limit(30)
+};

``````````

</details>


https://github.com/llvm/llvm-project/pull/102428


More information about the llvm-branch-commits mailing list