2015-03-12 01:26:37 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -verify %s
|
[clang] Pass the NamedDecl* instead of the DeclarationName into many diagnostics.
Background:
-----------
There are two related argument types which can be sent into a diagnostic to
display the name of an entity: DeclarationName (ak_declarationname) or
NamedDecl* (ak_nameddecl) (there is also ak_identifierinfo for
IdentifierInfo*, but we are not concerned with it here).
A DeclarationName in a diagnostic will just be streamed to the output,
which will directly result in a call to DeclarationName::print.
A NamedDecl* in a diagnostic will also ultimately result in a call to
DeclarationName::print, but with two customisation points along the way:
The first customisation point is NamedDecl::getNameForDiagnostic which is
overloaded by FunctionDecl, ClassTemplateSpecializationDecl and
VarTemplateSpecializationDecl to print the template arguments, if any.
The second customisation point is NamedDecl::printName. By default it just
streams the stored DeclarationName into the output but it can be customised
to provide a user-friendly name for an entity. It is currently overloaded by
DecompositionDecl and MSGuidDecl.
What this patch does:
---------------------
For many diagnostics a DeclarationName is used instead of the NamedDecl*.
This bypasses the two customisation points mentioned above. This patches fix
this for diagnostics in Sema.cpp, SemaCast.cpp, SemaChecking.cpp, SemaDecl.cpp,
SemaDeclAttr.cpp, SemaDecl.cpp, SemaOverload.cpp and SemaStmt.cpp.
I have only modified diagnostics where I could construct a test-case which
demonstrates that the change is appropriate (either with this patch or the next
one).
Reviewed By: erichkeane, aaron.ballman
Differential Revision: https://reviews.llvm.org/D84656
2020-07-28 06:22:21 +08:00
|
|
|
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
|
|
|
|
// RUN: %clang_cc1 -std=c++14 -fsyntax-only -verify %s
|
2007-07-17 11:52:31 +08:00
|
|
|
int g(int);
|
|
|
|
|
|
|
|
void f() {
|
|
|
|
int i;
|
|
|
|
int &r = i;
|
|
|
|
r = 1;
|
|
|
|
int *p = &r;
|
|
|
|
int &rr = r;
|
2008-12-18 00:19:15 +08:00
|
|
|
int (&rg)(int) = g;
|
2007-07-17 11:52:31 +08:00
|
|
|
rg(i);
|
|
|
|
int a[3];
|
2008-12-18 00:19:15 +08:00
|
|
|
int (&ra)[3] = a;
|
2007-07-17 11:52:31 +08:00
|
|
|
ra[1] = i;
|
|
|
|
int *Q;
|
|
|
|
int *& P = Q;
|
|
|
|
P[1] = 1;
|
|
|
|
}
|
2007-09-05 00:49:09 +08:00
|
|
|
|
2007-10-16 10:55:40 +08:00
|
|
|
typedef int t[1];
|
|
|
|
void test2() {
|
|
|
|
t a;
|
|
|
|
t& b = a;
|
|
|
|
|
|
|
|
|
|
|
|
int c[3];
|
2008-12-18 00:19:15 +08:00
|
|
|
int (&rc)[3] = c;
|
2008-10-29 08:13:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// C++ [dcl.init.ref]p5b1
|
|
|
|
struct A { };
|
|
|
|
struct B : A { } b;
|
|
|
|
|
|
|
|
void test3() {
|
|
|
|
double d = 2.0;
|
|
|
|
double& rd = d; // rd refers to d
|
|
|
|
const double& rcd = d; // rcd refers to d
|
|
|
|
|
|
|
|
A& ra = b; // ra refers to A subobject in b
|
|
|
|
const A& rca = b; // rca refers to A subobject in b
|
|
|
|
}
|
|
|
|
|
|
|
|
B fB();
|
|
|
|
|
|
|
|
// C++ [dcl.init.ref]p5b2
|
|
|
|
void test4() {
|
2009-12-10 07:02:17 +08:00
|
|
|
double& rd2 = 2.0; // expected-error{{non-const lvalue reference to type 'double' cannot bind to a temporary of type 'double'}}
|
2008-10-29 08:13:59 +08:00
|
|
|
int i = 2;
|
2009-12-10 07:02:17 +08:00
|
|
|
double& rd3 = i; // expected-error{{non-const lvalue reference to type 'double' cannot bind to a value of unrelated type 'int'}}
|
2008-10-29 08:13:59 +08:00
|
|
|
|
|
|
|
const A& rca = fB();
|
|
|
|
}
|
|
|
|
|
|
|
|
void test5() {
|
2009-12-10 07:02:17 +08:00
|
|
|
// const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0
|
2008-10-29 08:13:59 +08:00
|
|
|
const volatile int cvi = 1;
|
2019-06-21 18:50:02 +08:00
|
|
|
const int& r = cvi; // expected-error{{binding reference of type 'const int' to value of type 'const volatile int' drops 'volatile' qualifier}}
|
2018-07-13 01:43:49 +08:00
|
|
|
|
|
|
|
#if __cplusplus >= 201103L
|
2019-06-21 18:50:02 +08:00
|
|
|
const int& r2{cvi}; // expected-error{{binding reference of type 'const int' to value of type 'const volatile int' drops 'volatile' qualifier}}
|
2018-07-13 01:43:49 +08:00
|
|
|
|
|
|
|
const int a = 2;
|
2019-06-21 18:50:02 +08:00
|
|
|
int& r3{a}; // expected-error{{binding reference of type 'int' to value of type 'const int' drops 'const' qualifier}}
|
2018-07-13 01:43:49 +08:00
|
|
|
|
|
|
|
const int&& r4{a}; // expected-error{{rvalue reference to type 'const int' cannot bind to lvalue of type 'const int'}}
|
|
|
|
|
|
|
|
void func();
|
|
|
|
void func(int);
|
|
|
|
int &ptr1 = {func}; // expected-error{{address of overloaded function 'func' does not match required type 'int'}}
|
|
|
|
int &&ptr2{func}; // expected-error{{address of overloaded function 'func' does not match required type 'int'}}
|
|
|
|
// expected-note@-4{{candidate function}}
|
|
|
|
// expected-note@-4{{candidate function}}
|
|
|
|
// expected-note@-6{{candidate function}}
|
|
|
|
// expected-note@-6{{candidate function}}
|
|
|
|
#endif
|
2008-10-29 08:13:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// C++ [dcl.init.ref]p3
|
|
|
|
int& test6(int& x) {
|
|
|
|
int& yo; // expected-error{{declaration of reference variable 'yo' requires an initializer}}
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
int& not_initialized_error; // expected-error{{declaration of reference variable 'not_initialized_error' requires an initializer}}
|
|
|
|
extern int& not_initialized_okay;
|
|
|
|
|
2010-04-15 08:00:53 +08:00
|
|
|
class Test6 { // expected-warning{{class 'Test6' does not declare any constructor to initialize its non-modifiable members}}
|
|
|
|
int& okay; // expected-note{{reference member 'okay' will never be initialized}}
|
2008-10-29 08:13:59 +08:00
|
|
|
};
|
|
|
|
|
2015-01-19 09:44:02 +08:00
|
|
|
struct C : B, A { }; // expected-warning {{direct base 'A' is inaccessible due to ambiguity:\n struct C -> struct B -> struct A\nstruct C -> struct A}}
|
2008-10-29 08:13:59 +08:00
|
|
|
|
|
|
|
void test7(C& c) {
|
2010-03-10 19:27:22 +08:00
|
|
|
A& a1 = c; // expected-error {{ambiguous conversion from derived class 'C' to base class 'A':}}
|
2007-10-16 10:55:40 +08:00
|
|
|
}
|
2008-11-03 23:51:28 +08:00
|
|
|
|
|
|
|
// C++ [dcl.ref]p1, C++ [dcl.ref]p4
|
|
|
|
void test8(int& const,// expected-error{{'const' qualifier may not be applied to a reference}}
|
|
|
|
|
|
|
|
void&, // expected-error{{cannot form a reference to 'void'}}
|
2008-11-24 05:45:46 +08:00
|
|
|
int& &) // expected-error{{type name declared as a reference to a reference}}
|
2008-11-03 23:51:28 +08:00
|
|
|
{
|
|
|
|
typedef int& intref;
|
|
|
|
typedef intref& intrefref; // C++ DR 106: reference collapsing
|
|
|
|
|
2014-02-19 08:13:27 +08:00
|
|
|
typedef intref const intref_c; // expected-warning {{'const' qualifier on reference type 'intref' (aka 'int &') has no effect}}
|
|
|
|
typedef intref_c intref; // ok, same type
|
|
|
|
|
|
|
|
typedef intref volatile intref; // expected-warning {{'volatile' qualifier on reference type 'intref' (aka 'int &') has no effect}}
|
|
|
|
typedef intref _Atomic intref; // expected-warning {{'_Atomic' qualifier on reference type 'intref' (aka 'int &') has no effect}}
|
|
|
|
|
|
|
|
void restrict_ref(__restrict intref); // ok
|
|
|
|
void restrict_ref(int &__restrict); // ok
|
2008-11-03 23:51:28 +08:00
|
|
|
}
|
2009-09-24 06:51:26 +08:00
|
|
|
|
[clang] Pass the NamedDecl* instead of the DeclarationName into many diagnostics.
Background:
-----------
There are two related argument types which can be sent into a diagnostic to
display the name of an entity: DeclarationName (ak_declarationname) or
NamedDecl* (ak_nameddecl) (there is also ak_identifierinfo for
IdentifierInfo*, but we are not concerned with it here).
A DeclarationName in a diagnostic will just be streamed to the output,
which will directly result in a call to DeclarationName::print.
A NamedDecl* in a diagnostic will also ultimately result in a call to
DeclarationName::print, but with two customisation points along the way:
The first customisation point is NamedDecl::getNameForDiagnostic which is
overloaded by FunctionDecl, ClassTemplateSpecializationDecl and
VarTemplateSpecializationDecl to print the template arguments, if any.
The second customisation point is NamedDecl::printName. By default it just
streams the stored DeclarationName into the output but it can be customised
to provide a user-friendly name for an entity. It is currently overloaded by
DecompositionDecl and MSGuidDecl.
What this patch does:
---------------------
For many diagnostics a DeclarationName is used instead of the NamedDecl*.
This bypasses the two customisation points mentioned above. This patches fix
this for diagnostics in Sema.cpp, SemaCast.cpp, SemaChecking.cpp, SemaDecl.cpp,
SemaDeclAttr.cpp, SemaDecl.cpp, SemaOverload.cpp and SemaStmt.cpp.
I have only modified diagnostics where I could construct a test-case which
demonstrates that the change is appropriate (either with this patch or the next
one).
Reviewed By: erichkeane, aaron.ballman
Differential Revision: https://reviews.llvm.org/D84656
2020-07-28 06:22:21 +08:00
|
|
|
namespace var_template {
|
|
|
|
#if __cplusplus >= 201402L
|
|
|
|
int i;
|
|
|
|
template <typename> int &ref = i; // ok
|
|
|
|
template <> int &ref<float>; // expected-error {{declaration of reference variable 'ref<float>' requires an initializer}}
|
|
|
|
#endif
|
|
|
|
} // namespace var_template
|
|
|
|
|
2014-02-19 08:13:27 +08:00
|
|
|
template<typename T> int const_param(const T) {}
|
|
|
|
int const_ref_param = const_param<int&>(const_ref_param); // no-warning
|
|
|
|
|
2009-09-24 06:51:26 +08:00
|
|
|
|
|
|
|
class string {
|
|
|
|
char *Data;
|
|
|
|
unsigned Length;
|
|
|
|
public:
|
|
|
|
string();
|
|
|
|
~string();
|
|
|
|
};
|
|
|
|
|
|
|
|
string getInput();
|
|
|
|
|
|
|
|
void test9() {
|
|
|
|
string &s = getInput(); // expected-error{{lvalue reference}}
|
|
|
|
}
|
2010-02-01 01:18:49 +08:00
|
|
|
|
|
|
|
void test10() {
|
|
|
|
__attribute((vector_size(16))) typedef int vec4;
|
|
|
|
typedef __attribute__(( ext_vector_type(4) )) int ext_vec4;
|
|
|
|
|
|
|
|
vec4 v;
|
|
|
|
int &a = v[0]; // expected-error{{non-const reference cannot bind to vector element}}
|
|
|
|
const int &b = v[0];
|
|
|
|
|
|
|
|
ext_vec4 ev;
|
|
|
|
int &c = ev.x; // expected-error{{non-const reference cannot bind to vector element}}
|
|
|
|
const int &d = ev.x;
|
|
|
|
}
|
2010-05-18 02:45:21 +08:00
|
|
|
|
|
|
|
namespace PR7149 {
|
|
|
|
template<typename T> struct X0
|
|
|
|
{
|
|
|
|
T& first;
|
|
|
|
X0(T& p1) : first(p1) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void f()
|
|
|
|
{
|
|
|
|
int p1[1];
|
|
|
|
X0< const int[1]> c(p1);
|
|
|
|
}
|
|
|
|
}
|
2010-11-16 08:12:50 +08:00
|
|
|
|
|
|
|
namespace PR8608 {
|
|
|
|
bool& f(unsigned char& c) { return (bool&)c; }
|
|
|
|
}
|
2011-08-23 01:24:56 +08:00
|
|
|
|
|
|
|
// The following crashed trying to recursively evaluate the LValue.
|
2012-08-20 16:52:22 +08:00
|
|
|
const int &do_not_crash = do_not_crash; // expected-warning{{reference 'do_not_crash' is not yet bound to a value when used within its own initialization}}
|
2013-06-13 08:57:57 +08:00
|
|
|
|
|
|
|
namespace ExplicitRefInit {
|
|
|
|
// This is invalid: we can't copy-initialize an 'A' temporary using an
|
|
|
|
// explicit constructor.
|
|
|
|
struct A { explicit A(int); };
|
|
|
|
const A &a(0); // expected-error {{reference to type 'const ExplicitRefInit::A' could not bind to an rvalue of type 'int'}}
|
|
|
|
}
|
2018-04-29 13:33:38 +08:00
|
|
|
|
|
|
|
namespace RefCollapseTypePrinting {
|
|
|
|
template<typename T> void add_lref() {
|
|
|
|
using X = int(T); // expected-note 4{{previous}}
|
|
|
|
using X = const volatile T&;
|
|
|
|
// expected-error@-1 {{'int &' vs 'int (int &)'}}
|
|
|
|
// expected-error@-2 {{'int &' vs 'int (int &&)'}}
|
|
|
|
// expected-error@-3 {{'const int &' vs 'int (const int &)'}}
|
|
|
|
// expected-error@-4 {{'const int &' vs 'int (const int &&)'}}
|
|
|
|
}
|
|
|
|
template void add_lref<int&>(); // expected-note {{instantiation of}}
|
|
|
|
template void add_lref<int&&>(); // expected-note {{instantiation of}}
|
|
|
|
template void add_lref<const int&>(); // expected-note {{instantiation of}}
|
|
|
|
template void add_lref<const int&&>(); // expected-note {{instantiation of}}
|
|
|
|
|
|
|
|
template<typename T> void add_rref() {
|
|
|
|
using X = int(T); // expected-note 4{{previous}}
|
|
|
|
using X = const volatile T&&;
|
|
|
|
// expected-error@-1 {{'int &' vs 'int (int &)'}}
|
|
|
|
// expected-error@-2 {{'int &&' vs 'int (int &&)'}}
|
|
|
|
// expected-error@-3 {{'const int &' vs 'int (const int &)'}}
|
|
|
|
// expected-error@-4 {{'const int &&' vs 'int (const int &&)'}}
|
|
|
|
}
|
|
|
|
template void add_rref<int&>(); // expected-note {{instantiation of}}
|
|
|
|
template void add_rref<int&&>(); // expected-note {{instantiation of}}
|
|
|
|
template void add_rref<const int&>(); // expected-note {{instantiation of}}
|
|
|
|
template void add_rref<const int&&>(); // expected-note {{instantiation of}}
|
|
|
|
}
|
2020-07-08 09:25:57 +08:00
|
|
|
|
|
|
|
namespace PR45521 {
|
|
|
|
struct a { template<class b> a(const b * const&); };
|
|
|
|
int *d;
|
|
|
|
const a &r = d;
|
|
|
|
}
|