Handle ObjCEncodeExpr in extractStringLiteralCharacter.
This fixes an assertion failure that occurs later in the function when
an ObjCEncodeExpr is cast to StringLiteral.
rdar://problem/30111207
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@293596 91177308-0d34-0410-b5e6-96231b3b80d8
(cherry picked from commit 3aa1edef05b0ae11b9c2f27a24644e35f0bf72b7)
diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp
index 5fab58a..6a6baf9 100644
--- a/lib/AST/ExprConstant.cpp
+++ b/lib/AST/ExprConstant.cpp
@@ -2362,7 +2362,14 @@
/// Extract the value of a character from a string literal.
static APSInt extractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit,
uint64_t Index) {
- // FIXME: Support ObjCEncodeExpr, MakeStringConstant
+ // FIXME: Support MakeStringConstant
+ if (const auto *ObjCEnc = dyn_cast<ObjCEncodeExpr>(Lit)) {
+ std::string Str;
+ Info.Ctx.getObjCEncodingForType(ObjCEnc->getEncodedType(), Str);
+ assert(Index <= Str.size() && "Index too large");
+ return APSInt::getUnsigned(Str.c_str()[Index]);
+ }
+
if (auto PE = dyn_cast<PredefinedExpr>(Lit))
Lit = PE->getFunctionName();
const StringLiteral *S = cast<StringLiteral>(Lit);
diff --git a/test/CodeGenObjC/encode-test.m b/test/CodeGenObjC/encode-test.m
index 7f0e76f..a1f88e0 100644
--- a/test/CodeGenObjC/encode-test.m
+++ b/test/CodeGenObjC/encode-test.m
@@ -180,3 +180,14 @@
// CHECK: @g15 = constant [2 x i8] c":\00"
const char g15[] = @encode(SEL);
+
+typedef typeof(sizeof(int)) size_t;
+size_t strlen(const char *s);
+
+// CHECK-LABEL: @test_strlen(
+// CHECK: %[[i:.*]] = alloca i32
+// CHECK: store i32 1, i32* %[[i]]
+void test_strlen() {
+ const char array[] = @encode(int);
+ int i = strlen(array);
+}