On Mon, Jan 23, 2012 at 4:40 PM, Dmitri Gribenko <[email protected]> wrote:
> Please review.  Patch rebased to latest trunk and split in two.

And a test.

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <[email protected]>*/
Index: test/SemaCXX/warn-empty-body.cpp
===================================================================
--- test/SemaCXX/warn-empty-body.cpp	(revision 0)
+++ test/SemaCXX/warn-empty-body.cpp	(revision 0)
@@ -0,0 +1,265 @@
+// 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}}
+
+    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}}
+      a(i);
+
+    for (int j : arr)
+    {} // expected-warning{{range-based for loop has empty body}}
+
+    for (int j :
+         arr); // expected-warning{{range-based for loop has empty body}}
+      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);
+    }
+
+    switch(x) // no-warning
+    {
+      switch(y); // expected-warning{{switch statement has empty body}}
+      {
+        case 0:
+          a(10);
+          break;
+        default:
+          a(20);
+          break;
+      }
+    }
+  }
+}
+
+/// There should be no warning in `for' and `while' when null statement is
+/// placed on its own line.  Range-based `for' and `switch' should warn.
+void test2(int x, int y) {
+  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)
+    ; // expected-warning{{range-based for loop has empty body}}
+
+  while (b() == 0) // no-warning
+    ; // no-warning
+
+  while (b() == 0 ||
+         c() == 0) // no-warning
+    ; // no-warning
+
+  switch(x)
+  {
+    switch(y)
+      ; // expected-warning{{switch statement has empty body}}
+  }
+
+  // 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
+  }
+}
+
+/// 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 test4(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 test5(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
+    a(j);
+
+  while (b() == 0) // no-warning
+    a(i); // no-warning
+
+  while (b() == 0) { // no-warning
+    a(i); // no-warning
+  }
+
+  while (b() == 0 ||
+         c() == 0) // no-warning
+    a(i); // no-warning
+
+  while (b() == 0 ||
+         c() == 0) { // no-warning
+    a(i); // 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}}
+
+  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}}
+}
+
+template <typename T>
+void test_template(int x) {
+  if (x); // expected-warning{{if statement has empty body}} \
+             expected-warning{{if statement has empty body}}
+
+  if (x)
+    EMPTY(x); // no-warning
+
+  while (b() == 0); // expected-warning{{while loop has empty body}} \
+                       expected-warning{{while loop has empty body}} \
+                       expected-note{{put the semicolon on a separate line to silence this warning}} \
+                       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); // expected-note{{requested here}}
+}
+
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to