xref: /netbsd-src/external/apache2/llvm/dist/clang/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
17330f729Sjoerg //=- LocalizationChecker.cpp -------------------------------------*- C++ -*-==//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg //  This file defines a set of checks for localizability including:
107330f729Sjoerg //  1) A checker that warns about uses of non-localized NSStrings passed to
117330f729Sjoerg //     UI methods expecting localized strings
127330f729Sjoerg //  2) A syntactic checker that warns against the bad practice of
137330f729Sjoerg //     not including a comment in NSLocalizedString macros.
147330f729Sjoerg //
157330f729Sjoerg //===----------------------------------------------------------------------===//
167330f729Sjoerg 
177330f729Sjoerg #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
187330f729Sjoerg #include "clang/AST/Attr.h"
197330f729Sjoerg #include "clang/AST/Decl.h"
207330f729Sjoerg #include "clang/AST/DeclObjC.h"
217330f729Sjoerg #include "clang/AST/RecursiveASTVisitor.h"
227330f729Sjoerg #include "clang/AST/StmtVisitor.h"
237330f729Sjoerg #include "clang/Lex/Lexer.h"
247330f729Sjoerg #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
257330f729Sjoerg #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
267330f729Sjoerg #include "clang/StaticAnalyzer/Core/Checker.h"
277330f729Sjoerg #include "clang/StaticAnalyzer/Core/CheckerManager.h"
287330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
297330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
307330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
317330f729Sjoerg #include "llvm/Support/Unicode.h"
327330f729Sjoerg 
337330f729Sjoerg using namespace clang;
347330f729Sjoerg using namespace ento;
357330f729Sjoerg 
367330f729Sjoerg namespace {
377330f729Sjoerg struct LocalizedState {
387330f729Sjoerg private:
397330f729Sjoerg   enum Kind { NonLocalized, Localized } K;
LocalizedState__anonf70572170111::LocalizedState407330f729Sjoerg   LocalizedState(Kind InK) : K(InK) {}
417330f729Sjoerg 
427330f729Sjoerg public:
isLocalized__anonf70572170111::LocalizedState437330f729Sjoerg   bool isLocalized() const { return K == Localized; }
isNonLocalized__anonf70572170111::LocalizedState447330f729Sjoerg   bool isNonLocalized() const { return K == NonLocalized; }
457330f729Sjoerg 
getLocalized__anonf70572170111::LocalizedState467330f729Sjoerg   static LocalizedState getLocalized() { return LocalizedState(Localized); }
getNonLocalized__anonf70572170111::LocalizedState477330f729Sjoerg   static LocalizedState getNonLocalized() {
487330f729Sjoerg     return LocalizedState(NonLocalized);
497330f729Sjoerg   }
507330f729Sjoerg 
517330f729Sjoerg   // Overload the == operator
operator ==__anonf70572170111::LocalizedState527330f729Sjoerg   bool operator==(const LocalizedState &X) const { return K == X.K; }
537330f729Sjoerg 
547330f729Sjoerg   // LLVMs equivalent of a hash function
Profile__anonf70572170111::LocalizedState557330f729Sjoerg   void Profile(llvm::FoldingSetNodeID &ID) const { ID.AddInteger(K); }
567330f729Sjoerg };
577330f729Sjoerg 
587330f729Sjoerg class NonLocalizedStringChecker
597330f729Sjoerg     : public Checker<check::PreCall, check::PostCall, check::PreObjCMessage,
607330f729Sjoerg                      check::PostObjCMessage,
617330f729Sjoerg                      check::PostStmt<ObjCStringLiteral>> {
627330f729Sjoerg 
637330f729Sjoerg   mutable std::unique_ptr<BugType> BT;
647330f729Sjoerg 
657330f729Sjoerg   // Methods that require a localized string
667330f729Sjoerg   mutable llvm::DenseMap<const IdentifierInfo *,
677330f729Sjoerg                          llvm::DenseMap<Selector, uint8_t>> UIMethods;
687330f729Sjoerg   // Methods that return a localized string
697330f729Sjoerg   mutable llvm::SmallSet<std::pair<const IdentifierInfo *, Selector>, 12> LSM;
707330f729Sjoerg   // C Functions that return a localized string
717330f729Sjoerg   mutable llvm::SmallSet<const IdentifierInfo *, 5> LSF;
727330f729Sjoerg 
737330f729Sjoerg   void initUIMethods(ASTContext &Ctx) const;
747330f729Sjoerg   void initLocStringsMethods(ASTContext &Ctx) const;
757330f729Sjoerg 
767330f729Sjoerg   bool hasNonLocalizedState(SVal S, CheckerContext &C) const;
777330f729Sjoerg   bool hasLocalizedState(SVal S, CheckerContext &C) const;
787330f729Sjoerg   void setNonLocalizedState(SVal S, CheckerContext &C) const;
797330f729Sjoerg   void setLocalizedState(SVal S, CheckerContext &C) const;
807330f729Sjoerg 
817330f729Sjoerg   bool isAnnotatedAsReturningLocalized(const Decl *D) const;
827330f729Sjoerg   bool isAnnotatedAsTakingLocalized(const Decl *D) const;
837330f729Sjoerg   void reportLocalizationError(SVal S, const CallEvent &M, CheckerContext &C,
847330f729Sjoerg                                int argumentNumber = 0) const;
857330f729Sjoerg 
867330f729Sjoerg   int getLocalizedArgumentForSelector(const IdentifierInfo *Receiver,
877330f729Sjoerg                                       Selector S) const;
887330f729Sjoerg 
897330f729Sjoerg public:
907330f729Sjoerg   NonLocalizedStringChecker();
917330f729Sjoerg 
927330f729Sjoerg   // When this parameter is set to true, the checker assumes all
937330f729Sjoerg   // methods that return NSStrings are unlocalized. Thus, more false
947330f729Sjoerg   // positives will be reported.
957330f729Sjoerg   DefaultBool IsAggressive;
967330f729Sjoerg 
977330f729Sjoerg   void checkPreObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const;
987330f729Sjoerg   void checkPostObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const;
997330f729Sjoerg   void checkPostStmt(const ObjCStringLiteral *SL, CheckerContext &C) const;
1007330f729Sjoerg   void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
1017330f729Sjoerg   void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
1027330f729Sjoerg };
1037330f729Sjoerg 
1047330f729Sjoerg } // end anonymous namespace
1057330f729Sjoerg 
REGISTER_MAP_WITH_PROGRAMSTATE(LocalizedMemMap,const MemRegion *,LocalizedState)1067330f729Sjoerg REGISTER_MAP_WITH_PROGRAMSTATE(LocalizedMemMap, const MemRegion *,
1077330f729Sjoerg                                LocalizedState)
1087330f729Sjoerg 
1097330f729Sjoerg NonLocalizedStringChecker::NonLocalizedStringChecker() {
1107330f729Sjoerg   BT.reset(new BugType(this, "Unlocalizable string",
1117330f729Sjoerg                        "Localizability Issue (Apple)"));
1127330f729Sjoerg }
1137330f729Sjoerg 
1147330f729Sjoerg namespace {
1157330f729Sjoerg class NonLocalizedStringBRVisitor final : public BugReporterVisitor {
1167330f729Sjoerg 
1177330f729Sjoerg   const MemRegion *NonLocalizedString;
1187330f729Sjoerg   bool Satisfied;
1197330f729Sjoerg 
1207330f729Sjoerg public:
NonLocalizedStringBRVisitor(const MemRegion * NonLocalizedString)1217330f729Sjoerg   NonLocalizedStringBRVisitor(const MemRegion *NonLocalizedString)
1227330f729Sjoerg       : NonLocalizedString(NonLocalizedString), Satisfied(false) {
1237330f729Sjoerg     assert(NonLocalizedString);
1247330f729Sjoerg   }
1257330f729Sjoerg 
1267330f729Sjoerg   PathDiagnosticPieceRef VisitNode(const ExplodedNode *Succ,
1277330f729Sjoerg                                    BugReporterContext &BRC,
1287330f729Sjoerg                                    PathSensitiveBugReport &BR) override;
1297330f729Sjoerg 
Profile(llvm::FoldingSetNodeID & ID) const1307330f729Sjoerg   void Profile(llvm::FoldingSetNodeID &ID) const override {
1317330f729Sjoerg     ID.Add(NonLocalizedString);
1327330f729Sjoerg   }
1337330f729Sjoerg };
1347330f729Sjoerg } // End anonymous namespace.
1357330f729Sjoerg 
1367330f729Sjoerg #define NEW_RECEIVER(receiver)                                                 \
1377330f729Sjoerg   llvm::DenseMap<Selector, uint8_t> &receiver##M =                             \
1387330f729Sjoerg       UIMethods.insert({&Ctx.Idents.get(#receiver),                            \
1397330f729Sjoerg                         llvm::DenseMap<Selector, uint8_t>()})                  \
1407330f729Sjoerg           .first->second;
1417330f729Sjoerg #define ADD_NULLARY_METHOD(receiver, method, argument)                         \
1427330f729Sjoerg   receiver##M.insert(                                                          \
1437330f729Sjoerg       {Ctx.Selectors.getNullarySelector(&Ctx.Idents.get(#method)), argument});
1447330f729Sjoerg #define ADD_UNARY_METHOD(receiver, method, argument)                           \
1457330f729Sjoerg   receiver##M.insert(                                                          \
1467330f729Sjoerg       {Ctx.Selectors.getUnarySelector(&Ctx.Idents.get(#method)), argument});
1477330f729Sjoerg #define ADD_METHOD(receiver, method_list, count, argument)                     \
1487330f729Sjoerg   receiver##M.insert({Ctx.Selectors.getSelector(count, method_list), argument});
1497330f729Sjoerg 
1507330f729Sjoerg /// Initializes a list of methods that require a localized string
1517330f729Sjoerg /// Format: {"ClassName", {{"selectorName:", LocStringArg#}, ...}, ...}
initUIMethods(ASTContext & Ctx) const1527330f729Sjoerg void NonLocalizedStringChecker::initUIMethods(ASTContext &Ctx) const {
1537330f729Sjoerg   if (!UIMethods.empty())
1547330f729Sjoerg     return;
1557330f729Sjoerg 
1567330f729Sjoerg   // UI Methods
1577330f729Sjoerg   NEW_RECEIVER(UISearchDisplayController)
1587330f729Sjoerg   ADD_UNARY_METHOD(UISearchDisplayController, setSearchResultsTitle, 0)
1597330f729Sjoerg 
1607330f729Sjoerg   NEW_RECEIVER(UITabBarItem)
1617330f729Sjoerg   IdentifierInfo *initWithTitleUITabBarItemTag[] = {
1627330f729Sjoerg       &Ctx.Idents.get("initWithTitle"), &Ctx.Idents.get("image"),
1637330f729Sjoerg       &Ctx.Idents.get("tag")};
1647330f729Sjoerg   ADD_METHOD(UITabBarItem, initWithTitleUITabBarItemTag, 3, 0)
1657330f729Sjoerg   IdentifierInfo *initWithTitleUITabBarItemImage[] = {
1667330f729Sjoerg       &Ctx.Idents.get("initWithTitle"), &Ctx.Idents.get("image"),
1677330f729Sjoerg       &Ctx.Idents.get("selectedImage")};
1687330f729Sjoerg   ADD_METHOD(UITabBarItem, initWithTitleUITabBarItemImage, 3, 0)
1697330f729Sjoerg 
1707330f729Sjoerg   NEW_RECEIVER(NSDockTile)
1717330f729Sjoerg   ADD_UNARY_METHOD(NSDockTile, setBadgeLabel, 0)
1727330f729Sjoerg 
1737330f729Sjoerg   NEW_RECEIVER(NSStatusItem)
1747330f729Sjoerg   ADD_UNARY_METHOD(NSStatusItem, setTitle, 0)
1757330f729Sjoerg   ADD_UNARY_METHOD(NSStatusItem, setToolTip, 0)
1767330f729Sjoerg 
1777330f729Sjoerg   NEW_RECEIVER(UITableViewRowAction)
1787330f729Sjoerg   IdentifierInfo *rowActionWithStyleUITableViewRowAction[] = {
1797330f729Sjoerg       &Ctx.Idents.get("rowActionWithStyle"), &Ctx.Idents.get("title"),
1807330f729Sjoerg       &Ctx.Idents.get("handler")};
1817330f729Sjoerg   ADD_METHOD(UITableViewRowAction, rowActionWithStyleUITableViewRowAction, 3, 1)
1827330f729Sjoerg   ADD_UNARY_METHOD(UITableViewRowAction, setTitle, 0)
1837330f729Sjoerg 
1847330f729Sjoerg   NEW_RECEIVER(NSBox)
1857330f729Sjoerg   ADD_UNARY_METHOD(NSBox, setTitle, 0)
1867330f729Sjoerg 
1877330f729Sjoerg   NEW_RECEIVER(NSButton)
1887330f729Sjoerg   ADD_UNARY_METHOD(NSButton, setTitle, 0)
1897330f729Sjoerg   ADD_UNARY_METHOD(NSButton, setAlternateTitle, 0)
1907330f729Sjoerg   IdentifierInfo *radioButtonWithTitleNSButton[] = {
1917330f729Sjoerg       &Ctx.Idents.get("radioButtonWithTitle"), &Ctx.Idents.get("target"),
1927330f729Sjoerg       &Ctx.Idents.get("action")};
1937330f729Sjoerg   ADD_METHOD(NSButton, radioButtonWithTitleNSButton, 3, 0)
1947330f729Sjoerg   IdentifierInfo *buttonWithTitleNSButtonImage[] = {
1957330f729Sjoerg       &Ctx.Idents.get("buttonWithTitle"), &Ctx.Idents.get("image"),
1967330f729Sjoerg       &Ctx.Idents.get("target"), &Ctx.Idents.get("action")};
1977330f729Sjoerg   ADD_METHOD(NSButton, buttonWithTitleNSButtonImage, 4, 0)
1987330f729Sjoerg   IdentifierInfo *checkboxWithTitleNSButton[] = {
1997330f729Sjoerg       &Ctx.Idents.get("checkboxWithTitle"), &Ctx.Idents.get("target"),
2007330f729Sjoerg       &Ctx.Idents.get("action")};
2017330f729Sjoerg   ADD_METHOD(NSButton, checkboxWithTitleNSButton, 3, 0)
2027330f729Sjoerg   IdentifierInfo *buttonWithTitleNSButtonTarget[] = {
2037330f729Sjoerg       &Ctx.Idents.get("buttonWithTitle"), &Ctx.Idents.get("target"),
2047330f729Sjoerg       &Ctx.Idents.get("action")};
2057330f729Sjoerg   ADD_METHOD(NSButton, buttonWithTitleNSButtonTarget, 3, 0)
2067330f729Sjoerg 
2077330f729Sjoerg   NEW_RECEIVER(NSSavePanel)
2087330f729Sjoerg   ADD_UNARY_METHOD(NSSavePanel, setPrompt, 0)
2097330f729Sjoerg   ADD_UNARY_METHOD(NSSavePanel, setTitle, 0)
2107330f729Sjoerg   ADD_UNARY_METHOD(NSSavePanel, setNameFieldLabel, 0)
2117330f729Sjoerg   ADD_UNARY_METHOD(NSSavePanel, setNameFieldStringValue, 0)
2127330f729Sjoerg   ADD_UNARY_METHOD(NSSavePanel, setMessage, 0)
2137330f729Sjoerg 
2147330f729Sjoerg   NEW_RECEIVER(UIPrintInfo)
2157330f729Sjoerg   ADD_UNARY_METHOD(UIPrintInfo, setJobName, 0)
2167330f729Sjoerg 
2177330f729Sjoerg   NEW_RECEIVER(NSTabViewItem)
2187330f729Sjoerg   ADD_UNARY_METHOD(NSTabViewItem, setLabel, 0)
2197330f729Sjoerg   ADD_UNARY_METHOD(NSTabViewItem, setToolTip, 0)
2207330f729Sjoerg 
2217330f729Sjoerg   NEW_RECEIVER(NSBrowser)
2227330f729Sjoerg   IdentifierInfo *setTitleNSBrowser[] = {&Ctx.Idents.get("setTitle"),
2237330f729Sjoerg                                          &Ctx.Idents.get("ofColumn")};
2247330f729Sjoerg   ADD_METHOD(NSBrowser, setTitleNSBrowser, 2, 0)
2257330f729Sjoerg 
2267330f729Sjoerg   NEW_RECEIVER(UIAccessibilityElement)
2277330f729Sjoerg   ADD_UNARY_METHOD(UIAccessibilityElement, setAccessibilityLabel, 0)
2287330f729Sjoerg   ADD_UNARY_METHOD(UIAccessibilityElement, setAccessibilityHint, 0)
2297330f729Sjoerg   ADD_UNARY_METHOD(UIAccessibilityElement, setAccessibilityValue, 0)
2307330f729Sjoerg 
2317330f729Sjoerg   NEW_RECEIVER(UIAlertAction)
2327330f729Sjoerg   IdentifierInfo *actionWithTitleUIAlertAction[] = {
2337330f729Sjoerg       &Ctx.Idents.get("actionWithTitle"), &Ctx.Idents.get("style"),
2347330f729Sjoerg       &Ctx.Idents.get("handler")};
2357330f729Sjoerg   ADD_METHOD(UIAlertAction, actionWithTitleUIAlertAction, 3, 0)
2367330f729Sjoerg 
2377330f729Sjoerg   NEW_RECEIVER(NSPopUpButton)
2387330f729Sjoerg   ADD_UNARY_METHOD(NSPopUpButton, addItemWithTitle, 0)
2397330f729Sjoerg   IdentifierInfo *insertItemWithTitleNSPopUpButton[] = {
2407330f729Sjoerg       &Ctx.Idents.get("insertItemWithTitle"), &Ctx.Idents.get("atIndex")};
2417330f729Sjoerg   ADD_METHOD(NSPopUpButton, insertItemWithTitleNSPopUpButton, 2, 0)
2427330f729Sjoerg   ADD_UNARY_METHOD(NSPopUpButton, removeItemWithTitle, 0)
2437330f729Sjoerg   ADD_UNARY_METHOD(NSPopUpButton, selectItemWithTitle, 0)
2447330f729Sjoerg   ADD_UNARY_METHOD(NSPopUpButton, setTitle, 0)
2457330f729Sjoerg 
2467330f729Sjoerg   NEW_RECEIVER(NSTableViewRowAction)
2477330f729Sjoerg   IdentifierInfo *rowActionWithStyleNSTableViewRowAction[] = {
2487330f729Sjoerg       &Ctx.Idents.get("rowActionWithStyle"), &Ctx.Idents.get("title"),
2497330f729Sjoerg       &Ctx.Idents.get("handler")};
2507330f729Sjoerg   ADD_METHOD(NSTableViewRowAction, rowActionWithStyleNSTableViewRowAction, 3, 1)
2517330f729Sjoerg   ADD_UNARY_METHOD(NSTableViewRowAction, setTitle, 0)
2527330f729Sjoerg 
2537330f729Sjoerg   NEW_RECEIVER(NSImage)
2547330f729Sjoerg   ADD_UNARY_METHOD(NSImage, setAccessibilityDescription, 0)
2557330f729Sjoerg 
2567330f729Sjoerg   NEW_RECEIVER(NSUserActivity)
2577330f729Sjoerg   ADD_UNARY_METHOD(NSUserActivity, setTitle, 0)
2587330f729Sjoerg 
2597330f729Sjoerg   NEW_RECEIVER(NSPathControlItem)
2607330f729Sjoerg   ADD_UNARY_METHOD(NSPathControlItem, setTitle, 0)
2617330f729Sjoerg 
2627330f729Sjoerg   NEW_RECEIVER(NSCell)
2637330f729Sjoerg   ADD_UNARY_METHOD(NSCell, initTextCell, 0)
2647330f729Sjoerg   ADD_UNARY_METHOD(NSCell, setTitle, 0)
2657330f729Sjoerg   ADD_UNARY_METHOD(NSCell, setStringValue, 0)
2667330f729Sjoerg 
2677330f729Sjoerg   NEW_RECEIVER(NSPathControl)
2687330f729Sjoerg   ADD_UNARY_METHOD(NSPathControl, setPlaceholderString, 0)
2697330f729Sjoerg 
2707330f729Sjoerg   NEW_RECEIVER(UIAccessibility)
2717330f729Sjoerg   ADD_UNARY_METHOD(UIAccessibility, setAccessibilityLabel, 0)
2727330f729Sjoerg   ADD_UNARY_METHOD(UIAccessibility, setAccessibilityHint, 0)
2737330f729Sjoerg   ADD_UNARY_METHOD(UIAccessibility, setAccessibilityValue, 0)
2747330f729Sjoerg 
2757330f729Sjoerg   NEW_RECEIVER(NSTableColumn)
2767330f729Sjoerg   ADD_UNARY_METHOD(NSTableColumn, setTitle, 0)
2777330f729Sjoerg   ADD_UNARY_METHOD(NSTableColumn, setHeaderToolTip, 0)
2787330f729Sjoerg 
2797330f729Sjoerg   NEW_RECEIVER(NSSegmentedControl)
2807330f729Sjoerg   IdentifierInfo *setLabelNSSegmentedControl[] = {
2817330f729Sjoerg       &Ctx.Idents.get("setLabel"), &Ctx.Idents.get("forSegment")};
2827330f729Sjoerg   ADD_METHOD(NSSegmentedControl, setLabelNSSegmentedControl, 2, 0)
2837330f729Sjoerg   IdentifierInfo *setToolTipNSSegmentedControl[] = {
2847330f729Sjoerg       &Ctx.Idents.get("setToolTip"), &Ctx.Idents.get("forSegment")};
2857330f729Sjoerg   ADD_METHOD(NSSegmentedControl, setToolTipNSSegmentedControl, 2, 0)
2867330f729Sjoerg 
2877330f729Sjoerg   NEW_RECEIVER(NSButtonCell)
2887330f729Sjoerg   ADD_UNARY_METHOD(NSButtonCell, setTitle, 0)
2897330f729Sjoerg   ADD_UNARY_METHOD(NSButtonCell, setAlternateTitle, 0)
2907330f729Sjoerg 
2917330f729Sjoerg   NEW_RECEIVER(NSDatePickerCell)
2927330f729Sjoerg   ADD_UNARY_METHOD(NSDatePickerCell, initTextCell, 0)
2937330f729Sjoerg 
2947330f729Sjoerg   NEW_RECEIVER(NSSliderCell)
2957330f729Sjoerg   ADD_UNARY_METHOD(NSSliderCell, setTitle, 0)
2967330f729Sjoerg 
2977330f729Sjoerg   NEW_RECEIVER(NSControl)
2987330f729Sjoerg   ADD_UNARY_METHOD(NSControl, setStringValue, 0)
2997330f729Sjoerg 
3007330f729Sjoerg   NEW_RECEIVER(NSAccessibility)
3017330f729Sjoerg   ADD_UNARY_METHOD(NSAccessibility, setAccessibilityValueDescription, 0)
3027330f729Sjoerg   ADD_UNARY_METHOD(NSAccessibility, setAccessibilityLabel, 0)
3037330f729Sjoerg   ADD_UNARY_METHOD(NSAccessibility, setAccessibilityTitle, 0)
3047330f729Sjoerg   ADD_UNARY_METHOD(NSAccessibility, setAccessibilityPlaceholderValue, 0)
3057330f729Sjoerg   ADD_UNARY_METHOD(NSAccessibility, setAccessibilityHelp, 0)
3067330f729Sjoerg 
3077330f729Sjoerg   NEW_RECEIVER(NSMatrix)
3087330f729Sjoerg   IdentifierInfo *setToolTipNSMatrix[] = {&Ctx.Idents.get("setToolTip"),
3097330f729Sjoerg                                           &Ctx.Idents.get("forCell")};
3107330f729Sjoerg   ADD_METHOD(NSMatrix, setToolTipNSMatrix, 2, 0)
3117330f729Sjoerg 
3127330f729Sjoerg   NEW_RECEIVER(NSPrintPanel)
3137330f729Sjoerg   ADD_UNARY_METHOD(NSPrintPanel, setDefaultButtonTitle, 0)
3147330f729Sjoerg 
3157330f729Sjoerg   NEW_RECEIVER(UILocalNotification)
3167330f729Sjoerg   ADD_UNARY_METHOD(UILocalNotification, setAlertBody, 0)
3177330f729Sjoerg   ADD_UNARY_METHOD(UILocalNotification, setAlertAction, 0)
3187330f729Sjoerg   ADD_UNARY_METHOD(UILocalNotification, setAlertTitle, 0)
3197330f729Sjoerg 
3207330f729Sjoerg   NEW_RECEIVER(NSSlider)
3217330f729Sjoerg   ADD_UNARY_METHOD(NSSlider, setTitle, 0)
3227330f729Sjoerg 
3237330f729Sjoerg   NEW_RECEIVER(UIMenuItem)
3247330f729Sjoerg   IdentifierInfo *initWithTitleUIMenuItem[] = {&Ctx.Idents.get("initWithTitle"),
3257330f729Sjoerg                                                &Ctx.Idents.get("action")};
3267330f729Sjoerg   ADD_METHOD(UIMenuItem, initWithTitleUIMenuItem, 2, 0)
3277330f729Sjoerg   ADD_UNARY_METHOD(UIMenuItem, setTitle, 0)
3287330f729Sjoerg 
3297330f729Sjoerg   NEW_RECEIVER(UIAlertController)
3307330f729Sjoerg   IdentifierInfo *alertControllerWithTitleUIAlertController[] = {
3317330f729Sjoerg       &Ctx.Idents.get("alertControllerWithTitle"), &Ctx.Idents.get("message"),
3327330f729Sjoerg       &Ctx.Idents.get("preferredStyle")};
3337330f729Sjoerg   ADD_METHOD(UIAlertController, alertControllerWithTitleUIAlertController, 3, 1)
3347330f729Sjoerg   ADD_UNARY_METHOD(UIAlertController, setTitle, 0)
3357330f729Sjoerg   ADD_UNARY_METHOD(UIAlertController, setMessage, 0)
3367330f729Sjoerg 
3377330f729Sjoerg   NEW_RECEIVER(UIApplicationShortcutItem)
3387330f729Sjoerg   IdentifierInfo *initWithTypeUIApplicationShortcutItemIcon[] = {
3397330f729Sjoerg       &Ctx.Idents.get("initWithType"), &Ctx.Idents.get("localizedTitle"),
3407330f729Sjoerg       &Ctx.Idents.get("localizedSubtitle"), &Ctx.Idents.get("icon"),
3417330f729Sjoerg       &Ctx.Idents.get("userInfo")};
3427330f729Sjoerg   ADD_METHOD(UIApplicationShortcutItem,
3437330f729Sjoerg              initWithTypeUIApplicationShortcutItemIcon, 5, 1)
3447330f729Sjoerg   IdentifierInfo *initWithTypeUIApplicationShortcutItem[] = {
3457330f729Sjoerg       &Ctx.Idents.get("initWithType"), &Ctx.Idents.get("localizedTitle")};
3467330f729Sjoerg   ADD_METHOD(UIApplicationShortcutItem, initWithTypeUIApplicationShortcutItem,
3477330f729Sjoerg              2, 1)
3487330f729Sjoerg 
3497330f729Sjoerg   NEW_RECEIVER(UIActionSheet)
3507330f729Sjoerg   IdentifierInfo *initWithTitleUIActionSheet[] = {
3517330f729Sjoerg       &Ctx.Idents.get("initWithTitle"), &Ctx.Idents.get("delegate"),
3527330f729Sjoerg       &Ctx.Idents.get("cancelButtonTitle"),
3537330f729Sjoerg       &Ctx.Idents.get("destructiveButtonTitle"),
3547330f729Sjoerg       &Ctx.Idents.get("otherButtonTitles")};
3557330f729Sjoerg   ADD_METHOD(UIActionSheet, initWithTitleUIActionSheet, 5, 0)
3567330f729Sjoerg   ADD_UNARY_METHOD(UIActionSheet, addButtonWithTitle, 0)
3577330f729Sjoerg   ADD_UNARY_METHOD(UIActionSheet, setTitle, 0)
3587330f729Sjoerg 
3597330f729Sjoerg   NEW_RECEIVER(UIAccessibilityCustomAction)
3607330f729Sjoerg   IdentifierInfo *initWithNameUIAccessibilityCustomAction[] = {
3617330f729Sjoerg       &Ctx.Idents.get("initWithName"), &Ctx.Idents.get("target"),
3627330f729Sjoerg       &Ctx.Idents.get("selector")};
3637330f729Sjoerg   ADD_METHOD(UIAccessibilityCustomAction,
3647330f729Sjoerg              initWithNameUIAccessibilityCustomAction, 3, 0)
3657330f729Sjoerg   ADD_UNARY_METHOD(UIAccessibilityCustomAction, setName, 0)
3667330f729Sjoerg 
3677330f729Sjoerg   NEW_RECEIVER(UISearchBar)
3687330f729Sjoerg   ADD_UNARY_METHOD(UISearchBar, setText, 0)
3697330f729Sjoerg   ADD_UNARY_METHOD(UISearchBar, setPrompt, 0)
3707330f729Sjoerg   ADD_UNARY_METHOD(UISearchBar, setPlaceholder, 0)
3717330f729Sjoerg 
3727330f729Sjoerg   NEW_RECEIVER(UIBarItem)
3737330f729Sjoerg   ADD_UNARY_METHOD(UIBarItem, setTitle, 0)
3747330f729Sjoerg 
3757330f729Sjoerg   NEW_RECEIVER(UITextView)
3767330f729Sjoerg   ADD_UNARY_METHOD(UITextView, setText, 0)
3777330f729Sjoerg 
3787330f729Sjoerg   NEW_RECEIVER(NSView)
3797330f729Sjoerg   ADD_UNARY_METHOD(NSView, setToolTip, 0)
3807330f729Sjoerg 
3817330f729Sjoerg   NEW_RECEIVER(NSTextField)
3827330f729Sjoerg   ADD_UNARY_METHOD(NSTextField, setPlaceholderString, 0)
3837330f729Sjoerg   ADD_UNARY_METHOD(NSTextField, textFieldWithString, 0)
3847330f729Sjoerg   ADD_UNARY_METHOD(NSTextField, wrappingLabelWithString, 0)
3857330f729Sjoerg   ADD_UNARY_METHOD(NSTextField, labelWithString, 0)
3867330f729Sjoerg 
3877330f729Sjoerg   NEW_RECEIVER(NSAttributedString)
3887330f729Sjoerg   ADD_UNARY_METHOD(NSAttributedString, initWithString, 0)
3897330f729Sjoerg   IdentifierInfo *initWithStringNSAttributedString[] = {
3907330f729Sjoerg       &Ctx.Idents.get("initWithString"), &Ctx.Idents.get("attributes")};
3917330f729Sjoerg   ADD_METHOD(NSAttributedString, initWithStringNSAttributedString, 2, 0)
3927330f729Sjoerg 
3937330f729Sjoerg   NEW_RECEIVER(NSText)
3947330f729Sjoerg   ADD_UNARY_METHOD(NSText, setString, 0)
3957330f729Sjoerg 
3967330f729Sjoerg   NEW_RECEIVER(UIKeyCommand)
3977330f729Sjoerg   IdentifierInfo *keyCommandWithInputUIKeyCommand[] = {
3987330f729Sjoerg       &Ctx.Idents.get("keyCommandWithInput"), &Ctx.Idents.get("modifierFlags"),
3997330f729Sjoerg       &Ctx.Idents.get("action"), &Ctx.Idents.get("discoverabilityTitle")};
4007330f729Sjoerg   ADD_METHOD(UIKeyCommand, keyCommandWithInputUIKeyCommand, 4, 3)
4017330f729Sjoerg   ADD_UNARY_METHOD(UIKeyCommand, setDiscoverabilityTitle, 0)
4027330f729Sjoerg 
4037330f729Sjoerg   NEW_RECEIVER(UILabel)
4047330f729Sjoerg   ADD_UNARY_METHOD(UILabel, setText, 0)
4057330f729Sjoerg 
4067330f729Sjoerg   NEW_RECEIVER(NSAlert)
4077330f729Sjoerg   IdentifierInfo *alertWithMessageTextNSAlert[] = {
4087330f729Sjoerg       &Ctx.Idents.get("alertWithMessageText"), &Ctx.Idents.get("defaultButton"),
4097330f729Sjoerg       &Ctx.Idents.get("alternateButton"), &Ctx.Idents.get("otherButton"),
4107330f729Sjoerg       &Ctx.Idents.get("informativeTextWithFormat")};
4117330f729Sjoerg   ADD_METHOD(NSAlert, alertWithMessageTextNSAlert, 5, 0)
4127330f729Sjoerg   ADD_UNARY_METHOD(NSAlert, addButtonWithTitle, 0)
4137330f729Sjoerg   ADD_UNARY_METHOD(NSAlert, setMessageText, 0)
4147330f729Sjoerg   ADD_UNARY_METHOD(NSAlert, setInformativeText, 0)
4157330f729Sjoerg   ADD_UNARY_METHOD(NSAlert, setHelpAnchor, 0)
4167330f729Sjoerg 
4177330f729Sjoerg   NEW_RECEIVER(UIMutableApplicationShortcutItem)
4187330f729Sjoerg   ADD_UNARY_METHOD(UIMutableApplicationShortcutItem, setLocalizedTitle, 0)
4197330f729Sjoerg   ADD_UNARY_METHOD(UIMutableApplicationShortcutItem, setLocalizedSubtitle, 0)
4207330f729Sjoerg 
4217330f729Sjoerg   NEW_RECEIVER(UIButton)
4227330f729Sjoerg   IdentifierInfo *setTitleUIButton[] = {&Ctx.Idents.get("setTitle"),
4237330f729Sjoerg                                         &Ctx.Idents.get("forState")};
4247330f729Sjoerg   ADD_METHOD(UIButton, setTitleUIButton, 2, 0)
4257330f729Sjoerg 
4267330f729Sjoerg   NEW_RECEIVER(NSWindow)
4277330f729Sjoerg   ADD_UNARY_METHOD(NSWindow, setTitle, 0)
4287330f729Sjoerg   IdentifierInfo *minFrameWidthWithTitleNSWindow[] = {
4297330f729Sjoerg       &Ctx.Idents.get("minFrameWidthWithTitle"), &Ctx.Idents.get("styleMask")};
4307330f729Sjoerg   ADD_METHOD(NSWindow, minFrameWidthWithTitleNSWindow, 2, 0)
4317330f729Sjoerg   ADD_UNARY_METHOD(NSWindow, setMiniwindowTitle, 0)
4327330f729Sjoerg 
4337330f729Sjoerg   NEW_RECEIVER(NSPathCell)
4347330f729Sjoerg   ADD_UNARY_METHOD(NSPathCell, setPlaceholderString, 0)
4357330f729Sjoerg 
4367330f729Sjoerg   NEW_RECEIVER(UIDocumentMenuViewController)
4377330f729Sjoerg   IdentifierInfo *addOptionWithTitleUIDocumentMenuViewController[] = {
4387330f729Sjoerg       &Ctx.Idents.get("addOptionWithTitle"), &Ctx.Idents.get("image"),
4397330f729Sjoerg       &Ctx.Idents.get("order"), &Ctx.Idents.get("handler")};
4407330f729Sjoerg   ADD_METHOD(UIDocumentMenuViewController,
4417330f729Sjoerg              addOptionWithTitleUIDocumentMenuViewController, 4, 0)
4427330f729Sjoerg 
4437330f729Sjoerg   NEW_RECEIVER(UINavigationItem)
4447330f729Sjoerg   ADD_UNARY_METHOD(UINavigationItem, initWithTitle, 0)
4457330f729Sjoerg   ADD_UNARY_METHOD(UINavigationItem, setTitle, 0)
4467330f729Sjoerg   ADD_UNARY_METHOD(UINavigationItem, setPrompt, 0)
4477330f729Sjoerg 
4487330f729Sjoerg   NEW_RECEIVER(UIAlertView)
4497330f729Sjoerg   IdentifierInfo *initWithTitleUIAlertView[] = {
4507330f729Sjoerg       &Ctx.Idents.get("initWithTitle"), &Ctx.Idents.get("message"),
4517330f729Sjoerg       &Ctx.Idents.get("delegate"), &Ctx.Idents.get("cancelButtonTitle"),
4527330f729Sjoerg       &Ctx.Idents.get("otherButtonTitles")};
4537330f729Sjoerg   ADD_METHOD(UIAlertView, initWithTitleUIAlertView, 5, 0)
4547330f729Sjoerg   ADD_UNARY_METHOD(UIAlertView, addButtonWithTitle, 0)
4557330f729Sjoerg   ADD_UNARY_METHOD(UIAlertView, setTitle, 0)
4567330f729Sjoerg   ADD_UNARY_METHOD(UIAlertView, setMessage, 0)
4577330f729Sjoerg 
4587330f729Sjoerg   NEW_RECEIVER(NSFormCell)
4597330f729Sjoerg   ADD_UNARY_METHOD(NSFormCell, initTextCell, 0)
4607330f729Sjoerg   ADD_UNARY_METHOD(NSFormCell, setTitle, 0)
4617330f729Sjoerg   ADD_UNARY_METHOD(NSFormCell, setPlaceholderString, 0)
4627330f729Sjoerg 
4637330f729Sjoerg   NEW_RECEIVER(NSUserNotification)
4647330f729Sjoerg   ADD_UNARY_METHOD(NSUserNotification, setTitle, 0)
4657330f729Sjoerg   ADD_UNARY_METHOD(NSUserNotification, setSubtitle, 0)
4667330f729Sjoerg   ADD_UNARY_METHOD(NSUserNotification, setInformativeText, 0)
4677330f729Sjoerg   ADD_UNARY_METHOD(NSUserNotification, setActionButtonTitle, 0)
4687330f729Sjoerg   ADD_UNARY_METHOD(NSUserNotification, setOtherButtonTitle, 0)
4697330f729Sjoerg   ADD_UNARY_METHOD(NSUserNotification, setResponsePlaceholder, 0)
4707330f729Sjoerg 
4717330f729Sjoerg   NEW_RECEIVER(NSToolbarItem)
4727330f729Sjoerg   ADD_UNARY_METHOD(NSToolbarItem, setLabel, 0)
4737330f729Sjoerg   ADD_UNARY_METHOD(NSToolbarItem, setPaletteLabel, 0)
4747330f729Sjoerg   ADD_UNARY_METHOD(NSToolbarItem, setToolTip, 0)
4757330f729Sjoerg 
4767330f729Sjoerg   NEW_RECEIVER(NSProgress)
4777330f729Sjoerg   ADD_UNARY_METHOD(NSProgress, setLocalizedDescription, 0)
4787330f729Sjoerg   ADD_UNARY_METHOD(NSProgress, setLocalizedAdditionalDescription, 0)
4797330f729Sjoerg 
4807330f729Sjoerg   NEW_RECEIVER(NSSegmentedCell)
4817330f729Sjoerg   IdentifierInfo *setLabelNSSegmentedCell[] = {&Ctx.Idents.get("setLabel"),
4827330f729Sjoerg                                                &Ctx.Idents.get("forSegment")};
4837330f729Sjoerg   ADD_METHOD(NSSegmentedCell, setLabelNSSegmentedCell, 2, 0)
4847330f729Sjoerg   IdentifierInfo *setToolTipNSSegmentedCell[] = {&Ctx.Idents.get("setToolTip"),
4857330f729Sjoerg                                                  &Ctx.Idents.get("forSegment")};
4867330f729Sjoerg   ADD_METHOD(NSSegmentedCell, setToolTipNSSegmentedCell, 2, 0)
4877330f729Sjoerg 
4887330f729Sjoerg   NEW_RECEIVER(NSUndoManager)
4897330f729Sjoerg   ADD_UNARY_METHOD(NSUndoManager, setActionName, 0)
4907330f729Sjoerg   ADD_UNARY_METHOD(NSUndoManager, undoMenuTitleForUndoActionName, 0)
4917330f729Sjoerg   ADD_UNARY_METHOD(NSUndoManager, redoMenuTitleForUndoActionName, 0)
4927330f729Sjoerg 
4937330f729Sjoerg   NEW_RECEIVER(NSMenuItem)
4947330f729Sjoerg   IdentifierInfo *initWithTitleNSMenuItem[] = {
4957330f729Sjoerg       &Ctx.Idents.get("initWithTitle"), &Ctx.Idents.get("action"),
4967330f729Sjoerg       &Ctx.Idents.get("keyEquivalent")};
4977330f729Sjoerg   ADD_METHOD(NSMenuItem, initWithTitleNSMenuItem, 3, 0)
4987330f729Sjoerg   ADD_UNARY_METHOD(NSMenuItem, setTitle, 0)
4997330f729Sjoerg   ADD_UNARY_METHOD(NSMenuItem, setToolTip, 0)
5007330f729Sjoerg 
5017330f729Sjoerg   NEW_RECEIVER(NSPopUpButtonCell)
5027330f729Sjoerg   IdentifierInfo *initTextCellNSPopUpButtonCell[] = {
5037330f729Sjoerg       &Ctx.Idents.get("initTextCell"), &Ctx.Idents.get("pullsDown")};
5047330f729Sjoerg   ADD_METHOD(NSPopUpButtonCell, initTextCellNSPopUpButtonCell, 2, 0)
5057330f729Sjoerg   ADD_UNARY_METHOD(NSPopUpButtonCell, addItemWithTitle, 0)
5067330f729Sjoerg   IdentifierInfo *insertItemWithTitleNSPopUpButtonCell[] = {
5077330f729Sjoerg       &Ctx.Idents.get("insertItemWithTitle"), &Ctx.Idents.get("atIndex")};
5087330f729Sjoerg   ADD_METHOD(NSPopUpButtonCell, insertItemWithTitleNSPopUpButtonCell, 2, 0)
5097330f729Sjoerg   ADD_UNARY_METHOD(NSPopUpButtonCell, removeItemWithTitle, 0)
5107330f729Sjoerg   ADD_UNARY_METHOD(NSPopUpButtonCell, selectItemWithTitle, 0)
5117330f729Sjoerg   ADD_UNARY_METHOD(NSPopUpButtonCell, setTitle, 0)
5127330f729Sjoerg 
5137330f729Sjoerg   NEW_RECEIVER(NSViewController)
5147330f729Sjoerg   ADD_UNARY_METHOD(NSViewController, setTitle, 0)
5157330f729Sjoerg 
5167330f729Sjoerg   NEW_RECEIVER(NSMenu)
5177330f729Sjoerg   ADD_UNARY_METHOD(NSMenu, initWithTitle, 0)
5187330f729Sjoerg   IdentifierInfo *insertItemWithTitleNSMenu[] = {
5197330f729Sjoerg       &Ctx.Idents.get("insertItemWithTitle"), &Ctx.Idents.get("action"),
5207330f729Sjoerg       &Ctx.Idents.get("keyEquivalent"), &Ctx.Idents.get("atIndex")};
5217330f729Sjoerg   ADD_METHOD(NSMenu, insertItemWithTitleNSMenu, 4, 0)
5227330f729Sjoerg   IdentifierInfo *addItemWithTitleNSMenu[] = {
5237330f729Sjoerg       &Ctx.Idents.get("addItemWithTitle"), &Ctx.Idents.get("action"),
5247330f729Sjoerg       &Ctx.Idents.get("keyEquivalent")};
5257330f729Sjoerg   ADD_METHOD(NSMenu, addItemWithTitleNSMenu, 3, 0)
5267330f729Sjoerg   ADD_UNARY_METHOD(NSMenu, setTitle, 0)
5277330f729Sjoerg 
5287330f729Sjoerg   NEW_RECEIVER(UIMutableUserNotificationAction)
5297330f729Sjoerg   ADD_UNARY_METHOD(UIMutableUserNotificationAction, setTitle, 0)
5307330f729Sjoerg 
5317330f729Sjoerg   NEW_RECEIVER(NSForm)
5327330f729Sjoerg   ADD_UNARY_METHOD(NSForm, addEntry, 0)
5337330f729Sjoerg   IdentifierInfo *insertEntryNSForm[] = {&Ctx.Idents.get("insertEntry"),
5347330f729Sjoerg                                          &Ctx.Idents.get("atIndex")};
5357330f729Sjoerg   ADD_METHOD(NSForm, insertEntryNSForm, 2, 0)
5367330f729Sjoerg 
5377330f729Sjoerg   NEW_RECEIVER(NSTextFieldCell)
5387330f729Sjoerg   ADD_UNARY_METHOD(NSTextFieldCell, setPlaceholderString, 0)
5397330f729Sjoerg 
5407330f729Sjoerg   NEW_RECEIVER(NSUserNotificationAction)
5417330f729Sjoerg   IdentifierInfo *actionWithIdentifierNSUserNotificationAction[] = {
5427330f729Sjoerg       &Ctx.Idents.get("actionWithIdentifier"), &Ctx.Idents.get("title")};
5437330f729Sjoerg   ADD_METHOD(NSUserNotificationAction,
5447330f729Sjoerg              actionWithIdentifierNSUserNotificationAction, 2, 1)
5457330f729Sjoerg 
5467330f729Sjoerg   NEW_RECEIVER(UITextField)
5477330f729Sjoerg   ADD_UNARY_METHOD(UITextField, setText, 0)
5487330f729Sjoerg   ADD_UNARY_METHOD(UITextField, setPlaceholder, 0)
5497330f729Sjoerg 
5507330f729Sjoerg   NEW_RECEIVER(UIBarButtonItem)
5517330f729Sjoerg   IdentifierInfo *initWithTitleUIBarButtonItem[] = {
5527330f729Sjoerg       &Ctx.Idents.get("initWithTitle"), &Ctx.Idents.get("style"),
5537330f729Sjoerg       &Ctx.Idents.get("target"), &Ctx.Idents.get("action")};
5547330f729Sjoerg   ADD_METHOD(UIBarButtonItem, initWithTitleUIBarButtonItem, 4, 0)
5557330f729Sjoerg 
5567330f729Sjoerg   NEW_RECEIVER(UIViewController)
5577330f729Sjoerg   ADD_UNARY_METHOD(UIViewController, setTitle, 0)
5587330f729Sjoerg 
5597330f729Sjoerg   NEW_RECEIVER(UISegmentedControl)
5607330f729Sjoerg   IdentifierInfo *insertSegmentWithTitleUISegmentedControl[] = {
5617330f729Sjoerg       &Ctx.Idents.get("insertSegmentWithTitle"), &Ctx.Idents.get("atIndex"),
5627330f729Sjoerg       &Ctx.Idents.get("animated")};
5637330f729Sjoerg   ADD_METHOD(UISegmentedControl, insertSegmentWithTitleUISegmentedControl, 3, 0)
5647330f729Sjoerg   IdentifierInfo *setTitleUISegmentedControl[] = {
5657330f729Sjoerg       &Ctx.Idents.get("setTitle"), &Ctx.Idents.get("forSegmentAtIndex")};
5667330f729Sjoerg   ADD_METHOD(UISegmentedControl, setTitleUISegmentedControl, 2, 0)
5677330f729Sjoerg 
5687330f729Sjoerg   NEW_RECEIVER(NSAccessibilityCustomRotorItemResult)
5697330f729Sjoerg   IdentifierInfo
5707330f729Sjoerg       *initWithItemLoadingTokenNSAccessibilityCustomRotorItemResult[] = {
5717330f729Sjoerg           &Ctx.Idents.get("initWithItemLoadingToken"),
5727330f729Sjoerg           &Ctx.Idents.get("customLabel")};
5737330f729Sjoerg   ADD_METHOD(NSAccessibilityCustomRotorItemResult,
5747330f729Sjoerg              initWithItemLoadingTokenNSAccessibilityCustomRotorItemResult, 2, 1)
5757330f729Sjoerg   ADD_UNARY_METHOD(NSAccessibilityCustomRotorItemResult, setCustomLabel, 0)
5767330f729Sjoerg 
5777330f729Sjoerg   NEW_RECEIVER(UIContextualAction)
5787330f729Sjoerg   IdentifierInfo *contextualActionWithStyleUIContextualAction[] = {
5797330f729Sjoerg       &Ctx.Idents.get("contextualActionWithStyle"), &Ctx.Idents.get("title"),
5807330f729Sjoerg       &Ctx.Idents.get("handler")};
5817330f729Sjoerg   ADD_METHOD(UIContextualAction, contextualActionWithStyleUIContextualAction, 3,
5827330f729Sjoerg              1)
5837330f729Sjoerg   ADD_UNARY_METHOD(UIContextualAction, setTitle, 0)
5847330f729Sjoerg 
5857330f729Sjoerg   NEW_RECEIVER(NSAccessibilityCustomRotor)
5867330f729Sjoerg   IdentifierInfo *initWithLabelNSAccessibilityCustomRotor[] = {
5877330f729Sjoerg       &Ctx.Idents.get("initWithLabel"), &Ctx.Idents.get("itemSearchDelegate")};
5887330f729Sjoerg   ADD_METHOD(NSAccessibilityCustomRotor,
5897330f729Sjoerg              initWithLabelNSAccessibilityCustomRotor, 2, 0)
5907330f729Sjoerg   ADD_UNARY_METHOD(NSAccessibilityCustomRotor, setLabel, 0)
5917330f729Sjoerg 
5927330f729Sjoerg   NEW_RECEIVER(NSWindowTab)
5937330f729Sjoerg   ADD_UNARY_METHOD(NSWindowTab, setTitle, 0)
5947330f729Sjoerg   ADD_UNARY_METHOD(NSWindowTab, setToolTip, 0)
5957330f729Sjoerg 
5967330f729Sjoerg   NEW_RECEIVER(NSAccessibilityCustomAction)
5977330f729Sjoerg   IdentifierInfo *initWithNameNSAccessibilityCustomAction[] = {
5987330f729Sjoerg       &Ctx.Idents.get("initWithName"), &Ctx.Idents.get("handler")};
5997330f729Sjoerg   ADD_METHOD(NSAccessibilityCustomAction,
6007330f729Sjoerg              initWithNameNSAccessibilityCustomAction, 2, 0)
6017330f729Sjoerg   IdentifierInfo *initWithNameTargetNSAccessibilityCustomAction[] = {
6027330f729Sjoerg       &Ctx.Idents.get("initWithName"), &Ctx.Idents.get("target"),
6037330f729Sjoerg       &Ctx.Idents.get("selector")};
6047330f729Sjoerg   ADD_METHOD(NSAccessibilityCustomAction,
6057330f729Sjoerg              initWithNameTargetNSAccessibilityCustomAction, 3, 0)
6067330f729Sjoerg   ADD_UNARY_METHOD(NSAccessibilityCustomAction, setName, 0)
6077330f729Sjoerg }
6087330f729Sjoerg 
6097330f729Sjoerg #define LSF_INSERT(function_name) LSF.insert(&Ctx.Idents.get(function_name));
6107330f729Sjoerg #define LSM_INSERT_NULLARY(receiver, method_name)                              \
6117330f729Sjoerg   LSM.insert({&Ctx.Idents.get(receiver), Ctx.Selectors.getNullarySelector(     \
6127330f729Sjoerg                                              &Ctx.Idents.get(method_name))});
6137330f729Sjoerg #define LSM_INSERT_UNARY(receiver, method_name)                                \
6147330f729Sjoerg   LSM.insert({&Ctx.Idents.get(receiver),                                       \
6157330f729Sjoerg               Ctx.Selectors.getUnarySelector(&Ctx.Idents.get(method_name))});
6167330f729Sjoerg #define LSM_INSERT_SELECTOR(receiver, method_list, arguments)                  \
6177330f729Sjoerg   LSM.insert({&Ctx.Idents.get(receiver),                                       \
6187330f729Sjoerg               Ctx.Selectors.getSelector(arguments, method_list)});
6197330f729Sjoerg 
6207330f729Sjoerg /// Initializes a list of methods and C functions that return a localized string
initLocStringsMethods(ASTContext & Ctx) const6217330f729Sjoerg void NonLocalizedStringChecker::initLocStringsMethods(ASTContext &Ctx) const {
6227330f729Sjoerg   if (!LSM.empty())
6237330f729Sjoerg     return;
6247330f729Sjoerg 
6257330f729Sjoerg   IdentifierInfo *LocalizedStringMacro[] = {
6267330f729Sjoerg       &Ctx.Idents.get("localizedStringForKey"), &Ctx.Idents.get("value"),
6277330f729Sjoerg       &Ctx.Idents.get("table")};
6287330f729Sjoerg   LSM_INSERT_SELECTOR("NSBundle", LocalizedStringMacro, 3)
6297330f729Sjoerg   LSM_INSERT_UNARY("NSDateFormatter", "stringFromDate")
6307330f729Sjoerg   IdentifierInfo *LocalizedStringFromDate[] = {
6317330f729Sjoerg       &Ctx.Idents.get("localizedStringFromDate"), &Ctx.Idents.get("dateStyle"),
6327330f729Sjoerg       &Ctx.Idents.get("timeStyle")};
6337330f729Sjoerg   LSM_INSERT_SELECTOR("NSDateFormatter", LocalizedStringFromDate, 3)
6347330f729Sjoerg   LSM_INSERT_UNARY("NSNumberFormatter", "stringFromNumber")
6357330f729Sjoerg   LSM_INSERT_NULLARY("UITextField", "text")
6367330f729Sjoerg   LSM_INSERT_NULLARY("UITextView", "text")
6377330f729Sjoerg   LSM_INSERT_NULLARY("UILabel", "text")
6387330f729Sjoerg 
6397330f729Sjoerg   LSF_INSERT("CFDateFormatterCreateStringWithDate");
6407330f729Sjoerg   LSF_INSERT("CFDateFormatterCreateStringWithAbsoluteTime");
6417330f729Sjoerg   LSF_INSERT("CFNumberFormatterCreateStringWithNumber");
6427330f729Sjoerg }
6437330f729Sjoerg 
6447330f729Sjoerg /// Checks to see if the method / function declaration includes
6457330f729Sjoerg /// __attribute__((annotate("returns_localized_nsstring")))
isAnnotatedAsReturningLocalized(const Decl * D) const6467330f729Sjoerg bool NonLocalizedStringChecker::isAnnotatedAsReturningLocalized(
6477330f729Sjoerg     const Decl *D) const {
6487330f729Sjoerg   if (!D)
6497330f729Sjoerg     return false;
6507330f729Sjoerg   return std::any_of(
6517330f729Sjoerg       D->specific_attr_begin<AnnotateAttr>(),
6527330f729Sjoerg       D->specific_attr_end<AnnotateAttr>(), [](const AnnotateAttr *Ann) {
6537330f729Sjoerg         return Ann->getAnnotation() == "returns_localized_nsstring";
6547330f729Sjoerg       });
6557330f729Sjoerg }
6567330f729Sjoerg 
6577330f729Sjoerg /// Checks to see if the method / function declaration includes
6587330f729Sjoerg /// __attribute__((annotate("takes_localized_nsstring")))
isAnnotatedAsTakingLocalized(const Decl * D) const6597330f729Sjoerg bool NonLocalizedStringChecker::isAnnotatedAsTakingLocalized(
6607330f729Sjoerg     const Decl *D) const {
6617330f729Sjoerg   if (!D)
6627330f729Sjoerg     return false;
6637330f729Sjoerg   return std::any_of(
6647330f729Sjoerg       D->specific_attr_begin<AnnotateAttr>(),
6657330f729Sjoerg       D->specific_attr_end<AnnotateAttr>(), [](const AnnotateAttr *Ann) {
6667330f729Sjoerg         return Ann->getAnnotation() == "takes_localized_nsstring";
6677330f729Sjoerg       });
6687330f729Sjoerg }
6697330f729Sjoerg 
6707330f729Sjoerg /// Returns true if the given SVal is marked as Localized in the program state
hasLocalizedState(SVal S,CheckerContext & C) const6717330f729Sjoerg bool NonLocalizedStringChecker::hasLocalizedState(SVal S,
6727330f729Sjoerg                                                   CheckerContext &C) const {
6737330f729Sjoerg   const MemRegion *mt = S.getAsRegion();
6747330f729Sjoerg   if (mt) {
6757330f729Sjoerg     const LocalizedState *LS = C.getState()->get<LocalizedMemMap>(mt);
6767330f729Sjoerg     if (LS && LS->isLocalized())
6777330f729Sjoerg       return true;
6787330f729Sjoerg   }
6797330f729Sjoerg   return false;
6807330f729Sjoerg }
6817330f729Sjoerg 
6827330f729Sjoerg /// Returns true if the given SVal is marked as NonLocalized in the program
6837330f729Sjoerg /// state
hasNonLocalizedState(SVal S,CheckerContext & C) const6847330f729Sjoerg bool NonLocalizedStringChecker::hasNonLocalizedState(SVal S,
6857330f729Sjoerg                                                      CheckerContext &C) const {
6867330f729Sjoerg   const MemRegion *mt = S.getAsRegion();
6877330f729Sjoerg   if (mt) {
6887330f729Sjoerg     const LocalizedState *LS = C.getState()->get<LocalizedMemMap>(mt);
6897330f729Sjoerg     if (LS && LS->isNonLocalized())
6907330f729Sjoerg       return true;
6917330f729Sjoerg   }
6927330f729Sjoerg   return false;
6937330f729Sjoerg }
6947330f729Sjoerg 
6957330f729Sjoerg /// Marks the given SVal as Localized in the program state
setLocalizedState(const SVal S,CheckerContext & C) const6967330f729Sjoerg void NonLocalizedStringChecker::setLocalizedState(const SVal S,
6977330f729Sjoerg                                                   CheckerContext &C) const {
6987330f729Sjoerg   const MemRegion *mt = S.getAsRegion();
6997330f729Sjoerg   if (mt) {
7007330f729Sjoerg     ProgramStateRef State =
7017330f729Sjoerg         C.getState()->set<LocalizedMemMap>(mt, LocalizedState::getLocalized());
7027330f729Sjoerg     C.addTransition(State);
7037330f729Sjoerg   }
7047330f729Sjoerg }
7057330f729Sjoerg 
7067330f729Sjoerg /// Marks the given SVal as NonLocalized in the program state
setNonLocalizedState(const SVal S,CheckerContext & C) const7077330f729Sjoerg void NonLocalizedStringChecker::setNonLocalizedState(const SVal S,
7087330f729Sjoerg                                                      CheckerContext &C) const {
7097330f729Sjoerg   const MemRegion *mt = S.getAsRegion();
7107330f729Sjoerg   if (mt) {
7117330f729Sjoerg     ProgramStateRef State = C.getState()->set<LocalizedMemMap>(
7127330f729Sjoerg         mt, LocalizedState::getNonLocalized());
7137330f729Sjoerg     C.addTransition(State);
7147330f729Sjoerg   }
7157330f729Sjoerg }
7167330f729Sjoerg 
7177330f729Sjoerg 
isDebuggingName(std::string name)7187330f729Sjoerg static bool isDebuggingName(std::string name) {
7197330f729Sjoerg   return StringRef(name).lower().find("debug") != StringRef::npos;
7207330f729Sjoerg }
7217330f729Sjoerg 
7227330f729Sjoerg /// Returns true when, heuristically, the analyzer may be analyzing debugging
7237330f729Sjoerg /// code. We use this to suppress localization diagnostics in un-localized user
7247330f729Sjoerg /// interfaces that are only used for debugging and are therefore not user
7257330f729Sjoerg /// facing.
isDebuggingContext(CheckerContext & C)7267330f729Sjoerg static bool isDebuggingContext(CheckerContext &C) {
7277330f729Sjoerg   const Decl *D = C.getCurrentAnalysisDeclContext()->getDecl();
7287330f729Sjoerg   if (!D)
7297330f729Sjoerg     return false;
7307330f729Sjoerg 
7317330f729Sjoerg   if (auto *ND = dyn_cast<NamedDecl>(D)) {
7327330f729Sjoerg     if (isDebuggingName(ND->getNameAsString()))
7337330f729Sjoerg       return true;
7347330f729Sjoerg   }
7357330f729Sjoerg 
7367330f729Sjoerg   const DeclContext *DC = D->getDeclContext();
7377330f729Sjoerg 
7387330f729Sjoerg   if (auto *CD = dyn_cast<ObjCContainerDecl>(DC)) {
7397330f729Sjoerg     if (isDebuggingName(CD->getNameAsString()))
7407330f729Sjoerg       return true;
7417330f729Sjoerg   }
7427330f729Sjoerg 
7437330f729Sjoerg   return false;
7447330f729Sjoerg }
7457330f729Sjoerg 
7467330f729Sjoerg 
7477330f729Sjoerg /// Reports a localization error for the passed in method call and SVal
reportLocalizationError(SVal S,const CallEvent & M,CheckerContext & C,int argumentNumber) const7487330f729Sjoerg void NonLocalizedStringChecker::reportLocalizationError(
7497330f729Sjoerg     SVal S, const CallEvent &M, CheckerContext &C, int argumentNumber) const {
7507330f729Sjoerg 
7517330f729Sjoerg   // Don't warn about localization errors in classes and methods that
7527330f729Sjoerg   // may be debug code.
7537330f729Sjoerg   if (isDebuggingContext(C))
7547330f729Sjoerg     return;
7557330f729Sjoerg 
7567330f729Sjoerg   static CheckerProgramPointTag Tag("NonLocalizedStringChecker",
7577330f729Sjoerg                                     "UnlocalizedString");
7587330f729Sjoerg   ExplodedNode *ErrNode = C.addTransition(C.getState(), C.getPredecessor(), &Tag);
7597330f729Sjoerg 
7607330f729Sjoerg   if (!ErrNode)
7617330f729Sjoerg     return;
7627330f729Sjoerg 
7637330f729Sjoerg   // Generate the bug report.
7647330f729Sjoerg   auto R = std::make_unique<PathSensitiveBugReport>(
7657330f729Sjoerg       *BT, "User-facing text should use localized string macro", ErrNode);
7667330f729Sjoerg   if (argumentNumber) {
7677330f729Sjoerg     R->addRange(M.getArgExpr(argumentNumber - 1)->getSourceRange());
7687330f729Sjoerg   } else {
7697330f729Sjoerg     R->addRange(M.getSourceRange());
7707330f729Sjoerg   }
7717330f729Sjoerg   R->markInteresting(S);
7727330f729Sjoerg 
7737330f729Sjoerg   const MemRegion *StringRegion = S.getAsRegion();
7747330f729Sjoerg   if (StringRegion)
7757330f729Sjoerg     R->addVisitor(std::make_unique<NonLocalizedStringBRVisitor>(StringRegion));
7767330f729Sjoerg 
7777330f729Sjoerg   C.emitReport(std::move(R));
7787330f729Sjoerg }
7797330f729Sjoerg 
7807330f729Sjoerg /// Returns the argument number requiring localized string if it exists
7817330f729Sjoerg /// otherwise, returns -1
getLocalizedArgumentForSelector(const IdentifierInfo * Receiver,Selector S) const7827330f729Sjoerg int NonLocalizedStringChecker::getLocalizedArgumentForSelector(
7837330f729Sjoerg     const IdentifierInfo *Receiver, Selector S) const {
7847330f729Sjoerg   auto method = UIMethods.find(Receiver);
7857330f729Sjoerg 
7867330f729Sjoerg   if (method == UIMethods.end())
7877330f729Sjoerg     return -1;
7887330f729Sjoerg 
7897330f729Sjoerg   auto argumentIterator = method->getSecond().find(S);
7907330f729Sjoerg 
7917330f729Sjoerg   if (argumentIterator == method->getSecond().end())
7927330f729Sjoerg     return -1;
7937330f729Sjoerg 
7947330f729Sjoerg   int argumentNumber = argumentIterator->getSecond();
7957330f729Sjoerg   return argumentNumber;
7967330f729Sjoerg }
7977330f729Sjoerg 
7987330f729Sjoerg /// Check if the string being passed in has NonLocalized state
checkPreObjCMessage(const ObjCMethodCall & msg,CheckerContext & C) const7997330f729Sjoerg void NonLocalizedStringChecker::checkPreObjCMessage(const ObjCMethodCall &msg,
8007330f729Sjoerg                                                     CheckerContext &C) const {
8017330f729Sjoerg   initUIMethods(C.getASTContext());
8027330f729Sjoerg 
8037330f729Sjoerg   const ObjCInterfaceDecl *OD = msg.getReceiverInterface();
8047330f729Sjoerg   if (!OD)
8057330f729Sjoerg     return;
8067330f729Sjoerg   const IdentifierInfo *odInfo = OD->getIdentifier();
8077330f729Sjoerg 
8087330f729Sjoerg   Selector S = msg.getSelector();
8097330f729Sjoerg 
8107330f729Sjoerg   std::string SelectorString = S.getAsString();
8117330f729Sjoerg   StringRef SelectorName = SelectorString;
8127330f729Sjoerg   assert(!SelectorName.empty());
8137330f729Sjoerg 
8147330f729Sjoerg   if (odInfo->isStr("NSString")) {
8157330f729Sjoerg     // Handle the case where the receiver is an NSString
8167330f729Sjoerg     // These special NSString methods draw to the screen
8177330f729Sjoerg 
8187330f729Sjoerg     if (!(SelectorName.startswith("drawAtPoint") ||
8197330f729Sjoerg           SelectorName.startswith("drawInRect") ||
8207330f729Sjoerg           SelectorName.startswith("drawWithRect")))
8217330f729Sjoerg       return;
8227330f729Sjoerg 
8237330f729Sjoerg     SVal svTitle = msg.getReceiverSVal();
8247330f729Sjoerg 
8257330f729Sjoerg     bool isNonLocalized = hasNonLocalizedState(svTitle, C);
8267330f729Sjoerg 
8277330f729Sjoerg     if (isNonLocalized) {
8287330f729Sjoerg       reportLocalizationError(svTitle, msg, C);
8297330f729Sjoerg     }
8307330f729Sjoerg   }
8317330f729Sjoerg 
8327330f729Sjoerg   int argumentNumber = getLocalizedArgumentForSelector(odInfo, S);
8337330f729Sjoerg   // Go up each hierarchy of superclasses and their protocols
8347330f729Sjoerg   while (argumentNumber < 0 && OD->getSuperClass() != nullptr) {
8357330f729Sjoerg     for (const auto *P : OD->all_referenced_protocols()) {
8367330f729Sjoerg       argumentNumber = getLocalizedArgumentForSelector(P->getIdentifier(), S);
8377330f729Sjoerg       if (argumentNumber >= 0)
8387330f729Sjoerg         break;
8397330f729Sjoerg     }
8407330f729Sjoerg     if (argumentNumber < 0) {
8417330f729Sjoerg       OD = OD->getSuperClass();
8427330f729Sjoerg       argumentNumber = getLocalizedArgumentForSelector(OD->getIdentifier(), S);
8437330f729Sjoerg     }
8447330f729Sjoerg   }
8457330f729Sjoerg 
8467330f729Sjoerg   if (argumentNumber < 0) { // There was no match in UIMethods
8477330f729Sjoerg     if (const Decl *D = msg.getDecl()) {
8487330f729Sjoerg       if (const ObjCMethodDecl *OMD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
8497330f729Sjoerg         auto formals = OMD->parameters();
8507330f729Sjoerg         for (unsigned i = 0, ei = formals.size(); i != ei; ++i) {
8517330f729Sjoerg           if (isAnnotatedAsTakingLocalized(formals[i])) {
8527330f729Sjoerg             argumentNumber = i;
8537330f729Sjoerg             break;
8547330f729Sjoerg           }
8557330f729Sjoerg         }
8567330f729Sjoerg       }
8577330f729Sjoerg     }
8587330f729Sjoerg   }
8597330f729Sjoerg 
8607330f729Sjoerg   if (argumentNumber < 0) // Still no match
8617330f729Sjoerg     return;
8627330f729Sjoerg 
8637330f729Sjoerg   SVal svTitle = msg.getArgSVal(argumentNumber);
8647330f729Sjoerg 
8657330f729Sjoerg   if (const ObjCStringRegion *SR =
8667330f729Sjoerg           dyn_cast_or_null<ObjCStringRegion>(svTitle.getAsRegion())) {
8677330f729Sjoerg     StringRef stringValue =
8687330f729Sjoerg         SR->getObjCStringLiteral()->getString()->getString();
8697330f729Sjoerg     if ((stringValue.trim().size() == 0 && stringValue.size() > 0) ||
8707330f729Sjoerg         stringValue.empty())
8717330f729Sjoerg       return;
8727330f729Sjoerg     if (!IsAggressive && llvm::sys::unicode::columnWidthUTF8(stringValue) < 2)
8737330f729Sjoerg       return;
8747330f729Sjoerg   }
8757330f729Sjoerg 
8767330f729Sjoerg   bool isNonLocalized = hasNonLocalizedState(svTitle, C);
8777330f729Sjoerg 
8787330f729Sjoerg   if (isNonLocalized) {
8797330f729Sjoerg     reportLocalizationError(svTitle, msg, C, argumentNumber + 1);
8807330f729Sjoerg   }
8817330f729Sjoerg }
8827330f729Sjoerg 
checkPreCall(const CallEvent & Call,CheckerContext & C) const8837330f729Sjoerg void NonLocalizedStringChecker::checkPreCall(const CallEvent &Call,
8847330f729Sjoerg                                              CheckerContext &C) const {
8857330f729Sjoerg   const auto *FD = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
8867330f729Sjoerg   if (!FD)
8877330f729Sjoerg     return;
8887330f729Sjoerg 
8897330f729Sjoerg   auto formals = FD->parameters();
8907330f729Sjoerg   for (unsigned i = 0, ei = std::min(static_cast<unsigned>(formals.size()),
8917330f729Sjoerg                                      Call.getNumArgs()); i != ei; ++i) {
8927330f729Sjoerg     if (isAnnotatedAsTakingLocalized(formals[i])) {
8937330f729Sjoerg       auto actual = Call.getArgSVal(i);
8947330f729Sjoerg       if (hasNonLocalizedState(actual, C)) {
8957330f729Sjoerg         reportLocalizationError(actual, Call, C, i + 1);
8967330f729Sjoerg       }
8977330f729Sjoerg     }
8987330f729Sjoerg   }
8997330f729Sjoerg }
9007330f729Sjoerg 
isNSStringType(QualType T,ASTContext & Ctx)9017330f729Sjoerg static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
9027330f729Sjoerg 
9037330f729Sjoerg   const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
9047330f729Sjoerg   if (!PT)
9057330f729Sjoerg     return false;
9067330f729Sjoerg 
9077330f729Sjoerg   ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
9087330f729Sjoerg   if (!Cls)
9097330f729Sjoerg     return false;
9107330f729Sjoerg 
9117330f729Sjoerg   IdentifierInfo *ClsName = Cls->getIdentifier();
9127330f729Sjoerg 
9137330f729Sjoerg   // FIXME: Should we walk the chain of classes?
9147330f729Sjoerg   return ClsName == &Ctx.Idents.get("NSString") ||
9157330f729Sjoerg          ClsName == &Ctx.Idents.get("NSMutableString");
9167330f729Sjoerg }
9177330f729Sjoerg 
9187330f729Sjoerg /// Marks a string being returned by any call as localized
9197330f729Sjoerg /// if it is in LocStringFunctions (LSF) or the function is annotated.
9207330f729Sjoerg /// Otherwise, we mark it as NonLocalized (Aggressive) or
9217330f729Sjoerg /// NonLocalized only if it is not backed by a SymRegion (Non-Aggressive),
9227330f729Sjoerg /// basically leaving only string literals as NonLocalized.
checkPostCall(const CallEvent & Call,CheckerContext & C) const9237330f729Sjoerg void NonLocalizedStringChecker::checkPostCall(const CallEvent &Call,
9247330f729Sjoerg                                               CheckerContext &C) const {
9257330f729Sjoerg   initLocStringsMethods(C.getASTContext());
9267330f729Sjoerg 
9277330f729Sjoerg   if (!Call.getOriginExpr())
9287330f729Sjoerg     return;
9297330f729Sjoerg 
9307330f729Sjoerg   // Anything that takes in a localized NSString as an argument
9317330f729Sjoerg   // and returns an NSString will be assumed to be returning a
9327330f729Sjoerg   // localized NSString. (Counter: Incorrectly combining two LocalizedStrings)
9337330f729Sjoerg   const QualType RT = Call.getResultType();
9347330f729Sjoerg   if (isNSStringType(RT, C.getASTContext())) {
9357330f729Sjoerg     for (unsigned i = 0; i < Call.getNumArgs(); ++i) {
9367330f729Sjoerg       SVal argValue = Call.getArgSVal(i);
9377330f729Sjoerg       if (hasLocalizedState(argValue, C)) {
9387330f729Sjoerg         SVal sv = Call.getReturnValue();
9397330f729Sjoerg         setLocalizedState(sv, C);
9407330f729Sjoerg         return;
9417330f729Sjoerg       }
9427330f729Sjoerg     }
9437330f729Sjoerg   }
9447330f729Sjoerg 
9457330f729Sjoerg   const Decl *D = Call.getDecl();
9467330f729Sjoerg   if (!D)
9477330f729Sjoerg     return;
9487330f729Sjoerg 
9497330f729Sjoerg   const IdentifierInfo *Identifier = Call.getCalleeIdentifier();
9507330f729Sjoerg 
9517330f729Sjoerg   SVal sv = Call.getReturnValue();
9527330f729Sjoerg   if (isAnnotatedAsReturningLocalized(D) || LSF.count(Identifier) != 0) {
9537330f729Sjoerg     setLocalizedState(sv, C);
9547330f729Sjoerg   } else if (isNSStringType(RT, C.getASTContext()) &&
9557330f729Sjoerg              !hasLocalizedState(sv, C)) {
9567330f729Sjoerg     if (IsAggressive) {
9577330f729Sjoerg       setNonLocalizedState(sv, C);
9587330f729Sjoerg     } else {
9597330f729Sjoerg       const SymbolicRegion *SymReg =
9607330f729Sjoerg           dyn_cast_or_null<SymbolicRegion>(sv.getAsRegion());
9617330f729Sjoerg       if (!SymReg)
9627330f729Sjoerg         setNonLocalizedState(sv, C);
9637330f729Sjoerg     }
9647330f729Sjoerg   }
9657330f729Sjoerg }
9667330f729Sjoerg 
9677330f729Sjoerg /// Marks a string being returned by an ObjC method as localized
9687330f729Sjoerg /// if it is in LocStringMethods or the method is annotated
checkPostObjCMessage(const ObjCMethodCall & msg,CheckerContext & C) const9697330f729Sjoerg void NonLocalizedStringChecker::checkPostObjCMessage(const ObjCMethodCall &msg,
9707330f729Sjoerg                                                      CheckerContext &C) const {
9717330f729Sjoerg   initLocStringsMethods(C.getASTContext());
9727330f729Sjoerg 
9737330f729Sjoerg   if (!msg.isInstanceMessage())
9747330f729Sjoerg     return;
9757330f729Sjoerg 
9767330f729Sjoerg   const ObjCInterfaceDecl *OD = msg.getReceiverInterface();
9777330f729Sjoerg   if (!OD)
9787330f729Sjoerg     return;
9797330f729Sjoerg   const IdentifierInfo *odInfo = OD->getIdentifier();
9807330f729Sjoerg 
9817330f729Sjoerg   Selector S = msg.getSelector();
9827330f729Sjoerg   std::string SelectorName = S.getAsString();
9837330f729Sjoerg 
9847330f729Sjoerg   std::pair<const IdentifierInfo *, Selector> MethodDescription = {odInfo, S};
9857330f729Sjoerg 
9867330f729Sjoerg   if (LSM.count(MethodDescription) ||
9877330f729Sjoerg       isAnnotatedAsReturningLocalized(msg.getDecl())) {
9887330f729Sjoerg     SVal sv = msg.getReturnValue();
9897330f729Sjoerg     setLocalizedState(sv, C);
9907330f729Sjoerg   }
9917330f729Sjoerg }
9927330f729Sjoerg 
9937330f729Sjoerg /// Marks all empty string literals as localized
checkPostStmt(const ObjCStringLiteral * SL,CheckerContext & C) const9947330f729Sjoerg void NonLocalizedStringChecker::checkPostStmt(const ObjCStringLiteral *SL,
9957330f729Sjoerg                                               CheckerContext &C) const {
9967330f729Sjoerg   SVal sv = C.getSVal(SL);
9977330f729Sjoerg   setNonLocalizedState(sv, C);
9987330f729Sjoerg }
9997330f729Sjoerg 
10007330f729Sjoerg PathDiagnosticPieceRef
VisitNode(const ExplodedNode * Succ,BugReporterContext & BRC,PathSensitiveBugReport & BR)10017330f729Sjoerg NonLocalizedStringBRVisitor::VisitNode(const ExplodedNode *Succ,
10027330f729Sjoerg                                        BugReporterContext &BRC,
10037330f729Sjoerg                                        PathSensitiveBugReport &BR) {
10047330f729Sjoerg   if (Satisfied)
10057330f729Sjoerg     return nullptr;
10067330f729Sjoerg 
10077330f729Sjoerg   Optional<StmtPoint> Point = Succ->getLocation().getAs<StmtPoint>();
10087330f729Sjoerg   if (!Point.hasValue())
10097330f729Sjoerg     return nullptr;
10107330f729Sjoerg 
10117330f729Sjoerg   auto *LiteralExpr = dyn_cast<ObjCStringLiteral>(Point->getStmt());
10127330f729Sjoerg   if (!LiteralExpr)
10137330f729Sjoerg     return nullptr;
10147330f729Sjoerg 
10157330f729Sjoerg   SVal LiteralSVal = Succ->getSVal(LiteralExpr);
10167330f729Sjoerg   if (LiteralSVal.getAsRegion() != NonLocalizedString)
10177330f729Sjoerg     return nullptr;
10187330f729Sjoerg 
10197330f729Sjoerg   Satisfied = true;
10207330f729Sjoerg 
10217330f729Sjoerg   PathDiagnosticLocation L =
10227330f729Sjoerg       PathDiagnosticLocation::create(*Point, BRC.getSourceManager());
10237330f729Sjoerg 
10247330f729Sjoerg   if (!L.isValid() || !L.asLocation().isValid())
10257330f729Sjoerg     return nullptr;
10267330f729Sjoerg 
10277330f729Sjoerg   auto Piece = std::make_shared<PathDiagnosticEventPiece>(
10287330f729Sjoerg       L, "Non-localized string literal here");
10297330f729Sjoerg   Piece->addRange(LiteralExpr->getSourceRange());
10307330f729Sjoerg 
10317330f729Sjoerg   return std::move(Piece);
10327330f729Sjoerg }
10337330f729Sjoerg 
10347330f729Sjoerg namespace {
10357330f729Sjoerg class EmptyLocalizationContextChecker
10367330f729Sjoerg     : public Checker<check::ASTDecl<ObjCImplementationDecl>> {
10377330f729Sjoerg 
10387330f729Sjoerg   // A helper class, which walks the AST
10397330f729Sjoerg   class MethodCrawler : public ConstStmtVisitor<MethodCrawler> {
10407330f729Sjoerg     const ObjCMethodDecl *MD;
10417330f729Sjoerg     BugReporter &BR;
10427330f729Sjoerg     AnalysisManager &Mgr;
10437330f729Sjoerg     const CheckerBase *Checker;
10447330f729Sjoerg     LocationOrAnalysisDeclContext DCtx;
10457330f729Sjoerg 
10467330f729Sjoerg   public:
MethodCrawler(const ObjCMethodDecl * InMD,BugReporter & InBR,const CheckerBase * Checker,AnalysisManager & InMgr,AnalysisDeclContext * InDCtx)10477330f729Sjoerg     MethodCrawler(const ObjCMethodDecl *InMD, BugReporter &InBR,
10487330f729Sjoerg                   const CheckerBase *Checker, AnalysisManager &InMgr,
10497330f729Sjoerg                   AnalysisDeclContext *InDCtx)
10507330f729Sjoerg         : MD(InMD), BR(InBR), Mgr(InMgr), Checker(Checker), DCtx(InDCtx) {}
10517330f729Sjoerg 
VisitStmt(const Stmt * S)10527330f729Sjoerg     void VisitStmt(const Stmt *S) { VisitChildren(S); }
10537330f729Sjoerg 
10547330f729Sjoerg     void VisitObjCMessageExpr(const ObjCMessageExpr *ME);
10557330f729Sjoerg 
10567330f729Sjoerg     void reportEmptyContextError(const ObjCMessageExpr *M) const;
10577330f729Sjoerg 
VisitChildren(const Stmt * S)10587330f729Sjoerg     void VisitChildren(const Stmt *S) {
10597330f729Sjoerg       for (const Stmt *Child : S->children()) {
10607330f729Sjoerg         if (Child)
10617330f729Sjoerg           this->Visit(Child);
10627330f729Sjoerg       }
10637330f729Sjoerg     }
10647330f729Sjoerg   };
10657330f729Sjoerg 
10667330f729Sjoerg public:
10677330f729Sjoerg   void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager &Mgr,
10687330f729Sjoerg                     BugReporter &BR) const;
10697330f729Sjoerg };
10707330f729Sjoerg } // end anonymous namespace
10717330f729Sjoerg 
checkASTDecl(const ObjCImplementationDecl * D,AnalysisManager & Mgr,BugReporter & BR) const10727330f729Sjoerg void EmptyLocalizationContextChecker::checkASTDecl(
10737330f729Sjoerg     const ObjCImplementationDecl *D, AnalysisManager &Mgr,
10747330f729Sjoerg     BugReporter &BR) const {
10757330f729Sjoerg 
10767330f729Sjoerg   for (const ObjCMethodDecl *M : D->methods()) {
10777330f729Sjoerg     AnalysisDeclContext *DCtx = Mgr.getAnalysisDeclContext(M);
10787330f729Sjoerg 
10797330f729Sjoerg     const Stmt *Body = M->getBody();
1080*e038c9c4Sjoerg     if (!Body) {
1081*e038c9c4Sjoerg       assert(M->isSynthesizedAccessorStub());
1082*e038c9c4Sjoerg       continue;
1083*e038c9c4Sjoerg     }
10847330f729Sjoerg 
10857330f729Sjoerg     MethodCrawler MC(M->getCanonicalDecl(), BR, this, Mgr, DCtx);
10867330f729Sjoerg     MC.VisitStmt(Body);
10877330f729Sjoerg   }
10887330f729Sjoerg }
10897330f729Sjoerg 
10907330f729Sjoerg /// This check attempts to match these macros, assuming they are defined as
10917330f729Sjoerg /// follows:
10927330f729Sjoerg ///
10937330f729Sjoerg /// #define NSLocalizedString(key, comment) \
10947330f729Sjoerg /// [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]
10957330f729Sjoerg /// #define NSLocalizedStringFromTable(key, tbl, comment) \
10967330f729Sjoerg /// [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:(tbl)]
10977330f729Sjoerg /// #define NSLocalizedStringFromTableInBundle(key, tbl, bundle, comment) \
10987330f729Sjoerg /// [bundle localizedStringForKey:(key) value:@"" table:(tbl)]
10997330f729Sjoerg /// #define NSLocalizedStringWithDefaultValue(key, tbl, bundle, val, comment)
11007330f729Sjoerg ///
11017330f729Sjoerg /// We cannot use the path sensitive check because the macro argument we are
11027330f729Sjoerg /// checking for (comment) is not used and thus not present in the AST,
11037330f729Sjoerg /// so we use Lexer on the original macro call and retrieve the value of
11047330f729Sjoerg /// the comment. If it's empty or nil, we raise a warning.
VisitObjCMessageExpr(const ObjCMessageExpr * ME)11057330f729Sjoerg void EmptyLocalizationContextChecker::MethodCrawler::VisitObjCMessageExpr(
11067330f729Sjoerg     const ObjCMessageExpr *ME) {
11077330f729Sjoerg 
11087330f729Sjoerg   // FIXME: We may be able to use PPCallbacks to check for empty context
11097330f729Sjoerg   // comments as part of preprocessing and avoid this re-lexing hack.
11107330f729Sjoerg   const ObjCInterfaceDecl *OD = ME->getReceiverInterface();
11117330f729Sjoerg   if (!OD)
11127330f729Sjoerg     return;
11137330f729Sjoerg 
11147330f729Sjoerg   const IdentifierInfo *odInfo = OD->getIdentifier();
11157330f729Sjoerg 
11167330f729Sjoerg   if (!(odInfo->isStr("NSBundle") &&
11177330f729Sjoerg         ME->getSelector().getAsString() ==
11187330f729Sjoerg             "localizedStringForKey:value:table:")) {
11197330f729Sjoerg     return;
11207330f729Sjoerg   }
11217330f729Sjoerg 
11227330f729Sjoerg   SourceRange R = ME->getSourceRange();
11237330f729Sjoerg   if (!R.getBegin().isMacroID())
11247330f729Sjoerg     return;
11257330f729Sjoerg 
11267330f729Sjoerg   // getImmediateMacroCallerLoc gets the location of the immediate macro
11277330f729Sjoerg   // caller, one level up the stack toward the initial macro typed into the
11287330f729Sjoerg   // source, so SL should point to the NSLocalizedString macro.
11297330f729Sjoerg   SourceLocation SL =
11307330f729Sjoerg       Mgr.getSourceManager().getImmediateMacroCallerLoc(R.getBegin());
11317330f729Sjoerg   std::pair<FileID, unsigned> SLInfo =
11327330f729Sjoerg       Mgr.getSourceManager().getDecomposedLoc(SL);
11337330f729Sjoerg 
11347330f729Sjoerg   SrcMgr::SLocEntry SE = Mgr.getSourceManager().getSLocEntry(SLInfo.first);
11357330f729Sjoerg 
11367330f729Sjoerg   // If NSLocalizedString macro is wrapped in another macro, we need to
11377330f729Sjoerg   // unwrap the expansion until we get to the NSLocalizedStringMacro.
11387330f729Sjoerg   while (SE.isExpansion()) {
11397330f729Sjoerg     SL = SE.getExpansion().getSpellingLoc();
11407330f729Sjoerg     SLInfo = Mgr.getSourceManager().getDecomposedLoc(SL);
11417330f729Sjoerg     SE = Mgr.getSourceManager().getSLocEntry(SLInfo.first);
11427330f729Sjoerg   }
11437330f729Sjoerg 
1144*e038c9c4Sjoerg   llvm::Optional<llvm::MemoryBufferRef> BF =
1145*e038c9c4Sjoerg       Mgr.getSourceManager().getBufferOrNone(SLInfo.first, SL);
1146*e038c9c4Sjoerg   if (!BF)
11477330f729Sjoerg     return;
11487330f729Sjoerg 
11497330f729Sjoerg   Lexer TheLexer(SL, LangOptions(), BF->getBufferStart(),
11507330f729Sjoerg                  BF->getBufferStart() + SLInfo.second, BF->getBufferEnd());
11517330f729Sjoerg 
11527330f729Sjoerg   Token I;
11537330f729Sjoerg   Token Result;    // This will hold the token just before the last ')'
11547330f729Sjoerg   int p_count = 0; // This is for parenthesis matching
11557330f729Sjoerg   while (!TheLexer.LexFromRawLexer(I)) {
11567330f729Sjoerg     if (I.getKind() == tok::l_paren)
11577330f729Sjoerg       ++p_count;
11587330f729Sjoerg     if (I.getKind() == tok::r_paren) {
11597330f729Sjoerg       if (p_count == 1)
11607330f729Sjoerg         break;
11617330f729Sjoerg       --p_count;
11627330f729Sjoerg     }
11637330f729Sjoerg     Result = I;
11647330f729Sjoerg   }
11657330f729Sjoerg 
11667330f729Sjoerg   if (isAnyIdentifier(Result.getKind())) {
11677330f729Sjoerg     if (Result.getRawIdentifier().equals("nil")) {
11687330f729Sjoerg       reportEmptyContextError(ME);
11697330f729Sjoerg       return;
11707330f729Sjoerg     }
11717330f729Sjoerg   }
11727330f729Sjoerg 
11737330f729Sjoerg   if (!isStringLiteral(Result.getKind()))
11747330f729Sjoerg     return;
11757330f729Sjoerg 
11767330f729Sjoerg   StringRef Comment =
11777330f729Sjoerg       StringRef(Result.getLiteralData(), Result.getLength()).trim('"');
11787330f729Sjoerg 
11797330f729Sjoerg   if ((Comment.trim().size() == 0 && Comment.size() > 0) || // Is Whitespace
11807330f729Sjoerg       Comment.empty()) {
11817330f729Sjoerg     reportEmptyContextError(ME);
11827330f729Sjoerg   }
11837330f729Sjoerg }
11847330f729Sjoerg 
reportEmptyContextError(const ObjCMessageExpr * ME) const11857330f729Sjoerg void EmptyLocalizationContextChecker::MethodCrawler::reportEmptyContextError(
11867330f729Sjoerg     const ObjCMessageExpr *ME) const {
11877330f729Sjoerg   // Generate the bug report.
11887330f729Sjoerg   BR.EmitBasicReport(MD, Checker, "Context Missing",
11897330f729Sjoerg                      "Localizability Issue (Apple)",
11907330f729Sjoerg                      "Localized string macro should include a non-empty "
11917330f729Sjoerg                      "comment for translators",
11927330f729Sjoerg                      PathDiagnosticLocation(ME, BR.getSourceManager(), DCtx));
11937330f729Sjoerg }
11947330f729Sjoerg 
11957330f729Sjoerg namespace {
11967330f729Sjoerg class PluralMisuseChecker : public Checker<check::ASTCodeBody> {
11977330f729Sjoerg 
11987330f729Sjoerg   // A helper class, which walks the AST
11997330f729Sjoerg   class MethodCrawler : public RecursiveASTVisitor<MethodCrawler> {
12007330f729Sjoerg     BugReporter &BR;
12017330f729Sjoerg     const CheckerBase *Checker;
12027330f729Sjoerg     AnalysisDeclContext *AC;
12037330f729Sjoerg 
12047330f729Sjoerg     // This functions like a stack. We push on any IfStmt or
12057330f729Sjoerg     // ConditionalOperator that matches the condition
12067330f729Sjoerg     // and pop it off when we leave that statement
12077330f729Sjoerg     llvm::SmallVector<const clang::Stmt *, 8> MatchingStatements;
12087330f729Sjoerg     // This is true when we are the direct-child of a
12097330f729Sjoerg     // matching statement
12107330f729Sjoerg     bool InMatchingStatement = false;
12117330f729Sjoerg 
12127330f729Sjoerg   public:
MethodCrawler(BugReporter & InBR,const CheckerBase * Checker,AnalysisDeclContext * InAC)12137330f729Sjoerg     explicit MethodCrawler(BugReporter &InBR, const CheckerBase *Checker,
12147330f729Sjoerg                            AnalysisDeclContext *InAC)
12157330f729Sjoerg         : BR(InBR), Checker(Checker), AC(InAC) {}
12167330f729Sjoerg 
12177330f729Sjoerg     bool VisitIfStmt(const IfStmt *I);
12187330f729Sjoerg     bool EndVisitIfStmt(IfStmt *I);
12197330f729Sjoerg     bool TraverseIfStmt(IfStmt *x);
12207330f729Sjoerg     bool VisitConditionalOperator(const ConditionalOperator *C);
12217330f729Sjoerg     bool TraverseConditionalOperator(ConditionalOperator *C);
12227330f729Sjoerg     bool VisitCallExpr(const CallExpr *CE);
12237330f729Sjoerg     bool VisitObjCMessageExpr(const ObjCMessageExpr *ME);
12247330f729Sjoerg 
12257330f729Sjoerg   private:
12267330f729Sjoerg     void reportPluralMisuseError(const Stmt *S) const;
12277330f729Sjoerg     bool isCheckingPlurality(const Expr *E) const;
12287330f729Sjoerg   };
12297330f729Sjoerg 
12307330f729Sjoerg public:
checkASTCodeBody(const Decl * D,AnalysisManager & Mgr,BugReporter & BR) const12317330f729Sjoerg   void checkASTCodeBody(const Decl *D, AnalysisManager &Mgr,
12327330f729Sjoerg                         BugReporter &BR) const {
12337330f729Sjoerg     MethodCrawler Visitor(BR, this, Mgr.getAnalysisDeclContext(D));
12347330f729Sjoerg     Visitor.TraverseDecl(const_cast<Decl *>(D));
12357330f729Sjoerg   }
12367330f729Sjoerg };
12377330f729Sjoerg } // end anonymous namespace
12387330f729Sjoerg 
12397330f729Sjoerg // Checks the condition of the IfStmt and returns true if one
12407330f729Sjoerg // of the following heuristics are met:
12417330f729Sjoerg // 1) The conidtion is a variable with "singular" or "plural" in the name
12427330f729Sjoerg // 2) The condition is a binary operator with 1 or 2 on the right-hand side
isCheckingPlurality(const Expr * Condition) const12437330f729Sjoerg bool PluralMisuseChecker::MethodCrawler::isCheckingPlurality(
12447330f729Sjoerg     const Expr *Condition) const {
12457330f729Sjoerg   const BinaryOperator *BO = nullptr;
12467330f729Sjoerg   // Accounts for when a VarDecl represents a BinaryOperator
12477330f729Sjoerg   if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Condition)) {
12487330f729Sjoerg     if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
12497330f729Sjoerg       const Expr *InitExpr = VD->getInit();
12507330f729Sjoerg       if (InitExpr) {
12517330f729Sjoerg         if (const BinaryOperator *B =
12527330f729Sjoerg                 dyn_cast<BinaryOperator>(InitExpr->IgnoreParenImpCasts())) {
12537330f729Sjoerg           BO = B;
12547330f729Sjoerg         }
12557330f729Sjoerg       }
12567330f729Sjoerg       if (VD->getName().lower().find("plural") != StringRef::npos ||
12577330f729Sjoerg           VD->getName().lower().find("singular") != StringRef::npos) {
12587330f729Sjoerg         return true;
12597330f729Sjoerg       }
12607330f729Sjoerg     }
12617330f729Sjoerg   } else if (const BinaryOperator *B = dyn_cast<BinaryOperator>(Condition)) {
12627330f729Sjoerg     BO = B;
12637330f729Sjoerg   }
12647330f729Sjoerg 
12657330f729Sjoerg   if (BO == nullptr)
12667330f729Sjoerg     return false;
12677330f729Sjoerg 
12687330f729Sjoerg   if (IntegerLiteral *IL = dyn_cast_or_null<IntegerLiteral>(
12697330f729Sjoerg           BO->getRHS()->IgnoreParenImpCasts())) {
12707330f729Sjoerg     llvm::APInt Value = IL->getValue();
12717330f729Sjoerg     if (Value == 1 || Value == 2) {
12727330f729Sjoerg       return true;
12737330f729Sjoerg     }
12747330f729Sjoerg   }
12757330f729Sjoerg   return false;
12767330f729Sjoerg }
12777330f729Sjoerg 
12787330f729Sjoerg // A CallExpr with "LOC" in its identifier that takes in a string literal
12797330f729Sjoerg // has been shown to almost always be a function that returns a localized
12807330f729Sjoerg // string. Raise a diagnostic when this is in a statement that matches
12817330f729Sjoerg // the condition.
VisitCallExpr(const CallExpr * CE)12827330f729Sjoerg bool PluralMisuseChecker::MethodCrawler::VisitCallExpr(const CallExpr *CE) {
12837330f729Sjoerg   if (InMatchingStatement) {
12847330f729Sjoerg     if (const FunctionDecl *FD = CE->getDirectCallee()) {
12857330f729Sjoerg       std::string NormalizedName =
12867330f729Sjoerg           StringRef(FD->getNameInfo().getAsString()).lower();
12877330f729Sjoerg       if (NormalizedName.find("loc") != std::string::npos) {
12887330f729Sjoerg         for (const Expr *Arg : CE->arguments()) {
12897330f729Sjoerg           if (isa<ObjCStringLiteral>(Arg))
12907330f729Sjoerg             reportPluralMisuseError(CE);
12917330f729Sjoerg         }
12927330f729Sjoerg       }
12937330f729Sjoerg     }
12947330f729Sjoerg   }
12957330f729Sjoerg   return true;
12967330f729Sjoerg }
12977330f729Sjoerg 
12987330f729Sjoerg // The other case is for NSLocalizedString which also returns
12997330f729Sjoerg // a localized string. It's a macro for the ObjCMessageExpr
13007330f729Sjoerg // [NSBundle localizedStringForKey:value:table:] Raise a
13017330f729Sjoerg // diagnostic when this is in a statement that matches
13027330f729Sjoerg // the condition.
VisitObjCMessageExpr(const ObjCMessageExpr * ME)13037330f729Sjoerg bool PluralMisuseChecker::MethodCrawler::VisitObjCMessageExpr(
13047330f729Sjoerg     const ObjCMessageExpr *ME) {
13057330f729Sjoerg   const ObjCInterfaceDecl *OD = ME->getReceiverInterface();
13067330f729Sjoerg   if (!OD)
13077330f729Sjoerg     return true;
13087330f729Sjoerg 
13097330f729Sjoerg   const IdentifierInfo *odInfo = OD->getIdentifier();
13107330f729Sjoerg 
13117330f729Sjoerg   if (odInfo->isStr("NSBundle") &&
13127330f729Sjoerg       ME->getSelector().getAsString() == "localizedStringForKey:value:table:") {
13137330f729Sjoerg     if (InMatchingStatement) {
13147330f729Sjoerg       reportPluralMisuseError(ME);
13157330f729Sjoerg     }
13167330f729Sjoerg   }
13177330f729Sjoerg   return true;
13187330f729Sjoerg }
13197330f729Sjoerg 
13207330f729Sjoerg /// Override TraverseIfStmt so we know when we are done traversing an IfStmt
TraverseIfStmt(IfStmt * I)13217330f729Sjoerg bool PluralMisuseChecker::MethodCrawler::TraverseIfStmt(IfStmt *I) {
13227330f729Sjoerg   RecursiveASTVisitor<MethodCrawler>::TraverseIfStmt(I);
13237330f729Sjoerg   return EndVisitIfStmt(I);
13247330f729Sjoerg }
13257330f729Sjoerg 
13267330f729Sjoerg // EndVisit callbacks are not provided by the RecursiveASTVisitor
13277330f729Sjoerg // so we override TraverseIfStmt and make a call to EndVisitIfStmt
13287330f729Sjoerg // after traversing the IfStmt
EndVisitIfStmt(IfStmt * I)13297330f729Sjoerg bool PluralMisuseChecker::MethodCrawler::EndVisitIfStmt(IfStmt *I) {
13307330f729Sjoerg   MatchingStatements.pop_back();
13317330f729Sjoerg   if (!MatchingStatements.empty()) {
13327330f729Sjoerg     if (MatchingStatements.back() != nullptr) {
13337330f729Sjoerg       InMatchingStatement = true;
13347330f729Sjoerg       return true;
13357330f729Sjoerg     }
13367330f729Sjoerg   }
13377330f729Sjoerg   InMatchingStatement = false;
13387330f729Sjoerg   return true;
13397330f729Sjoerg }
13407330f729Sjoerg 
VisitIfStmt(const IfStmt * I)13417330f729Sjoerg bool PluralMisuseChecker::MethodCrawler::VisitIfStmt(const IfStmt *I) {
13427330f729Sjoerg   const Expr *Condition = I->getCond()->IgnoreParenImpCasts();
13437330f729Sjoerg   if (isCheckingPlurality(Condition)) {
13447330f729Sjoerg     MatchingStatements.push_back(I);
13457330f729Sjoerg     InMatchingStatement = true;
13467330f729Sjoerg   } else {
13477330f729Sjoerg     MatchingStatements.push_back(nullptr);
13487330f729Sjoerg     InMatchingStatement = false;
13497330f729Sjoerg   }
13507330f729Sjoerg 
13517330f729Sjoerg   return true;
13527330f729Sjoerg }
13537330f729Sjoerg 
13547330f729Sjoerg // Preliminary support for conditional operators.
TraverseConditionalOperator(ConditionalOperator * C)13557330f729Sjoerg bool PluralMisuseChecker::MethodCrawler::TraverseConditionalOperator(
13567330f729Sjoerg     ConditionalOperator *C) {
13577330f729Sjoerg   RecursiveASTVisitor<MethodCrawler>::TraverseConditionalOperator(C);
13587330f729Sjoerg   MatchingStatements.pop_back();
13597330f729Sjoerg   if (!MatchingStatements.empty()) {
13607330f729Sjoerg     if (MatchingStatements.back() != nullptr)
13617330f729Sjoerg       InMatchingStatement = true;
13627330f729Sjoerg     else
13637330f729Sjoerg       InMatchingStatement = false;
13647330f729Sjoerg   } else {
13657330f729Sjoerg     InMatchingStatement = false;
13667330f729Sjoerg   }
13677330f729Sjoerg   return true;
13687330f729Sjoerg }
13697330f729Sjoerg 
VisitConditionalOperator(const ConditionalOperator * C)13707330f729Sjoerg bool PluralMisuseChecker::MethodCrawler::VisitConditionalOperator(
13717330f729Sjoerg     const ConditionalOperator *C) {
13727330f729Sjoerg   const Expr *Condition = C->getCond()->IgnoreParenImpCasts();
13737330f729Sjoerg   if (isCheckingPlurality(Condition)) {
13747330f729Sjoerg     MatchingStatements.push_back(C);
13757330f729Sjoerg     InMatchingStatement = true;
13767330f729Sjoerg   } else {
13777330f729Sjoerg     MatchingStatements.push_back(nullptr);
13787330f729Sjoerg     InMatchingStatement = false;
13797330f729Sjoerg   }
13807330f729Sjoerg   return true;
13817330f729Sjoerg }
13827330f729Sjoerg 
reportPluralMisuseError(const Stmt * S) const13837330f729Sjoerg void PluralMisuseChecker::MethodCrawler::reportPluralMisuseError(
13847330f729Sjoerg     const Stmt *S) const {
13857330f729Sjoerg   // Generate the bug report.
13867330f729Sjoerg   BR.EmitBasicReport(AC->getDecl(), Checker, "Plural Misuse",
13877330f729Sjoerg                      "Localizability Issue (Apple)",
13887330f729Sjoerg                      "Plural cases are not supported across all languages. "
13897330f729Sjoerg                      "Use a .stringsdict file instead",
13907330f729Sjoerg                      PathDiagnosticLocation(S, BR.getSourceManager(), AC));
13917330f729Sjoerg }
13927330f729Sjoerg 
13937330f729Sjoerg //===----------------------------------------------------------------------===//
13947330f729Sjoerg // Checker registration.
13957330f729Sjoerg //===----------------------------------------------------------------------===//
13967330f729Sjoerg 
registerNonLocalizedStringChecker(CheckerManager & mgr)13977330f729Sjoerg void ento::registerNonLocalizedStringChecker(CheckerManager &mgr) {
13987330f729Sjoerg   NonLocalizedStringChecker *checker =
13997330f729Sjoerg       mgr.registerChecker<NonLocalizedStringChecker>();
14007330f729Sjoerg   checker->IsAggressive =
14017330f729Sjoerg       mgr.getAnalyzerOptions().getCheckerBooleanOption(
14027330f729Sjoerg           checker, "AggressiveReport");
14037330f729Sjoerg }
14047330f729Sjoerg 
shouldRegisterNonLocalizedStringChecker(const CheckerManager & mgr)1405*e038c9c4Sjoerg bool ento::shouldRegisterNonLocalizedStringChecker(const CheckerManager &mgr) {
14067330f729Sjoerg   return true;
14077330f729Sjoerg }
14087330f729Sjoerg 
registerEmptyLocalizationContextChecker(CheckerManager & mgr)14097330f729Sjoerg void ento::registerEmptyLocalizationContextChecker(CheckerManager &mgr) {
14107330f729Sjoerg   mgr.registerChecker<EmptyLocalizationContextChecker>();
14117330f729Sjoerg }
14127330f729Sjoerg 
shouldRegisterEmptyLocalizationContextChecker(const CheckerManager & mgr)14137330f729Sjoerg bool ento::shouldRegisterEmptyLocalizationContextChecker(
1414*e038c9c4Sjoerg                                                     const CheckerManager &mgr) {
14157330f729Sjoerg   return true;
14167330f729Sjoerg }
14177330f729Sjoerg 
registerPluralMisuseChecker(CheckerManager & mgr)14187330f729Sjoerg void ento::registerPluralMisuseChecker(CheckerManager &mgr) {
14197330f729Sjoerg   mgr.registerChecker<PluralMisuseChecker>();
14207330f729Sjoerg }
14217330f729Sjoerg 
shouldRegisterPluralMisuseChecker(const CheckerManager & mgr)1422*e038c9c4Sjoerg bool ento::shouldRegisterPluralMisuseChecker(const CheckerManager &mgr) {
14237330f729Sjoerg   return true;
14247330f729Sjoerg }
1425