r179035 - <rdar://problem/13540921> Cope with deduced 'auto' in a C++11 for-range loop that is actually an Objective-C fast enumeration loop.

Douglas Gregor dgregor at apple.com
Mon Apr 8 11:25:02 PDT 2013


Author: dgregor
Date: Mon Apr  8 13:25:02 2013
New Revision: 179035

URL: http://llvm.org/viewvc/llvm-project?rev=179035&view=rev
Log:
<rdar://problem/13540921> Cope with deduced 'auto' in a C++11 for-range loop that is actually an Objective-C fast enumeration loop.

Modified:
    cfe/trunk/lib/Sema/SemaStmt.cpp
    cfe/trunk/test/SemaObjCXX/foreach.mm

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=179035&r1=179034&r2=179035&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Mon Apr  8 13:25:02 2013
@@ -1570,6 +1570,33 @@ Sema::ActOnObjCForCollectionStmt(SourceL
       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())

Modified: cfe/trunk/test/SemaObjCXX/foreach.mm
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjCXX/foreach.mm?rev=179035&r1=179034&r2=179035&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjCXX/foreach.mm (original)
+++ cfe/trunk/test/SemaObjCXX/foreach.mm Mon Apr  8 13:25:02 2013
@@ -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 */





More information about the cfe-commits mailing list