[cfe-commits] r158239 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaDecl.cpp test/SemaObjCXX/arc-0x.mm

Jordan Rose jordan_rose at apple.com
Fri Jun 8 15:46:07 PDT 2012


Author: jrose
Date: Fri Jun  8 17:46:07 2012
New Revision: 158239

URL: http://llvm.org/viewvc/llvm-project?rev=158239&view=rev
Log:
Warn in ObjC++ when an 'auto' variable deduces type 'id'.

This could happen for cases like this:

- (NSArray *)getAllNames:(NSArray *)images {
  NSMutableArray *results = [NSMutableArray array];
  for (auto img in images) {
    [results addObject:img.name];
  }
  return results;
}

Here the property access will fail because 'img' has type 'id', rather than,
say, NSImage.

This warning will not fire in templated code, since the 'id' could have
come from a template parameter.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/SemaObjCXX/arc-0x.mm

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=158239&r1=158238&r2=158239&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri Jun  8 17:46:07 2012
@@ -1469,6 +1469,10 @@
 // Objective-C++
 def err_objc_decls_may_only_appear_in_global_scope : Error<
   "Objective-C declarations may only appear in global scope">;
+def warn_auto_var_is_id : Warning<
+  "'auto' deduced as 'id' in declaration of %0">,
+  InGroup<DiagGroup<"auto-var-id">>;
+
 // Attributes
 def err_nsobject_attribute : Error<
   "__attribute ((NSObject)) is for pointer types only">;

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=158239&r1=158238&r2=158239&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Fri Jun  8 17:46:07 2012
@@ -6303,6 +6303,17 @@
     if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(VDecl))
       VDecl->setInvalidDecl();
 
+    // Warn if we deduced 'id'. 'auto' usually implies type-safety, but using
+    // 'id' instead of a specific object type prevents most of our usual checks.
+    // We only want to warn outside of template instantiations, though:
+    // inside a template, the 'id' could have come from a parameter.
+    if (ActiveTemplateInstantiations.empty() &&
+        DeducedType->getType()->isObjCIdType()) {
+      SourceLocation Loc = DeducedType->getTypeLoc().getBeginLoc();
+      Diag(Loc, diag::warn_auto_var_is_id)
+        << VDecl->getDeclName() << DeduceInit->getSourceRange();
+    }
+
     // If this is a redeclaration, check that the type we just deduced matches
     // the previously declared type.
     if (VarDecl *Old = VDecl->getPreviousDecl())

Modified: cfe/trunk/test/SemaObjCXX/arc-0x.mm
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjCXX/arc-0x.mm?rev=158239&r1=158238&r2=158239&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjCXX/arc-0x.mm (original)
+++ cfe/trunk/test/SemaObjCXX/arc-0x.mm Fri Jun  8 17:46:07 2012
@@ -22,7 +22,7 @@
   __strong id *idp = new auto(obj);
 
   __strong id array[17];
-  for (auto x : array) {
+  for (auto x : array) { // expected-warning{{'auto' deduced as 'id' in declaration of 'x'}}
     __strong id *xPtr = &x;
   }
 
@@ -51,3 +51,24 @@
   (void) ^{ (void) p; };
   (void) ^{ (void) v; }; // expected-error {{cannot capture __autoreleasing variable in a block}}
 }
+
+
+// <rdar://problem/11319689>
+// warn when initializing an 'auto' variable with an 'id' initializer expression
+
+void testAutoId(id obj) {
+  auto x = obj; // expected-warning{{'auto' deduced as 'id' in declaration of 'x'}}
+}
+
+// ...but don't warn if it's coming from a template parameter.
+template<typename T>
+void autoTemplateFunction(T param, id obj) {
+  auto x = param; // no-warning
+  auto y = obj; // expected-warning{{'auto' deduced as 'id' in declaration of 'y'}}
+}
+
+void testAutoIdTemplate(id obj) {
+  autoTemplateFunction(obj, obj); // no-warning
+}
+
+





More information about the cfe-commits mailing list