From 9392962ef4382490bf0dc2e2c3b5823608d03931 Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Fri, 10 Apr 2015 04:52:06 +0000 Subject: [PATCH] [Sema] Don't assume that an initializer list has an initializer Given something like 'int({}, 1)', we would try to emit a diagnostic regarding the excess element in the scalar initializer. However, we assumed that the initializer list had an element in it. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@234565 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaInit.cpp | 6 +++--- test/SemaCXX/functional-cast.cpp | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/Sema/SemaInit.cpp b/lib/Sema/SemaInit.cpp index 4a6531e506..7392b671e0 100644 --- a/lib/Sema/SemaInit.cpp +++ b/lib/Sema/SemaInit.cpp @@ -6753,9 +6753,9 @@ bool InitializationSequence::Diagnose(Sema &S, case FK_TooManyInitsForScalar: { SourceRange R; - if (InitListExpr *InitList = dyn_cast(Args[0])) - R = SourceRange(InitList->getInit(0)->getLocEnd(), - InitList->getLocEnd()); + auto *InitList = dyn_cast(Args[0]); + if (InitList && InitList->getNumInits() == 1) + R = SourceRange(InitList->getInit(0)->getLocEnd(), InitList->getLocEnd()); else R = SourceRange(Args.front()->getLocEnd(), Args.back()->getLocEnd()); diff --git a/test/SemaCXX/functional-cast.cpp b/test/SemaCXX/functional-cast.cpp index f5ca76c38c..9db95e80d0 100644 --- a/test/SemaCXX/functional-cast.cpp +++ b/test/SemaCXX/functional-cast.cpp @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -fsyntax-only -verify %s +// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s // REQUIRES: LP64 // ------------ not interpreted as C-style cast ------------ @@ -11,7 +11,7 @@ struct InitViaConstructor { InitViaConstructor(int i = 7); }; -struct NoValueInit { // expected-note 2 {{candidate constructor (the implicit copy constructor)}} +struct NoValueInit { // expected-note 2 {{candidate constructor (the implicit copy constructor)}} expected-note 2 {{candidate constructor (the implicit move constructor)}} NoValueInit(int i, int j); // expected-note 2 {{candidate constructor}} }; @@ -25,6 +25,7 @@ void test_cxx_function_cast_multi() { (void)NoValueInit(0, 0); (void)NoValueInit(0, 0, 0); // expected-error{{no matching constructor for initialization}} (void)int(1, 2); // expected-error{{excess elements in scalar initializer}} + (void)int({}, 2); // expected-error{{excess elements in scalar initializer}} } -- 2.40.0