1 //=- LocalizationChecker.cpp -------------------------------------*- C++ -*-==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines a set of checks for localizability including:
11 //  1) A checker that warns about uses of non-localized NSStrings passed to
12 //     UI methods expecting localized strings
13 //  2) A syntactic checker that warns against the bad practice of
14 //     not including a comment in NSLocalizedString macros.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #include "ClangSACheckers.h"
19 #include "clang/AST/Attr.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/AST/DeclObjC.h"
22 #include "clang/AST/RecursiveASTVisitor.h"
23 #include "clang/AST/StmtVisitor.h"
24 #include "clang/Lex/Lexer.h"
25 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
26 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
27 #include "clang/StaticAnalyzer/Core/Checker.h"
28 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
29 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
30 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
31 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
32 #include "llvm/Support/Unicode.h"
33 
34 using namespace clang;
35 using namespace ento;
36 
37 namespace {
38 struct LocalizedState {
39 private:
40   enum Kind { NonLocalized, Localized } K;
41   LocalizedState(Kind InK) : K(InK) {}
42 
43 public:
44   bool isLocalized() const { return K == Localized; }
45   bool isNonLocalized() const { return K == NonLocalized; }
46 
47   static LocalizedState getLocalized() { return LocalizedState(Localized); }
48   static LocalizedState getNonLocalized() {
49     return LocalizedState(NonLocalized);
50   }
51 
52   // Overload the == operator
53   bool operator==(const LocalizedState &X) const { return K == X.K; }
54 
55   // LLVMs equivalent of a hash function
56   void Profile(llvm::FoldingSetNodeID &ID) const { ID.AddInteger(K); }
57 };
58 
59 class NonLocalizedStringChecker
60     : public Checker<check::PostCall, check::PreObjCMessage,
61                      check::PostObjCMessage,
62                      check::PostStmt<ObjCStringLiteral>> {
63 
64   mutable std::unique_ptr<BugType> BT;
65 
66   // Methods that require a localized string
67   mutable llvm::DenseMap<const IdentifierInfo *,
68                          llvm::DenseMap<Selector, uint8_t>> UIMethods;
69   // Methods that return a localized string
70   mutable llvm::SmallSet<std::pair<const IdentifierInfo *, Selector>, 12> LSM;
71   // C Functions that return a localized string
72   mutable llvm::SmallSet<const IdentifierInfo *, 5> LSF;
73 
74   void initUIMethods(ASTContext &Ctx) const;
75   void initLocStringsMethods(ASTContext &Ctx) const;
76 
77   bool hasNonLocalizedState(SVal S, CheckerContext &C) const;
78   bool hasLocalizedState(SVal S, CheckerContext &C) const;
79   void setNonLocalizedState(SVal S, CheckerContext &C) const;
80   void setLocalizedState(SVal S, CheckerContext &C) const;
81 
82   bool isAnnotatedAsLocalized(const Decl *D) const;
83   void reportLocalizationError(SVal S, const ObjCMethodCall &M,
84                                CheckerContext &C, int argumentNumber = 0) const;
85 
86   int getLocalizedArgumentForSelector(const IdentifierInfo *Receiver,
87                                       Selector S) const;
88 
89 public:
90   NonLocalizedStringChecker();
91 
92   // When this parameter is set to true, the checker assumes all
93   // methods that return NSStrings are unlocalized. Thus, more false
94   // positives will be reported.
95   DefaultBool IsAggressive;
96 
97   void checkPreObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const;
98   void checkPostObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const;
99   void checkPostStmt(const ObjCStringLiteral *SL, CheckerContext &C) const;
100   void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
101 };
102 
103 } // end anonymous namespace
104 
105 REGISTER_MAP_WITH_PROGRAMSTATE(LocalizedMemMap, const MemRegion *,
106                                LocalizedState)
107 
108 NonLocalizedStringChecker::NonLocalizedStringChecker() {
109   BT.reset(new BugType(this, "Unlocalizable string",
110                        "Localizability Issue (Apple)"));
111 }
112 
113 namespace {
114 class NonLocalizedStringBRVisitor final
115     : public BugReporterVisitorImpl<NonLocalizedStringBRVisitor> {
116 
117   const MemRegion *NonLocalizedString;
118   bool Satisfied;
119 
120 public:
121   NonLocalizedStringBRVisitor(const MemRegion *NonLocalizedString)
122       : NonLocalizedString(NonLocalizedString), Satisfied(false) {
123         assert(NonLocalizedString);
124   }
125 
126   PathDiagnosticPiece *VisitNode(const ExplodedNode *Succ,
127                                  const ExplodedNode *Pred,
128                                  BugReporterContext &BRC,
129                                  BugReport &BR) override;
130 
131   void Profile(llvm::FoldingSetNodeID &ID) const override {
132     ID.Add(NonLocalizedString);
133   }
134 };
135 } // End anonymous namespace.
136 
137 #define NEW_RECEIVER(receiver)                                                 \
138   llvm::DenseMap<Selector, uint8_t> &receiver##M =                             \
139       UIMethods.insert({&Ctx.Idents.get(#receiver),                            \
140                         llvm::DenseMap<Selector, uint8_t>()})                  \
141           .first->second;
142 #define ADD_NULLARY_METHOD(receiver, method, argument)                         \
143   receiver##M.insert(                                                          \
144       {Ctx.Selectors.getNullarySelector(&Ctx.Idents.get(#method)), argument});
145 #define ADD_UNARY_METHOD(receiver, method, argument)                           \
146   receiver##M.insert(                                                          \
147       {Ctx.Selectors.getUnarySelector(&Ctx.Idents.get(#method)), argument});
148 #define ADD_METHOD(receiver, method_list, count, argument)                     \
149   receiver##M.insert({Ctx.Selectors.getSelector(count, method_list), argument});
150 
151 /// Initializes a list of methods that require a localized string
152 /// Format: {"ClassName", {{"selectorName:", LocStringArg#}, ...}, ...}
153 void NonLocalizedStringChecker::initUIMethods(ASTContext &Ctx) const {
154   if (!UIMethods.empty())
155     return;
156 
157   // UI Methods
158   NEW_RECEIVER(UISearchDisplayController)
159   ADD_UNARY_METHOD(UISearchDisplayController, setSearchResultsTitle, 0)
160 
161   NEW_RECEIVER(UITabBarItem)
162   IdentifierInfo *initWithTitleUITabBarItemTag[] = {
163       &Ctx.Idents.get("initWithTitle"), &Ctx.Idents.get("image"),
164       &Ctx.Idents.get("tag")};
165   ADD_METHOD(UITabBarItem, initWithTitleUITabBarItemTag, 3, 0)
166   IdentifierInfo *initWithTitleUITabBarItemImage[] = {
167       &Ctx.Idents.get("initWithTitle"), &Ctx.Idents.get("image"),
168       &Ctx.Idents.get("selectedImage")};
169   ADD_METHOD(UITabBarItem, initWithTitleUITabBarItemImage, 3, 0)
170 
171   NEW_RECEIVER(NSDockTile)
172   ADD_UNARY_METHOD(NSDockTile, setBadgeLabel, 0)
173 
174   NEW_RECEIVER(NSStatusItem)
175   ADD_UNARY_METHOD(NSStatusItem, setTitle, 0)
176   ADD_UNARY_METHOD(NSStatusItem, setToolTip, 0)
177 
178   NEW_RECEIVER(UITableViewRowAction)
179   IdentifierInfo *rowActionWithStyleUITableViewRowAction[] = {
180       &Ctx.Idents.get("rowActionWithStyle"), &Ctx.Idents.get("title"),
181       &Ctx.Idents.get("handler")};
182   ADD_METHOD(UITableViewRowAction, rowActionWithStyleUITableViewRowAction, 3, 1)
183   ADD_UNARY_METHOD(UITableViewRowAction, setTitle, 0)
184 
185   NEW_RECEIVER(NSBox)
186   ADD_UNARY_METHOD(NSBox, setTitle, 0)
187 
188   NEW_RECEIVER(NSButton)
189   ADD_UNARY_METHOD(NSButton, setTitle, 0)
190   ADD_UNARY_METHOD(NSButton, setAlternateTitle, 0)
191   IdentifierInfo *radioButtonWithTitleNSButton[] = {
192       &Ctx.Idents.get("radioButtonWithTitle"), &Ctx.Idents.get("target"),
193       &Ctx.Idents.get("action")};
194   ADD_METHOD(NSButton, radioButtonWithTitleNSButton, 3, 0)
195   IdentifierInfo *buttonWithTitleNSButtonImage[] = {
196       &Ctx.Idents.get("buttonWithTitle"), &Ctx.Idents.get("image"),
197       &Ctx.Idents.get("target"), &Ctx.Idents.get("action")};
198   ADD_METHOD(NSButton, buttonWithTitleNSButtonImage, 4, 0)
199   IdentifierInfo *checkboxWithTitleNSButton[] = {
200       &Ctx.Idents.get("checkboxWithTitle"), &Ctx.Idents.get("target"),
201       &Ctx.Idents.get("action")};
202   ADD_METHOD(NSButton, checkboxWithTitleNSButton, 3, 0)
203   IdentifierInfo *buttonWithTitleNSButtonTarget[] = {
204       &Ctx.Idents.get("buttonWithTitle"), &Ctx.Idents.get("target"),
205       &Ctx.Idents.get("action")};
206   ADD_METHOD(NSButton, buttonWithTitleNSButtonTarget, 3, 0)
207 
208   NEW_RECEIVER(NSSavePanel)
209   ADD_UNARY_METHOD(NSSavePanel, setPrompt, 0)
210   ADD_UNARY_METHOD(NSSavePanel, setTitle, 0)
211   ADD_UNARY_METHOD(NSSavePanel, setNameFieldLabel, 0)
212   ADD_UNARY_METHOD(NSSavePanel, setNameFieldStringValue, 0)
213   ADD_UNARY_METHOD(NSSavePanel, setMessage, 0)
214 
215   NEW_RECEIVER(UIPrintInfo)
216   ADD_UNARY_METHOD(UIPrintInfo, setJobName, 0)
217 
218   NEW_RECEIVER(NSTabViewItem)
219   ADD_UNARY_METHOD(NSTabViewItem, setLabel, 0)
220   ADD_UNARY_METHOD(NSTabViewItem, setToolTip, 0)
221 
222   NEW_RECEIVER(NSBrowser)
223   IdentifierInfo *setTitleNSBrowser[] = {&Ctx.Idents.get("setTitle"),
224                                          &Ctx.Idents.get("ofColumn")};
225   ADD_METHOD(NSBrowser, setTitleNSBrowser, 2, 0)
226 
227   NEW_RECEIVER(UIAccessibilityElement)
228   ADD_UNARY_METHOD(UIAccessibilityElement, setAccessibilityLabel, 0)
229   ADD_UNARY_METHOD(UIAccessibilityElement, setAccessibilityHint, 0)
230   ADD_UNARY_METHOD(UIAccessibilityElement, setAccessibilityValue, 0)
231 
232   NEW_RECEIVER(UIAlertAction)
233   IdentifierInfo *actionWithTitleUIAlertAction[] = {
234       &Ctx.Idents.get("actionWithTitle"), &Ctx.Idents.get("style"),
235       &Ctx.Idents.get("handler")};
236   ADD_METHOD(UIAlertAction, actionWithTitleUIAlertAction, 3, 0)
237 
238   NEW_RECEIVER(NSPopUpButton)
239   ADD_UNARY_METHOD(NSPopUpButton, addItemWithTitle, 0)
240   IdentifierInfo *insertItemWithTitleNSPopUpButton[] = {
241       &Ctx.Idents.get("insertItemWithTitle"), &Ctx.Idents.get("atIndex")};
242   ADD_METHOD(NSPopUpButton, insertItemWithTitleNSPopUpButton, 2, 0)
243   ADD_UNARY_METHOD(NSPopUpButton, removeItemWithTitle, 0)
244   ADD_UNARY_METHOD(NSPopUpButton, selectItemWithTitle, 0)
245   ADD_UNARY_METHOD(NSPopUpButton, setTitle, 0)
246 
247   NEW_RECEIVER(NSTableViewRowAction)
248   IdentifierInfo *rowActionWithStyleNSTableViewRowAction[] = {
249       &Ctx.Idents.get("rowActionWithStyle"), &Ctx.Idents.get("title"),
250       &Ctx.Idents.get("handler")};
251   ADD_METHOD(NSTableViewRowAction, rowActionWithStyleNSTableViewRowAction, 3, 1)
252   ADD_UNARY_METHOD(NSTableViewRowAction, setTitle, 0)
253 
254   NEW_RECEIVER(NSImage)
255   ADD_UNARY_METHOD(NSImage, setAccessibilityDescription, 0)
256 
257   NEW_RECEIVER(NSUserActivity)
258   ADD_UNARY_METHOD(NSUserActivity, setTitle, 0)
259 
260   NEW_RECEIVER(NSPathControlItem)
261   ADD_UNARY_METHOD(NSPathControlItem, setTitle, 0)
262 
263   NEW_RECEIVER(NSCell)
264   ADD_UNARY_METHOD(NSCell, initTextCell, 0)
265   ADD_UNARY_METHOD(NSCell, setTitle, 0)
266   ADD_UNARY_METHOD(NSCell, setStringValue, 0)
267 
268   NEW_RECEIVER(NSPathControl)
269   ADD_UNARY_METHOD(NSPathControl, setPlaceholderString, 0)
270 
271   NEW_RECEIVER(UIAccessibility)
272   ADD_UNARY_METHOD(UIAccessibility, setAccessibilityLabel, 0)
273   ADD_UNARY_METHOD(UIAccessibility, setAccessibilityHint, 0)
274   ADD_UNARY_METHOD(UIAccessibility, setAccessibilityValue, 0)
275 
276   NEW_RECEIVER(NSTableColumn)
277   ADD_UNARY_METHOD(NSTableColumn, setTitle, 0)
278   ADD_UNARY_METHOD(NSTableColumn, setHeaderToolTip, 0)
279 
280   NEW_RECEIVER(NSSegmentedControl)
281   IdentifierInfo *setLabelNSSegmentedControl[] = {
282       &Ctx.Idents.get("setLabel"), &Ctx.Idents.get("forSegment")};
283   ADD_METHOD(NSSegmentedControl, setLabelNSSegmentedControl, 2, 0)
284 
285   NEW_RECEIVER(NSButtonCell)
286   ADD_UNARY_METHOD(NSButtonCell, setTitle, 0)
287   ADD_UNARY_METHOD(NSButtonCell, setAlternateTitle, 0)
288 
289   NEW_RECEIVER(NSDatePickerCell)
290   ADD_UNARY_METHOD(NSDatePickerCell, initTextCell, 0)
291 
292   NEW_RECEIVER(NSSliderCell)
293   ADD_UNARY_METHOD(NSSliderCell, setTitle, 0)
294 
295   NEW_RECEIVER(NSControl)
296   ADD_UNARY_METHOD(NSControl, setStringValue, 0)
297 
298   NEW_RECEIVER(NSAccessibility)
299   ADD_UNARY_METHOD(NSAccessibility, setAccessibilityValueDescription, 0)
300   ADD_UNARY_METHOD(NSAccessibility, setAccessibilityLabel, 0)
301   ADD_UNARY_METHOD(NSAccessibility, setAccessibilityTitle, 0)
302   ADD_UNARY_METHOD(NSAccessibility, setAccessibilityPlaceholderValue, 0)
303   ADD_UNARY_METHOD(NSAccessibility, setAccessibilityHelp, 0)
304 
305   NEW_RECEIVER(NSMatrix)
306   IdentifierInfo *setToolTipNSMatrix[] = {&Ctx.Idents.get("setToolTip"),
307                                           &Ctx.Idents.get("forCell")};
308   ADD_METHOD(NSMatrix, setToolTipNSMatrix, 2, 0)
309 
310   NEW_RECEIVER(NSPrintPanel)
311   ADD_UNARY_METHOD(NSPrintPanel, setDefaultButtonTitle, 0)
312 
313   NEW_RECEIVER(UILocalNotification)
314   ADD_UNARY_METHOD(UILocalNotification, setAlertBody, 0)
315   ADD_UNARY_METHOD(UILocalNotification, setAlertAction, 0)
316   ADD_UNARY_METHOD(UILocalNotification, setAlertTitle, 0)
317 
318   NEW_RECEIVER(NSSlider)
319   ADD_UNARY_METHOD(NSSlider, setTitle, 0)
320 
321   NEW_RECEIVER(UIMenuItem)
322   IdentifierInfo *initWithTitleUIMenuItem[] = {&Ctx.Idents.get("initWithTitle"),
323                                                &Ctx.Idents.get("action")};
324   ADD_METHOD(UIMenuItem, initWithTitleUIMenuItem, 2, 0)
325   ADD_UNARY_METHOD(UIMenuItem, setTitle, 0)
326 
327   NEW_RECEIVER(UIAlertController)
328   IdentifierInfo *alertControllerWithTitleUIAlertController[] = {
329       &Ctx.Idents.get("alertControllerWithTitle"), &Ctx.Idents.get("message"),
330       &Ctx.Idents.get("preferredStyle")};
331   ADD_METHOD(UIAlertController, alertControllerWithTitleUIAlertController, 3, 1)
332   ADD_UNARY_METHOD(UIAlertController, setTitle, 0)
333   ADD_UNARY_METHOD(UIAlertController, setMessage, 0)
334 
335   NEW_RECEIVER(UIApplicationShortcutItem)
336   IdentifierInfo *initWithTypeUIApplicationShortcutItemIcon[] = {
337       &Ctx.Idents.get("initWithType"), &Ctx.Idents.get("localizedTitle"),
338       &Ctx.Idents.get("localizedSubtitle"), &Ctx.Idents.get("icon"),
339       &Ctx.Idents.get("userInfo")};
340   ADD_METHOD(UIApplicationShortcutItem,
341              initWithTypeUIApplicationShortcutItemIcon, 5, 1)
342   IdentifierInfo *initWithTypeUIApplicationShortcutItem[] = {
343       &Ctx.Idents.get("initWithType"), &Ctx.Idents.get("localizedTitle")};
344   ADD_METHOD(UIApplicationShortcutItem, initWithTypeUIApplicationShortcutItem,
345              2, 1)
346 
347   NEW_RECEIVER(UIActionSheet)
348   IdentifierInfo *initWithTitleUIActionSheet[] = {
349       &Ctx.Idents.get("initWithTitle"), &Ctx.Idents.get("delegate"),
350       &Ctx.Idents.get("cancelButtonTitle"),
351       &Ctx.Idents.get("destructiveButtonTitle"),
352       &Ctx.Idents.get("otherButtonTitles")};
353   ADD_METHOD(UIActionSheet, initWithTitleUIActionSheet, 5, 0)
354   ADD_UNARY_METHOD(UIActionSheet, addButtonWithTitle, 0)
355   ADD_UNARY_METHOD(UIActionSheet, setTitle, 0)
356 
357   NEW_RECEIVER(UIAccessibilityCustomAction)
358   IdentifierInfo *initWithNameUIAccessibilityCustomAction[] = {
359       &Ctx.Idents.get("initWithName"), &Ctx.Idents.get("target"),
360       &Ctx.Idents.get("selector")};
361   ADD_METHOD(UIAccessibilityCustomAction,
362              initWithNameUIAccessibilityCustomAction, 3, 0)
363   ADD_UNARY_METHOD(UIAccessibilityCustomAction, setName, 0)
364 
365   NEW_RECEIVER(UISearchBar)
366   ADD_UNARY_METHOD(UISearchBar, setText, 0)
367   ADD_UNARY_METHOD(UISearchBar, setPrompt, 0)
368   ADD_UNARY_METHOD(UISearchBar, setPlaceholder, 0)
369 
370   NEW_RECEIVER(UIBarItem)
371   ADD_UNARY_METHOD(UIBarItem, setTitle, 0)
372 
373   NEW_RECEIVER(UITextView)
374   ADD_UNARY_METHOD(UITextView, setText, 0)
375 
376   NEW_RECEIVER(NSView)
377   ADD_UNARY_METHOD(NSView, setToolTip, 0)
378 
379   NEW_RECEIVER(NSTextField)
380   ADD_UNARY_METHOD(NSTextField, setPlaceholderString, 0)
381   ADD_UNARY_METHOD(NSTextField, textFieldWithString, 0)
382   ADD_UNARY_METHOD(NSTextField, wrappingLabelWithString, 0)
383   ADD_UNARY_METHOD(NSTextField, labelWithString, 0)
384 
385   NEW_RECEIVER(NSAttributedString)
386   ADD_UNARY_METHOD(NSAttributedString, initWithString, 0)
387   IdentifierInfo *initWithStringNSAttributedString[] = {
388       &Ctx.Idents.get("initWithString"), &Ctx.Idents.get("attributes")};
389   ADD_METHOD(NSAttributedString, initWithStringNSAttributedString, 2, 0)
390 
391   NEW_RECEIVER(NSText)
392   ADD_UNARY_METHOD(NSText, setString, 0)
393 
394   NEW_RECEIVER(UIKeyCommand)
395   IdentifierInfo *keyCommandWithInputUIKeyCommand[] = {
396       &Ctx.Idents.get("keyCommandWithInput"), &Ctx.Idents.get("modifierFlags"),
397       &Ctx.Idents.get("action"), &Ctx.Idents.get("discoverabilityTitle")};
398   ADD_METHOD(UIKeyCommand, keyCommandWithInputUIKeyCommand, 4, 3)
399   ADD_UNARY_METHOD(UIKeyCommand, setDiscoverabilityTitle, 0)
400 
401   NEW_RECEIVER(UILabel)
402   ADD_UNARY_METHOD(UILabel, setText, 0)
403 
404   NEW_RECEIVER(NSAlert)
405   IdentifierInfo *alertWithMessageTextNSAlert[] = {
406       &Ctx.Idents.get("alertWithMessageText"), &Ctx.Idents.get("defaultButton"),
407       &Ctx.Idents.get("alternateButton"), &Ctx.Idents.get("otherButton"),
408       &Ctx.Idents.get("informativeTextWithFormat")};
409   ADD_METHOD(NSAlert, alertWithMessageTextNSAlert, 5, 0)
410   ADD_UNARY_METHOD(NSAlert, addButtonWithTitle, 0)
411   ADD_UNARY_METHOD(NSAlert, setMessageText, 0)
412   ADD_UNARY_METHOD(NSAlert, setInformativeText, 0)
413   ADD_UNARY_METHOD(NSAlert, setHelpAnchor, 0)
414 
415   NEW_RECEIVER(UIMutableApplicationShortcutItem)
416   ADD_UNARY_METHOD(UIMutableApplicationShortcutItem, setLocalizedTitle, 0)
417   ADD_UNARY_METHOD(UIMutableApplicationShortcutItem, setLocalizedSubtitle, 0)
418 
419   NEW_RECEIVER(UIButton)
420   IdentifierInfo *setTitleUIButton[] = {&Ctx.Idents.get("setTitle"),
421                                         &Ctx.Idents.get("forState")};
422   ADD_METHOD(UIButton, setTitleUIButton, 2, 0)
423 
424   NEW_RECEIVER(NSWindow)
425   ADD_UNARY_METHOD(NSWindow, setTitle, 0)
426   IdentifierInfo *minFrameWidthWithTitleNSWindow[] = {
427       &Ctx.Idents.get("minFrameWidthWithTitle"), &Ctx.Idents.get("styleMask")};
428   ADD_METHOD(NSWindow, minFrameWidthWithTitleNSWindow, 2, 0)
429   ADD_UNARY_METHOD(NSWindow, setMiniwindowTitle, 0)
430 
431   NEW_RECEIVER(NSPathCell)
432   ADD_UNARY_METHOD(NSPathCell, setPlaceholderString, 0)
433 
434   NEW_RECEIVER(UIDocumentMenuViewController)
435   IdentifierInfo *addOptionWithTitleUIDocumentMenuViewController[] = {
436       &Ctx.Idents.get("addOptionWithTitle"), &Ctx.Idents.get("image"),
437       &Ctx.Idents.get("order"), &Ctx.Idents.get("handler")};
438   ADD_METHOD(UIDocumentMenuViewController,
439              addOptionWithTitleUIDocumentMenuViewController, 4, 0)
440 
441   NEW_RECEIVER(UINavigationItem)
442   ADD_UNARY_METHOD(UINavigationItem, initWithTitle, 0)
443   ADD_UNARY_METHOD(UINavigationItem, setTitle, 0)
444   ADD_UNARY_METHOD(UINavigationItem, setPrompt, 0)
445 
446   NEW_RECEIVER(UIAlertView)
447   IdentifierInfo *initWithTitleUIAlertView[] = {
448       &Ctx.Idents.get("initWithTitle"), &Ctx.Idents.get("message"),
449       &Ctx.Idents.get("delegate"), &Ctx.Idents.get("cancelButtonTitle"),
450       &Ctx.Idents.get("otherButtonTitles")};
451   ADD_METHOD(UIAlertView, initWithTitleUIAlertView, 5, 0)
452   ADD_UNARY_METHOD(UIAlertView, addButtonWithTitle, 0)
453   ADD_UNARY_METHOD(UIAlertView, setTitle, 0)
454   ADD_UNARY_METHOD(UIAlertView, setMessage, 0)
455 
456   NEW_RECEIVER(NSFormCell)
457   ADD_UNARY_METHOD(NSFormCell, initTextCell, 0)
458   ADD_UNARY_METHOD(NSFormCell, setTitle, 0)
459   ADD_UNARY_METHOD(NSFormCell, setPlaceholderString, 0)
460 
461   NEW_RECEIVER(NSUserNotification)
462   ADD_UNARY_METHOD(NSUserNotification, setTitle, 0)
463   ADD_UNARY_METHOD(NSUserNotification, setSubtitle, 0)
464   ADD_UNARY_METHOD(NSUserNotification, setInformativeText, 0)
465   ADD_UNARY_METHOD(NSUserNotification, setActionButtonTitle, 0)
466   ADD_UNARY_METHOD(NSUserNotification, setOtherButtonTitle, 0)
467   ADD_UNARY_METHOD(NSUserNotification, setResponsePlaceholder, 0)
468 
469   NEW_RECEIVER(NSToolbarItem)
470   ADD_UNARY_METHOD(NSToolbarItem, setLabel, 0)
471   ADD_UNARY_METHOD(NSToolbarItem, setPaletteLabel, 0)
472   ADD_UNARY_METHOD(NSToolbarItem, setToolTip, 0)
473 
474   NEW_RECEIVER(NSProgress)
475   ADD_UNARY_METHOD(NSProgress, setLocalizedDescription, 0)
476   ADD_UNARY_METHOD(NSProgress, setLocalizedAdditionalDescription, 0)
477 
478   NEW_RECEIVER(NSSegmentedCell)
479   IdentifierInfo *setLabelNSSegmentedCell[] = {&Ctx.Idents.get("setLabel"),
480                                                &Ctx.Idents.get("forSegment")};
481   ADD_METHOD(NSSegmentedCell, setLabelNSSegmentedCell, 2, 0)
482   IdentifierInfo *setToolTipNSSegmentedCell[] = {&Ctx.Idents.get("setToolTip"),
483                                                  &Ctx.Idents.get("forSegment")};
484   ADD_METHOD(NSSegmentedCell, setToolTipNSSegmentedCell, 2, 0)
485 
486   NEW_RECEIVER(NSUndoManager)
487   ADD_UNARY_METHOD(NSUndoManager, setActionName, 0)
488   ADD_UNARY_METHOD(NSUndoManager, undoMenuTitleForUndoActionName, 0)
489   ADD_UNARY_METHOD(NSUndoManager, redoMenuTitleForUndoActionName, 0)
490 
491   NEW_RECEIVER(NSMenuItem)
492   IdentifierInfo *initWithTitleNSMenuItem[] = {
493       &Ctx.Idents.get("initWithTitle"), &Ctx.Idents.get("action"),
494       &Ctx.Idents.get("keyEquivalent")};
495   ADD_METHOD(NSMenuItem, initWithTitleNSMenuItem, 3, 0)
496   ADD_UNARY_METHOD(NSMenuItem, setTitle, 0)
497   ADD_UNARY_METHOD(NSMenuItem, setToolTip, 0)
498 
499   NEW_RECEIVER(NSPopUpButtonCell)
500   IdentifierInfo *initTextCellNSPopUpButtonCell[] = {
501       &Ctx.Idents.get("initTextCell"), &Ctx.Idents.get("pullsDown")};
502   ADD_METHOD(NSPopUpButtonCell, initTextCellNSPopUpButtonCell, 2, 0)
503   ADD_UNARY_METHOD(NSPopUpButtonCell, addItemWithTitle, 0)
504   IdentifierInfo *insertItemWithTitleNSPopUpButtonCell[] = {
505       &Ctx.Idents.get("insertItemWithTitle"), &Ctx.Idents.get("atIndex")};
506   ADD_METHOD(NSPopUpButtonCell, insertItemWithTitleNSPopUpButtonCell, 2, 0)
507   ADD_UNARY_METHOD(NSPopUpButtonCell, removeItemWithTitle, 0)
508   ADD_UNARY_METHOD(NSPopUpButtonCell, selectItemWithTitle, 0)
509   ADD_UNARY_METHOD(NSPopUpButtonCell, setTitle, 0)
510 
511   NEW_RECEIVER(NSViewController)
512   ADD_UNARY_METHOD(NSViewController, setTitle, 0)
513 
514   NEW_RECEIVER(NSMenu)
515   ADD_UNARY_METHOD(NSMenu, initWithTitle, 0)
516   IdentifierInfo *insertItemWithTitleNSMenu[] = {
517       &Ctx.Idents.get("insertItemWithTitle"), &Ctx.Idents.get("action"),
518       &Ctx.Idents.get("keyEquivalent"), &Ctx.Idents.get("atIndex")};
519   ADD_METHOD(NSMenu, insertItemWithTitleNSMenu, 4, 0)
520   IdentifierInfo *addItemWithTitleNSMenu[] = {
521       &Ctx.Idents.get("addItemWithTitle"), &Ctx.Idents.get("action"),
522       &Ctx.Idents.get("keyEquivalent")};
523   ADD_METHOD(NSMenu, addItemWithTitleNSMenu, 3, 0)
524   ADD_UNARY_METHOD(NSMenu, setTitle, 0)
525 
526   NEW_RECEIVER(UIMutableUserNotificationAction)
527   ADD_UNARY_METHOD(UIMutableUserNotificationAction, setTitle, 0)
528 
529   NEW_RECEIVER(NSForm)
530   ADD_UNARY_METHOD(NSForm, addEntry, 0)
531   IdentifierInfo *insertEntryNSForm[] = {&Ctx.Idents.get("insertEntry"),
532                                          &Ctx.Idents.get("atIndex")};
533   ADD_METHOD(NSForm, insertEntryNSForm, 2, 0)
534 
535   NEW_RECEIVER(NSTextFieldCell)
536   ADD_UNARY_METHOD(NSTextFieldCell, setPlaceholderString, 0)
537 
538   NEW_RECEIVER(NSUserNotificationAction)
539   IdentifierInfo *actionWithIdentifierNSUserNotificationAction[] = {
540       &Ctx.Idents.get("actionWithIdentifier"), &Ctx.Idents.get("title")};
541   ADD_METHOD(NSUserNotificationAction,
542              actionWithIdentifierNSUserNotificationAction, 2, 1)
543 
544   NEW_RECEIVER(UITextField)
545   ADD_UNARY_METHOD(UITextField, setText, 0)
546   ADD_UNARY_METHOD(UITextField, setPlaceholder, 0)
547 
548   NEW_RECEIVER(UIBarButtonItem)
549   IdentifierInfo *initWithTitleUIBarButtonItem[] = {
550       &Ctx.Idents.get("initWithTitle"), &Ctx.Idents.get("style"),
551       &Ctx.Idents.get("target"), &Ctx.Idents.get("action")};
552   ADD_METHOD(UIBarButtonItem, initWithTitleUIBarButtonItem, 4, 0)
553 
554   NEW_RECEIVER(UIViewController)
555   ADD_UNARY_METHOD(UIViewController, setTitle, 0)
556 
557   NEW_RECEIVER(UISegmentedControl)
558   IdentifierInfo *insertSegmentWithTitleUISegmentedControl[] = {
559       &Ctx.Idents.get("insertSegmentWithTitle"), &Ctx.Idents.get("atIndex"),
560       &Ctx.Idents.get("animated")};
561   ADD_METHOD(UISegmentedControl, insertSegmentWithTitleUISegmentedControl, 3, 0)
562   IdentifierInfo *setTitleUISegmentedControl[] = {
563       &Ctx.Idents.get("setTitle"), &Ctx.Idents.get("forSegmentAtIndex")};
564   ADD_METHOD(UISegmentedControl, setTitleUISegmentedControl, 2, 0)
565 }
566 
567 #define LSF_INSERT(function_name) LSF.insert(&Ctx.Idents.get(function_name));
568 #define LSM_INSERT_NULLARY(receiver, method_name)                              \
569   LSM.insert({&Ctx.Idents.get(receiver), Ctx.Selectors.getNullarySelector(     \
570                                              &Ctx.Idents.get(method_name))});
571 #define LSM_INSERT_UNARY(receiver, method_name)                                \
572   LSM.insert({&Ctx.Idents.get(receiver),                                       \
573               Ctx.Selectors.getUnarySelector(&Ctx.Idents.get(method_name))});
574 #define LSM_INSERT_SELECTOR(receiver, method_list, arguments)                  \
575   LSM.insert({&Ctx.Idents.get(receiver),                                       \
576               Ctx.Selectors.getSelector(arguments, method_list)});
577 
578 /// Initializes a list of methods and C functions that return a localized string
579 void NonLocalizedStringChecker::initLocStringsMethods(ASTContext &Ctx) const {
580   if (!LSM.empty())
581     return;
582 
583   IdentifierInfo *LocalizedStringMacro[] = {
584       &Ctx.Idents.get("localizedStringForKey"), &Ctx.Idents.get("value"),
585       &Ctx.Idents.get("table")};
586   LSM_INSERT_SELECTOR("NSBundle", LocalizedStringMacro, 3)
587   LSM_INSERT_UNARY("NSDateFormatter", "stringFromDate")
588   IdentifierInfo *LocalizedStringFromDate[] = {
589       &Ctx.Idents.get("localizedStringFromDate"), &Ctx.Idents.get("dateStyle"),
590       &Ctx.Idents.get("timeStyle")};
591   LSM_INSERT_SELECTOR("NSDateFormatter", LocalizedStringFromDate, 3)
592   LSM_INSERT_UNARY("NSNumberFormatter", "stringFromNumber")
593   LSM_INSERT_NULLARY("UITextField", "text")
594   LSM_INSERT_NULLARY("UITextView", "text")
595   LSM_INSERT_NULLARY("UILabel", "text")
596 
597   LSF_INSERT("CFDateFormatterCreateStringWithDate");
598   LSF_INSERT("CFDateFormatterCreateStringWithAbsoluteTime");
599   LSF_INSERT("CFNumberFormatterCreateStringWithNumber");
600 }
601 
602 /// Checks to see if the method / function declaration includes
603 /// __attribute__((annotate("returns_localized_nsstring")))
604 bool NonLocalizedStringChecker::isAnnotatedAsLocalized(const Decl *D) const {
605   if (!D)
606     return false;
607   return std::any_of(
608       D->specific_attr_begin<AnnotateAttr>(),
609       D->specific_attr_end<AnnotateAttr>(), [](const AnnotateAttr *Ann) {
610         return Ann->getAnnotation() == "returns_localized_nsstring";
611       });
612 }
613 
614 /// Returns true if the given SVal is marked as Localized in the program state
615 bool NonLocalizedStringChecker::hasLocalizedState(SVal S,
616                                                   CheckerContext &C) const {
617   const MemRegion *mt = S.getAsRegion();
618   if (mt) {
619     const LocalizedState *LS = C.getState()->get<LocalizedMemMap>(mt);
620     if (LS && LS->isLocalized())
621       return true;
622   }
623   return false;
624 }
625 
626 /// Returns true if the given SVal is marked as NonLocalized in the program
627 /// state
628 bool NonLocalizedStringChecker::hasNonLocalizedState(SVal S,
629                                                      CheckerContext &C) const {
630   const MemRegion *mt = S.getAsRegion();
631   if (mt) {
632     const LocalizedState *LS = C.getState()->get<LocalizedMemMap>(mt);
633     if (LS && LS->isNonLocalized())
634       return true;
635   }
636   return false;
637 }
638 
639 /// Marks the given SVal as Localized in the program state
640 void NonLocalizedStringChecker::setLocalizedState(const SVal S,
641                                                   CheckerContext &C) const {
642   const MemRegion *mt = S.getAsRegion();
643   if (mt) {
644     ProgramStateRef State =
645         C.getState()->set<LocalizedMemMap>(mt, LocalizedState::getLocalized());
646     C.addTransition(State);
647   }
648 }
649 
650 /// Marks the given SVal as NonLocalized in the program state
651 void NonLocalizedStringChecker::setNonLocalizedState(const SVal S,
652                                                      CheckerContext &C) const {
653   const MemRegion *mt = S.getAsRegion();
654   if (mt) {
655     ProgramStateRef State = C.getState()->set<LocalizedMemMap>(
656         mt, LocalizedState::getNonLocalized());
657     C.addTransition(State);
658   }
659 }
660 
661 
662 static bool isDebuggingName(std::string name) {
663   return StringRef(name).lower().find("debug") != StringRef::npos;
664 }
665 
666 /// Returns true when, heuristically, the analyzer may be analyzing debugging
667 /// code. We use this to suppress localization diagnostics in un-localized user
668 /// interfaces that are only used for debugging and are therefore not user
669 /// facing.
670 static bool isDebuggingContext(CheckerContext &C) {
671   const Decl *D = C.getCurrentAnalysisDeclContext()->getDecl();
672   if (!D)
673     return false;
674 
675   if (auto *ND = dyn_cast<NamedDecl>(D)) {
676     if (isDebuggingName(ND->getNameAsString()))
677       return true;
678   }
679 
680   const DeclContext *DC = D->getDeclContext();
681 
682   if (auto *CD = dyn_cast<ObjCContainerDecl>(DC)) {
683     if (isDebuggingName(CD->getNameAsString()))
684       return true;
685   }
686 
687   return false;
688 }
689 
690 
691 /// Reports a localization error for the passed in method call and SVal
692 void NonLocalizedStringChecker::reportLocalizationError(
693     SVal S, const ObjCMethodCall &M, CheckerContext &C,
694     int argumentNumber) const {
695 
696   // Don't warn about localization errors in classes and methods that
697   // may be debug code.
698   if (isDebuggingContext(C))
699     return;
700 
701   ExplodedNode *ErrNode = C.getPredecessor();
702   static CheckerProgramPointTag Tag("NonLocalizedStringChecker",
703                                     "UnlocalizedString");
704   ErrNode = C.addTransition(C.getState(), C.getPredecessor(), &Tag);
705 
706   if (!ErrNode)
707     return;
708 
709   // Generate the bug report.
710   std::unique_ptr<BugReport> R(new BugReport(
711       *BT, "User-facing text should use localized string macro", ErrNode));
712   if (argumentNumber) {
713     R->addRange(M.getArgExpr(argumentNumber - 1)->getSourceRange());
714   } else {
715     R->addRange(M.getSourceRange());
716   }
717   R->markInteresting(S);
718 
719   const MemRegion *StringRegion = S.getAsRegion();
720   if (StringRegion)
721     R->addVisitor(llvm::make_unique<NonLocalizedStringBRVisitor>(StringRegion));
722 
723   C.emitReport(std::move(R));
724 }
725 
726 /// Returns the argument number requiring localized string if it exists
727 /// otherwise, returns -1
728 int NonLocalizedStringChecker::getLocalizedArgumentForSelector(
729     const IdentifierInfo *Receiver, Selector S) const {
730   auto method = UIMethods.find(Receiver);
731 
732   if (method == UIMethods.end())
733     return -1;
734 
735   auto argumentIterator = method->getSecond().find(S);
736 
737   if (argumentIterator == method->getSecond().end())
738     return -1;
739 
740   int argumentNumber = argumentIterator->getSecond();
741   return argumentNumber;
742 }
743 
744 /// Check if the string being passed in has NonLocalized state
745 void NonLocalizedStringChecker::checkPreObjCMessage(const ObjCMethodCall &msg,
746                                                     CheckerContext &C) const {
747   initUIMethods(C.getASTContext());
748 
749   const ObjCInterfaceDecl *OD = msg.getReceiverInterface();
750   if (!OD)
751     return;
752   const IdentifierInfo *odInfo = OD->getIdentifier();
753 
754   Selector S = msg.getSelector();
755 
756   std::string SelectorString = S.getAsString();
757   StringRef SelectorName = SelectorString;
758   assert(!SelectorName.empty());
759 
760   if (odInfo->isStr("NSString")) {
761     // Handle the case where the receiver is an NSString
762     // These special NSString methods draw to the screen
763 
764     if (!(SelectorName.startswith("drawAtPoint") ||
765           SelectorName.startswith("drawInRect") ||
766           SelectorName.startswith("drawWithRect")))
767       return;
768 
769     SVal svTitle = msg.getReceiverSVal();
770 
771     bool isNonLocalized = hasNonLocalizedState(svTitle, C);
772 
773     if (isNonLocalized) {
774       reportLocalizationError(svTitle, msg, C);
775     }
776   }
777 
778   int argumentNumber = getLocalizedArgumentForSelector(odInfo, S);
779   // Go up each hierarchy of superclasses and their protocols
780   while (argumentNumber < 0 && OD->getSuperClass() != nullptr) {
781     for (const auto *P : OD->all_referenced_protocols()) {
782       argumentNumber = getLocalizedArgumentForSelector(P->getIdentifier(), S);
783       if (argumentNumber >= 0)
784         break;
785     }
786     if (argumentNumber < 0) {
787       OD = OD->getSuperClass();
788       argumentNumber = getLocalizedArgumentForSelector(OD->getIdentifier(), S);
789     }
790   }
791 
792   if (argumentNumber < 0) // There was no match in UIMethods
793     return;
794 
795   SVal svTitle = msg.getArgSVal(argumentNumber);
796 
797   if (const ObjCStringRegion *SR =
798           dyn_cast_or_null<ObjCStringRegion>(svTitle.getAsRegion())) {
799     StringRef stringValue =
800         SR->getObjCStringLiteral()->getString()->getString();
801     if ((stringValue.trim().size() == 0 && stringValue.size() > 0) ||
802         stringValue.empty())
803       return;
804     if (!IsAggressive && llvm::sys::unicode::columnWidthUTF8(stringValue) < 2)
805       return;
806   }
807 
808   bool isNonLocalized = hasNonLocalizedState(svTitle, C);
809 
810   if (isNonLocalized) {
811     reportLocalizationError(svTitle, msg, C, argumentNumber + 1);
812   }
813 }
814 
815 static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
816 
817   const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
818   if (!PT)
819     return false;
820 
821   ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
822   if (!Cls)
823     return false;
824 
825   IdentifierInfo *ClsName = Cls->getIdentifier();
826 
827   // FIXME: Should we walk the chain of classes?
828   return ClsName == &Ctx.Idents.get("NSString") ||
829          ClsName == &Ctx.Idents.get("NSMutableString");
830 }
831 
832 /// Marks a string being returned by any call as localized
833 /// if it is in LocStringFunctions (LSF) or the function is annotated.
834 /// Otherwise, we mark it as NonLocalized (Aggressive) or
835 /// NonLocalized only if it is not backed by a SymRegion (Non-Aggressive),
836 /// basically leaving only string literals as NonLocalized.
837 void NonLocalizedStringChecker::checkPostCall(const CallEvent &Call,
838                                               CheckerContext &C) const {
839   initLocStringsMethods(C.getASTContext());
840 
841   if (!Call.getOriginExpr())
842     return;
843 
844   // Anything that takes in a localized NSString as an argument
845   // and returns an NSString will be assumed to be returning a
846   // localized NSString. (Counter: Incorrectly combining two LocalizedStrings)
847   const QualType RT = Call.getResultType();
848   if (isNSStringType(RT, C.getASTContext())) {
849     for (unsigned i = 0; i < Call.getNumArgs(); ++i) {
850       SVal argValue = Call.getArgSVal(i);
851       if (hasLocalizedState(argValue, C)) {
852         SVal sv = Call.getReturnValue();
853         setLocalizedState(sv, C);
854         return;
855       }
856     }
857   }
858 
859   const Decl *D = Call.getDecl();
860   if (!D)
861     return;
862 
863   const IdentifierInfo *Identifier = Call.getCalleeIdentifier();
864 
865   SVal sv = Call.getReturnValue();
866   if (isAnnotatedAsLocalized(D) || LSF.count(Identifier) != 0) {
867     setLocalizedState(sv, C);
868   } else if (isNSStringType(RT, C.getASTContext()) &&
869              !hasLocalizedState(sv, C)) {
870     if (IsAggressive) {
871       setNonLocalizedState(sv, C);
872     } else {
873       const SymbolicRegion *SymReg =
874           dyn_cast_or_null<SymbolicRegion>(sv.getAsRegion());
875       if (!SymReg)
876         setNonLocalizedState(sv, C);
877     }
878   }
879 }
880 
881 /// Marks a string being returned by an ObjC method as localized
882 /// if it is in LocStringMethods or the method is annotated
883 void NonLocalizedStringChecker::checkPostObjCMessage(const ObjCMethodCall &msg,
884                                                      CheckerContext &C) const {
885   initLocStringsMethods(C.getASTContext());
886 
887   if (!msg.isInstanceMessage())
888     return;
889 
890   const ObjCInterfaceDecl *OD = msg.getReceiverInterface();
891   if (!OD)
892     return;
893   const IdentifierInfo *odInfo = OD->getIdentifier();
894 
895   Selector S = msg.getSelector();
896   std::string SelectorName = S.getAsString();
897 
898   std::pair<const IdentifierInfo *, Selector> MethodDescription = {odInfo, S};
899 
900   if (LSM.count(MethodDescription) || isAnnotatedAsLocalized(msg.getDecl())) {
901     SVal sv = msg.getReturnValue();
902     setLocalizedState(sv, C);
903   }
904 }
905 
906 /// Marks all empty string literals as localized
907 void NonLocalizedStringChecker::checkPostStmt(const ObjCStringLiteral *SL,
908                                               CheckerContext &C) const {
909   SVal sv = C.getSVal(SL);
910   setNonLocalizedState(sv, C);
911 }
912 
913 PathDiagnosticPiece *
914 NonLocalizedStringBRVisitor::VisitNode(const ExplodedNode *Succ,
915                                        const ExplodedNode *Pred,
916                                        BugReporterContext &BRC, BugReport &BR) {
917   if (Satisfied)
918     return nullptr;
919 
920   Optional<StmtPoint> Point = Succ->getLocation().getAs<StmtPoint>();
921   if (!Point.hasValue())
922     return nullptr;
923 
924   auto *LiteralExpr = dyn_cast<ObjCStringLiteral>(Point->getStmt());
925   if (!LiteralExpr)
926     return nullptr;
927 
928   ProgramStateRef State = Succ->getState();
929   SVal LiteralSVal = State->getSVal(LiteralExpr, Succ->getLocationContext());
930   if (LiteralSVal.getAsRegion() != NonLocalizedString)
931     return nullptr;
932 
933   Satisfied = true;
934 
935   PathDiagnosticLocation L =
936       PathDiagnosticLocation::create(*Point, BRC.getSourceManager());
937 
938   if (!L.isValid() || !L.asLocation().isValid())
939     return nullptr;
940 
941   auto *Piece = new PathDiagnosticEventPiece(L,
942       "Non-localized string literal here");
943   Piece->addRange(LiteralExpr->getSourceRange());
944 
945   return Piece;
946 }
947 
948 namespace {
949 class EmptyLocalizationContextChecker
950     : public Checker<check::ASTDecl<ObjCImplementationDecl>> {
951 
952   // A helper class, which walks the AST
953   class MethodCrawler : public ConstStmtVisitor<MethodCrawler> {
954     const ObjCMethodDecl *MD;
955     BugReporter &BR;
956     AnalysisManager &Mgr;
957     const CheckerBase *Checker;
958     LocationOrAnalysisDeclContext DCtx;
959 
960   public:
961     MethodCrawler(const ObjCMethodDecl *InMD, BugReporter &InBR,
962                   const CheckerBase *Checker, AnalysisManager &InMgr,
963                   AnalysisDeclContext *InDCtx)
964         : MD(InMD), BR(InBR), Mgr(InMgr), Checker(Checker), DCtx(InDCtx) {}
965 
966     void VisitStmt(const Stmt *S) { VisitChildren(S); }
967 
968     void VisitObjCMessageExpr(const ObjCMessageExpr *ME);
969 
970     void reportEmptyContextError(const ObjCMessageExpr *M) const;
971 
972     void VisitChildren(const Stmt *S) {
973       for (const Stmt *Child : S->children()) {
974         if (Child)
975           this->Visit(Child);
976       }
977     }
978   };
979 
980 public:
981   void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager &Mgr,
982                     BugReporter &BR) const;
983 };
984 } // end anonymous namespace
985 
986 void EmptyLocalizationContextChecker::checkASTDecl(
987     const ObjCImplementationDecl *D, AnalysisManager &Mgr,
988     BugReporter &BR) const {
989 
990   for (const ObjCMethodDecl *M : D->methods()) {
991     AnalysisDeclContext *DCtx = Mgr.getAnalysisDeclContext(M);
992 
993     const Stmt *Body = M->getBody();
994     assert(Body);
995 
996     MethodCrawler MC(M->getCanonicalDecl(), BR, this, Mgr, DCtx);
997     MC.VisitStmt(Body);
998   }
999 }
1000 
1001 /// This check attempts to match these macros, assuming they are defined as
1002 /// follows:
1003 ///
1004 /// #define NSLocalizedString(key, comment) \
1005 /// [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]
1006 /// #define NSLocalizedStringFromTable(key, tbl, comment) \
1007 /// [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:(tbl)]
1008 /// #define NSLocalizedStringFromTableInBundle(key, tbl, bundle, comment) \
1009 /// [bundle localizedStringForKey:(key) value:@"" table:(tbl)]
1010 /// #define NSLocalizedStringWithDefaultValue(key, tbl, bundle, val, comment)
1011 ///
1012 /// We cannot use the path sensitive check because the macro argument we are
1013 /// checking for (comment) is not used and thus not present in the AST,
1014 /// so we use Lexer on the original macro call and retrieve the value of
1015 /// the comment. If it's empty or nil, we raise a warning.
1016 void EmptyLocalizationContextChecker::MethodCrawler::VisitObjCMessageExpr(
1017     const ObjCMessageExpr *ME) {
1018 
1019   const ObjCInterfaceDecl *OD = ME->getReceiverInterface();
1020   if (!OD)
1021     return;
1022 
1023   const IdentifierInfo *odInfo = OD->getIdentifier();
1024 
1025   if (!(odInfo->isStr("NSBundle") &&
1026         ME->getSelector().getAsString() ==
1027             "localizedStringForKey:value:table:")) {
1028     return;
1029   }
1030 
1031   SourceRange R = ME->getSourceRange();
1032   if (!R.getBegin().isMacroID())
1033     return;
1034 
1035   // getImmediateMacroCallerLoc gets the location of the immediate macro
1036   // caller, one level up the stack toward the initial macro typed into the
1037   // source, so SL should point to the NSLocalizedString macro.
1038   SourceLocation SL =
1039       Mgr.getSourceManager().getImmediateMacroCallerLoc(R.getBegin());
1040   std::pair<FileID, unsigned> SLInfo =
1041       Mgr.getSourceManager().getDecomposedLoc(SL);
1042 
1043   SrcMgr::SLocEntry SE = Mgr.getSourceManager().getSLocEntry(SLInfo.first);
1044 
1045   // If NSLocalizedString macro is wrapped in another macro, we need to
1046   // unwrap the expansion until we get to the NSLocalizedStringMacro.
1047   while (SE.isExpansion()) {
1048     SL = SE.getExpansion().getSpellingLoc();
1049     SLInfo = Mgr.getSourceManager().getDecomposedLoc(SL);
1050     SE = Mgr.getSourceManager().getSLocEntry(SLInfo.first);
1051   }
1052 
1053   llvm::MemoryBuffer *BF = SE.getFile().getContentCache()->getRawBuffer();
1054   Lexer TheLexer(SL, LangOptions(), BF->getBufferStart(),
1055                  BF->getBufferStart() + SLInfo.second, BF->getBufferEnd());
1056 
1057   Token I;
1058   Token Result;    // This will hold the token just before the last ')'
1059   int p_count = 0; // This is for parenthesis matching
1060   while (!TheLexer.LexFromRawLexer(I)) {
1061     if (I.getKind() == tok::l_paren)
1062       ++p_count;
1063     if (I.getKind() == tok::r_paren) {
1064       if (p_count == 1)
1065         break;
1066       --p_count;
1067     }
1068     Result = I;
1069   }
1070 
1071   if (isAnyIdentifier(Result.getKind())) {
1072     if (Result.getRawIdentifier().equals("nil")) {
1073       reportEmptyContextError(ME);
1074       return;
1075     }
1076   }
1077 
1078   if (!isStringLiteral(Result.getKind()))
1079     return;
1080 
1081   StringRef Comment =
1082       StringRef(Result.getLiteralData(), Result.getLength()).trim('"');
1083 
1084   if ((Comment.trim().size() == 0 && Comment.size() > 0) || // Is Whitespace
1085       Comment.empty()) {
1086     reportEmptyContextError(ME);
1087   }
1088 }
1089 
1090 void EmptyLocalizationContextChecker::MethodCrawler::reportEmptyContextError(
1091     const ObjCMessageExpr *ME) const {
1092   // Generate the bug report.
1093   BR.EmitBasicReport(MD, Checker, "Context Missing",
1094                      "Localizability Issue (Apple)",
1095                      "Localized string macro should include a non-empty "
1096                      "comment for translators",
1097                      PathDiagnosticLocation(ME, BR.getSourceManager(), DCtx));
1098 }
1099 
1100 namespace {
1101 class PluralMisuseChecker : public Checker<check::ASTCodeBody> {
1102 
1103   // A helper class, which walks the AST
1104   class MethodCrawler : public RecursiveASTVisitor<MethodCrawler> {
1105     BugReporter &BR;
1106     const CheckerBase *Checker;
1107     AnalysisDeclContext *AC;
1108 
1109     // This functions like a stack. We push on any IfStmt or
1110     // ConditionalOperator that matches the condition
1111     // and pop it off when we leave that statement
1112     llvm::SmallVector<const clang::Stmt *, 8> MatchingStatements;
1113     // This is true when we are the direct-child of a
1114     // matching statement
1115     bool InMatchingStatement = false;
1116 
1117   public:
1118     explicit MethodCrawler(BugReporter &InBR, const CheckerBase *Checker,
1119                            AnalysisDeclContext *InAC)
1120         : BR(InBR), Checker(Checker), AC(InAC) {}
1121 
1122     bool VisitIfStmt(const IfStmt *I);
1123     bool EndVisitIfStmt(IfStmt *I);
1124     bool TraverseIfStmt(IfStmt *x);
1125     bool VisitConditionalOperator(const ConditionalOperator *C);
1126     bool TraverseConditionalOperator(ConditionalOperator *C);
1127     bool VisitCallExpr(const CallExpr *CE);
1128     bool VisitObjCMessageExpr(const ObjCMessageExpr *ME);
1129 
1130   private:
1131     void reportPluralMisuseError(const Stmt *S) const;
1132     bool isCheckingPlurality(const Expr *E) const;
1133   };
1134 
1135 public:
1136   void checkASTCodeBody(const Decl *D, AnalysisManager &Mgr,
1137                         BugReporter &BR) const {
1138     MethodCrawler Visitor(BR, this, Mgr.getAnalysisDeclContext(D));
1139     Visitor.TraverseDecl(const_cast<Decl *>(D));
1140   }
1141 };
1142 } // end anonymous namespace
1143 
1144 // Checks the condition of the IfStmt and returns true if one
1145 // of the following heuristics are met:
1146 // 1) The conidtion is a variable with "singular" or "plural" in the name
1147 // 2) The condition is a binary operator with 1 or 2 on the right-hand side
1148 bool PluralMisuseChecker::MethodCrawler::isCheckingPlurality(
1149     const Expr *Condition) const {
1150   const BinaryOperator *BO = nullptr;
1151   // Accounts for when a VarDecl represents a BinaryOperator
1152   if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Condition)) {
1153     if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
1154       const Expr *InitExpr = VD->getInit();
1155       if (InitExpr) {
1156         if (const BinaryOperator *B =
1157                 dyn_cast<BinaryOperator>(InitExpr->IgnoreParenImpCasts())) {
1158           BO = B;
1159         }
1160       }
1161       if (VD->getName().lower().find("plural") != StringRef::npos ||
1162           VD->getName().lower().find("singular") != StringRef::npos) {
1163         return true;
1164       }
1165     }
1166   } else if (const BinaryOperator *B = dyn_cast<BinaryOperator>(Condition)) {
1167     BO = B;
1168   }
1169 
1170   if (BO == nullptr)
1171     return false;
1172 
1173   if (IntegerLiteral *IL = dyn_cast_or_null<IntegerLiteral>(
1174           BO->getRHS()->IgnoreParenImpCasts())) {
1175     llvm::APInt Value = IL->getValue();
1176     if (Value == 1 || Value == 2) {
1177       return true;
1178     }
1179   }
1180   return false;
1181 }
1182 
1183 // A CallExpr with "LOC" in its identifier that takes in a string literal
1184 // has been shown to almost always be a function that returns a localized
1185 // string. Raise a diagnostic when this is in a statement that matches
1186 // the condition.
1187 bool PluralMisuseChecker::MethodCrawler::VisitCallExpr(const CallExpr *CE) {
1188   if (InMatchingStatement) {
1189     if (const FunctionDecl *FD = CE->getDirectCallee()) {
1190       std::string NormalizedName =
1191           StringRef(FD->getNameInfo().getAsString()).lower();
1192       if (NormalizedName.find("loc") != std::string::npos) {
1193         for (const Expr *Arg : CE->arguments()) {
1194           if (isa<ObjCStringLiteral>(Arg))
1195             reportPluralMisuseError(CE);
1196         }
1197       }
1198     }
1199   }
1200   return true;
1201 }
1202 
1203 // The other case is for NSLocalizedString which also returns
1204 // a localized string. It's a macro for the ObjCMessageExpr
1205 // [NSBundle localizedStringForKey:value:table:] Raise a
1206 // diagnostic when this is in a statement that matches
1207 // the condition.
1208 bool PluralMisuseChecker::MethodCrawler::VisitObjCMessageExpr(
1209     const ObjCMessageExpr *ME) {
1210   const ObjCInterfaceDecl *OD = ME->getReceiverInterface();
1211   if (!OD)
1212     return true;
1213 
1214   const IdentifierInfo *odInfo = OD->getIdentifier();
1215 
1216   if (odInfo->isStr("NSBundle") &&
1217       ME->getSelector().getAsString() == "localizedStringForKey:value:table:") {
1218     if (InMatchingStatement) {
1219       reportPluralMisuseError(ME);
1220     }
1221   }
1222   return true;
1223 }
1224 
1225 /// Override TraverseIfStmt so we know when we are done traversing an IfStmt
1226 bool PluralMisuseChecker::MethodCrawler::TraverseIfStmt(IfStmt *I) {
1227   RecursiveASTVisitor<MethodCrawler>::TraverseIfStmt(I);
1228   return EndVisitIfStmt(I);
1229 }
1230 
1231 // EndVisit callbacks are not provided by the RecursiveASTVisitor
1232 // so we override TraverseIfStmt and make a call to EndVisitIfStmt
1233 // after traversing the IfStmt
1234 bool PluralMisuseChecker::MethodCrawler::EndVisitIfStmt(IfStmt *I) {
1235   MatchingStatements.pop_back();
1236   if (!MatchingStatements.empty()) {
1237     if (MatchingStatements.back() != nullptr) {
1238       InMatchingStatement = true;
1239       return true;
1240     }
1241   }
1242   InMatchingStatement = false;
1243   return true;
1244 }
1245 
1246 bool PluralMisuseChecker::MethodCrawler::VisitIfStmt(const IfStmt *I) {
1247   const Expr *Condition = I->getCond()->IgnoreParenImpCasts();
1248   if (isCheckingPlurality(Condition)) {
1249     MatchingStatements.push_back(I);
1250     InMatchingStatement = true;
1251   } else {
1252     MatchingStatements.push_back(nullptr);
1253     InMatchingStatement = false;
1254   }
1255 
1256   return true;
1257 }
1258 
1259 // Preliminary support for conditional operators.
1260 bool PluralMisuseChecker::MethodCrawler::TraverseConditionalOperator(
1261     ConditionalOperator *C) {
1262   RecursiveASTVisitor<MethodCrawler>::TraverseConditionalOperator(C);
1263   MatchingStatements.pop_back();
1264   if (!MatchingStatements.empty()) {
1265     if (MatchingStatements.back() != nullptr)
1266       InMatchingStatement = true;
1267     else
1268       InMatchingStatement = false;
1269   } else {
1270     InMatchingStatement = false;
1271   }
1272   return true;
1273 }
1274 
1275 bool PluralMisuseChecker::MethodCrawler::VisitConditionalOperator(
1276     const ConditionalOperator *C) {
1277   const Expr *Condition = C->getCond()->IgnoreParenImpCasts();
1278   if (isCheckingPlurality(Condition)) {
1279     MatchingStatements.push_back(C);
1280     InMatchingStatement = true;
1281   } else {
1282     MatchingStatements.push_back(nullptr);
1283     InMatchingStatement = false;
1284   }
1285   return true;
1286 }
1287 
1288 void PluralMisuseChecker::MethodCrawler::reportPluralMisuseError(
1289     const Stmt *S) const {
1290   // Generate the bug report.
1291   BR.EmitBasicReport(AC->getDecl(), Checker, "Plural Misuse",
1292                      "Localizability Issue (Apple)",
1293                      "Plural cases are not supported accross all languages. "
1294                      "Use a .stringsdict file instead",
1295                      PathDiagnosticLocation(S, BR.getSourceManager(), AC));
1296 }
1297 
1298 //===----------------------------------------------------------------------===//
1299 // Checker registration.
1300 //===----------------------------------------------------------------------===//
1301 
1302 void ento::registerNonLocalizedStringChecker(CheckerManager &mgr) {
1303   NonLocalizedStringChecker *checker =
1304       mgr.registerChecker<NonLocalizedStringChecker>();
1305   checker->IsAggressive =
1306       mgr.getAnalyzerOptions().getBooleanOption("AggressiveReport", false);
1307 }
1308 
1309 void ento::registerEmptyLocalizationContextChecker(CheckerManager &mgr) {
1310   mgr.registerChecker<EmptyLocalizationContextChecker>();
1311 }
1312 
1313 void ento::registerPluralMisuseChecker(CheckerManager &mgr) {
1314   mgr.registerChecker<PluralMisuseChecker>();
1315 }
1316