diff options
author | Richard Smith <richard-llvm@metafoo.co.uk> | 2013-01-17 01:17:56 +0000 |
---|---|---|
committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2013-01-17 01:17:56 +0000 |
commit | c406cb73648483276b6c938ab5ff6812c3fbcad9 (patch) | |
tree | f8b5078f11db2fc135628f5ef1e7583c6cf57a5c /clang/test/SemaCXX/warn-unsequenced.cpp | |
parent | 8c6cbe776ba85be759a9fa72518349767e0bc889 (diff) | |
download | bcm5719-llvm-c406cb73648483276b6c938ab5ff6812c3fbcad9.tar.gz bcm5719-llvm-c406cb73648483276b6c938ab5ff6812c3fbcad9.zip |
Add -Wunsequenced (with compatibility alias -Wsequence-point) to warn on
expressions which have undefined behavior due to multiple unsequenced
modifications or an unsequenced modification and use of a variable.
llvm-svn: 172690
Diffstat (limited to 'clang/test/SemaCXX/warn-unsequenced.cpp')
-rw-r--r-- | clang/test/SemaCXX/warn-unsequenced.cpp | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/clang/test/SemaCXX/warn-unsequenced.cpp b/clang/test/SemaCXX/warn-unsequenced.cpp new file mode 100644 index 00000000000..dcac0975867 --- /dev/null +++ b/clang/test/SemaCXX/warn-unsequenced.cpp @@ -0,0 +1,91 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wno-unused %s + +int f(int, int); + +struct A { + int x, y; +}; +struct S { + S(int, int); +}; + +void test() { + int a; + int xs[10]; + ++a = 0; // ok + a + ++a; // expected-warning {{unsequenced modification and access to 'a'}} + a = ++a; // ok + a + a++; // expected-warning {{unsequenced modification and access to 'a'}} + a = a++; // expected-warning {{multiple unsequenced modifications to 'a'}} + ++ ++a; // ok + (a++, a++); // ok + ++a + ++a; // expected-warning {{multiple unsequenced modifications to 'a'}} + a++ + a++; // expected-warning {{multiple unsequenced modifications}} + (a++, a) = 0; // ok, increment is sequenced before value computation of LHS + a = xs[++a]; // ok + a = xs[a++]; // expected-warning {{multiple unsequenced modifications}} + (a ? xs[0] : xs[1]) = ++a; // expected-warning {{unsequenced modification and access}} + a = (++a, ++a); // ok + a = (a++, ++a); // ok + a = (a++, a++); // expected-warning {{multiple unsequenced modifications}} + f(a, a); // ok + f(a = 0, a); // expected-warning {{unsequenced modification and access}} + f(a, a += 0); // expected-warning {{unsequenced modification and access}} + f(a = 0, a = 0); // expected-warning {{multiple unsequenced modifications}} + + // Compound assignment "A OP= B" is equivalent to "A = A OP B" except that A + // is evaluated only once. + (++a, a) = 1; // ok + (++a, a) += 1; // ok + a = ++a; // ok + a += ++a; // expected-warning {{unsequenced modification and access}} + + A agg1 = { a++, a++ }; // ok + A agg2 = { a++ + a, a++ }; // expected-warning {{unsequenced modification and access}} + + S str1(a++, a++); // expected-warning {{multiple unsequenced modifications}} + S str2 = { a++, a++ }; // ok + S str3 = { a++ + a, a++ }; // expected-warning {{unsequenced modification and access}} + + (xs[2] && (a = 0)) + a; // ok + (0 && (a = 0)) + a; // ok + (1 && (a = 0)) + a; // expected-warning {{unsequenced modification and access}} + + (xs[3] || (a = 0)) + a; // ok + (0 || (a = 0)) + a; // expected-warning {{unsequenced modification and access}} + (1 || (a = 0)) + a; // ok + + (xs[4] ? a : ++a) + a; // ok + (0 ? a : ++a) + a; // expected-warning {{unsequenced modification and access}} + (1 ? a : ++a) + a; // ok + (xs[5] ? ++a : ++a) + a; // FIXME: warn here + + (++a, xs[6] ? ++a : 0) + a; // expected-warning {{unsequenced modification and access}} + + // Here, the read of the fourth 'a' might happen before or after the write to + // the second 'a'. + a += (a++, a) + a; // expected-warning {{unsequenced modification and access}} + + int *p = xs; + a = *(a++, p); // ok + a = a++ && a; // ok + + A *q = &agg1; + (q = &agg2)->y = q->x; // expected-warning {{unsequenced modification and access to 'q'}} + + // This has undefined behavior if a == 0; otherwise, the side-effect of the + // increment is sequenced before the value computation of 'f(a, a)', which is + // sequenced before the value computation of the '&&', which is sequenced + // before the assignment. We treat the sequencing in '&&' as being + // unconditional. + a = a++ && f(a, a); + + // This has undefined behavior if a != 0. FIXME: We should diagnose this. + (a && a++) + a; + + (xs[7] && ++a) * (!xs[7] && ++a); // ok + + xs[0] = (a = 1, a); // ok + (a -= 128) &= 128; // ok + ++a += 1; // ok +} |