]> granicus.if.org Git - clang/commitdiff
Fix reference-binding when we have a reference to const volatile type;
authorDouglas Gregor <dgregor@apple.com>
Fri, 29 Jan 2010 19:39:15 +0000 (19:39 +0000)
committerDouglas Gregor <dgregor@apple.com>
Fri, 29 Jan 2010 19:39:15 +0000 (19:39 +0000)
previously, we were allowing this to bind to a temporary. Now, we
don't; add test-cases and improve diagnostics.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@94831 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaDeclCXX.cpp
lib/Sema/SemaInit.cpp
test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-var.cpp

index af64ccbc5467a141f30a79dcacbce93c4956ac86..7cd8d290510c83bde6bd254e9615c5cbb5cf7d73 100644 (file)
@@ -560,14 +560,14 @@ def err_invalid_initialization : Error<
 def err_lvalue_to_rvalue_ambig_ref : Error<"rvalue reference cannot bind to lvalue "
                                            "due to multiple conversion functions">;
 def err_not_reference_to_const_init : Error<
-  "non-const lvalue reference to type %0 cannot be initialized "
-  "with a %select{value|temporary}1 of type %2">;
+  "%select{non-const|volatile}0 lvalue reference to type %1 cannot be "
+  "initialized with a %select{value|temporary}2 of type %3">;
 def err_lvalue_reference_bind_to_temporary : Error<
-  "non-const lvalue reference to type %0 cannot bind to a temporary of type "
-  "%1">;
+  "%select{non-const|volatile}0 lvalue reference to type %1 cannot bind to a "
+  "temporary of type %2">;
 def err_lvalue_reference_bind_to_unrelated : Error<
-  "non-const lvalue reference to type %0 cannot bind to a value of unrelated "
-  "type %1">;
+  "%select{non-const|volatile}0 lvalue reference to type %1 cannot bind to a "
+  "value of unrelated type %2">;
 def err_reference_bind_drops_quals : Error<
   "binding of reference to type %0 to a value of type %1 drops qualifiers">;
 def err_reference_bind_failed : Error<
index 566e915bc0115fd8740acde7ad1c75ee6b30ec10..130bf4dbdf9aa6e71fe51470815a5ab693df47e2 100644 (file)
@@ -4616,6 +4616,7 @@ Sema::CheckReferenceInit(Expr *&Init, QualType DeclType,
   if (!isRValRef && T1.getCVRQualifiers() != Qualifiers::Const) {
     if (!ICS)
       Diag(DeclLoc, diag::err_not_reference_to_const_init)
+        << T1.isVolatileQualified()
         << T1 << int(InitLvalue != Expr::LV_Valid)
         << T2 << Init->getSourceRange();
     return true;
index 1a588094cd21968af57d1133e67cdf1bd81d5b2c..910d3f6469e56b640e894868b01191e541d57a90 100644 (file)
@@ -2369,7 +2369,7 @@ static void TryReferenceInitialization(Sema &S,
   //       non-volatile const type (i.e., cv1 shall be const), or the reference
   //       shall be an rvalue reference and the initializer expression shall 
   //       be an rvalue.
-  if (!((isLValueRef && T1Quals.hasConst()) ||
+  if (!((isLValueRef && T1Quals.hasConst() && !T1Quals.hasVolatile()) ||
         (isRValueRef && InitLvalue != Expr::LV_Valid))) {
     if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty())
       Sequence.SetOverloadFailure(
@@ -3556,6 +3556,7 @@ bool InitializationSequence::Diagnose(Sema &S,
            Failure == FK_NonConstLValueReferenceBindingToTemporary
              ? diag::err_lvalue_reference_bind_to_temporary
              : diag::err_lvalue_reference_bind_to_unrelated)
+      << DestType.getNonReferenceType().isVolatileQualified()
       << DestType.getNonReferenceType()
       << Args[0]->getType()
       << Args[0]->getSourceRange();
index cf3db5175fa4312d754d1b5b83b5c97b9386deea..d9c5d014014a27c3450cf50813de5b8cf5deb22c 100644 (file)
@@ -68,17 +68,23 @@ void bind_lvalue_quals(volatile Base b, volatile Derived d,
   volatile Base &bvr4 = dvc; // expected-error{{binding of reference to type 'struct Base volatile' to a value of type 'struct Derived const volatile' drops qualifiers}}
   
   volatile int &ir = ivc; // expected-error{{binding of reference to type 'int volatile' to a value of type 'int const volatile' drops qualifiers}}
+
+  const volatile Base &bcvr1 = b;
+  const volatile Base &bcvr2 = d;
 }
 
 void bind_lvalue_to_rvalue() {
   Base &br1 = Base(); // expected-error{{non-const lvalue reference to type 'struct Base' cannot bind to a temporary of type 'struct Base'}}
   Base &br2 = Derived(); // expected-error{{non-const lvalue reference to type 'struct Base' cannot bind to a temporary of type 'struct Derived'}}
+  const volatile Base &br3 = Base(); // expected-error{{volatile lvalue reference to type 'struct Base const volatile' cannot bind to a temporary of type 'struct Base'}}
+  const volatile Base &br4 = Derived(); // expected-error{{volatile lvalue reference to type 'struct Base const volatile' cannot bind to a temporary of type 'struct Derived'}}
 
   int &ir = 17; // expected-error{{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}}
 }
 
 void bind_lvalue_to_unrelated(Unrelated ur) {
   Base &br1 = ur; // expected-error{{non-const lvalue reference to type 'struct Base' cannot bind to a value of unrelated type 'struct Unrelated'}}
+  const volatile Base &br2 = ur; // expected-error{{volatile lvalue reference to type 'struct Base const volatile' cannot bind to a value of unrelated type 'struct Unrelated'}}
 }
 
 void bind_lvalue_to_conv_lvalue() {