summaryrefslogtreecommitdiffstats
path: root/clang/lib/AST/ExprConstant.cpp
diff options
context:
space:
mode:
authorRichard Smith <richard-llvm@metafoo.co.uk>2011-12-22 01:07:19 +0000
committerRichard Smith <richard-llvm@metafoo.co.uk>2011-12-22 01:07:19 +0000
commitca2cfbf5ce7e8ffa2526cd6d1aa2bf7779686c4f (patch)
treef97035489d8c02cf9e3ed1d7131afe7715ea4aab /clang/lib/AST/ExprConstant.cpp
parent4449b2129401417770245c24a3236b95e9f3c1d6 (diff)
downloadbcm5719-llvm-ca2cfbf5ce7e8ffa2526cd6d1aa2bf7779686c4f.tar.gz
bcm5719-llvm-ca2cfbf5ce7e8ffa2526cd6d1aa2bf7779686c4f.zip
PR11637: implement special-case constant evaluation for char arrays initialized
by string literals. llvm-svn: 147120
Diffstat (limited to 'clang/lib/AST/ExprConstant.cpp')
-rw-r--r--clang/lib/AST/ExprConstant.cpp26
1 files changed, 26 insertions, 0 deletions
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index d4828e34183..8e6682bcdee 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -3084,6 +3084,32 @@ bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
if (!CAT)
return Error(E);
+ // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...]
+ // an appropriately-typed string literal enclosed in braces.
+ if (E->getNumInits() == 1 && CAT->getElementType()->isAnyCharacterType() &&
+ Info.Ctx.hasSameUnqualifiedType(E->getType(), E->getInit(0)->getType())) {
+ LValue LV;
+ if (!EvaluateLValue(E->getInit(0), LV, Info))
+ return false;
+ uint64_t NumElements = CAT->getSize().getZExtValue();
+ Result = APValue(APValue::UninitArray(), NumElements, NumElements);
+
+ // Copy the string literal into the array. FIXME: Do this better.
+ LV.Designator.addIndex(0);
+ for (uint64_t I = 0; I < NumElements; ++I) {
+ CCValue Char;
+ if (!HandleLValueToRValueConversion(Info, E->getInit(0),
+ CAT->getElementType(), LV, Char))
+ return false;
+ if (!CheckConstantExpression(Info, E->getInit(0), Char,
+ Result.getArrayInitializedElt(I)))
+ return false;
+ if (!HandleLValueArrayAdjustment(Info, LV, CAT->getElementType(), 1))
+ return false;
+ }
+ return true;
+ }
+
Result = APValue(APValue::UninitArray(), E->getNumInits(),
CAT->getSize().getZExtValue());
LValue Subobject = This;
OpenPOWER on IntegriCloud