diff options
| author | Jordan Rose <jordan_rose@apple.com> | 2012-06-08 22:46:07 +0000 |
|---|---|---|
| committer | Jordan Rose <jordan_rose@apple.com> | 2012-06-08 22:46:07 +0000 |
| commit | d8d56694352c972bfb1e13bdc562b3d27b29f4ea (patch) | |
| tree | ab85fc5b75273c7fde46690a4c5e61702926b540 /clang/lib | |
| parent | 9b4cbec703187c0bcff2d456d8979d7b1983499b (diff) | |
| download | bcm5719-llvm-d8d56694352c972bfb1e13bdc562b3d27b29f4ea.tar.gz bcm5719-llvm-d8d56694352c972bfb1e13bdc562b3d27b29f4ea.zip | |
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.
llvm-svn: 158239
Diffstat (limited to 'clang/lib')
| -rw-r--r-- | clang/lib/Sema/SemaDecl.cpp | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 68f74694577..40cc35117c8 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -6303,6 +6303,17 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, 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()) |

