]> granicus.if.org Git - clang/commitdiff
Don't assume that a valid expression for the first part of a for-statement
authorRichard Smith <richard-llvm@metafoo.co.uk>
Tue, 21 Feb 2012 20:01:35 +0000 (20:01 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Tue, 21 Feb 2012 20:01:35 +0000 (20:01 +0000)
is non-null when diagnosing a broken attempt to write a for-range-statement.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@151081 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Parse/ParseStmt.cpp
test/SemaCXX/for-range-examples.cpp

index c256f7842deab5f4ae7a7c1193cb5c0be7dbb2f6..54f1b51f7e9a7ca9f4bdd2faa5765ea11cf868c8 100644 (file)
@@ -1396,8 +1396,7 @@ StmtResult Parser::ParseForStatement(ParsedAttributes &attrs,
         return StmtError();
       }
       Collection = ParseExpression();
-    } else if (getLang().CPlusPlus0x && Tok.is(tok::colon) &&
-               !FirstPart.isInvalid()) {
+    } else if (getLang().CPlusPlus0x && Tok.is(tok::colon) && FirstPart.get()) {
       // User tried to write the reasonable, but ill-formed, for-range-statement
       //   for (expr : expr) { ... }
       Diag(Tok, diag::err_for_range_expected_decl)
index 868de9d4f0e9c1379d61beaff4e62a069837dcd1..8bda51062a40bf9af47010c6c5e5cd85b191e216 100644 (file)
@@ -169,3 +169,14 @@ namespace test3 {
   template<typename T> void f() { for (auto a : A()) {} }
   void g() { f<int>(); }
 }
+
+namespace test4 {
+  void f() {
+    int y;
+
+    // Make sure these don't crash. Better diagnostics would be nice.
+    for (: {1, 2, 3}) {} // expected-error {{expected expression}} expected-error {{expected ';'}}
+    for (x : {1, 2, 3}) {} // expected-error {{undeclared identifier}} expected-error {{expected ';'}}
+    for (y : {1, 2, 3}) {} // expected-error {{must declare a variable}} expected-warning {{result unused}}
+  }
+}