summaryrefslogtreecommitdiffstats
path: root/clang/test/SemaCXX/uninitialized.cpp
diff options
context:
space:
mode:
authorRichard Trieu <rtrieu@google.com>2013-09-20 03:03:06 +0000
committerRichard Trieu <rtrieu@google.com>2013-09-20 03:03:06 +0000
commit406e65c8d1754985b04f74fec42edd47a0844934 (patch)
tree16d50c11e5fadf0f131c2e83c314312b21fabc36 /clang/test/SemaCXX/uninitialized.cpp
parentb8c65f0136faf07eb2d33a3d0436adae41b388a9 (diff)
downloadbcm5719-llvm-406e65c8d1754985b04f74fec42edd47a0844934.tar.gz
bcm5719-llvm-406e65c8d1754985b04f74fec42edd47a0844934.zip
Modify the uninitialized field visitor to detect uninitialized use across the
fields in the class. This allows a better checking of member intiailizers and in class initializers in regards to initialization ordering. For instance, this code will now produce warnings: class A { int x; int y; A() : x(y) {} // y is initialized after x, warn here A(int): y(x) {} // default initialization of leaves x uninitialized, warn here }; Several test cases were updated with -Wno-uninitialized to silence this warning. llvm-svn: 191068
Diffstat (limited to 'clang/test/SemaCXX/uninitialized.cpp')
-rw-r--r--clang/test/SemaCXX/uninitialized.cpp111
1 files changed, 109 insertions, 2 deletions
diff --git a/clang/test/SemaCXX/uninitialized.cpp b/clang/test/SemaCXX/uninitialized.cpp
index 31895c6e02c..2fb0482cacb 100644
--- a/clang/test/SemaCXX/uninitialized.cpp
+++ b/clang/test/SemaCXX/uninitialized.cpp
@@ -340,7 +340,10 @@ namespace {
};
struct E {
- int a, b, c;
+ int b = 1;
+ int c = 1;
+ int a; // This field needs to be last to prevent the cross field
+ // uninitialized warning.
E(char (*)[1]) : a(a ? b : c) {} // expected-warning {{field 'a' is uninitialized when used here}}
E(char (*)[2]) : a(b ? a : a) {} // expected-warning 2{{field 'a' is uninitialized when used here}}
E(char (*)[3]) : a(b ? (a) : c) {} // expected-warning {{field 'a' is uninitialized when used here}}
@@ -459,7 +462,7 @@ namespace in_class_initializers {
};
struct U {
- U() : a(b + 1), b(a + 1) {} // FIXME: Warn here.
+ U() : a(b + 1), b(a + 1) {} // expected-warning{{field 'b' is uninitialized when used here}}
int a = 42; // Note: because a and b are in the member initializer list, these initializers are ignored.
int b = 1;
};
@@ -561,3 +564,107 @@ namespace record_fields {
A a9 = normal(a9); // expected-warning {{uninitialized}}
};
}
+
+namespace cross_field_warnings {
+ struct A {
+ int a, b;
+ A() {}
+ A(char (*)[1]) : b(a) {} // expected-warning{{field 'a' is uninitialized when used here}}
+ A(char (*)[2]) : a(b) {} // expected-warning{{field 'b' is uninitialized when used here}}
+ };
+
+ struct B {
+ int a = b; // expected-warning{{field 'b' is uninitialized when used here}}
+ int b;
+ };
+
+ struct C {
+ int a;
+ int b = a; // expected-warning{{field 'a' is uninitialized when used here}}
+ C(char (*)[1]) : a(5) {}
+ C(char (*)[2]) {}
+ };
+
+ struct D {
+ int a;
+ int &b;
+ int &c = a;
+ int d = b;
+ D() : b(a) {}
+ };
+
+ struct E {
+ int a;
+ int get();
+ static int num();
+ E() {}
+ E(int) {}
+ };
+
+ struct F {
+ int a;
+ E e;
+ int b;
+ F(char (*)[1]) : a(e.get()) {} // expected-warning{{field 'e' is uninitialized when used here}}
+ F(char (*)[2]) : a(e.num()) {}
+ F(char (*)[3]) : e(a) {} // expected-warning{{field 'a' is uninitialized when used here}}
+ F(char (*)[4]) : a(4), e(a) {}
+ F(char (*)[5]) : e(b) {} // expected-warning{{field 'b' is uninitialized when used here}}
+ F(char (*)[6]) : e(b), b(4) {} // expected-warning{{field 'b' is uninitialized when used here}}
+ };
+
+ struct G {
+ G(const A&) {};
+ };
+
+ struct H {
+ A a1;
+ G g;
+ A a2;
+ H() : g(a1) {}
+ H(int) : g(a2) {}
+ };
+
+ struct I {
+ I(int*) {}
+ };
+
+ struct J : public I {
+ int *a;
+ int *b;
+ int c;
+ J() : I((a = new int(5))), b(a), c(*a) {}
+ };
+
+ struct K {
+ int a = (b = 5);
+ int b = b + 5;
+ };
+
+ struct L {
+ int a = (b = 5);
+ int b = b + 5; // expected-warning{{field 'b' is uninitialized when used here}}
+ L() : a(5) {} // expected-note{{during field initialization in this constructor}}
+ };
+
+ struct M { };
+
+ struct N : public M {
+ int a;
+ int b;
+ N() : b(a) { } // expected-warning{{field 'a' is uninitialized when used here}}
+ };
+
+ struct O {
+ int x = 42;
+ int get() { return x; }
+ };
+
+ struct P {
+ O o;
+ int x = o.get();
+ P() : x(o.get()) { }
+ };
+
+}
+
OpenPOWER on IntegriCloud