LocalizationChecker.cpp 52 KB

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