From 4acb0a7d13ebc582f377efcb4c2dc020b6135fc1 Mon Sep 17 00:00:00 2001 From: Devin Coughlin Date: Fri, 5 Feb 2016 04:22:15 +0000 Subject: [PATCH] [analyzer] Suppress localization diagnostics in debug classes and methods. If the class or method name case-insensitively contains the term "debug", suppress warnings about string constants flowing to user-facing UI APIs. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@259875 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Checkers/LocalizationChecker.cpp | 35 +++++++++++++++++++ test/Analysis/localization.m | 19 ++++++++++ 2 files changed, 54 insertions(+) diff --git a/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp b/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp index 56346cd4f7..504b8b3018 100644 --- a/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp +++ b/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp @@ -619,11 +619,46 @@ void NonLocalizedStringChecker::setNonLocalizedState(const SVal S, } } + +static bool isDebuggingName(std::string name) { + return StringRef(name).lower().find("debug") != StringRef::npos; +} + +/// Returns true when, heuristically, the analyzer may be analyzing debugging +/// code. We use this to suppress localization diagnostics in un-localized user +/// interfaces that are only used for debugging and are therefore not user +/// facing. +static bool isDebuggingContext(CheckerContext &C) { + const Decl *D = C.getCurrentAnalysisDeclContext()->getDecl(); + if (!D) + return false; + + if (auto *ND = dyn_cast(D)) { + if (isDebuggingName(ND->getNameAsString())) + return true; + } + + const DeclContext *DC = D->getDeclContext(); + + if (auto *CD = dyn_cast(DC)) { + if (isDebuggingName(CD->getNameAsString())) + return true; + } + + return false; +} + + /// Reports a localization error for the passed in method call and SVal void NonLocalizedStringChecker::reportLocalizationError( SVal S, const ObjCMethodCall &M, CheckerContext &C, int argumentNumber) const { + // Don't warn about localization errors in classes and methods that + // may be debug code. + if (isDebuggingContext(C)) + return; + ExplodedNode *ErrNode = C.getPredecessor(); static CheckerProgramPointTag Tag("NonLocalizedStringChecker", "UnlocalizedString"); diff --git a/test/Analysis/localization.m b/test/Analysis/localization.m index ce55609b1b..cf0697c76f 100644 --- a/test/Analysis/localization.m +++ b/test/Analysis/localization.m @@ -90,6 +90,13 @@ NSString *KHLocalizedString(NSString* key, NSString* comment) { [testLabel setText:bar]; // no-warning } + +// Suppress diagnostic about user-facing string constants when the method name +// contains the term "Debug". +- (void)debugScreen:(UILabel *)label { + label.text = @"Unlocalized"; +} + // Plural Misuse Checker Tests // These tests are modeled off incorrect uses of the many-one pattern // from real projects. @@ -205,3 +212,15 @@ NSString *KHLocalizedString(NSString* key, NSString* comment) { // } @end + + +// Suppress diagnostic about user-facing string constants when the class name +// contains "Debug" +@interface MyDebugView : NSObject +@end + +@implementation MyDebugView +- (void)setupScreen:(UILabel *)label { + label.text = @"Unlocalized"; +} +@end -- 2.40.0