<rdar://problem/13540921> Cope with instantiations of the C++11 range-based for loop...
authorDouglas Gregor <dgregor@apple.com>
Mon, 8 Apr 2013 18:40:13 +0000 (18:40 +0000)
committerDouglas Gregor <dgregor@apple.com>
Mon, 8 Apr 2013 18:40:13 +0000 (18:40 +0000)
llvm-svn: 179037

clang/lib/Sema/TreeTransform.h
clang/test/SemaObjCXX/foreach.mm

index bdd68a7..f68de26 100644 (file)
@@ -1338,6 +1338,20 @@ public:
                                     Expr *Cond, Expr *Inc,
                                     Stmt *LoopVar,
                                     SourceLocation RParenLoc) {
+    // If we've just learned that the range is actually an Objective-C
+    // collection, treat this as an Objective-C fast enumeration loop.
+    if (DeclStmt *RangeStmt = dyn_cast<DeclStmt>(Range)) {
+      if (RangeStmt->isSingleDecl()) {
+        if (VarDecl *RangeVar = dyn_cast<VarDecl>(RangeStmt->getSingleDecl())) {
+          Expr *RangeExpr = RangeVar->getInit();
+          if (!RangeExpr->isTypeDependent() &&
+              RangeExpr->getType()->isObjCObjectPointerType())
+            return getSema().ActOnObjCForCollectionStmt(ForLoc, LoopVar, RangeExpr,
+                                                        RParenLoc);
+        }
+      }
+    }
+
     return getSema().BuildCXXForRangeStmt(ForLoc, ColonLoc, Range, BeginEnd,
                                           Cond, Inc, LoopVar, RParenLoc,
                                           Sema::BFRK_Rebuild);
index ec6ed05..ac787bd 100644 (file)
@@ -16,6 +16,14 @@ void f(NSArray *a) {
   for (auto thisKey : keys) { } // expected-warning{{'auto' deduced as 'id' in declaration of 'thisKey'}}
 }
 
+template<typename Collection>
+void ft(Collection col) {
+  for (id x : col) { }
+  for (auto x : col) { }
+}
+
+template void ft(NSArray *);
+
 /* // rdar://9072298 */
 @protocol NSObject @end