blob: 3e429beded5db5f133e4463fc162dbf24f44d573 [file] [log] [blame]
John McCall8f0e8d22011-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 Carruth55fc8732012-12-04 09:13:33 +000011#include "clang/AST/ASTConsumer.h"
12#include "clang/Basic/DiagnosticCategories.h"
John McCall8f0e8d22011-06-15 23:25:17 +000013#include "clang/Frontend/ASTUnit.h"
14#include "clang/Frontend/CompilerInstance.h"
Douglas Gregor1f6b2b52012-01-20 16:28:04 +000015#include "clang/Frontend/FrontendAction.h"
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +000016#include "clang/Frontend/TextDiagnosticPrinter.h"
John McCall8f0e8d22011-06-15 23:25:17 +000017#include "clang/Frontend/Utils.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/Lex/Preprocessor.h"
Ted Kremenek305c6132012-09-01 05:09:24 +000019#include "clang/Rewrite/Core/Rewriter.h"
John McCall8f0e8d22011-06-15 23:25:17 +000020#include "clang/Sema/SemaDiagnostic.h"
Argyrios Kyrtzidised351e62013-02-05 16:37:00 +000021#include "clang/Serialization/ASTReader.h"
John McCall8f0e8d22011-06-15 23:25:17 +000022#include "llvm/ADT/Triple.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "llvm/Support/MemoryBuffer.h"
John McCall8f0e8d22011-06-15 23:25:17 +000024using namespace clang;
25using namespace arcmt;
John McCall8f0e8d22011-06-15 23:25:17 +000026
Chris Lattner2d3ba4f2011-07-23 17:14:25 +000027bool CapturedDiagList::clearDiagnostic(ArrayRef<unsigned> IDs,
John McCall8f0e8d22011-06-15 23:25:17 +000028 SourceRange range) {
29 if (range.isInvalid())
30 return false;
31
32 bool cleared = false;
33 ListTy::iterator I = List.begin();
34 while (I != List.end()) {
35 FullSourceLoc diagLoc = I->getLocation();
36 if ((IDs.empty() || // empty means clear all diagnostics in the range.
37 std::find(IDs.begin(), IDs.end(), I->getID()) != IDs.end()) &&
38 !diagLoc.isBeforeInTranslationUnitThan(range.getBegin()) &&
39 (diagLoc == range.getEnd() ||
40 diagLoc.isBeforeInTranslationUnitThan(range.getEnd()))) {
41 cleared = true;
42 ListTy::iterator eraseS = I++;
Argyrios Kyrtzidisea2224d2013-01-04 18:30:08 +000043 if (eraseS->getLevel() != DiagnosticsEngine::Note)
44 while (I != List.end() && I->getLevel() == DiagnosticsEngine::Note)
45 ++I;
John McCall8f0e8d22011-06-15 23:25:17 +000046 // Clear the diagnostic and any notes following it.
Joao Matose4d90e12012-08-31 17:28:09 +000047 I = List.erase(eraseS, I);
John McCall8f0e8d22011-06-15 23:25:17 +000048 continue;
49 }
50
51 ++I;
52 }
53
54 return cleared;
55}
56
Chris Lattner2d3ba4f2011-07-23 17:14:25 +000057bool CapturedDiagList::hasDiagnostic(ArrayRef<unsigned> IDs,
Argyrios Kyrtzidis60a5e3f2011-06-18 00:53:34 +000058 SourceRange range) const {
John McCall8f0e8d22011-06-15 23:25:17 +000059 if (range.isInvalid())
60 return false;
61
Argyrios Kyrtzidis60a5e3f2011-06-18 00:53:34 +000062 ListTy::const_iterator I = List.begin();
John McCall8f0e8d22011-06-15 23:25:17 +000063 while (I != List.end()) {
64 FullSourceLoc diagLoc = I->getLocation();
65 if ((IDs.empty() || // empty means any diagnostic in the range.
66 std::find(IDs.begin(), IDs.end(), I->getID()) != IDs.end()) &&
67 !diagLoc.isBeforeInTranslationUnitThan(range.getBegin()) &&
68 (diagLoc == range.getEnd() ||
69 diagLoc.isBeforeInTranslationUnitThan(range.getEnd()))) {
70 return true;
71 }
72
73 ++I;
74 }
75
76 return false;
77}
78
David Blaikied6471f72011-09-25 23:23:43 +000079void CapturedDiagList::reportDiagnostics(DiagnosticsEngine &Diags) const {
Argyrios Kyrtzidis60a5e3f2011-06-18 00:53:34 +000080 for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I)
John McCall8f0e8d22011-06-15 23:25:17 +000081 Diags.Report(*I);
82}
83
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +000084bool CapturedDiagList::hasErrors() const {
85 for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I)
David Blaikied6471f72011-09-25 23:23:43 +000086 if (I->getLevel() >= DiagnosticsEngine::Error)
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +000087 return true;
88
89 return false;
90}
91
John McCall8f0e8d22011-06-15 23:25:17 +000092namespace {
93
David Blaikieaee37ba2011-09-25 23:54:33 +000094class CaptureDiagnosticConsumer : public DiagnosticConsumer {
David Blaikied6471f72011-09-25 23:23:43 +000095 DiagnosticsEngine &Diags;
Jordan Rose7c304f52012-08-10 01:06:16 +000096 DiagnosticConsumer &DiagClient;
John McCall8f0e8d22011-06-15 23:25:17 +000097 CapturedDiagList &CapturedDiags;
Jordan Rose7c304f52012-08-10 01:06:16 +000098 bool HasBegunSourceFile;
John McCall8f0e8d22011-06-15 23:25:17 +000099public:
David Blaikieaee37ba2011-09-25 23:54:33 +0000100 CaptureDiagnosticConsumer(DiagnosticsEngine &diags,
Jordan Rose7c304f52012-08-10 01:06:16 +0000101 DiagnosticConsumer &client,
102 CapturedDiagList &capturedDiags)
103 : Diags(diags), DiagClient(client), CapturedDiags(capturedDiags),
104 HasBegunSourceFile(false) { }
105
106 virtual void BeginSourceFile(const LangOptions &Opts,
107 const Preprocessor *PP) {
108 // Pass BeginSourceFile message onto DiagClient on first call.
109 // The corresponding EndSourceFile call will be made from an
110 // explicit call to FinishCapture.
111 if (!HasBegunSourceFile) {
112 DiagClient.BeginSourceFile(Opts, PP);
113 HasBegunSourceFile = true;
114 }
115 }
116
117 void FinishCapture() {
118 // Call EndSourceFile on DiagClient on completion of capture to
119 // enable VerifyDiagnosticConsumer to check diagnostics *after*
120 // it has received the diagnostic list.
121 if (HasBegunSourceFile) {
122 DiagClient.EndSourceFile();
123 HasBegunSourceFile = false;
124 }
125 }
126
127 virtual ~CaptureDiagnosticConsumer() {
128 assert(!HasBegunSourceFile && "FinishCapture not called!");
129 }
John McCall8f0e8d22011-06-15 23:25:17 +0000130
David Blaikied6471f72011-09-25 23:23:43 +0000131 virtual void HandleDiagnostic(DiagnosticsEngine::Level level,
David Blaikie40847cf2011-09-26 01:18:08 +0000132 const Diagnostic &Info) {
Ted Kremenekafdc21a2011-10-20 05:07:47 +0000133 if (DiagnosticIDs::isARCDiagnostic(Info.getID()) ||
David Blaikied6471f72011-09-25 23:23:43 +0000134 level >= DiagnosticsEngine::Error || level == DiagnosticsEngine::Note) {
Argyrios Kyrtzidis489feee2012-12-12 22:48:28 +0000135 if (Info.getLocation().isValid())
136 CapturedDiags.push_back(StoredDiagnostic(level, Info));
John McCall8f0e8d22011-06-15 23:25:17 +0000137 return;
138 }
139
140 // Non-ARC warnings are ignored.
141 Diags.setLastDiagnosticIgnored();
142 }
143};
144
145} // end anonymous namespace
146
Argyrios Kyrtzidis9c479732011-06-20 19:59:52 +0000147static bool HasARCRuntime(CompilerInvocation &origCI) {
148 // This duplicates some functionality from Darwin::AddDeploymentTarget
149 // but this function is well defined, so keep it decoupled from the driver
150 // and avoid unrelated complications.
Argyrios Kyrtzidis9c479732011-06-20 19:59:52 +0000151 llvm::Triple triple(origCI.getTargetOpts().Triple);
152
Cameron Esfahani57b1da12013-09-14 01:09:11 +0000153 if (triple.isiOS())
Argyrios Kyrtzidis9c479732011-06-20 19:59:52 +0000154 return triple.getOSMajorVersion() >= 5;
155
156 if (triple.getOS() == llvm::Triple::Darwin)
157 return triple.getOSMajorVersion() >= 11;
158
159 if (triple.getOS() == llvm::Triple::MacOSX) {
160 unsigned Major, Minor, Micro;
161 triple.getOSVersion(Major, Minor, Micro);
162 return Major > 10 || (Major == 10 && Minor >= 7);
163 }
164
165 return false;
166}
167
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000168static CompilerInvocation *
169createInvocationForMigration(CompilerInvocation &origCI) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000170 OwningPtr<CompilerInvocation> CInvok;
John McCall8f0e8d22011-06-15 23:25:17 +0000171 CInvok.reset(new CompilerInvocation(origCI));
Argyrios Kyrtzidised351e62013-02-05 16:37:00 +0000172 PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts();
173 if (!PPOpts.ImplicitPCHInclude.empty()) {
174 // We can't use a PCH because it was likely built in non-ARC mode and we
175 // want to parse in ARC. Include the original header.
176 FileManager FileMgr(origCI.getFileSystemOpts());
177 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
178 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
179 new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
180 new IgnoringDiagConsumer()));
181 std::string OriginalFile =
182 ASTReader::getOriginalSourceFile(PPOpts.ImplicitPCHInclude,
183 FileMgr, *Diags);
184 if (!OriginalFile.empty())
185 PPOpts.Includes.insert(PPOpts.Includes.begin(), OriginalFile);
186 PPOpts.ImplicitPCHInclude.clear();
187 }
188 // FIXME: Get the original header of a PTH as well.
189 CInvok->getPreprocessorOpts().ImplicitPTHInclude.clear();
John McCall8f0e8d22011-06-15 23:25:17 +0000190 std::string define = getARCMTMacroName();
191 define += '=';
192 CInvok->getPreprocessorOpts().addMacroDef(define);
Ted Kremenekd3b74d92011-11-17 23:01:24 +0000193 CInvok->getLangOpts()->ObjCAutoRefCount = true;
194 CInvok->getLangOpts()->setGC(LangOptions::NonGC);
John McCall8f0e8d22011-06-15 23:25:17 +0000195 CInvok->getDiagnosticOpts().ErrorLimit = 0;
Argyrios Kyrtzidis19129822012-06-20 01:46:26 +0000196 CInvok->getDiagnosticOpts().PedanticErrors = 0;
Argyrios Kyrtzidisffe76dd2012-06-20 01:10:40 +0000197
198 // Ignore -Werror flags when migrating.
199 std::vector<std::string> WarnOpts;
200 for (std::vector<std::string>::iterator
201 I = CInvok->getDiagnosticOpts().Warnings.begin(),
202 E = CInvok->getDiagnosticOpts().Warnings.end(); I != E; ++I) {
203 if (!StringRef(*I).startswith("error"))
204 WarnOpts.push_back(*I);
205 }
206 WarnOpts.push_back("error=arc-unsafe-retained-assign");
Argyrios Kyrtzidis19129822012-06-20 01:46:26 +0000207 CInvok->getDiagnosticOpts().Warnings = llvm_move(WarnOpts);
Argyrios Kyrtzidisffe76dd2012-06-20 01:10:40 +0000208
John McCall0a7dd782012-08-21 02:47:43 +0000209 CInvok->getLangOpts()->ObjCARCWeak = HasARCRuntime(origCI);
John McCall8f0e8d22011-06-15 23:25:17 +0000210
211 return CInvok.take();
212}
213
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000214static void emitPremigrationErrors(const CapturedDiagList &arcDiags,
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000215 DiagnosticOptions *diagOpts,
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000216 Preprocessor &PP) {
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000217 TextDiagnosticPrinter printer(llvm::errs(), diagOpts);
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000218 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
219 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000220 new DiagnosticsEngine(DiagID, diagOpts, &printer,
221 /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000222 Diags->setSourceManager(&PP.getSourceManager());
223
David Blaikie4e4d0842012-03-11 07:00:24 +0000224 printer.BeginSourceFile(PP.getLangOpts(), &PP);
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000225 arcDiags.reportDiagnostics(*Diags);
226 printer.EndSourceFile();
227}
228
John McCall8f0e8d22011-06-15 23:25:17 +0000229//===----------------------------------------------------------------------===//
230// checkForManualIssues.
231//===----------------------------------------------------------------------===//
232
233bool arcmt::checkForManualIssues(CompilerInvocation &origCI,
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000234 const FrontendInputFile &Input,
David Blaikie78ad0b92011-09-25 23:39:51 +0000235 DiagnosticConsumer *DiagClient,
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000236 bool emitPremigrationARCErrors,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000237 StringRef plistOut) {
Ted Kremenekd3b74d92011-11-17 23:01:24 +0000238 if (!origCI.getLangOpts()->ObjC1)
John McCall8f0e8d22011-06-15 23:25:17 +0000239 return false;
240
Ted Kremenekd3b74d92011-11-17 23:01:24 +0000241 LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC();
Fariborz Jahanianb5c6bab2012-01-25 00:20:29 +0000242 bool NoNSAllocReallocError = origCI.getMigratorOpts().NoNSAllocReallocError;
Fariborz Jahanian26f0e4e2012-01-26 00:08:04 +0000243 bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval;
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000244
Fariborz Jahanianbbdfad52012-01-26 20:57:58 +0000245 std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode,
246 NoFinalizeRemoval);
John McCall8f0e8d22011-06-15 23:25:17 +0000247 assert(!transforms.empty());
248
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000249 OwningPtr<CompilerInvocation> CInvok;
John McCall8f0e8d22011-06-15 23:25:17 +0000250 CInvok.reset(createInvocationForMigration(origCI));
251 CInvok->getFrontendOpts().Inputs.clear();
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000252 CInvok->getFrontendOpts().Inputs.push_back(Input);
John McCall8f0e8d22011-06-15 23:25:17 +0000253
254 CapturedDiagList capturedDiags;
255
256 assert(DiagClient);
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000257 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
258 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000259 new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
260 DiagClient, /*ShouldOwnClient=*/false));
John McCall8f0e8d22011-06-15 23:25:17 +0000261
262 // Filter of all diagnostics.
Jordan Rose7c304f52012-08-10 01:06:16 +0000263 CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags);
John McCall8f0e8d22011-06-15 23:25:17 +0000264 Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
265
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000266 OwningPtr<ASTUnit> Unit(
John McCall8f0e8d22011-06-15 23:25:17 +0000267 ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags));
Jordan Rose7c304f52012-08-10 01:06:16 +0000268 if (!Unit) {
269 errRec.FinishCapture();
John McCall8f0e8d22011-06-15 23:25:17 +0000270 return true;
Jordan Rose7c304f52012-08-10 01:06:16 +0000271 }
John McCall8f0e8d22011-06-15 23:25:17 +0000272
273 // Don't filter diagnostics anymore.
274 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
275
276 ASTContext &Ctx = Unit->getASTContext();
277
278 if (Diags->hasFatalErrorOccurred()) {
279 Diags->Reset();
David Blaikie4e4d0842012-03-11 07:00:24 +0000280 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
John McCall8f0e8d22011-06-15 23:25:17 +0000281 capturedDiags.reportDiagnostics(*Diags);
282 DiagClient->EndSourceFile();
Jordan Rose7c304f52012-08-10 01:06:16 +0000283 errRec.FinishCapture();
John McCall8f0e8d22011-06-15 23:25:17 +0000284 return true;
285 }
286
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000287 if (emitPremigrationARCErrors)
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000288 emitPremigrationErrors(capturedDiags, &origCI.getDiagnosticOpts(),
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000289 Unit->getPreprocessor());
290 if (!plistOut.empty()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000291 SmallVector<StoredDiagnostic, 8> arcDiags;
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000292 for (CapturedDiagList::iterator
293 I = capturedDiags.begin(), E = capturedDiags.end(); I != E; ++I)
294 arcDiags.push_back(*I);
295 writeARCDiagsToPlist(plistOut, arcDiags,
David Blaikie4e4d0842012-03-11 07:00:24 +0000296 Ctx.getSourceManager(), Ctx.getLangOpts());
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000297 }
298
John McCall8f0e8d22011-06-15 23:25:17 +0000299 // After parsing of source files ended, we want to reuse the
300 // diagnostics objects to emit further diagnostics.
David Blaikie78ad0b92011-09-25 23:39:51 +0000301 // We call BeginSourceFile because DiagnosticConsumer requires that
John McCall8f0e8d22011-06-15 23:25:17 +0000302 // diagnostics with source range information are emitted only in between
303 // BeginSourceFile() and EndSourceFile().
David Blaikie4e4d0842012-03-11 07:00:24 +0000304 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
John McCall8f0e8d22011-06-15 23:25:17 +0000305
306 // No macros will be added since we are just checking and we won't modify
307 // source code.
308 std::vector<SourceLocation> ARCMTMacroLocs;
309
310 TransformActions testAct(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
Argyrios Kyrtzidisea2224d2013-01-04 18:30:08 +0000311 MigrationPass pass(Ctx, OrigGCMode, Unit->getSema(), testAct, capturedDiags,
312 ARCMTMacroLocs);
Fariborz Jahanianb5c6bab2012-01-25 00:20:29 +0000313 pass.setNSAllocReallocError(NoNSAllocReallocError);
Fariborz Jahanian26f0e4e2012-01-26 00:08:04 +0000314 pass.setNoFinalizeRemoval(NoFinalizeRemoval);
John McCall8f0e8d22011-06-15 23:25:17 +0000315
316 for (unsigned i=0, e = transforms.size(); i != e; ++i)
317 transforms[i](pass);
318
319 capturedDiags.reportDiagnostics(*Diags);
320
321 DiagClient->EndSourceFile();
Jordan Rose7c304f52012-08-10 01:06:16 +0000322 errRec.FinishCapture();
John McCall8f0e8d22011-06-15 23:25:17 +0000323
Argyrios Kyrtzidisfd103982011-07-18 07:44:45 +0000324 return capturedDiags.hasErrors() || testAct.hasReportedErrors();
John McCall8f0e8d22011-06-15 23:25:17 +0000325}
326
327//===----------------------------------------------------------------------===//
328// applyTransformations.
329//===----------------------------------------------------------------------===//
330
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000331static bool applyTransforms(CompilerInvocation &origCI,
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000332 const FrontendInputFile &Input,
David Blaikie78ad0b92011-09-25 23:39:51 +0000333 DiagnosticConsumer *DiagClient,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000334 StringRef outputDir,
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000335 bool emitPremigrationARCErrors,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000336 StringRef plistOut) {
Ted Kremenekd3b74d92011-11-17 23:01:24 +0000337 if (!origCI.getLangOpts()->ObjC1)
John McCall8f0e8d22011-06-15 23:25:17 +0000338 return false;
339
Ted Kremenekd3b74d92011-11-17 23:01:24 +0000340 LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC();
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000341
John McCall8f0e8d22011-06-15 23:25:17 +0000342 // Make sure checking is successful first.
343 CompilerInvocation CInvokForCheck(origCI);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000344 if (arcmt::checkForManualIssues(CInvokForCheck, Input, DiagClient,
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000345 emitPremigrationARCErrors, plistOut))
John McCall8f0e8d22011-06-15 23:25:17 +0000346 return true;
347
348 CompilerInvocation CInvok(origCI);
349 CInvok.getFrontendOpts().Inputs.clear();
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000350 CInvok.getFrontendOpts().Inputs.push_back(Input);
John McCall8f0e8d22011-06-15 23:25:17 +0000351
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000352 MigrationProcess migration(CInvok, DiagClient, outputDir);
Fariborz Jahanianbbdfad52012-01-26 20:57:58 +0000353 bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval;
John McCall8f0e8d22011-06-15 23:25:17 +0000354
Fariborz Jahanianbbdfad52012-01-26 20:57:58 +0000355 std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode,
356 NoFinalizeRemoval);
John McCall8f0e8d22011-06-15 23:25:17 +0000357 assert(!transforms.empty());
358
359 for (unsigned i=0, e = transforms.size(); i != e; ++i) {
360 bool err = migration.applyTransform(transforms[i]);
361 if (err) return true;
362 }
363
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000364 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
365 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000366 new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
367 DiagClient, /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000368
369 if (outputDir.empty()) {
Ted Kremenekd3b74d92011-11-17 23:01:24 +0000370 origCI.getLangOpts()->ObjCAutoRefCount = true;
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000371 return migration.getRemapper().overwriteOriginal(*Diags);
Argyrios Kyrtzidisa944b122011-07-14 00:17:54 +0000372 } else {
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000373 return migration.getRemapper().flushToDisk(outputDir, *Diags);
Argyrios Kyrtzidisa944b122011-07-14 00:17:54 +0000374 }
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000375}
376
377bool arcmt::applyTransformations(CompilerInvocation &origCI,
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000378 const FrontendInputFile &Input,
David Blaikie78ad0b92011-09-25 23:39:51 +0000379 DiagnosticConsumer *DiagClient) {
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000380 return applyTransforms(origCI, Input, DiagClient,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000381 StringRef(), false, StringRef());
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000382}
383
384bool arcmt::migrateWithTemporaryFiles(CompilerInvocation &origCI,
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000385 const FrontendInputFile &Input,
David Blaikie78ad0b92011-09-25 23:39:51 +0000386 DiagnosticConsumer *DiagClient,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000387 StringRef outputDir,
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000388 bool emitPremigrationARCErrors,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000389 StringRef plistOut) {
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000390 assert(!outputDir.empty() && "Expected output directory path");
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000391 return applyTransforms(origCI, Input, DiagClient,
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000392 outputDir, emitPremigrationARCErrors, plistOut);
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000393}
394
395bool arcmt::getFileRemappings(std::vector<std::pair<std::string,std::string> > &
396 remap,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000397 StringRef outputDir,
David Blaikie78ad0b92011-09-25 23:39:51 +0000398 DiagnosticConsumer *DiagClient) {
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000399 assert(!outputDir.empty());
John McCall8f0e8d22011-06-15 23:25:17 +0000400
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000401 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
402 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000403 new DiagnosticsEngine(DiagID, new DiagnosticOptions,
404 DiagClient, /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000405
406 FileRemapper remapper;
407 bool err = remapper.initFromDisk(outputDir, *Diags,
408 /*ignoreIfFilesChanged=*/true);
409 if (err)
410 return true;
411
Ted Kremenek30660a82012-03-06 20:06:33 +0000412 PreprocessorOptions PPOpts;
413 remapper.applyMappings(PPOpts);
414 remap = PPOpts.RemappedFiles;
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000415
416 return false;
John McCall8f0e8d22011-06-15 23:25:17 +0000417}
418
Ted Kremenek30660a82012-03-06 20:06:33 +0000419bool arcmt::getFileRemappingsFromFileList(
420 std::vector<std::pair<std::string,std::string> > &remap,
421 ArrayRef<StringRef> remapFiles,
422 DiagnosticConsumer *DiagClient) {
423 bool hasErrorOccurred = false;
424 llvm::StringMap<bool> Uniquer;
425
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000426 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
427 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000428 new DiagnosticsEngine(DiagID, new DiagnosticOptions,
429 DiagClient, /*ShouldOwnClient=*/false));
Ted Kremenek30660a82012-03-06 20:06:33 +0000430
431 for (ArrayRef<StringRef>::iterator
432 I = remapFiles.begin(), E = remapFiles.end(); I != E; ++I) {
433 StringRef file = *I;
434
435 FileRemapper remapper;
436 bool err = remapper.initFromFile(file, *Diags,
437 /*ignoreIfFilesChanged=*/true);
438 hasErrorOccurred = hasErrorOccurred || err;
439 if (err)
440 continue;
441
442 PreprocessorOptions PPOpts;
443 remapper.applyMappings(PPOpts);
444 for (PreprocessorOptions::remapped_file_iterator
445 RI = PPOpts.remapped_file_begin(), RE = PPOpts.remapped_file_end();
446 RI != RE; ++RI) {
447 bool &inserted = Uniquer[RI->first];
448 if (inserted)
449 continue;
450 inserted = true;
451 remap.push_back(*RI);
452 }
453 }
454
455 return hasErrorOccurred;
456}
457
John McCall8f0e8d22011-06-15 23:25:17 +0000458//===----------------------------------------------------------------------===//
John McCall8f0e8d22011-06-15 23:25:17 +0000459// CollectTransformActions.
460//===----------------------------------------------------------------------===//
461
462namespace {
463
464class ARCMTMacroTrackerPPCallbacks : public PPCallbacks {
465 std::vector<SourceLocation> &ARCMTMacroLocs;
466
467public:
468 ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs)
469 : ARCMTMacroLocs(ARCMTMacroLocs) { }
470
Argyrios Kyrtzidisc5159782013-02-24 00:05:14 +0000471 virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
Argyrios Kyrtzidisdd08a0c2013-05-03 22:31:32 +0000472 SourceRange Range, const MacroArgs *Args) {
John McCall8f0e8d22011-06-15 23:25:17 +0000473 if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName())
474 ARCMTMacroLocs.push_back(MacroNameTok.getLocation());
475 }
476};
477
478class ARCMTMacroTrackerAction : public ASTFrontendAction {
479 std::vector<SourceLocation> &ARCMTMacroLocs;
480
481public:
482 ARCMTMacroTrackerAction(std::vector<SourceLocation> &ARCMTMacroLocs)
483 : ARCMTMacroLocs(ARCMTMacroLocs) { }
484
485 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000486 StringRef InFile) {
John McCall8f0e8d22011-06-15 23:25:17 +0000487 CI.getPreprocessor().addPPCallbacks(
488 new ARCMTMacroTrackerPPCallbacks(ARCMTMacroLocs));
489 return new ASTConsumer();
490 }
491};
492
493class RewritesApplicator : public TransformActions::RewriteReceiver {
494 Rewriter &rewriter;
John McCall8f0e8d22011-06-15 23:25:17 +0000495 MigrationProcess::RewriteListener *Listener;
496
497public:
498 RewritesApplicator(Rewriter &rewriter, ASTContext &ctx,
499 MigrationProcess::RewriteListener *listener)
Benjamin Kramerfacde172012-06-06 17:32:50 +0000500 : rewriter(rewriter), Listener(listener) {
John McCall8f0e8d22011-06-15 23:25:17 +0000501 if (Listener)
502 Listener->start(ctx);
503 }
504 ~RewritesApplicator() {
505 if (Listener)
506 Listener->finish();
507 }
508
Chris Lattner5f9e2722011-07-23 10:55:15 +0000509 virtual void insert(SourceLocation loc, StringRef text) {
John McCall8f0e8d22011-06-15 23:25:17 +0000510 bool err = rewriter.InsertText(loc, text, /*InsertAfter=*/true,
511 /*indentNewLines=*/true);
512 if (!err && Listener)
513 Listener->insert(loc, text);
514 }
515
516 virtual void remove(CharSourceRange range) {
517 Rewriter::RewriteOptions removeOpts;
518 removeOpts.IncludeInsertsAtBeginOfRange = false;
519 removeOpts.IncludeInsertsAtEndOfRange = false;
520 removeOpts.RemoveLineIfEmpty = true;
521
522 bool err = rewriter.RemoveText(range, removeOpts);
523 if (!err && Listener)
524 Listener->remove(range);
525 }
526
527 virtual void increaseIndentation(CharSourceRange range,
528 SourceLocation parentIndent) {
529 rewriter.IncreaseIndentation(range, parentIndent);
530 }
531};
532
533} // end anonymous namespace.
534
535/// \brief Anchor for VTable.
536MigrationProcess::RewriteListener::~RewriteListener() { }
537
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000538MigrationProcess::MigrationProcess(const CompilerInvocation &CI,
David Blaikie78ad0b92011-09-25 23:39:51 +0000539 DiagnosticConsumer *diagClient,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000540 StringRef outputDir)
Argyrios Kyrtzidis0474cfd2013-07-22 18:13:54 +0000541 : OrigCI(CI), DiagClient(diagClient), HadARCErrors(false) {
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000542 if (!outputDir.empty()) {
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000543 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
544 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000545 new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(),
546 DiagClient, /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000547 Remapper.initFromDisk(outputDir, *Diags, /*ignoreIfFilesChanges=*/true);
548 }
549}
550
John McCall8f0e8d22011-06-15 23:25:17 +0000551bool MigrationProcess::applyTransform(TransformFn trans,
552 RewriteListener *listener) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000553 OwningPtr<CompilerInvocation> CInvok;
John McCall8f0e8d22011-06-15 23:25:17 +0000554 CInvok.reset(createInvocationForMigration(OrigCI));
555 CInvok->getDiagnosticOpts().IgnoreWarnings = true;
556
Ted Kremenek30660a82012-03-06 20:06:33 +0000557 Remapper.applyMappings(CInvok->getPreprocessorOpts());
John McCall8f0e8d22011-06-15 23:25:17 +0000558
559 CapturedDiagList capturedDiags;
560 std::vector<SourceLocation> ARCMTMacroLocs;
561
562 assert(DiagClient);
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000563 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
564 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000565 new DiagnosticsEngine(DiagID, new DiagnosticOptions,
566 DiagClient, /*ShouldOwnClient=*/false));
John McCall8f0e8d22011-06-15 23:25:17 +0000567
568 // Filter of all diagnostics.
Jordan Rose7c304f52012-08-10 01:06:16 +0000569 CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags);
John McCall8f0e8d22011-06-15 23:25:17 +0000570 Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
571
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000572 OwningPtr<ARCMTMacroTrackerAction> ASTAction;
John McCall8f0e8d22011-06-15 23:25:17 +0000573 ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs));
574
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000575 OwningPtr<ASTUnit> Unit(
John McCall8f0e8d22011-06-15 23:25:17 +0000576 ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags,
577 ASTAction.get()));
Jordan Rose7c304f52012-08-10 01:06:16 +0000578 if (!Unit) {
579 errRec.FinishCapture();
John McCall8f0e8d22011-06-15 23:25:17 +0000580 return true;
Jordan Rose7c304f52012-08-10 01:06:16 +0000581 }
John McCall8f0e8d22011-06-15 23:25:17 +0000582 Unit->setOwnsRemappedFileBuffers(false); // FileRemapper manages that.
583
Argyrios Kyrtzidis0474cfd2013-07-22 18:13:54 +0000584 HadARCErrors = HadARCErrors || capturedDiags.hasErrors();
585
John McCall8f0e8d22011-06-15 23:25:17 +0000586 // Don't filter diagnostics anymore.
587 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
588
589 ASTContext &Ctx = Unit->getASTContext();
590
591 if (Diags->hasFatalErrorOccurred()) {
592 Diags->Reset();
David Blaikie4e4d0842012-03-11 07:00:24 +0000593 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
John McCall8f0e8d22011-06-15 23:25:17 +0000594 capturedDiags.reportDiagnostics(*Diags);
595 DiagClient->EndSourceFile();
Jordan Rose7c304f52012-08-10 01:06:16 +0000596 errRec.FinishCapture();
John McCall8f0e8d22011-06-15 23:25:17 +0000597 return true;
598 }
599
600 // After parsing of source files ended, we want to reuse the
601 // diagnostics objects to emit further diagnostics.
David Blaikie78ad0b92011-09-25 23:39:51 +0000602 // We call BeginSourceFile because DiagnosticConsumer requires that
John McCall8f0e8d22011-06-15 23:25:17 +0000603 // diagnostics with source range information are emitted only in between
604 // BeginSourceFile() and EndSourceFile().
David Blaikie4e4d0842012-03-11 07:00:24 +0000605 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
John McCall8f0e8d22011-06-15 23:25:17 +0000606
David Blaikie4e4d0842012-03-11 07:00:24 +0000607 Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts());
John McCall8f0e8d22011-06-15 23:25:17 +0000608 TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
Ted Kremenekd3b74d92011-11-17 23:01:24 +0000609 MigrationPass pass(Ctx, OrigCI.getLangOpts()->getGC(),
Argyrios Kyrtzidisea2224d2013-01-04 18:30:08 +0000610 Unit->getSema(), TA, capturedDiags, ARCMTMacroLocs);
John McCall8f0e8d22011-06-15 23:25:17 +0000611
612 trans(pass);
613
614 {
615 RewritesApplicator applicator(rewriter, Ctx, listener);
616 TA.applyRewrites(applicator);
617 }
618
619 DiagClient->EndSourceFile();
Jordan Rose7c304f52012-08-10 01:06:16 +0000620 errRec.FinishCapture();
John McCall8f0e8d22011-06-15 23:25:17 +0000621
622 if (DiagClient->getNumErrors())
623 return true;
624
625 for (Rewriter::buffer_iterator
626 I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) {
627 FileID FID = I->first;
628 RewriteBuffer &buf = I->second;
629 const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID);
630 assert(file);
631 std::string newFname = file->getName();
632 newFname += "-trans";
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000633 SmallString<512> newText;
John McCall8f0e8d22011-06-15 23:25:17 +0000634 llvm::raw_svector_ostream vecOS(newText);
635 buf.write(vecOS);
636 vecOS.flush();
637 llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000638 StringRef(newText.data(), newText.size()), newFname);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000639 SmallString<64> filePath(file->getName());
John McCall8f0e8d22011-06-15 23:25:17 +0000640 Unit->getFileManager().FixupRelativePath(filePath);
641 Remapper.remap(filePath.str(), memBuf);
642 }
643
644 return false;
645}