xref: /freebsd/contrib/llvm-project/clang/lib/ARCMigrate/ObjCMT.cpp (revision 9bc300465e48e19d794d88d0c158a2adb92c7197)
1 //===--- ObjCMT.cpp - ObjC Migrate Tool -----------------------------------===//
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 #include "Transforms.h"
10 #include "clang/Analysis/RetainSummaryManager.h"
11 #include "clang/ARCMigrate/ARCMT.h"
12 #include "clang/ARCMigrate/ARCMTActions.h"
13 #include "clang/AST/ASTConsumer.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/Attr.h"
16 #include "clang/AST/NSAPI.h"
17 #include "clang/AST/ParentMap.h"
18 #include "clang/AST/RecursiveASTVisitor.h"
19 #include "clang/Analysis/DomainSpecific/CocoaConventions.h"
20 #include "clang/Basic/FileManager.h"
21 #include "clang/Edit/Commit.h"
22 #include "clang/Edit/EditedSource.h"
23 #include "clang/Edit/EditsReceiver.h"
24 #include "clang/Edit/Rewriters.h"
25 #include "clang/Frontend/CompilerInstance.h"
26 #include "clang/Frontend/MultiplexConsumer.h"
27 #include "clang/Lex/PPConditionalDirectiveRecord.h"
28 #include "clang/Lex/Preprocessor.h"
29 #include "clang/Rewrite/Core/Rewriter.h"
30 #include "llvm/ADT/SmallString.h"
31 #include "llvm/ADT/StringSet.h"
32 #include "llvm/Support/Path.h"
33 #include "llvm/Support/SourceMgr.h"
34 #include "llvm/Support/YAMLParser.h"
35 
36 using namespace clang;
37 using namespace arcmt;
38 using namespace ento;
39 
40 namespace {
41 
42 class ObjCMigrateASTConsumer : public ASTConsumer {
43   enum CF_BRIDGING_KIND {
44     CF_BRIDGING_NONE,
45     CF_BRIDGING_ENABLE,
46     CF_BRIDGING_MAY_INCLUDE
47   };
48 
49   void migrateDecl(Decl *D);
50   void migrateObjCContainerDecl(ASTContext &Ctx, ObjCContainerDecl *D);
51   void migrateProtocolConformance(ASTContext &Ctx,
52                                   const ObjCImplementationDecl *ImpDecl);
53   void CacheObjCNSIntegerTypedefed(const TypedefDecl *TypedefDcl);
54   bool migrateNSEnumDecl(ASTContext &Ctx, const EnumDecl *EnumDcl,
55                      const TypedefDecl *TypedefDcl);
56   void migrateAllMethodInstaceType(ASTContext &Ctx, ObjCContainerDecl *CDecl);
57   void migrateMethodInstanceType(ASTContext &Ctx, ObjCContainerDecl *CDecl,
58                                  ObjCMethodDecl *OM);
59   bool migrateProperty(ASTContext &Ctx, ObjCContainerDecl *D, ObjCMethodDecl *OM);
60   void migrateNsReturnsInnerPointer(ASTContext &Ctx, ObjCMethodDecl *OM);
61   void migratePropertyNsReturnsInnerPointer(ASTContext &Ctx, ObjCPropertyDecl *P);
62   void migrateFactoryMethod(ASTContext &Ctx, ObjCContainerDecl *CDecl,
63                             ObjCMethodDecl *OM,
64                             ObjCInstanceTypeFamily OIT_Family = OIT_None);
65 
66   void migrateCFAnnotation(ASTContext &Ctx, const Decl *Decl);
67   void AddCFAnnotations(ASTContext &Ctx,
68                         const RetainSummary *RS,
69                         const FunctionDecl *FuncDecl, bool ResultAnnotated);
70   void AddCFAnnotations(ASTContext &Ctx,
71                         const RetainSummary *RS,
72                         const ObjCMethodDecl *MethodDecl, bool ResultAnnotated);
73 
74   void AnnotateImplicitBridging(ASTContext &Ctx);
75 
76   CF_BRIDGING_KIND migrateAddFunctionAnnotation(ASTContext &Ctx,
77                                                 const FunctionDecl *FuncDecl);
78 
79   void migrateARCSafeAnnotation(ASTContext &Ctx, ObjCContainerDecl *CDecl);
80 
81   void migrateAddMethodAnnotation(ASTContext &Ctx,
82                                   const ObjCMethodDecl *MethodDecl);
83 
84   void inferDesignatedInitializers(ASTContext &Ctx,
85                                    const ObjCImplementationDecl *ImplD);
86 
87   bool InsertFoundation(ASTContext &Ctx, SourceLocation Loc);
88 
89   std::unique_ptr<RetainSummaryManager> Summaries;
90 
91 public:
92   std::string MigrateDir;
93   unsigned ASTMigrateActions;
94   FileID FileId;
95   const TypedefDecl *NSIntegerTypedefed;
96   const TypedefDecl *NSUIntegerTypedefed;
97   std::unique_ptr<NSAPI> NSAPIObj;
98   std::unique_ptr<edit::EditedSource> Editor;
99   FileRemapper &Remapper;
100   FileManager &FileMgr;
101   const PPConditionalDirectiveRecord *PPRec;
102   Preprocessor &PP;
103   bool IsOutputFile;
104   bool FoundationIncluded;
105   llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ObjCProtocolDecls;
106   llvm::SmallVector<const Decl *, 8> CFFunctionIBCandidates;
107   llvm::StringSet<> AllowListFilenames;
108 
109   RetainSummaryManager &getSummaryManager(ASTContext &Ctx) {
110     if (!Summaries)
111       Summaries.reset(new RetainSummaryManager(Ctx,
112                                                /*TrackNSCFObjects=*/true,
113                                                /*trackOSObjects=*/false));
114     return *Summaries;
115   }
116 
117   ObjCMigrateASTConsumer(StringRef migrateDir, unsigned astMigrateActions,
118                          FileRemapper &remapper, FileManager &fileMgr,
119                          const PPConditionalDirectiveRecord *PPRec,
120                          Preprocessor &PP, bool isOutputFile,
121                          ArrayRef<std::string> AllowList)
122       : MigrateDir(migrateDir), ASTMigrateActions(astMigrateActions),
123         NSIntegerTypedefed(nullptr), NSUIntegerTypedefed(nullptr),
124         Remapper(remapper), FileMgr(fileMgr), PPRec(PPRec), PP(PP),
125         IsOutputFile(isOutputFile), FoundationIncluded(false) {
126     AllowListFilenames.insert(AllowList.begin(), AllowList.end());
127   }
128 
129 protected:
130   void Initialize(ASTContext &Context) override {
131     NSAPIObj.reset(new NSAPI(Context));
132     Editor.reset(new edit::EditedSource(Context.getSourceManager(),
133                                         Context.getLangOpts(),
134                                         PPRec));
135   }
136 
137   bool HandleTopLevelDecl(DeclGroupRef DG) override {
138     for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
139       migrateDecl(*I);
140     return true;
141   }
142   void HandleInterestingDecl(DeclGroupRef DG) override {
143     // Ignore decls from the PCH.
144   }
145   void HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) override {
146     ObjCMigrateASTConsumer::HandleTopLevelDecl(DG);
147   }
148 
149   void HandleTranslationUnit(ASTContext &Ctx) override;
150 
151   bool canModifyFile(StringRef Path) {
152     if (AllowListFilenames.empty())
153       return true;
154     return AllowListFilenames.contains(llvm::sys::path::filename(Path));
155   }
156   bool canModifyFile(OptionalFileEntryRef FE) {
157     if (!FE)
158       return false;
159     return canModifyFile(FE->getName());
160   }
161   bool canModifyFile(FileID FID) {
162     if (FID.isInvalid())
163       return false;
164     return canModifyFile(PP.getSourceManager().getFileEntryRefForID(FID));
165   }
166 
167   bool canModify(const Decl *D) {
168     if (!D)
169       return false;
170     if (const ObjCCategoryImplDecl *CatImpl = dyn_cast<ObjCCategoryImplDecl>(D))
171       return canModify(CatImpl->getCategoryDecl());
172     if (const ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D))
173       return canModify(Impl->getClassInterface());
174     if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
175       return canModify(cast<Decl>(MD->getDeclContext()));
176 
177     FileID FID = PP.getSourceManager().getFileID(D->getLocation());
178     return canModifyFile(FID);
179   }
180 };
181 
182 } // end anonymous namespace
183 
184 ObjCMigrateAction::ObjCMigrateAction(
185     std::unique_ptr<FrontendAction> WrappedAction, StringRef migrateDir,
186     unsigned migrateAction)
187     : WrapperFrontendAction(std::move(WrappedAction)), MigrateDir(migrateDir),
188       ObjCMigAction(migrateAction), CompInst(nullptr) {
189   if (MigrateDir.empty())
190     MigrateDir = "."; // user current directory if none is given.
191 }
192 
193 std::unique_ptr<ASTConsumer>
194 ObjCMigrateAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
195   PPConditionalDirectiveRecord *
196     PPRec = new PPConditionalDirectiveRecord(CompInst->getSourceManager());
197   CI.getPreprocessor().addPPCallbacks(std::unique_ptr<PPCallbacks>(PPRec));
198   std::vector<std::unique_ptr<ASTConsumer>> Consumers;
199   Consumers.push_back(WrapperFrontendAction::CreateASTConsumer(CI, InFile));
200   Consumers.push_back(std::make_unique<ObjCMigrateASTConsumer>(
201       MigrateDir, ObjCMigAction, Remapper, CompInst->getFileManager(), PPRec,
202       CompInst->getPreprocessor(), false, std::nullopt));
203   return std::make_unique<MultiplexConsumer>(std::move(Consumers));
204 }
205 
206 bool ObjCMigrateAction::BeginInvocation(CompilerInstance &CI) {
207   Remapper.initFromDisk(MigrateDir, CI.getDiagnostics(),
208                         /*ignoreIfFilesChanged=*/true);
209   CompInst = &CI;
210   CI.getDiagnostics().setIgnoreAllWarnings(true);
211   return true;
212 }
213 
214 namespace {
215   // FIXME. This duplicates one in RewriteObjCFoundationAPI.cpp
216   bool subscriptOperatorNeedsParens(const Expr *FullExpr) {
217     const Expr* Expr = FullExpr->IgnoreImpCasts();
218     return !(isa<ArraySubscriptExpr>(Expr) || isa<CallExpr>(Expr) ||
219              isa<DeclRefExpr>(Expr) || isa<CXXNamedCastExpr>(Expr) ||
220              isa<CXXConstructExpr>(Expr) || isa<CXXThisExpr>(Expr) ||
221              isa<CXXTypeidExpr>(Expr) ||
222              isa<CXXUnresolvedConstructExpr>(Expr) ||
223              isa<ObjCMessageExpr>(Expr) || isa<ObjCPropertyRefExpr>(Expr) ||
224              isa<ObjCProtocolExpr>(Expr) || isa<MemberExpr>(Expr) ||
225              isa<ObjCIvarRefExpr>(Expr) || isa<ParenExpr>(FullExpr) ||
226              isa<ParenListExpr>(Expr) || isa<SizeOfPackExpr>(Expr));
227   }
228 
229   /// - Rewrite message expression for Objective-C setter and getters into
230   /// property-dot syntax.
231   bool rewriteToPropertyDotSyntax(const ObjCMessageExpr *Msg,
232                                   Preprocessor &PP,
233                                   const NSAPI &NS, edit::Commit &commit,
234                                   const ParentMap *PMap) {
235     if (!Msg || Msg->isImplicit() ||
236         (Msg->getReceiverKind() != ObjCMessageExpr::Instance &&
237          Msg->getReceiverKind() != ObjCMessageExpr::SuperInstance))
238       return false;
239     if (const Expr *Receiver = Msg->getInstanceReceiver())
240       if (Receiver->getType()->isObjCBuiltinType())
241         return false;
242 
243     const ObjCMethodDecl *Method = Msg->getMethodDecl();
244     if (!Method)
245       return false;
246     if (!Method->isPropertyAccessor())
247       return false;
248 
249     const ObjCPropertyDecl *Prop = Method->findPropertyDecl();
250     if (!Prop)
251       return false;
252 
253     SourceRange MsgRange = Msg->getSourceRange();
254     bool ReceiverIsSuper =
255       (Msg->getReceiverKind() == ObjCMessageExpr::SuperInstance);
256     // for 'super' receiver is nullptr.
257     const Expr *receiver = Msg->getInstanceReceiver();
258     bool NeedsParen =
259       ReceiverIsSuper ? false : subscriptOperatorNeedsParens(receiver);
260     bool IsGetter = (Msg->getNumArgs() == 0);
261     if (IsGetter) {
262       // Find space location range between receiver expression and getter method.
263       SourceLocation BegLoc =
264           ReceiverIsSuper ? Msg->getSuperLoc() : receiver->getEndLoc();
265       BegLoc = PP.getLocForEndOfToken(BegLoc);
266       SourceLocation EndLoc = Msg->getSelectorLoc(0);
267       SourceRange SpaceRange(BegLoc, EndLoc);
268       std::string PropertyDotString;
269       // rewrite getter method expression into: receiver.property or
270       // (receiver).property
271       if (NeedsParen) {
272         commit.insertBefore(receiver->getBeginLoc(), "(");
273         PropertyDotString = ").";
274       }
275       else
276         PropertyDotString = ".";
277       PropertyDotString += Prop->getName();
278       commit.replace(SpaceRange, PropertyDotString);
279 
280       // remove '[' ']'
281       commit.replace(SourceRange(MsgRange.getBegin(), MsgRange.getBegin()), "");
282       commit.replace(SourceRange(MsgRange.getEnd(), MsgRange.getEnd()), "");
283     } else {
284       if (NeedsParen)
285         commit.insertWrap("(", receiver->getSourceRange(), ")");
286       std::string PropertyDotString = ".";
287       PropertyDotString += Prop->getName();
288       PropertyDotString += " =";
289       const Expr*const* Args = Msg->getArgs();
290       const Expr *RHS = Args[0];
291       if (!RHS)
292         return false;
293       SourceLocation BegLoc =
294           ReceiverIsSuper ? Msg->getSuperLoc() : receiver->getEndLoc();
295       BegLoc = PP.getLocForEndOfToken(BegLoc);
296       SourceLocation EndLoc = RHS->getBeginLoc();
297       EndLoc = EndLoc.getLocWithOffset(-1);
298       const char *colon = PP.getSourceManager().getCharacterData(EndLoc);
299       // Add a space after '=' if there is no space between RHS and '='
300       if (colon && colon[0] == ':')
301         PropertyDotString += " ";
302       SourceRange Range(BegLoc, EndLoc);
303       commit.replace(Range, PropertyDotString);
304       // remove '[' ']'
305       commit.replace(SourceRange(MsgRange.getBegin(), MsgRange.getBegin()), "");
306       commit.replace(SourceRange(MsgRange.getEnd(), MsgRange.getEnd()), "");
307     }
308     return true;
309   }
310 
311 class ObjCMigrator : public RecursiveASTVisitor<ObjCMigrator> {
312   ObjCMigrateASTConsumer &Consumer;
313   ParentMap &PMap;
314 
315 public:
316   ObjCMigrator(ObjCMigrateASTConsumer &consumer, ParentMap &PMap)
317     : Consumer(consumer), PMap(PMap) { }
318 
319   bool shouldVisitTemplateInstantiations() const { return false; }
320   bool shouldWalkTypesOfTypeLocs() const { return false; }
321 
322   bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
323     if (Consumer.ASTMigrateActions & FrontendOptions::ObjCMT_Literals) {
324       edit::Commit commit(*Consumer.Editor);
325       edit::rewriteToObjCLiteralSyntax(E, *Consumer.NSAPIObj, commit, &PMap);
326       Consumer.Editor->commit(commit);
327     }
328 
329     if (Consumer.ASTMigrateActions & FrontendOptions::ObjCMT_Subscripting) {
330       edit::Commit commit(*Consumer.Editor);
331       edit::rewriteToObjCSubscriptSyntax(E, *Consumer.NSAPIObj, commit);
332       Consumer.Editor->commit(commit);
333     }
334 
335     if (Consumer.ASTMigrateActions & FrontendOptions::ObjCMT_PropertyDotSyntax) {
336       edit::Commit commit(*Consumer.Editor);
337       rewriteToPropertyDotSyntax(E, Consumer.PP, *Consumer.NSAPIObj,
338                                  commit, &PMap);
339       Consumer.Editor->commit(commit);
340     }
341 
342     return true;
343   }
344 
345   bool TraverseObjCMessageExpr(ObjCMessageExpr *E) {
346     // Do depth first; we want to rewrite the subexpressions first so that if
347     // we have to move expressions we will move them already rewritten.
348     for (Stmt *SubStmt : E->children())
349       if (!TraverseStmt(SubStmt))
350         return false;
351 
352     return WalkUpFromObjCMessageExpr(E);
353   }
354 };
355 
356 class BodyMigrator : public RecursiveASTVisitor<BodyMigrator> {
357   ObjCMigrateASTConsumer &Consumer;
358   std::unique_ptr<ParentMap> PMap;
359 
360 public:
361   BodyMigrator(ObjCMigrateASTConsumer &consumer) : Consumer(consumer) { }
362 
363   bool shouldVisitTemplateInstantiations() const { return false; }
364   bool shouldWalkTypesOfTypeLocs() const { return false; }
365 
366   bool TraverseStmt(Stmt *S) {
367     PMap.reset(new ParentMap(S));
368     ObjCMigrator(Consumer, *PMap).TraverseStmt(S);
369     return true;
370   }
371 };
372 } // end anonymous namespace
373 
374 void ObjCMigrateASTConsumer::migrateDecl(Decl *D) {
375   if (!D)
376     return;
377   if (isa<ObjCMethodDecl>(D))
378     return; // Wait for the ObjC container declaration.
379 
380   BodyMigrator(*this).TraverseDecl(D);
381 }
382 
383 static void append_attr(std::string &PropertyString, const char *attr,
384                         bool &LParenAdded) {
385   if (!LParenAdded) {
386     PropertyString += "(";
387     LParenAdded = true;
388   }
389   else
390     PropertyString += ", ";
391   PropertyString += attr;
392 }
393 
394 static
395 void MigrateBlockOrFunctionPointerTypeVariable(std::string & PropertyString,
396                                                const std::string& TypeString,
397                                                const char *name) {
398   const char *argPtr = TypeString.c_str();
399   int paren = 0;
400   while (*argPtr) {
401     switch (*argPtr) {
402       case '(':
403         PropertyString += *argPtr;
404         paren++;
405         break;
406       case ')':
407         PropertyString += *argPtr;
408         paren--;
409         break;
410       case '^':
411       case '*':
412         PropertyString += (*argPtr);
413         if (paren == 1) {
414           PropertyString += name;
415           name = "";
416         }
417         break;
418       default:
419         PropertyString += *argPtr;
420         break;
421     }
422     argPtr++;
423   }
424 }
425 
426 static const char *PropertyMemoryAttribute(ASTContext &Context, QualType ArgType) {
427   Qualifiers::ObjCLifetime propertyLifetime = ArgType.getObjCLifetime();
428   bool RetainableObject = ArgType->isObjCRetainableType();
429   if (RetainableObject &&
430       (propertyLifetime == Qualifiers::OCL_Strong
431        || propertyLifetime == Qualifiers::OCL_None)) {
432     if (const ObjCObjectPointerType *ObjPtrTy =
433         ArgType->getAs<ObjCObjectPointerType>()) {
434       ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
435       if (IDecl &&
436           IDecl->lookupNestedProtocol(&Context.Idents.get("NSCopying")))
437         return "copy";
438       else
439         return "strong";
440     }
441     else if (ArgType->isBlockPointerType())
442       return "copy";
443   } else if (propertyLifetime == Qualifiers::OCL_Weak)
444     // TODO. More precise determination of 'weak' attribute requires
445     // looking into setter's implementation for backing weak ivar.
446     return "weak";
447   else if (RetainableObject)
448     return ArgType->isBlockPointerType() ? "copy" : "strong";
449   return nullptr;
450 }
451 
452 static void rewriteToObjCProperty(const ObjCMethodDecl *Getter,
453                                   const ObjCMethodDecl *Setter,
454                                   const NSAPI &NS, edit::Commit &commit,
455                                   unsigned LengthOfPrefix,
456                                   bool Atomic, bool UseNsIosOnlyMacro,
457                                   bool AvailabilityArgsMatch) {
458   ASTContext &Context = NS.getASTContext();
459   bool LParenAdded = false;
460   std::string PropertyString = "@property ";
461   if (UseNsIosOnlyMacro && NS.isMacroDefined("NS_NONATOMIC_IOSONLY")) {
462     PropertyString += "(NS_NONATOMIC_IOSONLY";
463     LParenAdded = true;
464   } else if (!Atomic) {
465     PropertyString += "(nonatomic";
466     LParenAdded = true;
467   }
468 
469   std::string PropertyNameString = Getter->getNameAsString();
470   StringRef PropertyName(PropertyNameString);
471   if (LengthOfPrefix > 0) {
472     if (!LParenAdded) {
473       PropertyString += "(getter=";
474       LParenAdded = true;
475     }
476     else
477       PropertyString += ", getter=";
478     PropertyString += PropertyNameString;
479   }
480   // Property with no setter may be suggested as a 'readonly' property.
481   if (!Setter)
482     append_attr(PropertyString, "readonly", LParenAdded);
483 
484 
485   // Short circuit 'delegate' properties that contain the name "delegate" or
486   // "dataSource", or have exact name "target" to have 'assign' attribute.
487   if (PropertyName.equals("target") || PropertyName.contains("delegate") ||
488       PropertyName.contains("dataSource")) {
489     QualType QT = Getter->getReturnType();
490     if (!QT->isRealType())
491       append_attr(PropertyString, "assign", LParenAdded);
492   } else if (!Setter) {
493     QualType ResType = Context.getCanonicalType(Getter->getReturnType());
494     if (const char *MemoryManagementAttr = PropertyMemoryAttribute(Context, ResType))
495       append_attr(PropertyString, MemoryManagementAttr, LParenAdded);
496   } else {
497     const ParmVarDecl *argDecl = *Setter->param_begin();
498     QualType ArgType = Context.getCanonicalType(argDecl->getType());
499     if (const char *MemoryManagementAttr = PropertyMemoryAttribute(Context, ArgType))
500       append_attr(PropertyString, MemoryManagementAttr, LParenAdded);
501   }
502   if (LParenAdded)
503     PropertyString += ')';
504   QualType RT = Getter->getReturnType();
505   if (!RT->getAs<TypedefType>()) {
506     // strip off any ARC lifetime qualifier.
507     QualType CanResultTy = Context.getCanonicalType(RT);
508     if (CanResultTy.getQualifiers().hasObjCLifetime()) {
509       Qualifiers Qs = CanResultTy.getQualifiers();
510       Qs.removeObjCLifetime();
511       RT = Context.getQualifiedType(CanResultTy.getUnqualifiedType(), Qs);
512     }
513   }
514   PropertyString += " ";
515   PrintingPolicy SubPolicy(Context.getPrintingPolicy());
516   SubPolicy.SuppressStrongLifetime = true;
517   SubPolicy.SuppressLifetimeQualifiers = true;
518   std::string TypeString = RT.getAsString(SubPolicy);
519   if (LengthOfPrefix > 0) {
520     // property name must strip off "is" and lower case the first character
521     // after that; e.g. isContinuous will become continuous.
522     StringRef PropertyNameStringRef(PropertyNameString);
523     PropertyNameStringRef = PropertyNameStringRef.drop_front(LengthOfPrefix);
524     PropertyNameString = std::string(PropertyNameStringRef);
525     bool NoLowering = (isUppercase(PropertyNameString[0]) &&
526                        PropertyNameString.size() > 1 &&
527                        isUppercase(PropertyNameString[1]));
528     if (!NoLowering)
529       PropertyNameString[0] = toLowercase(PropertyNameString[0]);
530   }
531   if (RT->isBlockPointerType() || RT->isFunctionPointerType())
532     MigrateBlockOrFunctionPointerTypeVariable(PropertyString,
533                                               TypeString,
534                                               PropertyNameString.c_str());
535   else {
536     char LastChar = TypeString[TypeString.size()-1];
537     PropertyString += TypeString;
538     if (LastChar != '*')
539       PropertyString += ' ';
540     PropertyString += PropertyNameString;
541   }
542   SourceLocation StartGetterSelectorLoc = Getter->getSelectorStartLoc();
543   Selector GetterSelector = Getter->getSelector();
544 
545   SourceLocation EndGetterSelectorLoc =
546     StartGetterSelectorLoc.getLocWithOffset(GetterSelector.getNameForSlot(0).size());
547   commit.replace(CharSourceRange::getCharRange(Getter->getBeginLoc(),
548                                                EndGetterSelectorLoc),
549                  PropertyString);
550   if (Setter && AvailabilityArgsMatch) {
551     SourceLocation EndLoc = Setter->getDeclaratorEndLoc();
552     // Get location past ';'
553     EndLoc = EndLoc.getLocWithOffset(1);
554     SourceLocation BeginOfSetterDclLoc = Setter->getBeginLoc();
555     // FIXME. This assumes that setter decl; is immediately preceded by eoln.
556     // It is trying to remove the setter method decl. line entirely.
557     BeginOfSetterDclLoc = BeginOfSetterDclLoc.getLocWithOffset(-1);
558     commit.remove(SourceRange(BeginOfSetterDclLoc, EndLoc));
559   }
560 }
561 
562 static bool IsCategoryNameWithDeprecatedSuffix(ObjCContainerDecl *D) {
563   if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(D)) {
564     StringRef Name = CatDecl->getName();
565     return Name.ends_with("Deprecated");
566   }
567   return false;
568 }
569 
570 void ObjCMigrateASTConsumer::migrateObjCContainerDecl(ASTContext &Ctx,
571                                                       ObjCContainerDecl *D) {
572   if (D->isDeprecated() || IsCategoryNameWithDeprecatedSuffix(D))
573     return;
574 
575   for (auto *Method : D->methods()) {
576     if (Method->isDeprecated())
577       continue;
578     bool PropertyInferred = migrateProperty(Ctx, D, Method);
579     // If a property is inferred, do not attempt to attach NS_RETURNS_INNER_POINTER to
580     // the getter method as it ends up on the property itself which we don't want
581     // to do unless -objcmt-returns-innerpointer-property  option is on.
582     if (!PropertyInferred ||
583         (ASTMigrateActions & FrontendOptions::ObjCMT_ReturnsInnerPointerProperty))
584       if (ASTMigrateActions & FrontendOptions::ObjCMT_Annotation)
585         migrateNsReturnsInnerPointer(Ctx, Method);
586   }
587   if (!(ASTMigrateActions & FrontendOptions::ObjCMT_ReturnsInnerPointerProperty))
588     return;
589 
590   for (auto *Prop : D->instance_properties()) {
591     if ((ASTMigrateActions & FrontendOptions::ObjCMT_Annotation) &&
592         !Prop->isDeprecated())
593       migratePropertyNsReturnsInnerPointer(Ctx, Prop);
594   }
595 }
596 
597 static bool
598 ClassImplementsAllMethodsAndProperties(ASTContext &Ctx,
599                                       const ObjCImplementationDecl *ImpDecl,
600                                        const ObjCInterfaceDecl *IDecl,
601                                       ObjCProtocolDecl *Protocol) {
602   // In auto-synthesis, protocol properties are not synthesized. So,
603   // a conforming protocol must have its required properties declared
604   // in class interface.
605   bool HasAtleastOneRequiredProperty = false;
606   if (const ObjCProtocolDecl *PDecl = Protocol->getDefinition())
607     for (const auto *Property : PDecl->instance_properties()) {
608       if (Property->getPropertyImplementation() == ObjCPropertyDecl::Optional)
609         continue;
610       HasAtleastOneRequiredProperty = true;
611       DeclContext::lookup_result R = IDecl->lookup(Property->getDeclName());
612       if (R.empty()) {
613         // Relax the rule and look into class's implementation for a synthesize
614         // or dynamic declaration. Class is implementing a property coming from
615         // another protocol. This still makes the target protocol as conforming.
616         if (!ImpDecl->FindPropertyImplDecl(
617                                   Property->getDeclName().getAsIdentifierInfo(),
618                                   Property->getQueryKind()))
619           return false;
620       } else if (auto *ClassProperty = R.find_first<ObjCPropertyDecl>()) {
621         if ((ClassProperty->getPropertyAttributes() !=
622              Property->getPropertyAttributes()) ||
623             !Ctx.hasSameType(ClassProperty->getType(), Property->getType()))
624           return false;
625       } else
626         return false;
627     }
628 
629   // At this point, all required properties in this protocol conform to those
630   // declared in the class.
631   // Check that class implements the required methods of the protocol too.
632   bool HasAtleastOneRequiredMethod = false;
633   if (const ObjCProtocolDecl *PDecl = Protocol->getDefinition()) {
634     if (PDecl->meth_begin() == PDecl->meth_end())
635       return HasAtleastOneRequiredProperty;
636     for (const auto *MD : PDecl->methods()) {
637       if (MD->isImplicit())
638         continue;
639       if (MD->getImplementationControl() == ObjCImplementationControl::Optional)
640         continue;
641       DeclContext::lookup_result R = ImpDecl->lookup(MD->getDeclName());
642       if (R.empty())
643         return false;
644       bool match = false;
645       HasAtleastOneRequiredMethod = true;
646       for (NamedDecl *ND : R)
647         if (ObjCMethodDecl *ImpMD = dyn_cast<ObjCMethodDecl>(ND))
648           if (Ctx.ObjCMethodsAreEqual(MD, ImpMD)) {
649             match = true;
650             break;
651           }
652       if (!match)
653         return false;
654     }
655   }
656   return HasAtleastOneRequiredProperty || HasAtleastOneRequiredMethod;
657 }
658 
659 static bool rewriteToObjCInterfaceDecl(const ObjCInterfaceDecl *IDecl,
660                     llvm::SmallVectorImpl<ObjCProtocolDecl*> &ConformingProtocols,
661                     const NSAPI &NS, edit::Commit &commit) {
662   const ObjCList<ObjCProtocolDecl> &Protocols = IDecl->getReferencedProtocols();
663   std::string ClassString;
664   SourceLocation EndLoc =
665   IDecl->getSuperClass() ? IDecl->getSuperClassLoc() : IDecl->getLocation();
666 
667   if (Protocols.empty()) {
668     ClassString = '<';
669     for (unsigned i = 0, e = ConformingProtocols.size(); i != e; i++) {
670       ClassString += ConformingProtocols[i]->getNameAsString();
671       if (i != (e-1))
672         ClassString += ", ";
673     }
674     ClassString += "> ";
675   }
676   else {
677     ClassString = ", ";
678     for (unsigned i = 0, e = ConformingProtocols.size(); i != e; i++) {
679       ClassString += ConformingProtocols[i]->getNameAsString();
680       if (i != (e-1))
681         ClassString += ", ";
682     }
683     ObjCInterfaceDecl::protocol_loc_iterator PL = IDecl->protocol_loc_end() - 1;
684     EndLoc = *PL;
685   }
686 
687   commit.insertAfterToken(EndLoc, ClassString);
688   return true;
689 }
690 
691 static StringRef GetUnsignedName(StringRef NSIntegerName) {
692   StringRef UnsignedName = llvm::StringSwitch<StringRef>(NSIntegerName)
693     .Case("int8_t", "uint8_t")
694     .Case("int16_t", "uint16_t")
695     .Case("int32_t", "uint32_t")
696     .Case("NSInteger", "NSUInteger")
697     .Case("int64_t", "uint64_t")
698     .Default(NSIntegerName);
699   return UnsignedName;
700 }
701 
702 static bool rewriteToNSEnumDecl(const EnumDecl *EnumDcl,
703                                 const TypedefDecl *TypedefDcl,
704                                 const NSAPI &NS, edit::Commit &commit,
705                                 StringRef NSIntegerName,
706                                 bool NSOptions) {
707   std::string ClassString;
708   if (NSOptions) {
709     ClassString = "typedef NS_OPTIONS(";
710     ClassString += GetUnsignedName(NSIntegerName);
711   }
712   else {
713     ClassString = "typedef NS_ENUM(";
714     ClassString += NSIntegerName;
715   }
716   ClassString += ", ";
717 
718   ClassString += TypedefDcl->getIdentifier()->getName();
719   ClassString += ')';
720   SourceRange R(EnumDcl->getBeginLoc(), EnumDcl->getBeginLoc());
721   commit.replace(R, ClassString);
722   SourceLocation EndOfEnumDclLoc = EnumDcl->getEndLoc();
723   EndOfEnumDclLoc = trans::findSemiAfterLocation(EndOfEnumDclLoc,
724                                                  NS.getASTContext(), /*IsDecl*/true);
725   if (EndOfEnumDclLoc.isValid()) {
726     SourceRange EnumDclRange(EnumDcl->getBeginLoc(), EndOfEnumDclLoc);
727     commit.insertFromRange(TypedefDcl->getBeginLoc(), EnumDclRange);
728   }
729   else
730     return false;
731 
732   SourceLocation EndTypedefDclLoc = TypedefDcl->getEndLoc();
733   EndTypedefDclLoc = trans::findSemiAfterLocation(EndTypedefDclLoc,
734                                                  NS.getASTContext(), /*IsDecl*/true);
735   if (EndTypedefDclLoc.isValid()) {
736     SourceRange TDRange(TypedefDcl->getBeginLoc(), EndTypedefDclLoc);
737     commit.remove(TDRange);
738   }
739   else
740     return false;
741 
742   EndOfEnumDclLoc =
743       trans::findLocationAfterSemi(EnumDcl->getEndLoc(), NS.getASTContext(),
744                                    /*IsDecl*/ true);
745   if (EndOfEnumDclLoc.isValid()) {
746     SourceLocation BeginOfEnumDclLoc = EnumDcl->getBeginLoc();
747     // FIXME. This assumes that enum decl; is immediately preceded by eoln.
748     // It is trying to remove the enum decl. lines entirely.
749     BeginOfEnumDclLoc = BeginOfEnumDclLoc.getLocWithOffset(-1);
750     commit.remove(SourceRange(BeginOfEnumDclLoc, EndOfEnumDclLoc));
751     return true;
752   }
753   return false;
754 }
755 
756 static void rewriteToNSMacroDecl(ASTContext &Ctx,
757                                  const EnumDecl *EnumDcl,
758                                 const TypedefDecl *TypedefDcl,
759                                 const NSAPI &NS, edit::Commit &commit,
760                                  bool IsNSIntegerType) {
761   QualType DesignatedEnumType = EnumDcl->getIntegerType();
762   assert(!DesignatedEnumType.isNull()
763          && "rewriteToNSMacroDecl - underlying enum type is null");
764 
765   PrintingPolicy Policy(Ctx.getPrintingPolicy());
766   std::string TypeString = DesignatedEnumType.getAsString(Policy);
767   std::string ClassString = IsNSIntegerType ? "NS_ENUM(" : "NS_OPTIONS(";
768   ClassString += TypeString;
769   ClassString += ", ";
770 
771   ClassString += TypedefDcl->getIdentifier()->getName();
772   ClassString += ") ";
773   SourceLocation EndLoc = EnumDcl->getBraceRange().getBegin();
774   if (EndLoc.isInvalid())
775     return;
776   CharSourceRange R =
777       CharSourceRange::getCharRange(EnumDcl->getBeginLoc(), EndLoc);
778   commit.replace(R, ClassString);
779   // This is to remove spaces between '}' and typedef name.
780   SourceLocation StartTypedefLoc = EnumDcl->getEndLoc();
781   StartTypedefLoc = StartTypedefLoc.getLocWithOffset(+1);
782   SourceLocation EndTypedefLoc = TypedefDcl->getEndLoc();
783 
784   commit.remove(SourceRange(StartTypedefLoc, EndTypedefLoc));
785 }
786 
787 static bool UseNSOptionsMacro(Preprocessor &PP, ASTContext &Ctx,
788                               const EnumDecl *EnumDcl) {
789   bool PowerOfTwo = true;
790   bool AllHexdecimalEnumerator = true;
791   uint64_t MaxPowerOfTwoVal = 0;
792   for (auto *Enumerator : EnumDcl->enumerators()) {
793     const Expr *InitExpr = Enumerator->getInitExpr();
794     if (!InitExpr) {
795       PowerOfTwo = false;
796       AllHexdecimalEnumerator = false;
797       continue;
798     }
799     InitExpr = InitExpr->IgnoreParenCasts();
800     if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(InitExpr))
801       if (BO->isShiftOp() || BO->isBitwiseOp())
802         return true;
803 
804     uint64_t EnumVal = Enumerator->getInitVal().getZExtValue();
805     if (PowerOfTwo && EnumVal) {
806       if (!llvm::isPowerOf2_64(EnumVal))
807         PowerOfTwo = false;
808       else if (EnumVal > MaxPowerOfTwoVal)
809         MaxPowerOfTwoVal = EnumVal;
810     }
811     if (AllHexdecimalEnumerator && EnumVal) {
812       bool FoundHexdecimalEnumerator = false;
813       SourceLocation EndLoc = Enumerator->getEndLoc();
814       Token Tok;
815       if (!PP.getRawToken(EndLoc, Tok, /*IgnoreWhiteSpace=*/true))
816         if (Tok.isLiteral() && Tok.getLength() > 2) {
817           if (const char *StringLit = Tok.getLiteralData())
818             FoundHexdecimalEnumerator =
819               (StringLit[0] == '0' && (toLowercase(StringLit[1]) == 'x'));
820         }
821       if (!FoundHexdecimalEnumerator)
822         AllHexdecimalEnumerator = false;
823     }
824   }
825   return AllHexdecimalEnumerator || (PowerOfTwo && (MaxPowerOfTwoVal > 2));
826 }
827 
828 void ObjCMigrateASTConsumer::migrateProtocolConformance(ASTContext &Ctx,
829                                             const ObjCImplementationDecl *ImpDecl) {
830   const ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface();
831   if (!IDecl || ObjCProtocolDecls.empty() || IDecl->isDeprecated())
832     return;
833   // Find all implicit conforming protocols for this class
834   // and make them explicit.
835   llvm::SmallPtrSet<ObjCProtocolDecl *, 8> ExplicitProtocols;
836   Ctx.CollectInheritedProtocols(IDecl, ExplicitProtocols);
837   llvm::SmallVector<ObjCProtocolDecl *, 8> PotentialImplicitProtocols;
838 
839   for (ObjCProtocolDecl *ProtDecl : ObjCProtocolDecls)
840     if (!ExplicitProtocols.count(ProtDecl))
841       PotentialImplicitProtocols.push_back(ProtDecl);
842 
843   if (PotentialImplicitProtocols.empty())
844     return;
845 
846   // go through list of non-optional methods and properties in each protocol
847   // in the PotentialImplicitProtocols list. If class implements every one of the
848   // methods and properties, then this class conforms to this protocol.
849   llvm::SmallVector<ObjCProtocolDecl*, 8> ConformingProtocols;
850   for (unsigned i = 0, e = PotentialImplicitProtocols.size(); i != e; i++)
851     if (ClassImplementsAllMethodsAndProperties(Ctx, ImpDecl, IDecl,
852                                               PotentialImplicitProtocols[i]))
853       ConformingProtocols.push_back(PotentialImplicitProtocols[i]);
854 
855   if (ConformingProtocols.empty())
856     return;
857 
858   // Further reduce number of conforming protocols. If protocol P1 is in the list
859   // protocol P2 (P2<P1>), No need to include P1.
860   llvm::SmallVector<ObjCProtocolDecl*, 8> MinimalConformingProtocols;
861   for (unsigned i = 0, e = ConformingProtocols.size(); i != e; i++) {
862     bool DropIt = false;
863     ObjCProtocolDecl *TargetPDecl = ConformingProtocols[i];
864     for (unsigned i1 = 0, e1 = ConformingProtocols.size(); i1 != e1; i1++) {
865       ObjCProtocolDecl *PDecl = ConformingProtocols[i1];
866       if (PDecl == TargetPDecl)
867         continue;
868       if (PDecl->lookupProtocolNamed(
869             TargetPDecl->getDeclName().getAsIdentifierInfo())) {
870         DropIt = true;
871         break;
872       }
873     }
874     if (!DropIt)
875       MinimalConformingProtocols.push_back(TargetPDecl);
876   }
877   if (MinimalConformingProtocols.empty())
878     return;
879   edit::Commit commit(*Editor);
880   rewriteToObjCInterfaceDecl(IDecl, MinimalConformingProtocols,
881                              *NSAPIObj, commit);
882   Editor->commit(commit);
883 }
884 
885 void ObjCMigrateASTConsumer::CacheObjCNSIntegerTypedefed(
886                                           const TypedefDecl *TypedefDcl) {
887 
888   QualType qt = TypedefDcl->getTypeSourceInfo()->getType();
889   if (NSAPIObj->isObjCNSIntegerType(qt))
890     NSIntegerTypedefed = TypedefDcl;
891   else if (NSAPIObj->isObjCNSUIntegerType(qt))
892     NSUIntegerTypedefed = TypedefDcl;
893 }
894 
895 bool ObjCMigrateASTConsumer::migrateNSEnumDecl(ASTContext &Ctx,
896                                            const EnumDecl *EnumDcl,
897                                            const TypedefDecl *TypedefDcl) {
898   if (!EnumDcl->isCompleteDefinition() || EnumDcl->getIdentifier() ||
899       EnumDcl->isDeprecated())
900     return false;
901   if (!TypedefDcl) {
902     if (NSIntegerTypedefed) {
903       TypedefDcl = NSIntegerTypedefed;
904       NSIntegerTypedefed = nullptr;
905     }
906     else if (NSUIntegerTypedefed) {
907       TypedefDcl = NSUIntegerTypedefed;
908       NSUIntegerTypedefed = nullptr;
909     }
910     else
911       return false;
912     FileID FileIdOfTypedefDcl =
913       PP.getSourceManager().getFileID(TypedefDcl->getLocation());
914     FileID FileIdOfEnumDcl =
915       PP.getSourceManager().getFileID(EnumDcl->getLocation());
916     if (FileIdOfTypedefDcl != FileIdOfEnumDcl)
917       return false;
918   }
919   if (TypedefDcl->isDeprecated())
920     return false;
921 
922   QualType qt = TypedefDcl->getTypeSourceInfo()->getType();
923   StringRef NSIntegerName = NSAPIObj->GetNSIntegralKind(qt);
924 
925   if (NSIntegerName.empty()) {
926     // Also check for typedef enum {...} TD;
927     if (const EnumType *EnumTy = qt->getAs<EnumType>()) {
928       if (EnumTy->getDecl() == EnumDcl) {
929         bool NSOptions = UseNSOptionsMacro(PP, Ctx, EnumDcl);
930         if (!InsertFoundation(Ctx, TypedefDcl->getBeginLoc()))
931           return false;
932         edit::Commit commit(*Editor);
933         rewriteToNSMacroDecl(Ctx, EnumDcl, TypedefDcl, *NSAPIObj, commit, !NSOptions);
934         Editor->commit(commit);
935         return true;
936       }
937     }
938     return false;
939   }
940 
941   // We may still use NS_OPTIONS based on what we find in the enumertor list.
942   bool NSOptions = UseNSOptionsMacro(PP, Ctx, EnumDcl);
943   if (!InsertFoundation(Ctx, TypedefDcl->getBeginLoc()))
944     return false;
945   edit::Commit commit(*Editor);
946   bool Res = rewriteToNSEnumDecl(EnumDcl, TypedefDcl, *NSAPIObj,
947                                  commit, NSIntegerName, NSOptions);
948   Editor->commit(commit);
949   return Res;
950 }
951 
952 static void ReplaceWithInstancetype(ASTContext &Ctx,
953                                     const ObjCMigrateASTConsumer &ASTC,
954                                     ObjCMethodDecl *OM) {
955   if (OM->getReturnType() == Ctx.getObjCInstanceType())
956     return; // already has instancetype.
957 
958   SourceRange R;
959   std::string ClassString;
960   if (TypeSourceInfo *TSInfo = OM->getReturnTypeSourceInfo()) {
961     TypeLoc TL = TSInfo->getTypeLoc();
962     R = SourceRange(TL.getBeginLoc(), TL.getEndLoc());
963     ClassString = "instancetype";
964   }
965   else {
966     R = SourceRange(OM->getBeginLoc(), OM->getBeginLoc());
967     ClassString = OM->isInstanceMethod() ? '-' : '+';
968     ClassString += " (instancetype)";
969   }
970   edit::Commit commit(*ASTC.Editor);
971   commit.replace(R, ClassString);
972   ASTC.Editor->commit(commit);
973 }
974 
975 static void ReplaceWithClasstype(const ObjCMigrateASTConsumer &ASTC,
976                                     ObjCMethodDecl *OM) {
977   ObjCInterfaceDecl *IDecl = OM->getClassInterface();
978   SourceRange R;
979   std::string ClassString;
980   if (TypeSourceInfo *TSInfo = OM->getReturnTypeSourceInfo()) {
981     TypeLoc TL = TSInfo->getTypeLoc();
982     R = SourceRange(TL.getBeginLoc(), TL.getEndLoc()); {
983       ClassString = std::string(IDecl->getName());
984       ClassString += "*";
985     }
986   }
987   else {
988     R = SourceRange(OM->getBeginLoc(), OM->getBeginLoc());
989     ClassString = "+ (";
990     ClassString += IDecl->getName(); ClassString += "*)";
991   }
992   edit::Commit commit(*ASTC.Editor);
993   commit.replace(R, ClassString);
994   ASTC.Editor->commit(commit);
995 }
996 
997 void ObjCMigrateASTConsumer::migrateMethodInstanceType(ASTContext &Ctx,
998                                                        ObjCContainerDecl *CDecl,
999                                                        ObjCMethodDecl *OM) {
1000   ObjCInstanceTypeFamily OIT_Family =
1001     Selector::getInstTypeMethodFamily(OM->getSelector());
1002 
1003   std::string ClassName;
1004   switch (OIT_Family) {
1005     case OIT_None:
1006       migrateFactoryMethod(Ctx, CDecl, OM);
1007       return;
1008     case OIT_Array:
1009       ClassName = "NSArray";
1010       break;
1011     case OIT_Dictionary:
1012       ClassName = "NSDictionary";
1013       break;
1014     case OIT_Singleton:
1015       migrateFactoryMethod(Ctx, CDecl, OM, OIT_Singleton);
1016       return;
1017     case OIT_Init:
1018       if (OM->getReturnType()->isObjCIdType())
1019         ReplaceWithInstancetype(Ctx, *this, OM);
1020       return;
1021     case OIT_ReturnsSelf:
1022       migrateFactoryMethod(Ctx, CDecl, OM, OIT_ReturnsSelf);
1023       return;
1024   }
1025   if (!OM->getReturnType()->isObjCIdType())
1026     return;
1027 
1028   ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1029   if (!IDecl) {
1030     if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl))
1031       IDecl = CatDecl->getClassInterface();
1032     else if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(CDecl))
1033       IDecl = ImpDecl->getClassInterface();
1034   }
1035   if (!IDecl ||
1036       !IDecl->lookupInheritedClass(&Ctx.Idents.get(ClassName))) {
1037     migrateFactoryMethod(Ctx, CDecl, OM);
1038     return;
1039   }
1040   ReplaceWithInstancetype(Ctx, *this, OM);
1041 }
1042 
1043 static bool TypeIsInnerPointer(QualType T) {
1044   if (!T->isAnyPointerType())
1045     return false;
1046   if (T->isObjCObjectPointerType() || T->isObjCBuiltinType() ||
1047       T->isBlockPointerType() || T->isFunctionPointerType() ||
1048       ento::coreFoundation::isCFObjectRef(T))
1049     return false;
1050   // Also, typedef-of-pointer-to-incomplete-struct is something that we assume
1051   // is not an innter pointer type.
1052   QualType OrigT = T;
1053   while (const auto *TD = T->getAs<TypedefType>())
1054     T = TD->getDecl()->getUnderlyingType();
1055   if (OrigT == T || !T->isPointerType())
1056     return true;
1057   const PointerType* PT = T->getAs<PointerType>();
1058   QualType UPointeeT = PT->getPointeeType().getUnqualifiedType();
1059   if (UPointeeT->isRecordType()) {
1060     const RecordType *RecordTy = UPointeeT->getAs<RecordType>();
1061     if (!RecordTy->getDecl()->isCompleteDefinition())
1062       return false;
1063   }
1064   return true;
1065 }
1066 
1067 /// Check whether the two versions match.
1068 static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y) {
1069   return (X == Y);
1070 }
1071 
1072 /// AvailabilityAttrsMatch - This routine checks that if comparing two
1073 /// availability attributes, all their components match. It returns
1074 /// true, if not dealing with availability or when all components of
1075 /// availability attributes match. This routine is only called when
1076 /// the attributes are of the same kind.
1077 static bool AvailabilityAttrsMatch(Attr *At1, Attr *At2) {
1078   const AvailabilityAttr *AA1 = dyn_cast<AvailabilityAttr>(At1);
1079   if (!AA1)
1080     return true;
1081   const AvailabilityAttr *AA2 = cast<AvailabilityAttr>(At2);
1082 
1083   VersionTuple Introduced1 = AA1->getIntroduced();
1084   VersionTuple Deprecated1 = AA1->getDeprecated();
1085   VersionTuple Obsoleted1 = AA1->getObsoleted();
1086   bool IsUnavailable1 = AA1->getUnavailable();
1087   VersionTuple Introduced2 = AA2->getIntroduced();
1088   VersionTuple Deprecated2 = AA2->getDeprecated();
1089   VersionTuple Obsoleted2 = AA2->getObsoleted();
1090   bool IsUnavailable2 = AA2->getUnavailable();
1091   return (versionsMatch(Introduced1, Introduced2) &&
1092           versionsMatch(Deprecated1, Deprecated2) &&
1093           versionsMatch(Obsoleted1, Obsoleted2) &&
1094           IsUnavailable1 == IsUnavailable2);
1095 }
1096 
1097 static bool MatchTwoAttributeLists(const AttrVec &Attrs1, const AttrVec &Attrs2,
1098                                    bool &AvailabilityArgsMatch) {
1099   // This list is very small, so this need not be optimized.
1100   for (unsigned i = 0, e = Attrs1.size(); i != e; i++) {
1101     bool match = false;
1102     for (unsigned j = 0, f = Attrs2.size(); j != f; j++) {
1103       // Matching attribute kind only. Except for Availability attributes,
1104       // we are not getting into details of the attributes. For all practical purposes
1105       // this is sufficient.
1106       if (Attrs1[i]->getKind() == Attrs2[j]->getKind()) {
1107         if (AvailabilityArgsMatch)
1108           AvailabilityArgsMatch = AvailabilityAttrsMatch(Attrs1[i], Attrs2[j]);
1109         match = true;
1110         break;
1111       }
1112     }
1113     if (!match)
1114       return false;
1115   }
1116   return true;
1117 }
1118 
1119 /// AttributesMatch - This routine checks list of attributes for two
1120 /// decls. It returns false, if there is a mismatch in kind of
1121 /// attributes seen in the decls. It returns true if the two decls
1122 /// have list of same kind of attributes. Furthermore, when there
1123 /// are availability attributes in the two decls, it sets the
1124 /// AvailabilityArgsMatch to false if availability attributes have
1125 /// different versions, etc.
1126 static bool AttributesMatch(const Decl *Decl1, const Decl *Decl2,
1127                             bool &AvailabilityArgsMatch) {
1128   if (!Decl1->hasAttrs() || !Decl2->hasAttrs()) {
1129     AvailabilityArgsMatch = (Decl1->hasAttrs() == Decl2->hasAttrs());
1130     return true;
1131   }
1132   AvailabilityArgsMatch = true;
1133   const AttrVec &Attrs1 = Decl1->getAttrs();
1134   const AttrVec &Attrs2 = Decl2->getAttrs();
1135   bool match = MatchTwoAttributeLists(Attrs1, Attrs2, AvailabilityArgsMatch);
1136   if (match && (Attrs2.size() > Attrs1.size()))
1137     return MatchTwoAttributeLists(Attrs2, Attrs1, AvailabilityArgsMatch);
1138   return match;
1139 }
1140 
1141 static bool IsValidIdentifier(ASTContext &Ctx,
1142                               const char *Name) {
1143   if (!isAsciiIdentifierStart(Name[0]))
1144     return false;
1145   std::string NameString = Name;
1146   NameString[0] = toLowercase(NameString[0]);
1147   IdentifierInfo *II = &Ctx.Idents.get(NameString);
1148   return II->getTokenID() ==  tok::identifier;
1149 }
1150 
1151 bool ObjCMigrateASTConsumer::migrateProperty(ASTContext &Ctx,
1152                              ObjCContainerDecl *D,
1153                              ObjCMethodDecl *Method) {
1154   if (Method->isPropertyAccessor() || !Method->isInstanceMethod() ||
1155       Method->param_size() != 0)
1156     return false;
1157   // Is this method candidate to be a getter?
1158   QualType GRT = Method->getReturnType();
1159   if (GRT->isVoidType())
1160     return false;
1161 
1162   Selector GetterSelector = Method->getSelector();
1163   ObjCInstanceTypeFamily OIT_Family =
1164     Selector::getInstTypeMethodFamily(GetterSelector);
1165 
1166   if (OIT_Family != OIT_None)
1167     return false;
1168 
1169   IdentifierInfo *getterName = GetterSelector.getIdentifierInfoForSlot(0);
1170   Selector SetterSelector =
1171   SelectorTable::constructSetterSelector(PP.getIdentifierTable(),
1172                                          PP.getSelectorTable(),
1173                                          getterName);
1174   ObjCMethodDecl *SetterMethod = D->getInstanceMethod(SetterSelector);
1175   unsigned LengthOfPrefix = 0;
1176   if (!SetterMethod) {
1177     // try a different naming convention for getter: isXxxxx
1178     StringRef getterNameString = getterName->getName();
1179     bool IsPrefix = getterNameString.starts_with("is");
1180     // Note that we don't want to change an isXXX method of retainable object
1181     // type to property (readonly or otherwise).
1182     if (IsPrefix && GRT->isObjCRetainableType())
1183       return false;
1184     if (IsPrefix || getterNameString.starts_with("get")) {
1185       LengthOfPrefix = (IsPrefix ? 2 : 3);
1186       const char *CGetterName = getterNameString.data() + LengthOfPrefix;
1187       // Make sure that first character after "is" or "get" prefix can
1188       // start an identifier.
1189       if (!IsValidIdentifier(Ctx, CGetterName))
1190         return false;
1191       if (CGetterName[0] && isUppercase(CGetterName[0])) {
1192         getterName = &Ctx.Idents.get(CGetterName);
1193         SetterSelector =
1194         SelectorTable::constructSetterSelector(PP.getIdentifierTable(),
1195                                                PP.getSelectorTable(),
1196                                                getterName);
1197         SetterMethod = D->getInstanceMethod(SetterSelector);
1198       }
1199     }
1200   }
1201 
1202   if (SetterMethod) {
1203     if ((ASTMigrateActions & FrontendOptions::ObjCMT_ReadwriteProperty) == 0)
1204       return false;
1205     bool AvailabilityArgsMatch;
1206     if (SetterMethod->isDeprecated() ||
1207         !AttributesMatch(Method, SetterMethod, AvailabilityArgsMatch))
1208       return false;
1209 
1210     // Is this a valid setter, matching the target getter?
1211     QualType SRT = SetterMethod->getReturnType();
1212     if (!SRT->isVoidType())
1213       return false;
1214     const ParmVarDecl *argDecl = *SetterMethod->param_begin();
1215     QualType ArgType = argDecl->getType();
1216     if (!Ctx.hasSameUnqualifiedType(ArgType, GRT))
1217       return false;
1218     edit::Commit commit(*Editor);
1219     rewriteToObjCProperty(Method, SetterMethod, *NSAPIObj, commit,
1220                           LengthOfPrefix,
1221                           (ASTMigrateActions &
1222                            FrontendOptions::ObjCMT_AtomicProperty) != 0,
1223                           (ASTMigrateActions &
1224                            FrontendOptions::ObjCMT_NsAtomicIOSOnlyProperty) != 0,
1225                           AvailabilityArgsMatch);
1226     Editor->commit(commit);
1227     return true;
1228   }
1229   else if (ASTMigrateActions & FrontendOptions::ObjCMT_ReadonlyProperty) {
1230     // Try a non-void method with no argument (and no setter or property of same name
1231     // as a 'readonly' property.
1232     edit::Commit commit(*Editor);
1233     rewriteToObjCProperty(Method, nullptr /*SetterMethod*/, *NSAPIObj, commit,
1234                           LengthOfPrefix,
1235                           (ASTMigrateActions &
1236                            FrontendOptions::ObjCMT_AtomicProperty) != 0,
1237                           (ASTMigrateActions &
1238                            FrontendOptions::ObjCMT_NsAtomicIOSOnlyProperty) != 0,
1239                           /*AvailabilityArgsMatch*/false);
1240     Editor->commit(commit);
1241     return true;
1242   }
1243   return false;
1244 }
1245 
1246 void ObjCMigrateASTConsumer::migrateNsReturnsInnerPointer(ASTContext &Ctx,
1247                                                           ObjCMethodDecl *OM) {
1248   if (OM->isImplicit() ||
1249       !OM->isInstanceMethod() ||
1250       OM->hasAttr<ObjCReturnsInnerPointerAttr>())
1251     return;
1252 
1253   QualType RT = OM->getReturnType();
1254   if (!TypeIsInnerPointer(RT) ||
1255       !NSAPIObj->isMacroDefined("NS_RETURNS_INNER_POINTER"))
1256     return;
1257 
1258   edit::Commit commit(*Editor);
1259   commit.insertBefore(OM->getEndLoc(), " NS_RETURNS_INNER_POINTER");
1260   Editor->commit(commit);
1261 }
1262 
1263 void ObjCMigrateASTConsumer::migratePropertyNsReturnsInnerPointer(ASTContext &Ctx,
1264                                                                   ObjCPropertyDecl *P) {
1265   QualType T = P->getType();
1266 
1267   if (!TypeIsInnerPointer(T) ||
1268       !NSAPIObj->isMacroDefined("NS_RETURNS_INNER_POINTER"))
1269     return;
1270   edit::Commit commit(*Editor);
1271   commit.insertBefore(P->getEndLoc(), " NS_RETURNS_INNER_POINTER ");
1272   Editor->commit(commit);
1273 }
1274 
1275 void ObjCMigrateASTConsumer::migrateAllMethodInstaceType(ASTContext &Ctx,
1276                                                  ObjCContainerDecl *CDecl) {
1277   if (CDecl->isDeprecated() || IsCategoryNameWithDeprecatedSuffix(CDecl))
1278     return;
1279 
1280   // migrate methods which can have instancetype as their result type.
1281   for (auto *Method : CDecl->methods()) {
1282     if (Method->isDeprecated())
1283       continue;
1284     migrateMethodInstanceType(Ctx, CDecl, Method);
1285   }
1286 }
1287 
1288 void ObjCMigrateASTConsumer::migrateFactoryMethod(ASTContext &Ctx,
1289                                                   ObjCContainerDecl *CDecl,
1290                                                   ObjCMethodDecl *OM,
1291                                                   ObjCInstanceTypeFamily OIT_Family) {
1292   if (OM->isInstanceMethod() ||
1293       OM->getReturnType() == Ctx.getObjCInstanceType() ||
1294       !OM->getReturnType()->isObjCIdType())
1295     return;
1296 
1297   // Candidate factory methods are + (id) NaMeXXX : ... which belong to a class
1298   // NSYYYNamE with matching names be at least 3 characters long.
1299   ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1300   if (!IDecl) {
1301     if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl))
1302       IDecl = CatDecl->getClassInterface();
1303     else if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(CDecl))
1304       IDecl = ImpDecl->getClassInterface();
1305   }
1306   if (!IDecl)
1307     return;
1308 
1309   std::string StringClassName = std::string(IDecl->getName());
1310   StringRef LoweredClassName(StringClassName);
1311   std::string StringLoweredClassName = LoweredClassName.lower();
1312   LoweredClassName = StringLoweredClassName;
1313 
1314   IdentifierInfo *MethodIdName = OM->getSelector().getIdentifierInfoForSlot(0);
1315   // Handle method with no name at its first selector slot; e.g. + (id):(int)x.
1316   if (!MethodIdName)
1317     return;
1318 
1319   std::string MethodName = std::string(MethodIdName->getName());
1320   if (OIT_Family == OIT_Singleton || OIT_Family == OIT_ReturnsSelf) {
1321     StringRef STRefMethodName(MethodName);
1322     size_t len = 0;
1323     if (STRefMethodName.starts_with("standard"))
1324       len = strlen("standard");
1325     else if (STRefMethodName.starts_with("shared"))
1326       len = strlen("shared");
1327     else if (STRefMethodName.starts_with("default"))
1328       len = strlen("default");
1329     else
1330       return;
1331     MethodName = std::string(STRefMethodName.substr(len));
1332   }
1333   std::string MethodNameSubStr = MethodName.substr(0, 3);
1334   StringRef MethodNamePrefix(MethodNameSubStr);
1335   std::string StringLoweredMethodNamePrefix = MethodNamePrefix.lower();
1336   MethodNamePrefix = StringLoweredMethodNamePrefix;
1337   size_t Ix = LoweredClassName.rfind(MethodNamePrefix);
1338   if (Ix == StringRef::npos)
1339     return;
1340   std::string ClassNamePostfix = std::string(LoweredClassName.substr(Ix));
1341   StringRef LoweredMethodName(MethodName);
1342   std::string StringLoweredMethodName = LoweredMethodName.lower();
1343   LoweredMethodName = StringLoweredMethodName;
1344   if (!LoweredMethodName.starts_with(ClassNamePostfix))
1345     return;
1346   if (OIT_Family == OIT_ReturnsSelf)
1347     ReplaceWithClasstype(*this, OM);
1348   else
1349     ReplaceWithInstancetype(Ctx, *this, OM);
1350 }
1351 
1352 static bool IsVoidStarType(QualType Ty) {
1353   if (!Ty->isPointerType())
1354     return false;
1355 
1356   // Is the type void*?
1357   const PointerType* PT = Ty->castAs<PointerType>();
1358   if (PT->getPointeeType().getUnqualifiedType()->isVoidType())
1359     return true;
1360   return IsVoidStarType(PT->getPointeeType());
1361 }
1362 
1363 /// AuditedType - This routine audits the type AT and returns false if it is one of known
1364 /// CF object types or of the "void *" variety. It returns true if we don't care about the type
1365 /// such as a non-pointer or pointers which have no ownership issues (such as "int *").
1366 static bool AuditedType (QualType AT) {
1367   if (!AT->isAnyPointerType() && !AT->isBlockPointerType())
1368     return true;
1369   // FIXME. There isn't much we can say about CF pointer type; or is there?
1370   if (ento::coreFoundation::isCFObjectRef(AT) ||
1371       IsVoidStarType(AT) ||
1372       // If an ObjC object is type, assuming that it is not a CF function and
1373       // that it is an un-audited function.
1374       AT->isObjCObjectPointerType() || AT->isObjCBuiltinType())
1375     return false;
1376   // All other pointers are assumed audited as harmless.
1377   return true;
1378 }
1379 
1380 void ObjCMigrateASTConsumer::AnnotateImplicitBridging(ASTContext &Ctx) {
1381   if (CFFunctionIBCandidates.empty())
1382     return;
1383   if (!NSAPIObj->isMacroDefined("CF_IMPLICIT_BRIDGING_ENABLED")) {
1384     CFFunctionIBCandidates.clear();
1385     FileId = FileID();
1386     return;
1387   }
1388   // Insert CF_IMPLICIT_BRIDGING_ENABLE/CF_IMPLICIT_BRIDGING_DISABLED
1389   const Decl *FirstFD = CFFunctionIBCandidates[0];
1390   const Decl *LastFD  =
1391     CFFunctionIBCandidates[CFFunctionIBCandidates.size()-1];
1392   const char *PragmaString = "\nCF_IMPLICIT_BRIDGING_ENABLED\n\n";
1393   edit::Commit commit(*Editor);
1394   commit.insertBefore(FirstFD->getBeginLoc(), PragmaString);
1395   PragmaString = "\n\nCF_IMPLICIT_BRIDGING_DISABLED\n";
1396   SourceLocation EndLoc = LastFD->getEndLoc();
1397   // get location just past end of function location.
1398   EndLoc = PP.getLocForEndOfToken(EndLoc);
1399   if (isa<FunctionDecl>(LastFD)) {
1400     // For Methods, EndLoc points to the ending semcolon. So,
1401     // not of these extra work is needed.
1402     Token Tok;
1403     // get locaiton of token that comes after end of function.
1404     bool Failed = PP.getRawToken(EndLoc, Tok, /*IgnoreWhiteSpace=*/true);
1405     if (!Failed)
1406       EndLoc = Tok.getLocation();
1407   }
1408   commit.insertAfterToken(EndLoc, PragmaString);
1409   Editor->commit(commit);
1410   FileId = FileID();
1411   CFFunctionIBCandidates.clear();
1412 }
1413 
1414 void ObjCMigrateASTConsumer::migrateCFAnnotation(ASTContext &Ctx, const Decl *Decl) {
1415   if (Decl->isDeprecated())
1416     return;
1417 
1418   if (Decl->hasAttr<CFAuditedTransferAttr>()) {
1419     assert(CFFunctionIBCandidates.empty() &&
1420            "Cannot have audited functions/methods inside user "
1421            "provided CF_IMPLICIT_BRIDGING_ENABLE");
1422     return;
1423   }
1424 
1425   // Finction must be annotated first.
1426   if (const FunctionDecl *FuncDecl = dyn_cast<FunctionDecl>(Decl)) {
1427     CF_BRIDGING_KIND AuditKind = migrateAddFunctionAnnotation(Ctx, FuncDecl);
1428     if (AuditKind == CF_BRIDGING_ENABLE) {
1429       CFFunctionIBCandidates.push_back(Decl);
1430       if (FileId.isInvalid())
1431         FileId = PP.getSourceManager().getFileID(Decl->getLocation());
1432     }
1433     else if (AuditKind == CF_BRIDGING_MAY_INCLUDE) {
1434       if (!CFFunctionIBCandidates.empty()) {
1435         CFFunctionIBCandidates.push_back(Decl);
1436         if (FileId.isInvalid())
1437           FileId = PP.getSourceManager().getFileID(Decl->getLocation());
1438       }
1439     }
1440     else
1441       AnnotateImplicitBridging(Ctx);
1442   }
1443   else {
1444     migrateAddMethodAnnotation(Ctx, cast<ObjCMethodDecl>(Decl));
1445     AnnotateImplicitBridging(Ctx);
1446   }
1447 }
1448 
1449 void ObjCMigrateASTConsumer::AddCFAnnotations(ASTContext &Ctx,
1450                                               const RetainSummary *RS,
1451                                               const FunctionDecl *FuncDecl,
1452                                               bool ResultAnnotated) {
1453   // Annotate function.
1454   if (!ResultAnnotated) {
1455     RetEffect Ret = RS->getRetEffect();
1456     const char *AnnotationString = nullptr;
1457     if (Ret.getObjKind() == ObjKind::CF) {
1458       if (Ret.isOwned() && NSAPIObj->isMacroDefined("CF_RETURNS_RETAINED"))
1459         AnnotationString = " CF_RETURNS_RETAINED";
1460       else if (Ret.notOwned() &&
1461                NSAPIObj->isMacroDefined("CF_RETURNS_NOT_RETAINED"))
1462         AnnotationString = " CF_RETURNS_NOT_RETAINED";
1463     }
1464     else if (Ret.getObjKind() == ObjKind::ObjC) {
1465       if (Ret.isOwned() && NSAPIObj->isMacroDefined("NS_RETURNS_RETAINED"))
1466         AnnotationString = " NS_RETURNS_RETAINED";
1467     }
1468 
1469     if (AnnotationString) {
1470       edit::Commit commit(*Editor);
1471       commit.insertAfterToken(FuncDecl->getEndLoc(), AnnotationString);
1472       Editor->commit(commit);
1473     }
1474   }
1475   unsigned i = 0;
1476   for (FunctionDecl::param_const_iterator pi = FuncDecl->param_begin(),
1477        pe = FuncDecl->param_end(); pi != pe; ++pi, ++i) {
1478     const ParmVarDecl *pd = *pi;
1479     ArgEffect AE = RS->getArg(i);
1480     if (AE.getKind() == DecRef && AE.getObjKind() == ObjKind::CF &&
1481         !pd->hasAttr<CFConsumedAttr>() &&
1482         NSAPIObj->isMacroDefined("CF_CONSUMED")) {
1483       edit::Commit commit(*Editor);
1484       commit.insertBefore(pd->getLocation(), "CF_CONSUMED ");
1485       Editor->commit(commit);
1486     } else if (AE.getKind() == DecRef && AE.getObjKind() == ObjKind::ObjC &&
1487                !pd->hasAttr<NSConsumedAttr>() &&
1488                NSAPIObj->isMacroDefined("NS_CONSUMED")) {
1489       edit::Commit commit(*Editor);
1490       commit.insertBefore(pd->getLocation(), "NS_CONSUMED ");
1491       Editor->commit(commit);
1492     }
1493   }
1494 }
1495 
1496 ObjCMigrateASTConsumer::CF_BRIDGING_KIND
1497   ObjCMigrateASTConsumer::migrateAddFunctionAnnotation(
1498                                                   ASTContext &Ctx,
1499                                                   const FunctionDecl *FuncDecl) {
1500   if (FuncDecl->hasBody())
1501     return CF_BRIDGING_NONE;
1502 
1503   const RetainSummary *RS =
1504       getSummaryManager(Ctx).getSummary(AnyCall(FuncDecl));
1505   bool FuncIsReturnAnnotated = (FuncDecl->hasAttr<CFReturnsRetainedAttr>() ||
1506                                 FuncDecl->hasAttr<CFReturnsNotRetainedAttr>() ||
1507                                 FuncDecl->hasAttr<NSReturnsRetainedAttr>() ||
1508                                 FuncDecl->hasAttr<NSReturnsNotRetainedAttr>() ||
1509                                 FuncDecl->hasAttr<NSReturnsAutoreleasedAttr>());
1510 
1511   // Trivial case of when function is annotated and has no argument.
1512   if (FuncIsReturnAnnotated && FuncDecl->getNumParams() == 0)
1513     return CF_BRIDGING_NONE;
1514 
1515   bool ReturnCFAudited = false;
1516   if (!FuncIsReturnAnnotated) {
1517     RetEffect Ret = RS->getRetEffect();
1518     if (Ret.getObjKind() == ObjKind::CF &&
1519         (Ret.isOwned() || Ret.notOwned()))
1520       ReturnCFAudited = true;
1521     else if (!AuditedType(FuncDecl->getReturnType()))
1522       return CF_BRIDGING_NONE;
1523   }
1524 
1525   // At this point result type is audited for potential inclusion.
1526   unsigned i = 0;
1527   bool ArgCFAudited = false;
1528   for (FunctionDecl::param_const_iterator pi = FuncDecl->param_begin(),
1529        pe = FuncDecl->param_end(); pi != pe; ++pi, ++i) {
1530     const ParmVarDecl *pd = *pi;
1531     ArgEffect AE = RS->getArg(i);
1532     if ((AE.getKind() == DecRef /*CFConsumed annotated*/ ||
1533          AE.getKind() == IncRef) && AE.getObjKind() == ObjKind::CF) {
1534       if (AE.getKind() == DecRef && !pd->hasAttr<CFConsumedAttr>())
1535         ArgCFAudited = true;
1536       else if (AE.getKind() == IncRef)
1537         ArgCFAudited = true;
1538     } else {
1539       QualType AT = pd->getType();
1540       if (!AuditedType(AT)) {
1541         AddCFAnnotations(Ctx, RS, FuncDecl, FuncIsReturnAnnotated);
1542         return CF_BRIDGING_NONE;
1543       }
1544     }
1545   }
1546   if (ReturnCFAudited || ArgCFAudited)
1547     return CF_BRIDGING_ENABLE;
1548 
1549   return CF_BRIDGING_MAY_INCLUDE;
1550 }
1551 
1552 void ObjCMigrateASTConsumer::migrateARCSafeAnnotation(ASTContext &Ctx,
1553                                                  ObjCContainerDecl *CDecl) {
1554   if (!isa<ObjCInterfaceDecl>(CDecl) || CDecl->isDeprecated())
1555     return;
1556 
1557   // migrate methods which can have instancetype as their result type.
1558   for (const auto *Method : CDecl->methods())
1559     migrateCFAnnotation(Ctx, Method);
1560 }
1561 
1562 void ObjCMigrateASTConsumer::AddCFAnnotations(ASTContext &Ctx,
1563                                               const RetainSummary *RS,
1564                                               const ObjCMethodDecl *MethodDecl,
1565                                               bool ResultAnnotated) {
1566   // Annotate function.
1567   if (!ResultAnnotated) {
1568     RetEffect Ret = RS->getRetEffect();
1569     const char *AnnotationString = nullptr;
1570     if (Ret.getObjKind() == ObjKind::CF) {
1571       if (Ret.isOwned() && NSAPIObj->isMacroDefined("CF_RETURNS_RETAINED"))
1572         AnnotationString = " CF_RETURNS_RETAINED";
1573       else if (Ret.notOwned() &&
1574                NSAPIObj->isMacroDefined("CF_RETURNS_NOT_RETAINED"))
1575         AnnotationString = " CF_RETURNS_NOT_RETAINED";
1576     }
1577     else if (Ret.getObjKind() == ObjKind::ObjC) {
1578       ObjCMethodFamily OMF = MethodDecl->getMethodFamily();
1579       switch (OMF) {
1580         case clang::OMF_alloc:
1581         case clang::OMF_new:
1582         case clang::OMF_copy:
1583         case clang::OMF_init:
1584         case clang::OMF_mutableCopy:
1585           break;
1586 
1587         default:
1588           if (Ret.isOwned() && NSAPIObj->isMacroDefined("NS_RETURNS_RETAINED"))
1589             AnnotationString = " NS_RETURNS_RETAINED";
1590           break;
1591       }
1592     }
1593 
1594     if (AnnotationString) {
1595       edit::Commit commit(*Editor);
1596       commit.insertBefore(MethodDecl->getEndLoc(), AnnotationString);
1597       Editor->commit(commit);
1598     }
1599   }
1600   unsigned i = 0;
1601   for (ObjCMethodDecl::param_const_iterator pi = MethodDecl->param_begin(),
1602        pe = MethodDecl->param_end(); pi != pe; ++pi, ++i) {
1603     const ParmVarDecl *pd = *pi;
1604     ArgEffect AE = RS->getArg(i);
1605     if (AE.getKind() == DecRef
1606         && AE.getObjKind() == ObjKind::CF
1607         && !pd->hasAttr<CFConsumedAttr>() &&
1608         NSAPIObj->isMacroDefined("CF_CONSUMED")) {
1609       edit::Commit commit(*Editor);
1610       commit.insertBefore(pd->getLocation(), "CF_CONSUMED ");
1611       Editor->commit(commit);
1612     }
1613   }
1614 }
1615 
1616 void ObjCMigrateASTConsumer::migrateAddMethodAnnotation(
1617                                             ASTContext &Ctx,
1618                                             const ObjCMethodDecl *MethodDecl) {
1619   if (MethodDecl->hasBody() || MethodDecl->isImplicit())
1620     return;
1621 
1622   const RetainSummary *RS =
1623       getSummaryManager(Ctx).getSummary(AnyCall(MethodDecl));
1624 
1625   bool MethodIsReturnAnnotated =
1626       (MethodDecl->hasAttr<CFReturnsRetainedAttr>() ||
1627        MethodDecl->hasAttr<CFReturnsNotRetainedAttr>() ||
1628        MethodDecl->hasAttr<NSReturnsRetainedAttr>() ||
1629        MethodDecl->hasAttr<NSReturnsNotRetainedAttr>() ||
1630        MethodDecl->hasAttr<NSReturnsAutoreleasedAttr>());
1631 
1632   if (RS->getReceiverEffect().getKind() == DecRef &&
1633       !MethodDecl->hasAttr<NSConsumesSelfAttr>() &&
1634       MethodDecl->getMethodFamily() != OMF_init &&
1635       MethodDecl->getMethodFamily() != OMF_release &&
1636       NSAPIObj->isMacroDefined("NS_CONSUMES_SELF")) {
1637     edit::Commit commit(*Editor);
1638     commit.insertBefore(MethodDecl->getEndLoc(), " NS_CONSUMES_SELF");
1639     Editor->commit(commit);
1640   }
1641 
1642   // Trivial case of when function is annotated and has no argument.
1643   if (MethodIsReturnAnnotated &&
1644       (MethodDecl->param_begin() == MethodDecl->param_end()))
1645     return;
1646 
1647   if (!MethodIsReturnAnnotated) {
1648     RetEffect Ret = RS->getRetEffect();
1649     if ((Ret.getObjKind() == ObjKind::CF ||
1650          Ret.getObjKind() == ObjKind::ObjC) &&
1651         (Ret.isOwned() || Ret.notOwned())) {
1652       AddCFAnnotations(Ctx, RS, MethodDecl, false);
1653       return;
1654     } else if (!AuditedType(MethodDecl->getReturnType()))
1655       return;
1656   }
1657 
1658   // At this point result type is either annotated or audited.
1659   unsigned i = 0;
1660   for (ObjCMethodDecl::param_const_iterator pi = MethodDecl->param_begin(),
1661        pe = MethodDecl->param_end(); pi != pe; ++pi, ++i) {
1662     const ParmVarDecl *pd = *pi;
1663     ArgEffect AE = RS->getArg(i);
1664     if ((AE.getKind() == DecRef && !pd->hasAttr<CFConsumedAttr>()) ||
1665         AE.getKind() == IncRef || !AuditedType(pd->getType())) {
1666       AddCFAnnotations(Ctx, RS, MethodDecl, MethodIsReturnAnnotated);
1667       return;
1668     }
1669   }
1670 }
1671 
1672 namespace {
1673 class SuperInitChecker : public RecursiveASTVisitor<SuperInitChecker> {
1674 public:
1675   bool shouldVisitTemplateInstantiations() const { return false; }
1676   bool shouldWalkTypesOfTypeLocs() const { return false; }
1677 
1678   bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
1679     if (E->getReceiverKind() == ObjCMessageExpr::SuperInstance) {
1680       if (E->getMethodFamily() == OMF_init)
1681         return false;
1682     }
1683     return true;
1684   }
1685 };
1686 } // end anonymous namespace
1687 
1688 static bool hasSuperInitCall(const ObjCMethodDecl *MD) {
1689   return !SuperInitChecker().TraverseStmt(MD->getBody());
1690 }
1691 
1692 void ObjCMigrateASTConsumer::inferDesignatedInitializers(
1693     ASTContext &Ctx,
1694     const ObjCImplementationDecl *ImplD) {
1695 
1696   const ObjCInterfaceDecl *IFace = ImplD->getClassInterface();
1697   if (!IFace || IFace->hasDesignatedInitializers())
1698     return;
1699   if (!NSAPIObj->isMacroDefined("NS_DESIGNATED_INITIALIZER"))
1700     return;
1701 
1702   for (const auto *MD : ImplD->instance_methods()) {
1703     if (MD->isDeprecated() ||
1704         MD->getMethodFamily() != OMF_init ||
1705         MD->isDesignatedInitializerForTheInterface())
1706       continue;
1707     const ObjCMethodDecl *IFaceM = IFace->getMethod(MD->getSelector(),
1708                                                     /*isInstance=*/true);
1709     if (!IFaceM)
1710       continue;
1711     if (hasSuperInitCall(MD)) {
1712       edit::Commit commit(*Editor);
1713       commit.insert(IFaceM->getEndLoc(), " NS_DESIGNATED_INITIALIZER");
1714       Editor->commit(commit);
1715     }
1716   }
1717 }
1718 
1719 bool ObjCMigrateASTConsumer::InsertFoundation(ASTContext &Ctx,
1720                                               SourceLocation  Loc) {
1721   if (FoundationIncluded)
1722     return true;
1723   if (Loc.isInvalid())
1724     return false;
1725   auto *nsEnumId = &Ctx.Idents.get("NS_ENUM");
1726   if (PP.getMacroDefinitionAtLoc(nsEnumId, Loc)) {
1727     FoundationIncluded = true;
1728     return true;
1729   }
1730   edit::Commit commit(*Editor);
1731   if (Ctx.getLangOpts().Modules)
1732     commit.insert(Loc, "#ifndef NS_ENUM\n@import Foundation;\n#endif\n");
1733   else
1734     commit.insert(Loc, "#ifndef NS_ENUM\n#import <Foundation/Foundation.h>\n#endif\n");
1735   Editor->commit(commit);
1736   FoundationIncluded = true;
1737   return true;
1738 }
1739 
1740 namespace {
1741 
1742 class RewritesReceiver : public edit::EditsReceiver {
1743   Rewriter &Rewrite;
1744 
1745 public:
1746   RewritesReceiver(Rewriter &Rewrite) : Rewrite(Rewrite) { }
1747 
1748   void insert(SourceLocation loc, StringRef text) override {
1749     Rewrite.InsertText(loc, text);
1750   }
1751   void replace(CharSourceRange range, StringRef text) override {
1752     Rewrite.ReplaceText(range.getBegin(), Rewrite.getRangeSize(range), text);
1753   }
1754 };
1755 
1756 class JSONEditWriter : public edit::EditsReceiver {
1757   SourceManager &SourceMgr;
1758   llvm::raw_ostream &OS;
1759 
1760 public:
1761   JSONEditWriter(SourceManager &SM, llvm::raw_ostream &OS)
1762     : SourceMgr(SM), OS(OS) {
1763     OS << "[\n";
1764   }
1765   ~JSONEditWriter() override { OS << "]\n"; }
1766 
1767 private:
1768   struct EntryWriter {
1769     SourceManager &SourceMgr;
1770     llvm::raw_ostream &OS;
1771 
1772     EntryWriter(SourceManager &SM, llvm::raw_ostream &OS)
1773       : SourceMgr(SM), OS(OS) {
1774       OS << " {\n";
1775     }
1776     ~EntryWriter() {
1777       OS << " },\n";
1778     }
1779 
1780     void writeLoc(SourceLocation Loc) {
1781       FileID FID;
1782       unsigned Offset;
1783       std::tie(FID, Offset) = SourceMgr.getDecomposedLoc(Loc);
1784       assert(FID.isValid());
1785       SmallString<200> Path =
1786           StringRef(SourceMgr.getFileEntryRefForID(FID)->getName());
1787       llvm::sys::fs::make_absolute(Path);
1788       OS << "  \"file\": \"";
1789       OS.write_escaped(Path.str()) << "\",\n";
1790       OS << "  \"offset\": " << Offset << ",\n";
1791     }
1792 
1793     void writeRemove(CharSourceRange Range) {
1794       assert(Range.isCharRange());
1795       std::pair<FileID, unsigned> Begin =
1796           SourceMgr.getDecomposedLoc(Range.getBegin());
1797       std::pair<FileID, unsigned> End =
1798           SourceMgr.getDecomposedLoc(Range.getEnd());
1799       assert(Begin.first == End.first);
1800       assert(Begin.second <= End.second);
1801       unsigned Length = End.second - Begin.second;
1802 
1803       OS << "  \"remove\": " << Length << ",\n";
1804     }
1805 
1806     void writeText(StringRef Text) {
1807       OS << "  \"text\": \"";
1808       OS.write_escaped(Text) << "\",\n";
1809     }
1810   };
1811 
1812   void insert(SourceLocation Loc, StringRef Text) override {
1813     EntryWriter Writer(SourceMgr, OS);
1814     Writer.writeLoc(Loc);
1815     Writer.writeText(Text);
1816   }
1817 
1818   void replace(CharSourceRange Range, StringRef Text) override {
1819     EntryWriter Writer(SourceMgr, OS);
1820     Writer.writeLoc(Range.getBegin());
1821     Writer.writeRemove(Range);
1822     Writer.writeText(Text);
1823   }
1824 
1825   void remove(CharSourceRange Range) override {
1826     EntryWriter Writer(SourceMgr, OS);
1827     Writer.writeLoc(Range.getBegin());
1828     Writer.writeRemove(Range);
1829   }
1830 };
1831 
1832 } // end anonymous namespace
1833 
1834 void ObjCMigrateASTConsumer::HandleTranslationUnit(ASTContext &Ctx) {
1835 
1836   TranslationUnitDecl *TU = Ctx.getTranslationUnitDecl();
1837   if (ASTMigrateActions & FrontendOptions::ObjCMT_MigrateDecls) {
1838     for (DeclContext::decl_iterator D = TU->decls_begin(), DEnd = TU->decls_end();
1839          D != DEnd; ++D) {
1840       FileID FID = PP.getSourceManager().getFileID((*D)->getLocation());
1841       if (FID.isValid())
1842         if (FileId.isValid() && FileId != FID) {
1843           if (ASTMigrateActions & FrontendOptions::ObjCMT_Annotation)
1844             AnnotateImplicitBridging(Ctx);
1845         }
1846 
1847       if (ObjCInterfaceDecl *CDecl = dyn_cast<ObjCInterfaceDecl>(*D))
1848         if (canModify(CDecl))
1849           migrateObjCContainerDecl(Ctx, CDecl);
1850       if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(*D)) {
1851         if (canModify(CatDecl))
1852           migrateObjCContainerDecl(Ctx, CatDecl);
1853       }
1854       else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(*D)) {
1855         ObjCProtocolDecls.insert(PDecl->getCanonicalDecl());
1856         if (canModify(PDecl))
1857           migrateObjCContainerDecl(Ctx, PDecl);
1858       }
1859       else if (const ObjCImplementationDecl *ImpDecl =
1860                dyn_cast<ObjCImplementationDecl>(*D)) {
1861         if ((ASTMigrateActions & FrontendOptions::ObjCMT_ProtocolConformance) &&
1862             canModify(ImpDecl))
1863           migrateProtocolConformance(Ctx, ImpDecl);
1864       }
1865       else if (const EnumDecl *ED = dyn_cast<EnumDecl>(*D)) {
1866         if (!(ASTMigrateActions & FrontendOptions::ObjCMT_NsMacros))
1867           continue;
1868         if (!canModify(ED))
1869           continue;
1870         DeclContext::decl_iterator N = D;
1871         if (++N != DEnd) {
1872           const TypedefDecl *TD = dyn_cast<TypedefDecl>(*N);
1873           if (migrateNSEnumDecl(Ctx, ED, TD) && TD)
1874             D++;
1875         }
1876         else
1877           migrateNSEnumDecl(Ctx, ED, /*TypedefDecl */nullptr);
1878       }
1879       else if (const TypedefDecl *TD = dyn_cast<TypedefDecl>(*D)) {
1880         if (!(ASTMigrateActions & FrontendOptions::ObjCMT_NsMacros))
1881           continue;
1882         if (!canModify(TD))
1883           continue;
1884         DeclContext::decl_iterator N = D;
1885         if (++N == DEnd)
1886           continue;
1887         if (const EnumDecl *ED = dyn_cast<EnumDecl>(*N)) {
1888           if (canModify(ED)) {
1889             if (++N != DEnd)
1890               if (const TypedefDecl *TDF = dyn_cast<TypedefDecl>(*N)) {
1891                 // prefer typedef-follows-enum to enum-follows-typedef pattern.
1892                 if (migrateNSEnumDecl(Ctx, ED, TDF)) {
1893                   ++D; ++D;
1894                   CacheObjCNSIntegerTypedefed(TD);
1895                   continue;
1896                 }
1897               }
1898             if (migrateNSEnumDecl(Ctx, ED, TD)) {
1899               ++D;
1900               continue;
1901             }
1902           }
1903         }
1904         CacheObjCNSIntegerTypedefed(TD);
1905       }
1906       else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*D)) {
1907         if ((ASTMigrateActions & FrontendOptions::ObjCMT_Annotation) &&
1908             canModify(FD))
1909           migrateCFAnnotation(Ctx, FD);
1910       }
1911 
1912       if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(*D)) {
1913         bool CanModify = canModify(CDecl);
1914         // migrate methods which can have instancetype as their result type.
1915         if ((ASTMigrateActions & FrontendOptions::ObjCMT_Instancetype) &&
1916             CanModify)
1917           migrateAllMethodInstaceType(Ctx, CDecl);
1918         // annotate methods with CF annotations.
1919         if ((ASTMigrateActions & FrontendOptions::ObjCMT_Annotation) &&
1920             CanModify)
1921           migrateARCSafeAnnotation(Ctx, CDecl);
1922       }
1923 
1924       if (const ObjCImplementationDecl *
1925             ImplD = dyn_cast<ObjCImplementationDecl>(*D)) {
1926         if ((ASTMigrateActions & FrontendOptions::ObjCMT_DesignatedInitializer) &&
1927             canModify(ImplD))
1928           inferDesignatedInitializers(Ctx, ImplD);
1929       }
1930     }
1931     if (ASTMigrateActions & FrontendOptions::ObjCMT_Annotation)
1932       AnnotateImplicitBridging(Ctx);
1933   }
1934 
1935  if (IsOutputFile) {
1936    std::error_code EC;
1937    llvm::raw_fd_ostream OS(MigrateDir, EC, llvm::sys::fs::OF_None);
1938    if (EC) {
1939       DiagnosticsEngine &Diags = Ctx.getDiagnostics();
1940       Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0"))
1941           << EC.message();
1942       return;
1943     }
1944 
1945    JSONEditWriter Writer(Ctx.getSourceManager(), OS);
1946    Editor->applyRewrites(Writer);
1947    return;
1948  }
1949 
1950   Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts());
1951   RewritesReceiver Rec(rewriter);
1952   Editor->applyRewrites(Rec);
1953 
1954   for (Rewriter::buffer_iterator
1955         I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) {
1956     FileID FID = I->first;
1957     RewriteBuffer &buf = I->second;
1958     OptionalFileEntryRef file =
1959         Ctx.getSourceManager().getFileEntryRefForID(FID);
1960     assert(file);
1961     SmallString<512> newText;
1962     llvm::raw_svector_ostream vecOS(newText);
1963     buf.write(vecOS);
1964     std::unique_ptr<llvm::MemoryBuffer> memBuf(
1965         llvm::MemoryBuffer::getMemBufferCopy(
1966             StringRef(newText.data(), newText.size()), file->getName()));
1967     SmallString<64> filePath(file->getName());
1968     FileMgr.FixupRelativePath(filePath);
1969     Remapper.remap(filePath.str(), std::move(memBuf));
1970   }
1971 
1972   if (IsOutputFile) {
1973     Remapper.flushToFile(MigrateDir, Ctx.getDiagnostics());
1974   } else {
1975     Remapper.flushToDisk(MigrateDir, Ctx.getDiagnostics());
1976   }
1977 }
1978 
1979 bool MigrateSourceAction::BeginInvocation(CompilerInstance &CI) {
1980   CI.getDiagnostics().setIgnoreAllWarnings(true);
1981   return true;
1982 }
1983 
1984 static std::vector<std::string> getAllowListFilenames(StringRef DirPath) {
1985   using namespace llvm::sys::fs;
1986   using namespace llvm::sys::path;
1987 
1988   std::vector<std::string> Filenames;
1989   if (DirPath.empty() || !is_directory(DirPath))
1990     return Filenames;
1991 
1992   std::error_code EC;
1993   directory_iterator DI = directory_iterator(DirPath, EC);
1994   directory_iterator DE;
1995   for (; !EC && DI != DE; DI = DI.increment(EC)) {
1996     if (is_regular_file(DI->path()))
1997       Filenames.push_back(std::string(filename(DI->path())));
1998   }
1999 
2000   return Filenames;
2001 }
2002 
2003 std::unique_ptr<ASTConsumer>
2004 MigrateSourceAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
2005   PPConditionalDirectiveRecord *
2006     PPRec = new PPConditionalDirectiveRecord(CI.getSourceManager());
2007   unsigned ObjCMTAction = CI.getFrontendOpts().ObjCMTAction;
2008   unsigned ObjCMTOpts = ObjCMTAction;
2009   // These are companion flags, they do not enable transformations.
2010   ObjCMTOpts &= ~(FrontendOptions::ObjCMT_AtomicProperty |
2011                   FrontendOptions::ObjCMT_NsAtomicIOSOnlyProperty);
2012   if (ObjCMTOpts == FrontendOptions::ObjCMT_None) {
2013     // If no specific option was given, enable literals+subscripting transforms
2014     // by default.
2015     ObjCMTAction |=
2016         FrontendOptions::ObjCMT_Literals | FrontendOptions::ObjCMT_Subscripting;
2017   }
2018   CI.getPreprocessor().addPPCallbacks(std::unique_ptr<PPCallbacks>(PPRec));
2019   std::vector<std::string> AllowList =
2020       getAllowListFilenames(CI.getFrontendOpts().ObjCMTAllowListPath);
2021   return std::make_unique<ObjCMigrateASTConsumer>(
2022       CI.getFrontendOpts().OutputFile, ObjCMTAction, Remapper,
2023       CI.getFileManager(), PPRec, CI.getPreprocessor(),
2024       /*isOutputFile=*/true, AllowList);
2025 }
2026 
2027 namespace {
2028 struct EditEntry {
2029   OptionalFileEntryRef File;
2030   unsigned Offset = 0;
2031   unsigned RemoveLen = 0;
2032   std::string Text;
2033 };
2034 } // end anonymous namespace
2035 
2036 namespace llvm {
2037 template<> struct DenseMapInfo<EditEntry> {
2038   static inline EditEntry getEmptyKey() {
2039     EditEntry Entry;
2040     Entry.Offset = unsigned(-1);
2041     return Entry;
2042   }
2043   static inline EditEntry getTombstoneKey() {
2044     EditEntry Entry;
2045     Entry.Offset = unsigned(-2);
2046     return Entry;
2047   }
2048   static unsigned getHashValue(const EditEntry& Val) {
2049     return (unsigned)llvm::hash_combine(Val.File, Val.Offset, Val.RemoveLen,
2050                                         Val.Text);
2051   }
2052   static bool isEqual(const EditEntry &LHS, const EditEntry &RHS) {
2053     return LHS.File == RHS.File &&
2054         LHS.Offset == RHS.Offset &&
2055         LHS.RemoveLen == RHS.RemoveLen &&
2056         LHS.Text == RHS.Text;
2057   }
2058 };
2059 } // end namespace llvm
2060 
2061 namespace {
2062 class RemapFileParser {
2063   FileManager &FileMgr;
2064 
2065 public:
2066   RemapFileParser(FileManager &FileMgr) : FileMgr(FileMgr) { }
2067 
2068   bool parse(StringRef File, SmallVectorImpl<EditEntry> &Entries) {
2069     using namespace llvm::yaml;
2070 
2071     llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
2072         llvm::MemoryBuffer::getFile(File);
2073     if (!FileBufOrErr)
2074       return true;
2075 
2076     llvm::SourceMgr SM;
2077     Stream YAMLStream(FileBufOrErr.get()->getMemBufferRef(), SM);
2078     document_iterator I = YAMLStream.begin();
2079     if (I == YAMLStream.end())
2080       return true;
2081     Node *Root = I->getRoot();
2082     if (!Root)
2083       return true;
2084 
2085     SequenceNode *SeqNode = dyn_cast<SequenceNode>(Root);
2086     if (!SeqNode)
2087       return true;
2088 
2089     for (SequenceNode::iterator
2090            AI = SeqNode->begin(), AE = SeqNode->end(); AI != AE; ++AI) {
2091       MappingNode *MapNode = dyn_cast<MappingNode>(&*AI);
2092       if (!MapNode)
2093         continue;
2094       parseEdit(MapNode, Entries);
2095     }
2096 
2097     return false;
2098   }
2099 
2100 private:
2101   void parseEdit(llvm::yaml::MappingNode *Node,
2102                  SmallVectorImpl<EditEntry> &Entries) {
2103     using namespace llvm::yaml;
2104     EditEntry Entry;
2105     bool Ignore = false;
2106 
2107     for (MappingNode::iterator
2108            KVI = Node->begin(), KVE = Node->end(); KVI != KVE; ++KVI) {
2109       ScalarNode *KeyString = dyn_cast<ScalarNode>((*KVI).getKey());
2110       if (!KeyString)
2111         continue;
2112       SmallString<10> KeyStorage;
2113       StringRef Key = KeyString->getValue(KeyStorage);
2114 
2115       ScalarNode *ValueString = dyn_cast<ScalarNode>((*KVI).getValue());
2116       if (!ValueString)
2117         continue;
2118       SmallString<64> ValueStorage;
2119       StringRef Val = ValueString->getValue(ValueStorage);
2120 
2121       if (Key == "file") {
2122         if (auto File = FileMgr.getOptionalFileRef(Val))
2123           Entry.File = File;
2124         else
2125           Ignore = true;
2126       } else if (Key == "offset") {
2127         if (Val.getAsInteger(10, Entry.Offset))
2128           Ignore = true;
2129       } else if (Key == "remove") {
2130         if (Val.getAsInteger(10, Entry.RemoveLen))
2131           Ignore = true;
2132       } else if (Key == "text") {
2133         Entry.Text = std::string(Val);
2134       }
2135     }
2136 
2137     if (!Ignore)
2138       Entries.push_back(Entry);
2139   }
2140 };
2141 } // end anonymous namespace
2142 
2143 static bool reportDiag(const Twine &Err, DiagnosticsEngine &Diag) {
2144   Diag.Report(Diag.getCustomDiagID(DiagnosticsEngine::Error, "%0"))
2145       << Err.str();
2146   return true;
2147 }
2148 
2149 static std::string applyEditsToTemp(FileEntryRef FE,
2150                                     ArrayRef<EditEntry> Edits,
2151                                     FileManager &FileMgr,
2152                                     DiagnosticsEngine &Diag) {
2153   using namespace llvm::sys;
2154 
2155   SourceManager SM(Diag, FileMgr);
2156   FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User);
2157   LangOptions LangOpts;
2158   edit::EditedSource Editor(SM, LangOpts);
2159   for (ArrayRef<EditEntry>::iterator
2160         I = Edits.begin(), E = Edits.end(); I != E; ++I) {
2161     const EditEntry &Entry = *I;
2162     assert(Entry.File == FE);
2163     SourceLocation Loc =
2164         SM.getLocForStartOfFile(FID).getLocWithOffset(Entry.Offset);
2165     CharSourceRange Range;
2166     if (Entry.RemoveLen != 0) {
2167       Range = CharSourceRange::getCharRange(Loc,
2168                                          Loc.getLocWithOffset(Entry.RemoveLen));
2169     }
2170 
2171     edit::Commit commit(Editor);
2172     if (Range.isInvalid()) {
2173       commit.insert(Loc, Entry.Text);
2174     } else if (Entry.Text.empty()) {
2175       commit.remove(Range);
2176     } else {
2177       commit.replace(Range, Entry.Text);
2178     }
2179     Editor.commit(commit);
2180   }
2181 
2182   Rewriter rewriter(SM, LangOpts);
2183   RewritesReceiver Rec(rewriter);
2184   Editor.applyRewrites(Rec, /*adjustRemovals=*/false);
2185 
2186   const RewriteBuffer *Buf = rewriter.getRewriteBufferFor(FID);
2187   SmallString<512> NewText;
2188   llvm::raw_svector_ostream OS(NewText);
2189   Buf->write(OS);
2190 
2191   SmallString<64> TempPath;
2192   int FD;
2193   if (fs::createTemporaryFile(path::filename(FE.getName()),
2194                               path::extension(FE.getName()).drop_front(), FD,
2195                               TempPath)) {
2196     reportDiag("Could not create file: " + TempPath.str(), Diag);
2197     return std::string();
2198   }
2199 
2200   llvm::raw_fd_ostream TmpOut(FD, /*shouldClose=*/true);
2201   TmpOut.write(NewText.data(), NewText.size());
2202   TmpOut.close();
2203 
2204   return std::string(TempPath);
2205 }
2206 
2207 bool arcmt::getFileRemappingsFromFileList(
2208                         std::vector<std::pair<std::string,std::string> > &remap,
2209                         ArrayRef<StringRef> remapFiles,
2210                         DiagnosticConsumer *DiagClient) {
2211   bool hasErrorOccurred = false;
2212 
2213   FileSystemOptions FSOpts;
2214   FileManager FileMgr(FSOpts);
2215   RemapFileParser Parser(FileMgr);
2216 
2217   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
2218   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
2219       new DiagnosticsEngine(DiagID, new DiagnosticOptions,
2220                             DiagClient, /*ShouldOwnClient=*/false));
2221 
2222   typedef llvm::DenseMap<FileEntryRef, std::vector<EditEntry> >
2223       FileEditEntriesTy;
2224   FileEditEntriesTy FileEditEntries;
2225 
2226   llvm::DenseSet<EditEntry> EntriesSet;
2227 
2228   for (ArrayRef<StringRef>::iterator
2229          I = remapFiles.begin(), E = remapFiles.end(); I != E; ++I) {
2230     SmallVector<EditEntry, 16> Entries;
2231     if (Parser.parse(*I, Entries))
2232       continue;
2233 
2234     for (SmallVectorImpl<EditEntry>::iterator
2235            EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) {
2236       EditEntry &Entry = *EI;
2237       if (!Entry.File)
2238         continue;
2239       std::pair<llvm::DenseSet<EditEntry>::iterator, bool>
2240         Insert = EntriesSet.insert(Entry);
2241       if (!Insert.second)
2242         continue;
2243 
2244       FileEditEntries[*Entry.File].push_back(Entry);
2245     }
2246   }
2247 
2248   for (FileEditEntriesTy::iterator
2249          I = FileEditEntries.begin(), E = FileEditEntries.end(); I != E; ++I) {
2250     std::string TempFile = applyEditsToTemp(I->first, I->second,
2251                                             FileMgr, *Diags);
2252     if (TempFile.empty()) {
2253       hasErrorOccurred = true;
2254       continue;
2255     }
2256 
2257     remap.emplace_back(std::string(I->first.getName()), TempFile);
2258   }
2259 
2260   return hasErrorOccurred;
2261 }
2262