xref: /minix3/external/bsd/llvm/dist/clang/lib/ARCMigrate/ARCMT.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- ARCMT.cpp - Migration to ARC mode --------------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc #include "Internals.h"
11f4a2713aSLionel Sambuc #include "clang/AST/ASTConsumer.h"
12f4a2713aSLionel Sambuc #include "clang/Basic/DiagnosticCategories.h"
13f4a2713aSLionel Sambuc #include "clang/Frontend/ASTUnit.h"
14f4a2713aSLionel Sambuc #include "clang/Frontend/CompilerInstance.h"
15f4a2713aSLionel Sambuc #include "clang/Frontend/FrontendAction.h"
16f4a2713aSLionel Sambuc #include "clang/Frontend/TextDiagnosticPrinter.h"
17f4a2713aSLionel Sambuc #include "clang/Frontend/Utils.h"
18f4a2713aSLionel Sambuc #include "clang/Lex/Preprocessor.h"
19f4a2713aSLionel Sambuc #include "clang/Rewrite/Core/Rewriter.h"
20f4a2713aSLionel Sambuc #include "clang/Sema/SemaDiagnostic.h"
21f4a2713aSLionel Sambuc #include "clang/Serialization/ASTReader.h"
22f4a2713aSLionel Sambuc #include "llvm/ADT/Triple.h"
23f4a2713aSLionel Sambuc #include "llvm/Support/MemoryBuffer.h"
24f4a2713aSLionel Sambuc using namespace clang;
25f4a2713aSLionel Sambuc using namespace arcmt;
26f4a2713aSLionel Sambuc 
clearDiagnostic(ArrayRef<unsigned> IDs,SourceRange range)27f4a2713aSLionel Sambuc bool CapturedDiagList::clearDiagnostic(ArrayRef<unsigned> IDs,
28f4a2713aSLionel Sambuc                                        SourceRange range) {
29f4a2713aSLionel Sambuc   if (range.isInvalid())
30f4a2713aSLionel Sambuc     return false;
31f4a2713aSLionel Sambuc 
32f4a2713aSLionel Sambuc   bool cleared = false;
33f4a2713aSLionel Sambuc   ListTy::iterator I = List.begin();
34f4a2713aSLionel Sambuc   while (I != List.end()) {
35f4a2713aSLionel Sambuc     FullSourceLoc diagLoc = I->getLocation();
36f4a2713aSLionel Sambuc     if ((IDs.empty() || // empty means clear all diagnostics in the range.
37f4a2713aSLionel Sambuc          std::find(IDs.begin(), IDs.end(), I->getID()) != IDs.end()) &&
38f4a2713aSLionel Sambuc         !diagLoc.isBeforeInTranslationUnitThan(range.getBegin()) &&
39f4a2713aSLionel Sambuc         (diagLoc == range.getEnd() ||
40f4a2713aSLionel Sambuc            diagLoc.isBeforeInTranslationUnitThan(range.getEnd()))) {
41f4a2713aSLionel Sambuc       cleared = true;
42f4a2713aSLionel Sambuc       ListTy::iterator eraseS = I++;
43f4a2713aSLionel Sambuc       if (eraseS->getLevel() != DiagnosticsEngine::Note)
44f4a2713aSLionel Sambuc         while (I != List.end() && I->getLevel() == DiagnosticsEngine::Note)
45f4a2713aSLionel Sambuc           ++I;
46f4a2713aSLionel Sambuc       // Clear the diagnostic and any notes following it.
47f4a2713aSLionel Sambuc       I = List.erase(eraseS, I);
48f4a2713aSLionel Sambuc       continue;
49f4a2713aSLionel Sambuc     }
50f4a2713aSLionel Sambuc 
51f4a2713aSLionel Sambuc     ++I;
52f4a2713aSLionel Sambuc   }
53f4a2713aSLionel Sambuc 
54f4a2713aSLionel Sambuc   return cleared;
55f4a2713aSLionel Sambuc }
56f4a2713aSLionel Sambuc 
hasDiagnostic(ArrayRef<unsigned> IDs,SourceRange range) const57f4a2713aSLionel Sambuc bool CapturedDiagList::hasDiagnostic(ArrayRef<unsigned> IDs,
58f4a2713aSLionel Sambuc                                      SourceRange range) const {
59f4a2713aSLionel Sambuc   if (range.isInvalid())
60f4a2713aSLionel Sambuc     return false;
61f4a2713aSLionel Sambuc 
62f4a2713aSLionel Sambuc   ListTy::const_iterator I = List.begin();
63f4a2713aSLionel Sambuc   while (I != List.end()) {
64f4a2713aSLionel Sambuc     FullSourceLoc diagLoc = I->getLocation();
65f4a2713aSLionel Sambuc     if ((IDs.empty() || // empty means any diagnostic in the range.
66f4a2713aSLionel Sambuc          std::find(IDs.begin(), IDs.end(), I->getID()) != IDs.end()) &&
67f4a2713aSLionel Sambuc         !diagLoc.isBeforeInTranslationUnitThan(range.getBegin()) &&
68f4a2713aSLionel Sambuc         (diagLoc == range.getEnd() ||
69f4a2713aSLionel Sambuc            diagLoc.isBeforeInTranslationUnitThan(range.getEnd()))) {
70f4a2713aSLionel Sambuc       return true;
71f4a2713aSLionel Sambuc     }
72f4a2713aSLionel Sambuc 
73f4a2713aSLionel Sambuc     ++I;
74f4a2713aSLionel Sambuc   }
75f4a2713aSLionel Sambuc 
76f4a2713aSLionel Sambuc   return false;
77f4a2713aSLionel Sambuc }
78f4a2713aSLionel Sambuc 
reportDiagnostics(DiagnosticsEngine & Diags) const79f4a2713aSLionel Sambuc void CapturedDiagList::reportDiagnostics(DiagnosticsEngine &Diags) const {
80f4a2713aSLionel Sambuc   for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I)
81f4a2713aSLionel Sambuc     Diags.Report(*I);
82f4a2713aSLionel Sambuc }
83f4a2713aSLionel Sambuc 
hasErrors() const84f4a2713aSLionel Sambuc bool CapturedDiagList::hasErrors() const {
85f4a2713aSLionel Sambuc   for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I)
86f4a2713aSLionel Sambuc     if (I->getLevel() >= DiagnosticsEngine::Error)
87f4a2713aSLionel Sambuc       return true;
88f4a2713aSLionel Sambuc 
89f4a2713aSLionel Sambuc   return false;
90f4a2713aSLionel Sambuc }
91f4a2713aSLionel Sambuc 
92f4a2713aSLionel Sambuc namespace {
93f4a2713aSLionel Sambuc 
94f4a2713aSLionel Sambuc class CaptureDiagnosticConsumer : public DiagnosticConsumer {
95f4a2713aSLionel Sambuc   DiagnosticsEngine &Diags;
96f4a2713aSLionel Sambuc   DiagnosticConsumer &DiagClient;
97f4a2713aSLionel Sambuc   CapturedDiagList &CapturedDiags;
98f4a2713aSLionel Sambuc   bool HasBegunSourceFile;
99f4a2713aSLionel Sambuc public:
CaptureDiagnosticConsumer(DiagnosticsEngine & diags,DiagnosticConsumer & client,CapturedDiagList & capturedDiags)100f4a2713aSLionel Sambuc   CaptureDiagnosticConsumer(DiagnosticsEngine &diags,
101f4a2713aSLionel Sambuc                             DiagnosticConsumer &client,
102f4a2713aSLionel Sambuc                             CapturedDiagList &capturedDiags)
103f4a2713aSLionel Sambuc     : Diags(diags), DiagClient(client), CapturedDiags(capturedDiags),
104f4a2713aSLionel Sambuc       HasBegunSourceFile(false) { }
105f4a2713aSLionel Sambuc 
BeginSourceFile(const LangOptions & Opts,const Preprocessor * PP)106*0a6a1f1dSLionel Sambuc   void BeginSourceFile(const LangOptions &Opts,
107*0a6a1f1dSLionel Sambuc                        const Preprocessor *PP) override {
108f4a2713aSLionel Sambuc     // Pass BeginSourceFile message onto DiagClient on first call.
109f4a2713aSLionel Sambuc     // The corresponding EndSourceFile call will be made from an
110f4a2713aSLionel Sambuc     // explicit call to FinishCapture.
111f4a2713aSLionel Sambuc     if (!HasBegunSourceFile) {
112f4a2713aSLionel Sambuc       DiagClient.BeginSourceFile(Opts, PP);
113f4a2713aSLionel Sambuc       HasBegunSourceFile = true;
114f4a2713aSLionel Sambuc     }
115f4a2713aSLionel Sambuc   }
116f4a2713aSLionel Sambuc 
FinishCapture()117f4a2713aSLionel Sambuc   void FinishCapture() {
118f4a2713aSLionel Sambuc     // Call EndSourceFile on DiagClient on completion of capture to
119f4a2713aSLionel Sambuc     // enable VerifyDiagnosticConsumer to check diagnostics *after*
120f4a2713aSLionel Sambuc     // it has received the diagnostic list.
121f4a2713aSLionel Sambuc     if (HasBegunSourceFile) {
122f4a2713aSLionel Sambuc       DiagClient.EndSourceFile();
123f4a2713aSLionel Sambuc       HasBegunSourceFile = false;
124f4a2713aSLionel Sambuc     }
125f4a2713aSLionel Sambuc   }
126f4a2713aSLionel Sambuc 
~CaptureDiagnosticConsumer()127f4a2713aSLionel Sambuc   virtual ~CaptureDiagnosticConsumer() {
128f4a2713aSLionel Sambuc     assert(!HasBegunSourceFile && "FinishCapture not called!");
129f4a2713aSLionel Sambuc   }
130f4a2713aSLionel Sambuc 
HandleDiagnostic(DiagnosticsEngine::Level level,const Diagnostic & Info)131*0a6a1f1dSLionel Sambuc   void HandleDiagnostic(DiagnosticsEngine::Level level,
132*0a6a1f1dSLionel Sambuc                         const Diagnostic &Info) override {
133f4a2713aSLionel Sambuc     if (DiagnosticIDs::isARCDiagnostic(Info.getID()) ||
134f4a2713aSLionel Sambuc         level >= DiagnosticsEngine::Error || level == DiagnosticsEngine::Note) {
135f4a2713aSLionel Sambuc       if (Info.getLocation().isValid())
136f4a2713aSLionel Sambuc         CapturedDiags.push_back(StoredDiagnostic(level, Info));
137f4a2713aSLionel Sambuc       return;
138f4a2713aSLionel Sambuc     }
139f4a2713aSLionel Sambuc 
140f4a2713aSLionel Sambuc     // Non-ARC warnings are ignored.
141f4a2713aSLionel Sambuc     Diags.setLastDiagnosticIgnored();
142f4a2713aSLionel Sambuc   }
143f4a2713aSLionel Sambuc };
144f4a2713aSLionel Sambuc 
145f4a2713aSLionel Sambuc } // end anonymous namespace
146f4a2713aSLionel Sambuc 
HasARCRuntime(CompilerInvocation & origCI)147f4a2713aSLionel Sambuc static bool HasARCRuntime(CompilerInvocation &origCI) {
148f4a2713aSLionel Sambuc   // This duplicates some functionality from Darwin::AddDeploymentTarget
149f4a2713aSLionel Sambuc   // but this function is well defined, so keep it decoupled from the driver
150f4a2713aSLionel Sambuc   // and avoid unrelated complications.
151f4a2713aSLionel Sambuc   llvm::Triple triple(origCI.getTargetOpts().Triple);
152f4a2713aSLionel Sambuc 
153f4a2713aSLionel Sambuc   if (triple.isiOS())
154f4a2713aSLionel Sambuc     return triple.getOSMajorVersion() >= 5;
155f4a2713aSLionel Sambuc 
156f4a2713aSLionel Sambuc   if (triple.getOS() == llvm::Triple::Darwin)
157f4a2713aSLionel Sambuc     return triple.getOSMajorVersion() >= 11;
158f4a2713aSLionel Sambuc 
159f4a2713aSLionel Sambuc   if (triple.getOS() == llvm::Triple::MacOSX) {
160f4a2713aSLionel Sambuc     unsigned Major, Minor, Micro;
161f4a2713aSLionel Sambuc     triple.getOSVersion(Major, Minor, Micro);
162f4a2713aSLionel Sambuc     return Major > 10 || (Major == 10 && Minor >= 7);
163f4a2713aSLionel Sambuc   }
164f4a2713aSLionel Sambuc 
165f4a2713aSLionel Sambuc   return false;
166f4a2713aSLionel Sambuc }
167f4a2713aSLionel Sambuc 
168f4a2713aSLionel Sambuc static CompilerInvocation *
createInvocationForMigration(CompilerInvocation & origCI)169f4a2713aSLionel Sambuc createInvocationForMigration(CompilerInvocation &origCI) {
170*0a6a1f1dSLionel Sambuc   std::unique_ptr<CompilerInvocation> CInvok;
171f4a2713aSLionel Sambuc   CInvok.reset(new CompilerInvocation(origCI));
172f4a2713aSLionel Sambuc   PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts();
173f4a2713aSLionel Sambuc   if (!PPOpts.ImplicitPCHInclude.empty()) {
174f4a2713aSLionel Sambuc     // We can't use a PCH because it was likely built in non-ARC mode and we
175f4a2713aSLionel Sambuc     // want to parse in ARC. Include the original header.
176f4a2713aSLionel Sambuc     FileManager FileMgr(origCI.getFileSystemOpts());
177f4a2713aSLionel Sambuc     IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
178f4a2713aSLionel Sambuc     IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
179f4a2713aSLionel Sambuc         new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
180f4a2713aSLionel Sambuc                               new IgnoringDiagConsumer()));
181f4a2713aSLionel Sambuc     std::string OriginalFile =
182f4a2713aSLionel Sambuc         ASTReader::getOriginalSourceFile(PPOpts.ImplicitPCHInclude,
183f4a2713aSLionel Sambuc                                          FileMgr, *Diags);
184f4a2713aSLionel Sambuc     if (!OriginalFile.empty())
185f4a2713aSLionel Sambuc       PPOpts.Includes.insert(PPOpts.Includes.begin(), OriginalFile);
186f4a2713aSLionel Sambuc     PPOpts.ImplicitPCHInclude.clear();
187f4a2713aSLionel Sambuc   }
188f4a2713aSLionel Sambuc   // FIXME: Get the original header of a PTH as well.
189f4a2713aSLionel Sambuc   CInvok->getPreprocessorOpts().ImplicitPTHInclude.clear();
190f4a2713aSLionel Sambuc   std::string define = getARCMTMacroName();
191f4a2713aSLionel Sambuc   define += '=';
192f4a2713aSLionel Sambuc   CInvok->getPreprocessorOpts().addMacroDef(define);
193f4a2713aSLionel Sambuc   CInvok->getLangOpts()->ObjCAutoRefCount = true;
194f4a2713aSLionel Sambuc   CInvok->getLangOpts()->setGC(LangOptions::NonGC);
195f4a2713aSLionel Sambuc   CInvok->getDiagnosticOpts().ErrorLimit = 0;
196f4a2713aSLionel Sambuc   CInvok->getDiagnosticOpts().PedanticErrors = 0;
197f4a2713aSLionel Sambuc 
198f4a2713aSLionel Sambuc   // Ignore -Werror flags when migrating.
199f4a2713aSLionel Sambuc   std::vector<std::string> WarnOpts;
200f4a2713aSLionel Sambuc   for (std::vector<std::string>::iterator
201f4a2713aSLionel Sambuc          I = CInvok->getDiagnosticOpts().Warnings.begin(),
202f4a2713aSLionel Sambuc          E = CInvok->getDiagnosticOpts().Warnings.end(); I != E; ++I) {
203f4a2713aSLionel Sambuc     if (!StringRef(*I).startswith("error"))
204f4a2713aSLionel Sambuc       WarnOpts.push_back(*I);
205f4a2713aSLionel Sambuc   }
206f4a2713aSLionel Sambuc   WarnOpts.push_back("error=arc-unsafe-retained-assign");
207*0a6a1f1dSLionel Sambuc   CInvok->getDiagnosticOpts().Warnings = std::move(WarnOpts);
208f4a2713aSLionel Sambuc 
209f4a2713aSLionel Sambuc   CInvok->getLangOpts()->ObjCARCWeak = HasARCRuntime(origCI);
210f4a2713aSLionel Sambuc 
211*0a6a1f1dSLionel Sambuc   return CInvok.release();
212f4a2713aSLionel Sambuc }
213f4a2713aSLionel Sambuc 
emitPremigrationErrors(const CapturedDiagList & arcDiags,DiagnosticOptions * diagOpts,Preprocessor & PP)214f4a2713aSLionel Sambuc static void emitPremigrationErrors(const CapturedDiagList &arcDiags,
215f4a2713aSLionel Sambuc                                    DiagnosticOptions *diagOpts,
216f4a2713aSLionel Sambuc                                    Preprocessor &PP) {
217f4a2713aSLionel Sambuc   TextDiagnosticPrinter printer(llvm::errs(), diagOpts);
218f4a2713aSLionel Sambuc   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
219f4a2713aSLionel Sambuc   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
220f4a2713aSLionel Sambuc       new DiagnosticsEngine(DiagID, diagOpts, &printer,
221f4a2713aSLionel Sambuc                             /*ShouldOwnClient=*/false));
222f4a2713aSLionel Sambuc   Diags->setSourceManager(&PP.getSourceManager());
223f4a2713aSLionel Sambuc 
224f4a2713aSLionel Sambuc   printer.BeginSourceFile(PP.getLangOpts(), &PP);
225f4a2713aSLionel Sambuc   arcDiags.reportDiagnostics(*Diags);
226f4a2713aSLionel Sambuc   printer.EndSourceFile();
227f4a2713aSLionel Sambuc }
228f4a2713aSLionel Sambuc 
229f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
230f4a2713aSLionel Sambuc // checkForManualIssues.
231f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
232f4a2713aSLionel Sambuc 
checkForManualIssues(CompilerInvocation & origCI,const FrontendInputFile & Input,DiagnosticConsumer * DiagClient,bool emitPremigrationARCErrors,StringRef plistOut)233f4a2713aSLionel Sambuc bool arcmt::checkForManualIssues(CompilerInvocation &origCI,
234f4a2713aSLionel Sambuc                                  const FrontendInputFile &Input,
235f4a2713aSLionel Sambuc                                  DiagnosticConsumer *DiagClient,
236f4a2713aSLionel Sambuc                                  bool emitPremigrationARCErrors,
237f4a2713aSLionel Sambuc                                  StringRef plistOut) {
238f4a2713aSLionel Sambuc   if (!origCI.getLangOpts()->ObjC1)
239f4a2713aSLionel Sambuc     return false;
240f4a2713aSLionel Sambuc 
241f4a2713aSLionel Sambuc   LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC();
242f4a2713aSLionel Sambuc   bool NoNSAllocReallocError = origCI.getMigratorOpts().NoNSAllocReallocError;
243f4a2713aSLionel Sambuc   bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval;
244f4a2713aSLionel Sambuc 
245f4a2713aSLionel Sambuc   std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode,
246f4a2713aSLionel Sambuc                                                                      NoFinalizeRemoval);
247f4a2713aSLionel Sambuc   assert(!transforms.empty());
248f4a2713aSLionel Sambuc 
249*0a6a1f1dSLionel Sambuc   std::unique_ptr<CompilerInvocation> CInvok;
250f4a2713aSLionel Sambuc   CInvok.reset(createInvocationForMigration(origCI));
251f4a2713aSLionel Sambuc   CInvok->getFrontendOpts().Inputs.clear();
252f4a2713aSLionel Sambuc   CInvok->getFrontendOpts().Inputs.push_back(Input);
253f4a2713aSLionel Sambuc 
254f4a2713aSLionel Sambuc   CapturedDiagList capturedDiags;
255f4a2713aSLionel Sambuc 
256f4a2713aSLionel Sambuc   assert(DiagClient);
257f4a2713aSLionel Sambuc   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
258f4a2713aSLionel Sambuc   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
259f4a2713aSLionel Sambuc       new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
260f4a2713aSLionel Sambuc                             DiagClient, /*ShouldOwnClient=*/false));
261f4a2713aSLionel Sambuc 
262f4a2713aSLionel Sambuc   // Filter of all diagnostics.
263f4a2713aSLionel Sambuc   CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags);
264f4a2713aSLionel Sambuc   Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
265f4a2713aSLionel Sambuc 
266*0a6a1f1dSLionel Sambuc   std::unique_ptr<ASTUnit> Unit(
267*0a6a1f1dSLionel Sambuc       ASTUnit::LoadFromCompilerInvocationAction(CInvok.release(), Diags));
268f4a2713aSLionel Sambuc   if (!Unit) {
269f4a2713aSLionel Sambuc     errRec.FinishCapture();
270f4a2713aSLionel Sambuc     return true;
271f4a2713aSLionel Sambuc   }
272f4a2713aSLionel Sambuc 
273f4a2713aSLionel Sambuc   // Don't filter diagnostics anymore.
274f4a2713aSLionel Sambuc   Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
275f4a2713aSLionel Sambuc 
276f4a2713aSLionel Sambuc   ASTContext &Ctx = Unit->getASTContext();
277f4a2713aSLionel Sambuc 
278f4a2713aSLionel Sambuc   if (Diags->hasFatalErrorOccurred()) {
279f4a2713aSLionel Sambuc     Diags->Reset();
280f4a2713aSLionel Sambuc     DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
281f4a2713aSLionel Sambuc     capturedDiags.reportDiagnostics(*Diags);
282f4a2713aSLionel Sambuc     DiagClient->EndSourceFile();
283f4a2713aSLionel Sambuc     errRec.FinishCapture();
284f4a2713aSLionel Sambuc     return true;
285f4a2713aSLionel Sambuc   }
286f4a2713aSLionel Sambuc 
287f4a2713aSLionel Sambuc   if (emitPremigrationARCErrors)
288f4a2713aSLionel Sambuc     emitPremigrationErrors(capturedDiags, &origCI.getDiagnosticOpts(),
289f4a2713aSLionel Sambuc                            Unit->getPreprocessor());
290f4a2713aSLionel Sambuc   if (!plistOut.empty()) {
291f4a2713aSLionel Sambuc     SmallVector<StoredDiagnostic, 8> arcDiags;
292f4a2713aSLionel Sambuc     for (CapturedDiagList::iterator
293f4a2713aSLionel Sambuc            I = capturedDiags.begin(), E = capturedDiags.end(); I != E; ++I)
294f4a2713aSLionel Sambuc       arcDiags.push_back(*I);
295f4a2713aSLionel Sambuc     writeARCDiagsToPlist(plistOut, arcDiags,
296f4a2713aSLionel Sambuc                          Ctx.getSourceManager(), Ctx.getLangOpts());
297f4a2713aSLionel Sambuc   }
298f4a2713aSLionel Sambuc 
299f4a2713aSLionel Sambuc   // After parsing of source files ended, we want to reuse the
300f4a2713aSLionel Sambuc   // diagnostics objects to emit further diagnostics.
301f4a2713aSLionel Sambuc   // We call BeginSourceFile because DiagnosticConsumer requires that
302f4a2713aSLionel Sambuc   // diagnostics with source range information are emitted only in between
303f4a2713aSLionel Sambuc   // BeginSourceFile() and EndSourceFile().
304f4a2713aSLionel Sambuc   DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
305f4a2713aSLionel Sambuc 
306f4a2713aSLionel Sambuc   // No macros will be added since we are just checking and we won't modify
307f4a2713aSLionel Sambuc   // source code.
308f4a2713aSLionel Sambuc   std::vector<SourceLocation> ARCMTMacroLocs;
309f4a2713aSLionel Sambuc 
310f4a2713aSLionel Sambuc   TransformActions testAct(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
311f4a2713aSLionel Sambuc   MigrationPass pass(Ctx, OrigGCMode, Unit->getSema(), testAct, capturedDiags,
312f4a2713aSLionel Sambuc                      ARCMTMacroLocs);
313f4a2713aSLionel Sambuc   pass.setNoFinalizeRemoval(NoFinalizeRemoval);
314*0a6a1f1dSLionel Sambuc   if (!NoNSAllocReallocError)
315*0a6a1f1dSLionel Sambuc     Diags->setSeverity(diag::warn_arcmt_nsalloc_realloc, diag::Severity::Error,
316*0a6a1f1dSLionel Sambuc                        SourceLocation());
317f4a2713aSLionel Sambuc 
318f4a2713aSLionel Sambuc   for (unsigned i=0, e = transforms.size(); i != e; ++i)
319f4a2713aSLionel Sambuc     transforms[i](pass);
320f4a2713aSLionel Sambuc 
321f4a2713aSLionel Sambuc   capturedDiags.reportDiagnostics(*Diags);
322f4a2713aSLionel Sambuc 
323f4a2713aSLionel Sambuc   DiagClient->EndSourceFile();
324f4a2713aSLionel Sambuc   errRec.FinishCapture();
325f4a2713aSLionel Sambuc 
326f4a2713aSLionel Sambuc   return capturedDiags.hasErrors() || testAct.hasReportedErrors();
327f4a2713aSLionel Sambuc }
328f4a2713aSLionel Sambuc 
329f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
330f4a2713aSLionel Sambuc // applyTransformations.
331f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
332f4a2713aSLionel Sambuc 
applyTransforms(CompilerInvocation & origCI,const FrontendInputFile & Input,DiagnosticConsumer * DiagClient,StringRef outputDir,bool emitPremigrationARCErrors,StringRef plistOut)333f4a2713aSLionel Sambuc static bool applyTransforms(CompilerInvocation &origCI,
334f4a2713aSLionel Sambuc                             const FrontendInputFile &Input,
335f4a2713aSLionel Sambuc                             DiagnosticConsumer *DiagClient,
336f4a2713aSLionel Sambuc                             StringRef outputDir,
337f4a2713aSLionel Sambuc                             bool emitPremigrationARCErrors,
338f4a2713aSLionel Sambuc                             StringRef plistOut) {
339f4a2713aSLionel Sambuc   if (!origCI.getLangOpts()->ObjC1)
340f4a2713aSLionel Sambuc     return false;
341f4a2713aSLionel Sambuc 
342f4a2713aSLionel Sambuc   LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC();
343f4a2713aSLionel Sambuc 
344f4a2713aSLionel Sambuc   // Make sure checking is successful first.
345f4a2713aSLionel Sambuc   CompilerInvocation CInvokForCheck(origCI);
346f4a2713aSLionel Sambuc   if (arcmt::checkForManualIssues(CInvokForCheck, Input, DiagClient,
347f4a2713aSLionel Sambuc                                   emitPremigrationARCErrors, plistOut))
348f4a2713aSLionel Sambuc     return true;
349f4a2713aSLionel Sambuc 
350f4a2713aSLionel Sambuc   CompilerInvocation CInvok(origCI);
351f4a2713aSLionel Sambuc   CInvok.getFrontendOpts().Inputs.clear();
352f4a2713aSLionel Sambuc   CInvok.getFrontendOpts().Inputs.push_back(Input);
353f4a2713aSLionel Sambuc 
354f4a2713aSLionel Sambuc   MigrationProcess migration(CInvok, DiagClient, outputDir);
355f4a2713aSLionel Sambuc   bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval;
356f4a2713aSLionel Sambuc 
357f4a2713aSLionel Sambuc   std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode,
358f4a2713aSLionel Sambuc                                                                      NoFinalizeRemoval);
359f4a2713aSLionel Sambuc   assert(!transforms.empty());
360f4a2713aSLionel Sambuc 
361f4a2713aSLionel Sambuc   for (unsigned i=0, e = transforms.size(); i != e; ++i) {
362f4a2713aSLionel Sambuc     bool err = migration.applyTransform(transforms[i]);
363f4a2713aSLionel Sambuc     if (err) return true;
364f4a2713aSLionel Sambuc   }
365f4a2713aSLionel Sambuc 
366f4a2713aSLionel Sambuc   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
367f4a2713aSLionel Sambuc   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
368f4a2713aSLionel Sambuc       new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
369f4a2713aSLionel Sambuc                             DiagClient, /*ShouldOwnClient=*/false));
370f4a2713aSLionel Sambuc 
371f4a2713aSLionel Sambuc   if (outputDir.empty()) {
372f4a2713aSLionel Sambuc     origCI.getLangOpts()->ObjCAutoRefCount = true;
373f4a2713aSLionel Sambuc     return migration.getRemapper().overwriteOriginal(*Diags);
374f4a2713aSLionel Sambuc   } else {
375f4a2713aSLionel Sambuc     return migration.getRemapper().flushToDisk(outputDir, *Diags);
376f4a2713aSLionel Sambuc   }
377f4a2713aSLionel Sambuc }
378f4a2713aSLionel Sambuc 
applyTransformations(CompilerInvocation & origCI,const FrontendInputFile & Input,DiagnosticConsumer * DiagClient)379f4a2713aSLionel Sambuc bool arcmt::applyTransformations(CompilerInvocation &origCI,
380f4a2713aSLionel Sambuc                                  const FrontendInputFile &Input,
381f4a2713aSLionel Sambuc                                  DiagnosticConsumer *DiagClient) {
382f4a2713aSLionel Sambuc   return applyTransforms(origCI, Input, DiagClient,
383f4a2713aSLionel Sambuc                          StringRef(), false, StringRef());
384f4a2713aSLionel Sambuc }
385f4a2713aSLionel Sambuc 
migrateWithTemporaryFiles(CompilerInvocation & origCI,const FrontendInputFile & Input,DiagnosticConsumer * DiagClient,StringRef outputDir,bool emitPremigrationARCErrors,StringRef plistOut)386f4a2713aSLionel Sambuc bool arcmt::migrateWithTemporaryFiles(CompilerInvocation &origCI,
387f4a2713aSLionel Sambuc                                       const FrontendInputFile &Input,
388f4a2713aSLionel Sambuc                                       DiagnosticConsumer *DiagClient,
389f4a2713aSLionel Sambuc                                       StringRef outputDir,
390f4a2713aSLionel Sambuc                                       bool emitPremigrationARCErrors,
391f4a2713aSLionel Sambuc                                       StringRef plistOut) {
392f4a2713aSLionel Sambuc   assert(!outputDir.empty() && "Expected output directory path");
393f4a2713aSLionel Sambuc   return applyTransforms(origCI, Input, DiagClient,
394f4a2713aSLionel Sambuc                          outputDir, emitPremigrationARCErrors, plistOut);
395f4a2713aSLionel Sambuc }
396f4a2713aSLionel Sambuc 
getFileRemappings(std::vector<std::pair<std::string,std::string>> & remap,StringRef outputDir,DiagnosticConsumer * DiagClient)397f4a2713aSLionel Sambuc bool arcmt::getFileRemappings(std::vector<std::pair<std::string,std::string> > &
398f4a2713aSLionel Sambuc                                   remap,
399f4a2713aSLionel Sambuc                               StringRef outputDir,
400f4a2713aSLionel Sambuc                               DiagnosticConsumer *DiagClient) {
401f4a2713aSLionel Sambuc   assert(!outputDir.empty());
402f4a2713aSLionel Sambuc 
403f4a2713aSLionel Sambuc   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
404f4a2713aSLionel Sambuc   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
405f4a2713aSLionel Sambuc       new DiagnosticsEngine(DiagID, new DiagnosticOptions,
406f4a2713aSLionel Sambuc                             DiagClient, /*ShouldOwnClient=*/false));
407f4a2713aSLionel Sambuc 
408f4a2713aSLionel Sambuc   FileRemapper remapper;
409f4a2713aSLionel Sambuc   bool err = remapper.initFromDisk(outputDir, *Diags,
410f4a2713aSLionel Sambuc                                    /*ignoreIfFilesChanged=*/true);
411f4a2713aSLionel Sambuc   if (err)
412f4a2713aSLionel Sambuc     return true;
413f4a2713aSLionel Sambuc 
414f4a2713aSLionel Sambuc   PreprocessorOptions PPOpts;
415f4a2713aSLionel Sambuc   remapper.applyMappings(PPOpts);
416f4a2713aSLionel Sambuc   remap = PPOpts.RemappedFiles;
417f4a2713aSLionel Sambuc 
418f4a2713aSLionel Sambuc   return false;
419f4a2713aSLionel Sambuc }
420f4a2713aSLionel Sambuc 
421f4a2713aSLionel Sambuc 
422f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
423f4a2713aSLionel Sambuc // CollectTransformActions.
424f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
425f4a2713aSLionel Sambuc 
426f4a2713aSLionel Sambuc namespace {
427f4a2713aSLionel Sambuc 
428f4a2713aSLionel Sambuc class ARCMTMacroTrackerPPCallbacks : public PPCallbacks {
429f4a2713aSLionel Sambuc   std::vector<SourceLocation> &ARCMTMacroLocs;
430f4a2713aSLionel Sambuc 
431f4a2713aSLionel Sambuc public:
ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> & ARCMTMacroLocs)432f4a2713aSLionel Sambuc   ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs)
433f4a2713aSLionel Sambuc     : ARCMTMacroLocs(ARCMTMacroLocs) { }
434f4a2713aSLionel Sambuc 
MacroExpands(const Token & MacroNameTok,const MacroDirective * MD,SourceRange Range,const MacroArgs * Args)435*0a6a1f1dSLionel Sambuc   void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
436*0a6a1f1dSLionel Sambuc                     SourceRange Range, const MacroArgs *Args) override {
437f4a2713aSLionel Sambuc     if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName())
438f4a2713aSLionel Sambuc       ARCMTMacroLocs.push_back(MacroNameTok.getLocation());
439f4a2713aSLionel Sambuc   }
440f4a2713aSLionel Sambuc };
441f4a2713aSLionel Sambuc 
442f4a2713aSLionel Sambuc class ARCMTMacroTrackerAction : public ASTFrontendAction {
443f4a2713aSLionel Sambuc   std::vector<SourceLocation> &ARCMTMacroLocs;
444f4a2713aSLionel Sambuc 
445f4a2713aSLionel Sambuc public:
ARCMTMacroTrackerAction(std::vector<SourceLocation> & ARCMTMacroLocs)446f4a2713aSLionel Sambuc   ARCMTMacroTrackerAction(std::vector<SourceLocation> &ARCMTMacroLocs)
447f4a2713aSLionel Sambuc     : ARCMTMacroLocs(ARCMTMacroLocs) { }
448f4a2713aSLionel Sambuc 
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)449*0a6a1f1dSLionel Sambuc   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
450*0a6a1f1dSLionel Sambuc                                                  StringRef InFile) override {
451f4a2713aSLionel Sambuc     CI.getPreprocessor().addPPCallbacks(
452*0a6a1f1dSLionel Sambuc                llvm::make_unique<ARCMTMacroTrackerPPCallbacks>(ARCMTMacroLocs));
453*0a6a1f1dSLionel Sambuc     return llvm::make_unique<ASTConsumer>();
454f4a2713aSLionel Sambuc   }
455f4a2713aSLionel Sambuc };
456f4a2713aSLionel Sambuc 
457f4a2713aSLionel Sambuc class RewritesApplicator : public TransformActions::RewriteReceiver {
458f4a2713aSLionel Sambuc   Rewriter &rewriter;
459f4a2713aSLionel Sambuc   MigrationProcess::RewriteListener *Listener;
460f4a2713aSLionel Sambuc 
461f4a2713aSLionel Sambuc public:
RewritesApplicator(Rewriter & rewriter,ASTContext & ctx,MigrationProcess::RewriteListener * listener)462f4a2713aSLionel Sambuc   RewritesApplicator(Rewriter &rewriter, ASTContext &ctx,
463f4a2713aSLionel Sambuc                      MigrationProcess::RewriteListener *listener)
464f4a2713aSLionel Sambuc     : rewriter(rewriter), Listener(listener) {
465f4a2713aSLionel Sambuc     if (Listener)
466f4a2713aSLionel Sambuc       Listener->start(ctx);
467f4a2713aSLionel Sambuc   }
~RewritesApplicator()468f4a2713aSLionel Sambuc   ~RewritesApplicator() {
469f4a2713aSLionel Sambuc     if (Listener)
470f4a2713aSLionel Sambuc       Listener->finish();
471f4a2713aSLionel Sambuc   }
472f4a2713aSLionel Sambuc 
insert(SourceLocation loc,StringRef text)473*0a6a1f1dSLionel Sambuc   void insert(SourceLocation loc, StringRef text) override {
474f4a2713aSLionel Sambuc     bool err = rewriter.InsertText(loc, text, /*InsertAfter=*/true,
475f4a2713aSLionel Sambuc                                    /*indentNewLines=*/true);
476f4a2713aSLionel Sambuc     if (!err && Listener)
477f4a2713aSLionel Sambuc       Listener->insert(loc, text);
478f4a2713aSLionel Sambuc   }
479f4a2713aSLionel Sambuc 
remove(CharSourceRange range)480*0a6a1f1dSLionel Sambuc   void remove(CharSourceRange range) override {
481f4a2713aSLionel Sambuc     Rewriter::RewriteOptions removeOpts;
482f4a2713aSLionel Sambuc     removeOpts.IncludeInsertsAtBeginOfRange = false;
483f4a2713aSLionel Sambuc     removeOpts.IncludeInsertsAtEndOfRange = false;
484f4a2713aSLionel Sambuc     removeOpts.RemoveLineIfEmpty = true;
485f4a2713aSLionel Sambuc 
486f4a2713aSLionel Sambuc     bool err = rewriter.RemoveText(range, removeOpts);
487f4a2713aSLionel Sambuc     if (!err && Listener)
488f4a2713aSLionel Sambuc       Listener->remove(range);
489f4a2713aSLionel Sambuc   }
490f4a2713aSLionel Sambuc 
increaseIndentation(CharSourceRange range,SourceLocation parentIndent)491*0a6a1f1dSLionel Sambuc   void increaseIndentation(CharSourceRange range,
492*0a6a1f1dSLionel Sambuc                             SourceLocation parentIndent) override {
493f4a2713aSLionel Sambuc     rewriter.IncreaseIndentation(range, parentIndent);
494f4a2713aSLionel Sambuc   }
495f4a2713aSLionel Sambuc };
496f4a2713aSLionel Sambuc 
497f4a2713aSLionel Sambuc } // end anonymous namespace.
498f4a2713aSLionel Sambuc 
499f4a2713aSLionel Sambuc /// \brief Anchor for VTable.
~RewriteListener()500f4a2713aSLionel Sambuc MigrationProcess::RewriteListener::~RewriteListener() { }
501f4a2713aSLionel Sambuc 
MigrationProcess(const CompilerInvocation & CI,DiagnosticConsumer * diagClient,StringRef outputDir)502f4a2713aSLionel Sambuc MigrationProcess::MigrationProcess(const CompilerInvocation &CI,
503f4a2713aSLionel Sambuc                                    DiagnosticConsumer *diagClient,
504f4a2713aSLionel Sambuc                                    StringRef outputDir)
505f4a2713aSLionel Sambuc   : OrigCI(CI), DiagClient(diagClient), HadARCErrors(false) {
506f4a2713aSLionel Sambuc   if (!outputDir.empty()) {
507f4a2713aSLionel Sambuc     IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
508f4a2713aSLionel Sambuc     IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
509f4a2713aSLionel Sambuc       new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(),
510f4a2713aSLionel Sambuc                             DiagClient, /*ShouldOwnClient=*/false));
511f4a2713aSLionel Sambuc     Remapper.initFromDisk(outputDir, *Diags, /*ignoreIfFilesChanges=*/true);
512f4a2713aSLionel Sambuc   }
513f4a2713aSLionel Sambuc }
514f4a2713aSLionel Sambuc 
applyTransform(TransformFn trans,RewriteListener * listener)515f4a2713aSLionel Sambuc bool MigrationProcess::applyTransform(TransformFn trans,
516f4a2713aSLionel Sambuc                                       RewriteListener *listener) {
517*0a6a1f1dSLionel Sambuc   std::unique_ptr<CompilerInvocation> CInvok;
518f4a2713aSLionel Sambuc   CInvok.reset(createInvocationForMigration(OrigCI));
519f4a2713aSLionel Sambuc   CInvok->getDiagnosticOpts().IgnoreWarnings = true;
520f4a2713aSLionel Sambuc 
521f4a2713aSLionel Sambuc   Remapper.applyMappings(CInvok->getPreprocessorOpts());
522f4a2713aSLionel Sambuc 
523f4a2713aSLionel Sambuc   CapturedDiagList capturedDiags;
524f4a2713aSLionel Sambuc   std::vector<SourceLocation> ARCMTMacroLocs;
525f4a2713aSLionel Sambuc 
526f4a2713aSLionel Sambuc   assert(DiagClient);
527f4a2713aSLionel Sambuc   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
528f4a2713aSLionel Sambuc   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
529f4a2713aSLionel Sambuc       new DiagnosticsEngine(DiagID, new DiagnosticOptions,
530f4a2713aSLionel Sambuc                             DiagClient, /*ShouldOwnClient=*/false));
531f4a2713aSLionel Sambuc 
532f4a2713aSLionel Sambuc   // Filter of all diagnostics.
533f4a2713aSLionel Sambuc   CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags);
534f4a2713aSLionel Sambuc   Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
535f4a2713aSLionel Sambuc 
536*0a6a1f1dSLionel Sambuc   std::unique_ptr<ARCMTMacroTrackerAction> ASTAction;
537f4a2713aSLionel Sambuc   ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs));
538f4a2713aSLionel Sambuc 
539*0a6a1f1dSLionel Sambuc   std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCompilerInvocationAction(
540*0a6a1f1dSLionel Sambuc       CInvok.release(), Diags, ASTAction.get()));
541f4a2713aSLionel Sambuc   if (!Unit) {
542f4a2713aSLionel Sambuc     errRec.FinishCapture();
543f4a2713aSLionel Sambuc     return true;
544f4a2713aSLionel Sambuc   }
545f4a2713aSLionel Sambuc   Unit->setOwnsRemappedFileBuffers(false); // FileRemapper manages that.
546f4a2713aSLionel Sambuc 
547f4a2713aSLionel Sambuc   HadARCErrors = HadARCErrors || capturedDiags.hasErrors();
548f4a2713aSLionel Sambuc 
549f4a2713aSLionel Sambuc   // Don't filter diagnostics anymore.
550f4a2713aSLionel Sambuc   Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
551f4a2713aSLionel Sambuc 
552f4a2713aSLionel Sambuc   ASTContext &Ctx = Unit->getASTContext();
553f4a2713aSLionel Sambuc 
554f4a2713aSLionel Sambuc   if (Diags->hasFatalErrorOccurred()) {
555f4a2713aSLionel Sambuc     Diags->Reset();
556f4a2713aSLionel Sambuc     DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
557f4a2713aSLionel Sambuc     capturedDiags.reportDiagnostics(*Diags);
558f4a2713aSLionel Sambuc     DiagClient->EndSourceFile();
559f4a2713aSLionel Sambuc     errRec.FinishCapture();
560f4a2713aSLionel Sambuc     return true;
561f4a2713aSLionel Sambuc   }
562f4a2713aSLionel Sambuc 
563f4a2713aSLionel Sambuc   // After parsing of source files ended, we want to reuse the
564f4a2713aSLionel Sambuc   // diagnostics objects to emit further diagnostics.
565f4a2713aSLionel Sambuc   // We call BeginSourceFile because DiagnosticConsumer requires that
566f4a2713aSLionel Sambuc   // diagnostics with source range information are emitted only in between
567f4a2713aSLionel Sambuc   // BeginSourceFile() and EndSourceFile().
568f4a2713aSLionel Sambuc   DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
569f4a2713aSLionel Sambuc 
570f4a2713aSLionel Sambuc   Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts());
571f4a2713aSLionel Sambuc   TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
572f4a2713aSLionel Sambuc   MigrationPass pass(Ctx, OrigCI.getLangOpts()->getGC(),
573f4a2713aSLionel Sambuc                      Unit->getSema(), TA, capturedDiags, ARCMTMacroLocs);
574f4a2713aSLionel Sambuc 
575f4a2713aSLionel Sambuc   trans(pass);
576f4a2713aSLionel Sambuc 
577f4a2713aSLionel Sambuc   {
578f4a2713aSLionel Sambuc     RewritesApplicator applicator(rewriter, Ctx, listener);
579f4a2713aSLionel Sambuc     TA.applyRewrites(applicator);
580f4a2713aSLionel Sambuc   }
581f4a2713aSLionel Sambuc 
582f4a2713aSLionel Sambuc   DiagClient->EndSourceFile();
583f4a2713aSLionel Sambuc   errRec.FinishCapture();
584f4a2713aSLionel Sambuc 
585f4a2713aSLionel Sambuc   if (DiagClient->getNumErrors())
586f4a2713aSLionel Sambuc     return true;
587f4a2713aSLionel Sambuc 
588f4a2713aSLionel Sambuc   for (Rewriter::buffer_iterator
589f4a2713aSLionel Sambuc         I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) {
590f4a2713aSLionel Sambuc     FileID FID = I->first;
591f4a2713aSLionel Sambuc     RewriteBuffer &buf = I->second;
592f4a2713aSLionel Sambuc     const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID);
593f4a2713aSLionel Sambuc     assert(file);
594f4a2713aSLionel Sambuc     std::string newFname = file->getName();
595f4a2713aSLionel Sambuc     newFname += "-trans";
596f4a2713aSLionel Sambuc     SmallString<512> newText;
597f4a2713aSLionel Sambuc     llvm::raw_svector_ostream vecOS(newText);
598f4a2713aSLionel Sambuc     buf.write(vecOS);
599f4a2713aSLionel Sambuc     vecOS.flush();
600*0a6a1f1dSLionel Sambuc     std::unique_ptr<llvm::MemoryBuffer> memBuf(
601*0a6a1f1dSLionel Sambuc         llvm::MemoryBuffer::getMemBufferCopy(
602*0a6a1f1dSLionel Sambuc             StringRef(newText.data(), newText.size()), newFname));
603f4a2713aSLionel Sambuc     SmallString<64> filePath(file->getName());
604f4a2713aSLionel Sambuc     Unit->getFileManager().FixupRelativePath(filePath);
605*0a6a1f1dSLionel Sambuc     Remapper.remap(filePath.str(), std::move(memBuf));
606f4a2713aSLionel Sambuc   }
607f4a2713aSLionel Sambuc 
608f4a2713aSLionel Sambuc   return false;
609f4a2713aSLionel Sambuc }
610