r259875 - [analyzer] Suppress localization diagnostics in debug classes and methods.

Devin Coughlin via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 4 20:22:16 PST 2016


Author: dcoughlin
Date: Thu Feb  4 22:22:15 2016
New Revision: 259875

URL: http://llvm.org/viewvc/llvm-project?rev=259875&view=rev
Log:
[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.

Modified:
    cfe/trunk/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp
    cfe/trunk/test/Analysis/localization.m

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp?rev=259875&r1=259874&r2=259875&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp Thu Feb  4 22:22:15 2016
@@ -619,11 +619,46 @@ void NonLocalizedStringChecker::setNonLo
   }
 }
 
+
+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<NamedDecl>(D)) {
+    if (isDebuggingName(ND->getNameAsString()))
+      return true;
+  }
+
+  const DeclContext *DC = D->getDeclContext();
+
+  if (auto *CD = dyn_cast<ObjCContainerDecl>(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");

Modified: cfe/trunk/test/Analysis/localization.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/localization.m?rev=259875&r1=259874&r2=259875&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/localization.m (original)
+++ cfe/trunk/test/Analysis/localization.m Thu Feb  4 22:22:15 2016
@@ -90,6 +90,13 @@ NSString *KHLocalizedString(NSString* ke
   [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* ke
 // }
 
 @end
+
+
+// Suppress diagnostic about user-facing string constants when the class name
+// contains "Debug"
+ at interface MyDebugView : NSObject
+ at end
+
+ at implementation MyDebugView
+- (void)setupScreen:(UILabel *)label {
+  label.text = @"Unlocalized";
+}
+ at end




More information about the cfe-commits mailing list