[cfe-commits] r150066 - in /cfe/trunk: include/clang/Basic/DiagnosticASTKinds.td lib/AST/ExprConstant.cpp test/CXX/expr/expr.const/p2-0x.cpp test/SemaCXX/constant-expression-cxx11.cpp

Richard Smith richard-llvm at metafoo.co.uk
Wed Feb 8 00:11:33 PST 2012


Author: rsmith
Date: Wed Feb  8 02:11:33 2012
New Revision: 150066

URL: http://llvm.org/viewvc/llvm-project?rev=150066&view=rev
Log:
Implement DR1458: Taking the address of an object of incomplete class type is
not a constant expression, because we can't tell whether the complete class type
will have an overloaded operator&.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticASTKinds.td
    cfe/trunk/lib/AST/ExprConstant.cpp
    cfe/trunk/test/CXX/expr/expr.const/p2-0x.cpp
    cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticASTKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticASTKinds.td?rev=150066&r1=150065&r2=150066&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticASTKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticASTKinds.td Wed Feb  8 02:11:33 2012
@@ -55,6 +55,9 @@
   "specifiers (%1 vs %3) has unspecified value">;
 def note_constexpr_compare_virtual_mem_ptr : Note<
   "comparison of pointer to virtual member function %0 has unspecified value">;
+def note_constexpr_addr_of_incomplete : Note<
+  "cannot take address of object of incomplete class type %0 "
+  "in a constant expression">;
 def note_constexpr_past_end : Note<
   "dereferenced pointer past the end of %select{|subobject of }0"
   "%select{temporary|%2}1 is not a constant expression">;

Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=150066&r1=150065&r2=150066&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Wed Feb  8 02:11:33 2012
@@ -2937,6 +2937,18 @@
 }
 
 bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
+  QualType SrcTy = E->getSubExpr()->getType();
+  // In C++, taking the address of an object of incomplete class type has
+  // undefined behavior if the complete class type has an overloaded operator&.
+  // DR1458 makes such expressions non-constant.
+  if (Info.getLangOpts().CPlusPlus &&
+      SrcTy->isRecordType() && SrcTy->isIncompleteType()) {
+    const RecordType *RT = SrcTy->getAs<RecordType>();
+    Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_addr_of_incomplete, 1)
+      << SrcTy;
+    Info.Note(RT->getDecl()->getLocation(), diag::note_forward_declaration)
+      << RT->getDecl();
+  }
   return EvaluateLValue(E->getSubExpr(), Result, Info);
 }
 

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=150066&r1=150065&r2=150066&view=diff
==============================================================================
--- cfe/trunk/test/CXX/expr/expr.const/p2-0x.cpp (original)
+++ cfe/trunk/test/CXX/expr/expr.const/p2-0x.cpp Wed Feb  8 02:11:33 2012
@@ -109,6 +109,22 @@
   };
 }
 
+// DR1458: taking the address of an object of incomplete class type
+namespace IncompleteClassTypeAddr {
+  struct S; // expected-note {{forward}}
+  extern S s;
+  constexpr S *p = &s; // expected-error {{constant expression}} expected-note {{cannot take address of object of incomplete class type 'IncompleteClassTypeAddr::S' in a constant expression}}
+
+  extern S sArr[];
+  constexpr S (*p2)[] = &sArr; // ok
+
+  struct S {
+    constexpr S *operator&() { return nullptr; }
+  };
+  constexpr S *q = &s;
+  static_assert(!q, "");
+}
+
 // - an operation that would have undefined behavior [Note: including, for
 //   example, signed integer overflow (Clause 5 [expr]), certain pointer
 //   arithmetic (5.7 [expr.add]), division by zero (5.6 [expr.mul]), or certain

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=150066&r1=150065&r2=150066&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp (original)
+++ cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp Wed Feb  8 02:11:33 2012
@@ -299,6 +299,7 @@
 constexpr S* sptr = &s;
 constexpr bool dyncast = sptr == dynamic_cast<S*>(sptr); // expected-error {{constant expression}} expected-note {{dynamic_cast}}
 
+struct U {};
 struct Str {
   int a : dynamic_cast<S*>(sptr) == dynamic_cast<S*>(sptr); // \
     expected-warning {{not an integral constant expression}} \
@@ -315,7 +316,7 @@
   int e : (Str*)(sptr) == (Str*)(sptr); // \
     expected-warning {{not an integral constant expression}} \
     expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
-  int f : &(Str&)(*sptr) == &(Str&)(*sptr); // \
+  int f : &(U&)(*sptr) == &(U&)(*sptr); // \
     expected-warning {{not an integral constant expression}} \
     expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
   int g : (S*)(void*)(sptr) == sptr; // \





More information about the cfe-commits mailing list