diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp index 848e07974445..92b05344ec6b 100644 --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -185,21 +185,29 @@ static void computeDeclRefDependence(NamedDecl *D, QualType T, // (VD) - a constant with integral or enumeration type and is // initialized with an expression that is value-dependent. + // (VD) - a constant with literal type and is initialized with an + // expression that is value-dependent [C++11]. + // (VD) - FIXME: Missing from the standard: + // - an entity with reference type and is initialized with an + // expression that is value-dependent [C++11] if (VarDecl *Var = dyn_cast(D)) { - if (Var->getType()->isIntegralOrEnumerationType() && - Var->getType().getCVRQualifiers() == Qualifiers::Const) { + if ((D->getASTContext().getLangOptions().CPlusPlus0x ? + Var->getType()->isLiteralType() : + Var->getType()->isIntegralOrEnumerationType()) && + (Var->getType().getCVRQualifiers() == Qualifiers::Const || + Var->getType()->isReferenceType())) { if (const Expr *Init = Var->getAnyInitializer()) if (Init->isValueDependent()) { ValueDependent = true; InstantiationDependent = true; } - } - + } + // (VD) - FIXME: Missing from the standard: // - a member function or a static data member of the current // instantiation - else if (Var->isStaticDataMember() && - Var->getDeclContext()->isDependentContext()) { + if (Var->isStaticDataMember() && + Var->getDeclContext()->isDependentContext()) { ValueDependent = true; InstantiationDependent = true; } @@ -213,8 +221,7 @@ static void computeDeclRefDependence(NamedDecl *D, QualType T, if (isa(D) && D->getDeclContext()->isDependentContext()) { ValueDependent = true; InstantiationDependent = true; - return; - } + } } void DeclRefExpr::computeDependence() { diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index 746b094619ca..d6e263b44764 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -552,7 +552,7 @@ static bool EvaluateVarDeclInit(EvalInfo &Info, const VarDecl *VD, return false; const Expr *Init = VD->getAnyInitializer(); - if (!Init) + if (!Init || Init->isValueDependent()) return false; if (APValue *V = VD->getEvaluatedValue()) { @@ -3640,6 +3640,9 @@ static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) { // A variable of non-volatile const-qualified integral or enumeration // type initialized by an ICE can be used in ICEs. if (const VarDecl *Dcl = dyn_cast(D)) { + if (!Dcl->getType()->isIntegralOrEnumerationType()) + return ICEDiag(2, cast(E)->getLocation()); + // Look for a declaration of this variable that has an initializer. const VarDecl *ID = 0; const Expr *Init = Dcl->getAnyInitializer(ID); diff --git a/clang/test/SemaCXX/constant-expression-cxx11.cpp b/clang/test/SemaCXX/constant-expression-cxx11.cpp index bba949bbd58e..8481c8a6e197 100644 --- a/clang/test/SemaCXX/constant-expression-cxx11.cpp +++ b/clang/test/SemaCXX/constant-expression-cxx11.cpp @@ -322,3 +322,18 @@ static_assert_fold((&zs[0][0][0][2])[-1] == 2, ""); static_assert_fold(**(**(zs + 1) + 1) == 11, ""); } + +namespace DependentValues { + +struct I { int n; typedef I V[10]; }; +I::V x, y; +template struct S { + int k; + void f() { + I::V &cells = B ? x : y; + I &i = cells[k]; + switch (i.n) {} + } +}; + +} diff --git a/clang/test/SemaCXX/constant-expression.cpp b/clang/test/SemaCXX/constant-expression.cpp index e3f45c5ec0f4..2d2f1aee2b46 100644 --- a/clang/test/SemaCXX/constant-expression.cpp +++ b/clang/test/SemaCXX/constant-expression.cpp @@ -95,3 +95,10 @@ void diags(int n) { ; } } + +namespace IntOrEnum { + const int k = 0; + const int &p = k; + template struct S {}; + S

s; // expected-error {{not an integral constant expression}} +}