diff options
Diffstat (limited to 'clang/test/CXX/dcl.decl')
48 files changed, 2079 insertions, 0 deletions
diff --git a/clang/test/CXX/dcl.decl/dcl.fct.def/dcl.fct.def.default/p2.cpp b/clang/test/CXX/dcl.decl/dcl.fct.def/dcl.fct.def.default/p2.cpp new file mode 100644 index 0000000..06dd1bb --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.fct.def/dcl.fct.def.default/p2.cpp @@ -0,0 +1,56 @@ +// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s + +// An explicitly-defaulted function may be declared constexpr only if it would +// have been implicitly declared as constexpr. +struct S1 { + constexpr S1() = default; // expected-error {{defaulted definition of default constructor is not constexpr}} + constexpr S1(const S1&) = default; + constexpr S1(S1&&) = default; + constexpr S1 &operator=(const S1&) = default; // expected-error {{explicitly-defaulted copy assignment operator may not have}} + constexpr S1 &operator=(S1&&) = default; // expected-error {{explicitly-defaulted move assignment operator may not have}} + constexpr ~S1() = default; // expected-error {{destructor cannot be marked constexpr}} + int n; +}; +struct NoCopyMove { + constexpr NoCopyMove() {} + NoCopyMove(const NoCopyMove&); + NoCopyMove(NoCopyMove&&); +}; +struct S2 { + constexpr S2() = default; + constexpr S2(const S2&) = default; // expected-error {{defaulted definition of copy constructor is not constexpr}} + constexpr S2(S2&&) = default; // expected-error {{defaulted definition of move constructor is not constexpr}} + NoCopyMove ncm; +}; + +// If a function is explicitly defaulted on its first declaration +// -- it is implicitly considered to be constexpr if the implicit declaration +// would be +struct S3 { + S3() = default; // expected-note {{here}} + S3(const S3&) = default; + S3(S3&&) = default; + constexpr S3(int n) : n(n) {} + int n; +}; +constexpr S3 s3a = S3(0); +constexpr S3 s3b = s3a; +constexpr S3 s3c = S3(); +constexpr S3 s3d; // expected-error {{constant expression}} expected-note {{non-constexpr constructor}} + +struct S4 { + S4() = default; + S4(const S4&) = default; // expected-note {{here}} + S4(S4&&) = default; // expected-note {{here}} + NoCopyMove ncm; +}; +constexpr S4 s4a; // ok +constexpr S4 s4b = S4(); // expected-error {{constant expression}} expected-note {{non-constexpr constructor}} +constexpr S4 s4c = s4a; // expected-error {{constant expression}} expected-note {{non-constexpr constructor}} + +struct S5 { + constexpr S5(); + int n = 1, m = n + 3; +}; +constexpr S5::S5() = default; +static_assert(S5().m == 4, ""); diff --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.aggr/p1-0x.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.aggr/p1-0x.cpp new file mode 100644 index 0000000..7764980 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.aggr/p1-0x.cpp @@ -0,0 +1,78 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s + +// An aggregate is an array or a class... +struct Aggr { +private: + static const int n; + void f(); +protected: + struct Inner { int m; }; +public: + bool &br; // expected-note {{default constructor of 'Aggr' is implicitly deleted because field 'br' of reference type 'bool &' would not be initialized}} +}; +bool b; +Aggr ag = { b }; + +// with no user-provided constructors, ... +struct NonAggr1a { // expected-note 2 {{candidate constructor}} + NonAggr1a(int, int); // expected-note {{candidate constructor}} + int k; +}; +// In C++0x, 'user-provided' is only defined for special member functions, so +// this type is considered to be an aggregate. This is considered to be +// a language defect. +NonAggr1a na1a = { 42 }; // expected-error {{no matching constructor for initialization of 'NonAggr1a'}} + +struct NonAggr1b { + NonAggr1b(const NonAggr1b &); // expected-note {{candidate constructor}} + int k; +}; +NonAggr1b na1b = { 42 }; // expected-error {{no matching constructor for initialization of 'NonAggr1b'}} + +// no brace-or-equal-initializers for non-static data members, ... +struct NonAggr2 { // expected-note 3 {{candidate constructor}} + int m = { 123 }; +}; +NonAggr2 na2 = { 42 }; // expected-error {{no matching constructor for initialization of 'NonAggr2'}} + +// no private... +struct NonAggr3 { // expected-note 3 {{candidate constructor}} +private: + int n; +}; +NonAggr3 na3 = { 42 }; // expected-error {{no matching constructor for initialization of 'NonAggr3'}} + +// or protected non-static data members, ... +struct NonAggr4 { // expected-note 3 {{candidate constructor}} +protected: + int n; +}; +NonAggr4 na4 = { 42 }; // expected-error {{no matching constructor for initialization of 'NonAggr4'}} + +// no base classes, ... +struct NonAggr5 : Aggr { // expected-note 3 {{candidate constructor}} +}; +NonAggr5 na5 = { b }; // expected-error {{no matching constructor for initialization of 'NonAggr5'}} +template<typename...BaseList> +struct MaybeAggr5a : BaseList... {}; // expected-note {{default constructor of 'MaybeAggr5a<Aggr>' is implicitly deleted because base class 'Aggr' has a deleted default constructor}} +MaybeAggr5a<> ma5a0 = {}; // ok +MaybeAggr5a<Aggr> ma5a1 = {}; // expected-error {{call to implicitly-deleted default constructor of 'MaybeAggr5a<Aggr>'}} + +// and no virtual functions. +struct NonAggr6 { // expected-note 3 {{candidate constructor}} + virtual void f(); + int n; +}; +NonAggr6 na6 = { 42 }; // expected-error {{no matching constructor for initialization of 'NonAggr6'}} + +struct DefaultedAggr { + int n; + + DefaultedAggr() = default; + DefaultedAggr(const DefaultedAggr &) = default; + DefaultedAggr(DefaultedAggr &&) = default; + DefaultedAggr &operator=(const DefaultedAggr &) = default; + DefaultedAggr &operator=(DefaultedAggr &) = default; + ~DefaultedAggr() = default; +}; +DefaultedAggr da = { 42 } ; diff --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.aggr/p4.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.aggr/p4.cpp new file mode 100644 index 0000000..1041571 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.aggr/p4.cpp @@ -0,0 +1,18 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 -pedantic -Werror %s +int a1[] = { 1, 3, 5 }; +void f() { + int a2[] = { 1, 3, 5 }; +} +template <typename T> +void tf() { + T t; + // Element type may be dependent + T a3[] = { 1, 3, 5 }; + // As might be the initializer list, value + int a5[] = { sizeof(T) }; + // or even type. + int a6[] = { t.get() }; +} + +// Allowed by GNU extension +int a4[] = {}; // expected-error {{zero size arrays}} diff --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/basic.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/basic.cpp new file mode 100644 index 0000000..b30e0ec --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/basic.cpp @@ -0,0 +1,17 @@ +// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s + +void f0() { + int &ir = { 17 }; // expected-error{{reference to type 'int' cannot bind to an initializer list}} +} + +namespace PR12453 { + template<typename T> + void f(int i) { + T x{i}; // expected-error{{non-constant-expression cannot be narrowed from type 'int' to 'float' in initializer list}} \ + // expected-note{{override this message by inserting an explicit cast}} + T y{i}; // expected-error{{non-constant-expression cannot be narrowed from type 'int' to 'float' in initializer list}} \ + // expected-note{{override this message by inserting an explicit cast}} + } + + template void f<float>(int); // expected-note{{in instantiation of function template specialization 'PR12453::f<float>' requested here}} +} diff --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x-fixits.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x-fixits.cpp new file mode 100644 index 0000000..0bea4ed --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x-fixits.cpp @@ -0,0 +1,33 @@ +// RUN: %clang_cc1 -fsyntax-only -Wc++11-compat -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s + +// Verify that the appropriate fixits are emitted for narrowing conversions in +// initializer lists. + +typedef short int16_t; + +void fixits() { + int x = 999; + struct {char c;} c2 = {x}; + // CHECK: warning:{{.*}} cannot be narrowed + // CHECK: fix-it:{{.*}}:26}:"static_cast<char>(" + // CHECK: fix-it:{{.*}}:27}:")" + struct {int16_t i;} i16 = {70000}; + // CHECK: warning:{{.*}} cannot be narrowed + // CHECK: fix-it:{{.*}}:30}:"static_cast<int16_t>(" + // CHECK: fix-it:{{.*}}:35}:")" +} + +template<typename T> +void maybe_shrink_int(T t) { + struct {T t;} t2 = {700}; +} + +void test_template() { + maybe_shrink_int((char)3); + // CHECK: warning:{{.*}} cannot be narrowed + // CHECK: note:{{.*}} in instantiation + // CHECK: note:{{.*}} override + // FIXME: This should be static_cast<T>. + // CHECK: fix-it:{{.*}}"static_cast<char>(" + // CHECK: fix-it:{{.*}}")" +} diff --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp new file mode 100644 index 0000000..db20ea6 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp @@ -0,0 +1,209 @@ +// RUN: %clang_cc1 -fsyntax-only -std=c++11 -triple x86_64-apple-macosx10.6.7 -verify %s + +// Verify that narrowing conversions in initializer lists cause errors in C++0x +// mode. + +void std_example() { + int x = 999; // x is not a constant expression + const int y = 999; + const int z = 99; + char c1 = x; // OK, though it might narrow (in this case, it does narrow) + char c2{x}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + char c3{y}; // expected-error {{ cannot be narrowed }} expected-note {{override}} expected-warning {{changes value}} + char c4{z}; // OK: no narrowing needed + unsigned char uc1 = {5}; // OK: no narrowing needed + unsigned char uc2 = {-1}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + unsigned int ui1 = {-1}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + signed int si1 = + { (unsigned int)-1 }; // expected-error {{ cannot be narrowed }} expected-note {{override}} + int ii = {2.0}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + float f1 { x }; // expected-error {{ cannot be narrowed }} expected-note {{override}} + float f2 { 7 }; // OK: 7 can be exactly represented as a float + int f(int); + int a[] = + { 2, f(2), f(2.0) }; // OK: the double-to-int conversion is not at the top level +} + +// Test each rule individually. + +template<typename T> +struct Agg { + T t; +}; + +template<typename T> +struct Convert { + constexpr Convert(T v) : v(v) {} + constexpr operator T() const { return v; } + T v; +}; +template<typename T> Convert<T> ConvertVar(); + +// C++0x [dcl.init.list]p7: A narrowing conversion is an implicit conversion +// +// * from a floating-point type to an integer type, or + +void float_to_int() { + Agg<char> a1 = {1.0F}; // expected-error {{type 'float' cannot be narrowed to 'char'}} expected-note {{override}} + Agg<char> a2 = {1.0}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + Agg<char> a3 = {1.0L}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + + float f = 1.0; + double d = 1.0; + long double ld = 1.0; + Agg<char> a4 = {f}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + Agg<char> a5 = {d}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + Agg<char> a6 = {ld}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + + Agg<char> ce1 = { Convert<float>(1.0) }; // expected-error {{type 'float' cannot be narrowed to 'char'}} expected-note {{override}} + Agg<char> ce2 = { ConvertVar<double>() }; // expected-error {{type 'double' cannot be narrowed to 'char'}} expected-note {{override}} +} + +// * from long double to double or float, or from double to float, except where +// the source is a constant expression and the actual value after conversion +// is within the range of values that can be represented (even if it cannot be +// represented exactly), or + +void shrink_float() { + // These aren't constant expressions. + float f = 1.0; + double d = 1.0; + long double ld = 1.0; + + // Variables. + Agg<float> f1 = {f}; // OK (no-op) + Agg<float> f2 = {d}; // expected-error {{non-constant-expression cannot be narrowed from type 'double' to 'float'}} expected-note {{override}} + Agg<float> f3 = {ld}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + // Exact constants. + Agg<float> f4 = {1.0}; // OK (double constant represented exactly) + Agg<float> f5 = {1.0L}; // OK (long double constant represented exactly) + // Inexact but in-range constants. + Agg<float> f6 = {0.1}; // OK (double constant in range but rounded) + Agg<float> f7 = {0.1L}; // OK (long double constant in range but rounded) + // Out of range constants. + Agg<float> f8 = {1E50}; // expected-error {{constant expression evaluates to 1.000000e+50 which cannot be narrowed to type 'float'}} expected-note {{override}} + Agg<float> f9 = {1E50L}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + // More complex constant expression. + constexpr long double e40 = 1E40L, e30 = 1E30L, e39 = 1E39L; + Agg<float> f10 = {e40 - 5 * e39 + e30 - 5 * e39}; // OK + + // Variables. + Agg<double> d1 = {f}; // OK (widening) + Agg<double> d2 = {d}; // OK (no-op) + Agg<double> d3 = {ld}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + // Exact constant. + Agg<double> d4 = {1.0L}; // OK (long double constant represented exactly) + // Inexact but in-range constant. + Agg<double> d5 = {0.1L}; // OK (long double constant in range but rounded) + // Out of range constant. + Agg<double> d6 = {1E315L}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + // More complex constant expression. + constexpr long double e315 = 1E315L, e305 = 1E305L, e314 = 1E314L; + Agg<double> d7 = {e315 - 5 * e314 + e305 - 5 * e314}; // OK + + Agg<float> ce1 = { Convert<double>(1e300) }; // expected-error {{constant expression evaluates to 1.000000e+300 which cannot be narrowed to type 'float'}} expected-note {{override}} + Agg<double> ce2 = { ConvertVar<long double>() }; // expected-error {{non-constant-expression cannot be narrowed from type 'long double' to 'double'}} expected-note {{override}} +} + +// * from an integer type or unscoped enumeration type to a floating-point type, +// except where the source is a constant expression and the actual value after +// conversion will fit into the target type and will produce the original +// value when converted back to the original type, or +void int_to_float() { + // Not a constant expression. + char c = 1; + + // Variables. Yes, even though all char's will fit into any floating type. + Agg<float> f1 = {c}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + Agg<double> f2 = {c}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + Agg<long double> f3 = {c}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + + // Constants. + Agg<float> f4 = {12345678}; // OK (exactly fits in a float) + Agg<float> f5 = {123456789}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + + Agg<float> ce1 = { Convert<int>(123456789) }; // expected-error {{constant expression evaluates to 123456789 which cannot be narrowed to type 'float'}} expected-note {{override}} + Agg<double> ce2 = { ConvertVar<long long>() }; // expected-error {{non-constant-expression cannot be narrowed from type 'long long' to 'double'}} expected-note {{override}} +} + +// * from an integer type or unscoped enumeration type to an integer type that +// cannot represent all the values of the original type, except where the +// source is a constant expression and the actual value after conversion will +// fit into the target type and will produce the original value when converted +// back to the original type. +void shrink_int() { + // Not a constant expression. + short s = 1; + unsigned short us = 1; + Agg<char> c1 = {s}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + Agg<unsigned short> s1 = {s}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + Agg<short> s2 = {us}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + + // "that cannot represent all the values of the original type" means that the + // validity of the program depends on the relative sizes of integral types. + // This test compiles with -m64, so sizeof(int)<sizeof(long)==sizeof(long + // long). + long l1 = 1; + Agg<int> i1 = {l1}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + long long ll = 1; + Agg<long> l2 = {ll}; // OK + + // Constants. + Agg<char> c2 = {127}; // OK + Agg<char> c3 = {300}; // expected-error {{ cannot be narrowed }} expected-note {{override}} expected-warning {{changes value}} + + Agg<int> i2 = {0x7FFFFFFFU}; // OK + Agg<int> i3 = {0x80000000U}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + Agg<unsigned int> i4 = {-0x80000000L}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + + // Bool is also an integer type, but conversions to it are a different AST + // node. + Agg<bool> b1 = {0}; // OK + Agg<bool> b2 = {1}; // OK + Agg<bool> b3 = {-1}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + + // Conversions from pointers to booleans aren't narrowing conversions. + Agg<bool> b = {&b1}; // OK + + Agg<short> ce1 = { Convert<int>(100000) }; // expected-error {{constant expression evaluates to 100000 which cannot be narrowed to type 'short'}} expected-note {{override}} expected-warning {{changes value from 100000 to -31072}} + Agg<char> ce2 = { ConvertVar<short>() }; // expected-error {{non-constant-expression cannot be narrowed from type 'short' to 'char'}} expected-note {{override}} +} + +// Be sure that type- and value-dependent expressions in templates get the error +// too. + +template<int I, typename T> +void maybe_shrink_int(T t) { + Agg<short> s1 = {t}; // expected-error {{ cannot be narrowed }} expected-note {{override}} + Agg<short> s2 = {I}; // expected-error {{ cannot be narrowed }} expected-note {{override}} expected-warning {{changes value}} + Agg<T> t2 = {700}; // expected-error {{ cannot be narrowed }} expected-note {{override}} expected-warning {{changes value}} +} + +void test_template() { + maybe_shrink_int<15>((int)3); // expected-note {{in instantiation}} + maybe_shrink_int<70000>((char)3); // expected-note {{in instantiation}} +} + + +// We don't want qualifiers on the types in the diagnostic. + +void test_qualifiers(int i) { + const int j = i; + struct {const unsigned char c;} c1 = {j}; // expected-error {{from type 'int' to 'unsigned char' in}} expected-note {{override}} + // Template arguments make it harder to avoid printing qualifiers: + Agg<const unsigned char> c2 = {j}; // expected-error {{from type 'int' to 'const unsigned char' in}} expected-note {{override}} +} + +// Test SFINAE checks. +template<unsigned> struct Value { }; + +template<typename T> +int &check_narrowed(Value<sizeof((T){1.1})>); + +template<typename T> +float &check_narrowed(...); + +void test_narrowed(Value<sizeof(int)> vi, Value<sizeof(double)> vd) { + int &ir = check_narrowed<double>(vd); + float &fr = check_narrowed<int>(vi); +} diff --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-cxx11-nowarn.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-cxx11-nowarn.cpp new file mode 100644 index 0000000..4bcf113 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-cxx11-nowarn.cpp @@ -0,0 +1,210 @@ +// RUN: %clang_cc1 -fsyntax-only -std=c++11 -Wno-error=c++11-narrowing -triple x86_64-apple-macosx10.6.7 -verify %s +// RUN: %clang_cc1 -fsyntax-only -std=c++11 -Wno-error=narrowing -triple x86_64-apple-macosx10.6.7 -verify %s + +// Verify that narrowing conversions in initializer lists cause errors in C++0x +// mode. + +void std_example() { + int x = 999; // x is not a constant expression + const int y = 999; + const int z = 99; + char c1 = x; // OK, though it might narrow (in this case, it does narrow) + char c2{x}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + char c3{y}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} expected-warning {{changes value}} + char c4{z}; // OK: no narrowing needed + unsigned char uc1 = {5}; // OK: no narrowing needed + unsigned char uc2 = {-1}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + unsigned int ui1 = {-1}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + signed int si1 = + { (unsigned int)-1 }; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + int ii = {2.0}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + float f1 { x }; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + float f2 { 7 }; // OK: 7 can be exactly represented as a float + int f(int); + int a[] = + { 2, f(2), f(2.0) }; // OK: the double-to-int conversion is not at the top level +} + +// Test each rule individually. + +template<typename T> +struct Agg { + T t; +}; + +template<typename T> +struct Convert { + constexpr Convert(T v) : v(v) {} + constexpr operator T() const { return v; } + T v; +}; +template<typename T> Convert<T> ConvertVar(); + +// C++0x [dcl.init.list]p7: A narrowing conversion is an implicit conversion +// +// * from a floating-point type to an integer type, or + +void float_to_int() { + Agg<char> a1 = {1.0F}; // expected-warning {{type 'float' cannot be narrowed to 'char'}} expected-note {{override}} + Agg<char> a2 = {1.0}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + Agg<char> a3 = {1.0L}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + + float f = 1.0; + double d = 1.0; + long double ld = 1.0; + Agg<char> a4 = {f}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + Agg<char> a5 = {d}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + Agg<char> a6 = {ld}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + + Agg<char> ce1 = { Convert<float>(1.0) }; // expected-warning {{type 'float' cannot be narrowed to 'char'}} expected-note {{override}} + Agg<char> ce2 = { ConvertVar<double>() }; // expected-warning {{type 'double' cannot be narrowed to 'char'}} expected-note {{override}} +} + +// * from long double to double or float, or from double to float, except where +// the source is a constant expression and the actual value after conversion +// is within the range of values that can be represented (even if it cannot be +// represented exactly), or + +void shrink_float() { + // These aren't constant expressions. + float f = 1.0; + double d = 1.0; + long double ld = 1.0; + + // Variables. + Agg<float> f1 = {f}; // OK (no-op) + Agg<float> f2 = {d}; // expected-warning {{non-constant-expression cannot be narrowed from type 'double' to 'float'}} expected-note {{override}} + Agg<float> f3 = {ld}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + // Exact constants. + Agg<float> f4 = {1.0}; // OK (double constant represented exactly) + Agg<float> f5 = {1.0L}; // OK (long double constant represented exactly) + // Inexact but in-range constants. + Agg<float> f6 = {0.1}; // OK (double constant in range but rounded) + Agg<float> f7 = {0.1L}; // OK (long double constant in range but rounded) + // Out of range constants. + Agg<float> f8 = {1E50}; // expected-warning {{constant expression evaluates to 1.000000e+50 which cannot be narrowed to type 'float'}} expected-note {{override}} + Agg<float> f9 = {1E50L}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + // More complex constant expression. + constexpr long double e40 = 1E40L, e30 = 1E30L, e39 = 1E39L; + Agg<float> f10 = {e40 - 5 * e39 + e30 - 5 * e39}; // OK + + // Variables. + Agg<double> d1 = {f}; // OK (widening) + Agg<double> d2 = {d}; // OK (no-op) + Agg<double> d3 = {ld}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + // Exact constant. + Agg<double> d4 = {1.0L}; // OK (long double constant represented exactly) + // Inexact but in-range constant. + Agg<double> d5 = {0.1L}; // OK (long double constant in range but rounded) + // Out of range constant. + Agg<double> d6 = {1E315L}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + // More complex constant expression. + constexpr long double e315 = 1E315L, e305 = 1E305L, e314 = 1E314L; + Agg<double> d7 = {e315 - 5 * e314 + e305 - 5 * e314}; // OK + + Agg<float> ce1 = { Convert<double>(1e300) }; // expected-warning {{constant expression evaluates to 1.000000e+300 which cannot be narrowed to type 'float'}} expected-note {{override}} + Agg<double> ce2 = { ConvertVar<long double>() }; // expected-warning {{non-constant-expression cannot be narrowed from type 'long double' to 'double'}} expected-note {{override}} +} + +// * from an integer type or unscoped enumeration type to a floating-point type, +// except where the source is a constant expression and the actual value after +// conversion will fit into the target type and will produce the original +// value when converted back to the original type, or +void int_to_float() { + // Not a constant expression. + char c = 1; + + // Variables. Yes, even though all char's will fit into any floating type. + Agg<float> f1 = {c}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + Agg<double> f2 = {c}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + Agg<long double> f3 = {c}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + + // Constants. + Agg<float> f4 = {12345678}; // OK (exactly fits in a float) + Agg<float> f5 = {123456789}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + + Agg<float> ce1 = { Convert<int>(123456789) }; // expected-warning {{constant expression evaluates to 123456789 which cannot be narrowed to type 'float'}} expected-note {{override}} + Agg<double> ce2 = { ConvertVar<long long>() }; // expected-warning {{non-constant-expression cannot be narrowed from type 'long long' to 'double'}} expected-note {{override}} +} + +// * from an integer type or unscoped enumeration type to an integer type that +// cannot represent all the values of the original type, except where the +// source is a constant expression and the actual value after conversion will +// fit into the target type and will produce the original value when converted +// back to the original type. +void shrink_int() { + // Not a constant expression. + short s = 1; + unsigned short us = 1; + Agg<char> c1 = {s}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + Agg<unsigned short> s1 = {s}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + Agg<short> s2 = {us}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + + // "that cannot represent all the values of the original type" means that the + // validity of the program depends on the relative sizes of integral types. + // This test compiles with -m64, so sizeof(int)<sizeof(long)==sizeof(long + // long). + long l1 = 1; + Agg<int> i1 = {l1}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + long long ll = 1; + Agg<long> l2 = {ll}; // OK + + // Constants. + Agg<char> c2 = {127}; // OK + Agg<char> c3 = {300}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} expected-warning {{changes value}} + + Agg<int> i2 = {0x7FFFFFFFU}; // OK + Agg<int> i3 = {0x80000000U}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + Agg<unsigned int> i4 = {-0x80000000L}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + + // Bool is also an integer type, but conversions to it are a different AST + // node. + Agg<bool> b1 = {0}; // OK + Agg<bool> b2 = {1}; // OK + Agg<bool> b3 = {-1}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + + // Conversions from pointers to booleans aren't narrowing conversions. + Agg<bool> b = {&b1}; // OK + + Agg<short> ce1 = { Convert<int>(100000) }; // expected-warning {{constant expression evaluates to 100000 which cannot be narrowed to type 'short'}} expected-note {{override}} expected-warning {{changes value from 100000 to -31072}} + Agg<char> ce2 = { ConvertVar<short>() }; // expected-warning {{non-constant-expression cannot be narrowed from type 'short' to 'char'}} expected-note {{override}} +} + +// Be sure that type- and value-dependent expressions in templates get the warning +// too. + +template<int I, typename T> +void maybe_shrink_int(T t) { + Agg<short> s1 = {t}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + Agg<short> s2 = {I}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} expected-warning {{changes value}} + Agg<T> t2 = {700}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} expected-warning {{changes value}} +} + +void test_template() { + maybe_shrink_int<15>((int)3); // expected-note {{in instantiation}} + maybe_shrink_int<70000>((char)3); // expected-note {{in instantiation}} +} + + +// We don't want qualifiers on the types in the diagnostic. + +void test_qualifiers(int i) { + const int j = i; + struct {const unsigned char c;} c1 = {j}; // expected-warning {{from type 'int' to 'unsigned char' in}} expected-note {{override}} + // Template arguments make it harder to avoid printing qualifiers: + Agg<const unsigned char> c2 = {j}; // expected-warning {{from type 'int' to 'const unsigned char' in}} expected-note {{override}} +} + +// Make sure we still get the right SFINAE behavior. +template<unsigned> struct Value { }; + +template<typename T> +int &check_narrowed(Value<sizeof((T){1.1})>); + +template<typename T> +float &check_narrowed(...); + +void test_narrowed(Value<sizeof(int)> vi, Value<sizeof(double)> vd) { + int &ir = check_narrowed<double>(vd); + float &fr = check_narrowed<int>(vi); +} diff --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/basic.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/basic.cpp new file mode 100644 index 0000000..885d11b --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/basic.cpp @@ -0,0 +1,19 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +// PR5787 +class C { + public: + ~C() {} +}; + +template <typename T> +class E { + public: + E& Foo(const C&); + E& Bar() { return Foo(C()); } +}; + +void Test() { + E<int> e; + e.Bar(); +} diff --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p1.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p1.cpp new file mode 100644 index 0000000..20c059e --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p1.cpp @@ -0,0 +1,14 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s +int g(int); +void f() { + int i; + int& r = i; + r = 1; + int* p = &r; + int &rr=r; + int (&rg)(int) = g; + rg(i); + int a[3]; + int (&ra)[3] = a; + ra[1] = i; +} diff --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p3.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p3.cpp new file mode 100644 index 0000000..47e215a --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p3.cpp @@ -0,0 +1,3 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s +int& r1; // expected-error{{declaration of reference variable 'r1' requires an initializer}} +extern int& r2; 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 new file mode 100644 index 0000000..adbdff6 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-0x.cpp @@ -0,0 +1,194 @@ +// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify -pedantic %s + +// Test the c++0x-specific reference initialization rules, e.g., the +// rules for rvalue references. +template<typename T> T prvalue(); +template<typename T> T&& xvalue(); +template<typename T> T& lvalue(); + +struct Base { }; +struct Derived : Base { }; + +struct HasArray { + int array[5]; +}; + +int f(int); + +template<typename T> +struct ConvertsTo { + operator T(); // expected-note 2{{candidate function}} +}; + +void test_rvalue_refs() { + // If the initializer expression... + // - is an xvalue, class prvalue, array prvalue or function lvalue + // and "cv1 T1" is reference-compatible with "cv2 T2", or + + // xvalue case + Base&& base0 = xvalue<Base>(); + Base&& base1 = xvalue<Derived>(); + int&& int0 = xvalue<int>(); + + // class prvalue case + Base&& base2 = prvalue<Base>(); + Base&& base3 = prvalue<Derived>(); + + // array prvalue case + int (&&array0)[5] = HasArray().array; + + // function lvalue case + int (&&function0)(int) = f; + + // - has a class type (i.e., T2 is a class type), where T1 is not + // reference-related to T2, and can be implicitly converted to + // an xvalue, class prvalue, or function lvalue of type "cv3 + // T3", where "cv1 T1" is reference-compatible with "cv3 T3", + + // xvalue + Base&& base4 = ConvertsTo<Base&&>(); + Base&& base5 = ConvertsTo<Derived&&>(); + int && int1 = ConvertsTo<int&&>(); + + // class prvalue + Base&& base6 = ConvertsTo<Base>(); + Base&& base7 = ConvertsTo<Derived>(); + + // function lvalue + int (&&function1)(int) = ConvertsTo<int(&)(int)>(); + + // In the second case, if the reference is an rvalue reference and + // the second standard conversion sequence of the user-defined + // conversion sequence includes an lvalue-to-rvalue conversion, the + // program is ill-formed. + int &&int2 = ConvertsTo<int&>(); // expected-error{{no viable conversion from 'ConvertsTo<int &>' to 'int'}} + int &&int3 = ConvertsTo<float&>(); // expected-error{{no viable conversion from 'ConvertsTo<float &>' to 'int'}} +} + +class NonCopyable { + NonCopyable(const NonCopyable&); +}; + +class NonCopyableDerived : public NonCopyable { + NonCopyableDerived(const NonCopyableDerived&); +}; + +// Make sure we get direct bindings with no copies. +void test_direct_binding() { + NonCopyable &&nc0 = prvalue<NonCopyable>(); + NonCopyable &&nc1 = prvalue<NonCopyableDerived>(); + NonCopyable &&nc2 = xvalue<NonCopyable>(); + NonCopyable &&nc3 = xvalue<NonCopyableDerived>(); + const NonCopyable &nc4 = prvalue<NonCopyable>(); + const NonCopyable &nc5 = prvalue<NonCopyableDerived>(); + const NonCopyable &nc6 = xvalue<NonCopyable>(); + const NonCopyable &nc7 = xvalue<NonCopyableDerived>(); + NonCopyable &&nc8 = ConvertsTo<NonCopyable&&>(); + NonCopyable &&nc9 = ConvertsTo<NonCopyableDerived&&>(); + const NonCopyable &nc10 = ConvertsTo<NonCopyable&&>(); + const NonCopyable &nc11 = ConvertsTo<NonCopyableDerived&&>(); +} + +namespace std_example_1 { + double d = 2.0; + double& rd = d; + const double& rcd = d; + struct A { }; + struct B : A { + operator int&(); + } b; + A& ra = b; + const A& rca = b; + int& ir = B(); +} + +namespace std_example_2 { + double& rd2 = 2.0; // expected-error{{non-const lvalue reference to type 'double' cannot bind to a temporary of type 'double'}} + int i = 2; + double& rd3 = i; // expected-error{{non-const lvalue reference to type 'double' cannot bind to a value of unrelated type 'int'}} + struct A { }; + struct B : A { } b; + extern B f(); + const A& rca = f(); + A&& rra = f(); + struct X { + operator B(); // expected-note{{candidate function}} + operator int&(); // expected-note{{candidate function}} + } x; + const A& r = x; + int&& rri = static_cast<int&&>(i); + B&& rrb = x; + int&& rri2 = X(); // expected-error{{no viable conversion from 'std_example_2::X' to 'int'}} + + const double& rcd2 = 2; + double&& rrd = 2; + const volatile int cvi = 1; + const int& r2 = cvi; // expected-error{{binding of reference to type 'const int' to a value of type 'const volatile int' drops qualifiers}} + + double d; + double&& rrd2 = d; // expected-error{{rvalue reference to type 'double' cannot bind to lvalue of type 'double'}} + double&& rrd3 = i; +} + +namespace argument_passing { + void base_rvalue_ref(Base&&); + void int_rvalue_ref(int&&); // expected-note{{candidate function not viable: no known conversion from 'ConvertsTo<int &>' to 'int &&' for 1st argument}} \ + // expected-note{{candidate function not viable: no known conversion from 'ConvertsTo<float &>' to 'int &&' for 1st argument}} + + void array_rvalue_ref(int (&&)[5]); + void function_rvalue_ref(int (&&)(int)); + + void test() { + base_rvalue_ref(xvalue<Base>()); + base_rvalue_ref(xvalue<Derived>()); + int_rvalue_ref(xvalue<int>()); + + base_rvalue_ref(prvalue<Base>()); + base_rvalue_ref(prvalue<Derived>()); + + array_rvalue_ref(HasArray().array); + + function_rvalue_ref(f); + + base_rvalue_ref(ConvertsTo<Base&&>()); + base_rvalue_ref(ConvertsTo<Derived&&>()); + int_rvalue_ref(ConvertsTo<int&&>()); + + base_rvalue_ref(ConvertsTo<Base>()); + base_rvalue_ref(ConvertsTo<Derived>()); + + function_rvalue_ref(ConvertsTo<int(&)(int)>()); + + int_rvalue_ref(ConvertsTo<int&>()); // expected-error{{no matching function for call to 'int_rvalue_ref'}} + int_rvalue_ref(ConvertsTo<float&>()); // expected-error{{no matching function for call to 'int_rvalue_ref'}} + } + +} + +namespace pr10644 { + struct string { + string(const char* __s); + }; + class map { + int& operator[](const string& __k); + public: + int& operator[](const string&& __k); + }; + void foo() { + static map key_map; + key_map["line"]; + } +} + +namespace PR11003 { + class Value { + }; + struct MoveRef { + operator Value &() const ; + }; + MoveRef Move(int); + void growTo() { + Value x = Move(0); + Value y(Move(0)); + } +} diff --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-cxx03-extra-copy.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-cxx03-extra-copy.cpp new file mode 100644 index 0000000..d58a129 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-cxx03-extra-copy.cpp @@ -0,0 +1,77 @@ +// RUN: %clang_cc1 -fsyntax-only -fdiagnostics-show-option -verify %s + +// C++03 requires that we check for a copy constructor when binding a +// reference to a temporary, since we are allowed to make a copy, Even +// though we don't actually make that copy, make sure that we diagnose +// cases where that copy constructor is somehow unavailable. As an +// extension, this is only a warning. + +struct X1 { + X1(); + explicit X1(const X1&); +}; + +struct X2 { + X2(); + +private: + X2(const X2&); // expected-note{{declared private here}} +}; + +struct X3 { + X3(); + +private: + X3(X3&); // expected-note{{candidate constructor not viable: no known conversion from 'X3' to 'X3 &' for 1st argument}} +}; + +// Check for instantiation of default arguments +template<typename T> +T get_value_badly() { + double *dp = 0; + // The extension doesn't extend far enough to turn this error into a warning. + T *tp = dp; // expected-error{{cannot initialize a variable of type 'int *' with an lvalue of type 'double *'}} + return T(); +} + +template<typename T> +struct X4 { + X4(); + X4(const X4&, T = get_value_badly<T>()); // expected-note{{in instantiation of}} +}; + +// Check for "dangerous" default arguments that could cause recursion. +struct X5 { + X5(); + X5(const X5&, const X5& = X5()); // expected-warning{{no viable constructor copying parameter of type 'X5'}} +}; + +void g1(const X1&); +void g2(const X2&); +void g3(const X3&); +void g4(const X4<int>&); +void g5(const X5&); + +void test() { + g1(X1()); + g2(X2()); // expected-warning{{C++98 requires an accessible copy constructor for class 'X2' when binding a reference to a temporary; was private}} + g3(X3()); // expected-warning{{no viable constructor copying parameter of type 'X3'}} + g4(X4<int>()); + g5(X5()); // Generates a warning in the default argument. +} + +// Check that unavailable copy constructors still cause SFINAE failures. +template<int> struct int_c { }; + +template<typename T> T f(const T&); + +// Would be ambiguous with the next g(), except the instantiation failure in +// sizeof() prevents that. +template<typename T> +int &g(int_c<sizeof(f(T()))> * = 0); + +template<typename T> float &g(); + +void h() { + float &fp2 = g<X3>(); // Not ambiguous. +} diff --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-cxx0x-no-extra-copy.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-cxx0x-no-extra-copy.cpp new file mode 100644 index 0000000..27eb6d1 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-cxx0x-no-extra-copy.cpp @@ -0,0 +1,64 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s + +// C++03 requires that we check for a copy constructor when binding a +// reference to a reference-compatible rvalue, since we are allowed to +// make a copy. C++0x does not permit the copy, so ensure that we +// don't diagnose cases where the copy constructor is unavailable. + +struct X1 { + X1(); + explicit X1(const X1&); +}; + +struct X2 { + X2(); + +private: + X2(const X2&); +}; + +struct X3 { + X3(); + +private: + X3(X3&); +}; + +template<typename T> +T get_value_badly() { + double *dp = 0; + T *tp = dp; + return T(); +} + +template<typename T> +struct X4 { + X4(); + X4(const X4&, T = get_value_badly<T>()); +}; + +void g1(const X1&); +void g2(const X2&); +void g3(const X3&); +void g4(const X4<int>&); + +void test() { + g1(X1()); + g2(X2()); + g3(X3()); + g4(X4<int>()); +} + +// Check that unavailable copy constructors do not cause SFINAE failures. +template<int> struct int_c { }; + +template<typename T> T f(const T&); + +template<typename T> +int &g(int_c<sizeof(f(T()))> * = 0); // expected-note{{candidate function [with T = X3]}} + +template<typename T> float &g(); // expected-note{{candidate function [with T = X3]}} + +void h() { + float &fp = g<X3>(); // expected-error{{call to 'g' is ambiguous}} +} diff --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-examples.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-examples.cpp new file mode 100644 index 0000000..08d9639 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-examples.cpp @@ -0,0 +1,53 @@ +// RUN: %clang_cc1 -ast-dump %s 2>&1 | FileCheck %s + +// CHECK: example0 +void example0() { + double d = 2.0; + // CHECK: double &rd = + // CHECK-NEXT: DeclRefExpr + double &rd = d; + // CHECK: const double &rcd = + // CHECK-NEXT: ImplicitCastExpr{{.*}}'const double' lvalue <NoOp> + const double &rcd = d; +} + +struct A { }; +struct B : A { } b; + +// CHECK: example1 +void example1() { + // CHECK: A &ra = + // CHECK: ImplicitCastExpr{{.*}}'struct A' lvalue <DerivedToBase (A)> + A &ra = b; + // CHECK: const A &rca = + // CHECK: ImplicitCastExpr{{.*}}'const struct A' lvalue <NoOp> + // CHECK: ImplicitCastExpr{{.*}}'struct A' lvalue <DerivedToBase (A)> + const A& rca = b; +} + +extern B f(); + +struct X { + operator B(); +} x; + +// CHECK: example2 +void example2() { + // CHECK: const A &rca = + // CHECK: ImplicitCastExpr{{.*}}'const struct A' <NoOp> + // CHECK: ImplicitCastExpr{{.*}}'struct A' <DerivedToBase (A)> + // CHECK: CallExpr{{.*}}B + const A &rca = f(); + // CHECK: const A &r = + // CHECK: ImplicitCastExpr{{.*}}'const struct A' <NoOp> + // CHECK: ImplicitCastExpr{{.*}}'struct A' <DerivedToBase (A)> + // CHECK: CXXMemberCallExpr{{.*}}'struct B' + const A& r = x; +} + +// CHECK: example3 +void example3() { + // CHECK: const double &rcd2 = + // CHECK: ImplicitCastExpr{{.*}} <IntegralToFloating> + const double& rcd2 = 2; +} 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 new file mode 100644 index 0000000..fee5f96 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-var.cpp @@ -0,0 +1,135 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +struct Base { }; +struct Derived : Base { }; // expected-note{{candidate constructor (the implicit copy constructor) not viable}} +struct Unrelated { }; +struct Derived2 : Base { }; +struct Diamond : Derived, Derived2 { }; + +struct ConvertibleToBaseRef { + operator Base&() const; +}; + +struct ConvertibleToDerivedRef { + operator Derived&() const; +}; + +struct ConvertibleToBothDerivedRef { + operator Derived&(); // expected-note{{candidate function}} + operator Derived2&(); // expected-note{{candidate function}} +}; + +struct ConvertibleToIntRef { + operator int&(); +}; + +struct ConvertibleToBase { + operator Base() const; +}; + +struct ConvertibleToDerived { + operator Derived() const; +}; + +struct ConvertibleToBothDerived { + operator Derived(); // expected-note{{candidate function}} + operator Derived2(); // expected-note{{candidate function}} +}; + +struct ConvertibleToInt { + operator int(); +}; + +template<typename T> T create(); + +// First bullet: lvalue references binding to lvalues (the simple cases). +void bind_lvalue_to_lvalue(Base b, Derived d, + const Base bc, const Derived dc, + Diamond diamond, + int i) { + // Reference-compatible + Base &br1 = b; + 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 &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'}} +} + +void bind_lvalue_quals(volatile Base b, volatile Derived d, + volatile const Base bvc, volatile const Derived dvc, + volatile const int ivc) { + volatile Base &bvr1 = b; + volatile Base &bvr2 = d; + volatile Base &bvr3 = bvc; // expected-error{{binding of reference to type 'volatile Base' to a value of type 'const volatile Base' drops qualifiers}} + volatile Base &bvr4 = dvc; // expected-error{{binding of reference to type 'volatile Base' to a value of type 'const volatile Derived' drops qualifiers}} + + volatile int &ir = ivc; // expected-error{{binding of reference to type 'volatile int' to a value of type 'const volatile int' 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 'Base' cannot bind to a temporary of type 'Base'}} + Base &br2 = Derived(); // expected-error{{non-const lvalue reference to type 'Base' cannot bind to a temporary of type 'Derived'}} + const volatile Base &br3 = Base(); // expected-error{{volatile lvalue reference to type 'const volatile Base' cannot bind to a temporary of type 'Base'}} + const volatile Base &br4 = Derived(); // expected-error{{volatile lvalue reference to type 'const volatile Base' cannot bind to a temporary of type '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 'Base' cannot bind to a value of unrelated type 'Unrelated'}} + const volatile Base &br2 = ur; // expected-error{{volatile lvalue reference to type 'const volatile Base' cannot bind to a value of unrelated type 'Unrelated'}} +} + +void bind_lvalue_to_conv_lvalue() { + // Not reference-related, but convertible + Base &nbr1 = ConvertibleToBaseRef(); + Base &nbr2 = ConvertibleToDerivedRef(); + Derived &ndr1 = ConvertibleToDerivedRef(); + int &ir = ConvertibleToIntRef(); +} + +void bind_lvalue_to_conv_lvalue_ambig(ConvertibleToBothDerivedRef both) { + Derived &dr1 = both; + Base &br1 = both; // expected-error{{reference initialization of type 'Base &' with initializer of type 'ConvertibleToBothDerivedRef' is ambiguous}} +} + +struct IntBitfield { + int i : 17; // expected-note{{bit-field is declared here}} +}; + +void test_bitfield(IntBitfield ib) { + int & ir1 = (ib.i); // expected-error{{non-const reference cannot bind to bit-field 'i'}} +} + +// Second bullet: const lvalue reference binding to an rvalue with +// similar type (both of which are class types). +void bind_const_lvalue_to_rvalue() { + const Base &br1 = create<Base>(); + const Base &br2 = create<Derived>(); + const Derived &dr1 = create<Base>(); // expected-error{{no viable conversion}} + + const Base &br3 = create<const Base>(); + const Base &br4 = create<const Derived>(); + + const Base &br5 = create<const volatile Base>(); // expected-error{{binding of reference to type 'const Base' to a value of type 'const volatile Base' drops qualifiers}} + const Base &br6 = create<const volatile Derived>(); // expected-error{{binding of reference to type 'const Base' to a value of type 'const volatile Derived' drops qualifiers}} + + const int &ir = create<int>(); +} + +// Second bullet: const lvalue reference binds to the result of a conversion. +void bind_const_lvalue_to_class_conv_temporary() { + const Base &br1 = ConvertibleToBase(); + const Base &br2 = ConvertibleToDerived(); +} +void bind_lvalue_to_conv_rvalue_ambig(ConvertibleToBothDerived both) { + const Derived &dr1 = both; + const Base &br1 = both; // expected-error{{reference initialization of type 'const Base &' with initializer of type 'ConvertibleToBothDerived' is ambiguous}} +} diff --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5.cpp new file mode 100644 index 0000000..51d61a5 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5.cpp @@ -0,0 +1,40 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +namespace PR5909 { + struct Foo { + int x : 20; + }; + + bool Test(const int& foo); + + const Foo f = { 0 }; // It compiles without the 'const'. + bool z = Test(f.x); +} + +namespace PR6264 { + typedef int (&T)[3]; + struct S + { + operator T (); + }; + void f() + { + T bar = S(); + } +} + +namespace PR6066 { + struct B { }; + struct A : B { + operator B*(); + operator B&(); // expected-warning{{conversion function converting 'PR6066::A' to its base class 'PR6066::B' will never be used}} + }; + + void f(B&); // no rvalues accepted + void f(B*); + + int g() { + f(A()); // calls f(B*) + return 0; + } +} diff --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.string/p1.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.string/p1.cpp new file mode 100644 index 0000000..3631af1 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.string/p1.cpp @@ -0,0 +1,19 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +char x1[]("hello"); +extern char x1[6]; + +char x2[] = "hello"; +extern char x2[6]; + +char x3[] = { "hello" }; +extern char x3[6]; + +wchar_t x4[](L"hello"); +extern wchar_t x4[6]; + +wchar_t x5[] = L"hello"; +extern wchar_t x5[6]; + +wchar_t x6[] = { L"hello" }; +extern wchar_t x6[6]; diff --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.string/p2.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.string/p2.cpp new file mode 100644 index 0000000..3d67fcc --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.string/p2.cpp @@ -0,0 +1,2 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s +char test1[1]="f"; // expected-error {{initializer-string for char array is too long}} diff --git a/clang/test/CXX/dcl.decl/dcl.init/p14-0x.cpp b/clang/test/CXX/dcl.decl/dcl.init/p14-0x.cpp new file mode 100644 index 0000000..419f2bf --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.init/p14-0x.cpp @@ -0,0 +1,44 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s + +struct NoDefault { + NoDefault() = delete; // expected-note {{here}} + NoDefault(int); +}; +struct Explicit { // expected-note 2 {{candidate}} expected-note {{here}} + explicit Explicit(int); +}; +struct NoCopy { + NoCopy(); + NoCopy(const NoCopy &) = delete; // expected-note {{here}} +}; +struct NoMove { + NoMove(); + NoMove(NoMove &&) = delete; // expected-note {{here}} +}; +class Private { + Private(int); // expected-note {{here}} +public: + Private(); +}; +class Friend { + friend class S; + Friend(int); +}; + + +class S { + NoDefault nd1; + NoDefault nd2 = 42; + Explicit e1; // expected-note {{here}} + Explicit e2 = 42; // expected-error {{no viable conversion}} + NoCopy nc = NoCopy(); // expected-error {{call to deleted}} + NoMove nm = NoMove(); // expected-error {{call to deleted}} + Private p = 42; // expected-error {{private constructor}} + Friend f = 42; + + S() {} // expected-error {{call to deleted constructor of 'NoDefault'}} \ + expected-error {{must explicitly initialize the member 'e1' which does not have a default constructor}} + S(int) : nd1(42), e1(42) {} +}; + +// FIXME: test the other forms which use copy-initialization diff --git a/clang/test/CXX/dcl.decl/dcl.init/p5.cpp b/clang/test/CXX/dcl.decl/dcl.init/p5.cpp new file mode 100644 index 0000000..b50e8d7 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.init/p5.cpp @@ -0,0 +1,20 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +// FIXME: Very incomplete! + +// A program that calls for default-initialization or value-initialization of +// an entity of reference type is illformed. If T is a cv-qualified type, the +// cv-unqualified version of T is used for these definitions of +// zero-initialization, default-initialization, and value-initialization. +// +// FIXME: The diagnostics for these errors are terrible because they fall out +// of the AST representation rather than being explicitly issued during the +// respective initialization forms. +struct S { // expected-error {{implicit default constructor for 'S' must explicitly initialize the reference member}} \ + // expected-note {{candidate constructor (the implicit copy constructor) not viable}} + int& x; // expected-note {{declared here}} +}; +S s; // expected-note {{implicit default constructor for 'S' first required here}} +S f() { + return S(); // expected-error {{no matching constructor for initialization of 'S'}} +} diff --git a/clang/test/CXX/dcl.decl/dcl.init/p6.cpp b/clang/test/CXX/dcl.decl/dcl.init/p6.cpp new file mode 100644 index 0000000..514fd0c --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.init/p6.cpp @@ -0,0 +1,28 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +// FIXME: Very incomplete! + +// If a program calls for the default initialization of an object of a +// const-qualified type T, T shall be a class type with a +// user-provided default constructor. +struct MakeNonPOD { MakeNonPOD(); }; +struct NoUserDefault : public MakeNonPOD { }; +struct HasUserDefault { HasUserDefault(); }; + +void test_const_default_init() { + const NoUserDefault x1; // expected-error{{default initialization of an object of const type 'const NoUserDefault' requires a user-provided default constructor}} + const HasUserDefault x2; + const int x3; // expected-error{{default initialization of an object of const type 'const int'}} +} + +// rdar://8501008 +struct s0 {}; +struct s1 { static const s0 foo; }; +const struct s0 s1::foo; // expected-error{{default initialization of an object of const type 'const struct s0' requires a user-provided default constructor}} + +template<typename T> +struct s2 { + static const s0 foo; +}; + +template<> const struct s0 s2<int>::foo; // okay diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1-cxx0x.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1-cxx0x.cpp new file mode 100644 index 0000000..102746c --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1-cxx0x.cpp @@ -0,0 +1,7 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11 + +void f() { + int b[5]; + auto a[5] = b; // expected-error{{'a' declared as array of 'auto'}} + auto *c[5] = b; // expected-error{{'c' declared as array of 'auto *'}} +} diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1.cpp new file mode 100644 index 0000000..bb4a48e --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1.cpp @@ -0,0 +1,54 @@ +// RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s + +// Simple form +int ar1[10]; + +// Element type cannot be: +// - (cv) void +volatile void ar2[10]; // expected-error {{incomplete element type 'volatile void'}} +// - a reference +int& ar3[10]; // expected-error {{array of references}} +// - a function type +typedef void Fn(); +Fn ar4[10]; // expected-error {{array of functions}} +// - an abstract class +struct Abstract { virtual void fn() = 0; }; // expected-note {{pure virtual}} +Abstract ar5[10]; // expected-error {{abstract class}} + +// If we have a size, it must be greater than zero. +int ar6[-1]; // expected-error {{array with a negative size}} +int ar7[0u]; // expected-warning {{zero size arrays are an extension}} + +// An array with unknown bound is incomplete. +int ar8[]; // expected-error {{needs an explicit size or an initializer}} +// So is an array with an incomplete element type. +struct Incomplete; // expected-note {{forward declaration}} +Incomplete ar9[10]; // expected-error {{incomplete type}} +// Neither of which should be a problem in situations where no complete type +// is required. (PR5048) +void fun(int p1[], Incomplete p2[10]); +extern int ear1[]; +extern Incomplete ear2[10]; + +// cv migrates to element type +typedef const int cint; +extern cint car1[10]; +typedef int intar[10]; +// thus this is a valid redeclaration +extern const intar car1; + +// Check that instantiation works properly when the element type is a template. +template <typename T> struct S { + typename T::type x; // expected-error {{has no members}} +}; +S<int> ar10[10]; // expected-note {{requested here}} + +// Ensure that negative array size errors include the name of the declared +// array as this is often used to simulate static_assert with template +// instantiations, placing the 'error message' in the declarator name. +int +user_error_message +[-1]; // expected-error {{user_error_message}} +typedef int +another_user_error_message +[-1]; // expected-error {{another_user_error_message}} diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p10.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p10.cpp new file mode 100644 index 0000000..385e45d --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p10.cpp @@ -0,0 +1,16 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +struct A { + virtual void f(int a = 7); +}; + +struct B : public A { + void f(int a); // expected-note{{'f' declared here}} +}; + +void m() { + B* pb = new B; + A* pa = pb; + pa->f(); // OK, calls pa->B::f(7) + pb->f(); // expected-error{{too few arguments}} +} diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p2.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p2.cpp new file mode 100644 index 0000000..0a107eb --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p2.cpp @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +void point(int = 3, int = 4); + +void test_point() { + point(1,2); + point(1); + point(); +} diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p3.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p3.cpp new file mode 100644 index 0000000..e9c5e0c --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p3.cpp @@ -0,0 +1,13 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +void nondecl(int (*f)(int x = 5)) // {expected-error {{default arguments can only be specified}}} +{ + void (*f2)(int = 17) // {expected-error {{default arguments can only be specified}}} + = (void (*)(int = 42))f; // {expected-error {{default arguments can only be specified}}} +} + +struct X0 { + int (*f)(int = 17); // expected-error{{default arguments can only be specified for parameters in a function declaration}} + + void mem8(int (*fp)(int) = (int (*)(int = 17))0); // expected-error{{default arguments can only be specified for parameters in a function declaration}} +}; diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p4.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p4.cpp new file mode 100644 index 0000000..f3dec52 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p4.cpp @@ -0,0 +1,55 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +void f0(int i, int j, int k = 3); +void f0(int i, int j, int k); +void f0(int i, int j = 2, int k); +void f0(int i, int j, int k); +void f0(int i = 1, // expected-note{{previous definition}} + int j, int k); +void f0(int i, int j, int k); + +namespace N0 { + void f0(int, int, int); // expected-note{{candidate}} + + void test_f0_inner_scope() { + f0(); // expected-error{{no matching}} + } +} + +void test_f0_outer_scope() { + f0(); // okay +} + +void f0(int i = 1, // expected-error{{redefinition of default argument}} + int, int); + +template<typename T> void f1(T); // expected-note{{previous}} + +template<typename T> +void f1(T = T()); // expected-error{{cannot be added}} + + +namespace N1 { + // example from C++03 standard + // FIXME: make these "f2"s into "f"s, then fix our scoping issues + void f2(int, int); + void f2(int, int = 7); + void h() { + f2(3); // OK, calls f(3, 7) + void f(int = 1, int); // expected-error{{missing default argument}} + } + + void m() + { + void f(int, int); // expected-note{{'f' declared here}} + f(4); // expected-error{{too few arguments to function call}} + void f(int, int = 5); // expected-note{{previous definition}} + f(4); // okay + void f(int, int = 5); // expected-error{{redefinition of default argument}} + } + + void n() + { + f2(6); // okay + } +} diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p5.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p5.cpp new file mode 100644 index 0000000..3100e56 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p5.cpp @@ -0,0 +1,19 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +float global_f; + +void f0(int *ip = &global_f); // expected-error{{cannot initialize}} \ +// expected-note{{passing argument to parameter 'ip' here}} + +// Example from C++03 standard +int a = 1; +int f(int); +int g(int x = f(a)); + +void h() { + a = 2; + { + int *a = 0; + g(); // FIXME: check that a is called with a value of 2 + } +} diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p6.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p6.cpp new file mode 100644 index 0000000..9ab0b48 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p6.cpp @@ -0,0 +1,33 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +class C { +public: + void f(int i = 3); // expected-note{{here}} + void g(int i, int j = 99); +}; + +void C::f(int i = 3) { } // expected-error{{redefinition of default argument}} + +void C::g(int i = 88, int j) { } + +void test_C(C c) { + c.f(); + c.g(); +} + +template<typename T> +struct X0 { + void f(int); + + struct Inner { + void g(int); + }; +}; + +// DR217 +template<typename T> +void X0<T>::f(int = 17) { } // expected-error{{cannot be added}} + +// DR217 + DR205 (reading tea leaves) +template<typename T> +void X0<T>::Inner::g(int = 17) { } // expected-error{{cannot be added}} diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p7.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p7.cpp new file mode 100644 index 0000000..164eb36 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p7.cpp @@ -0,0 +1,7 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +void h() +{ + int i; + extern void h2(int x = sizeof(i)); // expected-error {{default argument references local variable 'i' of enclosing function}} +} diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p8.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p8.cpp new file mode 100644 index 0000000..1a08ab7 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p8.cpp @@ -0,0 +1,4 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s +class A { + void f(A* p = this) { } // expected-error{{invalid use of 'this'}} +}; diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/dcl.fct.def.default/p2.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/dcl.fct.def.default/p2.cpp new file mode 100644 index 0000000..a879829 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/dcl.fct.def.default/p2.cpp @@ -0,0 +1,62 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s + +// FIXME: test with non-std qualifiers + +namespace move { + struct Const { + Const(const Const&&) = default; // expected-error {{the parameter for an explicitly-defaulted move constructor may not be const}} + Const& operator=(const Const&&) = default; // expected-error {{the parameter for an explicitly-defaulted move assignment operator may not be const}} + }; + + struct Volatile { + Volatile(volatile Volatile&&) = default; // expected-error {{the parameter for an explicitly-defaulted move constructor may not be volatile}} + Volatile& operator=(volatile Volatile&&) = default; // expected-error {{the parameter for an explicitly-defaulted move assignment operator may not be volatile}} + }; + + struct AssignmentRet1 { + AssignmentRet1&& operator=(AssignmentRet1&&) = default; // expected-error {{an explicitly-defaulted move assignment operator must return an unqualified lvalue reference to its class type}} + }; + + struct AssignmentRet2 { + const AssignmentRet2& operator=(AssignmentRet2&&) = default; // expected-error {{an explicitly-defaulted move assignment operator must return an unqualified lvalue reference to its class type}} + }; + + struct ConstAssignment { + ConstAssignment& operator=(ConstAssignment&&) const = default; // expected-error {{an explicitly-defaulted move assignment operator may not have 'const', 'constexpr' or 'volatile' qualifiers}} + }; +} + +namespace copy { + struct Volatile { + Volatile(const volatile Volatile&) = default; // expected-error {{the parameter for an explicitly-defaulted copy constructor may not be volatile}} + Volatile& operator=(const volatile Volatile&) = default; // expected-error {{the parameter for an explicitly-defaulted copy assignment operator may not be volatile}} + }; + + struct Const { + Const(const Const&) = default; + Const& operator=(const Const&) = default; + }; + + struct NonConst { + NonConst(NonConst&) = default; + NonConst& operator=(NonConst&) = default; + }; + + struct BadConst { + NonConst nc; // makes implicit copy non-const + BadConst(const BadConst&) = default; // expected-error {{is const, but}} + BadConst& operator=(const BadConst&) = default; // expected-error {{is const, but}} + }; + + struct AssignmentRet1 { + AssignmentRet1&& operator=(const AssignmentRet1&) = default; // expected-error {{an explicitly-defaulted copy assignment operator must return an unqualified lvalue reference to its class type}} + }; + + struct AssignmentRet2 { + const AssignmentRet2& operator=(const AssignmentRet2&) = default; // expected-error {{an explicitly-defaulted copy assignment operator must return an unqualified lvalue reference to its class type}} + }; + + struct ConstAssignment { + ConstAssignment& operator=(const ConstAssignment&) const = default; // expected-error {{an explicitly-defaulted copy assignment operator may not have 'const', 'constexpr' or 'volatile' qualifiers}} + }; +} diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p13.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p13.cpp new file mode 100644 index 0000000..19a5f23 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p13.cpp @@ -0,0 +1,48 @@ +// RUN: %clang_cc1 -std=c++11 -fsyntax-only -fcxx-exceptions -fexceptions -verify %s + +// When it is part of a parameter-declaration-clause, the parameter +// pack is a function parameter pack. +template<typename ...Types> +void f0(Types ...args); + +template<typename ...Types> +void f1(const Types &...args); + +// [ Note: Otherwise, the parameter-declaration is part of a +// template-parameter-list and the parameter pack is a template +// parameter pack; see 14.1. -- end note ] +template<int ...N> +struct X0 { }; + +template<typename ...Types> +struct X1 { + template<Types ...Values> struct Inner; +}; + +// A declarator-id or abstract-declarator containing an ellipsis shall +// only be used in a parameter-declaration. +int (...f2)(int); // expected-error{{only function and template parameters can be parameter packs}} + +void f3() { + int ...x; // expected-error{{only function and template parameters can be parameter packs}} + if (int ...y = 17) { } // expected-error{{only function and template parameters can be parameter packs}} + + for (int ...z = 0; z < 10; ++z) { } // expected-error{{only function and template parameters can be parameter packs}} + + try { + } catch (int ...e) { // expected-error{{only function and template parameters can be parameter packs}} + } +} + +template<typename ...Types> +struct X2 { + Types ...members; // expected-error{{only function and template parameters can be parameter packs}} \ + // expected-error{{data member type contains unexpanded parameter pack}} +}; + +// The type T of the declarator-id of the function parameter pack +// shall contain a template parameter pack; each template parameter +// pack in T is expanded by the function parameter pack. +template<typename T> +void f4(T ...args); // expected-error{{type 'T' of function parameter pack does not contain any unexpanded parameter packs}} + diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p14.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p14.cpp new file mode 100644 index 0000000..0e69521 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p14.cpp @@ -0,0 +1,31 @@ +// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s + +template<typename T> struct identity; +template<typename ...Types> struct tuple; + +template<typename T, typename U> struct is_same { + static const bool value = false; +}; + +template<typename T> struct is_same<T, T> { + static const bool value = true; +}; + +// There is a syntactic ambiguity when an ellipsis occurs at the end +// of a parameter-declaration-clause without a preceding comma. In +// this case, the ellipsis is parsed as part of the +// abstract-declarator if the type of the parameter names a template +// parameter pack that has not been expanded; otherwise, it is parsed +// as part of the parameter-declaration-clause. + +template<typename T, typename ...Types> +struct X0 { + typedef identity<T(Types...)> function_pack_1; + typedef identity<T(Types......)> variadic_function_pack_1; + typedef identity<T(T...)> variadic_1; + typedef tuple<T(Types, ...)...> template_arg_expansion_1; +}; + + + +// FIXME: Once function parameter packs are implemented, we can test all of the disambiguation diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p2-cxx0x.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p2-cxx0x.cpp new file mode 100644 index 0000000..6b1f3e4 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p2-cxx0x.cpp @@ -0,0 +1,7 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s + +auto a() -> int; // ok +const auto b() -> int; // expected-error {{function with trailing return type must specify return type 'auto', not 'auto const'}} +auto *c() -> int; // expected-error {{function with trailing return type must specify return type 'auto', not 'auto *'}} +auto (d() -> int); // expected-error {{trailing return type may not be nested within parentheses}} +auto e() -> auto (*)() -> auto (*)() -> void; // ok: same as void (*(*e())())(); diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p3.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p3.cpp new file mode 100644 index 0000000..ad827fb --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p3.cpp @@ -0,0 +1,3 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s +void f(int) { } // expected-note {{previous definition is here}} +void f(const int) { } // expected-error {{redefinition of 'f'}} diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p6-0x.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p6-0x.cpp new file mode 100644 index 0000000..2ec1454 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p6-0x.cpp @@ -0,0 +1,53 @@ +// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s + +void f0() &; // expected-error {{non-member function cannot have '&' qualifier}} +void f1() &&; // expected-error {{non-member function cannot have '&&' qualifier}} +void f2() const volatile &&; // expected-error {{non-member function cannot have 'const volatile &&' qualifier}} + +struct X { + void f0() &; + void f1() &&; + static void f2() &; // expected-error{{static member function cannot have '&' qualifier}} + static void f3() &&; // expected-error{{static member function cannot have '&&' qualifier}} +}; + +typedef void func_type_lvalue() &; +typedef void func_type_rvalue() &&; + +typedef func_type_lvalue *func_type_lvalue_ptr; // expected-error{{pointer to function type 'func_type_lvalue' (aka 'void () &') cannot have '&' qualifier}} +typedef func_type_rvalue *func_type_rvalue_ptr; // expected-error{{pointer to function type 'func_type_rvalue' (aka 'void () &&') cannot have '&&' qualifier}} + +typedef func_type_lvalue &func_type_lvalue_ref; // expected-error{{reference to function type 'func_type_lvalue' (aka 'void () &') cannot have '&' qualifier}} +typedef func_type_rvalue &func_type_rvalue_ref; // expected-error{{reference to function type 'func_type_rvalue' (aka 'void () &&') cannot have '&&' qualifier}} + +template<typename T = func_type_lvalue> struct wrap { + typedef T val; + typedef T *ptr; + typedef T &ref; +}; + +using func_type_lvalue = wrap<>::val; +using func_type_lvalue = wrap<func_type_lvalue>::val; +using func_type_rvalue = wrap<func_type_rvalue>::val; + +using func_type_lvalue_ptr = wrap<>::ptr; +using func_type_lvalue_ptr = wrap<func_type_lvalue>::ptr; +using func_type_rvalue_ptr = wrap<func_type_rvalue>::ptr; + +using func_type_lvalue_ref = wrap<>::ref; +using func_type_lvalue_ref = wrap<func_type_lvalue>::ref; +using func_type_rvalue_ref = wrap<func_type_rvalue>::ref; + +func_type_lvalue f2; // expected-error{{non-member function of type 'func_type_lvalue' (aka 'void () &') cannot have '&' qualifier}} +func_type_rvalue f3; // expected-error{{non-member function of type 'func_type_rvalue' (aka 'void () &&') cannot have '&&' qualifier}} + +struct Y { + func_type_lvalue f0; + func_type_rvalue f1; +}; + +void (X::*mpf1)() & = &X::f0; +void (X::*mpf2)() && = &X::f1; + + +void (f() &&); // expected-error{{non-member function cannot have '&&' qualifier}} diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p6.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p6.cpp new file mode 100644 index 0000000..e2d94fb --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p6.cpp @@ -0,0 +1,20 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +typedef void F() const; + +void f() const; // expected-error {{non-member function cannot have 'const' qualifier}} +F g; // expected-error {{non-member function of type 'F' (aka 'void () const') cannot have 'const' qualifier}} + +struct X { + void f() const; + friend void g() const; // expected-error {{non-member function cannot have 'const' qualifier}} + static void h() const; // expected-error {{static member function cannot have 'const' qualifier}} + F i; // ok + friend F j; // expected-error {{non-member function of type 'F' (aka 'void () const') cannot have 'const' qualifier}} + static F k; // expected-error {{static member function of type 'F' (aka 'void () const') cannot have 'const' qualifier}} +}; + +struct Y { + friend void X::f() const; + friend void ::f() const; // expected-error {{non-member function cannot have 'const' qualifier}} +}; diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p8-0x.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p8-0x.cpp new file mode 100644 index 0000000..11926f1 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p8-0x.cpp @@ -0,0 +1,6 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s + +auto f() -> int[32]; // expected-error{{function cannot return array}} +auto g() -> int(int); // expected-error{{function cannot return function}} +auto h() -> auto() -> int; // expected-error{{function cannot return function}} +auto i() -> auto(*)() -> int; diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p8.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p8.cpp new file mode 100644 index 0000000..34a8c85 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p8.cpp @@ -0,0 +1,10 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +struct A { }; +A::A (enum { e1 }) {} // expected-error{{can not be defined in a parameter}} \ +// expected-error{{out-of-line definition}} +void A::f(enum { e2 }) {} // expected-error{{can not be defined in a parameter}} \ +// expected-error{{out-of-line definition}} + +enum { e3 } A::g() { } // expected-error{{can not be defined in the result type}} \ +// expected-error{{out-of-line definition}} diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p9-0x.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p9-0x.cpp new file mode 100644 index 0000000..574a3e7 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p9-0x.cpp @@ -0,0 +1,3 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s + +auto j() -> enum { e3 }; // expected-error{{unnamed enumeration must be a definition}} expected-error {{requires a specifier or qualifier}} expected-error {{without trailing return type}} diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.mptr/p3.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.mptr/p3.cpp new file mode 100644 index 0000000..7e35788 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.mptr/p3.cpp @@ -0,0 +1,26 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s +class A { +public: + int& i; + + A(int& i) : i(i) { } + + static int s; +}; + +template<typename T> void ft(T& t) { + t.*&T::i = 10; // expected-error{{cannot form a pointer-to-member to member 'i' of reference type 'int &'}} +} + +void f() { + int b; + A a(b); + + int A::*ip = &A::s; // expected-error {{cannot initialize a variable of type 'int A::*' with an rvalue of type 'int *'}} + a.*&A::s = 10; // expected-error{{right hand operand to .* has non pointer-to-member type 'int *'}} + + a.*&A::i = 10; // expected-error{{cannot form a pointer-to-member to member 'i' of reference type 'int &'}} + ft(a); // expected-note{{in instantiation of function template specialization 'ft<A>' requested here}} + + void A::*p = 0; // expected-error{{'p' declared as a member pointer to void}} +} diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.ref/p5.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.ref/p5.cpp new file mode 100644 index 0000000..c02105c --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.ref/p5.cpp @@ -0,0 +1,145 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +// C++ [dcl.ref]p5: +// There shall be no references to references, no arrays of +// references, and no pointers to references. + +// The crazy formatting in here is to enforce the exact report locations. + +typedef int &intref; +typedef intref &intrefref; + +template <class T> class RefMem { // expected-warning{{class 'RefMem<int &>' does not declare any constructor to initialize its non-modifiable members}} + T + & + member; // expected-note{{reference member 'member' will never be initialized}} +}; + +struct RefRef { + int + & + & // expected-error {{declared as a reference to a reference}} + refref0; + + intref + & + refref1; // collapses + + intrefref + & + refref2; // collapses + + RefMem + < + int + & + > + refref3; // collapses expected-note{{in instantiation of template class 'RefMem<int &>' requested here}} +}; + + +template <class T> class PtrMem { + T + * // expected-error {{declared as a pointer to a reference}} + member; +}; + +struct RefPtr { + typedef + int + & + * // expected-error {{declared as a pointer to a reference}} + intrefptr; + + typedef + intref + * // expected-error {{declared as a pointer to a reference}} + intrefptr2; + + int + & + * // expected-error {{declared as a pointer to a reference}} + refptr0; + + intref + * // expected-error {{declared as a pointer to a reference}} + refptr1; + + PtrMem + < + int + & + > + refptr2; // expected-note {{in instantiation}} +}; + +template <class T> class ArrMem { + T + member + [ // expected-error {{declared as array of references}} + 10 + ]; +}; +template <class T, unsigned N> class DepArrMem { + T + member + [ // expected-error {{declared as array of references}} + N + ]; +}; + +struct RefArr { + typedef + int + & + intrefarr + [ // expected-error {{declared as array of references}} + 2 + ]; + + typedef + intref + intrefarr + [ // expected-error {{declared as array of references}} + 2 + ]; + + int + & + refarr0 + [ // expected-error {{declared as array of references}} + 2 + ]; + intref + refarr1 + [ // expected-error {{declared as array of references}} + 2 + ]; + ArrMem + < + int + & + > + refarr2; // expected-note {{in instantiation}} + DepArrMem + < + int + &, + 10 + > + refarr3; // expected-note {{in instantiation}} +}; + + +// The declaration of a reference shall contain an initializer +// (8.5.3) except when the declaration contains an explicit extern +// specifier (7.1.1), is a class member (9.2) declaration within a +// class definition, or is the declaration of a parameter or a +// return type (8.3.5); see 3.1. A reference shall be initialized to +// refer to a valid object or function. [ Note: in particular, a +// null reference cannot exist in a well-defined program, because +// the only way to create such a reference would be to bind it to +// the "object" obtained by dereferencing a null pointer, which +// causes undefined behavior. As described in 9.6, a reference +// cannot be bound directly to a bit-field. + diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.ref/p6-0x.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.ref/p6-0x.cpp new file mode 100644 index 0000000..4ce80bc --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.ref/p6-0x.cpp @@ -0,0 +1,26 @@ +// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s + +template<typename T, typename U> +struct is_same { + static const bool value = false; +}; + +template<typename T> +struct is_same<T, T> { + static const bool value = true; +}; +#define JOIN2(X,Y) X##Y +#define JOIN(X,Y) JOIN2(X,Y) +#define CHECK_EQUAL_TYPES(T1, T2) \ + int JOIN(array,__LINE__)[is_same<T1, T2>::value? 1 : -1] + +int i; +typedef int& LRI; +typedef int&& RRI; + +typedef LRI& r1; CHECK_EQUAL_TYPES(r1, int&); +typedef const LRI& r2; CHECK_EQUAL_TYPES(r2, int&); +typedef const LRI&& r3; CHECK_EQUAL_TYPES(r3, int&); + +typedef RRI& r4; CHECK_EQUAL_TYPES(r4, int&); +typedef RRI&& r5; CHECK_EQUAL_TYPES(r5, int&&); diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/p1-0x.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/p1-0x.cpp new file mode 100644 index 0000000..99334b8 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/p1-0x.cpp @@ -0,0 +1,24 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s + +// The nested-name-specifier of a qualified declarator-id shall not begin with a decltype-specifier. +class foo { + static int i; + void func(); +}; + +int decltype(foo())::i; // expected-error{{'decltype' cannot be used to name a declaration}} +void decltype(foo())::func() { // expected-error{{'decltype' cannot be used to name a declaration}} +} + + +template<typename T> +class tfoo { + static int i; + void func(); +}; + +template<typename T> +int decltype(tfoo<T>())::i; // expected-error{{nested name specifier 'decltype(tfoo<T>())::' for declaration does not refer into a class, class template or class template partial specialization}} +template<typename T> +void decltype(tfoo<T>())::func() { // expected-error{{nested name specifier 'decltype(tfoo<T>())::' for declaration does not refer into a class, class template or class template partial specialization}} +} diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/p1.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/p1.cpp new file mode 100644 index 0000000..3672ea0 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/p1.cpp @@ -0,0 +1,37 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +namespace PR8019 { + struct x; + template<typename T> struct x2; + struct y { + struct PR8019::x { int x; }; // expected-error{{non-friend class member 'x' cannot have a qualified name}} + + struct inner; + struct y::inner { }; // expected-warning{{extra qualification on member 'inner'}} + + template<typename T> + struct PR8019::x2 { }; // expected-error{{non-friend class member 'x2' cannot have a qualified name}} + + template<typename T> + struct inner_template; + + template<typename T> + struct y::inner_template { }; // expected-warning{{extra qualification on member 'inner_template'}} + }; + +} + +namespace NS { + void foo(); + extern int bar; + struct X; + template<typename T> struct Y; + template<typename T> void wibble(T); +} +namespace NS { + void NS::foo() {} // expected-warning{{extra qualification on member 'foo'}} + int NS::bar; // expected-warning{{extra qualification on member 'bar'}} + struct NS::X { }; // expected-warning{{extra qualification on member 'X'}} + template<typename T> struct NS::Y; // expected-warning{{extra qualification on member 'Y'}} + template<typename T> void NS::wibble(T) { } // expected-warning{{extra qualification on member 'wibble'}} +} diff --git a/clang/test/CXX/dcl.decl/dcl.name/p1.cpp b/clang/test/CXX/dcl.decl/dcl.name/p1.cpp new file mode 100644 index 0000000..9838b4f --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.name/p1.cpp @@ -0,0 +1,20 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +namespace pr6200 { + struct v {}; + enum E { e }; + struct s { + int i; + operator struct v() { return v(); }; + operator enum E() { return e; } + }; + + void f() + { + // None of these is a declaration. + (void)new struct s; + (void)new enum E; + (void)&s::operator struct v; + (void)&s::operator enum E; + } +} diff --git a/clang/test/CXX/dcl.decl/p4-0x.cpp b/clang/test/CXX/dcl.decl/p4-0x.cpp new file mode 100644 index 0000000..98c33b2 --- /dev/null +++ b/clang/test/CXX/dcl.decl/p4-0x.cpp @@ -0,0 +1,8 @@ +// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s + +struct X { + void f() &; + void g() &&; +}; + +void (X::*pmf)() & = &X::f; |