r248350 - [analyzer] Improve localizability checks for iOS / OS X.

Devin Coughlin via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 22 16:58:05 PDT 2015


Author: dcoughlin
Date: Tue Sep 22 18:58:04 2015
New Revision: 248350

URL: http://llvm.org/viewvc/llvm-project?rev=248350&view=rev
Log:
[analyzer] Improve localizability checks for iOS / OS X.

Various improvements to the localization checker:
* Adjusted copy to be consistent with diagnostic text in other Apple
  API checkers.
* Added in ~150 UIKit / AppKit methods that require localized strings in
  UnlocalizedStringsChecker.
* UnlocalizedStringChecker now checks for UI methods up the class hierarchy and
  UI methods that conform for a certain Objective-C protocol.
* Added in alpha version of PluralMisuseChecker and some regression tests. False
  positives are still not ideal.

A patch by Kulpreet Chilana!

Differential Revision: http://reviews.llvm.org/D12417

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

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/Checkers.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/Checkers.td?rev=248350&r1=248349&r2=248350&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/Checkers.td (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/Checkers.td Tue Sep 22 18:58:04 2015
@@ -493,6 +493,10 @@ def EmptyLocalizationContextChecker : Ch
   HelpText<"Check that NSLocalizedString macros include a comment for context">,
   DescFile<"LocalizationChecker.cpp">;
 
+def PluralMisuseChecker : Checker<"PluralMisuseChecker">,
+  HelpText<"Warns against using one vs. many plural pattern in code when generating localized strings.">,
+  DescFile<"LocalizationChecker.cpp">;
+
 } // end "alpha.osx.cocoa"
 
 let ParentPackage = CoreFoundation in {

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp?rev=248350&r1=248349&r2=248350&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp Tue Sep 22 18:58:04 2015
@@ -16,7 +16,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "ClangSACheckers.h"
-#include "SelectorExtras.h"
 #include "clang/AST/Attr.h"
 #include "clang/AST/Decl.h"
 #include "clang/AST/DeclObjC.h"
@@ -66,11 +65,12 @@ class NonLocalizedStringChecker
   mutable std::unique_ptr<BugType> BT;
 
   // Methods that require a localized string
-  mutable llvm::StringMap<llvm::StringMap<uint8_t>> UIMethods;
+  mutable llvm::DenseMap<const IdentifierInfo *,
+                         llvm::DenseMap<Selector, uint8_t>> UIMethods;
   // Methods that return a localized string
-  mutable llvm::SmallSet<std::pair<StringRef, StringRef>, 12> LSM;
+  mutable llvm::SmallSet<std::pair<const IdentifierInfo *, Selector>, 12> LSM;
   // C Functions that return a localized string
-  mutable llvm::StringSet<> LSF;
+  mutable llvm::SmallSet<const IdentifierInfo *, 5> LSF;
 
   void initUIMethods(ASTContext &Ctx) const;
   void initLocStringsMethods(ASTContext &Ctx) const;
@@ -84,6 +84,9 @@ class NonLocalizedStringChecker
   void reportLocalizationError(SVal S, const ObjCMethodCall &M,
                                CheckerContext &C, int argumentNumber = 0) const;
 
+  int getLocalizedArgumentForSelector(const IdentifierInfo *Receiver,
+                                      Selector S) const;
+
 public:
   NonLocalizedStringChecker();
 
@@ -104,87 +107,464 @@ REGISTER_MAP_WITH_PROGRAMSTATE(Localized
                                LocalizedState)
 
 NonLocalizedStringChecker::NonLocalizedStringChecker() {
-  BT.reset(new BugType(this, "Unlocalized string", "Localizability Error"));
+  BT.reset(new BugType(this, "Unlocalizable string",
+                       "Localizability Issue (Apple)"));
 }
 
+#define NEW_RECEIVER(receiver)                                                 \
+  llvm::DenseMap<Selector, uint8_t> &receiver##M =                             \
+      UIMethods.insert({&Ctx.Idents.get(#receiver),                            \
+                        llvm::DenseMap<Selector, uint8_t>()})                  \
+          .first->second;
+#define ADD_NULLARY_METHOD(receiver, method, argument)                         \
+  receiver##M.insert(                                                          \
+      {Ctx.Selectors.getNullarySelector(&Ctx.Idents.get(#method)), argument});
+#define ADD_UNARY_METHOD(receiver, method, argument)                           \
+  receiver##M.insert(                                                          \
+      {Ctx.Selectors.getUnarySelector(&Ctx.Idents.get(#method)), argument});
+#define ADD_METHOD(receiver, method_list, count, argument)                     \
+  receiver##M.insert({Ctx.Selectors.getSelector(count, method_list), argument});
+
 /// Initializes a list of methods that require a localized string
 /// Format: {"ClassName", {{"selectorName:", LocStringArg#}, ...}, ...}
 void NonLocalizedStringChecker::initUIMethods(ASTContext &Ctx) const {
   if (!UIMethods.empty())
     return;
 
-  // TODO: This should eventually be a comprehensive list of UIKit methods
-
-  // UILabel Methods
-  llvm::StringMap<uint8_t> &UILabelM =
-      UIMethods.insert({"UILabel", llvm::StringMap<uint8_t>()}).first->second;
-  UILabelM.insert({"setText:", 0});
-
-  // UIButton Methods
-  llvm::StringMap<uint8_t> &UIButtonM =
-      UIMethods.insert({"UIButton", llvm::StringMap<uint8_t>()}).first->second;
-  UIButtonM.insert({"setText:", 0});
-
-  // UIAlertAction Methods
-  llvm::StringMap<uint8_t> &UIAlertActionM =
-      UIMethods.insert({"UIAlertAction", llvm::StringMap<uint8_t>()})
-          .first->second;
-  UIAlertActionM.insert({"actionWithTitle:style:handler:", 0});
-
-  // UIAlertController Methods
-  llvm::StringMap<uint8_t> &UIAlertControllerM =
-      UIMethods.insert({"UIAlertController", llvm::StringMap<uint8_t>()})
-          .first->second;
-  UIAlertControllerM.insert(
-      {"alertControllerWithTitle:message:preferredStyle:", 1});
-
-  // NSButton Methods
-  llvm::StringMap<uint8_t> &NSButtonM =
-      UIMethods.insert({"NSButton", llvm::StringMap<uint8_t>()}).first->second;
-  NSButtonM.insert({"setTitle:", 0});
-
-  // NSButtonCell Methods
-  llvm::StringMap<uint8_t> &NSButtonCellM =
-      UIMethods.insert({"NSButtonCell", llvm::StringMap<uint8_t>()})
-          .first->second;
-  NSButtonCellM.insert({"setTitle:", 0});
-
-  // NSMenuItem Methods
-  llvm::StringMap<uint8_t> &NSMenuItemM =
-      UIMethods.insert({"NSMenuItem", llvm::StringMap<uint8_t>()})
-          .first->second;
-  NSMenuItemM.insert({"setTitle:", 0});
-
-  // NSAttributedString Methods
-  llvm::StringMap<uint8_t> &NSAttributedStringM =
-      UIMethods.insert({"NSAttributedString", llvm::StringMap<uint8_t>()})
-          .first->second;
-  NSAttributedStringM.insert({"initWithString:", 0});
-  NSAttributedStringM.insert({"initWithString:attributes:", 0});
+  // UI Methods
+  NEW_RECEIVER(UISearchDisplayController)
+  ADD_UNARY_METHOD(UISearchDisplayController, setSearchResultsTitle, 0)
+
+  NEW_RECEIVER(UITabBarItem)
+  IdentifierInfo *initWithTitleUITabBarItemTag[] = {
+      &Ctx.Idents.get("initWithTitle"), &Ctx.Idents.get("image"),
+      &Ctx.Idents.get("tag")};
+  ADD_METHOD(UITabBarItem, initWithTitleUITabBarItemTag, 3, 0)
+  IdentifierInfo *initWithTitleUITabBarItemImage[] = {
+      &Ctx.Idents.get("initWithTitle"), &Ctx.Idents.get("image"),
+      &Ctx.Idents.get("selectedImage")};
+  ADD_METHOD(UITabBarItem, initWithTitleUITabBarItemImage, 3, 0)
+
+  NEW_RECEIVER(NSDockTile)
+  ADD_UNARY_METHOD(NSDockTile, setBadgeLabel, 0)
+
+  NEW_RECEIVER(NSStatusItem)
+  ADD_UNARY_METHOD(NSStatusItem, setTitle, 0)
+  ADD_UNARY_METHOD(NSStatusItem, setToolTip, 0)
+
+  NEW_RECEIVER(UITableViewRowAction)
+  IdentifierInfo *rowActionWithStyleUITableViewRowAction[] = {
+      &Ctx.Idents.get("rowActionWithStyle"), &Ctx.Idents.get("title"),
+      &Ctx.Idents.get("handler")};
+  ADD_METHOD(UITableViewRowAction, rowActionWithStyleUITableViewRowAction, 3, 1)
+  ADD_UNARY_METHOD(UITableViewRowAction, setTitle, 0)
+
+  NEW_RECEIVER(NSBox)
+  ADD_UNARY_METHOD(NSBox, setTitle, 0)
+
+  NEW_RECEIVER(NSButton)
+  ADD_UNARY_METHOD(NSButton, setTitle, 0)
+  ADD_UNARY_METHOD(NSButton, setAlternateTitle, 0)
+
+  NEW_RECEIVER(NSSavePanel)
+  ADD_UNARY_METHOD(NSSavePanel, setPrompt, 0)
+  ADD_UNARY_METHOD(NSSavePanel, setTitle, 0)
+  ADD_UNARY_METHOD(NSSavePanel, setNameFieldLabel, 0)
+  ADD_UNARY_METHOD(NSSavePanel, setNameFieldStringValue, 0)
+  ADD_UNARY_METHOD(NSSavePanel, setMessage, 0)
+
+  NEW_RECEIVER(UIPrintInfo)
+  ADD_UNARY_METHOD(UIPrintInfo, setJobName, 0)
+
+  NEW_RECEIVER(NSTabViewItem)
+  ADD_UNARY_METHOD(NSTabViewItem, setLabel, 0)
+  ADD_UNARY_METHOD(NSTabViewItem, setToolTip, 0)
+
+  NEW_RECEIVER(NSBrowser)
+  IdentifierInfo *setTitleNSBrowser[] = {&Ctx.Idents.get("setTitle"),
+                                         &Ctx.Idents.get("ofColumn")};
+  ADD_METHOD(NSBrowser, setTitleNSBrowser, 2, 0)
+
+  NEW_RECEIVER(UIAccessibilityElement)
+  ADD_UNARY_METHOD(UIAccessibilityElement, setAccessibilityLabel, 0)
+  ADD_UNARY_METHOD(UIAccessibilityElement, setAccessibilityHint, 0)
+  ADD_UNARY_METHOD(UIAccessibilityElement, setAccessibilityValue, 0)
+
+  NEW_RECEIVER(UIAlertAction)
+  IdentifierInfo *actionWithTitleUIAlertAction[] = {
+      &Ctx.Idents.get("actionWithTitle"), &Ctx.Idents.get("style"),
+      &Ctx.Idents.get("handler")};
+  ADD_METHOD(UIAlertAction, actionWithTitleUIAlertAction, 3, 0)
+
+  NEW_RECEIVER(NSPopUpButton)
+  ADD_UNARY_METHOD(NSPopUpButton, addItemWithTitle, 0)
+  IdentifierInfo *insertItemWithTitleNSPopUpButton[] = {
+      &Ctx.Idents.get("insertItemWithTitle"), &Ctx.Idents.get("atIndex")};
+  ADD_METHOD(NSPopUpButton, insertItemWithTitleNSPopUpButton, 2, 0)
+  ADD_UNARY_METHOD(NSPopUpButton, removeItemWithTitle, 0)
+  ADD_UNARY_METHOD(NSPopUpButton, selectItemWithTitle, 0)
+  ADD_UNARY_METHOD(NSPopUpButton, setTitle, 0)
+
+  NEW_RECEIVER(NSTableViewRowAction)
+  IdentifierInfo *rowActionWithStyleNSTableViewRowAction[] = {
+      &Ctx.Idents.get("rowActionWithStyle"), &Ctx.Idents.get("title"),
+      &Ctx.Idents.get("handler")};
+  ADD_METHOD(NSTableViewRowAction, rowActionWithStyleNSTableViewRowAction, 3, 1)
+  ADD_UNARY_METHOD(NSTableViewRowAction, setTitle, 0)
+
+  NEW_RECEIVER(NSImage)
+  ADD_UNARY_METHOD(NSImage, setAccessibilityDescription, 0)
+
+  NEW_RECEIVER(NSUserActivity)
+  ADD_UNARY_METHOD(NSUserActivity, setTitle, 0)
+
+  NEW_RECEIVER(NSPathControlItem)
+  ADD_UNARY_METHOD(NSPathControlItem, setTitle, 0)
+
+  NEW_RECEIVER(NSCell)
+  ADD_UNARY_METHOD(NSCell, initTextCell, 0)
+  ADD_UNARY_METHOD(NSCell, setTitle, 0)
+  ADD_UNARY_METHOD(NSCell, setStringValue, 0)
+
+  NEW_RECEIVER(NSPathControl)
+  ADD_UNARY_METHOD(NSPathControl, setPlaceholderString, 0)
+
+  NEW_RECEIVER(UIAccessibility)
+  ADD_UNARY_METHOD(UIAccessibility, setAccessibilityLabel, 0)
+  ADD_UNARY_METHOD(UIAccessibility, setAccessibilityHint, 0)
+  ADD_UNARY_METHOD(UIAccessibility, setAccessibilityValue, 0)
+
+  NEW_RECEIVER(NSTableColumn)
+  ADD_UNARY_METHOD(NSTableColumn, setTitle, 0)
+  ADD_UNARY_METHOD(NSTableColumn, setHeaderToolTip, 0)
+
+  NEW_RECEIVER(NSSegmentedControl)
+  IdentifierInfo *setLabelNSSegmentedControl[] = {
+      &Ctx.Idents.get("setLabel"), &Ctx.Idents.get("forSegment")};
+  ADD_METHOD(NSSegmentedControl, setLabelNSSegmentedControl, 2, 0)
+
+  NEW_RECEIVER(NSButtonCell)
+  ADD_UNARY_METHOD(NSButtonCell, setTitle, 0)
+  ADD_UNARY_METHOD(NSButtonCell, setAlternateTitle, 0)
+
+  NEW_RECEIVER(NSSliderCell)
+  ADD_UNARY_METHOD(NSSliderCell, setTitle, 0)
+
+  NEW_RECEIVER(NSControl)
+  ADD_UNARY_METHOD(NSControl, setStringValue, 0)
+
+  NEW_RECEIVER(NSAccessibility)
+  ADD_UNARY_METHOD(NSAccessibility, setAccessibilityValueDescription, 0)
+  ADD_UNARY_METHOD(NSAccessibility, setAccessibilityLabel, 0)
+  ADD_UNARY_METHOD(NSAccessibility, setAccessibilityTitle, 0)
+  ADD_UNARY_METHOD(NSAccessibility, setAccessibilityPlaceholderValue, 0)
+  ADD_UNARY_METHOD(NSAccessibility, setAccessibilityHelp, 0)
+
+  NEW_RECEIVER(NSMatrix)
+  IdentifierInfo *setToolTipNSMatrix[] = {&Ctx.Idents.get("setToolTip"),
+                                          &Ctx.Idents.get("forCell")};
+  ADD_METHOD(NSMatrix, setToolTipNSMatrix, 2, 0)
+
+  NEW_RECEIVER(NSPrintPanel)
+  ADD_UNARY_METHOD(NSPrintPanel, setDefaultButtonTitle, 0)
+
+  NEW_RECEIVER(UILocalNotification)
+  ADD_UNARY_METHOD(UILocalNotification, setAlertBody, 0)
+  ADD_UNARY_METHOD(UILocalNotification, setAlertAction, 0)
+  ADD_UNARY_METHOD(UILocalNotification, setAlertTitle, 0)
+
+  NEW_RECEIVER(NSSlider)
+  ADD_UNARY_METHOD(NSSlider, setTitle, 0)
+
+  NEW_RECEIVER(UIMenuItem)
+  IdentifierInfo *initWithTitleUIMenuItem[] = {&Ctx.Idents.get("initWithTitle"),
+                                               &Ctx.Idents.get("action")};
+  ADD_METHOD(UIMenuItem, initWithTitleUIMenuItem, 2, 0)
+  ADD_UNARY_METHOD(UIMenuItem, setTitle, 0)
+
+  NEW_RECEIVER(UIAlertController)
+  IdentifierInfo *alertControllerWithTitleUIAlertController[] = {
+      &Ctx.Idents.get("alertControllerWithTitle"), &Ctx.Idents.get("message"),
+      &Ctx.Idents.get("preferredStyle")};
+  ADD_METHOD(UIAlertController, alertControllerWithTitleUIAlertController, 3, 1)
+  ADD_UNARY_METHOD(UIAlertController, setTitle, 0)
+  ADD_UNARY_METHOD(UIAlertController, setMessage, 0)
+
+  NEW_RECEIVER(UIApplicationShortcutItem)
+  IdentifierInfo *initWithTypeUIApplicationShortcutItemIcon[] = {
+      &Ctx.Idents.get("initWithType"), &Ctx.Idents.get("localizedTitle"),
+      &Ctx.Idents.get("localizedSubtitle"), &Ctx.Idents.get("icon"),
+      &Ctx.Idents.get("userInfo")};
+  ADD_METHOD(UIApplicationShortcutItem,
+             initWithTypeUIApplicationShortcutItemIcon, 5, 1)
+  IdentifierInfo *initWithTypeUIApplicationShortcutItem[] = {
+      &Ctx.Idents.get("initWithType"), &Ctx.Idents.get("localizedTitle")};
+  ADD_METHOD(UIApplicationShortcutItem, initWithTypeUIApplicationShortcutItem,
+             2, 1)
+
+  NEW_RECEIVER(UIActionSheet)
+  IdentifierInfo *initWithTitleUIActionSheet[] = {
+      &Ctx.Idents.get("initWithTitle"), &Ctx.Idents.get("delegate"),
+      &Ctx.Idents.get("cancelButtonTitle"),
+      &Ctx.Idents.get("destructiveButtonTitle"),
+      &Ctx.Idents.get("otherButtonTitles")};
+  ADD_METHOD(UIActionSheet, initWithTitleUIActionSheet, 5, 0)
+  ADD_UNARY_METHOD(UIActionSheet, addButtonWithTitle, 0)
+  ADD_UNARY_METHOD(UIActionSheet, setTitle, 0)
+
+  NEW_RECEIVER(NSURLSessionTask)
+  ADD_UNARY_METHOD(NSURLSessionTask, setTaskDescription, 0)
+
+  NEW_RECEIVER(UIAccessibilityCustomAction)
+  IdentifierInfo *initWithNameUIAccessibilityCustomAction[] = {
+      &Ctx.Idents.get("initWithName"), &Ctx.Idents.get("target"),
+      &Ctx.Idents.get("selector")};
+  ADD_METHOD(UIAccessibilityCustomAction,
+             initWithNameUIAccessibilityCustomAction, 3, 0)
+  ADD_UNARY_METHOD(UIAccessibilityCustomAction, setName, 0)
+
+  NEW_RECEIVER(UISearchBar)
+  ADD_UNARY_METHOD(UISearchBar, setText, 0)
+  ADD_UNARY_METHOD(UISearchBar, setPrompt, 0)
+  ADD_UNARY_METHOD(UISearchBar, setPlaceholder, 0)
+
+  NEW_RECEIVER(UIBarItem)
+  ADD_UNARY_METHOD(UIBarItem, setTitle, 0)
+
+  NEW_RECEIVER(UITextView)
+  ADD_UNARY_METHOD(UITextView, setText, 0)
+
+  NEW_RECEIVER(NSView)
+  ADD_UNARY_METHOD(NSView, setToolTip, 0)
+
+  NEW_RECEIVER(NSTextField)
+  ADD_UNARY_METHOD(NSTextField, setPlaceholderString, 0)
+
+  NEW_RECEIVER(NSAttributedString)
+  ADD_UNARY_METHOD(NSAttributedString, initWithString, 0)
+  IdentifierInfo *initWithStringNSAttributedString[] = {
+      &Ctx.Idents.get("initWithString"), &Ctx.Idents.get("attributes")};
+  ADD_METHOD(NSAttributedString, initWithStringNSAttributedString, 2, 0)
+
+  NEW_RECEIVER(NSText)
+  ADD_UNARY_METHOD(NSText, setString, 0)
+
+  NEW_RECEIVER(UIKeyCommand)
+  IdentifierInfo *keyCommandWithInputUIKeyCommand[] = {
+      &Ctx.Idents.get("keyCommandWithInput"), &Ctx.Idents.get("modifierFlags"),
+      &Ctx.Idents.get("action"), &Ctx.Idents.get("discoverabilityTitle")};
+  ADD_METHOD(UIKeyCommand, keyCommandWithInputUIKeyCommand, 4, 3)
+  ADD_UNARY_METHOD(UIKeyCommand, setDiscoverabilityTitle, 0)
+
+  NEW_RECEIVER(UILabel)
+  ADD_UNARY_METHOD(UILabel, setText, 0)
+
+  NEW_RECEIVER(NSAlert)
+  IdentifierInfo *alertWithMessageTextNSAlert[] = {
+      &Ctx.Idents.get("alertWithMessageText"), &Ctx.Idents.get("defaultButton"),
+      &Ctx.Idents.get("alternateButton"), &Ctx.Idents.get("otherButton"),
+      &Ctx.Idents.get("informativeTextWithFormat")};
+  ADD_METHOD(NSAlert, alertWithMessageTextNSAlert, 5, 0)
+  ADD_UNARY_METHOD(NSAlert, addButtonWithTitle, 0)
+  ADD_UNARY_METHOD(NSAlert, setMessageText, 0)
+  ADD_UNARY_METHOD(NSAlert, setInformativeText, 0)
+  ADD_UNARY_METHOD(NSAlert, setHelpAnchor, 0)
+
+  NEW_RECEIVER(UIMutableApplicationShortcutItem)
+  ADD_UNARY_METHOD(UIMutableApplicationShortcutItem, setLocalizedTitle, 0)
+  ADD_UNARY_METHOD(UIMutableApplicationShortcutItem, setLocalizedSubtitle, 0)
+
+  NEW_RECEIVER(UIButton)
+  IdentifierInfo *setTitleUIButton[] = {&Ctx.Idents.get("setTitle"),
+                                        &Ctx.Idents.get("forState")};
+  ADD_METHOD(UIButton, setTitleUIButton, 2, 0)
+
+  NEW_RECEIVER(NSWindow)
+  ADD_UNARY_METHOD(NSWindow, setTitle, 0)
+  IdentifierInfo *minFrameWidthWithTitleNSWindow[] = {
+      &Ctx.Idents.get("minFrameWidthWithTitle"), &Ctx.Idents.get("styleMask")};
+  ADD_METHOD(NSWindow, minFrameWidthWithTitleNSWindow, 2, 0)
+  ADD_UNARY_METHOD(NSWindow, setMiniwindowTitle, 0)
+
+  NEW_RECEIVER(NSPathCell)
+  ADD_UNARY_METHOD(NSPathCell, setPlaceholderString, 0)
+
+  NEW_RECEIVER(UIDocumentMenuViewController)
+  IdentifierInfo *addOptionWithTitleUIDocumentMenuViewController[] = {
+      &Ctx.Idents.get("addOptionWithTitle"), &Ctx.Idents.get("image"),
+      &Ctx.Idents.get("order"), &Ctx.Idents.get("handler")};
+  ADD_METHOD(UIDocumentMenuViewController,
+             addOptionWithTitleUIDocumentMenuViewController, 4, 0)
+
+  NEW_RECEIVER(UINavigationItem)
+  ADD_UNARY_METHOD(UINavigationItem, initWithTitle, 0)
+  ADD_UNARY_METHOD(UINavigationItem, setTitle, 0)
+  ADD_UNARY_METHOD(UINavigationItem, setPrompt, 0)
+
+  NEW_RECEIVER(UIAlertView)
+  IdentifierInfo *initWithTitleUIAlertView[] = {
+      &Ctx.Idents.get("initWithTitle"), &Ctx.Idents.get("message"),
+      &Ctx.Idents.get("delegate"), &Ctx.Idents.get("cancelButtonTitle"),
+      &Ctx.Idents.get("otherButtonTitles")};
+  ADD_METHOD(UIAlertView, initWithTitleUIAlertView, 5, 0)
+  ADD_UNARY_METHOD(UIAlertView, addButtonWithTitle, 0)
+  ADD_UNARY_METHOD(UIAlertView, setTitle, 0)
+  ADD_UNARY_METHOD(UIAlertView, setMessage, 0)
+
+  NEW_RECEIVER(NSFormCell)
+  ADD_UNARY_METHOD(NSFormCell, initTextCell, 0)
+  ADD_UNARY_METHOD(NSFormCell, setTitle, 0)
+  ADD_UNARY_METHOD(NSFormCell, setPlaceholderString, 0)
+
+  NEW_RECEIVER(NSUserNotification)
+  ADD_UNARY_METHOD(NSUserNotification, setTitle, 0)
+  ADD_UNARY_METHOD(NSUserNotification, setSubtitle, 0)
+  ADD_UNARY_METHOD(NSUserNotification, setInformativeText, 0)
+  ADD_UNARY_METHOD(NSUserNotification, setActionButtonTitle, 0)
+  ADD_UNARY_METHOD(NSUserNotification, setOtherButtonTitle, 0)
+  ADD_UNARY_METHOD(NSUserNotification, setResponsePlaceholder, 0)
+
+  NEW_RECEIVER(NSToolbarItem)
+  ADD_UNARY_METHOD(NSToolbarItem, setLabel, 0)
+  ADD_UNARY_METHOD(NSToolbarItem, setPaletteLabel, 0)
+  ADD_UNARY_METHOD(NSToolbarItem, setToolTip, 0)
+
+  NEW_RECEIVER(NSProgress)
+  ADD_UNARY_METHOD(NSProgress, setLocalizedDescription, 0)
+  ADD_UNARY_METHOD(NSProgress, setLocalizedAdditionalDescription, 0)
+
+  NEW_RECEIVER(NSSegmentedCell)
+  IdentifierInfo *setLabelNSSegmentedCell[] = {&Ctx.Idents.get("setLabel"),
+                                               &Ctx.Idents.get("forSegment")};
+  ADD_METHOD(NSSegmentedCell, setLabelNSSegmentedCell, 2, 0)
+  IdentifierInfo *setToolTipNSSegmentedCell[] = {&Ctx.Idents.get("setToolTip"),
+                                                 &Ctx.Idents.get("forSegment")};
+  ADD_METHOD(NSSegmentedCell, setToolTipNSSegmentedCell, 2, 0)
+
+  NEW_RECEIVER(NSUndoManager)
+  ADD_UNARY_METHOD(NSUndoManager, setActionName, 0)
+  ADD_UNARY_METHOD(NSUndoManager, undoMenuTitleForUndoActionName, 0)
+  ADD_UNARY_METHOD(NSUndoManager, redoMenuTitleForUndoActionName, 0)
+
+  NEW_RECEIVER(NSMenuItem)
+  IdentifierInfo *initWithTitleNSMenuItem[] = {
+      &Ctx.Idents.get("initWithTitle"), &Ctx.Idents.get("action"),
+      &Ctx.Idents.get("keyEquivalent")};
+  ADD_METHOD(NSMenuItem, initWithTitleNSMenuItem, 3, 0)
+  ADD_UNARY_METHOD(NSMenuItem, setTitle, 0)
+  ADD_UNARY_METHOD(NSMenuItem, setToolTip, 0)
+
+  NEW_RECEIVER(NSPopUpButtonCell)
+  IdentifierInfo *initTextCellNSPopUpButtonCell[] = {
+      &Ctx.Idents.get("initTextCell"), &Ctx.Idents.get("pullsDown")};
+  ADD_METHOD(NSPopUpButtonCell, initTextCellNSPopUpButtonCell, 2, 0)
+  ADD_UNARY_METHOD(NSPopUpButtonCell, addItemWithTitle, 0)
+  IdentifierInfo *insertItemWithTitleNSPopUpButtonCell[] = {
+      &Ctx.Idents.get("insertItemWithTitle"), &Ctx.Idents.get("atIndex")};
+  ADD_METHOD(NSPopUpButtonCell, insertItemWithTitleNSPopUpButtonCell, 2, 0)
+  ADD_UNARY_METHOD(NSPopUpButtonCell, removeItemWithTitle, 0)
+  ADD_UNARY_METHOD(NSPopUpButtonCell, selectItemWithTitle, 0)
+  ADD_UNARY_METHOD(NSPopUpButtonCell, setTitle, 0)
+
+  NEW_RECEIVER(NSViewController)
+  ADD_UNARY_METHOD(NSViewController, setTitle, 0)
+
+  NEW_RECEIVER(NSMenu)
+  ADD_UNARY_METHOD(NSMenu, initWithTitle, 0)
+  IdentifierInfo *insertItemWithTitleNSMenu[] = {
+      &Ctx.Idents.get("insertItemWithTitle"), &Ctx.Idents.get("action"),
+      &Ctx.Idents.get("keyEquivalent"), &Ctx.Idents.get("atIndex")};
+  ADD_METHOD(NSMenu, insertItemWithTitleNSMenu, 4, 0)
+  IdentifierInfo *addItemWithTitleNSMenu[] = {
+      &Ctx.Idents.get("addItemWithTitle"), &Ctx.Idents.get("action"),
+      &Ctx.Idents.get("keyEquivalent")};
+  ADD_METHOD(NSMenu, addItemWithTitleNSMenu, 3, 0)
+  ADD_UNARY_METHOD(NSMenu, setTitle, 0)
+
+  NEW_RECEIVER(UIMutableUserNotificationAction)
+  ADD_UNARY_METHOD(UIMutableUserNotificationAction, setTitle, 0)
+
+  NEW_RECEIVER(NSForm)
+  ADD_UNARY_METHOD(NSForm, addEntry, 0)
+  IdentifierInfo *insertEntryNSForm[] = {&Ctx.Idents.get("insertEntry"),
+                                         &Ctx.Idents.get("atIndex")};
+  ADD_METHOD(NSForm, insertEntryNSForm, 2, 0)
+
+  NEW_RECEIVER(NSTextFieldCell)
+  ADD_UNARY_METHOD(NSTextFieldCell, setPlaceholderString, 0)
+
+  NEW_RECEIVER(NSUserNotificationAction)
+  IdentifierInfo *actionWithIdentifierNSUserNotificationAction[] = {
+      &Ctx.Idents.get("actionWithIdentifier"), &Ctx.Idents.get("title")};
+  ADD_METHOD(NSUserNotificationAction,
+             actionWithIdentifierNSUserNotificationAction, 2, 1)
+
+  NEW_RECEIVER(NSURLSession)
+  ADD_UNARY_METHOD(NSURLSession, setSessionDescription, 0)
+
+  NEW_RECEIVER(UITextField)
+  ADD_UNARY_METHOD(UITextField, setText, 0)
+  ADD_UNARY_METHOD(UITextField, setPlaceholder, 0)
+
+  NEW_RECEIVER(UIBarButtonItem)
+  IdentifierInfo *initWithTitleUIBarButtonItem[] = {
+      &Ctx.Idents.get("initWithTitle"), &Ctx.Idents.get("style"),
+      &Ctx.Idents.get("target"), &Ctx.Idents.get("action")};
+  ADD_METHOD(UIBarButtonItem, initWithTitleUIBarButtonItem, 4, 0)
+
+  NEW_RECEIVER(UIViewController)
+  ADD_UNARY_METHOD(UIViewController, setTitle, 0)
+
+  NEW_RECEIVER(UISegmentedControl)
+  IdentifierInfo *insertSegmentWithTitleUISegmentedControl[] = {
+      &Ctx.Idents.get("insertSegmentWithTitle"), &Ctx.Idents.get("atIndex"),
+      &Ctx.Idents.get("animated")};
+  ADD_METHOD(UISegmentedControl, insertSegmentWithTitleUISegmentedControl, 3, 0)
+  IdentifierInfo *setTitleUISegmentedControl[] = {
+      &Ctx.Idents.get("setTitle"), &Ctx.Idents.get("forSegmentAtIndex")};
+  ADD_METHOD(UISegmentedControl, setTitleUISegmentedControl, 2, 0)
 }
 
+#define LSF_INSERT(function_name) LSF.insert(&Ctx.Idents.get(function_name));
+#define LSM_INSERT_NULLARY(receiver, method_name)                              \
+  LSM.insert({&Ctx.Idents.get(receiver), Ctx.Selectors.getNullarySelector(     \
+                                             &Ctx.Idents.get(method_name))});
+#define LSM_INSERT_UNARY(receiver, method_name)                                \
+  LSM.insert({&Ctx.Idents.get(receiver),                                       \
+              Ctx.Selectors.getUnarySelector(&Ctx.Idents.get(method_name))});
+#define LSM_INSERT_SELECTOR(receiver, method_list, arguments)                  \
+  LSM.insert({&Ctx.Idents.get(receiver),                                       \
+              Ctx.Selectors.getSelector(arguments, method_list)});
+
 /// Initializes a list of methods and C functions that return a localized string
 void NonLocalizedStringChecker::initLocStringsMethods(ASTContext &Ctx) const {
   if (!LSM.empty())
     return;
 
-  LSM.insert({"NSBundle", "localizedStringForKey:value:table:"});
-  LSM.insert({"NSDateFormatter", "stringFromDate:"});
-  LSM.insert(
-      {"NSDateFormatter", "localizedStringFromDate:dateStyle:timeStyle:"});
-  LSM.insert({"NSNumberFormatter", "stringFromNumber:"});
-  LSM.insert({"UITextField", "text"});
-  LSM.insert({"UITextView", "text"});
-  LSM.insert({"UILabel", "text"});
-
-  LSF.insert("CFDateFormatterCreateStringWithDate");
-  LSF.insert("CFDateFormatterCreateStringWithAbsoluteTime");
-  LSF.insert("CFNumberFormatterCreateStringWithNumber");
+  IdentifierInfo *LocalizedStringMacro[] = {
+      &Ctx.Idents.get("localizedStringForKey"), &Ctx.Idents.get("value"),
+      &Ctx.Idents.get("table")};
+  LSM_INSERT_SELECTOR("NSBundle", LocalizedStringMacro, 3)
+  LSM_INSERT_UNARY("NSDateFormatter", "stringFromDate")
+  IdentifierInfo *LocalizedStringFromDate[] = {
+      &Ctx.Idents.get("localizedStringFromDate"), &Ctx.Idents.get("dateStyle"),
+      &Ctx.Idents.get("timeStyle")};
+  LSM_INSERT_SELECTOR("NSDateFormatter", LocalizedStringFromDate, 3)
+  LSM_INSERT_UNARY("NSNumberFormatter", "stringFromNumber")
+  LSM_INSERT_NULLARY("UITextField", "text")
+  LSM_INSERT_NULLARY("UITextView", "text")
+  LSM_INSERT_NULLARY("UILabel", "text")
+
+  LSF_INSERT("CFDateFormatterCreateStringWithDate");
+  LSF_INSERT("CFDateFormatterCreateStringWithAbsoluteTime");
+  LSF_INSERT("CFNumberFormatterCreateStringWithNumber");
 }
 
 /// Checks to see if the method / function declaration includes
 /// __attribute__((annotate("returns_localized_nsstring")))
 bool NonLocalizedStringChecker::isAnnotatedAsLocalized(const Decl *D) const {
+  if (!D)
+    return false;
   return std::any_of(
       D->specific_attr_begin<AnnotateAttr>(),
       D->specific_attr_end<AnnotateAttr>(), [](const AnnotateAttr *Ann) {
@@ -253,8 +633,8 @@ void NonLocalizedStringChecker::reportLo
     return;
 
   // Generate the bug report.
-  std::unique_ptr<BugReport> R(
-      new BugReport(*BT, "String should be localized", ErrNode));
+  std::unique_ptr<BugReport> R(new BugReport(
+      *BT, "User-facing text should use localized string macro", ErrNode));
   if (argumentNumber) {
     R->addRange(M.getArgExpr(argumentNumber - 1)->getSourceRange());
   } else {
@@ -264,6 +644,24 @@ void NonLocalizedStringChecker::reportLo
   C.emitReport(std::move(R));
 }
 
+/// Returns the argument number requiring localized string if it exists
+/// otherwise, returns -1
+int NonLocalizedStringChecker::getLocalizedArgumentForSelector(
+    const IdentifierInfo *Receiver, Selector S) const {
+  auto method = UIMethods.find(Receiver);
+
+  if (method == UIMethods.end())
+    return -1;
+
+  auto argumentIterator = method->getSecond().find(S);
+
+  if (argumentIterator == method->getSecond().end())
+    return -1;
+
+  int argumentNumber = argumentIterator->getSecond();
+  return argumentNumber;
+}
+
 /// Check if the string being passed in has NonLocalized state
 void NonLocalizedStringChecker::checkPreObjCMessage(const ObjCMethodCall &msg,
                                                     CheckerContext &C) const {
@@ -280,7 +678,6 @@ void NonLocalizedStringChecker::checkPre
   StringRef SelectorName = SelectorString;
   assert(!SelectorName.empty());
 
-  auto method = UIMethods.find(odInfo->getName());
   if (odInfo->isStr("NSString")) {
     // Handle the case where the receiver is an NSString
     // These special NSString methods draw to the screen
@@ -297,33 +694,42 @@ void NonLocalizedStringChecker::checkPre
     if (isNonLocalized) {
       reportLocalizationError(svTitle, msg, C);
     }
-  } else if (method != UIMethods.end()) {
-
-    auto argumentIterator = method->getValue().find(SelectorName);
+  }
 
-    if (argumentIterator == method->getValue().end())
-      return;
+  int argumentNumber = getLocalizedArgumentForSelector(odInfo, S);
+  // Go up each hierarchy of superclasses and their protocols
+  while (argumentNumber < 0 && OD->getSuperClass() != nullptr) {
+    for (const auto *P : OD->all_referenced_protocols()) {
+      argumentNumber = getLocalizedArgumentForSelector(P->getIdentifier(), S);
+      if (argumentNumber >= 0)
+        break;
+    }
+    if (argumentNumber < 0) {
+      OD = OD->getSuperClass();
+      argumentNumber = getLocalizedArgumentForSelector(OD->getIdentifier(), S);
+    }
+  }
 
-    int argumentNumber = argumentIterator->getValue();
+  if (argumentNumber < 0) // There was no match in UIMethods
+    return;
 
-    SVal svTitle = msg.getArgSVal(argumentNumber);
+  SVal svTitle = msg.getArgSVal(argumentNumber);
 
-    if (const ObjCStringRegion *SR =
-            dyn_cast_or_null<ObjCStringRegion>(svTitle.getAsRegion())) {
-      StringRef stringValue =
-          SR->getObjCStringLiteral()->getString()->getString();
-      if ((stringValue.trim().size() == 0 && stringValue.size() > 0) ||
-          stringValue.empty())
-        return;
-      if (!IsAggressive && llvm::sys::unicode::columnWidthUTF8(stringValue) < 2)
-        return;
-    }
+  if (const ObjCStringRegion *SR =
+          dyn_cast_or_null<ObjCStringRegion>(svTitle.getAsRegion())) {
+    StringRef stringValue =
+        SR->getObjCStringLiteral()->getString()->getString();
+    if ((stringValue.trim().size() == 0 && stringValue.size() > 0) ||
+        stringValue.empty())
+      return;
+    if (!IsAggressive && llvm::sys::unicode::columnWidthUTF8(stringValue) < 2)
+      return;
+  }
 
-    bool isNonLocalized = hasNonLocalizedState(svTitle, C);
+  bool isNonLocalized = hasNonLocalizedState(svTitle, C);
 
-    if (isNonLocalized) {
-      reportLocalizationError(svTitle, msg, C, argumentNumber + 1);
-    }
+  if (isNonLocalized) {
+    reportLocalizationError(svTitle, msg, C, argumentNumber + 1);
   }
 }
 
@@ -375,10 +781,10 @@ void NonLocalizedStringChecker::checkPos
   if (!D)
     return;
 
-  StringRef IdentifierName = C.getCalleeName(D->getAsFunction());
+  const IdentifierInfo *Identifier = Call.getCalleeIdentifier();
 
   SVal sv = Call.getReturnValue();
-  if (isAnnotatedAsLocalized(D) || LSF.find(IdentifierName) != LSF.end()) {
+  if (isAnnotatedAsLocalized(D) || LSF.count(Identifier) != 0) {
     setLocalizedState(sv, C);
   } else if (isNSStringType(RT, C.getASTContext()) &&
              !hasLocalizedState(sv, C)) {
@@ -407,13 +813,10 @@ void NonLocalizedStringChecker::checkPos
     return;
   const IdentifierInfo *odInfo = OD->getIdentifier();
 
-  StringRef IdentifierName = odInfo->getName();
-
   Selector S = msg.getSelector();
   std::string SelectorName = S.getAsString();
 
-  std::pair<StringRef, StringRef> MethodDescription = {IdentifierName,
-                                                       SelectorName};
+  std::pair<const IdentifierInfo *, Selector> MethodDescription = {odInfo, S};
 
   if (LSM.count(MethodDescription) || isAnnotatedAsLocalized(msg.getDecl())) {
     SVal sv = msg.getReturnValue();
@@ -505,7 +908,7 @@ void EmptyLocalizationContextChecker::Me
 
   const IdentifierInfo *odInfo = OD->getIdentifier();
 
-  if (!(odInfo->isStr("NSBundle") ||
+  if (!(odInfo->isStr("NSBundle") &&
         ME->getSelector().getAsString() ==
             "localizedStringForKey:value:table:")) {
     return;
@@ -573,12 +976,211 @@ void EmptyLocalizationContextChecker::Me
 void EmptyLocalizationContextChecker::MethodCrawler::reportEmptyContextError(
     const ObjCMessageExpr *ME) const {
   // Generate the bug report.
-  BR.EmitBasicReport(MD, Checker, "Context Missing", "Localizability Error",
+  BR.EmitBasicReport(MD, Checker, "Context Missing",
+                     "Localizability Issue (Apple)",
                      "Localized string macro should include a non-empty "
                      "comment for translators",
                      PathDiagnosticLocation(ME, BR.getSourceManager(), DCtx));
 }
 
+namespace {
+class PluralMisuseChecker : public Checker<check::ASTCodeBody> {
+
+  // A helper class, which walks the AST
+  class MethodCrawler : public RecursiveASTVisitor<MethodCrawler> {
+    BugReporter &BR;
+    const CheckerBase *Checker;
+    AnalysisDeclContext *AC;
+
+    // This functions like a stack. We push on any IfStmt or
+    // ConditionalOperator that matches the condition
+    // and pop it off when we leave that statement
+    llvm::SmallVector<const clang::Stmt *, 8> MatchingStatements;
+    // This is true when we are the direct-child of a
+    // matching statement
+    bool InMatchingStatement = false;
+
+  public:
+    explicit MethodCrawler(BugReporter &InBR, const CheckerBase *Checker,
+                           AnalysisDeclContext *InAC)
+        : BR(InBR), Checker(Checker), AC(InAC) {}
+
+    bool VisitIfStmt(const IfStmt *I);
+    bool EndVisitIfStmt(IfStmt *I);
+    bool TraverseIfStmt(IfStmt *x);
+    bool VisitConditionalOperator(const ConditionalOperator *C);
+    bool TraverseConditionalOperator(ConditionalOperator *C);
+    bool VisitCallExpr(const CallExpr *CE);
+    bool VisitObjCMessageExpr(const ObjCMessageExpr *ME);
+
+  private:
+    void reportPluralMisuseError(const Stmt *S) const;
+    bool isCheckingPlurality(const Expr *E) const;
+  };
+
+public:
+  void checkASTCodeBody(const Decl *D, AnalysisManager &Mgr,
+                        BugReporter &BR) const {
+    MethodCrawler Visitor(BR, this, Mgr.getAnalysisDeclContext(D));
+    Visitor.TraverseDecl(const_cast<Decl *>(D));
+  }
+};
+} // end anonymous namespace
+
+// Checks the condition of the IfStmt and returns true if one
+// of the following heuristics are met:
+// 1) The conidtion is a variable with "singular" or "plural" in the name
+// 2) The condition is a binary operator with 1 or 2 on the right-hand side
+bool PluralMisuseChecker::MethodCrawler::isCheckingPlurality(
+    const Expr *Condition) const {
+  const BinaryOperator *BO = nullptr;
+  // Accounts for when a VarDecl represents a BinaryOperator
+  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Condition)) {
+    if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
+      const Expr *InitExpr = VD->getInit();
+      if (InitExpr) {
+        if (const BinaryOperator *B =
+                dyn_cast<BinaryOperator>(InitExpr->IgnoreParenImpCasts())) {
+          BO = B;
+        }
+      }
+      if (VD->getName().lower().find("plural") != StringRef::npos ||
+          VD->getName().lower().find("singular") != StringRef::npos) {
+        return true;
+      }
+    }
+  } else if (const BinaryOperator *B = dyn_cast<BinaryOperator>(Condition)) {
+    BO = B;
+  }
+
+  if (BO == nullptr)
+    return false;
+
+  if (IntegerLiteral *IL = dyn_cast_or_null<IntegerLiteral>(
+          BO->getRHS()->IgnoreParenImpCasts())) {
+    llvm::APInt Value = IL->getValue();
+    if (Value == 1 || Value == 2) {
+      return true;
+    }
+  }
+  return false;
+}
+
+// A CallExpr with "LOC" in its identifier that takes in a string literal
+// has been shown to almost always be a function that returns a localized
+// string. Raise a diagnostic when this is in a statement that matches
+// the condition.
+bool PluralMisuseChecker::MethodCrawler::VisitCallExpr(const CallExpr *CE) {
+  if (InMatchingStatement) {
+    if (const FunctionDecl *FD = CE->getDirectCallee()) {
+      StringRef NormalizedName =
+          StringRef(FD->getNameInfo().getAsString()).lower();
+      if (NormalizedName.find("loc") != StringRef::npos) {
+        for (const Expr *Arg : CE->arguments()) {
+          if (isa<ObjCStringLiteral>(Arg))
+            reportPluralMisuseError(CE);
+        }
+      }
+    }
+  }
+  return true;
+}
+
+// The other case is for NSLocalizedString which also returns
+// a localized string. It's a macro for the ObjCMessageExpr
+// [NSBundle localizedStringForKey:value:table:] Raise a
+// diagnostic when this is in a statement that matches
+// the condition.
+bool PluralMisuseChecker::MethodCrawler::VisitObjCMessageExpr(
+    const ObjCMessageExpr *ME) {
+  const ObjCInterfaceDecl *OD = ME->getReceiverInterface();
+  if (!OD)
+    return true;
+
+  const IdentifierInfo *odInfo = OD->getIdentifier();
+
+  if (odInfo->isStr("NSBundle") &&
+      ME->getSelector().getAsString() == "localizedStringForKey:value:table:") {
+    if (InMatchingStatement) {
+      reportPluralMisuseError(ME);
+    }
+  }
+  return true;
+}
+
+/// Override TraverseIfStmt so we know when we are done traversing an IfStmt
+bool PluralMisuseChecker::MethodCrawler::TraverseIfStmt(IfStmt *I) {
+  RecursiveASTVisitor<MethodCrawler>::TraverseIfStmt(I);
+  return EndVisitIfStmt(I);
+}
+
+// EndVisit callbacks are not provided by the RecursiveASTVisitor
+// so we override TraverseIfStmt and make a call to EndVisitIfStmt
+// after traversing the IfStmt
+bool PluralMisuseChecker::MethodCrawler::EndVisitIfStmt(IfStmt *I) {
+  MatchingStatements.pop_back();
+  if (!MatchingStatements.empty()) {
+    if (MatchingStatements.back() != nullptr) {
+      InMatchingStatement = true;
+      return true;
+    }
+  }
+  InMatchingStatement = false;
+  return true;
+}
+
+bool PluralMisuseChecker::MethodCrawler::VisitIfStmt(const IfStmt *I) {
+  const Expr *Condition = I->getCond()->IgnoreParenImpCasts();
+  if (isCheckingPlurality(Condition)) {
+    MatchingStatements.push_back(I);
+    InMatchingStatement = true;
+  } else {
+    MatchingStatements.push_back(nullptr);
+    InMatchingStatement = false;
+  }
+
+  return true;
+}
+
+// Preliminary support for conditional operators.
+bool PluralMisuseChecker::MethodCrawler::TraverseConditionalOperator(
+    ConditionalOperator *C) {
+  RecursiveASTVisitor<MethodCrawler>::TraverseConditionalOperator(C);
+  MatchingStatements.pop_back();
+  if (!MatchingStatements.empty()) {
+    if (MatchingStatements.back() != nullptr)
+      InMatchingStatement = true;
+    else
+      InMatchingStatement = false;
+  } else {
+    InMatchingStatement = false;
+  }
+  return true;
+}
+
+bool PluralMisuseChecker::MethodCrawler::VisitConditionalOperator(
+    const ConditionalOperator *C) {
+  const Expr *Condition = C->getCond()->IgnoreParenImpCasts();
+  if (isCheckingPlurality(Condition)) {
+    MatchingStatements.push_back(C);
+    InMatchingStatement = true;
+  } else {
+    MatchingStatements.push_back(nullptr);
+    InMatchingStatement = false;
+  }
+  return true;
+}
+
+void PluralMisuseChecker::MethodCrawler::reportPluralMisuseError(
+    const Stmt *S) const {
+  // Generate the bug report.
+  BR.EmitBasicReport(AC->getDecl(), Checker, "Plural Misuse",
+                     "Localizability Issue (Apple)",
+                     "Plural cases are not supported accross all languages. "
+                     "Use a .stringsdict file instead",
+                     PathDiagnosticLocation(S, BR.getSourceManager(), AC));
+}
+
 //===----------------------------------------------------------------------===//
 // Checker registration.
 //===----------------------------------------------------------------------===//
@@ -593,3 +1195,7 @@ void ento::registerNonLocalizedStringChe
 void ento::registerEmptyLocalizationContextChecker(CheckerManager &mgr) {
   mgr.registerChecker<EmptyLocalizationContextChecker>();
 }
+
+void ento::registerPluralMisuseChecker(CheckerManager &mgr) {
+  mgr.registerChecker<PluralMisuseChecker>();
+}
\ No newline at end of file

Modified: cfe/trunk/test/Analysis/localization-aggressive.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/localization-aggressive.m?rev=248350&r1=248349&r2=248350&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/localization-aggressive.m (original)
+++ cfe/trunk/test/Analysis/localization-aggressive.m Tue Sep 22 18:58:04 2015
@@ -34,13 +34,21 @@ typedef struct CGPoint CGPoint;
                               value:(NSString *)value
                               table:(NSString *)tableName;
 @end
- at interface UILabel : NSObject
- at property(nullable, nonatomic, copy) NSString *text;
+ at protocol UIAccessibility 
 - (void)accessibilitySetIdentification:(NSString *)ident;
+- (void)setAccessibilityLabel:(NSString *)label;
+ at end
+ at interface UILabel : NSObject <UIAccessibility>
+ at property(nullable, nonatomic, copy) NSString *text;
 @end
 @interface TestObject : NSObject
 @property(strong) NSString *text;
 @end
+ at interface NSView : NSObject
+ at property (strong) NSString *toolTip;
+ at end
+ at interface NSViewSubclass : NSView
+ at end
 
 @interface LocalizationTestSuite : NSObject
 NSString *ForceLocalized(NSString *str)
@@ -78,7 +86,7 @@ NSString *ForceLocalized(NSString *str)
     bar = @"Unlocalized string";
   }
 
-  [testLabel setText:bar]; // expected-warning {{String should be localized}}
+  [testLabel setText:bar]; // expected-warning {{User-facing text should use localized string macro}}
 }
 
 - (void)testLocalizationErrorDetectedOnNSString {
@@ -88,7 +96,7 @@ NSString *ForceLocalized(NSString *str)
     bar = @"Unlocalized string";
   }
 
-  [bar drawAtPoint:CGPointMake(0, 0) withAttributes:nil]; // expected-warning {{String should be localized}}
+  [bar drawAtPoint:CGPointMake(0, 0) withAttributes:nil]; // expected-warning {{User-facing text should use localized string macro}}
 }
 
 - (void)testNoLocalizationErrorDetectedFromCFunction {
@@ -137,7 +145,7 @@ NSString *ForceLocalized(NSString *str)
 // An string literal @"Hello" inline should raise an error
 - (void)testInlineStringLiteralHasLocalizedState {
   UILabel *testLabel = [[UILabel alloc] init];
-  [testLabel setText:@"Hello"]; // expected-warning {{String should be localized}}
+  [testLabel setText:@"Hello"]; // expected-warning {{User-facing text should use localized string macro}}
 }
 
 // A nil string should not raise an error
@@ -176,7 +184,7 @@ NSString *ForceLocalized(NSString *str)
 - (void)localizedStringAsArgument:(NSString *)argumentString {
   UILabel *testLabel = [[UILabel alloc] init];
 
-  [testLabel setText:argumentString]; // expected-warning {{String should be localized}}
+  [testLabel setText:argumentString]; // expected-warning {{User-facing text should use localized string macro}}
 }
 
 // [LocalizationTestSuite unLocalizedStringMethod] returns an unlocalized string
@@ -187,7 +195,7 @@ NSString *ForceLocalized(NSString *str)
   UILabel *testLabel = [[UILabel alloc] init];
   NSString *bar = NSLocalizedString(@"Hello", @"Comment");
 
-  [testLabel setText:[LocalizationTestSuite unLocalizedStringMethod]]; // expected-warning {{String should be localized}}
+  [testLabel setText:[LocalizationTestSuite unLocalizedStringMethod]]; // expected-warning {{User-facing text should use localized string macro}}
 }
 
 // This is the reverse situation: accessibilitySetIdentification: doesn't care
@@ -198,6 +206,21 @@ NSString *ForceLocalized(NSString *str)
   [testLabel accessibilitySetIdentification:@"UnlocalizedString"]; // no-warning
 }
 
+// An NSView subclass should raise a warning for methods in NSView that 
+// require localized strings 
+- (void)testRequiresLocalizationMethodFromSuperclass {
+  NSViewSubclass *s = [[NSViewSubclass alloc] init];
+  NSString *bar = @"UnlocalizedString";
+
+  [s setToolTip:bar]; // expected-warning {{User-facing text should use localized string macro}}
+}
+
+- (void)testRequiresLocalizationMethodFromProtocol {
+  UILabel *testLabel = [[UILabel alloc] init];
+
+  [testLabel setAccessibilityLabel:@"UnlocalizedString"]; // expected-warning {{User-facing text should use localized string macro}}
+}
+
 // EmptyLocalizationContextChecker tests
 #define HOM(s) YOLOC(s)
 #define YOLOC(x) NSLocalizedString(x, nil)

Modified: cfe/trunk/test/Analysis/localization.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/localization.m?rev=248350&r1=248349&r2=248350&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/localization.m (original)
+++ cfe/trunk/test/Analysis/localization.m Tue Sep 22 18:58:04 2015
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -analyze -fblocks -analyzer-store=region -analyzer-checker=alpha.osx.cocoa.NonLocalizedStringChecker -analyzer-checker=alpha.osx.cocoa.EmptyLocalizationContextChecker -verify  %s
+// RUN: %clang_cc1 -analyze -fblocks -analyzer-store=region -analyzer-checker=alpha.osx.cocoa.NonLocalizedStringChecker -analyzer-checker=alpha.osx.cocoa.PluralMisuseChecker -verify  %s
 
 // The larger set of tests in located in localization.m. These are tests
 // specific for non-aggressive reporting.
@@ -20,6 +20,8 @@
 - (id)init;
 @end
 @interface NSString : NSObject
+- (NSString *)stringByAppendingFormat:(NSString *)format, ...;
++ (instancetype)stringWithFormat:(NSString *)format, ...;
 @end
 @interface NSBundle : NSObject
 + (NSBundle *)mainBundle;
@@ -36,11 +38,16 @@
 
 @interface LocalizationTestSuite : NSObject
 int random();
+ at property (assign) int unreadArticlesCount;
 @end
-
+#define MCLocalizedString(s) NSLocalizedString(s,nil);
 // Test cases begin here
 @implementation LocalizationTestSuite
 
+NSString *KHLocalizedString(NSString* key, NSString* comment) {
+    return NSLocalizedString(key, comment);
+}
+
 // An object passed in as an parameter's string member
 // should not be considered unlocalized
 - (void)testObjectAsArgument:(TestObject *)argumentObject {
@@ -58,7 +65,7 @@ int random();
     bar = @"Unlocalized string";
   }
 
-  [testLabel setText:bar]; // expected-warning {{String should be localized}}
+  [testLabel setText:bar]; // expected-warning {{User-facing text should use localized string macro}}
 }
 
 - (void)testOneCharacterStringsDoNotGiveAWarning {
@@ -83,4 +90,118 @@ int random();
   [testLabel setText:bar]; // no-warning
 }
 
+// Plural Misuse Checker Tests
+// These tests are modeled off incorrect uses of the many-one pattern
+// from real projects. 
+
+- (NSString *)test1:(int)plural {
+    if (plural) {
+        return MCLocalizedString(@"TYPE_PLURAL"); // expected-warning {{Plural cases are not supported accross all languages. Use a .stringsdict file}}
+    }
+    return MCLocalizedString(@"TYPE");
+}
+
+- (NSString *)test2:(int)numOfReminders {
+    if (numOfReminders > 0) {
+        return [NSString stringWithFormat:@"%@, %@", @"Test", (numOfReminders != 1) ? [NSString stringWithFormat:NSLocalizedString(@"%@ Reminders", @"Plural count of reminders"), numOfReminders] : [NSString stringWithFormat:NSLocalizedString(@"1 reminder", @"One reminder")]]; // expected-warning {{Plural cases are not supported accross all languages. Use a .stringsdict file}} expected-warning {{Plural cases are not supported accross all languages. Use a .stringsdict file}}
+    } 
+    return nil;
+}
+
+- (void)test3 {
+    NSString *count;
+    if (self.unreadArticlesCount > 1)
+    {
+        count = [count stringByAppendingFormat:@"%@", KHLocalizedString(@"New Stories", @"Plural count for new stories")]; // expected-warning {{Plural cases are not supported accross all languages. Use a .stringsdict file}}
+    } else {
+        count = [count stringByAppendingFormat:@"%@",  KHLocalizedString(@"New Story", @"One new story")]; // expected-warning {{Plural cases are not supported accross all languages. Use a .stringsdict file}}
+    }
+}
+
+- (NSString *)test4:(int)count {
+    if ( count == 1 )
+    {
+        return [NSString stringWithFormat:KHLocalizedString(@"value.singular",nil), count]; // expected-warning {{Plural cases are not supported accross all languages. Use a .stringsdict file}}
+    } else {
+        return [NSString stringWithFormat:KHLocalizedString(@"value.plural",nil), count]; // expected-warning {{Plural cases are not supported accross all languages. Use a .stringsdict file}}
+    }
+}
+
+- (NSString *)test5:(int)count {
+	int test = count == 1;
+    if (test)
+    {
+        return [NSString stringWithFormat:KHLocalizedString(@"value.singular",nil), count]; // expected-warning {{Plural cases are not supported accross all languages. Use a .stringsdict file}}
+    } else {
+        return [NSString stringWithFormat:KHLocalizedString(@"value.plural",nil), count]; // expected-warning {{Plural cases are not supported accross all languages. Use a .stringsdict file}}
+    }
+}
+
+// This tests the heuristic that the direct parent IfStmt must match the isCheckingPlurality confition to avoid false positives generated from complex code (generally the pattern we're looking for is simple If-Else)
+
+- (NSString *)test6:(int)sectionIndex {
+	int someOtherVariable = 0;
+    if (sectionIndex == 1)
+    {
+		// Do some other crazy stuff
+		if (someOtherVariable)
+        	return KHLocalizedString(@"OK",nil); // no-warning
+    } else {
+        return KHLocalizedString(@"value.plural",nil); // expected-warning {{Plural cases are not supported accross all languages. Use a .stringsdict file}}
+    }
+	return nil;
+}
+
+// False positives that we are not accounting for involve matching the heuristic
+// of having 1 or 2 in the RHS of a BinaryOperator and having a localized string 
+// in the body of the IfStmt. This is seen a lot when checking for the section
+// indexpath of something like a UITableView
+
+// - (NSString *)testNotAccountedFor:(int)sectionIndex {
+//     if (sectionIndex == 1)
+//     {
+//         return KHLocalizedString(@"1",nil); // false-positive
+//     } else if (sectionIndex == 2) {
+//     	return KHLocalizedString(@"2",nil); // false-positive
+//     } else if (sectionIndex == 3) {
+// 		return KHLocalizedString(@"3",nil); // no-false-positive
+// 	}
+// }
+
+// Potential test-cases to support in the future
+
+// - (NSString *)test7:(int)count {
+//     BOOL plural = count != 1;
+//     return KHLocalizedString(plural ? @"PluralString" : @"SingularString", @"");
+// }
+//
+// - (NSString *)test8:(BOOL)plural {
+//     return KHLocalizedString(([NSString stringWithFormat:@"RELATIVE_DATE_%@_%@", ((1 == 1) ? @"FUTURE" : @"PAST"), plural ? @"PLURAL" : @"SINGULAR"]));
+// }
+//
+//
+//
+// - (void)test9:(int)numberOfTimesEarned {
+//     NSString* localizedDescriptionKey;
+//     if (numberOfTimesEarned == 1) {
+//         localizedDescriptionKey = @"SINGULAR_%@";
+//     } else {
+//         localizedDescriptionKey = @"PLURAL_%@_%@";
+//     }
+//     NSLocalizedString(localizedDescriptionKey, nil);
+// }
+//
+// - (NSString *)test10 {
+//     NSInteger count = self.problems.count;
+//     NSString *title = [NSString stringWithFormat:@"%ld Problems", (long) count];
+//     if (count < 2) {
+//         if (count == 0) {
+//             title = [NSString stringWithFormat:@"No Problems Found"];
+//         } else {
+//             title = [NSString stringWithFormat:@"%ld Problem", (long) count];
+//         }
+//     }
+//     return title;
+// }
+
 @end




More information about the cfe-commits mailing list