[clang] [clang] Fix underlying type of EmbedExpr (PR #99050)

Mariya Podchishchaeva via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 18 08:37:43 PDT 2024


https://github.com/Fznamznon updated https://github.com/llvm/llvm-project/pull/99050

>From c089e2519184f029ab569d40facba14351928346 Mon Sep 17 00:00:00 2001
From: "Podchishchaeva, Mariya" <mariya.podchishchaeva at intel.com>
Date: Tue, 16 Jul 2024 05:37:33 -0700
Subject: [PATCH 1/2] [clang] Fix underlying type of EmbedExpr

This patch makes remaining cases of #embed to emit int type since there is an
agreement to do that for C. C++ is being discussed, but in general we
don't want to produce different types for C and C++.
---
 clang/lib/AST/Expr.cpp                    | 2 +-
 clang/lib/Sema/SemaInit.cpp               | 2 +-
 clang/test/Preprocessor/embed_codegen.cpp | 8 ++++----
 3 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 6af1b5683e08b..9d5b8167d0ee6 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -2376,7 +2376,7 @@ APValue SourceLocExpr::EvaluateInContext(const ASTContext &Ctx,
 EmbedExpr::EmbedExpr(const ASTContext &Ctx, SourceLocation Loc,
                      EmbedDataStorage *Data, unsigned Begin,
                      unsigned NumOfElements)
-    : Expr(EmbedExprClass, Ctx.UnsignedCharTy, VK_PRValue, OK_Ordinary),
+    : Expr(EmbedExprClass, Ctx.IntTy, VK_PRValue, OK_Ordinary),
       EmbedKeywordLoc(Loc), Ctx(&Ctx), Data(Data), Begin(Begin),
       NumOfElements(NumOfElements) {
   setDependence(ExprDependence::None);
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index a27ed02fc73b8..2c963b28aeb2b 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -2007,7 +2007,7 @@ static bool canInitializeArrayWithEmbedDataString(ArrayRef<Expr *> ExprList,
   if (InitType->isArrayType()) {
     const ArrayType *InitArrayType = InitType->getAsArrayTypeUnsafe();
     QualType InitElementTy = InitArrayType->getElementType();
-    QualType EmbedExprElementTy = EE->getType();
+    QualType EmbedExprElementTy = EE->getDataStringLiteral()->getType();
     const bool TypesMatch =
         Context.typesAreCompatible(InitElementTy, EmbedExprElementTy) ||
         (InitElementTy->isCharType() && EmbedExprElementTy->isCharType());
diff --git a/clang/test/Preprocessor/embed_codegen.cpp b/clang/test/Preprocessor/embed_codegen.cpp
index 201bf300bc669..fdbf4f2aa400f 100644
--- a/clang/test/Preprocessor/embed_codegen.cpp
+++ b/clang/test/Preprocessor/embed_codegen.cpp
@@ -13,9 +13,9 @@ int ca[] = {
 };
 
 // CHECK: %arrayinit.element = getelementptr inbounds i32, ptr %notca, i64 1
-// CHECK: store i8 106, ptr %arrayinit.element, align 4
+// CHECK: store i32 106, ptr %arrayinit.element, align 4
 // CHECK: %arrayinit.element1 = getelementptr inbounds i32, ptr %notca, i64 2
-// CHECK: store i8 107, ptr %arrayinit.element1, align 4
+// CHECK: store i32 107, ptr %arrayinit.element1, align 4
 int notca[] = {
 a
 #embed <jk.txt> prefix(,)
@@ -74,9 +74,9 @@ constexpr struct T t[] = {
 // CHECK:  %arrayinit.element7 = getelementptr inbounds %struct.T, ptr %tnonc, i64 1
 // CHECK:  call void @llvm.memset.p0.i64(ptr align 4 %arrayinit.element7, i8 0, i64 20, i1 false)
 // CHECK:  %arr8 = getelementptr inbounds %struct.T, ptr %arrayinit.element7, i32 0, i32 0
-// CHECK:  store i8 106, ptr %arr8, align 4
+// CHECK:  store i32 106, ptr %arr8, align 4
 // CHECK:  %arrayinit.element9 = getelementptr inbounds i32, ptr %arr8, i64 1
-// CHECK:  store i8 107, ptr %arrayinit.element9, align 4
+// CHECK:  store i32 107, ptr %arrayinit.element9, align 4
 struct T tnonc[] = {
   a, 300, 1, 2, 3
 #embed <jk.txt> prefix(,)

>From 8798f21342e22a1f6955ef3cb65396a5d7ff1d07 Mon Sep 17 00:00:00 2001
From: "Podchishchaeva, Mariya" <mariya.podchishchaeva at intel.com>
Date: Thu, 18 Jul 2024 08:37:14 -0700
Subject: [PATCH 2/2] Add test cases

---
 clang/test/Preprocessor/Inputs/big_char.txt |  1 +
 clang/test/Preprocessor/embed_weird.cpp     | 11 +++++++++++
 2 files changed, 12 insertions(+)
 create mode 100644 clang/test/Preprocessor/Inputs/big_char.txt

diff --git a/clang/test/Preprocessor/Inputs/big_char.txt b/clang/test/Preprocessor/Inputs/big_char.txt
new file mode 100644
index 0000000000000..ce542efaa5124
--- /dev/null
+++ b/clang/test/Preprocessor/Inputs/big_char.txt
@@ -0,0 +1 @@
+�
\ No newline at end of file
diff --git a/clang/test/Preprocessor/embed_weird.cpp b/clang/test/Preprocessor/embed_weird.cpp
index cc73a88e5a657..6eb2923152f15 100644
--- a/clang/test/Preprocessor/embed_weird.cpp
+++ b/clang/test/Preprocessor/embed_weird.cpp
@@ -115,3 +115,14 @@ void f1() {
   };
 }
 #endif
+
+struct HasChar {
+  signed char ch;
+};
+
+constexpr struct HasChar c = {
+#embed "Inputs/big_char.txt" // cxx-error {{constant expression evaluates to 255 which cannot be narrowed to type 'signed char'}} \
+                                cxx-note {{insert an explicit cast to silence this issue}} \
+                                c-error {{constexpr initializer evaluates to 255 which is not exactly representable in type 'signed char'}}
+
+};



More information about the cfe-commits mailing list