[cfe-commits] r151881 - in /cfe/trunk: lib/AST/Decl.cpp lib/Sema/SemaExpr.cpp test/CXX/expr/expr.const/p2-0x.cpp test/SemaCXX/constant-expression-cxx11.cpp test/SemaCXX/lambda-expressions.cpp test/SemaTemplate/constexpr-instantiate.cpp

Richard Smith richard-llvm at metafoo.co.uk
Thu Mar 1 20:14:40 PST 2012


Author: rsmith
Date: Thu Mar  1 22:14:40 2012
New Revision: 151881

URL: http://llvm.org/viewvc/llvm-project?rev=151881&view=rev
Log:
Ensure that we instantiate static reference data members of class templates
early, since their values can be used in constant expressions in C++11. For
odr-use checking, the opposite change is required, since references are
odr-used whether or not they satisfy the requirements for appearing in a
constant expression.

Modified:
    cfe/trunk/lib/AST/Decl.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/CXX/expr/expr.const/p2-0x.cpp
    cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp
    cfe/trunk/test/SemaCXX/lambda-expressions.cpp
    cfe/trunk/test/SemaTemplate/constexpr-instantiate.cpp

Modified: cfe/trunk/lib/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Decl.cpp?rev=151881&r1=151880&r2=151881&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Thu Mar  1 22:14:40 2012
@@ -1372,11 +1372,18 @@
 bool VarDecl::isUsableInConstantExpressions() const {
   const LangOptions &Lang = getASTContext().getLangOptions();
 
-  // Only const variables can be used in constant expressions in C++. C++98 does
+  if (!Lang.CPlusPlus)
+    return false;
+
+  // In C++11, any variable of reference type can be used in a constant
+  // expression if it is initialized by a constant expression.
+  if (Lang.CPlusPlus0x && getType()->isReferenceType())
+    return true;
+
+  // Only const objects can be used in constant expressions in C++. C++98 does
   // not require the variable to be non-volatile, but we consider this to be a
   // defect.
-  if (!Lang.CPlusPlus ||
-      !getType().isConstQualified() || getType().isVolatileQualified())
+  if (!getType().isConstQualified() || getType().isVolatileQualified())
     return false;
 
   // In C++, const, non-volatile variables of integral or enumeration types
@@ -1384,9 +1391,9 @@
   if (getType()->isIntegralOrEnumerationType())
     return true;
 
-  // Additionally, in C++11, non-volatile constexpr variables and references can
-  // be used in constant expressions.
-  return Lang.CPlusPlus0x && (isConstexpr() || getType()->isReferenceType());
+  // Additionally, in C++11, non-volatile constexpr variables can be used in
+  // constant expressions.
+  return Lang.CPlusPlus0x && isConstexpr();
 }
 
 /// Convert the initializer for this declaration to the elaborated EvaluatedStmt

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=151881&r1=151880&r2=151881&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Thu Mar  1 22:14:40 2012
@@ -10108,9 +10108,10 @@
   // is immediately applied."  We check the first part here, and
   // Sema::UpdateMarkingForLValueToRValue deals with the second part.
   // Note that we use the C++11 definition everywhere because nothing in
-  // C++03 depends on whether we get the C++03 version correct.
+  // C++03 depends on whether we get the C++03 version correct. This does not
+  // apply to references, since they are not objects.
   const VarDecl *DefVD;
-  if (E && !isa<ParmVarDecl>(Var) &&
+  if (E && !isa<ParmVarDecl>(Var) && !Var->getType()->isReferenceType() &&
       Var->isUsableInConstantExpressions() &&
       Var->getAnyInitializer(DefVD) && DefVD->checkInitIsICE())
     SemaRef.MaybeODRUseExprs.insert(E);

Modified: cfe/trunk/test/CXX/expr/expr.const/p2-0x.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/expr/expr.const/p2-0x.cpp?rev=151881&r1=151880&r2=151881&view=diff
==============================================================================
--- cfe/trunk/test/CXX/expr/expr.const/p2-0x.cpp (original)
+++ cfe/trunk/test/CXX/expr/expr.const/p2-0x.cpp Thu Mar  1 22:14:40 2012
@@ -362,8 +362,8 @@
   constexpr int e = 42;
   int &f = const_cast<int&>(e);
   extern int &g;
-  constexpr int &h(); // expected-note 2{{here}}
-  int &i = h(); // expected-note {{here}} expected-note {{undefined function 'h' cannot be used in a constant expression}}
+  constexpr int &h(); // expected-note {{here}}
+  int &i = h(); // expected-note {{here}}
   constexpr int &j() { return b; }
   int &k = j();
 

Modified: cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp?rev=151881&r1=151880&r2=151881&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp (original)
+++ cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp Thu Mar  1 22:14:40 2012
@@ -100,8 +100,8 @@
 }
 
 extern int &Recurse1;
-int &Recurse2 = Recurse1; // expected-note 2{{declared here}} expected-note {{initializer of 'Recurse1' is not a constant expression}}
-int &Recurse1 = Recurse2; // expected-note {{declared here}} expected-note {{initializer of 'Recurse2' is not a constant expression}}
+int &Recurse2 = Recurse1; // expected-note {{declared here}}
+int &Recurse1 = Recurse2;
 constexpr int &Recurse3 = Recurse2; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'Recurse2' is not a constant expression}}
 
 extern const int RecurseA;

Modified: cfe/trunk/test/SemaCXX/lambda-expressions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/lambda-expressions.cpp?rev=151881&r1=151880&r2=151881&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/lambda-expressions.cpp (original)
+++ cfe/trunk/test/SemaCXX/lambda-expressions.cpp Thu Mar  1 22:14:40 2012
@@ -85,6 +85,13 @@
 
     const int h = a; // expected-note {{declared}}
     []() { return h; }; // expected-error {{variable 'h' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}} 
+
+    // The exemption for variables which can appear in constant expressions
+    // applies only to objects (and not to references).
+    // FIXME: This might be a bug in the standard.
+    static int i;
+    constexpr int &ref_i = i; // expected-note {{declared}}
+    [] { return ref_i; }; // expected-error {{variable 'ref_i' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}}
   }
 }
 

Modified: cfe/trunk/test/SemaTemplate/constexpr-instantiate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/constexpr-instantiate.cpp?rev=151881&r1=151880&r2=151881&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/constexpr-instantiate.cpp (original)
+++ cfe/trunk/test/SemaTemplate/constexpr-instantiate.cpp Thu Mar  1 22:14:40 2012
@@ -65,3 +65,13 @@
   constexpr int m = S<int>::k; // ok
   constexpr int o = n; // expected-error {{constant expression}} expected-note {{initializer of 'n'}}
 }
+
+namespace Reference {
+  const int k = 5;
+  template<typename T> struct S {
+    static volatile int &r;
+  };
+  template<typename T> volatile int &S<T>::r = const_cast<volatile int&>(k);
+  constexpr int n = const_cast<int&>(S<int>::r);
+  static_assert(n == 5, "");
+}





More information about the cfe-commits mailing list