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