blob: 6e024907ad63a9f5ee955339d6d4bc52169dfc86 [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"
Chandler Carruth3a022472012-12-04 09:13:33 +000011#include "clang/AST/ASTConsumer.h"
12#include "clang/Basic/DiagnosticCategories.h"
John McCalld70fb982011-06-15 23:25:17 +000013#include "clang/Frontend/ASTUnit.h"
14#include "clang/Frontend/CompilerInstance.h"
Douglas Gregor32fbe312012-01-20 16:28:04 +000015#include "clang/Frontend/FrontendAction.h"
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +000016#include "clang/Frontend/TextDiagnosticPrinter.h"
John McCalld70fb982011-06-15 23:25:17 +000017#include "clang/Frontend/Utils.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Lex/Preprocessor.h"
Ted Kremenekcdf81492012-09-01 05:09:24 +000019#include "clang/Rewrite/Core/Rewriter.h"
John McCalld70fb982011-06-15 23:25:17 +000020#include "clang/Sema/SemaDiagnostic.h"
John McCalld70fb982011-06-15 23:25:17 +000021#include "llvm/ADT/Triple.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "llvm/Support/MemoryBuffer.h"
John McCalld70fb982011-06-15 23:25:17 +000023using namespace clang;
24using namespace arcmt;
John McCalld70fb982011-06-15 23:25:17 +000025
Chris Lattner54b16772011-07-23 17:14:25 +000026bool CapturedDiagList::clearDiagnostic(ArrayRef<unsigned> IDs,
John McCalld70fb982011-06-15 23:25:17 +000027 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++;
Argyrios Kyrtzidis03fbe3e2013-01-04 18:30:08 +000042 if (eraseS->getLevel() != DiagnosticsEngine::Note)
43 while (I != List.end() && I->getLevel() == DiagnosticsEngine::Note)
44 ++I;
John McCalld70fb982011-06-15 23:25:17 +000045 // Clear the diagnostic and any notes following it.
Joao Matos0e167f72012-08-31 17:28:09 +000046 I = List.erase(eraseS, I);
John McCalld70fb982011-06-15 23:25:17 +000047 continue;
48 }
49
50 ++I;
51 }
52
53 return cleared;
54}
55
Chris Lattner54b16772011-07-23 17:14:25 +000056bool CapturedDiagList::hasDiagnostic(ArrayRef<unsigned> IDs,
Argyrios Kyrtzidis0f3f9f72011-06-18 00:53:34 +000057 SourceRange range) const {
John McCalld70fb982011-06-15 23:25:17 +000058 if (range.isInvalid())
59 return false;
60
Argyrios Kyrtzidis0f3f9f72011-06-18 00:53:34 +000061 ListTy::const_iterator I = List.begin();
John McCalld70fb982011-06-15 23:25:17 +000062 while (I != List.end()) {
63 FullSourceLoc diagLoc = I->getLocation();
64 if ((IDs.empty() || // empty means any diagnostic in the range.
65 std::find(IDs.begin(), IDs.end(), I->getID()) != IDs.end()) &&
66 !diagLoc.isBeforeInTranslationUnitThan(range.getBegin()) &&
67 (diagLoc == range.getEnd() ||
68 diagLoc.isBeforeInTranslationUnitThan(range.getEnd()))) {
69 return true;
70 }
71
72 ++I;
73 }
74
75 return false;
76}
77
David Blaikie9c902b52011-09-25 23:23:43 +000078void CapturedDiagList::reportDiagnostics(DiagnosticsEngine &Diags) const {
Argyrios Kyrtzidis0f3f9f72011-06-18 00:53:34 +000079 for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I)
John McCalld70fb982011-06-15 23:25:17 +000080 Diags.Report(*I);
81}
82
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +000083bool CapturedDiagList::hasErrors() const {
84 for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I)
David Blaikie9c902b52011-09-25 23:23:43 +000085 if (I->getLevel() >= DiagnosticsEngine::Error)
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +000086 return true;
87
88 return false;
89}
90
John McCalld70fb982011-06-15 23:25:17 +000091namespace {
92
David Blaikiea24a0bc2011-09-25 23:54:33 +000093class CaptureDiagnosticConsumer : public DiagnosticConsumer {
David Blaikie9c902b52011-09-25 23:23:43 +000094 DiagnosticsEngine &Diags;
Jordan Roseb00073d2012-08-10 01:06:16 +000095 DiagnosticConsumer &DiagClient;
John McCalld70fb982011-06-15 23:25:17 +000096 CapturedDiagList &CapturedDiags;
Jordan Roseb00073d2012-08-10 01:06:16 +000097 bool HasBegunSourceFile;
John McCalld70fb982011-06-15 23:25:17 +000098public:
David Blaikiea24a0bc2011-09-25 23:54:33 +000099 CaptureDiagnosticConsumer(DiagnosticsEngine &diags,
Jordan Roseb00073d2012-08-10 01:06:16 +0000100 DiagnosticConsumer &client,
101 CapturedDiagList &capturedDiags)
102 : Diags(diags), DiagClient(client), CapturedDiags(capturedDiags),
103 HasBegunSourceFile(false) { }
104
105 virtual void BeginSourceFile(const LangOptions &Opts,
106 const Preprocessor *PP) {
107 // Pass BeginSourceFile message onto DiagClient on first call.
108 // The corresponding EndSourceFile call will be made from an
109 // explicit call to FinishCapture.
110 if (!HasBegunSourceFile) {
111 DiagClient.BeginSourceFile(Opts, PP);
112 HasBegunSourceFile = true;
113 }
114 }
115
116 void FinishCapture() {
117 // Call EndSourceFile on DiagClient on completion of capture to
118 // enable VerifyDiagnosticConsumer to check diagnostics *after*
119 // it has received the diagnostic list.
120 if (HasBegunSourceFile) {
121 DiagClient.EndSourceFile();
122 HasBegunSourceFile = false;
123 }
124 }
125
126 virtual ~CaptureDiagnosticConsumer() {
127 assert(!HasBegunSourceFile && "FinishCapture not called!");
128 }
John McCalld70fb982011-06-15 23:25:17 +0000129
David Blaikie9c902b52011-09-25 23:23:43 +0000130 virtual void HandleDiagnostic(DiagnosticsEngine::Level level,
David Blaikieb5784322011-09-26 01:18:08 +0000131 const Diagnostic &Info) {
Ted Kremenek337c5b82011-10-20 05:07:47 +0000132 if (DiagnosticIDs::isARCDiagnostic(Info.getID()) ||
David Blaikie9c902b52011-09-25 23:23:43 +0000133 level >= DiagnosticsEngine::Error || level == DiagnosticsEngine::Note) {
Argyrios Kyrtzidis1f732162012-12-12 22:48:28 +0000134 if (Info.getLocation().isValid())
135 CapturedDiags.push_back(StoredDiagnostic(level, Info));
John McCalld70fb982011-06-15 23:25:17 +0000136 return;
137 }
138
139 // Non-ARC warnings are ignored.
140 Diags.setLastDiagnosticIgnored();
141 }
Douglas Gregord0e9e3a2011-09-29 00:38:00 +0000142
143 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
144 // Just drop any diagnostics that come from cloned consumers; they'll
145 // have different source managers anyway.
146 return new IgnoringDiagConsumer();
147 }
John McCalld70fb982011-06-15 23:25:17 +0000148};
149
150} // end anonymous namespace
151
Argyrios Kyrtzidis81a35902011-06-20 19:59:52 +0000152static bool HasARCRuntime(CompilerInvocation &origCI) {
153 // This duplicates some functionality from Darwin::AddDeploymentTarget
154 // but this function is well defined, so keep it decoupled from the driver
155 // and avoid unrelated complications.
Argyrios Kyrtzidis81a35902011-06-20 19:59:52 +0000156 llvm::Triple triple(origCI.getTargetOpts().Triple);
157
158 if (triple.getOS() == llvm::Triple::IOS)
159 return triple.getOSMajorVersion() >= 5;
160
161 if (triple.getOS() == llvm::Triple::Darwin)
162 return triple.getOSMajorVersion() >= 11;
163
164 if (triple.getOS() == llvm::Triple::MacOSX) {
165 unsigned Major, Minor, Micro;
166 triple.getOSVersion(Major, Minor, Micro);
167 return Major > 10 || (Major == 10 && Minor >= 7);
168 }
169
170 return false;
171}
172
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000173static CompilerInvocation *
174createInvocationForMigration(CompilerInvocation &origCI) {
Dylan Noblesmithe2778992012-02-05 02:12:40 +0000175 OwningPtr<CompilerInvocation> CInvok;
John McCalld70fb982011-06-15 23:25:17 +0000176 CInvok.reset(new CompilerInvocation(origCI));
177 CInvok->getPreprocessorOpts().ImplicitPCHInclude = std::string();
178 CInvok->getPreprocessorOpts().ImplicitPTHInclude = std::string();
179 std::string define = getARCMTMacroName();
180 define += '=';
181 CInvok->getPreprocessorOpts().addMacroDef(define);
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000182 CInvok->getLangOpts()->ObjCAutoRefCount = true;
183 CInvok->getLangOpts()->setGC(LangOptions::NonGC);
John McCalld70fb982011-06-15 23:25:17 +0000184 CInvok->getDiagnosticOpts().ErrorLimit = 0;
Argyrios Kyrtzidisaa38f692012-06-20 01:46:26 +0000185 CInvok->getDiagnosticOpts().PedanticErrors = 0;
Argyrios Kyrtzidis692bf8c2012-06-20 01:10:40 +0000186
187 // Ignore -Werror flags when migrating.
188 std::vector<std::string> WarnOpts;
189 for (std::vector<std::string>::iterator
190 I = CInvok->getDiagnosticOpts().Warnings.begin(),
191 E = CInvok->getDiagnosticOpts().Warnings.end(); I != E; ++I) {
192 if (!StringRef(*I).startswith("error"))
193 WarnOpts.push_back(*I);
194 }
195 WarnOpts.push_back("error=arc-unsafe-retained-assign");
Argyrios Kyrtzidisaa38f692012-06-20 01:46:26 +0000196 CInvok->getDiagnosticOpts().Warnings = llvm_move(WarnOpts);
Argyrios Kyrtzidis692bf8c2012-06-20 01:10:40 +0000197
John McCall3deb1ad2012-08-21 02:47:43 +0000198 CInvok->getLangOpts()->ObjCARCWeak = HasARCRuntime(origCI);
John McCalld70fb982011-06-15 23:25:17 +0000199
200 return CInvok.take();
201}
202
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000203static void emitPremigrationErrors(const CapturedDiagList &arcDiags,
Douglas Gregor811db4e2012-10-23 22:26:28 +0000204 DiagnosticOptions *diagOpts,
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000205 Preprocessor &PP) {
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000206 TextDiagnosticPrinter printer(llvm::errs(), diagOpts);
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000207 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
208 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000209 new DiagnosticsEngine(DiagID, diagOpts, &printer,
210 /*ShouldOwnClient=*/false));
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000211 Diags->setSourceManager(&PP.getSourceManager());
212
David Blaikiebbafb8a2012-03-11 07:00:24 +0000213 printer.BeginSourceFile(PP.getLangOpts(), &PP);
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000214 arcDiags.reportDiagnostics(*Diags);
215 printer.EndSourceFile();
216}
217
John McCalld70fb982011-06-15 23:25:17 +0000218//===----------------------------------------------------------------------===//
219// checkForManualIssues.
220//===----------------------------------------------------------------------===//
221
222bool arcmt::checkForManualIssues(CompilerInvocation &origCI,
Douglas Gregor32fbe312012-01-20 16:28:04 +0000223 const FrontendInputFile &Input,
David Blaikiee2eefae2011-09-25 23:39:51 +0000224 DiagnosticConsumer *DiagClient,
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000225 bool emitPremigrationARCErrors,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000226 StringRef plistOut) {
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000227 if (!origCI.getLangOpts()->ObjC1)
John McCalld70fb982011-06-15 23:25:17 +0000228 return false;
229
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000230 LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC();
Fariborz Jahanianaa7b9aa2012-01-25 00:20:29 +0000231 bool NoNSAllocReallocError = origCI.getMigratorOpts().NoNSAllocReallocError;
Fariborz Jahanian0c859d62012-01-26 00:08:04 +0000232 bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval;
Argyrios Kyrtzidisd208ef92011-11-04 15:58:08 +0000233
Fariborz Jahanian48fd81b2012-01-26 20:57:58 +0000234 std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode,
235 NoFinalizeRemoval);
John McCalld70fb982011-06-15 23:25:17 +0000236 assert(!transforms.empty());
237
Dylan Noblesmithe2778992012-02-05 02:12:40 +0000238 OwningPtr<CompilerInvocation> CInvok;
John McCalld70fb982011-06-15 23:25:17 +0000239 CInvok.reset(createInvocationForMigration(origCI));
240 CInvok->getFrontendOpts().Inputs.clear();
Douglas Gregor32fbe312012-01-20 16:28:04 +0000241 CInvok->getFrontendOpts().Inputs.push_back(Input);
John McCalld70fb982011-06-15 23:25:17 +0000242
243 CapturedDiagList capturedDiags;
244
245 assert(DiagClient);
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000246 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
247 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000248 new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
249 DiagClient, /*ShouldOwnClient=*/false));
John McCalld70fb982011-06-15 23:25:17 +0000250
251 // Filter of all diagnostics.
Jordan Roseb00073d2012-08-10 01:06:16 +0000252 CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags);
John McCalld70fb982011-06-15 23:25:17 +0000253 Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
254
Dylan Noblesmithe2778992012-02-05 02:12:40 +0000255 OwningPtr<ASTUnit> Unit(
John McCalld70fb982011-06-15 23:25:17 +0000256 ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags));
Jordan Roseb00073d2012-08-10 01:06:16 +0000257 if (!Unit) {
258 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000259 return true;
Jordan Roseb00073d2012-08-10 01:06:16 +0000260 }
John McCalld70fb982011-06-15 23:25:17 +0000261
262 // Don't filter diagnostics anymore.
263 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
264
265 ASTContext &Ctx = Unit->getASTContext();
266
267 if (Diags->hasFatalErrorOccurred()) {
268 Diags->Reset();
David Blaikiebbafb8a2012-03-11 07:00:24 +0000269 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
John McCalld70fb982011-06-15 23:25:17 +0000270 capturedDiags.reportDiagnostics(*Diags);
271 DiagClient->EndSourceFile();
Jordan Roseb00073d2012-08-10 01:06:16 +0000272 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000273 return true;
274 }
275
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000276 if (emitPremigrationARCErrors)
Douglas Gregor811db4e2012-10-23 22:26:28 +0000277 emitPremigrationErrors(capturedDiags, &origCI.getDiagnosticOpts(),
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000278 Unit->getPreprocessor());
279 if (!plistOut.empty()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000280 SmallVector<StoredDiagnostic, 8> arcDiags;
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000281 for (CapturedDiagList::iterator
282 I = capturedDiags.begin(), E = capturedDiags.end(); I != E; ++I)
283 arcDiags.push_back(*I);
284 writeARCDiagsToPlist(plistOut, arcDiags,
David Blaikiebbafb8a2012-03-11 07:00:24 +0000285 Ctx.getSourceManager(), Ctx.getLangOpts());
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000286 }
287
John McCalld70fb982011-06-15 23:25:17 +0000288 // After parsing of source files ended, we want to reuse the
289 // diagnostics objects to emit further diagnostics.
David Blaikiee2eefae2011-09-25 23:39:51 +0000290 // We call BeginSourceFile because DiagnosticConsumer requires that
John McCalld70fb982011-06-15 23:25:17 +0000291 // diagnostics with source range information are emitted only in between
292 // BeginSourceFile() and EndSourceFile().
David Blaikiebbafb8a2012-03-11 07:00:24 +0000293 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
John McCalld70fb982011-06-15 23:25:17 +0000294
295 // No macros will be added since we are just checking and we won't modify
296 // source code.
297 std::vector<SourceLocation> ARCMTMacroLocs;
298
299 TransformActions testAct(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
Argyrios Kyrtzidis03fbe3e2013-01-04 18:30:08 +0000300 MigrationPass pass(Ctx, OrigGCMode, Unit->getSema(), testAct, capturedDiags,
301 ARCMTMacroLocs);
Fariborz Jahanianaa7b9aa2012-01-25 00:20:29 +0000302 pass.setNSAllocReallocError(NoNSAllocReallocError);
Fariborz Jahanian0c859d62012-01-26 00:08:04 +0000303 pass.setNoFinalizeRemoval(NoFinalizeRemoval);
John McCalld70fb982011-06-15 23:25:17 +0000304
305 for (unsigned i=0, e = transforms.size(); i != e; ++i)
306 transforms[i](pass);
307
308 capturedDiags.reportDiagnostics(*Diags);
309
310 DiagClient->EndSourceFile();
Jordan Roseb00073d2012-08-10 01:06:16 +0000311 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000312
Argyrios Kyrtzidise2e40b42011-07-14 00:17:54 +0000313 // If we are migrating code that gets the '-fobjc-arc' flag, make sure
314 // to remove it so that we don't get errors from normal compilation.
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000315 origCI.getLangOpts()->ObjCAutoRefCount = false;
Argyrios Kyrtzidise2e40b42011-07-14 00:17:54 +0000316
Argyrios Kyrtzidis73a0d322011-07-18 07:44:45 +0000317 return capturedDiags.hasErrors() || testAct.hasReportedErrors();
John McCalld70fb982011-06-15 23:25:17 +0000318}
319
320//===----------------------------------------------------------------------===//
321// applyTransformations.
322//===----------------------------------------------------------------------===//
323
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000324static bool applyTransforms(CompilerInvocation &origCI,
Douglas Gregor32fbe312012-01-20 16:28:04 +0000325 const FrontendInputFile &Input,
David Blaikiee2eefae2011-09-25 23:39:51 +0000326 DiagnosticConsumer *DiagClient,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000327 StringRef outputDir,
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000328 bool emitPremigrationARCErrors,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000329 StringRef plistOut) {
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000330 if (!origCI.getLangOpts()->ObjC1)
John McCalld70fb982011-06-15 23:25:17 +0000331 return false;
332
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000333 LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC();
Argyrios Kyrtzidisd208ef92011-11-04 15:58:08 +0000334
John McCalld70fb982011-06-15 23:25:17 +0000335 // Make sure checking is successful first.
336 CompilerInvocation CInvokForCheck(origCI);
Douglas Gregor32fbe312012-01-20 16:28:04 +0000337 if (arcmt::checkForManualIssues(CInvokForCheck, Input, DiagClient,
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000338 emitPremigrationARCErrors, plistOut))
John McCalld70fb982011-06-15 23:25:17 +0000339 return true;
340
341 CompilerInvocation CInvok(origCI);
342 CInvok.getFrontendOpts().Inputs.clear();
Douglas Gregor32fbe312012-01-20 16:28:04 +0000343 CInvok.getFrontendOpts().Inputs.push_back(Input);
John McCalld70fb982011-06-15 23:25:17 +0000344
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000345 MigrationProcess migration(CInvok, DiagClient, outputDir);
Fariborz Jahanian48fd81b2012-01-26 20:57:58 +0000346 bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval;
John McCalld70fb982011-06-15 23:25:17 +0000347
Fariborz Jahanian48fd81b2012-01-26 20:57:58 +0000348 std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode,
349 NoFinalizeRemoval);
John McCalld70fb982011-06-15 23:25:17 +0000350 assert(!transforms.empty());
351
352 for (unsigned i=0, e = transforms.size(); i != e; ++i) {
353 bool err = migration.applyTransform(transforms[i]);
354 if (err) return true;
355 }
356
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000357 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
358 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000359 new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
360 DiagClient, /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000361
362 if (outputDir.empty()) {
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000363 origCI.getLangOpts()->ObjCAutoRefCount = true;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000364 return migration.getRemapper().overwriteOriginal(*Diags);
Argyrios Kyrtzidise2e40b42011-07-14 00:17:54 +0000365 } else {
366 // If we are migrating code that gets the '-fobjc-arc' flag, make sure
367 // to remove it so that we don't get errors from normal compilation.
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000368 origCI.getLangOpts()->ObjCAutoRefCount = false;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000369 return migration.getRemapper().flushToDisk(outputDir, *Diags);
Argyrios Kyrtzidise2e40b42011-07-14 00:17:54 +0000370 }
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000371}
372
373bool arcmt::applyTransformations(CompilerInvocation &origCI,
Douglas Gregor32fbe312012-01-20 16:28:04 +0000374 const FrontendInputFile &Input,
David Blaikiee2eefae2011-09-25 23:39:51 +0000375 DiagnosticConsumer *DiagClient) {
Douglas Gregor32fbe312012-01-20 16:28:04 +0000376 return applyTransforms(origCI, Input, DiagClient,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000377 StringRef(), false, StringRef());
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000378}
379
380bool arcmt::migrateWithTemporaryFiles(CompilerInvocation &origCI,
Douglas Gregor32fbe312012-01-20 16:28:04 +0000381 const FrontendInputFile &Input,
David Blaikiee2eefae2011-09-25 23:39:51 +0000382 DiagnosticConsumer *DiagClient,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000383 StringRef outputDir,
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000384 bool emitPremigrationARCErrors,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000385 StringRef plistOut) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000386 assert(!outputDir.empty() && "Expected output directory path");
Douglas Gregor32fbe312012-01-20 16:28:04 +0000387 return applyTransforms(origCI, Input, DiagClient,
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000388 outputDir, emitPremigrationARCErrors, plistOut);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000389}
390
391bool arcmt::getFileRemappings(std::vector<std::pair<std::string,std::string> > &
392 remap,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000393 StringRef outputDir,
David Blaikiee2eefae2011-09-25 23:39:51 +0000394 DiagnosticConsumer *DiagClient) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000395 assert(!outputDir.empty());
John McCalld70fb982011-06-15 23:25:17 +0000396
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000397 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
398 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000399 new DiagnosticsEngine(DiagID, new DiagnosticOptions,
400 DiagClient, /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000401
402 FileRemapper remapper;
403 bool err = remapper.initFromDisk(outputDir, *Diags,
404 /*ignoreIfFilesChanged=*/true);
405 if (err)
406 return true;
407
Ted Kremenekf7639e12012-03-06 20:06:33 +0000408 PreprocessorOptions PPOpts;
409 remapper.applyMappings(PPOpts);
410 remap = PPOpts.RemappedFiles;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000411
412 return false;
John McCalld70fb982011-06-15 23:25:17 +0000413}
414
Ted Kremenekf7639e12012-03-06 20:06:33 +0000415bool arcmt::getFileRemappingsFromFileList(
416 std::vector<std::pair<std::string,std::string> > &remap,
417 ArrayRef<StringRef> remapFiles,
418 DiagnosticConsumer *DiagClient) {
419 bool hasErrorOccurred = false;
420 llvm::StringMap<bool> Uniquer;
421
422 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
423 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000424 new DiagnosticsEngine(DiagID, new DiagnosticOptions,
425 DiagClient, /*ShouldOwnClient=*/false));
Ted Kremenekf7639e12012-03-06 20:06:33 +0000426
427 for (ArrayRef<StringRef>::iterator
428 I = remapFiles.begin(), E = remapFiles.end(); I != E; ++I) {
429 StringRef file = *I;
430
431 FileRemapper remapper;
432 bool err = remapper.initFromFile(file, *Diags,
433 /*ignoreIfFilesChanged=*/true);
434 hasErrorOccurred = hasErrorOccurred || err;
435 if (err)
436 continue;
437
438 PreprocessorOptions PPOpts;
439 remapper.applyMappings(PPOpts);
440 for (PreprocessorOptions::remapped_file_iterator
441 RI = PPOpts.remapped_file_begin(), RE = PPOpts.remapped_file_end();
442 RI != RE; ++RI) {
443 bool &inserted = Uniquer[RI->first];
444 if (inserted)
445 continue;
446 inserted = true;
447 remap.push_back(*RI);
448 }
449 }
450
451 return hasErrorOccurred;
452}
453
John McCalld70fb982011-06-15 23:25:17 +0000454//===----------------------------------------------------------------------===//
John McCalld70fb982011-06-15 23:25:17 +0000455// CollectTransformActions.
456//===----------------------------------------------------------------------===//
457
458namespace {
459
460class ARCMTMacroTrackerPPCallbacks : public PPCallbacks {
461 std::vector<SourceLocation> &ARCMTMacroLocs;
462
463public:
464 ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs)
465 : ARCMTMacroLocs(ARCMTMacroLocs) { }
466
Argyrios Kyrtzidis85a14bb2011-08-18 01:05:45 +0000467 virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo *MI,
468 SourceRange Range) {
John McCalld70fb982011-06-15 23:25:17 +0000469 if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName())
470 ARCMTMacroLocs.push_back(MacroNameTok.getLocation());
471 }
472};
473
474class ARCMTMacroTrackerAction : public ASTFrontendAction {
475 std::vector<SourceLocation> &ARCMTMacroLocs;
476
477public:
478 ARCMTMacroTrackerAction(std::vector<SourceLocation> &ARCMTMacroLocs)
479 : ARCMTMacroLocs(ARCMTMacroLocs) { }
480
481 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000482 StringRef InFile) {
John McCalld70fb982011-06-15 23:25:17 +0000483 CI.getPreprocessor().addPPCallbacks(
484 new ARCMTMacroTrackerPPCallbacks(ARCMTMacroLocs));
485 return new ASTConsumer();
486 }
487};
488
489class RewritesApplicator : public TransformActions::RewriteReceiver {
490 Rewriter &rewriter;
John McCalld70fb982011-06-15 23:25:17 +0000491 MigrationProcess::RewriteListener *Listener;
492
493public:
494 RewritesApplicator(Rewriter &rewriter, ASTContext &ctx,
495 MigrationProcess::RewriteListener *listener)
Benjamin Kramerd1d76b22012-06-06 17:32:50 +0000496 : rewriter(rewriter), Listener(listener) {
John McCalld70fb982011-06-15 23:25:17 +0000497 if (Listener)
498 Listener->start(ctx);
499 }
500 ~RewritesApplicator() {
501 if (Listener)
502 Listener->finish();
503 }
504
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000505 virtual void insert(SourceLocation loc, StringRef text) {
John McCalld70fb982011-06-15 23:25:17 +0000506 bool err = rewriter.InsertText(loc, text, /*InsertAfter=*/true,
507 /*indentNewLines=*/true);
508 if (!err && Listener)
509 Listener->insert(loc, text);
510 }
511
512 virtual void remove(CharSourceRange range) {
513 Rewriter::RewriteOptions removeOpts;
514 removeOpts.IncludeInsertsAtBeginOfRange = false;
515 removeOpts.IncludeInsertsAtEndOfRange = false;
516 removeOpts.RemoveLineIfEmpty = true;
517
518 bool err = rewriter.RemoveText(range, removeOpts);
519 if (!err && Listener)
520 Listener->remove(range);
521 }
522
523 virtual void increaseIndentation(CharSourceRange range,
524 SourceLocation parentIndent) {
525 rewriter.IncreaseIndentation(range, parentIndent);
526 }
527};
528
529} // end anonymous namespace.
530
531/// \brief Anchor for VTable.
532MigrationProcess::RewriteListener::~RewriteListener() { }
533
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000534MigrationProcess::MigrationProcess(const CompilerInvocation &CI,
David Blaikiee2eefae2011-09-25 23:39:51 +0000535 DiagnosticConsumer *diagClient,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000536 StringRef outputDir)
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000537 : OrigCI(CI), DiagClient(diagClient) {
538 if (!outputDir.empty()) {
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000539 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
540 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000541 new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(),
542 DiagClient, /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000543 Remapper.initFromDisk(outputDir, *Diags, /*ignoreIfFilesChanges=*/true);
544 }
545}
546
John McCalld70fb982011-06-15 23:25:17 +0000547bool MigrationProcess::applyTransform(TransformFn trans,
548 RewriteListener *listener) {
Dylan Noblesmithe2778992012-02-05 02:12:40 +0000549 OwningPtr<CompilerInvocation> CInvok;
John McCalld70fb982011-06-15 23:25:17 +0000550 CInvok.reset(createInvocationForMigration(OrigCI));
551 CInvok->getDiagnosticOpts().IgnoreWarnings = true;
552
Ted Kremenekf7639e12012-03-06 20:06:33 +0000553 Remapper.applyMappings(CInvok->getPreprocessorOpts());
John McCalld70fb982011-06-15 23:25:17 +0000554
555 CapturedDiagList capturedDiags;
556 std::vector<SourceLocation> ARCMTMacroLocs;
557
558 assert(DiagClient);
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000559 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
560 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000561 new DiagnosticsEngine(DiagID, new DiagnosticOptions,
562 DiagClient, /*ShouldOwnClient=*/false));
John McCalld70fb982011-06-15 23:25:17 +0000563
564 // Filter of all diagnostics.
Jordan Roseb00073d2012-08-10 01:06:16 +0000565 CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags);
John McCalld70fb982011-06-15 23:25:17 +0000566 Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
567
Dylan Noblesmithe2778992012-02-05 02:12:40 +0000568 OwningPtr<ARCMTMacroTrackerAction> ASTAction;
John McCalld70fb982011-06-15 23:25:17 +0000569 ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs));
570
Dylan Noblesmithe2778992012-02-05 02:12:40 +0000571 OwningPtr<ASTUnit> Unit(
John McCalld70fb982011-06-15 23:25:17 +0000572 ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags,
573 ASTAction.get()));
Jordan Roseb00073d2012-08-10 01:06:16 +0000574 if (!Unit) {
575 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000576 return true;
Jordan Roseb00073d2012-08-10 01:06:16 +0000577 }
John McCalld70fb982011-06-15 23:25:17 +0000578 Unit->setOwnsRemappedFileBuffers(false); // FileRemapper manages that.
579
580 // Don't filter diagnostics anymore.
581 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
582
583 ASTContext &Ctx = Unit->getASTContext();
584
585 if (Diags->hasFatalErrorOccurred()) {
586 Diags->Reset();
David Blaikiebbafb8a2012-03-11 07:00:24 +0000587 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
John McCalld70fb982011-06-15 23:25:17 +0000588 capturedDiags.reportDiagnostics(*Diags);
589 DiagClient->EndSourceFile();
Jordan Roseb00073d2012-08-10 01:06:16 +0000590 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000591 return true;
592 }
593
594 // After parsing of source files ended, we want to reuse the
595 // diagnostics objects to emit further diagnostics.
David Blaikiee2eefae2011-09-25 23:39:51 +0000596 // We call BeginSourceFile because DiagnosticConsumer requires that
John McCalld70fb982011-06-15 23:25:17 +0000597 // diagnostics with source range information are emitted only in between
598 // BeginSourceFile() and EndSourceFile().
David Blaikiebbafb8a2012-03-11 07:00:24 +0000599 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
John McCalld70fb982011-06-15 23:25:17 +0000600
David Blaikiebbafb8a2012-03-11 07:00:24 +0000601 Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts());
John McCalld70fb982011-06-15 23:25:17 +0000602 TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000603 MigrationPass pass(Ctx, OrigCI.getLangOpts()->getGC(),
Argyrios Kyrtzidis03fbe3e2013-01-04 18:30:08 +0000604 Unit->getSema(), TA, capturedDiags, ARCMTMacroLocs);
John McCalld70fb982011-06-15 23:25:17 +0000605
606 trans(pass);
607
608 {
609 RewritesApplicator applicator(rewriter, Ctx, listener);
610 TA.applyRewrites(applicator);
611 }
612
613 DiagClient->EndSourceFile();
Jordan Roseb00073d2012-08-10 01:06:16 +0000614 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000615
616 if (DiagClient->getNumErrors())
617 return true;
618
619 for (Rewriter::buffer_iterator
620 I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) {
621 FileID FID = I->first;
622 RewriteBuffer &buf = I->second;
623 const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID);
624 assert(file);
625 std::string newFname = file->getName();
626 newFname += "-trans";
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000627 SmallString<512> newText;
John McCalld70fb982011-06-15 23:25:17 +0000628 llvm::raw_svector_ostream vecOS(newText);
629 buf.write(vecOS);
630 vecOS.flush();
631 llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000632 StringRef(newText.data(), newText.size()), newFname);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000633 SmallString<64> filePath(file->getName());
John McCalld70fb982011-06-15 23:25:17 +0000634 Unit->getFileManager().FixupRelativePath(filePath);
635 Remapper.remap(filePath.str(), memBuf);
636 }
637
638 return false;
639}