summaryrefslogtreecommitdiff
path: root/clang/test/SemaCXX/warn-global-constructors.cpp
blob: 6330958df703e8b9006880d7bd65b0b1742d28a3 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// RUN: %clang_cc1 -fsyntax-only -Wglobal-constructors %s -verify

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}}
  A e = A(A());
  A f = A(a); // expected-warning {{global constructor}}
  A g(a); // expected-warning {{global constructor}}
  A h((A()));  // elided
  A i((A(A()))); // elided
}

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}}

  A &d = a;
  A &e = b[5];
  A &f = c[5][7];
}

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}}

  A &d = a;
  A &e = b[5];
  A &f = c[5][7];
}

namespace test4 {
  char a[] = "hello";
  char b[6] = "hello";
  char c[][6] = { "hello" };
}

namespace test5 {
  struct A { A(); };

  void f1() {
    static A a;
  }
  void f2() {
    static A& a = *new A;
  }
}

namespace test6 {
  struct A { ~A(); };

  void f1() {
    static A a;
  }
  void f2() {
    static A& a = *new A;
  }
}

namespace pr8095 {
  struct Foo {
    int x;
    Foo(int x1) : x(x1) {}
  };
  void foo() {
    static Foo a(0);
  }

  struct Bar {
    ~Bar();
  };
  void bar() {
    static Bar b;
  }
}