blob: 680aa3e48da401ef3fab94625a69698cd647d4a9 [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"
Mehdi Amini9670f842016-07-18 19:02:11 +000019#include "clang/Lex/PreprocessorOptions.h"
Ted Kremenekcdf81492012-09-01 05:09:24 +000020#include "clang/Rewrite/Core/Rewriter.h"
John McCalld70fb982011-06-15 23:25:17 +000021#include "clang/Sema/SemaDiagnostic.h"
Argyrios Kyrtzidis88c0d3b2013-02-05 16:37:00 +000022#include "clang/Serialization/ASTReader.h"
John McCalld70fb982011-06-15 23:25:17 +000023#include "llvm/ADT/Triple.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramercfeacf52016-05-27 14:27:13 +000025#include <utility>
John McCalld70fb982011-06-15 23:25:17 +000026using namespace clang;
27using namespace arcmt;
John McCalld70fb982011-06-15 23:25:17 +000028
Chris Lattner54b16772011-07-23 17:14:25 +000029bool CapturedDiagList::clearDiagnostic(ArrayRef<unsigned> IDs,
John McCalld70fb982011-06-15 23:25:17 +000030 SourceRange range) {
31 if (range.isInvalid())
32 return false;
33
34 bool cleared = false;
35 ListTy::iterator I = List.begin();
36 while (I != List.end()) {
37 FullSourceLoc diagLoc = I->getLocation();
38 if ((IDs.empty() || // empty means clear all diagnostics in the range.
39 std::find(IDs.begin(), IDs.end(), I->getID()) != IDs.end()) &&
40 !diagLoc.isBeforeInTranslationUnitThan(range.getBegin()) &&
41 (diagLoc == range.getEnd() ||
42 diagLoc.isBeforeInTranslationUnitThan(range.getEnd()))) {
43 cleared = true;
44 ListTy::iterator eraseS = I++;
Argyrios Kyrtzidis03fbe3e2013-01-04 18:30:08 +000045 if (eraseS->getLevel() != DiagnosticsEngine::Note)
46 while (I != List.end() && I->getLevel() == DiagnosticsEngine::Note)
47 ++I;
John McCalld70fb982011-06-15 23:25:17 +000048 // Clear the diagnostic and any notes following it.
Joao Matos0e167f72012-08-31 17:28:09 +000049 I = List.erase(eraseS, I);
John McCalld70fb982011-06-15 23:25:17 +000050 continue;
51 }
52
53 ++I;
54 }
55
56 return cleared;
57}
58
Chris Lattner54b16772011-07-23 17:14:25 +000059bool CapturedDiagList::hasDiagnostic(ArrayRef<unsigned> IDs,
Argyrios Kyrtzidis0f3f9f72011-06-18 00:53:34 +000060 SourceRange range) const {
John McCalld70fb982011-06-15 23:25:17 +000061 if (range.isInvalid())
62 return false;
63
Argyrios Kyrtzidis0f3f9f72011-06-18 00:53:34 +000064 ListTy::const_iterator I = List.begin();
John McCalld70fb982011-06-15 23:25:17 +000065 while (I != List.end()) {
66 FullSourceLoc diagLoc = I->getLocation();
67 if ((IDs.empty() || // empty means any diagnostic in the range.
68 std::find(IDs.begin(), IDs.end(), I->getID()) != IDs.end()) &&
69 !diagLoc.isBeforeInTranslationUnitThan(range.getBegin()) &&
70 (diagLoc == range.getEnd() ||
71 diagLoc.isBeforeInTranslationUnitThan(range.getEnd()))) {
72 return true;
73 }
74
75 ++I;
76 }
77
78 return false;
79}
80
David Blaikie9c902b52011-09-25 23:23:43 +000081void CapturedDiagList::reportDiagnostics(DiagnosticsEngine &Diags) const {
Argyrios Kyrtzidis0f3f9f72011-06-18 00:53:34 +000082 for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I)
John McCalld70fb982011-06-15 23:25:17 +000083 Diags.Report(*I);
84}
85
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +000086bool CapturedDiagList::hasErrors() const {
87 for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I)
David Blaikie9c902b52011-09-25 23:23:43 +000088 if (I->getLevel() >= DiagnosticsEngine::Error)
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +000089 return true;
90
91 return false;
92}
93
John McCalld70fb982011-06-15 23:25:17 +000094namespace {
95
David Blaikiea24a0bc2011-09-25 23:54:33 +000096class CaptureDiagnosticConsumer : public DiagnosticConsumer {
David Blaikie9c902b52011-09-25 23:23:43 +000097 DiagnosticsEngine &Diags;
Jordan Roseb00073d2012-08-10 01:06:16 +000098 DiagnosticConsumer &DiagClient;
John McCalld70fb982011-06-15 23:25:17 +000099 CapturedDiagList &CapturedDiags;
Jordan Roseb00073d2012-08-10 01:06:16 +0000100 bool HasBegunSourceFile;
John McCalld70fb982011-06-15 23:25:17 +0000101public:
David Blaikiea24a0bc2011-09-25 23:54:33 +0000102 CaptureDiagnosticConsumer(DiagnosticsEngine &diags,
Jordan Roseb00073d2012-08-10 01:06:16 +0000103 DiagnosticConsumer &client,
104 CapturedDiagList &capturedDiags)
105 : Diags(diags), DiagClient(client), CapturedDiags(capturedDiags),
106 HasBegunSourceFile(false) { }
107
Craig Topperb45acb82014-03-14 06:02:07 +0000108 void BeginSourceFile(const LangOptions &Opts,
109 const Preprocessor *PP) override {
Jordan Roseb00073d2012-08-10 01:06:16 +0000110 // Pass BeginSourceFile message onto DiagClient on first call.
111 // The corresponding EndSourceFile call will be made from an
112 // explicit call to FinishCapture.
113 if (!HasBegunSourceFile) {
114 DiagClient.BeginSourceFile(Opts, PP);
115 HasBegunSourceFile = true;
116 }
117 }
118
119 void FinishCapture() {
120 // Call EndSourceFile on DiagClient on completion of capture to
121 // enable VerifyDiagnosticConsumer to check diagnostics *after*
122 // it has received the diagnostic list.
123 if (HasBegunSourceFile) {
124 DiagClient.EndSourceFile();
125 HasBegunSourceFile = false;
126 }
127 }
128
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000129 ~CaptureDiagnosticConsumer() override {
Jordan Roseb00073d2012-08-10 01:06:16 +0000130 assert(!HasBegunSourceFile && "FinishCapture not called!");
131 }
John McCalld70fb982011-06-15 23:25:17 +0000132
Craig Topperb45acb82014-03-14 06:02:07 +0000133 void HandleDiagnostic(DiagnosticsEngine::Level level,
134 const Diagnostic &Info) override {
Ted Kremenek337c5b82011-10-20 05:07:47 +0000135 if (DiagnosticIDs::isARCDiagnostic(Info.getID()) ||
David Blaikie9c902b52011-09-25 23:23:43 +0000136 level >= DiagnosticsEngine::Error || level == DiagnosticsEngine::Note) {
Argyrios Kyrtzidis1f732162012-12-12 22:48:28 +0000137 if (Info.getLocation().isValid())
138 CapturedDiags.push_back(StoredDiagnostic(level, Info));
John McCalld70fb982011-06-15 23:25:17 +0000139 return;
140 }
141
142 // Non-ARC warnings are ignored.
143 Diags.setLastDiagnosticIgnored();
144 }
145};
146
147} // end anonymous namespace
148
Argyrios Kyrtzidis81a35902011-06-20 19:59:52 +0000149static bool HasARCRuntime(CompilerInvocation &origCI) {
150 // This duplicates some functionality from Darwin::AddDeploymentTarget
151 // but this function is well defined, so keep it decoupled from the driver
152 // and avoid unrelated complications.
Argyrios Kyrtzidis81a35902011-06-20 19:59:52 +0000153 llvm::Triple triple(origCI.getTargetOpts().Triple);
154
Cameron Esfahani556d91e2013-09-14 01:09:11 +0000155 if (triple.isiOS())
Argyrios Kyrtzidis81a35902011-06-20 19:59:52 +0000156 return triple.getOSMajorVersion() >= 5;
157
Tim Northover756447a2015-10-30 16:30:36 +0000158 if (triple.isWatchOS())
159 return true;
160
Argyrios Kyrtzidis81a35902011-06-20 19:59:52 +0000161 if (triple.getOS() == llvm::Triple::Darwin)
162 return triple.getOSMajorVersion() >= 11;
163
164 if (triple.getOS() == llvm::Triple::MacOSX) {
165 unsigned Major, Minor, Micro;
166 triple.getOSVersion(Major, Minor, Micro);
167 return Major > 10 || (Major == 10 && Minor >= 7);
168 }
169
170 return false;
171}
172
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000173static CompilerInvocation *
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000174createInvocationForMigration(CompilerInvocation &origCI,
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000175 const PCHContainerReader &PCHContainerRdr) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000176 std::unique_ptr<CompilerInvocation> CInvok;
John McCalld70fb982011-06-15 23:25:17 +0000177 CInvok.reset(new CompilerInvocation(origCI));
Argyrios Kyrtzidis88c0d3b2013-02-05 16:37:00 +0000178 PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts();
179 if (!PPOpts.ImplicitPCHInclude.empty()) {
180 // We can't use a PCH because it was likely built in non-ARC mode and we
181 // want to parse in ARC. Include the original header.
182 FileManager FileMgr(origCI.getFileSystemOpts());
183 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
184 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
185 new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
186 new IgnoringDiagConsumer()));
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000187 std::string OriginalFile = ASTReader::getOriginalSourceFile(
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000188 PPOpts.ImplicitPCHInclude, FileMgr, PCHContainerRdr, *Diags);
Argyrios Kyrtzidis88c0d3b2013-02-05 16:37:00 +0000189 if (!OriginalFile.empty())
190 PPOpts.Includes.insert(PPOpts.Includes.begin(), OriginalFile);
191 PPOpts.ImplicitPCHInclude.clear();
192 }
193 // FIXME: Get the original header of a PTH as well.
194 CInvok->getPreprocessorOpts().ImplicitPTHInclude.clear();
John McCalld70fb982011-06-15 23:25:17 +0000195 std::string define = getARCMTMacroName();
196 define += '=';
197 CInvok->getPreprocessorOpts().addMacroDef(define);
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000198 CInvok->getLangOpts()->ObjCAutoRefCount = true;
199 CInvok->getLangOpts()->setGC(LangOptions::NonGC);
John McCalld70fb982011-06-15 23:25:17 +0000200 CInvok->getDiagnosticOpts().ErrorLimit = 0;
Argyrios Kyrtzidisaa38f692012-06-20 01:46:26 +0000201 CInvok->getDiagnosticOpts().PedanticErrors = 0;
Argyrios Kyrtzidis692bf8c2012-06-20 01:10:40 +0000202
203 // Ignore -Werror flags when migrating.
204 std::vector<std::string> WarnOpts;
205 for (std::vector<std::string>::iterator
206 I = CInvok->getDiagnosticOpts().Warnings.begin(),
207 E = CInvok->getDiagnosticOpts().Warnings.end(); I != E; ++I) {
208 if (!StringRef(*I).startswith("error"))
209 WarnOpts.push_back(*I);
210 }
211 WarnOpts.push_back("error=arc-unsafe-retained-assign");
Chandler Carruthc72d9b32014-03-02 04:02:40 +0000212 CInvok->getDiagnosticOpts().Warnings = std::move(WarnOpts);
Argyrios Kyrtzidis692bf8c2012-06-20 01:10:40 +0000213
John McCall460ce582015-10-22 18:38:17 +0000214 CInvok->getLangOpts()->ObjCWeakRuntime = HasARCRuntime(origCI);
215 CInvok->getLangOpts()->ObjCWeak = CInvok->getLangOpts()->ObjCWeakRuntime;
John McCalld70fb982011-06-15 23:25:17 +0000216
Ahmed Charles9a16beb2014-03-07 19:33:25 +0000217 return CInvok.release();
John McCalld70fb982011-06-15 23:25:17 +0000218}
219
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000220static void emitPremigrationErrors(const CapturedDiagList &arcDiags,
Douglas Gregor811db4e2012-10-23 22:26:28 +0000221 DiagnosticOptions *diagOpts,
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000222 Preprocessor &PP) {
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000223 TextDiagnosticPrinter printer(llvm::errs(), diagOpts);
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000224 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
225 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000226 new DiagnosticsEngine(DiagID, diagOpts, &printer,
227 /*ShouldOwnClient=*/false));
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000228 Diags->setSourceManager(&PP.getSourceManager());
229
David Blaikiebbafb8a2012-03-11 07:00:24 +0000230 printer.BeginSourceFile(PP.getLangOpts(), &PP);
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000231 arcDiags.reportDiagnostics(*Diags);
232 printer.EndSourceFile();
233}
234
John McCalld70fb982011-06-15 23:25:17 +0000235//===----------------------------------------------------------------------===//
236// checkForManualIssues.
237//===----------------------------------------------------------------------===//
238
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000239bool arcmt::checkForManualIssues(
240 CompilerInvocation &origCI, const FrontendInputFile &Input,
241 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
242 DiagnosticConsumer *DiagClient, bool emitPremigrationARCErrors,
243 StringRef plistOut) {
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000244 if (!origCI.getLangOpts()->ObjC1)
John McCalld70fb982011-06-15 23:25:17 +0000245 return false;
246
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000247 LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC();
Fariborz Jahanianaa7b9aa2012-01-25 00:20:29 +0000248 bool NoNSAllocReallocError = origCI.getMigratorOpts().NoNSAllocReallocError;
Fariborz Jahanian0c859d62012-01-26 00:08:04 +0000249 bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval;
Argyrios Kyrtzidisd208ef92011-11-04 15:58:08 +0000250
Fariborz Jahanian48fd81b2012-01-26 20:57:58 +0000251 std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode,
252 NoFinalizeRemoval);
John McCalld70fb982011-06-15 23:25:17 +0000253 assert(!transforms.empty());
254
Ahmed Charlesb8984322014-03-07 20:03:18 +0000255 std::unique_ptr<CompilerInvocation> CInvok;
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000256 CInvok.reset(
257 createInvocationForMigration(origCI, PCHContainerOps->getRawReader()));
John McCalld70fb982011-06-15 23:25:17 +0000258 CInvok->getFrontendOpts().Inputs.clear();
Douglas Gregor32fbe312012-01-20 16:28:04 +0000259 CInvok->getFrontendOpts().Inputs.push_back(Input);
John McCalld70fb982011-06-15 23:25:17 +0000260
261 CapturedDiagList capturedDiags;
262
263 assert(DiagClient);
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000264 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
265 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000266 new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
267 DiagClient, /*ShouldOwnClient=*/false));
John McCalld70fb982011-06-15 23:25:17 +0000268
269 // Filter of all diagnostics.
Jordan Roseb00073d2012-08-10 01:06:16 +0000270 CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags);
John McCalld70fb982011-06-15 23:25:17 +0000271 Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
272
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000273 std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCompilerInvocationAction(
274 CInvok.release(), PCHContainerOps, Diags));
Jordan Roseb00073d2012-08-10 01:06:16 +0000275 if (!Unit) {
276 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000277 return true;
Jordan Roseb00073d2012-08-10 01:06:16 +0000278 }
John McCalld70fb982011-06-15 23:25:17 +0000279
280 // Don't filter diagnostics anymore.
281 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
282
283 ASTContext &Ctx = Unit->getASTContext();
284
285 if (Diags->hasFatalErrorOccurred()) {
286 Diags->Reset();
David Blaikiebbafb8a2012-03-11 07:00:24 +0000287 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
John McCalld70fb982011-06-15 23:25:17 +0000288 capturedDiags.reportDiagnostics(*Diags);
289 DiagClient->EndSourceFile();
Jordan Roseb00073d2012-08-10 01:06:16 +0000290 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000291 return true;
292 }
293
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000294 if (emitPremigrationARCErrors)
Douglas Gregor811db4e2012-10-23 22:26:28 +0000295 emitPremigrationErrors(capturedDiags, &origCI.getDiagnosticOpts(),
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000296 Unit->getPreprocessor());
297 if (!plistOut.empty()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000298 SmallVector<StoredDiagnostic, 8> arcDiags;
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000299 for (CapturedDiagList::iterator
300 I = capturedDiags.begin(), E = capturedDiags.end(); I != E; ++I)
301 arcDiags.push_back(*I);
302 writeARCDiagsToPlist(plistOut, arcDiags,
David Blaikiebbafb8a2012-03-11 07:00:24 +0000303 Ctx.getSourceManager(), Ctx.getLangOpts());
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000304 }
305
John McCalld70fb982011-06-15 23:25:17 +0000306 // After parsing of source files ended, we want to reuse the
307 // diagnostics objects to emit further diagnostics.
David Blaikiee2eefae2011-09-25 23:39:51 +0000308 // We call BeginSourceFile because DiagnosticConsumer requires that
John McCalld70fb982011-06-15 23:25:17 +0000309 // diagnostics with source range information are emitted only in between
310 // BeginSourceFile() and EndSourceFile().
David Blaikiebbafb8a2012-03-11 07:00:24 +0000311 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
John McCalld70fb982011-06-15 23:25:17 +0000312
313 // No macros will be added since we are just checking and we won't modify
314 // source code.
315 std::vector<SourceLocation> ARCMTMacroLocs;
316
317 TransformActions testAct(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
Argyrios Kyrtzidis03fbe3e2013-01-04 18:30:08 +0000318 MigrationPass pass(Ctx, OrigGCMode, Unit->getSema(), testAct, capturedDiags,
319 ARCMTMacroLocs);
Fariborz Jahanian0c859d62012-01-26 00:08:04 +0000320 pass.setNoFinalizeRemoval(NoFinalizeRemoval);
Alp Toker57cccec2014-05-19 23:48:49 +0000321 if (!NoNSAllocReallocError)
Alp Tokerd576e002014-06-12 11:13:52 +0000322 Diags->setSeverity(diag::warn_arcmt_nsalloc_realloc, diag::Severity::Error,
323 SourceLocation());
John McCalld70fb982011-06-15 23:25:17 +0000324
325 for (unsigned i=0, e = transforms.size(); i != e; ++i)
326 transforms[i](pass);
327
328 capturedDiags.reportDiagnostics(*Diags);
329
330 DiagClient->EndSourceFile();
Jordan Roseb00073d2012-08-10 01:06:16 +0000331 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000332
Argyrios Kyrtzidis73a0d322011-07-18 07:44:45 +0000333 return capturedDiags.hasErrors() || testAct.hasReportedErrors();
John McCalld70fb982011-06-15 23:25:17 +0000334}
335
336//===----------------------------------------------------------------------===//
337// applyTransformations.
338//===----------------------------------------------------------------------===//
339
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000340static bool
341applyTransforms(CompilerInvocation &origCI, const FrontendInputFile &Input,
342 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
343 DiagnosticConsumer *DiagClient, StringRef outputDir,
344 bool emitPremigrationARCErrors, StringRef plistOut) {
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000345 if (!origCI.getLangOpts()->ObjC1)
John McCalld70fb982011-06-15 23:25:17 +0000346 return false;
347
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000348 LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC();
Argyrios Kyrtzidisd208ef92011-11-04 15:58:08 +0000349
John McCalld70fb982011-06-15 23:25:17 +0000350 // Make sure checking is successful first.
351 CompilerInvocation CInvokForCheck(origCI);
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000352 if (arcmt::checkForManualIssues(CInvokForCheck, Input, PCHContainerOps,
353 DiagClient, emitPremigrationARCErrors,
354 plistOut))
John McCalld70fb982011-06-15 23:25:17 +0000355 return true;
356
357 CompilerInvocation CInvok(origCI);
358 CInvok.getFrontendOpts().Inputs.clear();
Douglas Gregor32fbe312012-01-20 16:28:04 +0000359 CInvok.getFrontendOpts().Inputs.push_back(Input);
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000360
361 MigrationProcess migration(CInvok, PCHContainerOps, DiagClient, outputDir);
Fariborz Jahanian48fd81b2012-01-26 20:57:58 +0000362 bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval;
John McCalld70fb982011-06-15 23:25:17 +0000363
Fariborz Jahanian48fd81b2012-01-26 20:57:58 +0000364 std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode,
365 NoFinalizeRemoval);
John McCalld70fb982011-06-15 23:25:17 +0000366 assert(!transforms.empty());
367
368 for (unsigned i=0, e = transforms.size(); i != e; ++i) {
369 bool err = migration.applyTransform(transforms[i]);
370 if (err) return true;
371 }
372
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000373 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
374 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000375 new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
376 DiagClient, /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000377
378 if (outputDir.empty()) {
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000379 origCI.getLangOpts()->ObjCAutoRefCount = true;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000380 return migration.getRemapper().overwriteOriginal(*Diags);
Argyrios Kyrtzidise2e40b42011-07-14 00:17:54 +0000381 } else {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000382 return migration.getRemapper().flushToDisk(outputDir, *Diags);
Argyrios Kyrtzidise2e40b42011-07-14 00:17:54 +0000383 }
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000384}
385
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000386bool arcmt::applyTransformations(
387 CompilerInvocation &origCI, const FrontendInputFile &Input,
388 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
389 DiagnosticConsumer *DiagClient) {
390 return applyTransforms(origCI, Input, PCHContainerOps, DiagClient,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000391 StringRef(), false, StringRef());
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000392}
393
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000394bool arcmt::migrateWithTemporaryFiles(
395 CompilerInvocation &origCI, const FrontendInputFile &Input,
396 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
397 DiagnosticConsumer *DiagClient, StringRef outputDir,
398 bool emitPremigrationARCErrors, StringRef plistOut) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000399 assert(!outputDir.empty() && "Expected output directory path");
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000400 return applyTransforms(origCI, Input, PCHContainerOps, DiagClient, outputDir,
401 emitPremigrationARCErrors, plistOut);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000402}
403
404bool arcmt::getFileRemappings(std::vector<std::pair<std::string,std::string> > &
405 remap,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000406 StringRef outputDir,
David Blaikiee2eefae2011-09-25 23:39:51 +0000407 DiagnosticConsumer *DiagClient) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000408 assert(!outputDir.empty());
John McCalld70fb982011-06-15 23:25:17 +0000409
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000410 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
411 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000412 new DiagnosticsEngine(DiagID, new DiagnosticOptions,
413 DiagClient, /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000414
415 FileRemapper remapper;
416 bool err = remapper.initFromDisk(outputDir, *Diags,
417 /*ignoreIfFilesChanged=*/true);
418 if (err)
419 return true;
420
Ted Kremenekf7639e12012-03-06 20:06:33 +0000421 PreprocessorOptions PPOpts;
422 remapper.applyMappings(PPOpts);
423 remap = PPOpts.RemappedFiles;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000424
425 return false;
John McCalld70fb982011-06-15 23:25:17 +0000426}
427
Ted Kremenekf7639e12012-03-06 20:06:33 +0000428
John McCalld70fb982011-06-15 23:25:17 +0000429//===----------------------------------------------------------------------===//
John McCalld70fb982011-06-15 23:25:17 +0000430// CollectTransformActions.
431//===----------------------------------------------------------------------===//
432
433namespace {
434
435class ARCMTMacroTrackerPPCallbacks : public PPCallbacks {
436 std::vector<SourceLocation> &ARCMTMacroLocs;
437
438public:
439 ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs)
440 : ARCMTMacroLocs(ARCMTMacroLocs) { }
441
Richard Smith36bd40d2015-05-04 03:15:40 +0000442 void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
Craig Topperb45acb82014-03-14 06:02:07 +0000443 SourceRange Range, const MacroArgs *Args) override {
John McCalld70fb982011-06-15 23:25:17 +0000444 if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName())
445 ARCMTMacroLocs.push_back(MacroNameTok.getLocation());
446 }
447};
448
449class ARCMTMacroTrackerAction : public ASTFrontendAction {
450 std::vector<SourceLocation> &ARCMTMacroLocs;
451
452public:
453 ARCMTMacroTrackerAction(std::vector<SourceLocation> &ARCMTMacroLocs)
454 : ARCMTMacroLocs(ARCMTMacroLocs) { }
455
David Blaikie6beb6aa2014-08-10 19:56:51 +0000456 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
457 StringRef InFile) override {
John McCalld70fb982011-06-15 23:25:17 +0000458 CI.getPreprocessor().addPPCallbacks(
Craig Topperb8a70532014-09-10 04:53:53 +0000459 llvm::make_unique<ARCMTMacroTrackerPPCallbacks>(ARCMTMacroLocs));
David Blaikie6beb6aa2014-08-10 19:56:51 +0000460 return llvm::make_unique<ASTConsumer>();
John McCalld70fb982011-06-15 23:25:17 +0000461 }
462};
463
464class RewritesApplicator : public TransformActions::RewriteReceiver {
465 Rewriter &rewriter;
John McCalld70fb982011-06-15 23:25:17 +0000466 MigrationProcess::RewriteListener *Listener;
467
468public:
469 RewritesApplicator(Rewriter &rewriter, ASTContext &ctx,
470 MigrationProcess::RewriteListener *listener)
Benjamin Kramerd1d76b22012-06-06 17:32:50 +0000471 : rewriter(rewriter), Listener(listener) {
John McCalld70fb982011-06-15 23:25:17 +0000472 if (Listener)
473 Listener->start(ctx);
474 }
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000475 ~RewritesApplicator() override {
John McCalld70fb982011-06-15 23:25:17 +0000476 if (Listener)
477 Listener->finish();
478 }
479
Craig Topperb45acb82014-03-14 06:02:07 +0000480 void insert(SourceLocation loc, StringRef text) override {
John McCalld70fb982011-06-15 23:25:17 +0000481 bool err = rewriter.InsertText(loc, text, /*InsertAfter=*/true,
482 /*indentNewLines=*/true);
483 if (!err && Listener)
484 Listener->insert(loc, text);
485 }
486
Craig Topperb45acb82014-03-14 06:02:07 +0000487 void remove(CharSourceRange range) override {
John McCalld70fb982011-06-15 23:25:17 +0000488 Rewriter::RewriteOptions removeOpts;
489 removeOpts.IncludeInsertsAtBeginOfRange = false;
490 removeOpts.IncludeInsertsAtEndOfRange = false;
491 removeOpts.RemoveLineIfEmpty = true;
492
493 bool err = rewriter.RemoveText(range, removeOpts);
494 if (!err && Listener)
495 Listener->remove(range);
496 }
497
Craig Topperb45acb82014-03-14 06:02:07 +0000498 void increaseIndentation(CharSourceRange range,
499 SourceLocation parentIndent) override {
John McCalld70fb982011-06-15 23:25:17 +0000500 rewriter.IncreaseIndentation(range, parentIndent);
501 }
502};
503
504} // end anonymous namespace.
505
506/// \brief Anchor for VTable.
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000507MigrationProcess::RewriteListener::~RewriteListener() { }
John McCalld70fb982011-06-15 23:25:17 +0000508
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000509MigrationProcess::MigrationProcess(
510 const CompilerInvocation &CI,
511 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
512 DiagnosticConsumer *diagClient, StringRef outputDir)
Benjamin Kramercfeacf52016-05-27 14:27:13 +0000513 : OrigCI(CI), PCHContainerOps(std::move(PCHContainerOps)),
514 DiagClient(diagClient), HadARCErrors(false) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000515 if (!outputDir.empty()) {
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000516 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
517 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000518 new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(),
519 DiagClient, /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000520 Remapper.initFromDisk(outputDir, *Diags, /*ignoreIfFilesChanges=*/true);
521 }
522}
523
John McCalld70fb982011-06-15 23:25:17 +0000524bool MigrationProcess::applyTransform(TransformFn trans,
525 RewriteListener *listener) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000526 std::unique_ptr<CompilerInvocation> CInvok;
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000527 CInvok.reset(
528 createInvocationForMigration(OrigCI, PCHContainerOps->getRawReader()));
John McCalld70fb982011-06-15 23:25:17 +0000529 CInvok->getDiagnosticOpts().IgnoreWarnings = true;
530
Ted Kremenekf7639e12012-03-06 20:06:33 +0000531 Remapper.applyMappings(CInvok->getPreprocessorOpts());
John McCalld70fb982011-06-15 23:25:17 +0000532
533 CapturedDiagList capturedDiags;
534 std::vector<SourceLocation> ARCMTMacroLocs;
535
536 assert(DiagClient);
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000537 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
538 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000539 new DiagnosticsEngine(DiagID, new DiagnosticOptions,
540 DiagClient, /*ShouldOwnClient=*/false));
John McCalld70fb982011-06-15 23:25:17 +0000541
542 // Filter of all diagnostics.
Jordan Roseb00073d2012-08-10 01:06:16 +0000543 CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags);
John McCalld70fb982011-06-15 23:25:17 +0000544 Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
545
Ahmed Charlesb8984322014-03-07 20:03:18 +0000546 std::unique_ptr<ARCMTMacroTrackerAction> ASTAction;
John McCalld70fb982011-06-15 23:25:17 +0000547 ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs));
548
Ahmed Charlesaf94d562014-03-09 11:34:25 +0000549 std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCompilerInvocationAction(
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000550 CInvok.release(), PCHContainerOps, Diags, ASTAction.get()));
Jordan Roseb00073d2012-08-10 01:06:16 +0000551 if (!Unit) {
552 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000553 return true;
Jordan Roseb00073d2012-08-10 01:06:16 +0000554 }
John McCalld70fb982011-06-15 23:25:17 +0000555 Unit->setOwnsRemappedFileBuffers(false); // FileRemapper manages that.
556
Argyrios Kyrtzidisec852d92013-07-22 18:13:54 +0000557 HadARCErrors = HadARCErrors || capturedDiags.hasErrors();
558
John McCalld70fb982011-06-15 23:25:17 +0000559 // Don't filter diagnostics anymore.
560 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
561
562 ASTContext &Ctx = Unit->getASTContext();
563
564 if (Diags->hasFatalErrorOccurred()) {
565 Diags->Reset();
David Blaikiebbafb8a2012-03-11 07:00:24 +0000566 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
John McCalld70fb982011-06-15 23:25:17 +0000567 capturedDiags.reportDiagnostics(*Diags);
568 DiagClient->EndSourceFile();
Jordan Roseb00073d2012-08-10 01:06:16 +0000569 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000570 return true;
571 }
572
573 // After parsing of source files ended, we want to reuse the
574 // diagnostics objects to emit further diagnostics.
David Blaikiee2eefae2011-09-25 23:39:51 +0000575 // We call BeginSourceFile because DiagnosticConsumer requires that
John McCalld70fb982011-06-15 23:25:17 +0000576 // diagnostics with source range information are emitted only in between
577 // BeginSourceFile() and EndSourceFile().
David Blaikiebbafb8a2012-03-11 07:00:24 +0000578 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
John McCalld70fb982011-06-15 23:25:17 +0000579
David Blaikiebbafb8a2012-03-11 07:00:24 +0000580 Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts());
John McCalld70fb982011-06-15 23:25:17 +0000581 TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000582 MigrationPass pass(Ctx, OrigCI.getLangOpts()->getGC(),
Argyrios Kyrtzidis03fbe3e2013-01-04 18:30:08 +0000583 Unit->getSema(), TA, capturedDiags, ARCMTMacroLocs);
John McCalld70fb982011-06-15 23:25:17 +0000584
585 trans(pass);
586
587 {
588 RewritesApplicator applicator(rewriter, Ctx, listener);
589 TA.applyRewrites(applicator);
590 }
591
592 DiagClient->EndSourceFile();
Jordan Roseb00073d2012-08-10 01:06:16 +0000593 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000594
595 if (DiagClient->getNumErrors())
596 return true;
597
598 for (Rewriter::buffer_iterator
599 I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) {
600 FileID FID = I->first;
601 RewriteBuffer &buf = I->second;
602 const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID);
603 assert(file);
604 std::string newFname = file->getName();
605 newFname += "-trans";
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000606 SmallString<512> newText;
John McCalld70fb982011-06-15 23:25:17 +0000607 llvm::raw_svector_ostream vecOS(newText);
608 buf.write(vecOS);
Rafael Espindola1a1b1562014-08-17 23:12:27 +0000609 std::unique_ptr<llvm::MemoryBuffer> memBuf(
610 llvm::MemoryBuffer::getMemBufferCopy(
611 StringRef(newText.data(), newText.size()), newFname));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000612 SmallString<64> filePath(file->getName());
John McCalld70fb982011-06-15 23:25:17 +0000613 Unit->getFileManager().FixupRelativePath(filePath);
Rafael Espindola1a1b1562014-08-17 23:12:27 +0000614 Remapper.remap(filePath.str(), std::move(memBuf));
John McCalld70fb982011-06-15 23:25:17 +0000615 }
616
617 return false;
618}