diff options
author | Carlo Zancanaro <carlo@pc-4w14-0.cs.usyd.edu.au> | 2012-10-15 17:10:06 +1100 |
---|---|---|
committer | Carlo Zancanaro <carlo@pc-4w14-0.cs.usyd.edu.au> | 2012-10-15 17:10:06 +1100 |
commit | be1de4be954c80875ad4108e0a33e8e131b2f2c0 (patch) | |
tree | 1fbbecf276bf7c7bdcbb4dd446099d6d90eaa516 /clang/test/SemaCXX/warn-empty-body.cpp | |
parent | c4626a62754862d20b41e8a46a3574264ea80e6d (diff) | |
parent | f1bd2e48c5324d3f7cda4090c87f8a5b6f463ce2 (diff) |
Merge branch 'master' of ssh://bitbucket.org/czan/honours
Diffstat (limited to 'clang/test/SemaCXX/warn-empty-body.cpp')
-rw-r--r-- | clang/test/SemaCXX/warn-empty-body.cpp | 271 |
1 files changed, 271 insertions, 0 deletions
diff --git a/clang/test/SemaCXX/warn-empty-body.cpp b/clang/test/SemaCXX/warn-empty-body.cpp new file mode 100644 index 0000000..d643ced --- /dev/null +++ b/clang/test/SemaCXX/warn-empty-body.cpp @@ -0,0 +1,271 @@ +// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s + +void a(int i); +int b(); +int c(); + +void test1(int x, int y) { + while(true) { + if (x); // expected-warning {{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}} + + int i; + // PR11329 + for (i = 0; i < x; i++); { // expected-warning{{for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}} + a(i); + b(); + } + + for (i = 0; i < x; i++); // expected-warning{{for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}} + { + a(i); + } + + for (i = 0; + i < x; + i++); // expected-warning{{for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}} + { + a(i); + } + + int arr[3] = { 1, 2, 3 }; + for (int j : arr); // expected-warning{{range-based for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}} + a(i); + + for (int j : + arr); // expected-warning{{range-based for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}} + a(i); + + while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}} + a(i); + + while (b() == 0); { // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}} + a(i); + } + + while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}} + { + a(i); + } + + while (b() == 0 || + c() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}} + { + a(i); + } + + do; // expected-note{{to match this 'do'}} + b(); // expected-error{{expected 'while' in do/while loop}} + while (b()); // no-warning + c(); + + do; // expected-note{{to match this 'do'}} + b(); // expected-error{{expected 'while' in do/while loop}} + while (b()); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}} + c(); + + switch(x) // no-warning + { + switch(y); // expected-warning{{switch statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}} + { + case 0: + a(10); + break; + default: + a(20); + break; + } + } + } +} + +/// There should be no warning when null statement is placed on its own line. +void test2(int x, int y) { + if (x) // no-warning + ; // no-warning + + int i; + for (i = 0; i < x; i++) // no-warning + ; // no-warning + + for (i = 0; + i < x; + i++) // no-warning + ; // no-warning + + int arr[3] = { 1, 2, 3 }; + for (int j : arr) // no-warning + ; // no-warning + + while (b() == 0) // no-warning + ; // no-warning + + while (b() == 0 || + c() == 0) // no-warning + ; // no-warning + + switch(x) + { + switch(y) // no-warning + ; // no-warning + } + + // Last `for' or `while' statement in compound statement shouldn't warn. + while(b() == 0); // no-warning +} + +/// There should be no warning for a null statement resulting from an empty macro. +#define EMPTY(a) +void test3(int x, int y) { + if (x) EMPTY(x); // no-warning + + int i; + for (i = 0; i < x; i++) EMPTY(i); // no-warning + + for (i = 0; + i < x; + i++) EMPTY(i); // no-warning + + int arr[3] = { 1, 2, 3 }; + for (int j : arr) EMPTY(j); // no-warning + + for (int j : + arr) EMPTY(j); // no-warning + + while (b() == 0) EMPTY(i); // no-warning + + while (b() == 0 || + c() == 0) EMPTY(i); // no-warning + + switch (x) { + switch (y) + EMPTY(i); // no-warning + } +} + +void test4(int x) +{ + // Idiom used in some metaprogramming constructs. + switch (x) default:; // no-warning + + // Frequent idiom used in macros. + do {} while (false); // no-warning +} + +/// There should be no warning for a common for/while idiom when it is obvious +/// from indentation that next statement wasn't meant to be a body. +void test5(int x, int y) { + int i; + for (i = 0; i < x; i++); // expected-warning{{for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}} + a(i); + + for (i = 0; i < x; i++); // no-warning + a(i); + + for (i = 0; + i < x; + i++); // expected-warning{{for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}} + a(i); + + for (i = 0; + i < x; + i++); // no-warning + a(i); + + while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}} + a(i); + + while (b() == 0); // no-warning + a(i); + + while (b() == 0 || + c() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}} + a(i); + + while (b() == 0 || + c() == 0); // no-warning + a(i); +} + +/// There should be no warning for a statement with a non-null body. +void test6(int x, int y) { + if (x) {} // no-warning + + if (x) + a(x); // no-warning + + int i; + for (i = 0; i < x; i++) // no-warning + a(i); // no-warning + + for (i = 0; i < x; i++) { // no-warning + a(i); // no-warning + } + + for (i = 0; + i < x; + i++) // no-warning + a(i); // no-warning + + int arr[3] = { 1, 2, 3 }; + for (int j : arr) // no-warning + a(j); + + for (int j : arr) {} // no-warning + + while (b() == 0) // no-warning + a(i); // no-warning + + while (b() == 0) {} // no-warning + + switch(x) // no-warning + { + switch(y) // no-warning + { + case 0: + a(10); + break; + default: + a(20); + break; + } + } +} + +void test_errors(int x) { + if (1) + aa; // expected-error{{use of undeclared identifier}} + // no empty body warning. + + int i; + for (i = 0; i < x; i++) + bb; // expected-error{{use of undeclared identifier}} + + int arr[3] = { 1, 2, 3 }; + for (int j : arr) + cc; // expected-error{{use of undeclared identifier}} + + while (b() == 0) + dd; // expected-error{{use of undeclared identifier}} +} + +// Warnings for statements in templates shouldn't be duplicated for all +// instantiations. +template <typename T> +void test_template(int x) { + if (x); // expected-warning{{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}} + + if (x) + EMPTY(x); // no-warning + + int arr[3] = { 1, 2, 3 }; + for (int j : arr); // expected-warning{{range-based for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}} + + while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}} + a(x); +} + +void test_template_inst(int x) { + test_template<int>(x); + test_template<double>(x); +} + |