blob: da93d8418e78606164fb8f76846fda75d71b9730 [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"
Argyrios Kyrtzidis88c0d3b2013-02-05 16:37:00 +000021#include "clang/Serialization/ASTReader.h"
John McCalld70fb982011-06-15 23:25:17 +000022#include "llvm/ADT/Triple.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramercfeacf52016-05-27 14:27:13 +000024#include <utility>
John McCalld70fb982011-06-15 23:25:17 +000025using namespace clang;
26using namespace arcmt;
John McCalld70fb982011-06-15 23:25:17 +000027
Chris Lattner54b16772011-07-23 17:14:25 +000028bool CapturedDiagList::clearDiagnostic(ArrayRef<unsigned> IDs,
John McCalld70fb982011-06-15 23:25:17 +000029 SourceRange range) {
30 if (range.isInvalid())
31 return false;
32
33 bool cleared = false;
34 ListTy::iterator I = List.begin();
35 while (I != List.end()) {
36 FullSourceLoc diagLoc = I->getLocation();
37 if ((IDs.empty() || // empty means clear all diagnostics in the range.
38 std::find(IDs.begin(), IDs.end(), I->getID()) != IDs.end()) &&
39 !diagLoc.isBeforeInTranslationUnitThan(range.getBegin()) &&
40 (diagLoc == range.getEnd() ||
41 diagLoc.isBeforeInTranslationUnitThan(range.getEnd()))) {
42 cleared = true;
43 ListTy::iterator eraseS = I++;
Argyrios Kyrtzidis03fbe3e2013-01-04 18:30:08 +000044 if (eraseS->getLevel() != DiagnosticsEngine::Note)
45 while (I != List.end() && I->getLevel() == DiagnosticsEngine::Note)
46 ++I;
John McCalld70fb982011-06-15 23:25:17 +000047 // Clear the diagnostic and any notes following it.
Joao Matos0e167f72012-08-31 17:28:09 +000048 I = List.erase(eraseS, I);
John McCalld70fb982011-06-15 23:25:17 +000049 continue;
50 }
51
52 ++I;
53 }
54
55 return cleared;
56}
57
Chris Lattner54b16772011-07-23 17:14:25 +000058bool CapturedDiagList::hasDiagnostic(ArrayRef<unsigned> IDs,
Argyrios Kyrtzidis0f3f9f72011-06-18 00:53:34 +000059 SourceRange range) const {
John McCalld70fb982011-06-15 23:25:17 +000060 if (range.isInvalid())
61 return false;
62
Argyrios Kyrtzidis0f3f9f72011-06-18 00:53:34 +000063 ListTy::const_iterator I = List.begin();
John McCalld70fb982011-06-15 23:25:17 +000064 while (I != List.end()) {
65 FullSourceLoc diagLoc = I->getLocation();
66 if ((IDs.empty() || // empty means any diagnostic in the range.
67 std::find(IDs.begin(), IDs.end(), I->getID()) != IDs.end()) &&
68 !diagLoc.isBeforeInTranslationUnitThan(range.getBegin()) &&
69 (diagLoc == range.getEnd() ||
70 diagLoc.isBeforeInTranslationUnitThan(range.getEnd()))) {
71 return true;
72 }
73
74 ++I;
75 }
76
77 return false;
78}
79
David Blaikie9c902b52011-09-25 23:23:43 +000080void CapturedDiagList::reportDiagnostics(DiagnosticsEngine &Diags) const {
Argyrios Kyrtzidis0f3f9f72011-06-18 00:53:34 +000081 for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I)
John McCalld70fb982011-06-15 23:25:17 +000082 Diags.Report(*I);
83}
84
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +000085bool CapturedDiagList::hasErrors() const {
86 for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I)
David Blaikie9c902b52011-09-25 23:23:43 +000087 if (I->getLevel() >= DiagnosticsEngine::Error)
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +000088 return true;
89
90 return false;
91}
92
John McCalld70fb982011-06-15 23:25:17 +000093namespace {
94
David Blaikiea24a0bc2011-09-25 23:54:33 +000095class CaptureDiagnosticConsumer : public DiagnosticConsumer {
David Blaikie9c902b52011-09-25 23:23:43 +000096 DiagnosticsEngine &Diags;
Jordan Roseb00073d2012-08-10 01:06:16 +000097 DiagnosticConsumer &DiagClient;
John McCalld70fb982011-06-15 23:25:17 +000098 CapturedDiagList &CapturedDiags;
Jordan Roseb00073d2012-08-10 01:06:16 +000099 bool HasBegunSourceFile;
John McCalld70fb982011-06-15 23:25:17 +0000100public:
David Blaikiea24a0bc2011-09-25 23:54:33 +0000101 CaptureDiagnosticConsumer(DiagnosticsEngine &diags,
Jordan Roseb00073d2012-08-10 01:06:16 +0000102 DiagnosticConsumer &client,
103 CapturedDiagList &capturedDiags)
104 : Diags(diags), DiagClient(client), CapturedDiags(capturedDiags),
105 HasBegunSourceFile(false) { }
106
Craig Topperb45acb82014-03-14 06:02:07 +0000107 void BeginSourceFile(const LangOptions &Opts,
108 const Preprocessor *PP) override {
Jordan Roseb00073d2012-08-10 01:06:16 +0000109 // Pass BeginSourceFile message onto DiagClient on first call.
110 // The corresponding EndSourceFile call will be made from an
111 // explicit call to FinishCapture.
112 if (!HasBegunSourceFile) {
113 DiagClient.BeginSourceFile(Opts, PP);
114 HasBegunSourceFile = true;
115 }
116 }
117
118 void FinishCapture() {
119 // Call EndSourceFile on DiagClient on completion of capture to
120 // enable VerifyDiagnosticConsumer to check diagnostics *after*
121 // it has received the diagnostic list.
122 if (HasBegunSourceFile) {
123 DiagClient.EndSourceFile();
124 HasBegunSourceFile = false;
125 }
126 }
127
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000128 ~CaptureDiagnosticConsumer() override {
Jordan Roseb00073d2012-08-10 01:06:16 +0000129 assert(!HasBegunSourceFile && "FinishCapture not called!");
130 }
John McCalld70fb982011-06-15 23:25:17 +0000131
Craig Topperb45acb82014-03-14 06:02:07 +0000132 void HandleDiagnostic(DiagnosticsEngine::Level level,
133 const Diagnostic &Info) override {
Ted Kremenek337c5b82011-10-20 05:07:47 +0000134 if (DiagnosticIDs::isARCDiagnostic(Info.getID()) ||
David Blaikie9c902b52011-09-25 23:23:43 +0000135 level >= DiagnosticsEngine::Error || level == DiagnosticsEngine::Note) {
Argyrios Kyrtzidis1f732162012-12-12 22:48:28 +0000136 if (Info.getLocation().isValid())
137 CapturedDiags.push_back(StoredDiagnostic(level, Info));
John McCalld70fb982011-06-15 23:25:17 +0000138 return;
139 }
140
141 // Non-ARC warnings are ignored.
142 Diags.setLastDiagnosticIgnored();
143 }
144};
145
146} // end anonymous namespace
147
Argyrios Kyrtzidis81a35902011-06-20 19:59:52 +0000148static bool HasARCRuntime(CompilerInvocation &origCI) {
149 // This duplicates some functionality from Darwin::AddDeploymentTarget
150 // but this function is well defined, so keep it decoupled from the driver
151 // and avoid unrelated complications.
Argyrios Kyrtzidis81a35902011-06-20 19:59:52 +0000152 llvm::Triple triple(origCI.getTargetOpts().Triple);
153
Cameron Esfahani556d91e2013-09-14 01:09:11 +0000154 if (triple.isiOS())
Argyrios Kyrtzidis81a35902011-06-20 19:59:52 +0000155 return triple.getOSMajorVersion() >= 5;
156
Tim Northover756447a2015-10-30 16:30:36 +0000157 if (triple.isWatchOS())
158 return true;
159
Argyrios Kyrtzidis81a35902011-06-20 19:59:52 +0000160 if (triple.getOS() == llvm::Triple::Darwin)
161 return triple.getOSMajorVersion() >= 11;
162
163 if (triple.getOS() == llvm::Triple::MacOSX) {
164 unsigned Major, Minor, Micro;
165 triple.getOSVersion(Major, Minor, Micro);
166 return Major > 10 || (Major == 10 && Minor >= 7);
167 }
168
169 return false;
170}
171
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000172static CompilerInvocation *
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000173createInvocationForMigration(CompilerInvocation &origCI,
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000174 const PCHContainerReader &PCHContainerRdr) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000175 std::unique_ptr<CompilerInvocation> CInvok;
John McCalld70fb982011-06-15 23:25:17 +0000176 CInvok.reset(new CompilerInvocation(origCI));
Argyrios Kyrtzidis88c0d3b2013-02-05 16:37:00 +0000177 PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts();
178 if (!PPOpts.ImplicitPCHInclude.empty()) {
179 // We can't use a PCH because it was likely built in non-ARC mode and we
180 // want to parse in ARC. Include the original header.
181 FileManager FileMgr(origCI.getFileSystemOpts());
182 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
183 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
184 new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
185 new IgnoringDiagConsumer()));
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000186 std::string OriginalFile = ASTReader::getOriginalSourceFile(
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000187 PPOpts.ImplicitPCHInclude, FileMgr, PCHContainerRdr, *Diags);
Argyrios Kyrtzidis88c0d3b2013-02-05 16:37:00 +0000188 if (!OriginalFile.empty())
189 PPOpts.Includes.insert(PPOpts.Includes.begin(), OriginalFile);
190 PPOpts.ImplicitPCHInclude.clear();
191 }
192 // FIXME: Get the original header of a PTH as well.
193 CInvok->getPreprocessorOpts().ImplicitPTHInclude.clear();
John McCalld70fb982011-06-15 23:25:17 +0000194 std::string define = getARCMTMacroName();
195 define += '=';
196 CInvok->getPreprocessorOpts().addMacroDef(define);
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000197 CInvok->getLangOpts()->ObjCAutoRefCount = true;
198 CInvok->getLangOpts()->setGC(LangOptions::NonGC);
John McCalld70fb982011-06-15 23:25:17 +0000199 CInvok->getDiagnosticOpts().ErrorLimit = 0;
Argyrios Kyrtzidisaa38f692012-06-20 01:46:26 +0000200 CInvok->getDiagnosticOpts().PedanticErrors = 0;
Argyrios Kyrtzidis692bf8c2012-06-20 01:10:40 +0000201
202 // Ignore -Werror flags when migrating.
203 std::vector<std::string> WarnOpts;
204 for (std::vector<std::string>::iterator
205 I = CInvok->getDiagnosticOpts().Warnings.begin(),
206 E = CInvok->getDiagnosticOpts().Warnings.end(); I != E; ++I) {
207 if (!StringRef(*I).startswith("error"))
208 WarnOpts.push_back(*I);
209 }
210 WarnOpts.push_back("error=arc-unsafe-retained-assign");
Chandler Carruthc72d9b32014-03-02 04:02:40 +0000211 CInvok->getDiagnosticOpts().Warnings = std::move(WarnOpts);
Argyrios Kyrtzidis692bf8c2012-06-20 01:10:40 +0000212
John McCall460ce582015-10-22 18:38:17 +0000213 CInvok->getLangOpts()->ObjCWeakRuntime = HasARCRuntime(origCI);
214 CInvok->getLangOpts()->ObjCWeak = CInvok->getLangOpts()->ObjCWeakRuntime;
John McCalld70fb982011-06-15 23:25:17 +0000215
Ahmed Charles9a16beb2014-03-07 19:33:25 +0000216 return CInvok.release();
John McCalld70fb982011-06-15 23:25:17 +0000217}
218
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000219static void emitPremigrationErrors(const CapturedDiagList &arcDiags,
Douglas Gregor811db4e2012-10-23 22:26:28 +0000220 DiagnosticOptions *diagOpts,
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000221 Preprocessor &PP) {
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000222 TextDiagnosticPrinter printer(llvm::errs(), diagOpts);
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000223 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
224 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000225 new DiagnosticsEngine(DiagID, diagOpts, &printer,
226 /*ShouldOwnClient=*/false));
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000227 Diags->setSourceManager(&PP.getSourceManager());
228
David Blaikiebbafb8a2012-03-11 07:00:24 +0000229 printer.BeginSourceFile(PP.getLangOpts(), &PP);
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000230 arcDiags.reportDiagnostics(*Diags);
231 printer.EndSourceFile();
232}
233
John McCalld70fb982011-06-15 23:25:17 +0000234//===----------------------------------------------------------------------===//
235// checkForManualIssues.
236//===----------------------------------------------------------------------===//
237
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000238bool arcmt::checkForManualIssues(
239 CompilerInvocation &origCI, const FrontendInputFile &Input,
240 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
241 DiagnosticConsumer *DiagClient, bool emitPremigrationARCErrors,
242 StringRef plistOut) {
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000243 if (!origCI.getLangOpts()->ObjC1)
John McCalld70fb982011-06-15 23:25:17 +0000244 return false;
245
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000246 LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC();
Fariborz Jahanianaa7b9aa2012-01-25 00:20:29 +0000247 bool NoNSAllocReallocError = origCI.getMigratorOpts().NoNSAllocReallocError;
Fariborz Jahanian0c859d62012-01-26 00:08:04 +0000248 bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval;
Argyrios Kyrtzidisd208ef92011-11-04 15:58:08 +0000249
Fariborz Jahanian48fd81b2012-01-26 20:57:58 +0000250 std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode,
251 NoFinalizeRemoval);
John McCalld70fb982011-06-15 23:25:17 +0000252 assert(!transforms.empty());
253
Ahmed Charlesb8984322014-03-07 20:03:18 +0000254 std::unique_ptr<CompilerInvocation> CInvok;
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000255 CInvok.reset(
256 createInvocationForMigration(origCI, PCHContainerOps->getRawReader()));
John McCalld70fb982011-06-15 23:25:17 +0000257 CInvok->getFrontendOpts().Inputs.clear();
Douglas Gregor32fbe312012-01-20 16:28:04 +0000258 CInvok->getFrontendOpts().Inputs.push_back(Input);
John McCalld70fb982011-06-15 23:25:17 +0000259
260 CapturedDiagList capturedDiags;
261
262 assert(DiagClient);
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000263 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
264 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000265 new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
266 DiagClient, /*ShouldOwnClient=*/false));
John McCalld70fb982011-06-15 23:25:17 +0000267
268 // Filter of all diagnostics.
Jordan Roseb00073d2012-08-10 01:06:16 +0000269 CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags);
John McCalld70fb982011-06-15 23:25:17 +0000270 Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
271
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000272 std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCompilerInvocationAction(
273 CInvok.release(), PCHContainerOps, Diags));
Jordan Roseb00073d2012-08-10 01:06:16 +0000274 if (!Unit) {
275 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000276 return true;
Jordan Roseb00073d2012-08-10 01:06:16 +0000277 }
John McCalld70fb982011-06-15 23:25:17 +0000278
279 // Don't filter diagnostics anymore.
280 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
281
282 ASTContext &Ctx = Unit->getASTContext();
283
284 if (Diags->hasFatalErrorOccurred()) {
285 Diags->Reset();
David Blaikiebbafb8a2012-03-11 07:00:24 +0000286 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
John McCalld70fb982011-06-15 23:25:17 +0000287 capturedDiags.reportDiagnostics(*Diags);
288 DiagClient->EndSourceFile();
Jordan Roseb00073d2012-08-10 01:06:16 +0000289 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000290 return true;
291 }
292
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000293 if (emitPremigrationARCErrors)
Douglas Gregor811db4e2012-10-23 22:26:28 +0000294 emitPremigrationErrors(capturedDiags, &origCI.getDiagnosticOpts(),
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000295 Unit->getPreprocessor());
296 if (!plistOut.empty()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000297 SmallVector<StoredDiagnostic, 8> arcDiags;
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000298 for (CapturedDiagList::iterator
299 I = capturedDiags.begin(), E = capturedDiags.end(); I != E; ++I)
300 arcDiags.push_back(*I);
301 writeARCDiagsToPlist(plistOut, arcDiags,
David Blaikiebbafb8a2012-03-11 07:00:24 +0000302 Ctx.getSourceManager(), Ctx.getLangOpts());
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000303 }
304
John McCalld70fb982011-06-15 23:25:17 +0000305 // After parsing of source files ended, we want to reuse the
306 // diagnostics objects to emit further diagnostics.
David Blaikiee2eefae2011-09-25 23:39:51 +0000307 // We call BeginSourceFile because DiagnosticConsumer requires that
John McCalld70fb982011-06-15 23:25:17 +0000308 // diagnostics with source range information are emitted only in between
309 // BeginSourceFile() and EndSourceFile().
David Blaikiebbafb8a2012-03-11 07:00:24 +0000310 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
John McCalld70fb982011-06-15 23:25:17 +0000311
312 // No macros will be added since we are just checking and we won't modify
313 // source code.
314 std::vector<SourceLocation> ARCMTMacroLocs;
315
316 TransformActions testAct(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
Argyrios Kyrtzidis03fbe3e2013-01-04 18:30:08 +0000317 MigrationPass pass(Ctx, OrigGCMode, Unit->getSema(), testAct, capturedDiags,
318 ARCMTMacroLocs);
Fariborz Jahanian0c859d62012-01-26 00:08:04 +0000319 pass.setNoFinalizeRemoval(NoFinalizeRemoval);
Alp Toker57cccec2014-05-19 23:48:49 +0000320 if (!NoNSAllocReallocError)
Alp Tokerd576e002014-06-12 11:13:52 +0000321 Diags->setSeverity(diag::warn_arcmt_nsalloc_realloc, diag::Severity::Error,
322 SourceLocation());
John McCalld70fb982011-06-15 23:25:17 +0000323
324 for (unsigned i=0, e = transforms.size(); i != e; ++i)
325 transforms[i](pass);
326
327 capturedDiags.reportDiagnostics(*Diags);
328
329 DiagClient->EndSourceFile();
Jordan Roseb00073d2012-08-10 01:06:16 +0000330 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000331
Argyrios Kyrtzidis73a0d322011-07-18 07:44:45 +0000332 return capturedDiags.hasErrors() || testAct.hasReportedErrors();
John McCalld70fb982011-06-15 23:25:17 +0000333}
334
335//===----------------------------------------------------------------------===//
336// applyTransformations.
337//===----------------------------------------------------------------------===//
338
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000339static bool
340applyTransforms(CompilerInvocation &origCI, const FrontendInputFile &Input,
341 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
342 DiagnosticConsumer *DiagClient, StringRef outputDir,
343 bool emitPremigrationARCErrors, StringRef plistOut) {
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000344 if (!origCI.getLangOpts()->ObjC1)
John McCalld70fb982011-06-15 23:25:17 +0000345 return false;
346
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000347 LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC();
Argyrios Kyrtzidisd208ef92011-11-04 15:58:08 +0000348
John McCalld70fb982011-06-15 23:25:17 +0000349 // Make sure checking is successful first.
350 CompilerInvocation CInvokForCheck(origCI);
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000351 if (arcmt::checkForManualIssues(CInvokForCheck, Input, PCHContainerOps,
352 DiagClient, emitPremigrationARCErrors,
353 plistOut))
John McCalld70fb982011-06-15 23:25:17 +0000354 return true;
355
356 CompilerInvocation CInvok(origCI);
357 CInvok.getFrontendOpts().Inputs.clear();
Douglas Gregor32fbe312012-01-20 16:28:04 +0000358 CInvok.getFrontendOpts().Inputs.push_back(Input);
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000359
360 MigrationProcess migration(CInvok, PCHContainerOps, DiagClient, outputDir);
Fariborz Jahanian48fd81b2012-01-26 20:57:58 +0000361 bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval;
John McCalld70fb982011-06-15 23:25:17 +0000362
Fariborz Jahanian48fd81b2012-01-26 20:57:58 +0000363 std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode,
364 NoFinalizeRemoval);
John McCalld70fb982011-06-15 23:25:17 +0000365 assert(!transforms.empty());
366
367 for (unsigned i=0, e = transforms.size(); i != e; ++i) {
368 bool err = migration.applyTransform(transforms[i]);
369 if (err) return true;
370 }
371
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000372 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
373 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000374 new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
375 DiagClient, /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000376
377 if (outputDir.empty()) {
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000378 origCI.getLangOpts()->ObjCAutoRefCount = true;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000379 return migration.getRemapper().overwriteOriginal(*Diags);
Argyrios Kyrtzidise2e40b42011-07-14 00:17:54 +0000380 } else {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000381 return migration.getRemapper().flushToDisk(outputDir, *Diags);
Argyrios Kyrtzidise2e40b42011-07-14 00:17:54 +0000382 }
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000383}
384
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000385bool arcmt::applyTransformations(
386 CompilerInvocation &origCI, const FrontendInputFile &Input,
387 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
388 DiagnosticConsumer *DiagClient) {
389 return applyTransforms(origCI, Input, PCHContainerOps, DiagClient,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000390 StringRef(), false, StringRef());
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000391}
392
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000393bool arcmt::migrateWithTemporaryFiles(
394 CompilerInvocation &origCI, const FrontendInputFile &Input,
395 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
396 DiagnosticConsumer *DiagClient, StringRef outputDir,
397 bool emitPremigrationARCErrors, StringRef plistOut) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000398 assert(!outputDir.empty() && "Expected output directory path");
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000399 return applyTransforms(origCI, Input, PCHContainerOps, DiagClient, outputDir,
400 emitPremigrationARCErrors, plistOut);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000401}
402
403bool arcmt::getFileRemappings(std::vector<std::pair<std::string,std::string> > &
404 remap,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000405 StringRef outputDir,
David Blaikiee2eefae2011-09-25 23:39:51 +0000406 DiagnosticConsumer *DiagClient) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000407 assert(!outputDir.empty());
John McCalld70fb982011-06-15 23:25:17 +0000408
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000409 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
410 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000411 new DiagnosticsEngine(DiagID, new DiagnosticOptions,
412 DiagClient, /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000413
414 FileRemapper remapper;
415 bool err = remapper.initFromDisk(outputDir, *Diags,
416 /*ignoreIfFilesChanged=*/true);
417 if (err)
418 return true;
419
Ted Kremenekf7639e12012-03-06 20:06:33 +0000420 PreprocessorOptions PPOpts;
421 remapper.applyMappings(PPOpts);
422 remap = PPOpts.RemappedFiles;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000423
424 return false;
John McCalld70fb982011-06-15 23:25:17 +0000425}
426
Ted Kremenekf7639e12012-03-06 20:06:33 +0000427
John McCalld70fb982011-06-15 23:25:17 +0000428//===----------------------------------------------------------------------===//
John McCalld70fb982011-06-15 23:25:17 +0000429// CollectTransformActions.
430//===----------------------------------------------------------------------===//
431
432namespace {
433
434class ARCMTMacroTrackerPPCallbacks : public PPCallbacks {
435 std::vector<SourceLocation> &ARCMTMacroLocs;
436
437public:
438 ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs)
439 : ARCMTMacroLocs(ARCMTMacroLocs) { }
440
Richard Smith36bd40d2015-05-04 03:15:40 +0000441 void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
Craig Topperb45acb82014-03-14 06:02:07 +0000442 SourceRange Range, const MacroArgs *Args) override {
John McCalld70fb982011-06-15 23:25:17 +0000443 if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName())
444 ARCMTMacroLocs.push_back(MacroNameTok.getLocation());
445 }
446};
447
448class ARCMTMacroTrackerAction : public ASTFrontendAction {
449 std::vector<SourceLocation> &ARCMTMacroLocs;
450
451public:
452 ARCMTMacroTrackerAction(std::vector<SourceLocation> &ARCMTMacroLocs)
453 : ARCMTMacroLocs(ARCMTMacroLocs) { }
454
David Blaikie6beb6aa2014-08-10 19:56:51 +0000455 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
456 StringRef InFile) override {
John McCalld70fb982011-06-15 23:25:17 +0000457 CI.getPreprocessor().addPPCallbacks(
Craig Topperb8a70532014-09-10 04:53:53 +0000458 llvm::make_unique<ARCMTMacroTrackerPPCallbacks>(ARCMTMacroLocs));
David Blaikie6beb6aa2014-08-10 19:56:51 +0000459 return llvm::make_unique<ASTConsumer>();
John McCalld70fb982011-06-15 23:25:17 +0000460 }
461};
462
463class RewritesApplicator : public TransformActions::RewriteReceiver {
464 Rewriter &rewriter;
John McCalld70fb982011-06-15 23:25:17 +0000465 MigrationProcess::RewriteListener *Listener;
466
467public:
468 RewritesApplicator(Rewriter &rewriter, ASTContext &ctx,
469 MigrationProcess::RewriteListener *listener)
Benjamin Kramerd1d76b22012-06-06 17:32:50 +0000470 : rewriter(rewriter), Listener(listener) {
John McCalld70fb982011-06-15 23:25:17 +0000471 if (Listener)
472 Listener->start(ctx);
473 }
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000474 ~RewritesApplicator() override {
John McCalld70fb982011-06-15 23:25:17 +0000475 if (Listener)
476 Listener->finish();
477 }
478
Craig Topperb45acb82014-03-14 06:02:07 +0000479 void insert(SourceLocation loc, StringRef text) override {
John McCalld70fb982011-06-15 23:25:17 +0000480 bool err = rewriter.InsertText(loc, text, /*InsertAfter=*/true,
481 /*indentNewLines=*/true);
482 if (!err && Listener)
483 Listener->insert(loc, text);
484 }
485
Craig Topperb45acb82014-03-14 06:02:07 +0000486 void remove(CharSourceRange range) override {
John McCalld70fb982011-06-15 23:25:17 +0000487 Rewriter::RewriteOptions removeOpts;
488 removeOpts.IncludeInsertsAtBeginOfRange = false;
489 removeOpts.IncludeInsertsAtEndOfRange = false;
490 removeOpts.RemoveLineIfEmpty = true;
491
492 bool err = rewriter.RemoveText(range, removeOpts);
493 if (!err && Listener)
494 Listener->remove(range);
495 }
496
Craig Topperb45acb82014-03-14 06:02:07 +0000497 void increaseIndentation(CharSourceRange range,
498 SourceLocation parentIndent) override {
John McCalld70fb982011-06-15 23:25:17 +0000499 rewriter.IncreaseIndentation(range, parentIndent);
500 }
501};
502
503} // end anonymous namespace.
504
505/// \brief Anchor for VTable.
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000506MigrationProcess::RewriteListener::~RewriteListener() { }
John McCalld70fb982011-06-15 23:25:17 +0000507
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000508MigrationProcess::MigrationProcess(
509 const CompilerInvocation &CI,
510 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
511 DiagnosticConsumer *diagClient, StringRef outputDir)
Benjamin Kramercfeacf52016-05-27 14:27:13 +0000512 : OrigCI(CI), PCHContainerOps(std::move(PCHContainerOps)),
513 DiagClient(diagClient), HadARCErrors(false) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000514 if (!outputDir.empty()) {
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000515 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
516 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000517 new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(),
518 DiagClient, /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000519 Remapper.initFromDisk(outputDir, *Diags, /*ignoreIfFilesChanges=*/true);
520 }
521}
522
John McCalld70fb982011-06-15 23:25:17 +0000523bool MigrationProcess::applyTransform(TransformFn trans,
524 RewriteListener *listener) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000525 std::unique_ptr<CompilerInvocation> CInvok;
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000526 CInvok.reset(
527 createInvocationForMigration(OrigCI, PCHContainerOps->getRawReader()));
John McCalld70fb982011-06-15 23:25:17 +0000528 CInvok->getDiagnosticOpts().IgnoreWarnings = true;
529
Ted Kremenekf7639e12012-03-06 20:06:33 +0000530 Remapper.applyMappings(CInvok->getPreprocessorOpts());
John McCalld70fb982011-06-15 23:25:17 +0000531
532 CapturedDiagList capturedDiags;
533 std::vector<SourceLocation> ARCMTMacroLocs;
534
535 assert(DiagClient);
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000536 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
537 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000538 new DiagnosticsEngine(DiagID, new DiagnosticOptions,
539 DiagClient, /*ShouldOwnClient=*/false));
John McCalld70fb982011-06-15 23:25:17 +0000540
541 // Filter of all diagnostics.
Jordan Roseb00073d2012-08-10 01:06:16 +0000542 CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags);
John McCalld70fb982011-06-15 23:25:17 +0000543 Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
544
Ahmed Charlesb8984322014-03-07 20:03:18 +0000545 std::unique_ptr<ARCMTMacroTrackerAction> ASTAction;
John McCalld70fb982011-06-15 23:25:17 +0000546 ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs));
547
Ahmed Charlesaf94d562014-03-09 11:34:25 +0000548 std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCompilerInvocationAction(
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000549 CInvok.release(), PCHContainerOps, Diags, ASTAction.get()));
Jordan Roseb00073d2012-08-10 01:06:16 +0000550 if (!Unit) {
551 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000552 return true;
Jordan Roseb00073d2012-08-10 01:06:16 +0000553 }
John McCalld70fb982011-06-15 23:25:17 +0000554 Unit->setOwnsRemappedFileBuffers(false); // FileRemapper manages that.
555
Argyrios Kyrtzidisec852d92013-07-22 18:13:54 +0000556 HadARCErrors = HadARCErrors || capturedDiags.hasErrors();
557
John McCalld70fb982011-06-15 23:25:17 +0000558 // Don't filter diagnostics anymore.
559 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
560
561 ASTContext &Ctx = Unit->getASTContext();
562
563 if (Diags->hasFatalErrorOccurred()) {
564 Diags->Reset();
David Blaikiebbafb8a2012-03-11 07:00:24 +0000565 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
John McCalld70fb982011-06-15 23:25:17 +0000566 capturedDiags.reportDiagnostics(*Diags);
567 DiagClient->EndSourceFile();
Jordan Roseb00073d2012-08-10 01:06:16 +0000568 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000569 return true;
570 }
571
572 // After parsing of source files ended, we want to reuse the
573 // diagnostics objects to emit further diagnostics.
David Blaikiee2eefae2011-09-25 23:39:51 +0000574 // We call BeginSourceFile because DiagnosticConsumer requires that
John McCalld70fb982011-06-15 23:25:17 +0000575 // diagnostics with source range information are emitted only in between
576 // BeginSourceFile() and EndSourceFile().
David Blaikiebbafb8a2012-03-11 07:00:24 +0000577 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
John McCalld70fb982011-06-15 23:25:17 +0000578
David Blaikiebbafb8a2012-03-11 07:00:24 +0000579 Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts());
John McCalld70fb982011-06-15 23:25:17 +0000580 TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000581 MigrationPass pass(Ctx, OrigCI.getLangOpts()->getGC(),
Argyrios Kyrtzidis03fbe3e2013-01-04 18:30:08 +0000582 Unit->getSema(), TA, capturedDiags, ARCMTMacroLocs);
John McCalld70fb982011-06-15 23:25:17 +0000583
584 trans(pass);
585
586 {
587 RewritesApplicator applicator(rewriter, Ctx, listener);
588 TA.applyRewrites(applicator);
589 }
590
591 DiagClient->EndSourceFile();
Jordan Roseb00073d2012-08-10 01:06:16 +0000592 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000593
594 if (DiagClient->getNumErrors())
595 return true;
596
597 for (Rewriter::buffer_iterator
598 I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) {
599 FileID FID = I->first;
600 RewriteBuffer &buf = I->second;
601 const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID);
602 assert(file);
603 std::string newFname = file->getName();
604 newFname += "-trans";
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000605 SmallString<512> newText;
John McCalld70fb982011-06-15 23:25:17 +0000606 llvm::raw_svector_ostream vecOS(newText);
607 buf.write(vecOS);
Rafael Espindola1a1b1562014-08-17 23:12:27 +0000608 std::unique_ptr<llvm::MemoryBuffer> memBuf(
609 llvm::MemoryBuffer::getMemBufferCopy(
610 StringRef(newText.data(), newText.size()), newFname));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000611 SmallString<64> filePath(file->getName());
John McCalld70fb982011-06-15 23:25:17 +0000612 Unit->getFileManager().FixupRelativePath(filePath);
Rafael Espindola1a1b1562014-08-17 23:12:27 +0000613 Remapper.remap(filePath.str(), std::move(memBuf));
John McCalld70fb982011-06-15 23:25:17 +0000614 }
615
616 return false;
617}