2011-10-14 06:29:44 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -std=c++11 -Wall %s
|
2011-06-12 01:19:42 +08:00
|
|
|
|
|
|
|
struct Bitfield {
|
2017-08-28 08:28:14 +08:00
|
|
|
int n : 3 = 7; // expected-warning {{C++2a extension}} expected-warning {{changes value from 7 to -1}}
|
2011-06-12 01:19:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int a;
|
|
|
|
class NoWarning {
|
|
|
|
int &n = a;
|
|
|
|
public:
|
|
|
|
int &GetN() { return n; }
|
|
|
|
};
|
|
|
|
|
|
|
|
bool b();
|
|
|
|
int k;
|
2018-09-06 06:30:37 +08:00
|
|
|
struct Recurse { // expected-error {{initializer for 'n' needed}}
|
2016-11-23 06:55:12 +08:00
|
|
|
int &n = // expected-note {{declared here}}
|
2014-11-18 07:36:45 +08:00
|
|
|
b() ?
|
2018-09-06 06:30:37 +08:00
|
|
|
Recurse().n : // expected-note {{in evaluation of exception spec}}
|
2014-11-18 07:36:45 +08:00
|
|
|
k;
|
2011-06-12 01:19:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct UnknownBound {
|
|
|
|
int as[] = { 1, 2, 3 }; // expected-error {{array bound cannot be deduced from an in-class initializer}}
|
|
|
|
int bs[4] = { 4, 5, 6, 7 };
|
|
|
|
int cs[] = { 8, 9, 10 }; // expected-error {{array bound cannot be deduced from an in-class initializer}}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<int n> struct T { static const int B; };
|
|
|
|
template<> struct T<2> { template<int C, int D> using B = int; };
|
|
|
|
const int C = 0, D = 0;
|
|
|
|
struct S {
|
|
|
|
int as[] = { decltype(x)::B<C, D>(0) }; // expected-error {{array bound cannot be deduced from an in-class initializer}}
|
2012-05-09 16:23:23 +08:00
|
|
|
T<sizeof(as) / sizeof(int)> x;
|
2012-02-14 17:00:46 +08:00
|
|
|
// test that we handle invalid array bound deductions without crashing when the declarator name is itself invalid
|
|
|
|
operator int[](){}; // expected-error {{'operator int' cannot be the name of a variable or data member}} \
|
|
|
|
// expected-error {{array bound cannot be deduced from an in-class initializer}}
|
2011-06-12 01:19:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ThrowCtor { ThrowCtor(int) noexcept(false); };
|
|
|
|
struct NoThrowCtor { NoThrowCtor(int) noexcept(true); };
|
|
|
|
|
|
|
|
struct Throw { ThrowCtor tc = 42; };
|
|
|
|
struct NoThrow { NoThrowCtor tc = 42; };
|
|
|
|
|
|
|
|
static_assert(!noexcept(Throw()), "incorrect exception specification");
|
|
|
|
static_assert(noexcept(NoThrow()), "incorrect exception specification");
|
|
|
|
|
|
|
|
struct CheckExcSpec {
|
|
|
|
CheckExcSpec() noexcept(true) = default;
|
|
|
|
int n = 0;
|
|
|
|
};
|
|
|
|
struct CheckExcSpecFail {
|
2019-05-06 13:04:56 +08:00
|
|
|
CheckExcSpecFail() noexcept(true) = default; // ok, but calls terminate() on exception
|
2011-06-12 01:19:42 +08:00
|
|
|
ThrowCtor tc = 123;
|
|
|
|
};
|
2011-06-12 19:43:46 +08:00
|
|
|
|
|
|
|
struct TypedefInit {
|
|
|
|
typedef int A = 0; // expected-error {{illegal initializer}}
|
|
|
|
};
|
2011-09-08 04:36:12 +08:00
|
|
|
|
|
|
|
// PR10578 / <rdar://problem/9877267>
|
|
|
|
namespace PR10578 {
|
|
|
|
template<typename T>
|
|
|
|
struct X {
|
|
|
|
X() {
|
|
|
|
T* x = 1; // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Y : X<int> {
|
|
|
|
Y();
|
|
|
|
};
|
|
|
|
|
|
|
|
Y::Y() try { // expected-note{{in instantiation of member function 'PR10578::X<int>::X' requested here}}
|
|
|
|
} catch(...) {
|
|
|
|
}
|
|
|
|
}
|
2013-01-08 08:08:23 +08:00
|
|
|
|
|
|
|
namespace PR14838 {
|
|
|
|
struct base { ~base() {} };
|
|
|
|
class function : base {
|
|
|
|
~function() {} // expected-note {{implicitly declared private here}}
|
|
|
|
public:
|
|
|
|
function(...) {}
|
|
|
|
};
|
|
|
|
struct thing {};
|
|
|
|
struct another {
|
2018-07-18 06:24:09 +08:00
|
|
|
another() : r(thing()) {} // expected-error {{binds to a temporary object}}
|
2016-12-10 02:49:13 +08:00
|
|
|
// expected-error@-1 {{temporary of type 'PR14838::function' has private destructor}}
|
2013-01-08 08:08:23 +08:00
|
|
|
const function &r; // expected-note {{reference member declared here}}
|
|
|
|
} af;
|
|
|
|
}
|
2013-06-07 09:48:56 +08:00
|
|
|
|
|
|
|
namespace rdar14084171 {
|
|
|
|
struct Point { // expected-note 3 {{candidate constructor}}
|
|
|
|
double x;
|
|
|
|
double y;
|
|
|
|
};
|
|
|
|
struct Sprite {
|
|
|
|
Point location = Point(0,0); // expected-error {{no matching constructor for initialization of 'rdar14084171::Point'}}
|
|
|
|
};
|
[Sema] Extend -Wself-assign and -Wself-assign-field to warn on overloaded self-assignment (classes)
Summary:
This has just bit me, so i though it would be nice to avoid that next time :)
Motivational case:
https://godbolt.org/g/cq9UNk
Basically, it's likely to happen if you don't like shadowing issues,
and use `-Wshadow` and friends. And it won't be diagnosed by clang.
The reason is, these self-assign diagnostics only work for builtin assignment
operators. Which makes sense, one could have a very special operator=,
that does something unusual in case of self-assignment,
so it may make sense to not warn on that.
But while it may be intentional in some cases, it may be a bug in other cases,
so it would be really great to have some diagnostic about it...
Reviewers: aaron.ballman, rsmith, rtrieu, nikola, rjmccall, dblaikie
Reviewed By: rjmccall
Subscribers: EricWF, lebedev.ri, thakis, Quuxplusone, cfe-commits
Differential Revision: https://reviews.llvm.org/D44883
llvm-svn: 329493
2018-04-07 18:39:21 +08:00
|
|
|
void f(Sprite& x) { x = x; } // expected-warning {{explicitly assigning value of variable}}
|
2013-06-07 09:48:56 +08:00
|
|
|
}
|
2014-01-24 09:54:52 +08:00
|
|
|
|
|
|
|
namespace PR18560 {
|
|
|
|
struct X { int m; };
|
|
|
|
|
|
|
|
template<typename T = X,
|
|
|
|
typename U = decltype(T::m)>
|
|
|
|
int f();
|
|
|
|
|
|
|
|
struct Y { int b = f(); };
|
|
|
|
}
|
2014-11-18 07:36:45 +08:00
|
|
|
|
|
|
|
namespace template_valid {
|
|
|
|
// Valid, we shouldn't build a CXXDefaultInitExpr until A's ctor definition.
|
|
|
|
struct A {
|
|
|
|
A();
|
|
|
|
template <typename T>
|
|
|
|
struct B { int m1 = sizeof(A) + sizeof(T); };
|
|
|
|
B<int> m2;
|
|
|
|
};
|
|
|
|
A::A() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace template_default_ctor {
|
|
|
|
struct A {
|
|
|
|
template <typename T>
|
2018-09-06 06:30:37 +08:00
|
|
|
struct B { // expected-error {{initializer for 'm1' needed}}
|
2016-11-23 06:55:12 +08:00
|
|
|
int m1 = 0; // expected-note {{declared here}}
|
2014-11-18 07:36:45 +08:00
|
|
|
};
|
2018-09-06 06:30:37 +08:00
|
|
|
enum { NOE = noexcept(B<int>()) }; // expected-note {{in evaluation of exception spec}}
|
2014-11-18 07:36:45 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace default_ctor {
|
|
|
|
struct A {
|
2018-09-06 06:30:37 +08:00
|
|
|
struct B { // expected-error {{initializer for 'm1' needed}}
|
2016-11-23 06:55:12 +08:00
|
|
|
int m1 = 0; // expected-note {{declared here}}
|
2014-11-18 07:36:45 +08:00
|
|
|
};
|
2018-09-06 06:30:37 +08:00
|
|
|
enum { NOE = noexcept(B()) }; // expected-note {{in evaluation of exception spec}}
|
2014-11-18 07:36:45 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace member_template {
|
|
|
|
struct A {
|
|
|
|
template <typename T>
|
|
|
|
struct B {
|
2018-09-06 06:30:37 +08:00
|
|
|
struct C { // expected-error {{initializer for 'm1' needed}}
|
2016-11-23 06:55:12 +08:00
|
|
|
int m1 = 0; // expected-note {{declared here}}
|
2014-11-18 07:36:45 +08:00
|
|
|
};
|
|
|
|
template <typename U>
|
2018-09-06 06:30:37 +08:00
|
|
|
struct D { // expected-error {{initializer for 'm1' needed}}
|
2016-11-23 06:55:12 +08:00
|
|
|
int m1 = 0; // expected-note {{declared here}}
|
2014-11-18 07:36:45 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
enum {
|
2018-09-06 06:30:37 +08:00
|
|
|
NOE1 = noexcept(B<int>::C()), // expected-note {{in evaluation of exception spec}}
|
|
|
|
NOE2 = noexcept(B<int>::D<int>()) // expected-note {{in evaluation of exception spec}}
|
2014-11-18 07:36:45 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace explicit_instantiation {
|
|
|
|
template<typename T> struct X {
|
|
|
|
X(); // expected-note {{in instantiation of default member initializer 'explicit_instantiation::X<float>::n' requested here}}
|
|
|
|
int n = T::error; // expected-error {{type 'float' cannot be used prior to '::' because it has no members}}
|
|
|
|
};
|
|
|
|
template struct X<int>; // ok
|
|
|
|
template<typename T> X<T>::X() {}
|
|
|
|
template struct X<float>; // expected-note {{in instantiation of member function 'explicit_instantiation::X<float>::X' requested here}}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace local_class {
|
|
|
|
template<typename T> void f() {
|
|
|
|
struct X { // expected-note {{in instantiation of default member initializer 'local_class::f()::X::n' requested here}}
|
|
|
|
int n = T::error; // expected-error {{type 'int' cannot be used prior to '::' because it has no members}}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
void g() { f<int>(); } // expected-note {{in instantiation of function template specialization 'local_class::f<int>' requested here}}
|
|
|
|
}
|
2015-01-09 09:39:09 +08:00
|
|
|
|
|
|
|
namespace PR22056 {
|
|
|
|
template <int N>
|
|
|
|
struct S {
|
|
|
|
int x[3] = {[N] = 3};
|
|
|
|
};
|
|
|
|
}
|
2016-06-09 13:26:56 +08:00
|
|
|
|
|
|
|
namespace PR28060 {
|
|
|
|
template <class T>
|
|
|
|
void foo(T v) {
|
|
|
|
struct s {
|
|
|
|
T *s = 0;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
template void foo(int);
|
|
|
|
}
|