RewriteObjCFoundationAPI.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  1. //===--- RewriteObjCFoundationAPI.cpp - Foundation API Rewriter -----------===//
  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. // Rewrites legacy method calls to modern syntax.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Edit/Rewriters.h"
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/ExprCXX.h"
  16. #include "clang/AST/ExprObjC.h"
  17. #include "clang/AST/NSAPI.h"
  18. #include "clang/AST/ParentMap.h"
  19. #include "clang/Edit/Commit.h"
  20. #include "clang/Lex/Lexer.h"
  21. using namespace clang;
  22. using namespace edit;
  23. static bool checkForLiteralCreation(const ObjCMessageExpr *Msg,
  24. IdentifierInfo *&ClassId,
  25. const LangOptions &LangOpts) {
  26. if (!Msg || Msg->isImplicit() || !Msg->getMethodDecl())
  27. return false;
  28. const ObjCInterfaceDecl *Receiver = Msg->getReceiverInterface();
  29. if (!Receiver)
  30. return false;
  31. ClassId = Receiver->getIdentifier();
  32. if (Msg->getReceiverKind() == ObjCMessageExpr::Class)
  33. return true;
  34. // When in ARC mode we also convert "[[.. alloc] init]" messages to literals,
  35. // since the change from +1 to +0 will be handled fine by ARC.
  36. if (LangOpts.ObjCAutoRefCount) {
  37. if (Msg->getReceiverKind() == ObjCMessageExpr::Instance) {
  38. if (const ObjCMessageExpr *Rec = dyn_cast<ObjCMessageExpr>(
  39. Msg->getInstanceReceiver()->IgnoreParenImpCasts())) {
  40. if (Rec->getMethodFamily() == OMF_alloc)
  41. return true;
  42. }
  43. }
  44. }
  45. return false;
  46. }
  47. //===----------------------------------------------------------------------===//
  48. // rewriteObjCRedundantCallWithLiteral.
  49. //===----------------------------------------------------------------------===//
  50. bool edit::rewriteObjCRedundantCallWithLiteral(const ObjCMessageExpr *Msg,
  51. const NSAPI &NS, Commit &commit) {
  52. IdentifierInfo *II = 0;
  53. if (!checkForLiteralCreation(Msg, II, NS.getASTContext().getLangOpts()))
  54. return false;
  55. if (Msg->getNumArgs() != 1)
  56. return false;
  57. const Expr *Arg = Msg->getArg(0)->IgnoreParenImpCasts();
  58. Selector Sel = Msg->getSelector();
  59. if ((isa<ObjCStringLiteral>(Arg) &&
  60. NS.getNSClassId(NSAPI::ClassId_NSString) == II &&
  61. (NS.getNSStringSelector(NSAPI::NSStr_stringWithString) == Sel ||
  62. NS.getNSStringSelector(NSAPI::NSStr_initWithString) == Sel)) ||
  63. (isa<ObjCArrayLiteral>(Arg) &&
  64. NS.getNSClassId(NSAPI::ClassId_NSArray) == II &&
  65. (NS.getNSArraySelector(NSAPI::NSArr_arrayWithArray) == Sel ||
  66. NS.getNSArraySelector(NSAPI::NSArr_initWithArray) == Sel)) ||
  67. (isa<ObjCDictionaryLiteral>(Arg) &&
  68. NS.getNSClassId(NSAPI::ClassId_NSDictionary) == II &&
  69. (NS.getNSDictionarySelector(
  70. NSAPI::NSDict_dictionaryWithDictionary) == Sel ||
  71. NS.getNSDictionarySelector(NSAPI::NSDict_initWithDictionary) == Sel))) {
  72. commit.replaceWithInner(Msg->getSourceRange(),
  73. Msg->getArg(0)->getSourceRange());
  74. return true;
  75. }
  76. return false;
  77. }
  78. //===----------------------------------------------------------------------===//
  79. // rewriteToObjCSubscriptSyntax.
  80. //===----------------------------------------------------------------------===//
  81. /// \brief Check for classes that accept 'objectForKey:' (or the other selectors
  82. /// that the migrator handles) but return their instances as 'id', resulting
  83. /// in the compiler resolving 'objectForKey:' as the method from NSDictionary.
  84. ///
  85. /// When checking if we can convert to subscripting syntax, check whether
  86. /// the receiver is a result of a class method from a hardcoded list of
  87. /// such classes. In such a case return the specific class as the interface
  88. /// of the receiver.
  89. ///
  90. /// FIXME: Remove this when these classes start using 'instancetype'.
  91. static const ObjCInterfaceDecl *
  92. maybeAdjustInterfaceForSubscriptingCheck(const ObjCInterfaceDecl *IFace,
  93. const Expr *Receiver,
  94. ASTContext &Ctx) {
  95. assert(IFace && Receiver);
  96. // If the receiver has type 'id'...
  97. if (!Ctx.isObjCIdType(Receiver->getType().getUnqualifiedType()))
  98. return IFace;
  99. const ObjCMessageExpr *
  100. InnerMsg = dyn_cast<ObjCMessageExpr>(Receiver->IgnoreParenCasts());
  101. if (!InnerMsg)
  102. return IFace;
  103. QualType ClassRec;
  104. switch (InnerMsg->getReceiverKind()) {
  105. case ObjCMessageExpr::Instance:
  106. case ObjCMessageExpr::SuperInstance:
  107. return IFace;
  108. case ObjCMessageExpr::Class:
  109. ClassRec = InnerMsg->getClassReceiver();
  110. break;
  111. case ObjCMessageExpr::SuperClass:
  112. ClassRec = InnerMsg->getSuperType();
  113. break;
  114. }
  115. if (ClassRec.isNull())
  116. return IFace;
  117. // ...and it is the result of a class message...
  118. const ObjCObjectType *ObjTy = ClassRec->getAs<ObjCObjectType>();
  119. if (!ObjTy)
  120. return IFace;
  121. const ObjCInterfaceDecl *OID = ObjTy->getInterface();
  122. // ...and the receiving class is NSMapTable or NSLocale, return that
  123. // class as the receiving interface.
  124. if (OID->getName() == "NSMapTable" ||
  125. OID->getName() == "NSLocale")
  126. return OID;
  127. return IFace;
  128. }
  129. static bool canRewriteToSubscriptSyntax(const ObjCInterfaceDecl *&IFace,
  130. const ObjCMessageExpr *Msg,
  131. ASTContext &Ctx,
  132. Selector subscriptSel) {
  133. const Expr *Rec = Msg->getInstanceReceiver();
  134. if (!Rec)
  135. return false;
  136. IFace = maybeAdjustInterfaceForSubscriptingCheck(IFace, Rec, Ctx);
  137. if (const ObjCMethodDecl *MD = IFace->lookupInstanceMethod(subscriptSel)) {
  138. if (!MD->isUnavailable())
  139. return true;
  140. }
  141. return false;
  142. }
  143. static bool subscriptOperatorNeedsParens(const Expr *FullExpr);
  144. static void maybePutParensOnReceiver(const Expr *Receiver, Commit &commit) {
  145. if (subscriptOperatorNeedsParens(Receiver)) {
  146. SourceRange RecRange = Receiver->getSourceRange();
  147. commit.insertWrap("(", RecRange, ")");
  148. }
  149. }
  150. static bool rewriteToSubscriptGetCommon(const ObjCMessageExpr *Msg,
  151. Commit &commit) {
  152. if (Msg->getNumArgs() != 1)
  153. return false;
  154. const Expr *Rec = Msg->getInstanceReceiver();
  155. if (!Rec)
  156. return false;
  157. SourceRange MsgRange = Msg->getSourceRange();
  158. SourceRange RecRange = Rec->getSourceRange();
  159. SourceRange ArgRange = Msg->getArg(0)->getSourceRange();
  160. commit.replaceWithInner(CharSourceRange::getCharRange(MsgRange.getBegin(),
  161. ArgRange.getBegin()),
  162. CharSourceRange::getTokenRange(RecRange));
  163. commit.replaceWithInner(SourceRange(ArgRange.getBegin(), MsgRange.getEnd()),
  164. ArgRange);
  165. commit.insertWrap("[", ArgRange, "]");
  166. maybePutParensOnReceiver(Rec, commit);
  167. return true;
  168. }
  169. static bool rewriteToArraySubscriptGet(const ObjCInterfaceDecl *IFace,
  170. const ObjCMessageExpr *Msg,
  171. const NSAPI &NS,
  172. Commit &commit) {
  173. if (!canRewriteToSubscriptSyntax(IFace, Msg, NS.getASTContext(),
  174. NS.getObjectAtIndexedSubscriptSelector()))
  175. return false;
  176. return rewriteToSubscriptGetCommon(Msg, commit);
  177. }
  178. static bool rewriteToDictionarySubscriptGet(const ObjCInterfaceDecl *IFace,
  179. const ObjCMessageExpr *Msg,
  180. const NSAPI &NS,
  181. Commit &commit) {
  182. if (!canRewriteToSubscriptSyntax(IFace, Msg, NS.getASTContext(),
  183. NS.getObjectForKeyedSubscriptSelector()))
  184. return false;
  185. return rewriteToSubscriptGetCommon(Msg, commit);
  186. }
  187. static bool rewriteToArraySubscriptSet(const ObjCInterfaceDecl *IFace,
  188. const ObjCMessageExpr *Msg,
  189. const NSAPI &NS,
  190. Commit &commit) {
  191. if (!canRewriteToSubscriptSyntax(IFace, Msg, NS.getASTContext(),
  192. NS.getSetObjectAtIndexedSubscriptSelector()))
  193. return false;
  194. if (Msg->getNumArgs() != 2)
  195. return false;
  196. const Expr *Rec = Msg->getInstanceReceiver();
  197. if (!Rec)
  198. return false;
  199. SourceRange MsgRange = Msg->getSourceRange();
  200. SourceRange RecRange = Rec->getSourceRange();
  201. SourceRange Arg0Range = Msg->getArg(0)->getSourceRange();
  202. SourceRange Arg1Range = Msg->getArg(1)->getSourceRange();
  203. commit.replaceWithInner(CharSourceRange::getCharRange(MsgRange.getBegin(),
  204. Arg0Range.getBegin()),
  205. CharSourceRange::getTokenRange(RecRange));
  206. commit.replaceWithInner(CharSourceRange::getCharRange(Arg0Range.getBegin(),
  207. Arg1Range.getBegin()),
  208. CharSourceRange::getTokenRange(Arg0Range));
  209. commit.replaceWithInner(SourceRange(Arg1Range.getBegin(), MsgRange.getEnd()),
  210. Arg1Range);
  211. commit.insertWrap("[", CharSourceRange::getCharRange(Arg0Range.getBegin(),
  212. Arg1Range.getBegin()),
  213. "] = ");
  214. maybePutParensOnReceiver(Rec, commit);
  215. return true;
  216. }
  217. static bool rewriteToDictionarySubscriptSet(const ObjCInterfaceDecl *IFace,
  218. const ObjCMessageExpr *Msg,
  219. const NSAPI &NS,
  220. Commit &commit) {
  221. if (!canRewriteToSubscriptSyntax(IFace, Msg, NS.getASTContext(),
  222. NS.getSetObjectForKeyedSubscriptSelector()))
  223. return false;
  224. if (Msg->getNumArgs() != 2)
  225. return false;
  226. const Expr *Rec = Msg->getInstanceReceiver();
  227. if (!Rec)
  228. return false;
  229. SourceRange MsgRange = Msg->getSourceRange();
  230. SourceRange RecRange = Rec->getSourceRange();
  231. SourceRange Arg0Range = Msg->getArg(0)->getSourceRange();
  232. SourceRange Arg1Range = Msg->getArg(1)->getSourceRange();
  233. SourceLocation LocBeforeVal = Arg0Range.getBegin();
  234. commit.insertBefore(LocBeforeVal, "] = ");
  235. commit.insertFromRange(LocBeforeVal, Arg1Range, /*afterToken=*/false,
  236. /*beforePreviousInsertions=*/true);
  237. commit.insertBefore(LocBeforeVal, "[");
  238. commit.replaceWithInner(CharSourceRange::getCharRange(MsgRange.getBegin(),
  239. Arg0Range.getBegin()),
  240. CharSourceRange::getTokenRange(RecRange));
  241. commit.replaceWithInner(SourceRange(Arg0Range.getBegin(), MsgRange.getEnd()),
  242. Arg0Range);
  243. maybePutParensOnReceiver(Rec, commit);
  244. return true;
  245. }
  246. bool edit::rewriteToObjCSubscriptSyntax(const ObjCMessageExpr *Msg,
  247. const NSAPI &NS, Commit &commit) {
  248. if (!Msg || Msg->isImplicit() ||
  249. Msg->getReceiverKind() != ObjCMessageExpr::Instance)
  250. return false;
  251. const ObjCMethodDecl *Method = Msg->getMethodDecl();
  252. if (!Method)
  253. return false;
  254. const ObjCInterfaceDecl *IFace =
  255. NS.getASTContext().getObjContainingInterface(Method);
  256. if (!IFace)
  257. return false;
  258. Selector Sel = Msg->getSelector();
  259. if (Sel == NS.getNSArraySelector(NSAPI::NSArr_objectAtIndex))
  260. return rewriteToArraySubscriptGet(IFace, Msg, NS, commit);
  261. if (Sel == NS.getNSDictionarySelector(NSAPI::NSDict_objectForKey))
  262. return rewriteToDictionarySubscriptGet(IFace, Msg, NS, commit);
  263. if (Msg->getNumArgs() != 2)
  264. return false;
  265. if (Sel == NS.getNSArraySelector(NSAPI::NSMutableArr_replaceObjectAtIndex))
  266. return rewriteToArraySubscriptSet(IFace, Msg, NS, commit);
  267. if (Sel == NS.getNSDictionarySelector(NSAPI::NSMutableDict_setObjectForKey))
  268. return rewriteToDictionarySubscriptSet(IFace, Msg, NS, commit);
  269. return false;
  270. }
  271. //===----------------------------------------------------------------------===//
  272. // rewriteToObjCLiteralSyntax.
  273. //===----------------------------------------------------------------------===//
  274. static bool rewriteToArrayLiteral(const ObjCMessageExpr *Msg,
  275. const NSAPI &NS, Commit &commit,
  276. const ParentMap *PMap);
  277. static bool rewriteToDictionaryLiteral(const ObjCMessageExpr *Msg,
  278. const NSAPI &NS, Commit &commit);
  279. static bool rewriteToNumberLiteral(const ObjCMessageExpr *Msg,
  280. const NSAPI &NS, Commit &commit);
  281. static bool rewriteToNumericBoxedExpression(const ObjCMessageExpr *Msg,
  282. const NSAPI &NS, Commit &commit);
  283. static bool rewriteToStringBoxedExpression(const ObjCMessageExpr *Msg,
  284. const NSAPI &NS, Commit &commit);
  285. bool edit::rewriteToObjCLiteralSyntax(const ObjCMessageExpr *Msg,
  286. const NSAPI &NS, Commit &commit,
  287. const ParentMap *PMap) {
  288. IdentifierInfo *II = 0;
  289. if (!checkForLiteralCreation(Msg, II, NS.getASTContext().getLangOpts()))
  290. return false;
  291. if (II == NS.getNSClassId(NSAPI::ClassId_NSArray))
  292. return rewriteToArrayLiteral(Msg, NS, commit, PMap);
  293. if (II == NS.getNSClassId(NSAPI::ClassId_NSDictionary))
  294. return rewriteToDictionaryLiteral(Msg, NS, commit);
  295. if (II == NS.getNSClassId(NSAPI::ClassId_NSNumber))
  296. return rewriteToNumberLiteral(Msg, NS, commit);
  297. if (II == NS.getNSClassId(NSAPI::ClassId_NSString))
  298. return rewriteToStringBoxedExpression(Msg, NS, commit);
  299. return false;
  300. }
  301. /// \brief Returns true if the immediate message arguments of \c Msg should not
  302. /// be rewritten because it will interfere with the rewrite of the parent
  303. /// message expression. e.g.
  304. /// \code
  305. /// [NSDictionary dictionaryWithObjects:
  306. /// [NSArray arrayWithObjects:@"1", @"2", nil]
  307. /// forKeys:[NSArray arrayWithObjects:@"A", @"B", nil]];
  308. /// \endcode
  309. /// It will return true for this because we are going to rewrite this directly
  310. /// to a dictionary literal without any array literals.
  311. static bool shouldNotRewriteImmediateMessageArgs(const ObjCMessageExpr *Msg,
  312. const NSAPI &NS);
  313. //===----------------------------------------------------------------------===//
  314. // rewriteToArrayLiteral.
  315. //===----------------------------------------------------------------------===//
  316. /// \brief Adds an explicit cast to 'id' if the type is not objc object.
  317. static void objectifyExpr(const Expr *E, Commit &commit);
  318. static bool rewriteToArrayLiteral(const ObjCMessageExpr *Msg,
  319. const NSAPI &NS, Commit &commit,
  320. const ParentMap *PMap) {
  321. if (PMap) {
  322. const ObjCMessageExpr *ParentMsg =
  323. dyn_cast_or_null<ObjCMessageExpr>(PMap->getParentIgnoreParenCasts(Msg));
  324. if (shouldNotRewriteImmediateMessageArgs(ParentMsg, NS))
  325. return false;
  326. }
  327. Selector Sel = Msg->getSelector();
  328. SourceRange MsgRange = Msg->getSourceRange();
  329. if (Sel == NS.getNSArraySelector(NSAPI::NSArr_array)) {
  330. if (Msg->getNumArgs() != 0)
  331. return false;
  332. commit.replace(MsgRange, "@[]");
  333. return true;
  334. }
  335. if (Sel == NS.getNSArraySelector(NSAPI::NSArr_arrayWithObject)) {
  336. if (Msg->getNumArgs() != 1)
  337. return false;
  338. objectifyExpr(Msg->getArg(0), commit);
  339. SourceRange ArgRange = Msg->getArg(0)->getSourceRange();
  340. commit.replaceWithInner(MsgRange, ArgRange);
  341. commit.insertWrap("@[", ArgRange, "]");
  342. return true;
  343. }
  344. if (Sel == NS.getNSArraySelector(NSAPI::NSArr_arrayWithObjects) ||
  345. Sel == NS.getNSArraySelector(NSAPI::NSArr_initWithObjects)) {
  346. if (Msg->getNumArgs() == 0)
  347. return false;
  348. const Expr *SentinelExpr = Msg->getArg(Msg->getNumArgs() - 1);
  349. if (!NS.getASTContext().isSentinelNullExpr(SentinelExpr))
  350. return false;
  351. for (unsigned i = 0, e = Msg->getNumArgs() - 1; i != e; ++i)
  352. objectifyExpr(Msg->getArg(i), commit);
  353. if (Msg->getNumArgs() == 1) {
  354. commit.replace(MsgRange, "@[]");
  355. return true;
  356. }
  357. SourceRange ArgRange(Msg->getArg(0)->getLocStart(),
  358. Msg->getArg(Msg->getNumArgs()-2)->getLocEnd());
  359. commit.replaceWithInner(MsgRange, ArgRange);
  360. commit.insertWrap("@[", ArgRange, "]");
  361. return true;
  362. }
  363. return false;
  364. }
  365. //===----------------------------------------------------------------------===//
  366. // rewriteToDictionaryLiteral.
  367. //===----------------------------------------------------------------------===//
  368. /// \brief If \c Msg is an NSArray creation message or literal, this gets the
  369. /// objects that were used to create it.
  370. /// \returns true if it is an NSArray and we got objects, or false otherwise.
  371. static bool getNSArrayObjects(const Expr *E, const NSAPI &NS,
  372. SmallVectorImpl<const Expr *> &Objs) {
  373. if (!E)
  374. return false;
  375. E = E->IgnoreParenCasts();
  376. if (!E)
  377. return false;
  378. if (const ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) {
  379. IdentifierInfo *Cls = 0;
  380. if (!checkForLiteralCreation(Msg, Cls, NS.getASTContext().getLangOpts()))
  381. return false;
  382. if (Cls != NS.getNSClassId(NSAPI::ClassId_NSArray))
  383. return false;
  384. Selector Sel = Msg->getSelector();
  385. if (Sel == NS.getNSArraySelector(NSAPI::NSArr_array))
  386. return true; // empty array.
  387. if (Sel == NS.getNSArraySelector(NSAPI::NSArr_arrayWithObject)) {
  388. if (Msg->getNumArgs() != 1)
  389. return false;
  390. Objs.push_back(Msg->getArg(0));
  391. return true;
  392. }
  393. if (Sel == NS.getNSArraySelector(NSAPI::NSArr_arrayWithObjects) ||
  394. Sel == NS.getNSArraySelector(NSAPI::NSArr_initWithObjects)) {
  395. if (Msg->getNumArgs() == 0)
  396. return false;
  397. const Expr *SentinelExpr = Msg->getArg(Msg->getNumArgs() - 1);
  398. if (!NS.getASTContext().isSentinelNullExpr(SentinelExpr))
  399. return false;
  400. for (unsigned i = 0, e = Msg->getNumArgs() - 1; i != e; ++i)
  401. Objs.push_back(Msg->getArg(i));
  402. return true;
  403. }
  404. } else if (const ObjCArrayLiteral *ArrLit = dyn_cast<ObjCArrayLiteral>(E)) {
  405. for (unsigned i = 0, e = ArrLit->getNumElements(); i != e; ++i)
  406. Objs.push_back(ArrLit->getElement(i));
  407. return true;
  408. }
  409. return false;
  410. }
  411. static bool rewriteToDictionaryLiteral(const ObjCMessageExpr *Msg,
  412. const NSAPI &NS, Commit &commit) {
  413. Selector Sel = Msg->getSelector();
  414. SourceRange MsgRange = Msg->getSourceRange();
  415. if (Sel == NS.getNSDictionarySelector(NSAPI::NSDict_dictionary)) {
  416. if (Msg->getNumArgs() != 0)
  417. return false;
  418. commit.replace(MsgRange, "@{}");
  419. return true;
  420. }
  421. if (Sel == NS.getNSDictionarySelector(
  422. NSAPI::NSDict_dictionaryWithObjectForKey)) {
  423. if (Msg->getNumArgs() != 2)
  424. return false;
  425. objectifyExpr(Msg->getArg(0), commit);
  426. objectifyExpr(Msg->getArg(1), commit);
  427. SourceRange ValRange = Msg->getArg(0)->getSourceRange();
  428. SourceRange KeyRange = Msg->getArg(1)->getSourceRange();
  429. // Insert key before the value.
  430. commit.insertBefore(ValRange.getBegin(), ": ");
  431. commit.insertFromRange(ValRange.getBegin(),
  432. CharSourceRange::getTokenRange(KeyRange),
  433. /*afterToken=*/false, /*beforePreviousInsertions=*/true);
  434. commit.insertBefore(ValRange.getBegin(), "@{");
  435. commit.insertAfterToken(ValRange.getEnd(), "}");
  436. commit.replaceWithInner(MsgRange, ValRange);
  437. return true;
  438. }
  439. if (Sel == NS.getNSDictionarySelector(
  440. NSAPI::NSDict_dictionaryWithObjectsAndKeys) ||
  441. Sel == NS.getNSDictionarySelector(NSAPI::NSDict_initWithObjectsAndKeys)) {
  442. if (Msg->getNumArgs() % 2 != 1)
  443. return false;
  444. unsigned SentinelIdx = Msg->getNumArgs() - 1;
  445. const Expr *SentinelExpr = Msg->getArg(SentinelIdx);
  446. if (!NS.getASTContext().isSentinelNullExpr(SentinelExpr))
  447. return false;
  448. if (Msg->getNumArgs() == 1) {
  449. commit.replace(MsgRange, "@{}");
  450. return true;
  451. }
  452. for (unsigned i = 0; i < SentinelIdx; i += 2) {
  453. objectifyExpr(Msg->getArg(i), commit);
  454. objectifyExpr(Msg->getArg(i+1), commit);
  455. SourceRange ValRange = Msg->getArg(i)->getSourceRange();
  456. SourceRange KeyRange = Msg->getArg(i+1)->getSourceRange();
  457. // Insert value after key.
  458. commit.insertAfterToken(KeyRange.getEnd(), ": ");
  459. commit.insertFromRange(KeyRange.getEnd(), ValRange, /*afterToken=*/true);
  460. commit.remove(CharSourceRange::getCharRange(ValRange.getBegin(),
  461. KeyRange.getBegin()));
  462. }
  463. // Range of arguments up until and including the last key.
  464. // The sentinel and first value are cut off, the value will move after the
  465. // key.
  466. SourceRange ArgRange(Msg->getArg(1)->getLocStart(),
  467. Msg->getArg(SentinelIdx-1)->getLocEnd());
  468. commit.insertWrap("@{", ArgRange, "}");
  469. commit.replaceWithInner(MsgRange, ArgRange);
  470. return true;
  471. }
  472. if (Sel == NS.getNSDictionarySelector(
  473. NSAPI::NSDict_dictionaryWithObjectsForKeys) ||
  474. Sel == NS.getNSDictionarySelector(NSAPI::NSDict_initWithObjectsForKeys)) {
  475. if (Msg->getNumArgs() != 2)
  476. return false;
  477. SmallVector<const Expr *, 8> Vals;
  478. if (!getNSArrayObjects(Msg->getArg(0), NS, Vals))
  479. return false;
  480. SmallVector<const Expr *, 8> Keys;
  481. if (!getNSArrayObjects(Msg->getArg(1), NS, Keys))
  482. return false;
  483. if (Vals.size() != Keys.size())
  484. return false;
  485. if (Vals.empty()) {
  486. commit.replace(MsgRange, "@{}");
  487. return true;
  488. }
  489. for (unsigned i = 0, n = Vals.size(); i < n; ++i) {
  490. objectifyExpr(Vals[i], commit);
  491. objectifyExpr(Keys[i], commit);
  492. SourceRange ValRange = Vals[i]->getSourceRange();
  493. SourceRange KeyRange = Keys[i]->getSourceRange();
  494. // Insert value after key.
  495. commit.insertAfterToken(KeyRange.getEnd(), ": ");
  496. commit.insertFromRange(KeyRange.getEnd(), ValRange, /*afterToken=*/true);
  497. }
  498. // Range of arguments up until and including the last key.
  499. // The first value is cut off, the value will move after the key.
  500. SourceRange ArgRange(Keys.front()->getLocStart(),
  501. Keys.back()->getLocEnd());
  502. commit.insertWrap("@{", ArgRange, "}");
  503. commit.replaceWithInner(MsgRange, ArgRange);
  504. return true;
  505. }
  506. return false;
  507. }
  508. static bool shouldNotRewriteImmediateMessageArgs(const ObjCMessageExpr *Msg,
  509. const NSAPI &NS) {
  510. if (!Msg)
  511. return false;
  512. IdentifierInfo *II = 0;
  513. if (!checkForLiteralCreation(Msg, II, NS.getASTContext().getLangOpts()))
  514. return false;
  515. if (II != NS.getNSClassId(NSAPI::ClassId_NSDictionary))
  516. return false;
  517. Selector Sel = Msg->getSelector();
  518. if (Sel == NS.getNSDictionarySelector(
  519. NSAPI::NSDict_dictionaryWithObjectsForKeys) ||
  520. Sel == NS.getNSDictionarySelector(NSAPI::NSDict_initWithObjectsForKeys)) {
  521. if (Msg->getNumArgs() != 2)
  522. return false;
  523. SmallVector<const Expr *, 8> Vals;
  524. if (!getNSArrayObjects(Msg->getArg(0), NS, Vals))
  525. return false;
  526. SmallVector<const Expr *, 8> Keys;
  527. if (!getNSArrayObjects(Msg->getArg(1), NS, Keys))
  528. return false;
  529. if (Vals.size() != Keys.size())
  530. return false;
  531. return true;
  532. }
  533. return false;
  534. }
  535. //===----------------------------------------------------------------------===//
  536. // rewriteToNumberLiteral.
  537. //===----------------------------------------------------------------------===//
  538. static bool rewriteToCharLiteral(const ObjCMessageExpr *Msg,
  539. const CharacterLiteral *Arg,
  540. const NSAPI &NS, Commit &commit) {
  541. if (Arg->getKind() != CharacterLiteral::Ascii)
  542. return false;
  543. if (NS.isNSNumberLiteralSelector(NSAPI::NSNumberWithChar,
  544. Msg->getSelector())) {
  545. SourceRange ArgRange = Arg->getSourceRange();
  546. commit.replaceWithInner(Msg->getSourceRange(), ArgRange);
  547. commit.insert(ArgRange.getBegin(), "@");
  548. return true;
  549. }
  550. return rewriteToNumericBoxedExpression(Msg, NS, commit);
  551. }
  552. static bool rewriteToBoolLiteral(const ObjCMessageExpr *Msg,
  553. const Expr *Arg,
  554. const NSAPI &NS, Commit &commit) {
  555. if (NS.isNSNumberLiteralSelector(NSAPI::NSNumberWithBool,
  556. Msg->getSelector())) {
  557. SourceRange ArgRange = Arg->getSourceRange();
  558. commit.replaceWithInner(Msg->getSourceRange(), ArgRange);
  559. commit.insert(ArgRange.getBegin(), "@");
  560. return true;
  561. }
  562. return rewriteToNumericBoxedExpression(Msg, NS, commit);
  563. }
  564. namespace {
  565. struct LiteralInfo {
  566. bool Hex, Octal;
  567. StringRef U, F, L, LL;
  568. CharSourceRange WithoutSuffRange;
  569. };
  570. }
  571. static bool getLiteralInfo(SourceRange literalRange,
  572. bool isFloat, bool isIntZero,
  573. ASTContext &Ctx, LiteralInfo &Info) {
  574. if (literalRange.getBegin().isMacroID() ||
  575. literalRange.getEnd().isMacroID())
  576. return false;
  577. StringRef text = Lexer::getSourceText(
  578. CharSourceRange::getTokenRange(literalRange),
  579. Ctx.getSourceManager(), Ctx.getLangOpts());
  580. if (text.empty())
  581. return false;
  582. Optional<bool> UpperU, UpperL;
  583. bool UpperF = false;
  584. struct Suff {
  585. static bool has(StringRef suff, StringRef &text) {
  586. if (text.endswith(suff)) {
  587. text = text.substr(0, text.size()-suff.size());
  588. return true;
  589. }
  590. return false;
  591. }
  592. };
  593. while (1) {
  594. if (Suff::has("u", text)) {
  595. UpperU = false;
  596. } else if (Suff::has("U", text)) {
  597. UpperU = true;
  598. } else if (Suff::has("ll", text)) {
  599. UpperL = false;
  600. } else if (Suff::has("LL", text)) {
  601. UpperL = true;
  602. } else if (Suff::has("l", text)) {
  603. UpperL = false;
  604. } else if (Suff::has("L", text)) {
  605. UpperL = true;
  606. } else if (isFloat && Suff::has("f", text)) {
  607. UpperF = false;
  608. } else if (isFloat && Suff::has("F", text)) {
  609. UpperF = true;
  610. } else
  611. break;
  612. }
  613. if (!UpperU.hasValue() && !UpperL.hasValue())
  614. UpperU = UpperL = true;
  615. else if (UpperU.hasValue() && !UpperL.hasValue())
  616. UpperL = UpperU;
  617. else if (UpperL.hasValue() && !UpperU.hasValue())
  618. UpperU = UpperL;
  619. Info.U = *UpperU ? "U" : "u";
  620. Info.L = *UpperL ? "L" : "l";
  621. Info.LL = *UpperL ? "LL" : "ll";
  622. Info.F = UpperF ? "F" : "f";
  623. Info.Hex = Info.Octal = false;
  624. if (text.startswith("0x"))
  625. Info.Hex = true;
  626. else if (!isFloat && !isIntZero && text.startswith("0"))
  627. Info.Octal = true;
  628. SourceLocation B = literalRange.getBegin();
  629. Info.WithoutSuffRange =
  630. CharSourceRange::getCharRange(B, B.getLocWithOffset(text.size()));
  631. return true;
  632. }
  633. static bool rewriteToNumberLiteral(const ObjCMessageExpr *Msg,
  634. const NSAPI &NS, Commit &commit) {
  635. if (Msg->getNumArgs() != 1)
  636. return false;
  637. const Expr *Arg = Msg->getArg(0)->IgnoreParenImpCasts();
  638. if (const CharacterLiteral *CharE = dyn_cast<CharacterLiteral>(Arg))
  639. return rewriteToCharLiteral(Msg, CharE, NS, commit);
  640. if (const ObjCBoolLiteralExpr *BE = dyn_cast<ObjCBoolLiteralExpr>(Arg))
  641. return rewriteToBoolLiteral(Msg, BE, NS, commit);
  642. if (const CXXBoolLiteralExpr *BE = dyn_cast<CXXBoolLiteralExpr>(Arg))
  643. return rewriteToBoolLiteral(Msg, BE, NS, commit);
  644. const Expr *literalE = Arg;
  645. if (const UnaryOperator *UOE = dyn_cast<UnaryOperator>(literalE)) {
  646. if (UOE->getOpcode() == UO_Plus || UOE->getOpcode() == UO_Minus)
  647. literalE = UOE->getSubExpr();
  648. }
  649. // Only integer and floating literals, otherwise try to rewrite to boxed
  650. // expression.
  651. if (!isa<IntegerLiteral>(literalE) && !isa<FloatingLiteral>(literalE))
  652. return rewriteToNumericBoxedExpression(Msg, NS, commit);
  653. ASTContext &Ctx = NS.getASTContext();
  654. Selector Sel = Msg->getSelector();
  655. Optional<NSAPI::NSNumberLiteralMethodKind>
  656. MKOpt = NS.getNSNumberLiteralMethodKind(Sel);
  657. if (!MKOpt)
  658. return false;
  659. NSAPI::NSNumberLiteralMethodKind MK = *MKOpt;
  660. bool CallIsUnsigned = false, CallIsLong = false, CallIsLongLong = false;
  661. bool CallIsFloating = false, CallIsDouble = false;
  662. switch (MK) {
  663. // We cannot have these calls with int/float literals.
  664. case NSAPI::NSNumberWithChar:
  665. case NSAPI::NSNumberWithUnsignedChar:
  666. case NSAPI::NSNumberWithShort:
  667. case NSAPI::NSNumberWithUnsignedShort:
  668. case NSAPI::NSNumberWithBool:
  669. return rewriteToNumericBoxedExpression(Msg, NS, commit);
  670. case NSAPI::NSNumberWithUnsignedInt:
  671. case NSAPI::NSNumberWithUnsignedInteger:
  672. CallIsUnsigned = true;
  673. case NSAPI::NSNumberWithInt:
  674. case NSAPI::NSNumberWithInteger:
  675. break;
  676. case NSAPI::NSNumberWithUnsignedLong:
  677. CallIsUnsigned = true;
  678. case NSAPI::NSNumberWithLong:
  679. CallIsLong = true;
  680. break;
  681. case NSAPI::NSNumberWithUnsignedLongLong:
  682. CallIsUnsigned = true;
  683. case NSAPI::NSNumberWithLongLong:
  684. CallIsLongLong = true;
  685. break;
  686. case NSAPI::NSNumberWithDouble:
  687. CallIsDouble = true;
  688. case NSAPI::NSNumberWithFloat:
  689. CallIsFloating = true;
  690. break;
  691. }
  692. SourceRange ArgRange = Arg->getSourceRange();
  693. QualType ArgTy = Arg->getType();
  694. QualType CallTy = Msg->getArg(0)->getType();
  695. // Check for the easy case, the literal maps directly to the call.
  696. if (Ctx.hasSameType(ArgTy, CallTy)) {
  697. commit.replaceWithInner(Msg->getSourceRange(), ArgRange);
  698. commit.insert(ArgRange.getBegin(), "@");
  699. return true;
  700. }
  701. // We will need to modify the literal suffix to get the same type as the call.
  702. // Try with boxed expression if it came from a macro.
  703. if (ArgRange.getBegin().isMacroID())
  704. return rewriteToNumericBoxedExpression(Msg, NS, commit);
  705. bool LitIsFloat = ArgTy->isFloatingType();
  706. // For a float passed to integer call, don't try rewriting to objc literal.
  707. // It is difficult and a very uncommon case anyway.
  708. // But try with boxed expression.
  709. if (LitIsFloat && !CallIsFloating)
  710. return rewriteToNumericBoxedExpression(Msg, NS, commit);
  711. // Try to modify the literal make it the same type as the method call.
  712. // -Modify the suffix, and/or
  713. // -Change integer to float
  714. LiteralInfo LitInfo;
  715. bool isIntZero = false;
  716. if (const IntegerLiteral *IntE = dyn_cast<IntegerLiteral>(literalE))
  717. isIntZero = !IntE->getValue().getBoolValue();
  718. if (!getLiteralInfo(ArgRange, LitIsFloat, isIntZero, Ctx, LitInfo))
  719. return rewriteToNumericBoxedExpression(Msg, NS, commit);
  720. // Not easy to do int -> float with hex/octal and uncommon anyway.
  721. if (!LitIsFloat && CallIsFloating && (LitInfo.Hex || LitInfo.Octal))
  722. return rewriteToNumericBoxedExpression(Msg, NS, commit);
  723. SourceLocation LitB = LitInfo.WithoutSuffRange.getBegin();
  724. SourceLocation LitE = LitInfo.WithoutSuffRange.getEnd();
  725. commit.replaceWithInner(CharSourceRange::getTokenRange(Msg->getSourceRange()),
  726. LitInfo.WithoutSuffRange);
  727. commit.insert(LitB, "@");
  728. if (!LitIsFloat && CallIsFloating)
  729. commit.insert(LitE, ".0");
  730. if (CallIsFloating) {
  731. if (!CallIsDouble)
  732. commit.insert(LitE, LitInfo.F);
  733. } else {
  734. if (CallIsUnsigned)
  735. commit.insert(LitE, LitInfo.U);
  736. if (CallIsLong)
  737. commit.insert(LitE, LitInfo.L);
  738. else if (CallIsLongLong)
  739. commit.insert(LitE, LitInfo.LL);
  740. }
  741. return true;
  742. }
  743. // FIXME: Make determination of operator precedence more general and
  744. // make it broadly available.
  745. static bool subscriptOperatorNeedsParens(const Expr *FullExpr) {
  746. const Expr* Expr = FullExpr->IgnoreImpCasts();
  747. if (isa<ArraySubscriptExpr>(Expr) ||
  748. isa<CallExpr>(Expr) ||
  749. isa<DeclRefExpr>(Expr) ||
  750. isa<CXXNamedCastExpr>(Expr) ||
  751. isa<CXXConstructExpr>(Expr) ||
  752. isa<CXXThisExpr>(Expr) ||
  753. isa<CXXTypeidExpr>(Expr) ||
  754. isa<CXXUnresolvedConstructExpr>(Expr) ||
  755. isa<ObjCMessageExpr>(Expr) ||
  756. isa<ObjCPropertyRefExpr>(Expr) ||
  757. isa<ObjCProtocolExpr>(Expr) ||
  758. isa<MemberExpr>(Expr) ||
  759. isa<ObjCIvarRefExpr>(Expr) ||
  760. isa<ParenExpr>(FullExpr) ||
  761. isa<ParenListExpr>(Expr) ||
  762. isa<SizeOfPackExpr>(Expr))
  763. return false;
  764. return true;
  765. }
  766. static bool castOperatorNeedsParens(const Expr *FullExpr) {
  767. const Expr* Expr = FullExpr->IgnoreImpCasts();
  768. if (isa<ArraySubscriptExpr>(Expr) ||
  769. isa<CallExpr>(Expr) ||
  770. isa<DeclRefExpr>(Expr) ||
  771. isa<CastExpr>(Expr) ||
  772. isa<CXXNewExpr>(Expr) ||
  773. isa<CXXConstructExpr>(Expr) ||
  774. isa<CXXDeleteExpr>(Expr) ||
  775. isa<CXXNoexceptExpr>(Expr) ||
  776. isa<CXXPseudoDestructorExpr>(Expr) ||
  777. isa<CXXScalarValueInitExpr>(Expr) ||
  778. isa<CXXThisExpr>(Expr) ||
  779. isa<CXXTypeidExpr>(Expr) ||
  780. isa<CXXUnresolvedConstructExpr>(Expr) ||
  781. isa<ObjCMessageExpr>(Expr) ||
  782. isa<ObjCPropertyRefExpr>(Expr) ||
  783. isa<ObjCProtocolExpr>(Expr) ||
  784. isa<MemberExpr>(Expr) ||
  785. isa<ObjCIvarRefExpr>(Expr) ||
  786. isa<ParenExpr>(FullExpr) ||
  787. isa<ParenListExpr>(Expr) ||
  788. isa<SizeOfPackExpr>(Expr) ||
  789. isa<UnaryOperator>(Expr))
  790. return false;
  791. return true;
  792. }
  793. static void objectifyExpr(const Expr *E, Commit &commit) {
  794. if (!E) return;
  795. QualType T = E->getType();
  796. if (T->isObjCObjectPointerType()) {
  797. if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
  798. if (ICE->getCastKind() != CK_CPointerToObjCPointerCast)
  799. return;
  800. } else {
  801. return;
  802. }
  803. } else if (!T->isPointerType()) {
  804. return;
  805. }
  806. SourceRange Range = E->getSourceRange();
  807. if (castOperatorNeedsParens(E))
  808. commit.insertWrap("(", Range, ")");
  809. commit.insertBefore(Range.getBegin(), "(id)");
  810. }
  811. //===----------------------------------------------------------------------===//
  812. // rewriteToNumericBoxedExpression.
  813. //===----------------------------------------------------------------------===//
  814. static bool isEnumConstant(const Expr *E) {
  815. if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
  816. if (const ValueDecl *VD = DRE->getDecl())
  817. return isa<EnumConstantDecl>(VD);
  818. return false;
  819. }
  820. static bool rewriteToNumericBoxedExpression(const ObjCMessageExpr *Msg,
  821. const NSAPI &NS, Commit &commit) {
  822. if (Msg->getNumArgs() != 1)
  823. return false;
  824. const Expr *Arg = Msg->getArg(0);
  825. if (Arg->isTypeDependent())
  826. return false;
  827. ASTContext &Ctx = NS.getASTContext();
  828. Selector Sel = Msg->getSelector();
  829. Optional<NSAPI::NSNumberLiteralMethodKind>
  830. MKOpt = NS.getNSNumberLiteralMethodKind(Sel);
  831. if (!MKOpt)
  832. return false;
  833. NSAPI::NSNumberLiteralMethodKind MK = *MKOpt;
  834. const Expr *OrigArg = Arg->IgnoreImpCasts();
  835. QualType FinalTy = Arg->getType();
  836. QualType OrigTy = OrigArg->getType();
  837. uint64_t FinalTySize = Ctx.getTypeSize(FinalTy);
  838. uint64_t OrigTySize = Ctx.getTypeSize(OrigTy);
  839. bool isTruncated = FinalTySize < OrigTySize;
  840. bool needsCast = false;
  841. if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
  842. switch (ICE->getCastKind()) {
  843. case CK_LValueToRValue:
  844. case CK_NoOp:
  845. case CK_UserDefinedConversion:
  846. break;
  847. case CK_IntegralCast: {
  848. if (MK == NSAPI::NSNumberWithBool && OrigTy->isBooleanType())
  849. break;
  850. // Be more liberal with Integer/UnsignedInteger which are very commonly
  851. // used.
  852. if ((MK == NSAPI::NSNumberWithInteger ||
  853. MK == NSAPI::NSNumberWithUnsignedInteger) &&
  854. !isTruncated) {
  855. if (OrigTy->getAs<EnumType>() || isEnumConstant(OrigArg))
  856. break;
  857. if ((MK==NSAPI::NSNumberWithInteger) == OrigTy->isSignedIntegerType() &&
  858. OrigTySize >= Ctx.getTypeSize(Ctx.IntTy))
  859. break;
  860. }
  861. needsCast = true;
  862. break;
  863. }
  864. case CK_PointerToBoolean:
  865. case CK_IntegralToBoolean:
  866. case CK_IntegralToFloating:
  867. case CK_FloatingToIntegral:
  868. case CK_FloatingToBoolean:
  869. case CK_FloatingCast:
  870. case CK_FloatingComplexToReal:
  871. case CK_FloatingComplexToBoolean:
  872. case CK_IntegralComplexToReal:
  873. case CK_IntegralComplexToBoolean:
  874. case CK_AtomicToNonAtomic:
  875. needsCast = true;
  876. break;
  877. case CK_Dependent:
  878. case CK_BitCast:
  879. case CK_LValueBitCast:
  880. case CK_BaseToDerived:
  881. case CK_DerivedToBase:
  882. case CK_UncheckedDerivedToBase:
  883. case CK_Dynamic:
  884. case CK_ToUnion:
  885. case CK_ArrayToPointerDecay:
  886. case CK_FunctionToPointerDecay:
  887. case CK_NullToPointer:
  888. case CK_NullToMemberPointer:
  889. case CK_BaseToDerivedMemberPointer:
  890. case CK_DerivedToBaseMemberPointer:
  891. case CK_MemberPointerToBoolean:
  892. case CK_ReinterpretMemberPointer:
  893. case CK_ConstructorConversion:
  894. case CK_IntegralToPointer:
  895. case CK_PointerToIntegral:
  896. case CK_ToVoid:
  897. case CK_VectorSplat:
  898. case CK_CPointerToObjCPointerCast:
  899. case CK_BlockPointerToObjCPointerCast:
  900. case CK_AnyPointerToBlockPointerCast:
  901. case CK_ObjCObjectLValueCast:
  902. case CK_FloatingRealToComplex:
  903. case CK_FloatingComplexCast:
  904. case CK_FloatingComplexToIntegralComplex:
  905. case CK_IntegralRealToComplex:
  906. case CK_IntegralComplexCast:
  907. case CK_IntegralComplexToFloatingComplex:
  908. case CK_ARCProduceObject:
  909. case CK_ARCConsumeObject:
  910. case CK_ARCReclaimReturnedObject:
  911. case CK_ARCExtendBlockObject:
  912. case CK_NonAtomicToAtomic:
  913. case CK_CopyAndAutoreleaseBlockObject:
  914. case CK_BuiltinFnToFnPtr:
  915. case CK_ZeroToOCLEvent:
  916. return false;
  917. }
  918. }
  919. if (needsCast) {
  920. DiagnosticsEngine &Diags = Ctx.getDiagnostics();
  921. // FIXME: Use a custom category name to distinguish migration diagnostics.
  922. unsigned diagID = Diags.getCustomDiagID(DiagnosticsEngine::Warning,
  923. "converting to boxing syntax requires casting %0 to %1");
  924. Diags.Report(Msg->getExprLoc(), diagID) << OrigTy << FinalTy
  925. << Msg->getSourceRange();
  926. return false;
  927. }
  928. SourceRange ArgRange = OrigArg->getSourceRange();
  929. commit.replaceWithInner(Msg->getSourceRange(), ArgRange);
  930. if (isa<ParenExpr>(OrigArg) || isa<IntegerLiteral>(OrigArg))
  931. commit.insertBefore(ArgRange.getBegin(), "@");
  932. else
  933. commit.insertWrap("@(", ArgRange, ")");
  934. return true;
  935. }
  936. //===----------------------------------------------------------------------===//
  937. // rewriteToStringBoxedExpression.
  938. //===----------------------------------------------------------------------===//
  939. static bool doRewriteToUTF8StringBoxedExpressionHelper(
  940. const ObjCMessageExpr *Msg,
  941. const NSAPI &NS, Commit &commit) {
  942. const Expr *Arg = Msg->getArg(0);
  943. if (Arg->isTypeDependent())
  944. return false;
  945. ASTContext &Ctx = NS.getASTContext();
  946. const Expr *OrigArg = Arg->IgnoreImpCasts();
  947. QualType OrigTy = OrigArg->getType();
  948. if (OrigTy->isArrayType())
  949. OrigTy = Ctx.getArrayDecayedType(OrigTy);
  950. if (const StringLiteral *
  951. StrE = dyn_cast<StringLiteral>(OrigArg->IgnoreParens())) {
  952. commit.replaceWithInner(Msg->getSourceRange(), StrE->getSourceRange());
  953. commit.insert(StrE->getLocStart(), "@");
  954. return true;
  955. }
  956. if (const PointerType *PT = OrigTy->getAs<PointerType>()) {
  957. QualType PointeeType = PT->getPointeeType();
  958. if (Ctx.hasSameUnqualifiedType(PointeeType, Ctx.CharTy)) {
  959. SourceRange ArgRange = OrigArg->getSourceRange();
  960. commit.replaceWithInner(Msg->getSourceRange(), ArgRange);
  961. if (isa<ParenExpr>(OrigArg) || isa<IntegerLiteral>(OrigArg))
  962. commit.insertBefore(ArgRange.getBegin(), "@");
  963. else
  964. commit.insertWrap("@(", ArgRange, ")");
  965. return true;
  966. }
  967. }
  968. return false;
  969. }
  970. static bool rewriteToStringBoxedExpression(const ObjCMessageExpr *Msg,
  971. const NSAPI &NS, Commit &commit) {
  972. Selector Sel = Msg->getSelector();
  973. if (Sel == NS.getNSStringSelector(NSAPI::NSStr_stringWithUTF8String) ||
  974. Sel == NS.getNSStringSelector(NSAPI::NSStr_stringWithCString)) {
  975. if (Msg->getNumArgs() != 1)
  976. return false;
  977. return doRewriteToUTF8StringBoxedExpressionHelper(Msg, NS, commit);
  978. }
  979. if (Sel == NS.getNSStringSelector(NSAPI::NSStr_stringWithCStringEncoding)) {
  980. if (Msg->getNumArgs() != 2)
  981. return false;
  982. const Expr *encodingArg = Msg->getArg(1);
  983. if (NS.isNSUTF8StringEncodingConstant(encodingArg) ||
  984. NS.isNSASCIIStringEncodingConstant(encodingArg))
  985. return doRewriteToUTF8StringBoxedExpressionHelper(Msg, NS, commit);
  986. }
  987. return false;
  988. }