blob: 8a2b939ac302ff0c551b7c47d708eaf92f53d1d1 [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
153 if (triple.getOS() == llvm::Triple::IOS)
154 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 Kyrtzidisa944b122011-07-14 00:17:54 +0000324 // If we are migrating code that gets the '-fobjc-arc' flag, make sure
325 // to remove it so that we don't get errors from normal compilation.
Ted Kremenekd3b74d92011-11-17 23:01:24 +0000326 origCI.getLangOpts()->ObjCAutoRefCount = false;
Argyrios Kyrtzidisd0c5b8a2013-07-19 18:57:15 +0000327 // Disable auto-synthesize to avoid "@synthesize of 'weak' property is only
328 // allowed in ARC" errors.
329 origCI.getLangOpts()->ObjCDefaultSynthProperties = false;
Argyrios Kyrtzidisa944b122011-07-14 00:17:54 +0000330
Argyrios Kyrtzidisfd103982011-07-18 07:44:45 +0000331 return capturedDiags.hasErrors() || testAct.hasReportedErrors();
John McCall8f0e8d22011-06-15 23:25:17 +0000332}
333
334//===----------------------------------------------------------------------===//
335// applyTransformations.
336//===----------------------------------------------------------------------===//
337
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000338static bool applyTransforms(CompilerInvocation &origCI,
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000339 const FrontendInputFile &Input,
David Blaikie78ad0b92011-09-25 23:39:51 +0000340 DiagnosticConsumer *DiagClient,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000341 StringRef outputDir,
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000342 bool emitPremigrationARCErrors,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000343 StringRef plistOut) {
Ted Kremenekd3b74d92011-11-17 23:01:24 +0000344 if (!origCI.getLangOpts()->ObjC1)
John McCall8f0e8d22011-06-15 23:25:17 +0000345 return false;
346
Ted Kremenekd3b74d92011-11-17 23:01:24 +0000347 LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC();
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000348
John McCall8f0e8d22011-06-15 23:25:17 +0000349 // Make sure checking is successful first.
350 CompilerInvocation CInvokForCheck(origCI);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000351 if (arcmt::checkForManualIssues(CInvokForCheck, Input, DiagClient,
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000352 emitPremigrationARCErrors, plistOut))
John McCall8f0e8d22011-06-15 23:25:17 +0000353 return true;
354
355 CompilerInvocation CInvok(origCI);
356 CInvok.getFrontendOpts().Inputs.clear();
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000357 CInvok.getFrontendOpts().Inputs.push_back(Input);
John McCall8f0e8d22011-06-15 23:25:17 +0000358
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000359 MigrationProcess migration(CInvok, DiagClient, outputDir);
Fariborz Jahanianbbdfad52012-01-26 20:57:58 +0000360 bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval;
John McCall8f0e8d22011-06-15 23:25:17 +0000361
Fariborz Jahanianbbdfad52012-01-26 20:57:58 +0000362 std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode,
363 NoFinalizeRemoval);
John McCall8f0e8d22011-06-15 23:25:17 +0000364 assert(!transforms.empty());
365
366 for (unsigned i=0, e = transforms.size(); i != e; ++i) {
367 bool err = migration.applyTransform(transforms[i]);
368 if (err) return true;
369 }
370
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000371 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
372 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000373 new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
374 DiagClient, /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000375
376 if (outputDir.empty()) {
Ted Kremenekd3b74d92011-11-17 23:01:24 +0000377 origCI.getLangOpts()->ObjCAutoRefCount = true;
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000378 return migration.getRemapper().overwriteOriginal(*Diags);
Argyrios Kyrtzidisa944b122011-07-14 00:17:54 +0000379 } else {
380 // If we are migrating code that gets the '-fobjc-arc' flag, make sure
381 // to remove it so that we don't get errors from normal compilation.
Ted Kremenekd3b74d92011-11-17 23:01:24 +0000382 origCI.getLangOpts()->ObjCAutoRefCount = false;
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000383 return migration.getRemapper().flushToDisk(outputDir, *Diags);
Argyrios Kyrtzidisa944b122011-07-14 00:17:54 +0000384 }
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000385}
386
387bool arcmt::applyTransformations(CompilerInvocation &origCI,
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000388 const FrontendInputFile &Input,
David Blaikie78ad0b92011-09-25 23:39:51 +0000389 DiagnosticConsumer *DiagClient) {
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000390 return applyTransforms(origCI, Input, DiagClient,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000391 StringRef(), false, StringRef());
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000392}
393
394bool arcmt::migrateWithTemporaryFiles(CompilerInvocation &origCI,
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000395 const FrontendInputFile &Input,
David Blaikie78ad0b92011-09-25 23:39:51 +0000396 DiagnosticConsumer *DiagClient,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000397 StringRef outputDir,
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000398 bool emitPremigrationARCErrors,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000399 StringRef plistOut) {
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000400 assert(!outputDir.empty() && "Expected output directory path");
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000401 return applyTransforms(origCI, Input, DiagClient,
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000402 outputDir, emitPremigrationARCErrors, plistOut);
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000403}
404
405bool arcmt::getFileRemappings(std::vector<std::pair<std::string,std::string> > &
406 remap,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000407 StringRef outputDir,
David Blaikie78ad0b92011-09-25 23:39:51 +0000408 DiagnosticConsumer *DiagClient) {
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000409 assert(!outputDir.empty());
John McCall8f0e8d22011-06-15 23:25:17 +0000410
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000411 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
412 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000413 new DiagnosticsEngine(DiagID, new DiagnosticOptions,
414 DiagClient, /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000415
416 FileRemapper remapper;
417 bool err = remapper.initFromDisk(outputDir, *Diags,
418 /*ignoreIfFilesChanged=*/true);
419 if (err)
420 return true;
421
Ted Kremenek30660a82012-03-06 20:06:33 +0000422 PreprocessorOptions PPOpts;
423 remapper.applyMappings(PPOpts);
424 remap = PPOpts.RemappedFiles;
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000425
426 return false;
John McCall8f0e8d22011-06-15 23:25:17 +0000427}
428
Ted Kremenek30660a82012-03-06 20:06:33 +0000429bool arcmt::getFileRemappingsFromFileList(
430 std::vector<std::pair<std::string,std::string> > &remap,
431 ArrayRef<StringRef> remapFiles,
432 DiagnosticConsumer *DiagClient) {
433 bool hasErrorOccurred = false;
434 llvm::StringMap<bool> Uniquer;
435
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000436 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
437 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000438 new DiagnosticsEngine(DiagID, new DiagnosticOptions,
439 DiagClient, /*ShouldOwnClient=*/false));
Ted Kremenek30660a82012-03-06 20:06:33 +0000440
441 for (ArrayRef<StringRef>::iterator
442 I = remapFiles.begin(), E = remapFiles.end(); I != E; ++I) {
443 StringRef file = *I;
444
445 FileRemapper remapper;
446 bool err = remapper.initFromFile(file, *Diags,
447 /*ignoreIfFilesChanged=*/true);
448 hasErrorOccurred = hasErrorOccurred || err;
449 if (err)
450 continue;
451
452 PreprocessorOptions PPOpts;
453 remapper.applyMappings(PPOpts);
454 for (PreprocessorOptions::remapped_file_iterator
455 RI = PPOpts.remapped_file_begin(), RE = PPOpts.remapped_file_end();
456 RI != RE; ++RI) {
457 bool &inserted = Uniquer[RI->first];
458 if (inserted)
459 continue;
460 inserted = true;
461 remap.push_back(*RI);
462 }
463 }
464
465 return hasErrorOccurred;
466}
467
John McCall8f0e8d22011-06-15 23:25:17 +0000468//===----------------------------------------------------------------------===//
John McCall8f0e8d22011-06-15 23:25:17 +0000469// CollectTransformActions.
470//===----------------------------------------------------------------------===//
471
472namespace {
473
474class ARCMTMacroTrackerPPCallbacks : public PPCallbacks {
475 std::vector<SourceLocation> &ARCMTMacroLocs;
476
477public:
478 ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs)
479 : ARCMTMacroLocs(ARCMTMacroLocs) { }
480
Argyrios Kyrtzidisc5159782013-02-24 00:05:14 +0000481 virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
Argyrios Kyrtzidisdd08a0c2013-05-03 22:31:32 +0000482 SourceRange Range, const MacroArgs *Args) {
John McCall8f0e8d22011-06-15 23:25:17 +0000483 if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName())
484 ARCMTMacroLocs.push_back(MacroNameTok.getLocation());
485 }
486};
487
488class ARCMTMacroTrackerAction : public ASTFrontendAction {
489 std::vector<SourceLocation> &ARCMTMacroLocs;
490
491public:
492 ARCMTMacroTrackerAction(std::vector<SourceLocation> &ARCMTMacroLocs)
493 : ARCMTMacroLocs(ARCMTMacroLocs) { }
494
495 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000496 StringRef InFile) {
John McCall8f0e8d22011-06-15 23:25:17 +0000497 CI.getPreprocessor().addPPCallbacks(
498 new ARCMTMacroTrackerPPCallbacks(ARCMTMacroLocs));
499 return new ASTConsumer();
500 }
501};
502
503class RewritesApplicator : public TransformActions::RewriteReceiver {
504 Rewriter &rewriter;
John McCall8f0e8d22011-06-15 23:25:17 +0000505 MigrationProcess::RewriteListener *Listener;
506
507public:
508 RewritesApplicator(Rewriter &rewriter, ASTContext &ctx,
509 MigrationProcess::RewriteListener *listener)
Benjamin Kramerfacde172012-06-06 17:32:50 +0000510 : rewriter(rewriter), Listener(listener) {
John McCall8f0e8d22011-06-15 23:25:17 +0000511 if (Listener)
512 Listener->start(ctx);
513 }
514 ~RewritesApplicator() {
515 if (Listener)
516 Listener->finish();
517 }
518
Chris Lattner5f9e2722011-07-23 10:55:15 +0000519 virtual void insert(SourceLocation loc, StringRef text) {
John McCall8f0e8d22011-06-15 23:25:17 +0000520 bool err = rewriter.InsertText(loc, text, /*InsertAfter=*/true,
521 /*indentNewLines=*/true);
522 if (!err && Listener)
523 Listener->insert(loc, text);
524 }
525
526 virtual void remove(CharSourceRange range) {
527 Rewriter::RewriteOptions removeOpts;
528 removeOpts.IncludeInsertsAtBeginOfRange = false;
529 removeOpts.IncludeInsertsAtEndOfRange = false;
530 removeOpts.RemoveLineIfEmpty = true;
531
532 bool err = rewriter.RemoveText(range, removeOpts);
533 if (!err && Listener)
534 Listener->remove(range);
535 }
536
537 virtual void increaseIndentation(CharSourceRange range,
538 SourceLocation parentIndent) {
539 rewriter.IncreaseIndentation(range, parentIndent);
540 }
541};
542
543} // end anonymous namespace.
544
545/// \brief Anchor for VTable.
546MigrationProcess::RewriteListener::~RewriteListener() { }
547
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000548MigrationProcess::MigrationProcess(const CompilerInvocation &CI,
David Blaikie78ad0b92011-09-25 23:39:51 +0000549 DiagnosticConsumer *diagClient,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000550 StringRef outputDir)
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000551 : OrigCI(CI), DiagClient(diagClient) {
552 if (!outputDir.empty()) {
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000553 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
554 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000555 new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(),
556 DiagClient, /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000557 Remapper.initFromDisk(outputDir, *Diags, /*ignoreIfFilesChanges=*/true);
558 }
559}
560
John McCall8f0e8d22011-06-15 23:25:17 +0000561bool MigrationProcess::applyTransform(TransformFn trans,
562 RewriteListener *listener) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000563 OwningPtr<CompilerInvocation> CInvok;
John McCall8f0e8d22011-06-15 23:25:17 +0000564 CInvok.reset(createInvocationForMigration(OrigCI));
565 CInvok->getDiagnosticOpts().IgnoreWarnings = true;
566
Ted Kremenek30660a82012-03-06 20:06:33 +0000567 Remapper.applyMappings(CInvok->getPreprocessorOpts());
John McCall8f0e8d22011-06-15 23:25:17 +0000568
569 CapturedDiagList capturedDiags;
570 std::vector<SourceLocation> ARCMTMacroLocs;
571
572 assert(DiagClient);
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000573 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
574 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000575 new DiagnosticsEngine(DiagID, new DiagnosticOptions,
576 DiagClient, /*ShouldOwnClient=*/false));
John McCall8f0e8d22011-06-15 23:25:17 +0000577
578 // Filter of all diagnostics.
Jordan Rose7c304f52012-08-10 01:06:16 +0000579 CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags);
John McCall8f0e8d22011-06-15 23:25:17 +0000580 Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
581
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000582 OwningPtr<ARCMTMacroTrackerAction> ASTAction;
John McCall8f0e8d22011-06-15 23:25:17 +0000583 ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs));
584
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000585 OwningPtr<ASTUnit> Unit(
John McCall8f0e8d22011-06-15 23:25:17 +0000586 ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags,
587 ASTAction.get()));
Jordan Rose7c304f52012-08-10 01:06:16 +0000588 if (!Unit) {
589 errRec.FinishCapture();
John McCall8f0e8d22011-06-15 23:25:17 +0000590 return true;
Jordan Rose7c304f52012-08-10 01:06:16 +0000591 }
John McCall8f0e8d22011-06-15 23:25:17 +0000592 Unit->setOwnsRemappedFileBuffers(false); // FileRemapper manages that.
593
594 // Don't filter diagnostics anymore.
595 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
596
597 ASTContext &Ctx = Unit->getASTContext();
598
599 if (Diags->hasFatalErrorOccurred()) {
600 Diags->Reset();
David Blaikie4e4d0842012-03-11 07:00:24 +0000601 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
John McCall8f0e8d22011-06-15 23:25:17 +0000602 capturedDiags.reportDiagnostics(*Diags);
603 DiagClient->EndSourceFile();
Jordan Rose7c304f52012-08-10 01:06:16 +0000604 errRec.FinishCapture();
John McCall8f0e8d22011-06-15 23:25:17 +0000605 return true;
606 }
607
608 // After parsing of source files ended, we want to reuse the
609 // diagnostics objects to emit further diagnostics.
David Blaikie78ad0b92011-09-25 23:39:51 +0000610 // We call BeginSourceFile because DiagnosticConsumer requires that
John McCall8f0e8d22011-06-15 23:25:17 +0000611 // diagnostics with source range information are emitted only in between
612 // BeginSourceFile() and EndSourceFile().
David Blaikie4e4d0842012-03-11 07:00:24 +0000613 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
John McCall8f0e8d22011-06-15 23:25:17 +0000614
David Blaikie4e4d0842012-03-11 07:00:24 +0000615 Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts());
John McCall8f0e8d22011-06-15 23:25:17 +0000616 TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
Ted Kremenekd3b74d92011-11-17 23:01:24 +0000617 MigrationPass pass(Ctx, OrigCI.getLangOpts()->getGC(),
Argyrios Kyrtzidisea2224d2013-01-04 18:30:08 +0000618 Unit->getSema(), TA, capturedDiags, ARCMTMacroLocs);
John McCall8f0e8d22011-06-15 23:25:17 +0000619
620 trans(pass);
621
622 {
623 RewritesApplicator applicator(rewriter, Ctx, listener);
624 TA.applyRewrites(applicator);
625 }
626
627 DiagClient->EndSourceFile();
Jordan Rose7c304f52012-08-10 01:06:16 +0000628 errRec.FinishCapture();
John McCall8f0e8d22011-06-15 23:25:17 +0000629
630 if (DiagClient->getNumErrors())
631 return true;
632
633 for (Rewriter::buffer_iterator
634 I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) {
635 FileID FID = I->first;
636 RewriteBuffer &buf = I->second;
637 const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID);
638 assert(file);
639 std::string newFname = file->getName();
640 newFname += "-trans";
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000641 SmallString<512> newText;
John McCall8f0e8d22011-06-15 23:25:17 +0000642 llvm::raw_svector_ostream vecOS(newText);
643 buf.write(vecOS);
644 vecOS.flush();
645 llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000646 StringRef(newText.data(), newText.size()), newFname);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000647 SmallString<64> filePath(file->getName());
John McCall8f0e8d22011-06-15 23:25:17 +0000648 Unit->getFileManager().FixupRelativePath(filePath);
649 Remapper.remap(filePath.str(), memBuf);
650 }
651
652 return false;
653}