diff options
author | Ted Kremenek <kremenek@apple.com> | 2009-11-20 04:31:57 +0000 |
---|---|---|
committer | Ted Kremenek <kremenek@apple.com> | 2009-11-20 04:31:57 +0000 |
commit | c1f161c012a2371cea2390a681180e405cd9f6a1 (patch) | |
tree | 06e2b7d475d91151a446e19ee1ef0330b573728f /clang/lib | |
parent | 1aec3c04273534f5848e05b41ef614a28baa83cb (diff) | |
download | bcm5719-llvm-c1f161c012a2371cea2390a681180e405cd9f6a1.tar.gz bcm5719-llvm-c1f161c012a2371cea2390a681180e405cd9f6a1.zip |
Unused ivar checker: ivars referenced by lexically nested functions should not be flagged as unused. Fixes <rdar://problem/7254495>.
llvm-svn: 89448
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/Analysis/CheckObjCUnusedIVars.cpp | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/clang/lib/Analysis/CheckObjCUnusedIVars.cpp b/clang/lib/Analysis/CheckObjCUnusedIVars.cpp index 2d9b53163f6..3ee5cb12d95 100644 --- a/clang/lib/Analysis/CheckObjCUnusedIVars.cpp +++ b/clang/lib/Analysis/CheckObjCUnusedIVars.cpp @@ -85,6 +85,17 @@ static void Scan(IvarUsageMap& M, const ObjCContainerDecl* D) { } } +static void Scan(IvarUsageMap &M, const DeclContext *C, const FileID FID, + SourceManager &SM) { + for (DeclContext::decl_iterator I=C->decls_begin(), E=C->decls_end(); + I!=E; ++I) + if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { + SourceLocation L = FD->getLocStart(); + if (SM.getFileID(L) == FID) + Scan(M, FD->getBody()); + } +} + void clang::CheckObjCUnusedIvar(const ObjCImplementationDecl *D, BugReporter &BR) { @@ -110,10 +121,30 @@ void clang::CheckObjCUnusedIvar(const ObjCImplementationDecl *D, if (M.empty()) return; - + // Now scan the implementation declaration. Scan(M, D); + + // Any potentially unused ivars? + bool hasUnused = false; + for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I) + if (I->second == Unused) { + hasUnused = true; + break; + } + + if (!hasUnused) + return; + + // We found some potentially unused ivars. Scan the entire translation unit + // for functions inside the @implementation that reference these ivars. + // FIXME: In the future hopefully we can just use the lexical DeclContext + // to go from the ObjCImplementationDecl to the lexically "nested" + // C functions. + SourceManager &SM = BR.getSourceManager(); + Scan(M, D->getDeclContext(), SM.getFileID(D->getLocation()), SM); + // Find ivars that are unused. for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I) if (I->second == Unused) { |