summaryrefslogtreecommitdiffstats
path: root/clang/test
diff options
context:
space:
mode:
authorRichard Trieu <rtrieu@google.com>2013-08-06 21:31:54 +0000
committerRichard Trieu <rtrieu@google.com>2013-08-06 21:31:54 +0000
commit4e7c9628917af377f9927a8cf96e7444d3b86c81 (patch)
tree20b7cb0b8254851877856d3d329e544a02b6a899 /clang/test
parentadaaec9aeae2978e919a3037cd0036c7a4397687 (diff)
downloadbcm5719-llvm-4e7c9628917af377f9927a8cf96e7444d3b86c81.tar.gz
bcm5719-llvm-4e7c9628917af377f9927a8cf96e7444d3b86c81.zip
Add a new warning to -Wloop-analysis to detect suspicious increments or
decrements inside for loops. Idea for this warning proposed in PR15636: http://llvm.org/bugs/show_bug.cgi?id=15636 llvm-svn: 187817
Diffstat (limited to 'clang/test')
-rw-r--r--clang/test/SemaCXX/warn-loop-analysis.cpp108
1 files changed, 108 insertions, 0 deletions
diff --git a/clang/test/SemaCXX/warn-loop-analysis.cpp b/clang/test/SemaCXX/warn-loop-analysis.cpp
index 627bc51d1b0..c666c48fc01 100644
--- a/clang/test/SemaCXX/warn-loop-analysis.cpp
+++ b/clang/test/SemaCXX/warn-loop-analysis.cpp
@@ -152,3 +152,111 @@ void test6() {
for (;x6;);
for (;y;);
}
+
+void test7() {
+ int i;
+ for (;;i++) { // expected-note{{incremented here}}
+ if (true) test7();
+ i++; // expected-warning{{incremented both}}
+ }
+ for (;;i++) { // expected-note{{incremented here}}
+ if (true) break;
+ ++i; // expected-warning{{incremented both}}
+ }
+ for (;;++i) { // expected-note{{incremented here}}
+ while (true) return;
+ i++; // expected-warning{{incremented both}}
+ }
+ for (;;++i) { // expected-note{{incremented here}}
+ ++i; // expected-warning{{incremented both}}
+ }
+
+ for (;;i--) { // expected-note{{decremented here}}
+ if (true) test7();
+ i--; // expected-warning{{decremented both}}
+ }
+ for (;;i--) { // expected-note{{decremented here}}
+ if (true) break;
+ --i; // expected-warning{{decremented both}}
+ }
+ for (;;--i) { // expected-note{{decremented here}}
+ while (true) return;
+ i--; // expected-warning{{decremented both}}
+ }
+ for (;;--i) { // expected-note{{decremented here}}
+ --i; // expected-warning{{decremented both}}
+ }
+
+ // Don't warn when loop is only one statement.
+ for (;;++i)
+ i++;
+ for (;;--i)
+ --i;
+
+ // Don't warn when loop has continue statement.
+ for (;;i++) {
+ if (true) continue;
+ i++;
+ }
+ for (;;i--) {
+ if (true) continue;
+ i--;
+ }
+}
+
+struct iterator {
+ iterator operator++() { return *this; }
+ iterator operator++(int) { return *this; }
+ iterator operator--() { return *this; }
+ iterator operator--(int) { return *this; }
+};
+void test8() {
+ iterator i;
+ for (;;i++) { // expected-note{{incremented here}}
+ if (true) test7();
+ i++; // expected-warning{{incremented both}}
+ }
+ for (;;i++) { // expected-note{{incremented here}}
+ if (true) break;
+ ++i; // expected-warning{{incremented both}}
+ }
+ for (;;++i) { // expected-note{{incremented here}}
+ while (true) return;
+ i++; // expected-warning{{incremented both}}
+ }
+ for (;;++i) { // expected-note{{incremented here}}
+ ++i; // expected-warning{{incremented both}}
+ }
+
+ for (;;i--) { // expected-note{{decremented here}}
+ if (true) test7();
+ i--; // expected-warning{{decremented both}}
+ }
+ for (;;i--) { // expected-note{{decremented here}}
+ if (true) break;
+ --i; // expected-warning{{decremented both}}
+ }
+ for (;;--i) { // expected-note{{decremented here}}
+ while (true) return;
+ i--; // expected-warning{{decremented both}}
+ }
+ for (;;--i) { // expected-note{{decremented here}}
+ --i; // expected-warning{{decremented both}}
+ }
+
+ // Don't warn when loop is only one statement.
+ for (;;++i)
+ i++;
+ for (;;--i)
+ --i;
+
+ // Don't warn when loop has continue statement.
+ for (;;i++) {
+ if (true) continue;
+ i++;
+ }
+ for (;;i--) {
+ if (true) continue;
+ i--;
+ }
+}
OpenPOWER on IntegriCloud