[cfe-commits] r146955 - in /cfe/trunk: include/clang/Sema/Sema.h lib/Parse/ParseExpr.cpp lib/Sema/SemaDeclCXX.cpp lib/Sema/SemaExpr.cpp lib/Sema/SemaExprMember.cpp lib/Sema/SemaTemplateInstantiateDecl.cpp lib/Sema/SemaType.cpp lib/Sema/TreeTransform.h test/CXX/expr/expr.prim/expr.prim.general/p12-0x.cpp test/CodeGenCXX/vla.cpp test/SemaCXX/PR10177.cpp
Richard Smith
richard-llvm at metafoo.co.uk
Mon Dec 19 18:08:33 PST 2011
Author: rsmith
Date: Mon Dec 19 20:08:33 2011
New Revision: 146955
URL: http://llvm.org/viewvc/llvm-project?rev=146955&view=rev
Log:
Unlike in C++03, a constant-expression is not an unevaluated operand in C++11.
Split out a new ExpressionEvaluationContext flag for this case, and don't treat
it as unevaluated in C++11. This fixes some crash-on-invalids where we would
allow references to class members in potentially-evaluated constant expressions
in static member functions, and also fixes half of PR10177.
The fix to PR10177 exposed a case where template instantiation failed to provide
a source location for a diagnostic, so TreeTransform has been tweaked to supply
source locations when transforming a type. The source location is still not very
good, but MarkDeclarationsReferencedInType would need to operate on a TypeLoc to
improve it further.
Also fix MarkDeclarationReferenced in C++98 mode to trigger instantiation for
static data members of class templates which are used in constant expressions.
This fixes a link-time problem, but we still incorrectly treat the member as
non-constant. The rest of the fix for that issue is blocked on PCH support for
early-instantiated static data members, which will be added in a subsequent
patch.
Added:
cfe/trunk/test/SemaCXX/PR10177.cpp
Modified:
cfe/trunk/include/clang/Sema/Sema.h
cfe/trunk/lib/Parse/ParseExpr.cpp
cfe/trunk/lib/Sema/SemaDeclCXX.cpp
cfe/trunk/lib/Sema/SemaExpr.cpp
cfe/trunk/lib/Sema/SemaExprMember.cpp
cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
cfe/trunk/lib/Sema/SemaType.cpp
cfe/trunk/lib/Sema/TreeTransform.h
cfe/trunk/test/CXX/expr/expr.prim/expr.prim.general/p12-0x.cpp
cfe/trunk/test/CodeGenCXX/vla.cpp
Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=146955&r1=146954&r2=146955&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Mon Dec 19 20:08:33 2011
@@ -512,12 +512,17 @@
/// evaluated at run-time, if at all.
enum ExpressionEvaluationContext {
/// \brief The current expression and its subexpressions occur within an
- /// unevaluated operand (C++0x [expr]p8), such as a constant expression
- /// or the subexpression of \c sizeof, where the type or the value of the
- /// expression may be significant but no code will be generated to evaluate
- /// the value of the expression at run time.
+ /// unevaluated operand (C++11 [expr]p7), such as the subexpression of
+ /// \c sizeof, where the type of the expression may be significant but
+ /// no code will be generated to evaluate the value of the expression at
+ /// run time.
Unevaluated,
+ /// \brief The current expression and its subexpressions occur within a
+ /// constant expression. Such a context is not potentially-evaluated in
+ /// C++98, but is potentially-evaluated in C++11.
+ ConstantEvaluated,
+
/// \brief The current expression is potentially evaluated at run time,
/// which means that code may be generated to evaluate the value of the
/// expression at run time.
Modified: cfe/trunk/lib/Parse/ParseExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseExpr.cpp?rev=146955&r1=146954&r2=146955&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseExpr.cpp (original)
+++ cfe/trunk/lib/Parse/ParseExpr.cpp Mon Dec 19 20:08:33 2011
@@ -247,11 +247,12 @@
ExprResult Parser::ParseConstantExpression() {
- // C++ [basic.def.odr]p2:
+ // C++03 [basic.def.odr]p2:
// An expression is potentially evaluated unless it appears where an
// integral constant expression is required (see 5.19) [...].
+ // C++98 and C++11 have no such rule, but this is only a defect in C++98.
EnterExpressionEvaluationContext Unevaluated(Actions,
- Sema::Unevaluated);
+ Sema::ConstantEvaluated);
ExprResult LHS(ParseCastExpression(false));
return ParseRHSOfBinaryExpression(LHS, prec::Conditional);
@@ -1041,7 +1042,7 @@
if (T.expectAndConsume(diag::err_expected_lparen_after, "noexcept"))
return ExprError();
- // C++ [expr.unary.noexcept]p1:
+ // C++11 [expr.unary.noexcept]p1:
// The noexcept operator determines whether the evaluation of its operand,
// which is an unevaluated operand, can throw an exception.
EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
@@ -1439,8 +1440,8 @@
// [...] The operand is either an expression, which is an unevaluated
// operand (Clause 5) [...]
//
- // The GNU typeof and alignof extensions also behave as unevaluated
- // operands.
+ // The GNU typeof and GNU/C++11 alignof extensions also behave as
+ // unevaluated operands.
EnterExpressionEvaluationContext Unevaluated(Actions,
Sema::Unevaluated);
Operand = ParseCastExpression(true/*isUnaryExpression*/);
@@ -1456,8 +1457,8 @@
// [...] The operand is either an expression, which is an unevaluated
// operand (Clause 5) [...]
//
- // The GNU typeof and alignof extensions also behave as unevaluated
- // operands.
+ // The GNU typeof and GNU/C++11 alignof extensions also behave as
+ // unevaluated operands.
EnterExpressionEvaluationContext Unevaluated(Actions,
Sema::Unevaluated);
Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=146955&r1=146954&r2=146955&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Mon Dec 19 20:08:33 2011
@@ -10515,7 +10515,8 @@
// not have a vtable.
if (!Class->isDynamicClass() || Class->isDependentContext() ||
CurContext->isDependentContext() ||
- ExprEvalContexts.back().Context == Unevaluated)
+ ExprEvalContexts.back().Context == Unevaluated ||
+ ExprEvalContexts.back().Context == ConstantEvaluated)
return;
// Try to insert this class into the map.
Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=146955&r1=146954&r2=146955&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Mon Dec 19 20:08:33 2011
@@ -1199,7 +1199,8 @@
VarDecl *var, DeclContext *DC) {
switch (S.ExprEvalContexts.back().Context) {
case Sema::Unevaluated:
- // The argument will never be evaluated, so don't complain.
+ case Sema::ConstantEvaluated:
+ // The argument will never be evaluated at runtime, so don't complain.
return CR_NoCapture;
case Sema::PotentiallyEvaluated:
@@ -9330,7 +9331,7 @@
// temporaries that we may have created as part of the evaluation of
// the expression in that context: they aren't relevant because they
// will never be constructed.
- if (Rec.Context == Unevaluated) {
+ if (Rec.Context == Unevaluated || Rec.Context == ConstantEvaluated) {
ExprCleanupObjects.erase(ExprCleanupObjects.begin() + Rec.NumCleanupObjects,
ExprCleanupObjects.end());
ExprNeedsCleanups = Rec.ParentNeedsCleanups;
@@ -9392,6 +9393,16 @@
// We are in an expression that is not potentially evaluated; do nothing.
return;
+ case ConstantEvaluated:
+ // We are in an expression that will be evaluated during translation; in
+ // C++11, we need to define any functions which are used in case they're
+ // constexpr, whereas in C++98, we only need to define static data members
+ // of class templates.
+ if (!getLangOptions().CPlusPlus ||
+ (!getLangOptions().CPlusPlus0x && !isa<VarDecl>(D)))
+ return;
+ break;
+
case PotentiallyEvaluated:
// We are in a potentially-evaluated expression, so this declaration is
// "used"; handle this below.
@@ -9663,6 +9674,10 @@
// The argument will never be evaluated, so don't complain.
break;
+ case ConstantEvaluated:
+ // Relevant diagnostics should be produced by constant evaluation.
+ break;
+
case PotentiallyEvaluated:
case PotentiallyEvaluatedIfUsed:
if (Statement && getCurFunctionOrMethodDecl()) {
Modified: cfe/trunk/lib/Sema/SemaExprMember.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprMember.cpp?rev=146955&r1=146954&r2=146955&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprMember.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprMember.cpp Mon Dec 19 20:08:33 2011
@@ -140,7 +140,7 @@
return IMA_Mixed_StaticContext;
if (SemaRef.getLangOptions().CPlusPlus0x && hasField) {
- // C++0x [expr.prim.general]p10:
+ // C++11 [expr.prim.general]p12:
// An id-expression that denotes a non-static data member or non-static
// member function of a class can only be used:
// (...)
Modified: cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp?rev=146955&r1=146954&r2=146955&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp Mon Dec 19 20:08:33 2011
@@ -66,17 +66,16 @@
// FIXME: This should be generalized to more than just the AlignedAttr.
if (const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr)) {
if (Aligned->isAlignmentDependent()) {
- // The alignment expression is not potentially evaluated.
- EnterExpressionEvaluationContext Unevaluated(*this,
- Sema::Unevaluated);
-
if (Aligned->isAlignmentExpr()) {
+ // The alignment expression is a constant expression.
+ EnterExpressionEvaluationContext Unevaluated(*this,
+ Sema::ConstantEvaluated);
+
ExprResult Result = SubstExpr(Aligned->getAlignmentExpr(),
TemplateArgs);
if (!Result.isInvalid())
AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>());
- }
- else {
+ } else {
TypeSourceInfo *Result = SubstType(Aligned->getAlignmentType(),
TemplateArgs,
Aligned->getLocation(),
@@ -380,7 +379,7 @@
// We already have an initializer in the class.
} else if (D->getInit()) {
if (Var->isStaticDataMember() && !D->isOutOfLine())
- SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
+ SemaRef.PushExpressionEvaluationContext(Sema::ConstantEvaluated);
else
SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
@@ -462,8 +461,9 @@
if (Invalid)
BitWidth = 0;
else if (BitWidth) {
- // The bit-width expression is not potentially evaluated.
- EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
+ // The bit-width expression is a constant expression.
+ EnterExpressionEvaluationContext Unevaluated(SemaRef,
+ Sema::ConstantEvaluated);
ExprResult InstantiatedBitWidth
= SemaRef.SubstExpr(BitWidth, TemplateArgs);
@@ -591,8 +591,9 @@
Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
Expr *AssertExpr = D->getAssertExpr();
- // The expression in a static assertion is not potentially evaluated.
- EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
+ // The expression in a static assertion is a constant expression.
+ EnterExpressionEvaluationContext Unevaluated(SemaRef,
+ Sema::ConstantEvaluated);
ExprResult InstantiatedAssertExpr
= SemaRef.SubstExpr(AssertExpr, TemplateArgs);
@@ -653,9 +654,9 @@
// The specified value for the enumerator.
ExprResult Value = SemaRef.Owned((Expr *)0);
if (Expr *UninstValue = EC->getInitExpr()) {
- // The enumerator's value expression is not potentially evaluated.
+ // The enumerator's value expression is a constant expression.
EnterExpressionEvaluationContext Unevaluated(SemaRef,
- Sema::Unevaluated);
+ Sema::ConstantEvaluated);
Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
}
@@ -2321,11 +2322,12 @@
}
Expr *NoexceptExpr = 0;
if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) {
- EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
+ EnterExpressionEvaluationContext Unevaluated(SemaRef,
+ Sema::ConstantEvaluated);
ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs);
if (E.isUsable())
E = SemaRef.CheckBooleanCondition(E.get(), E.get()->getLocStart());
-
+
if (E.isUsable()) {
SourceLocation ErrLoc;
llvm::APSInt NoexceptVal;
Modified: cfe/trunk/lib/Sema/SemaType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaType.cpp?rev=146955&r1=146954&r2=146955&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaType.cpp (original)
+++ cfe/trunk/lib/Sema/SemaType.cpp Mon Dec 19 20:08:33 2011
@@ -1055,7 +1055,8 @@
// If we are in an unevaluated context, like sizeof, assume ExplicitNone and
// don't give error.
- } else if (S.ExprEvalContexts.back().Context == Sema::Unevaluated) {
+ } else if (S.ExprEvalContexts.back().Context == Sema::Unevaluated ||
+ S.ExprEvalContexts.back().Context == Sema::ConstantEvaluated) {
implicitLifetime = Qualifiers::OCL_ExplicitNone;
// If that failed, give an error and recover using __autoreleasing.
Modified: cfe/trunk/lib/Sema/TreeTransform.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/TreeTransform.h?rev=146955&r1=146954&r2=146955&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/TreeTransform.h (original)
+++ cfe/trunk/lib/Sema/TreeTransform.h Mon Dec 19 20:08:33 2011
@@ -2898,7 +2898,7 @@
Expr *SourceExpr = Input.getSourceDeclExpression();
if (SourceExpr) {
EnterExpressionEvaluationContext Unevaluated(getSema(),
- Sema::Unevaluated);
+ Sema::ConstantEvaluated);
ExprResult E = getDerived().TransformExpr(SourceExpr);
SourceExpr = (E.isInvalid() ? 0 : E.take());
}
@@ -2932,9 +2932,9 @@
llvm_unreachable("Caller should expand pack expansions");
case TemplateArgument::Expression: {
- // Template argument expressions are not potentially evaluated.
+ // Template argument expressions are constant expressions.
EnterExpressionEvaluationContext Unevaluated(getSema(),
- Sema::Unevaluated);
+ Sema::ConstantEvaluated);
Expr *InputExpr = Input.getSourceExpression();
if (!InputExpr) InputExpr = Input.getArgument().getAsExpr();
@@ -3182,6 +3182,9 @@
template<typename Derived>
TypeSourceInfo *TreeTransform<Derived>::TransformType(TypeSourceInfo *DI) {
+ // Refine the base location to the type's location.
+ TemporaryBase Rebase(*this, DI->getTypeLoc().getBeginLoc(),
+ getDerived().getBaseEntity());
if (getDerived().AlreadyTransformed(DI->getType()))
return DI;
@@ -3595,7 +3598,8 @@
Expr *Size = TL.getSizeExpr();
if (Size) {
- EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
+ EnterExpressionEvaluationContext Unevaluated(SemaRef,
+ Sema::ConstantEvaluated);
Size = getDerived().TransformExpr(Size).template takeAs<Expr>();
}
NewTL.setSizeExpr(Size);
@@ -3640,9 +3644,6 @@
if (ElementType.isNull())
return QualType();
- // Array bounds are not potentially evaluated contexts
- EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
-
ExprResult SizeResult
= getDerived().TransformExpr(T->getSizeExpr());
if (SizeResult.isInvalid())
@@ -3680,8 +3681,9 @@
if (ElementType.isNull())
return QualType();
- // Array bounds are not potentially evaluated contexts
- EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
+ // Array bounds are constant expressions.
+ EnterExpressionEvaluationContext Unevaluated(SemaRef,
+ Sema::ConstantEvaluated);
// Prefer the expression from the TypeLoc; the other may have been uniqued.
Expr *origSize = TL.getSizeExpr();
@@ -3728,8 +3730,9 @@
if (ElementType.isNull())
return QualType();
- // Vector sizes are not potentially evaluated contexts
- EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
+ // Vector sizes are constant expressions.
+ EnterExpressionEvaluationContext Unevaluated(SemaRef,
+ Sema::ConstantEvaluated);
ExprResult Size = getDerived().TransformExpr(T->getSizeExpr());
if (Size.isInvalid())
@@ -6913,9 +6916,6 @@
E->getLocEnd());
}
- // We don't know whether the expression is potentially evaluated until
- // after we perform semantic analysis, so the expression is potentially
- // potentially evaluated.
EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
ExprResult SubExpr = getDerived().TransformExpr(E->getExprOperand());
Modified: cfe/trunk/test/CXX/expr/expr.prim/expr.prim.general/p12-0x.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/expr/expr.prim/expr.prim.general/p12-0x.cpp?rev=146955&r1=146954&r2=146955&view=diff
==============================================================================
--- cfe/trunk/test/CXX/expr/expr.prim/expr.prim.general/p12-0x.cpp (original)
+++ cfe/trunk/test/CXX/expr/expr.prim/expr.prim.general/p12-0x.cpp Mon Dec 19 20:08:33 2011
@@ -10,3 +10,12 @@
int i = sizeof(S::m); // ok
int j = sizeof(S::m + 42); // ok
+
+
+struct T {
+ int n;
+ static void f() {
+ int a[n]; // expected-error {{invalid use of member 'n' in static member function}}
+ int b[sizeof n]; // ok
+ }
+};
Modified: cfe/trunk/test/CodeGenCXX/vla.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/vla.cpp?rev=146955&r1=146954&r2=146955&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/vla.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/vla.cpp Mon Dec 19 20:08:33 2011
@@ -1,5 +1,19 @@
// RUN: %clang_cc1 -triple x86_64-apple-darwin %s -emit-llvm -o - | FileCheck %s
+template<typename T>
+struct S {
+ static int n;
+};
+template<typename T> int S<T>::n = 5;
+
+int f() {
+ // Make sure that the reference here is enough to trigger the instantiation of
+ // the static data member.
+ // CHECK: @_ZN1SIiE1nE = weak_odr global i32 5
+ int a[S<int>::n];
+ return sizeof a;
+}
+
// rdar://problem/9506377
void test0(void *array, int n) {
// CHECK: define void @_Z5test0Pvi(
@@ -40,4 +54,3 @@
// CHECK-NEXT: ret void
}
-
Added: cfe/trunk/test/SemaCXX/PR10177.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/PR10177.cpp?rev=146955&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/PR10177.cpp (added)
+++ cfe/trunk/test/SemaCXX/PR10177.cpp Mon Dec 19 20:08:33 2011
@@ -0,0 +1,40 @@
+// RUN: %clang_cc1 -fsyntax-only -std=c++11 -verify %s
+
+template<typename T, typename U, U> using alias_ref = T;
+template<typename T, typename U, U> void func_ref() {}
+template<typename T, typename U, U> struct class_ref {};
+
+template<int N>
+struct U {
+ static int a;
+};
+
+template<int N> struct S; // expected-note 2{{here}}
+
+template<int N>
+int U<N>::a = S<N>::kError; // expected-error 2{{undefined}}
+
+template<typename T>
+void f() {
+ // FIXME: The standard suggests that U<0>::a is odr-used by this expression,
+ // but it's not entirely clear that's the right behaviour.
+ (void)alias_ref<int, int&, U<0>::a>();
+ (void)func_ref<int, int&, U<1>::a>(); // expected-note {{here}}
+ (void)class_ref<int, int&, U<2>::a>(); // expected-note {{here}}
+};
+
+int main() {
+ f<int>(); // expected-note 2{{here}}
+}
+
+namespace N {
+ template<typename T> struct S { static int n; };
+ template<typename T> int S<T>::n = 5;
+ void g(int*);
+ template<typename T> int f() {
+ int k[S<T>::n];
+ g(k);
+ return k[3];
+ }
+ int j = f<int>();
+}
More information about the cfe-commits
mailing list