]> granicus.if.org Git - clang/commitdiff
<rdar://problem/13540921> Cope with deduced 'auto' in a C++11 for-range loop that...
authorDouglas Gregor <dgregor@apple.com>
Mon, 8 Apr 2013 18:25:02 +0000 (18:25 +0000)
committerDouglas Gregor <dgregor@apple.com>
Mon, 8 Apr 2013 18:25:02 +0000 (18:25 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@179035 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaStmt.cpp
test/SemaObjCXX/foreach.mm

index ff1db821b658d45df34176d5d7e8404737301582..dea01d5be21eaaa91e2464a7ff54f970e33d57aa 100644 (file)
@@ -1570,6 +1570,33 @@ Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc,
       if (!D->hasLocalStorage())
         return StmtError(Diag(D->getLocation(),
                               diag::err_non_variable_decl_in_for));
+
+      // If the type contained 'auto', deduce the 'auto' to 'id'.
+      if (FirstType->getContainedAutoType()) {
+        TypeSourceInfo *DeducedType = 0;
+        OpaqueValueExpr OpaqueId(D->getLocation(), Context.getObjCIdType(),
+                                 VK_RValue);
+        Expr *DeducedInit = &OpaqueId;
+        if (DeduceAutoType(D->getTypeSourceInfo(), DeducedInit, DeducedType)
+              == DAR_Failed) {
+          DiagnoseAutoDeductionFailure(D, DeducedInit);
+        }
+        if (!DeducedType) {
+          D->setInvalidDecl();
+          return StmtError();
+        }
+
+        D->setTypeSourceInfo(DeducedType);
+        D->setType(DeducedType->getType());
+        FirstType = DeducedType->getType();
+
+        if (ActiveTemplateInstantiations.empty()) {
+          SourceLocation Loc = DeducedType->getTypeLoc().getBeginLoc();
+          Diag(Loc, diag::warn_auto_var_is_id)
+            << D->getDeclName();
+        }
+      }
+
     } else {
       Expr *FirstE = cast<Expr>(First);
       if (!FirstE->isTypeDependent() && !FirstE->isLValue())
index 3c4b908eab94929874902db169aa9d6f048a8c95..ec6ed0550e228c100e62a0c7db6eab0d461bb514 100644 (file)
@@ -12,6 +12,8 @@ void f(NSArray *a) {
                  // expected-warning {{expression result unused}}
   
   for (id thisKey : keys);
+
+  for (auto thisKey : keys) { } // expected-warning{{'auto' deduced as 'id' in declaration of 'thisKey'}}
 }
 
 /* // rdar://9072298 */