2014-03-26 23:58:20 +08:00
|
|
|
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wglobal-constructors %s -verify
|
2010-08-02 04:20:59 +08:00
|
|
|
|
|
|
|
int opaque_int();
|
|
|
|
|
|
|
|
namespace test0 {
|
|
|
|
// These should never require global constructors.
|
|
|
|
int a;
|
|
|
|
int b = 20;
|
|
|
|
float c = 5.0f;
|
|
|
|
|
|
|
|
// This global constructor is avoidable based on initialization order.
|
|
|
|
int d = b; // expected-warning {{global constructor}}
|
|
|
|
|
|
|
|
// These global constructors are unavoidable.
|
|
|
|
int e = opaque_int(); // expected-warning {{global constructor}}
|
|
|
|
int f = b; // expected-warning {{global constructor}}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace test1 {
|
|
|
|
struct A { int x; };
|
|
|
|
A a;
|
|
|
|
A b = A();
|
|
|
|
A c = { 10 };
|
|
|
|
A d = { opaque_int() }; // expected-warning {{global constructor}}
|
2010-08-03 05:13:48 +08:00
|
|
|
A e = A(A());
|
|
|
|
A f = A(a); // expected-warning {{global constructor}}
|
|
|
|
A g(a); // expected-warning {{global constructor}}
|
2010-09-15 18:14:12 +08:00
|
|
|
A h((A())); // elided
|
|
|
|
A i((A(A()))); // elided
|
2010-08-02 04:20:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace test2 {
|
|
|
|
struct A { A(); };
|
|
|
|
A a; // expected-warning {{global constructor}}
|
|
|
|
A b[10]; // expected-warning {{global constructor}}
|
|
|
|
A c[10][10]; // expected-warning {{global constructor}}
|
2010-08-02 05:51:45 +08:00
|
|
|
|
2010-08-03 05:13:48 +08:00
|
|
|
A &d = a;
|
|
|
|
A &e = b[5];
|
|
|
|
A &f = c[5][7];
|
2010-08-02 04:20:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace test3 {
|
|
|
|
struct A { ~A(); };
|
|
|
|
A a; // expected-warning {{global destructor}}
|
|
|
|
A b[10]; // expected-warning {{global destructor}}
|
|
|
|
A c[10][10]; // expected-warning {{global destructor}}
|
2010-08-02 05:51:45 +08:00
|
|
|
|
2010-08-03 05:13:48 +08:00
|
|
|
A &d = a;
|
|
|
|
A &e = b[5];
|
|
|
|
A &f = c[5][7];
|
2010-08-02 05:51:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace test4 {
|
|
|
|
char a[] = "hello";
|
2011-04-11 08:23:45 +08:00
|
|
|
char b[6] = "hello";
|
|
|
|
char c[][6] = { "hello" };
|
2010-08-02 04:20:59 +08:00
|
|
|
}
|
2010-09-03 09:11:38 +08:00
|
|
|
|
|
|
|
namespace test5 {
|
|
|
|
struct A { A(); };
|
|
|
|
|
|
|
|
void f1() {
|
|
|
|
static A a;
|
|
|
|
}
|
|
|
|
void f2() {
|
|
|
|
static A& a = *new A;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace test6 {
|
|
|
|
struct A { ~A(); };
|
|
|
|
|
|
|
|
void f1() {
|
2010-09-18 13:25:11 +08:00
|
|
|
static A a;
|
2010-09-03 09:11:38 +08:00
|
|
|
}
|
|
|
|
void f2() {
|
|
|
|
static A& a = *new A;
|
|
|
|
}
|
2010-09-08 12:46:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace pr8095 {
|
|
|
|
struct Foo {
|
|
|
|
int x;
|
|
|
|
Foo(int x1) : x(x1) {}
|
|
|
|
};
|
2010-09-18 13:25:11 +08:00
|
|
|
void foo() {
|
|
|
|
static Foo a(0);
|
|
|
|
}
|
2010-09-03 09:11:38 +08:00
|
|
|
|
2010-09-18 13:25:11 +08:00
|
|
|
struct Bar {
|
|
|
|
~Bar();
|
|
|
|
};
|
2010-09-08 12:46:19 +08:00
|
|
|
void bar() {
|
2010-09-18 13:25:11 +08:00
|
|
|
static Bar b;
|
2010-09-08 12:46:19 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-17 06:40:53 +08:00
|
|
|
|
|
|
|
namespace referencemember {
|
|
|
|
struct A { int &a; };
|
|
|
|
int a;
|
|
|
|
A b = { a };
|
|
|
|
}
|
2014-03-26 23:58:20 +08:00
|
|
|
|
|
|
|
namespace pr19253 {
|
|
|
|
struct A { ~A() = default; };
|
|
|
|
A a;
|
2014-03-28 04:23:36 +08:00
|
|
|
|
|
|
|
struct B { ~B(); };
|
2014-03-26 23:58:20 +08:00
|
|
|
struct C : B { ~C() = default; };
|
|
|
|
C c; // expected-warning {{global destructor}}
|
2014-03-28 04:23:36 +08:00
|
|
|
|
|
|
|
class D {
|
|
|
|
friend struct E;
|
|
|
|
~D() = default;
|
|
|
|
};
|
|
|
|
struct E : D {
|
|
|
|
D d;
|
|
|
|
~E() = default;
|
|
|
|
};
|
|
|
|
E e;
|
2014-03-26 23:58:20 +08:00
|
|
|
}
|
2014-07-24 07:24:25 +08:00
|
|
|
|
|
|
|
namespace pr20420 {
|
|
|
|
// No warning is expected. This used to crash.
|
|
|
|
void *array_storage[1];
|
|
|
|
const int &global_reference = *(int *)array_storage;
|
|
|
|
}
|
2017-10-24 10:17:07 +08:00
|
|
|
|
|
|
|
namespace bitfields {
|
|
|
|
struct HasUnnamedBitfield {
|
|
|
|
unsigned a;
|
|
|
|
unsigned : 20;
|
|
|
|
unsigned b;
|
|
|
|
|
|
|
|
constexpr HasUnnamedBitfield() : a(), b() {}
|
|
|
|
constexpr HasUnnamedBitfield(unsigned a, unsigned b) : a(a), b(b) {}
|
|
|
|
explicit HasUnnamedBitfield(unsigned a) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
const HasUnnamedBitfield zeroConst{};
|
|
|
|
HasUnnamedBitfield zeroMutable{};
|
|
|
|
const HasUnnamedBitfield explicitConst{1, 2};
|
|
|
|
HasUnnamedBitfield explicitMutable{1, 2};
|
|
|
|
const HasUnnamedBitfield nonConstexprConst{1}; // expected-warning {{global constructor}}
|
|
|
|
HasUnnamedBitfield nonConstexprMutable{1}; // expected-warning {{global constructor}}
|
|
|
|
}
|