[clang] ea1c01d - PR51105: look through ConstantExpr when looking for a braced string literal initialization.

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 25 11:36:26 PDT 2021


Author: Richard Smith
Date: 2021-08-25T11:36:11-07:00
New Revision: ea1c01dde03b45e0b33d3883147d3b4fb489978c

URL: https://github.com/llvm/llvm-project/commit/ea1c01dde03b45e0b33d3883147d3b4fb489978c
DIFF: https://github.com/llvm/llvm-project/commit/ea1c01dde03b45e0b33d3883147d3b4fb489978c.diff

LOG: PR51105: look through ConstantExpr when looking for a braced string literal initialization.

Added: 
    

Modified: 
    clang/lib/AST/Expr.cpp
    clang/lib/AST/ExprConstant.cpp
    clang/lib/Sema/SemaInit.cpp
    clang/test/CodeGenCXX/const-init.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 11f10d4695fcd..31733932361f3 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -2308,7 +2308,7 @@ bool InitListExpr::isStringLiteralInit() const {
   const Expr *Init = getInit(0);
   if (!Init)
     return false;
-  Init = Init->IgnoreParens();
+  Init = Init->IgnoreParenImpCasts();
   return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
 }
 

diff  --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index f49a144879b3e..a3879db27bd70 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -10473,13 +10473,17 @@ bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E,
   // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...]
   // an appropriately-typed string literal enclosed in braces.
   if (E->isStringLiteralInit()) {
-    auto *SL = dyn_cast<StringLiteral>(E->getInit(0)->IgnoreParens());
+    auto *SL = dyn_cast<StringLiteral>(E->getInit(0)->IgnoreParenImpCasts());
     // FIXME: Support ObjCEncodeExpr here once we support it in
     // ArrayExprEvaluator generally.
     if (!SL)
       return Error(E);
     return VisitStringLiteral(SL, AllocType);
   }
+  // Any other transparent list init will need proper handling of the
+  // AllocType; we can't just recurse to the inner initializer.
+  assert(!E->isTransparent() &&
+         "transparent array list initialization is not string literal init?");
 
   bool Success = true;
 

diff  --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 78574e34d9068..111d93b1e7a4d 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -2899,7 +2899,7 @@ InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity,
     // We're modifying a string literal init; we have to decompose the string
     // so we can modify the individual characters.
     ASTContext &Context = SemaRef.Context;
-    Expr *SubExpr = StructuredList->getInit(0)->IgnoreParens();
+    Expr *SubExpr = StructuredList->getInit(0)->IgnoreParenImpCasts();
 
     // Compute the character type
     QualType CharTy = AT->getElementType();

diff  --git a/clang/test/CodeGenCXX/const-init.cpp b/clang/test/CodeGenCXX/const-init.cpp
index f5c9dae7ba4b6..ca53db02f10a9 100644
--- a/clang/test/CodeGenCXX/const-init.cpp
+++ b/clang/test/CodeGenCXX/const-init.cpp
@@ -84,6 +84,13 @@ int arr[2];
 // CHECK: @pastEnd = constant i32* bitcast (i8* getelementptr (i8, i8* bitcast ([2 x i32]* @arr to i8*), i64 8) to i32*)
 int &pastEnd = arr[2];
 
+// CHECK: @[[WCHAR_STR:.*]] = internal global [2 x i32] [i32 112, i32 0],
+// CHECK: @PR51105_a = global i32* {{.*}} @[[WCHAR_STR]],
+wchar_t *PR51105_a = (wchar_t[2]){ (L"p") };
+// CHECK: @[[CHAR_STR:.*]] = internal global [5 x i8] c"p\00\00\00\00",
+// CHECK: @PR51105_b = global i8* {{.*}} @[[CHAR_STR]],
+char *PR51105_b = (char [5]){ ("p") };
+
 struct X {
   long n : 8;
 };


        


More information about the cfe-commits mailing list