From f956a49e6da306c9ec3ac805822035043626481c Mon Sep 17 00:00:00 2001 From: Richard Trieu Date: Sat, 16 May 2015 01:27:03 +0000 Subject: [PATCH] When emitting a dropped qualifier error, show which qualifiers are dropped. llvm-svn: 237505 --- .../clang/Basic/DiagnosticSemaKinds.td | 6 ++- clang/lib/Sema/SemaInit.cpp | 13 ++++-- .../dcl.decl/dcl.init/dcl.init.ref/p5-0x.cpp | 2 +- .../dcl.decl/dcl.init/dcl.init.ref/p5-var.cpp | 14 +++--- .../expr/expr.prim/expr.prim.lambda/p16.cpp | 8 ++-- .../CXX/temp/temp.arg/temp.arg.nontype/p5.cpp | 2 +- clang/test/Misc/diag-template-diffing.cpp | 2 +- clang/test/SemaCXX/builtins-arm.cpp | 2 +- .../err_reference_bind_drops_quals.cpp | 43 +++++++++++++++++++ clang/test/SemaCXX/increment-decrement.cpp | 4 +- clang/test/SemaCXX/references.cpp | 2 +- 11 files changed, 75 insertions(+), 23 deletions(-) create mode 100644 clang/test/SemaCXX/err_reference_bind_drops_quals.cpp diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index bc52d61d55c7..052c93ebdd63 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -1490,8 +1490,10 @@ def err_lvalue_reference_bind_to_unrelated : Error< "%diff{to type $ cannot bind to a value of unrelated type $|" "cannot bind to a value of unrelated type}1,2">; def err_reference_bind_drops_quals : Error< - "binding value %diff{of type $ to reference of type $ drops qualifiers|" - "to reference drops qualifiers}0,1">; + "binding value %diff{of type $ to reference of type $|to reference}0,1 " + "drops %select{<>|'const'|'restrict'|'const' and 'restrict'|" + "'volatile'|'const' and 'volatile'|'restrict' and 'volatile'|" + "'const', 'restrict', and 'volatile'}2 qualifier%plural{1:|2:|4:|:s}2">; def err_reference_bind_failed : Error< "reference %diff{to type $ could not bind to an %select{rvalue|lvalue}1 of " "type $|could not bind to %select{rvalue|lvalue}1 of incompatible type}0,2">; diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp index 32570c717571..7dd93e8de589 100644 --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -6852,12 +6852,19 @@ bool InitializationSequence::Diagnose(Sema &S, << Args[0]->getSourceRange(); break; - case FK_ReferenceInitDropsQualifiers: + case FK_ReferenceInitDropsQualifiers: { + QualType SourceType = Args[0]->getType(); + QualType NonRefType = DestType.getNonReferenceType(); + Qualifiers DroppedQualifiers = + SourceType.getQualifiers() - NonRefType.getQualifiers(); + S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) - << Args[0]->getType() - << DestType.getNonReferenceType() + << SourceType + << NonRefType + << DroppedQualifiers.getCVRQualifiers() << Args[0]->getSourceRange(); break; + } case FK_ReferenceInitFailed: S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) diff --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-0x.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-0x.cpp index b872281b05a5..a187713cb5a8 100644 --- a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-0x.cpp +++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-0x.cpp @@ -123,7 +123,7 @@ namespace std_example_2 { const double& rcd2 = 2; double&& rrd = 2; const volatile int cvi = 1; - const int& r2 = cvi; // expected-error{{binding value of type 'const volatile int' to reference of type 'const int' drops qualifiers}} + const int& r2 = cvi; // expected-error{{binding value of type 'const volatile int' to reference of type 'const int' drops 'volatile' qualifier}} double d; double&& rrd2 = d; // expected-error{{rvalue reference to type 'double' cannot bind to lvalue of type 'double'}} diff --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-var.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-var.cpp index 36fcf9042576..9397bbd7a064 100644 --- a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-var.cpp +++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-var.cpp @@ -52,8 +52,8 @@ void bind_lvalue_to_lvalue(Base b, Derived d, Base &br2 = d; Derived &dr1 = d; Derived &dr2 = b; // expected-error{{non-const lvalue reference to type 'Derived' cannot bind to a value of unrelated type 'Base'}} - Base &br3 = bc; // expected-error{{drops qualifiers}} - Base &br4 = dc; // expected-error{{drops qualifiers}} + Base &br3 = bc; // expected-error{{drops 'const' qualifier}} + Base &br4 = dc; // expected-error{{drops 'const' qualifier}} Base &br5 = diamond; // expected-error{{ambiguous conversion from derived class 'Diamond' to base class 'Base':}} int &ir = i; long &lr = i; // expected-error{{non-const lvalue reference to type 'long' cannot bind to a value of unrelated type 'int'}} @@ -64,10 +64,10 @@ void bind_lvalue_quals(volatile Base b, volatile Derived d, volatile const int ivc) { volatile Base &bvr1 = b; volatile Base &bvr2 = d; - volatile Base &bvr3 = bvc; // expected-error{{binding value of type 'const volatile Base' to reference of type 'volatile Base' drops qualifiers}} - volatile Base &bvr4 = dvc; // expected-error{{binding value of type 'const volatile Derived' to reference of type 'volatile Base' drops qualifiers}} + volatile Base &bvr3 = bvc; // expected-error{{binding value of type 'const volatile Base' to reference of type 'volatile Base' drops 'const' qualifier}} + volatile Base &bvr4 = dvc; // expected-error{{binding value of type 'const volatile Derived' to reference of type 'volatile Base' drops 'const' qualifier}} - volatile int &ir = ivc; // expected-error{{binding value of type 'const volatile int' to reference of type 'volatile int' drops qualifiers}} + volatile int &ir = ivc; // expected-error{{binding value of type 'const volatile int' to reference of type 'volatile int' drops 'const' qualifier}} const volatile Base &bcvr1 = b; const volatile Base &bcvr2 = d; @@ -118,8 +118,8 @@ void bind_const_lvalue_to_rvalue() { const Base &br3 = create(); const Base &br4 = create(); - const Base &br5 = create(); // expected-error{{binding value of type 'const volatile Base' to reference of type 'const Base' drops qualifiers}} - const Base &br6 = create(); // expected-error{{binding value of type 'const volatile Derived' to reference of type 'const Base' drops qualifiers}} + const Base &br5 = create(); // expected-error{{binding value of type 'const volatile Base' to reference of type 'const Base' drops 'volatile' qualifier}} + const Base &br6 = create(); // expected-error{{binding value of type 'const volatile Derived' to reference of type 'const Base' drops 'volatile' qualifier}} const int &ir = create(); } diff --git a/clang/test/CXX/expr/expr.prim/expr.prim.lambda/p16.cpp b/clang/test/CXX/expr/expr.prim/expr.prim.lambda/p16.cpp index 0cbffe844e76..3f350bdbb87b 100644 --- a/clang/test/CXX/expr/expr.prim/expr.prim.lambda/p16.cpp +++ b/clang/test/CXX/expr/expr.prim/expr.prim.lambda/p16.cpp @@ -24,16 +24,16 @@ void test_capture(X x) { int a; [=]{ [&] { - int &x = a; // expected-error{{binding value of type 'const int' to reference of type 'int' drops qualifiers}} - int &x2 = a; // expected-error{{binding value of type 'const int' to reference of type 'int' drops qualifiers}} + int &x = a; // expected-error{{binding value of type 'const int' to reference of type 'int' drops 'const' qualifier}} + int &x2 = a; // expected-error{{binding value of type 'const int' to reference of type 'int' drops 'const' qualifier}} }(); }(); [=]{ [&a] { [&] { - int &x = a; // expected-error{{binding value of type 'const int' to reference of type 'int' drops qualifiers}} - int &x2 = a; // expected-error{{binding value of type 'const int' to reference of type 'int' drops qualifiers}} + int &x = a; // expected-error{{binding value of type 'const int' to reference of type 'int' drops 'const' qualifier}} + int &x2 = a; // expected-error{{binding value of type 'const int' to reference of type 'int' drops 'const' qualifier}} }(); }(); }(); diff --git a/clang/test/CXX/temp/temp.arg/temp.arg.nontype/p5.cpp b/clang/test/CXX/temp/temp.arg/temp.arg.nontype/p5.cpp index e87153b9efe2..249563e7d9bf 100644 --- a/clang/test/CXX/temp/temp.arg/temp.arg.nontype/p5.cpp +++ b/clang/test/CXX/temp/temp.arg/temp.arg.nontype/p5.cpp @@ -114,7 +114,7 @@ namespace reference_parameters { } template void bind() { - T &ref2 = ref; // expected-error{{drops qualifiers}} + T &ref2 = ref; // expected-error{{drops 'const' qualifier}} } int counter; diff --git a/clang/test/Misc/diag-template-diffing.cpp b/clang/test/Misc/diag-template-diffing.cpp index 367de4b8a741..f345535ea1c5 100644 --- a/clang/test/Misc/diag-template-diffing.cpp +++ b/clang/test/Misc/diag-template-diffing.cpp @@ -1258,7 +1258,7 @@ using T = condition<(is_const())>; void foo(const T &t) { T &t2 = t; } -// CHECK-ELIDE-NOTREE: binding value of type 'const condition<[...]>' to reference of type 'condition<[...]>' drops qualifiers +// CHECK-ELIDE-NOTREE: binding value of type 'const condition<[...]>' to reference of type 'condition<[...]>' drops 'const' qualifier } namespace BoolArgumentBitExtended { diff --git a/clang/test/SemaCXX/builtins-arm.cpp b/clang/test/SemaCXX/builtins-arm.cpp index 683317dbbe65..81502794a948 100644 --- a/clang/test/SemaCXX/builtins-arm.cpp +++ b/clang/test/SemaCXX/builtins-arm.cpp @@ -2,5 +2,5 @@ // va_list on ARM AAPCS is struct { void* __ap }. int test1(const __builtin_va_list &ap) { - return __builtin_va_arg(ap, int); // expected-error {{binding value of type 'const __builtin_va_list' to reference of type '__builtin_va_list' drops qualifiers}} + return __builtin_va_arg(ap, int); // expected-error {{binding value of type 'const __builtin_va_list' to reference of type '__builtin_va_list' drops 'const' qualifier}} } diff --git a/clang/test/SemaCXX/err_reference_bind_drops_quals.cpp b/clang/test/SemaCXX/err_reference_bind_drops_quals.cpp new file mode 100644 index 000000000000..afdd8327f936 --- /dev/null +++ b/clang/test/SemaCXX/err_reference_bind_drops_quals.cpp @@ -0,0 +1,43 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +#define restrict __restrict__ +typedef int* ptr; +void test1(ptr p, const ptr cp, restrict ptr rp, const restrict ptr crp, + volatile ptr vp, const volatile ptr cvp, restrict volatile ptr rvp, + const restrict volatile ptr crvp) { + ptr& p1 = p; + ptr& p2 = cp; // expected-error {{drops 'const' qualifier}} + ptr& p3 = rp; // expected-error {{drops 'restrict' qualifier}} + ptr& p4 = crp; // expected-error {{drops 'const' and 'restrict' qualifiers}} + ptr& p5 = vp; // expected-error {{drops 'volatile' qualifier}} + ptr& p6 = cvp; // expected-error {{drops 'const' and 'volatile' qualifiers}} + ptr& p7 = rvp; // expected-error {{drops 'restrict' and 'volatile' qualifiers}} + ptr& p8 = crvp; // expected-error {{drops 'const', 'restrict', and 'volatile' qualifiers}} + + const ptr& cp1 = p; + const ptr& cp2 = cp; + const ptr& cp3 = rp; // expected-error {{drops 'restrict' qualifier}} + const ptr& cp4 = crp; // expected-error {{drops 'restrict' qualifier}} + const ptr& cp5 = vp; // expected-error {{drops 'volatile' qualifier}} + const ptr& cp6 = cvp; // expected-error {{drops 'volatile' qualifier}} + const ptr& cp7 = rvp; // expected-error {{drops 'restrict' and 'volatile' qualifiers}} + const ptr& cp8 = crvp; // expected-error {{drops 'restrict' and 'volatile' qualifiers}} + + const volatile ptr& cvp1 = p; + const volatile ptr& cvp2 = cp; + const volatile ptr& cvp3 = rp; // expected-error {{drops 'restrict' qualifier}} + const volatile ptr& cvp4 = crp; // expected-error {{drops 'restrict' qualifier}} + const volatile ptr& cvp5 = vp; + const volatile ptr& cvp6 = cvp; + const volatile ptr& cvp7 = rvp; // expected-error {{drops 'restrict' qualifier}} + const volatile ptr& cvp8 = crvp; // expected-error {{drops 'restrict' qualifier}} + + const restrict volatile ptr& crvp1 = p; + const restrict volatile ptr& crvp2 = cp; + const restrict volatile ptr& crvp3 = rp; + const restrict volatile ptr& crvp4 = crp; + const restrict volatile ptr& crvp5 = vp; + const restrict volatile ptr& crvp6 = cvp; + const restrict volatile ptr& crvp7 = rvp; + const restrict volatile ptr& crvp8 = crvp; +} diff --git a/clang/test/SemaCXX/increment-decrement.cpp b/clang/test/SemaCXX/increment-decrement.cpp index 11b7d1e12f4e..4f131d76f046 100644 --- a/clang/test/SemaCXX/increment-decrement.cpp +++ b/clang/test/SemaCXX/increment-decrement.cpp @@ -5,8 +5,8 @@ volatile int i; const int &inc = i++; const int &dec = i--; -const int &incfail = ++i; // expected-error {{drops qualifiers}} -const int &decfail = --i; // expected-error {{drops qualifiers}} +const int &incfail = ++i; // expected-error {{drops 'volatile' qualifier}} +const int &decfail = --i; // expected-error {{drops 'volatile' qualifier}} // PR7794 void f0(int e) { diff --git a/clang/test/SemaCXX/references.cpp b/clang/test/SemaCXX/references.cpp index 64d87bd132b5..aae4930593a2 100644 --- a/clang/test/SemaCXX/references.cpp +++ b/clang/test/SemaCXX/references.cpp @@ -54,7 +54,7 @@ void test4() { void test5() { // const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0 const volatile int cvi = 1; - const int& r = cvi; // expected-error{{binding value of type 'const volatile int' to reference of type 'const int' drops qualifiers}} + const int& r = cvi; // expected-error{{binding value of type 'const volatile int' to reference of type 'const int' drops 'volatile' qualifier}} } // C++ [dcl.init.ref]p3