blob: 3b44702fa1d7c0fd798412ac66f55a25927c8b56 [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;
156 unsigned Major, Minor, Micro;
157 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
John McCalld70fb982011-06-15 23:25:17 +0000179CompilerInvocation *createInvocationForMigration(CompilerInvocation &origCI) {
180 llvm::OwningPtr<CompilerInvocation> CInvok;
181 CInvok.reset(new CompilerInvocation(origCI));
182 CInvok->getPreprocessorOpts().ImplicitPCHInclude = std::string();
183 CInvok->getPreprocessorOpts().ImplicitPTHInclude = std::string();
184 std::string define = getARCMTMacroName();
185 define += '=';
186 CInvok->getPreprocessorOpts().addMacroDef(define);
187 CInvok->getLangOpts().ObjCAutoRefCount = true;
188 CInvok->getDiagnosticOpts().ErrorLimit = 0;
Argyrios Kyrtzidis59b5f352011-06-22 18:03:59 +0000189 CInvok->getDiagnosticOpts().Warnings.push_back(
190 "error=arc-unsafe-retained-assign");
John McCall24fc0de2011-07-06 00:26:06 +0000191 CInvok->getLangOpts().ObjCRuntimeHasWeak = HasARCRuntime(origCI);
John McCalld70fb982011-06-15 23:25:17 +0000192
193 return CInvok.take();
194}
195
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000196void emitPremigrationErrors(const CapturedDiagList &arcDiags,
197 const DiagnosticOptions &diagOpts,
198 Preprocessor &PP) {
199 TextDiagnosticPrinter printer(llvm::errs(), diagOpts);
200 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
201 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(
202 new Diagnostic(DiagID, &printer, /*ShouldOwnClient=*/false));
203 Diags->setSourceManager(&PP.getSourceManager());
204
205 printer.BeginSourceFile(PP.getLangOptions(), &PP);
206 arcDiags.reportDiagnostics(*Diags);
207 printer.EndSourceFile();
208}
209
John McCalld70fb982011-06-15 23:25:17 +0000210//===----------------------------------------------------------------------===//
211// checkForManualIssues.
212//===----------------------------------------------------------------------===//
213
214bool arcmt::checkForManualIssues(CompilerInvocation &origCI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000215 StringRef Filename, InputKind Kind,
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000216 DiagnosticClient *DiagClient,
217 bool emitPremigrationARCErrors,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000218 StringRef plistOut) {
John McCalld70fb982011-06-15 23:25:17 +0000219 if (!origCI.getLangOpts().ObjC1)
220 return false;
221
222 std::vector<TransformFn> transforms = arcmt::getAllTransformations();
223 assert(!transforms.empty());
224
225 llvm::OwningPtr<CompilerInvocation> CInvok;
226 CInvok.reset(createInvocationForMigration(origCI));
227 CInvok->getFrontendOpts().Inputs.clear();
228 CInvok->getFrontendOpts().Inputs.push_back(std::make_pair(Kind, Filename));
229
230 CapturedDiagList capturedDiags;
231
232 assert(DiagClient);
233 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
234 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(
235 new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false));
236
237 // Filter of all diagnostics.
238 CaptureDiagnosticClient errRec(*Diags, capturedDiags);
239 Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
240
241 llvm::OwningPtr<ASTUnit> Unit(
242 ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags));
243 if (!Unit)
244 return true;
245
246 // Don't filter diagnostics anymore.
247 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
248
249 ASTContext &Ctx = Unit->getASTContext();
250
251 if (Diags->hasFatalErrorOccurred()) {
252 Diags->Reset();
253 DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor());
254 capturedDiags.reportDiagnostics(*Diags);
255 DiagClient->EndSourceFile();
256 return true;
257 }
258
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000259 if (emitPremigrationARCErrors)
260 emitPremigrationErrors(capturedDiags, origCI.getDiagnosticOpts(),
261 Unit->getPreprocessor());
262 if (!plistOut.empty()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000263 SmallVector<StoredDiagnostic, 8> arcDiags;
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000264 for (CapturedDiagList::iterator
265 I = capturedDiags.begin(), E = capturedDiags.end(); I != E; ++I)
266 arcDiags.push_back(*I);
267 writeARCDiagsToPlist(plistOut, arcDiags,
268 Ctx.getSourceManager(), Ctx.getLangOptions());
269 }
270
John McCalld70fb982011-06-15 23:25:17 +0000271 // After parsing of source files ended, we want to reuse the
272 // diagnostics objects to emit further diagnostics.
273 // We call BeginSourceFile because DiagnosticClient requires that
274 // diagnostics with source range information are emitted only in between
275 // BeginSourceFile() and EndSourceFile().
276 DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor());
277
278 // No macros will be added since we are just checking and we won't modify
279 // source code.
280 std::vector<SourceLocation> ARCMTMacroLocs;
281
282 TransformActions testAct(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
283 MigrationPass pass(Ctx, Unit->getSema(), testAct, ARCMTMacroLocs);
284
285 for (unsigned i=0, e = transforms.size(); i != e; ++i)
286 transforms[i](pass);
287
288 capturedDiags.reportDiagnostics(*Diags);
289
290 DiagClient->EndSourceFile();
291
Argyrios Kyrtzidise2e40b42011-07-14 00:17:54 +0000292 // If we are migrating code that gets the '-fobjc-arc' flag, make sure
293 // to remove it so that we don't get errors from normal compilation.
294 origCI.getLangOpts().ObjCAutoRefCount = false;
295
Argyrios Kyrtzidis73a0d322011-07-18 07:44:45 +0000296 return capturedDiags.hasErrors() || testAct.hasReportedErrors();
John McCalld70fb982011-06-15 23:25:17 +0000297}
298
299//===----------------------------------------------------------------------===//
300// applyTransformations.
301//===----------------------------------------------------------------------===//
302
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000303static bool applyTransforms(CompilerInvocation &origCI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000304 StringRef Filename, InputKind Kind,
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000305 DiagnosticClient *DiagClient,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000306 StringRef outputDir,
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000307 bool emitPremigrationARCErrors,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000308 StringRef plistOut) {
John McCalld70fb982011-06-15 23:25:17 +0000309 if (!origCI.getLangOpts().ObjC1)
310 return false;
311
312 // Make sure checking is successful first.
313 CompilerInvocation CInvokForCheck(origCI);
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000314 if (arcmt::checkForManualIssues(CInvokForCheck, Filename, Kind, DiagClient,
315 emitPremigrationARCErrors, plistOut))
John McCalld70fb982011-06-15 23:25:17 +0000316 return true;
317
318 CompilerInvocation CInvok(origCI);
319 CInvok.getFrontendOpts().Inputs.clear();
320 CInvok.getFrontendOpts().Inputs.push_back(std::make_pair(Kind, Filename));
321
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000322 MigrationProcess migration(CInvok, DiagClient, outputDir);
John McCalld70fb982011-06-15 23:25:17 +0000323
324 std::vector<TransformFn> transforms = arcmt::getAllTransformations();
325 assert(!transforms.empty());
326
327 for (unsigned i=0, e = transforms.size(); i != e; ++i) {
328 bool err = migration.applyTransform(transforms[i]);
329 if (err) return true;
330 }
331
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000332 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
333 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(
334 new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false));
335
336 if (outputDir.empty()) {
337 origCI.getLangOpts().ObjCAutoRefCount = true;
338 return migration.getRemapper().overwriteOriginal(*Diags);
Argyrios Kyrtzidise2e40b42011-07-14 00:17:54 +0000339 } else {
340 // If we are migrating code that gets the '-fobjc-arc' flag, make sure
341 // to remove it so that we don't get errors from normal compilation.
342 origCI.getLangOpts().ObjCAutoRefCount = false;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000343 return migration.getRemapper().flushToDisk(outputDir, *Diags);
Argyrios Kyrtzidise2e40b42011-07-14 00:17:54 +0000344 }
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000345}
346
347bool arcmt::applyTransformations(CompilerInvocation &origCI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000348 StringRef Filename, InputKind Kind,
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000349 DiagnosticClient *DiagClient) {
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000350 return applyTransforms(origCI, Filename, Kind, DiagClient,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000351 StringRef(), false, StringRef());
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000352}
353
354bool arcmt::migrateWithTemporaryFiles(CompilerInvocation &origCI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000355 StringRef Filename, InputKind Kind,
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000356 DiagnosticClient *DiagClient,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000357 StringRef outputDir,
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000358 bool emitPremigrationARCErrors,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000359 StringRef plistOut) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000360 assert(!outputDir.empty() && "Expected output directory path");
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000361 return applyTransforms(origCI, Filename, Kind, DiagClient,
362 outputDir, emitPremigrationARCErrors, plistOut);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000363}
364
365bool arcmt::getFileRemappings(std::vector<std::pair<std::string,std::string> > &
366 remap,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000367 StringRef outputDir,
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000368 DiagnosticClient *DiagClient) {
369 assert(!outputDir.empty());
John McCalld70fb982011-06-15 23:25:17 +0000370
371 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
372 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(
373 new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000374
375 FileRemapper remapper;
376 bool err = remapper.initFromDisk(outputDir, *Diags,
377 /*ignoreIfFilesChanged=*/true);
378 if (err)
379 return true;
380
381 CompilerInvocation CI;
382 remapper.applyMappings(CI);
383 remap = CI.getPreprocessorOpts().RemappedFiles;
384
385 return false;
John McCalld70fb982011-06-15 23:25:17 +0000386}
387
388//===----------------------------------------------------------------------===//
John McCalld70fb982011-06-15 23:25:17 +0000389// CollectTransformActions.
390//===----------------------------------------------------------------------===//
391
392namespace {
393
394class ARCMTMacroTrackerPPCallbacks : public PPCallbacks {
395 std::vector<SourceLocation> &ARCMTMacroLocs;
396
397public:
398 ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs)
399 : ARCMTMacroLocs(ARCMTMacroLocs) { }
400
401 virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo *MI) {
402 if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName())
403 ARCMTMacroLocs.push_back(MacroNameTok.getLocation());
404 }
405};
406
407class ARCMTMacroTrackerAction : public ASTFrontendAction {
408 std::vector<SourceLocation> &ARCMTMacroLocs;
409
410public:
411 ARCMTMacroTrackerAction(std::vector<SourceLocation> &ARCMTMacroLocs)
412 : ARCMTMacroLocs(ARCMTMacroLocs) { }
413
414 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000415 StringRef InFile) {
John McCalld70fb982011-06-15 23:25:17 +0000416 CI.getPreprocessor().addPPCallbacks(
417 new ARCMTMacroTrackerPPCallbacks(ARCMTMacroLocs));
418 return new ASTConsumer();
419 }
420};
421
422class RewritesApplicator : public TransformActions::RewriteReceiver {
423 Rewriter &rewriter;
424 ASTContext &Ctx;
425 MigrationProcess::RewriteListener *Listener;
426
427public:
428 RewritesApplicator(Rewriter &rewriter, ASTContext &ctx,
429 MigrationProcess::RewriteListener *listener)
430 : rewriter(rewriter), Ctx(ctx), Listener(listener) {
431 if (Listener)
432 Listener->start(ctx);
433 }
434 ~RewritesApplicator() {
435 if (Listener)
436 Listener->finish();
437 }
438
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000439 virtual void insert(SourceLocation loc, StringRef text) {
John McCalld70fb982011-06-15 23:25:17 +0000440 bool err = rewriter.InsertText(loc, text, /*InsertAfter=*/true,
441 /*indentNewLines=*/true);
442 if (!err && Listener)
443 Listener->insert(loc, text);
444 }
445
446 virtual void remove(CharSourceRange range) {
447 Rewriter::RewriteOptions removeOpts;
448 removeOpts.IncludeInsertsAtBeginOfRange = false;
449 removeOpts.IncludeInsertsAtEndOfRange = false;
450 removeOpts.RemoveLineIfEmpty = true;
451
452 bool err = rewriter.RemoveText(range, removeOpts);
453 if (!err && Listener)
454 Listener->remove(range);
455 }
456
457 virtual void increaseIndentation(CharSourceRange range,
458 SourceLocation parentIndent) {
459 rewriter.IncreaseIndentation(range, parentIndent);
460 }
461};
462
463} // end anonymous namespace.
464
465/// \brief Anchor for VTable.
466MigrationProcess::RewriteListener::~RewriteListener() { }
467
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000468MigrationProcess::MigrationProcess(const CompilerInvocation &CI,
469 DiagnosticClient *diagClient,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000470 StringRef outputDir)
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000471 : OrigCI(CI), DiagClient(diagClient) {
472 if (!outputDir.empty()) {
473 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
474 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(
475 new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false));
476 Remapper.initFromDisk(outputDir, *Diags, /*ignoreIfFilesChanges=*/true);
477 }
478}
479
John McCalld70fb982011-06-15 23:25:17 +0000480bool MigrationProcess::applyTransform(TransformFn trans,
481 RewriteListener *listener) {
482 llvm::OwningPtr<CompilerInvocation> CInvok;
483 CInvok.reset(createInvocationForMigration(OrigCI));
484 CInvok->getDiagnosticOpts().IgnoreWarnings = true;
485
486 Remapper.applyMappings(*CInvok);
487
488 CapturedDiagList capturedDiags;
489 std::vector<SourceLocation> ARCMTMacroLocs;
490
491 assert(DiagClient);
492 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
493 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(
494 new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false));
495
496 // Filter of all diagnostics.
497 CaptureDiagnosticClient errRec(*Diags, capturedDiags);
498 Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
499
500 llvm::OwningPtr<ARCMTMacroTrackerAction> ASTAction;
501 ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs));
502
503 llvm::OwningPtr<ASTUnit> Unit(
504 ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags,
505 ASTAction.get()));
506 if (!Unit)
507 return true;
508 Unit->setOwnsRemappedFileBuffers(false); // FileRemapper manages that.
509
510 // Don't filter diagnostics anymore.
511 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
512
513 ASTContext &Ctx = Unit->getASTContext();
514
515 if (Diags->hasFatalErrorOccurred()) {
516 Diags->Reset();
517 DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor());
518 capturedDiags.reportDiagnostics(*Diags);
519 DiagClient->EndSourceFile();
520 return true;
521 }
522
523 // After parsing of source files ended, we want to reuse the
524 // diagnostics objects to emit further diagnostics.
525 // We call BeginSourceFile because DiagnosticClient requires that
526 // diagnostics with source range information are emitted only in between
527 // BeginSourceFile() and EndSourceFile().
528 DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor());
529
530 Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOptions());
531 TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
532 MigrationPass pass(Ctx, Unit->getSema(), TA, ARCMTMacroLocs);
533
534 trans(pass);
535
536 {
537 RewritesApplicator applicator(rewriter, Ctx, listener);
538 TA.applyRewrites(applicator);
539 }
540
541 DiagClient->EndSourceFile();
542
543 if (DiagClient->getNumErrors())
544 return true;
545
546 for (Rewriter::buffer_iterator
547 I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) {
548 FileID FID = I->first;
549 RewriteBuffer &buf = I->second;
550 const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID);
551 assert(file);
552 std::string newFname = file->getName();
553 newFname += "-trans";
554 llvm::SmallString<512> newText;
555 llvm::raw_svector_ostream vecOS(newText);
556 buf.write(vecOS);
557 vecOS.flush();
558 llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000559 StringRef(newText.data(), newText.size()), newFname);
John McCalld70fb982011-06-15 23:25:17 +0000560 llvm::SmallString<64> filePath(file->getName());
561 Unit->getFileManager().FixupRelativePath(filePath);
562 Remapper.remap(filePath.str(), memBuf);
563 }
564
565 return false;
566}
567
568//===----------------------------------------------------------------------===//
569// isARCDiagnostic.
570//===----------------------------------------------------------------------===//
571
572bool arcmt::isARCDiagnostic(unsigned diagID, Diagnostic &Diag) {
573 return Diag.getDiagnosticIDs()->getCategoryNumberForDiag(diagID) ==
574 diag::DiagCat_Automatic_Reference_Counting_Issue;
575}