blob: 5779b4cbb9fdcc12fb87982bf4612a1e9c900599 [file] [log] [blame]
John McCalld70fb982011-06-15 23:25:17 +00001//===--- 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"
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +000013#include "clang/Frontend/TextDiagnosticPrinter.h"
John McCalld70fb982011-06-15 23:25:17 +000014#include "clang/Frontend/Utils.h"
15#include "clang/AST/ASTConsumer.h"
16#include "clang/Rewrite/Rewriter.h"
17#include "clang/Sema/SemaDiagnostic.h"
18#include "clang/Basic/DiagnosticCategories.h"
19#include "clang/Lex/Preprocessor.h"
20#include "llvm/Support/MemoryBuffer.h"
21#include "llvm/ADT/Triple.h"
John McCalld70fb982011-06-15 23:25:17 +000022using namespace clang;
23using namespace arcmt;
John McCalld70fb982011-06-15 23:25:17 +000024
Chris Lattner54b16772011-07-23 17:14:25 +000025bool CapturedDiagList::clearDiagnostic(ArrayRef<unsigned> IDs,
John McCalld70fb982011-06-15 23:25:17 +000026 SourceRange range) {
27 if (range.isInvalid())
28 return false;
29
30 bool cleared = false;
31 ListTy::iterator I = List.begin();
32 while (I != List.end()) {
33 FullSourceLoc diagLoc = I->getLocation();
34 if ((IDs.empty() || // empty means clear all diagnostics in the range.
35 std::find(IDs.begin(), IDs.end(), I->getID()) != IDs.end()) &&
36 !diagLoc.isBeforeInTranslationUnitThan(range.getBegin()) &&
37 (diagLoc == range.getEnd() ||
38 diagLoc.isBeforeInTranslationUnitThan(range.getEnd()))) {
39 cleared = true;
40 ListTy::iterator eraseS = I++;
41 while (I != List.end() && I->getLevel() == Diagnostic::Note)
42 ++I;
43 // Clear the diagnostic and any notes following it.
44 List.erase(eraseS, I);
45 continue;
46 }
47
48 ++I;
49 }
50
51 return cleared;
52}
53
Chris Lattner54b16772011-07-23 17:14:25 +000054bool CapturedDiagList::hasDiagnostic(ArrayRef<unsigned> IDs,
Argyrios Kyrtzidis0f3f9f72011-06-18 00:53:34 +000055 SourceRange range) const {
John McCalld70fb982011-06-15 23:25:17 +000056 if (range.isInvalid())
57 return false;
58
Argyrios Kyrtzidis0f3f9f72011-06-18 00:53:34 +000059 ListTy::const_iterator I = List.begin();
John McCalld70fb982011-06-15 23:25:17 +000060 while (I != List.end()) {
61 FullSourceLoc diagLoc = I->getLocation();
62 if ((IDs.empty() || // empty means any diagnostic in the range.
63 std::find(IDs.begin(), IDs.end(), I->getID()) != IDs.end()) &&
64 !diagLoc.isBeforeInTranslationUnitThan(range.getBegin()) &&
65 (diagLoc == range.getEnd() ||
66 diagLoc.isBeforeInTranslationUnitThan(range.getEnd()))) {
67 return true;
68 }
69
70 ++I;
71 }
72
73 return false;
74}
75
Argyrios Kyrtzidis0f3f9f72011-06-18 00:53:34 +000076void CapturedDiagList::reportDiagnostics(Diagnostic &Diags) const {
77 for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I)
John McCalld70fb982011-06-15 23:25:17 +000078 Diags.Report(*I);
79}
80
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +000081bool CapturedDiagList::hasErrors() const {
82 for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I)
83 if (I->getLevel() >= Diagnostic::Error)
84 return true;
85
86 return false;
87}
88
John McCalld70fb982011-06-15 23:25:17 +000089namespace {
90
91class CaptureDiagnosticClient : public DiagnosticClient {
92 Diagnostic &Diags;
93 CapturedDiagList &CapturedDiags;
94public:
95 CaptureDiagnosticClient(Diagnostic &diags,
96 CapturedDiagList &capturedDiags)
97 : Diags(diags), CapturedDiags(capturedDiags) { }
98
99 virtual void HandleDiagnostic(Diagnostic::Level level,
100 const DiagnosticInfo &Info) {
101 if (arcmt::isARCDiagnostic(Info.getID(), Diags) ||
102 level >= Diagnostic::Error || level == Diagnostic::Note) {
103 CapturedDiags.push_back(StoredDiagnostic(level, Info));
104 return;
105 }
106
107 // Non-ARC warnings are ignored.
108 Diags.setLastDiagnosticIgnored();
109 }
110};
111
112} // end anonymous namespace
113
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000114static inline StringRef SimulatorVersionDefineName() {
Argyrios Kyrtzidis81a35902011-06-20 19:59:52 +0000115 return "__IPHONE_OS_VERSION_MIN_REQUIRED=";
116}
117
118/// \brief Parse the simulator version define:
119/// __IPHONE_OS_VERSION_MIN_REQUIRED=([0-9])([0-9][0-9])([0-9][0-9])
120// and return the grouped values as integers, e.g:
121// __IPHONE_OS_VERSION_MIN_REQUIRED=40201
122// will return Major=4, Minor=2, Micro=1.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000123static bool GetVersionFromSimulatorDefine(StringRef define,
Argyrios Kyrtzidis81a35902011-06-20 19:59:52 +0000124 unsigned &Major, unsigned &Minor,
125 unsigned &Micro) {
126 assert(define.startswith(SimulatorVersionDefineName()));
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000127 StringRef name, version;
Argyrios Kyrtzidis81a35902011-06-20 19:59:52 +0000128 llvm::tie(name, version) = define.split('=');
129 if (version.empty())
130 return false;
131 std::string verstr = version.str();
132 char *end;
133 unsigned num = (unsigned) strtol(verstr.c_str(), &end, 10);
134 if (*end != '\0')
135 return false;
136 Major = num / 10000;
137 num = num % 10000;
138 Minor = num / 100;
139 Micro = num % 100;
140 return true;
141}
142
143static bool HasARCRuntime(CompilerInvocation &origCI) {
144 // This duplicates some functionality from Darwin::AddDeploymentTarget
145 // but this function is well defined, so keep it decoupled from the driver
146 // and avoid unrelated complications.
147
148 for (unsigned i = 0, e = origCI.getPreprocessorOpts().Macros.size();
149 i != e; ++i) {
150 StringRef define = origCI.getPreprocessorOpts().Macros[i].first;
151 bool isUndef = origCI.getPreprocessorOpts().Macros[i].second;
152 if (isUndef)
153 continue;
154 if (!define.startswith(SimulatorVersionDefineName()))
155 continue;
NAKAMURA Takumic77574a2011-08-14 00:37:22 +0000156 unsigned Major = 0, Minor = 0, Micro = 0;
Argyrios Kyrtzidis81a35902011-06-20 19:59:52 +0000157 if (GetVersionFromSimulatorDefine(define, Major, Minor, Micro) &&
158 Major < 10 && Minor < 100 && Micro < 100)
159 return Major >= 5;
160 }
161
162 llvm::Triple triple(origCI.getTargetOpts().Triple);
163
164 if (triple.getOS() == llvm::Triple::IOS)
165 return triple.getOSMajorVersion() >= 5;
166
167 if (triple.getOS() == llvm::Triple::Darwin)
168 return triple.getOSMajorVersion() >= 11;
169
170 if (triple.getOS() == llvm::Triple::MacOSX) {
171 unsigned Major, Minor, Micro;
172 triple.getOSVersion(Major, Minor, Micro);
173 return Major > 10 || (Major == 10 && Minor >= 7);
174 }
175
176 return false;
177}
178
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000179static CompilerInvocation *
180createInvocationForMigration(CompilerInvocation &origCI) {
John McCalld70fb982011-06-15 23:25:17 +0000181 llvm::OwningPtr<CompilerInvocation> CInvok;
182 CInvok.reset(new CompilerInvocation(origCI));
183 CInvok->getPreprocessorOpts().ImplicitPCHInclude = std::string();
184 CInvok->getPreprocessorOpts().ImplicitPTHInclude = std::string();
185 std::string define = getARCMTMacroName();
186 define += '=';
187 CInvok->getPreprocessorOpts().addMacroDef(define);
188 CInvok->getLangOpts().ObjCAutoRefCount = true;
189 CInvok->getDiagnosticOpts().ErrorLimit = 0;
Argyrios Kyrtzidis59b5f352011-06-22 18:03:59 +0000190 CInvok->getDiagnosticOpts().Warnings.push_back(
191 "error=arc-unsafe-retained-assign");
John McCall24fc0de2011-07-06 00:26:06 +0000192 CInvok->getLangOpts().ObjCRuntimeHasWeak = HasARCRuntime(origCI);
John McCalld70fb982011-06-15 23:25:17 +0000193
194 return CInvok.take();
195}
196
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000197static void emitPremigrationErrors(const CapturedDiagList &arcDiags,
198 const DiagnosticOptions &diagOpts,
199 Preprocessor &PP) {
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000200 TextDiagnosticPrinter printer(llvm::errs(), diagOpts);
201 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
202 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(
203 new Diagnostic(DiagID, &printer, /*ShouldOwnClient=*/false));
204 Diags->setSourceManager(&PP.getSourceManager());
205
206 printer.BeginSourceFile(PP.getLangOptions(), &PP);
207 arcDiags.reportDiagnostics(*Diags);
208 printer.EndSourceFile();
209}
210
John McCalld70fb982011-06-15 23:25:17 +0000211//===----------------------------------------------------------------------===//
212// checkForManualIssues.
213//===----------------------------------------------------------------------===//
214
215bool arcmt::checkForManualIssues(CompilerInvocation &origCI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000216 StringRef Filename, InputKind Kind,
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000217 DiagnosticClient *DiagClient,
218 bool emitPremigrationARCErrors,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000219 StringRef plistOut) {
John McCalld70fb982011-06-15 23:25:17 +0000220 if (!origCI.getLangOpts().ObjC1)
221 return false;
222
223 std::vector<TransformFn> transforms = arcmt::getAllTransformations();
224 assert(!transforms.empty());
225
226 llvm::OwningPtr<CompilerInvocation> CInvok;
227 CInvok.reset(createInvocationForMigration(origCI));
228 CInvok->getFrontendOpts().Inputs.clear();
229 CInvok->getFrontendOpts().Inputs.push_back(std::make_pair(Kind, Filename));
230
231 CapturedDiagList capturedDiags;
232
233 assert(DiagClient);
234 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
235 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(
236 new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false));
237
238 // Filter of all diagnostics.
239 CaptureDiagnosticClient errRec(*Diags, capturedDiags);
240 Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
241
242 llvm::OwningPtr<ASTUnit> Unit(
243 ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags));
244 if (!Unit)
245 return true;
246
247 // Don't filter diagnostics anymore.
248 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
249
250 ASTContext &Ctx = Unit->getASTContext();
251
252 if (Diags->hasFatalErrorOccurred()) {
253 Diags->Reset();
254 DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor());
255 capturedDiags.reportDiagnostics(*Diags);
256 DiagClient->EndSourceFile();
257 return true;
258 }
259
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000260 if (emitPremigrationARCErrors)
261 emitPremigrationErrors(capturedDiags, origCI.getDiagnosticOpts(),
262 Unit->getPreprocessor());
263 if (!plistOut.empty()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000264 SmallVector<StoredDiagnostic, 8> arcDiags;
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000265 for (CapturedDiagList::iterator
266 I = capturedDiags.begin(), E = capturedDiags.end(); I != E; ++I)
267 arcDiags.push_back(*I);
268 writeARCDiagsToPlist(plistOut, arcDiags,
269 Ctx.getSourceManager(), Ctx.getLangOptions());
270 }
271
John McCalld70fb982011-06-15 23:25:17 +0000272 // After parsing of source files ended, we want to reuse the
273 // diagnostics objects to emit further diagnostics.
274 // We call BeginSourceFile because DiagnosticClient requires that
275 // diagnostics with source range information are emitted only in between
276 // BeginSourceFile() and EndSourceFile().
277 DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor());
278
279 // No macros will be added since we are just checking and we won't modify
280 // source code.
281 std::vector<SourceLocation> ARCMTMacroLocs;
282
283 TransformActions testAct(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
284 MigrationPass pass(Ctx, Unit->getSema(), testAct, ARCMTMacroLocs);
285
286 for (unsigned i=0, e = transforms.size(); i != e; ++i)
287 transforms[i](pass);
288
289 capturedDiags.reportDiagnostics(*Diags);
290
291 DiagClient->EndSourceFile();
292
Argyrios Kyrtzidise2e40b42011-07-14 00:17:54 +0000293 // If we are migrating code that gets the '-fobjc-arc' flag, make sure
294 // to remove it so that we don't get errors from normal compilation.
295 origCI.getLangOpts().ObjCAutoRefCount = false;
296
Argyrios Kyrtzidis73a0d322011-07-18 07:44:45 +0000297 return capturedDiags.hasErrors() || testAct.hasReportedErrors();
John McCalld70fb982011-06-15 23:25:17 +0000298}
299
300//===----------------------------------------------------------------------===//
301// applyTransformations.
302//===----------------------------------------------------------------------===//
303
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000304static bool applyTransforms(CompilerInvocation &origCI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000305 StringRef Filename, InputKind Kind,
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000306 DiagnosticClient *DiagClient,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000307 StringRef outputDir,
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000308 bool emitPremigrationARCErrors,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000309 StringRef plistOut) {
John McCalld70fb982011-06-15 23:25:17 +0000310 if (!origCI.getLangOpts().ObjC1)
311 return false;
312
313 // Make sure checking is successful first.
314 CompilerInvocation CInvokForCheck(origCI);
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000315 if (arcmt::checkForManualIssues(CInvokForCheck, Filename, Kind, DiagClient,
316 emitPremigrationARCErrors, plistOut))
John McCalld70fb982011-06-15 23:25:17 +0000317 return true;
318
319 CompilerInvocation CInvok(origCI);
320 CInvok.getFrontendOpts().Inputs.clear();
321 CInvok.getFrontendOpts().Inputs.push_back(std::make_pair(Kind, Filename));
322
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000323 MigrationProcess migration(CInvok, DiagClient, outputDir);
John McCalld70fb982011-06-15 23:25:17 +0000324
325 std::vector<TransformFn> transforms = arcmt::getAllTransformations();
326 assert(!transforms.empty());
327
328 for (unsigned i=0, e = transforms.size(); i != e; ++i) {
329 bool err = migration.applyTransform(transforms[i]);
330 if (err) return true;
331 }
332
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000333 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
334 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(
335 new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false));
336
337 if (outputDir.empty()) {
338 origCI.getLangOpts().ObjCAutoRefCount = true;
339 return migration.getRemapper().overwriteOriginal(*Diags);
Argyrios Kyrtzidise2e40b42011-07-14 00:17:54 +0000340 } else {
341 // If we are migrating code that gets the '-fobjc-arc' flag, make sure
342 // to remove it so that we don't get errors from normal compilation.
343 origCI.getLangOpts().ObjCAutoRefCount = false;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000344 return migration.getRemapper().flushToDisk(outputDir, *Diags);
Argyrios Kyrtzidise2e40b42011-07-14 00:17:54 +0000345 }
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000346}
347
348bool arcmt::applyTransformations(CompilerInvocation &origCI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000349 StringRef Filename, InputKind Kind,
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000350 DiagnosticClient *DiagClient) {
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000351 return applyTransforms(origCI, Filename, Kind, DiagClient,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000352 StringRef(), false, StringRef());
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000353}
354
355bool arcmt::migrateWithTemporaryFiles(CompilerInvocation &origCI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000356 StringRef Filename, InputKind Kind,
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000357 DiagnosticClient *DiagClient,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000358 StringRef outputDir,
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000359 bool emitPremigrationARCErrors,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000360 StringRef plistOut) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000361 assert(!outputDir.empty() && "Expected output directory path");
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000362 return applyTransforms(origCI, Filename, Kind, DiagClient,
363 outputDir, emitPremigrationARCErrors, plistOut);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000364}
365
366bool arcmt::getFileRemappings(std::vector<std::pair<std::string,std::string> > &
367 remap,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000368 StringRef outputDir,
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000369 DiagnosticClient *DiagClient) {
370 assert(!outputDir.empty());
John McCalld70fb982011-06-15 23:25:17 +0000371
372 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
373 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(
374 new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000375
376 FileRemapper remapper;
377 bool err = remapper.initFromDisk(outputDir, *Diags,
378 /*ignoreIfFilesChanged=*/true);
379 if (err)
380 return true;
381
382 CompilerInvocation CI;
383 remapper.applyMappings(CI);
384 remap = CI.getPreprocessorOpts().RemappedFiles;
385
386 return false;
John McCalld70fb982011-06-15 23:25:17 +0000387}
388
389//===----------------------------------------------------------------------===//
John McCalld70fb982011-06-15 23:25:17 +0000390// CollectTransformActions.
391//===----------------------------------------------------------------------===//
392
393namespace {
394
395class ARCMTMacroTrackerPPCallbacks : public PPCallbacks {
396 std::vector<SourceLocation> &ARCMTMacroLocs;
397
398public:
399 ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs)
400 : ARCMTMacroLocs(ARCMTMacroLocs) { }
401
Argyrios Kyrtzidis85a14bb2011-08-18 01:05:45 +0000402 virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo *MI,
403 SourceRange Range) {
John McCalld70fb982011-06-15 23:25:17 +0000404 if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName())
405 ARCMTMacroLocs.push_back(MacroNameTok.getLocation());
406 }
407};
408
409class ARCMTMacroTrackerAction : public ASTFrontendAction {
410 std::vector<SourceLocation> &ARCMTMacroLocs;
411
412public:
413 ARCMTMacroTrackerAction(std::vector<SourceLocation> &ARCMTMacroLocs)
414 : ARCMTMacroLocs(ARCMTMacroLocs) { }
415
416 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000417 StringRef InFile) {
John McCalld70fb982011-06-15 23:25:17 +0000418 CI.getPreprocessor().addPPCallbacks(
419 new ARCMTMacroTrackerPPCallbacks(ARCMTMacroLocs));
420 return new ASTConsumer();
421 }
422};
423
424class RewritesApplicator : public TransformActions::RewriteReceiver {
425 Rewriter &rewriter;
426 ASTContext &Ctx;
427 MigrationProcess::RewriteListener *Listener;
428
429public:
430 RewritesApplicator(Rewriter &rewriter, ASTContext &ctx,
431 MigrationProcess::RewriteListener *listener)
432 : rewriter(rewriter), Ctx(ctx), Listener(listener) {
433 if (Listener)
434 Listener->start(ctx);
435 }
436 ~RewritesApplicator() {
437 if (Listener)
438 Listener->finish();
439 }
440
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000441 virtual void insert(SourceLocation loc, StringRef text) {
John McCalld70fb982011-06-15 23:25:17 +0000442 bool err = rewriter.InsertText(loc, text, /*InsertAfter=*/true,
443 /*indentNewLines=*/true);
444 if (!err && Listener)
445 Listener->insert(loc, text);
446 }
447
448 virtual void remove(CharSourceRange range) {
449 Rewriter::RewriteOptions removeOpts;
450 removeOpts.IncludeInsertsAtBeginOfRange = false;
451 removeOpts.IncludeInsertsAtEndOfRange = false;
452 removeOpts.RemoveLineIfEmpty = true;
453
454 bool err = rewriter.RemoveText(range, removeOpts);
455 if (!err && Listener)
456 Listener->remove(range);
457 }
458
459 virtual void increaseIndentation(CharSourceRange range,
460 SourceLocation parentIndent) {
461 rewriter.IncreaseIndentation(range, parentIndent);
462 }
463};
464
465} // end anonymous namespace.
466
467/// \brief Anchor for VTable.
468MigrationProcess::RewriteListener::~RewriteListener() { }
469
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000470MigrationProcess::MigrationProcess(const CompilerInvocation &CI,
471 DiagnosticClient *diagClient,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000472 StringRef outputDir)
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000473 : OrigCI(CI), DiagClient(diagClient) {
474 if (!outputDir.empty()) {
475 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
476 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(
477 new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false));
478 Remapper.initFromDisk(outputDir, *Diags, /*ignoreIfFilesChanges=*/true);
479 }
480}
481
John McCalld70fb982011-06-15 23:25:17 +0000482bool MigrationProcess::applyTransform(TransformFn trans,
483 RewriteListener *listener) {
484 llvm::OwningPtr<CompilerInvocation> CInvok;
485 CInvok.reset(createInvocationForMigration(OrigCI));
486 CInvok->getDiagnosticOpts().IgnoreWarnings = true;
487
488 Remapper.applyMappings(*CInvok);
489
490 CapturedDiagList capturedDiags;
491 std::vector<SourceLocation> ARCMTMacroLocs;
492
493 assert(DiagClient);
494 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
495 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(
496 new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false));
497
498 // Filter of all diagnostics.
499 CaptureDiagnosticClient errRec(*Diags, capturedDiags);
500 Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
501
502 llvm::OwningPtr<ARCMTMacroTrackerAction> ASTAction;
503 ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs));
504
505 llvm::OwningPtr<ASTUnit> Unit(
506 ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags,
507 ASTAction.get()));
508 if (!Unit)
509 return true;
510 Unit->setOwnsRemappedFileBuffers(false); // FileRemapper manages that.
511
512 // Don't filter diagnostics anymore.
513 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
514
515 ASTContext &Ctx = Unit->getASTContext();
516
517 if (Diags->hasFatalErrorOccurred()) {
518 Diags->Reset();
519 DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor());
520 capturedDiags.reportDiagnostics(*Diags);
521 DiagClient->EndSourceFile();
522 return true;
523 }
524
525 // After parsing of source files ended, we want to reuse the
526 // diagnostics objects to emit further diagnostics.
527 // We call BeginSourceFile because DiagnosticClient requires that
528 // diagnostics with source range information are emitted only in between
529 // BeginSourceFile() and EndSourceFile().
530 DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor());
531
532 Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOptions());
533 TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
534 MigrationPass pass(Ctx, Unit->getSema(), TA, ARCMTMacroLocs);
535
536 trans(pass);
537
538 {
539 RewritesApplicator applicator(rewriter, Ctx, listener);
540 TA.applyRewrites(applicator);
541 }
542
543 DiagClient->EndSourceFile();
544
545 if (DiagClient->getNumErrors())
546 return true;
547
548 for (Rewriter::buffer_iterator
549 I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) {
550 FileID FID = I->first;
551 RewriteBuffer &buf = I->second;
552 const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID);
553 assert(file);
554 std::string newFname = file->getName();
555 newFname += "-trans";
556 llvm::SmallString<512> newText;
557 llvm::raw_svector_ostream vecOS(newText);
558 buf.write(vecOS);
559 vecOS.flush();
560 llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000561 StringRef(newText.data(), newText.size()), newFname);
John McCalld70fb982011-06-15 23:25:17 +0000562 llvm::SmallString<64> filePath(file->getName());
563 Unit->getFileManager().FixupRelativePath(filePath);
564 Remapper.remap(filePath.str(), memBuf);
565 }
566
567 return false;
568}
569
570//===----------------------------------------------------------------------===//
571// isARCDiagnostic.
572//===----------------------------------------------------------------------===//
573
574bool arcmt::isARCDiagnostic(unsigned diagID, Diagnostic &Diag) {
575 return Diag.getDiagnosticIDs()->getCategoryNumberForDiag(diagID) ==
576 diag::DiagCat_Automatic_Reference_Counting_Issue;
577}