diff options
author | Douglas Gregor <dgregor@apple.com> | 2008-10-29 00:13:59 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2008-10-29 00:13:59 +0000 |
commit | 8e1cf608dc587c266d32b151a5f209010e29e249 (patch) | |
tree | 177da3462880cff330d46c89ae8c7504eb4add2c /clang/test | |
parent | cc2045534623f86869933b5288bc8dc2745bf92c (diff) | |
download | bcm5719-llvm-8e1cf608dc587c266d32b151a5f209010e29e249.tar.gz bcm5719-llvm-8e1cf608dc587c266d32b151a5f209010e29e249.zip |
Implement initialization of a reference (C++ [dcl.init.ref]) as part
of copy initialization. Other pieces of the puzzle:
- Try/Perform-ImplicitConversion now handles implicit conversions
that don't involve references.
- Try/Perform-CopyInitialization uses
CheckSingleAssignmentConstraints for C. PerformCopyInitialization
is now used for all argument passing and returning values from a
function.
- Diagnose errors with declaring references and const values without
an initializer. (Uses a new Action callback, ActOnUninitializedDecl).
We do not yet have implicit conversion sequences for reference
binding, which means that we don't have any overloading support for
reference parameters yet.
llvm-svn: 58353
Diffstat (limited to 'clang/test')
-rw-r--r-- | clang/test/Parser/cxx-reference.cpp | 8 | ||||
-rw-r--r-- | clang/test/SemaCXX/overload-call.cpp | 11 | ||||
-rw-r--r-- | clang/test/SemaCXX/references.cpp | 60 |
3 files changed, 72 insertions, 7 deletions
diff --git a/clang/test/Parser/cxx-reference.cpp b/clang/test/Parser/cxx-reference.cpp index 4f3b58c5d53..8d65defe7df 100644 --- a/clang/test/Parser/cxx-reference.cpp +++ b/clang/test/Parser/cxx-reference.cpp @@ -3,6 +3,8 @@ extern char *bork; char *& bar = bork; +int val; + void foo(int &a) { } @@ -11,7 +13,7 @@ typedef int & A; void g(const A aref) { } -int & const X; // expected-error {{'const' qualifier may not be applied to a reference}} -int & volatile Y; // expected-error {{'volatile' qualifier may not be applied to a reference}} -int & const volatile Z; /* expected-error {{'const' qualifier may not be applied}} \ +int & const X = val; // expected-error {{'const' qualifier may not be applied to a reference}} +int & volatile Y = val; // expected-error {{'volatile' qualifier may not be applied to a reference}} +int & const volatile Z = val; /* expected-error {{'const' qualifier may not be applied}} \ expected-error {{'volatile' qualifier may not be applied}} */ diff --git a/clang/test/SemaCXX/overload-call.cpp b/clang/test/SemaCXX/overload-call.cpp index 1bd64966128..663474d3282 100644 --- a/clang/test/SemaCXX/overload-call.cpp +++ b/clang/test/SemaCXX/overload-call.cpp @@ -214,3 +214,14 @@ void test_derived(B* b, B const* bc, C* c, const C* cc, void* v, D* d) { char* d8 = derived3(d); } + +// Test overloading of references. +// (FIXME: tests binding to determine candidate sets, not overload +// resolution per se). +int* intref(int&); +float* intref(const int&); + +void intref_test() { + float* ir1 = intref(5); + float* ir2 = intref(5.5); +} diff --git a/clang/test/SemaCXX/references.cpp b/clang/test/SemaCXX/references.cpp index 3637573b445..eebc3e8f7b3 100644 --- a/clang/test/SemaCXX/references.cpp +++ b/clang/test/SemaCXX/references.cpp @@ -1,4 +1,4 @@ -// RUN: clang -fsyntax-only %s +// RUN: clang -fsyntax-only -verify %s int g(int); void f() { @@ -7,10 +7,10 @@ void f() { r = 1; int *p = &r; int &rr = r; - int (&rg)(int) = g; + int (&rg)(int) = g; // expected-warning{{statement was disambiguated as declaration}} rg(i); int a[3]; - int (&ra)[3] = a; + int (&ra)[3] = a; // expected-warning{{statement was disambiguated as declaration}} ra[1] = i; int *Q; int *& P = Q; @@ -24,5 +24,57 @@ void test2() { int c[3]; - int (&rc)[3] = c; + int (&rc)[3] = c; // expected-warning{{statement was disambiguated as declaration}} +} + +// 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() { + double& rd2 = 2.0; // expected-error{{non-const reference to type 'double' cannot be initialized with a temporary of type 'double'}} + int i = 2; + double& rd3 = i; // expected-error{{non-const reference to type 'double' cannot be initialized with a value of type 'int'}} + + const A& rca = fB(); +} + +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{{initialization of reference to type 'int const' with a value of type 'int const volatile' drops qualifiers}} +} + +// C++ [dcl.init.ref]p3 +int& test6(int& x) { + int& yo; // expected-error{{declaration of reference variable 'yo' requires an initializer}} + + + const int val; // expected-error{{declaration of const variable 'val' 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; + +class Test6 { + int& okay; +}; + +struct C : B, A { }; + +void test7(C& c) { + A& a1 = c; // expected-error {{ambiguous conversion from derived class 'struct C' to base class 'struct A':}} } |