From 27f9cf3a5a92f70f043b6cfbc5f3f2ac3a38f182 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Mon, 6 May 2013 00:35:47 +0000 Subject: [PATCH] Fix assert if __extension__ or _Generic is used when initializing a char array from a string literal. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@181174 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaInit.cpp | 15 +++++++++++---- test/Sema/array-init.c | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/Sema/SemaInit.cpp b/lib/Sema/SemaInit.cpp index 08536fbc21..993e68bd67 100644 --- a/lib/Sema/SemaInit.cpp +++ b/lib/Sema/SemaInit.cpp @@ -85,12 +85,19 @@ static Expr *IsStringInit(Expr *init, QualType declType, ASTContext &Context) { /// Update the type of a string literal, including any surrounding parentheses, /// to match the type of the object which it is initializing. static void updateStringLiteralType(Expr *E, QualType Ty) { - while (ParenExpr *PE = dyn_cast(E)) { + while (true) { E->setType(Ty); - E = PE->getSubExpr(); + if (isa(E) || isa(E)) + break; + else if (ParenExpr *PE = dyn_cast(E)) + E = PE->getSubExpr(); + else if (UnaryOperator *UO = dyn_cast(E)) + E = UO->getSubExpr(); + else if (GenericSelectionExpr *GSE = dyn_cast(E)) + E = GSE->getResultExpr(); + else + llvm_unreachable("unexpected expr in string literal init"); } - assert(isa(E) || isa(E)); - E->setType(Ty); } static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, diff --git a/test/Sema/array-init.c b/test/Sema/array-init.c index b3cae60495..f92852f341 100644 --- a/test/Sema/array-init.c +++ b/test/Sema/array-init.c @@ -187,7 +187,7 @@ char r6[sizeof r5 == 15 ? 1 : -1]; const char r7[] = "zxcv"; char r8[5] = "5char"; char r9[5] = "6chars"; //expected-warning{{initializer-string for char array is too long}} - +unsigned char r10[] = __extension__ (_Generic(0, int: (__extension__ "foo" ))); int r11[0] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}} // Some struct tests -- 2.40.0