blob: 8c04c8371cefc6ca171c22ac3c808cef485cebcc [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"
John McCalld70fb982011-06-15 23:25:17 +000024using namespace clang;
25using namespace arcmt;
John McCalld70fb982011-06-15 23:25:17 +000026
Chris Lattner54b16772011-07-23 17:14:25 +000027bool CapturedDiagList::clearDiagnostic(ArrayRef<unsigned> IDs,
John McCalld70fb982011-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 Kyrtzidis03fbe3e2013-01-04 18:30:08 +000043 if (eraseS->getLevel() != DiagnosticsEngine::Note)
44 while (I != List.end() && I->getLevel() == DiagnosticsEngine::Note)
45 ++I;
John McCalld70fb982011-06-15 23:25:17 +000046 // Clear the diagnostic and any notes following it.
Joao Matos0e167f72012-08-31 17:28:09 +000047 I = List.erase(eraseS, I);
John McCalld70fb982011-06-15 23:25:17 +000048 continue;
49 }
50
51 ++I;
52 }
53
54 return cleared;
55}
56
Chris Lattner54b16772011-07-23 17:14:25 +000057bool CapturedDiagList::hasDiagnostic(ArrayRef<unsigned> IDs,
Argyrios Kyrtzidis0f3f9f72011-06-18 00:53:34 +000058 SourceRange range) const {
John McCalld70fb982011-06-15 23:25:17 +000059 if (range.isInvalid())
60 return false;
61
Argyrios Kyrtzidis0f3f9f72011-06-18 00:53:34 +000062 ListTy::const_iterator I = List.begin();
John McCalld70fb982011-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 Blaikie9c902b52011-09-25 23:23:43 +000079void CapturedDiagList::reportDiagnostics(DiagnosticsEngine &Diags) const {
Argyrios Kyrtzidis0f3f9f72011-06-18 00:53:34 +000080 for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I)
John McCalld70fb982011-06-15 23:25:17 +000081 Diags.Report(*I);
82}
83
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +000084bool CapturedDiagList::hasErrors() const {
85 for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I)
David Blaikie9c902b52011-09-25 23:23:43 +000086 if (I->getLevel() >= DiagnosticsEngine::Error)
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +000087 return true;
88
89 return false;
90}
91
John McCalld70fb982011-06-15 23:25:17 +000092namespace {
93
David Blaikiea24a0bc2011-09-25 23:54:33 +000094class CaptureDiagnosticConsumer : public DiagnosticConsumer {
David Blaikie9c902b52011-09-25 23:23:43 +000095 DiagnosticsEngine &Diags;
Jordan Roseb00073d2012-08-10 01:06:16 +000096 DiagnosticConsumer &DiagClient;
John McCalld70fb982011-06-15 23:25:17 +000097 CapturedDiagList &CapturedDiags;
Jordan Roseb00073d2012-08-10 01:06:16 +000098 bool HasBegunSourceFile;
John McCalld70fb982011-06-15 23:25:17 +000099public:
David Blaikiea24a0bc2011-09-25 23:54:33 +0000100 CaptureDiagnosticConsumer(DiagnosticsEngine &diags,
Jordan Roseb00073d2012-08-10 01:06:16 +0000101 DiagnosticConsumer &client,
102 CapturedDiagList &capturedDiags)
103 : Diags(diags), DiagClient(client), CapturedDiags(capturedDiags),
104 HasBegunSourceFile(false) { }
105
Craig Topperb45acb82014-03-14 06:02:07 +0000106 void BeginSourceFile(const LangOptions &Opts,
107 const Preprocessor *PP) override {
Jordan Roseb00073d2012-08-10 01:06:16 +0000108 // 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
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000127 ~CaptureDiagnosticConsumer() override {
Jordan Roseb00073d2012-08-10 01:06:16 +0000128 assert(!HasBegunSourceFile && "FinishCapture not called!");
129 }
John McCalld70fb982011-06-15 23:25:17 +0000130
Craig Topperb45acb82014-03-14 06:02:07 +0000131 void HandleDiagnostic(DiagnosticsEngine::Level level,
132 const Diagnostic &Info) override {
Ted Kremenek337c5b82011-10-20 05:07:47 +0000133 if (DiagnosticIDs::isARCDiagnostic(Info.getID()) ||
David Blaikie9c902b52011-09-25 23:23:43 +0000134 level >= DiagnosticsEngine::Error || level == DiagnosticsEngine::Note) {
Argyrios Kyrtzidis1f732162012-12-12 22:48:28 +0000135 if (Info.getLocation().isValid())
136 CapturedDiags.push_back(StoredDiagnostic(level, Info));
John McCalld70fb982011-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 Kyrtzidis81a35902011-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 Kyrtzidis81a35902011-06-20 19:59:52 +0000151 llvm::Triple triple(origCI.getTargetOpts().Triple);
152
Cameron Esfahani556d91e2013-09-14 01:09:11 +0000153 if (triple.isiOS())
Argyrios Kyrtzidis81a35902011-06-20 19:59:52 +0000154 return triple.getOSMajorVersion() >= 5;
155
Tim Northover756447a2015-10-30 16:30:36 +0000156 if (triple.isWatchOS())
157 return true;
158
Argyrios Kyrtzidis81a35902011-06-20 19:59:52 +0000159 if (triple.getOS() == llvm::Triple::Darwin)
160 return triple.getOSMajorVersion() >= 11;
161
162 if (triple.getOS() == llvm::Triple::MacOSX) {
163 unsigned Major, Minor, Micro;
164 triple.getOSVersion(Major, Minor, Micro);
165 return Major > 10 || (Major == 10 && Minor >= 7);
166 }
167
168 return false;
169}
170
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000171static CompilerInvocation *
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000172createInvocationForMigration(CompilerInvocation &origCI,
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000173 const PCHContainerReader &PCHContainerRdr) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000174 std::unique_ptr<CompilerInvocation> CInvok;
John McCalld70fb982011-06-15 23:25:17 +0000175 CInvok.reset(new CompilerInvocation(origCI));
Argyrios Kyrtzidis88c0d3b2013-02-05 16:37:00 +0000176 PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts();
177 if (!PPOpts.ImplicitPCHInclude.empty()) {
178 // We can't use a PCH because it was likely built in non-ARC mode and we
179 // want to parse in ARC. Include the original header.
180 FileManager FileMgr(origCI.getFileSystemOpts());
181 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
182 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
183 new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
184 new IgnoringDiagConsumer()));
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000185 std::string OriginalFile = ASTReader::getOriginalSourceFile(
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000186 PPOpts.ImplicitPCHInclude, FileMgr, PCHContainerRdr, *Diags);
Argyrios Kyrtzidis88c0d3b2013-02-05 16:37:00 +0000187 if (!OriginalFile.empty())
188 PPOpts.Includes.insert(PPOpts.Includes.begin(), OriginalFile);
189 PPOpts.ImplicitPCHInclude.clear();
190 }
191 // FIXME: Get the original header of a PTH as well.
192 CInvok->getPreprocessorOpts().ImplicitPTHInclude.clear();
John McCalld70fb982011-06-15 23:25:17 +0000193 std::string define = getARCMTMacroName();
194 define += '=';
195 CInvok->getPreprocessorOpts().addMacroDef(define);
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000196 CInvok->getLangOpts()->ObjCAutoRefCount = true;
197 CInvok->getLangOpts()->setGC(LangOptions::NonGC);
John McCalld70fb982011-06-15 23:25:17 +0000198 CInvok->getDiagnosticOpts().ErrorLimit = 0;
Argyrios Kyrtzidisaa38f692012-06-20 01:46:26 +0000199 CInvok->getDiagnosticOpts().PedanticErrors = 0;
Argyrios Kyrtzidis692bf8c2012-06-20 01:10:40 +0000200
201 // Ignore -Werror flags when migrating.
202 std::vector<std::string> WarnOpts;
203 for (std::vector<std::string>::iterator
204 I = CInvok->getDiagnosticOpts().Warnings.begin(),
205 E = CInvok->getDiagnosticOpts().Warnings.end(); I != E; ++I) {
206 if (!StringRef(*I).startswith("error"))
207 WarnOpts.push_back(*I);
208 }
209 WarnOpts.push_back("error=arc-unsafe-retained-assign");
Chandler Carruthc72d9b32014-03-02 04:02:40 +0000210 CInvok->getDiagnosticOpts().Warnings = std::move(WarnOpts);
Argyrios Kyrtzidis692bf8c2012-06-20 01:10:40 +0000211
John McCall460ce582015-10-22 18:38:17 +0000212 CInvok->getLangOpts()->ObjCWeakRuntime = HasARCRuntime(origCI);
213 CInvok->getLangOpts()->ObjCWeak = CInvok->getLangOpts()->ObjCWeakRuntime;
John McCalld70fb982011-06-15 23:25:17 +0000214
Ahmed Charles9a16beb2014-03-07 19:33:25 +0000215 return CInvok.release();
John McCalld70fb982011-06-15 23:25:17 +0000216}
217
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000218static void emitPremigrationErrors(const CapturedDiagList &arcDiags,
Douglas Gregor811db4e2012-10-23 22:26:28 +0000219 DiagnosticOptions *diagOpts,
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000220 Preprocessor &PP) {
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000221 TextDiagnosticPrinter printer(llvm::errs(), diagOpts);
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000222 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
223 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000224 new DiagnosticsEngine(DiagID, diagOpts, &printer,
225 /*ShouldOwnClient=*/false));
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000226 Diags->setSourceManager(&PP.getSourceManager());
227
David Blaikiebbafb8a2012-03-11 07:00:24 +0000228 printer.BeginSourceFile(PP.getLangOpts(), &PP);
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000229 arcDiags.reportDiagnostics(*Diags);
230 printer.EndSourceFile();
231}
232
John McCalld70fb982011-06-15 23:25:17 +0000233//===----------------------------------------------------------------------===//
234// checkForManualIssues.
235//===----------------------------------------------------------------------===//
236
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000237bool arcmt::checkForManualIssues(
238 CompilerInvocation &origCI, const FrontendInputFile &Input,
239 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
240 DiagnosticConsumer *DiagClient, bool emitPremigrationARCErrors,
241 StringRef plistOut) {
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000242 if (!origCI.getLangOpts()->ObjC1)
John McCalld70fb982011-06-15 23:25:17 +0000243 return false;
244
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000245 LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC();
Fariborz Jahanianaa7b9aa2012-01-25 00:20:29 +0000246 bool NoNSAllocReallocError = origCI.getMigratorOpts().NoNSAllocReallocError;
Fariborz Jahanian0c859d62012-01-26 00:08:04 +0000247 bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval;
Argyrios Kyrtzidisd208ef92011-11-04 15:58:08 +0000248
Fariborz Jahanian48fd81b2012-01-26 20:57:58 +0000249 std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode,
250 NoFinalizeRemoval);
John McCalld70fb982011-06-15 23:25:17 +0000251 assert(!transforms.empty());
252
Ahmed Charlesb8984322014-03-07 20:03:18 +0000253 std::unique_ptr<CompilerInvocation> CInvok;
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000254 CInvok.reset(
255 createInvocationForMigration(origCI, PCHContainerOps->getRawReader()));
John McCalld70fb982011-06-15 23:25:17 +0000256 CInvok->getFrontendOpts().Inputs.clear();
Douglas Gregor32fbe312012-01-20 16:28:04 +0000257 CInvok->getFrontendOpts().Inputs.push_back(Input);
John McCalld70fb982011-06-15 23:25:17 +0000258
259 CapturedDiagList capturedDiags;
260
261 assert(DiagClient);
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000262 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
263 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000264 new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
265 DiagClient, /*ShouldOwnClient=*/false));
John McCalld70fb982011-06-15 23:25:17 +0000266
267 // Filter of all diagnostics.
Jordan Roseb00073d2012-08-10 01:06:16 +0000268 CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags);
John McCalld70fb982011-06-15 23:25:17 +0000269 Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
270
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000271 std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCompilerInvocationAction(
272 CInvok.release(), PCHContainerOps, Diags));
Jordan Roseb00073d2012-08-10 01:06:16 +0000273 if (!Unit) {
274 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000275 return true;
Jordan Roseb00073d2012-08-10 01:06:16 +0000276 }
John McCalld70fb982011-06-15 23:25:17 +0000277
278 // Don't filter diagnostics anymore.
279 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
280
281 ASTContext &Ctx = Unit->getASTContext();
282
283 if (Diags->hasFatalErrorOccurred()) {
284 Diags->Reset();
David Blaikiebbafb8a2012-03-11 07:00:24 +0000285 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
John McCalld70fb982011-06-15 23:25:17 +0000286 capturedDiags.reportDiagnostics(*Diags);
287 DiagClient->EndSourceFile();
Jordan Roseb00073d2012-08-10 01:06:16 +0000288 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000289 return true;
290 }
291
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000292 if (emitPremigrationARCErrors)
Douglas Gregor811db4e2012-10-23 22:26:28 +0000293 emitPremigrationErrors(capturedDiags, &origCI.getDiagnosticOpts(),
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000294 Unit->getPreprocessor());
295 if (!plistOut.empty()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000296 SmallVector<StoredDiagnostic, 8> arcDiags;
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000297 for (CapturedDiagList::iterator
298 I = capturedDiags.begin(), E = capturedDiags.end(); I != E; ++I)
299 arcDiags.push_back(*I);
300 writeARCDiagsToPlist(plistOut, arcDiags,
David Blaikiebbafb8a2012-03-11 07:00:24 +0000301 Ctx.getSourceManager(), Ctx.getLangOpts());
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +0000302 }
303
John McCalld70fb982011-06-15 23:25:17 +0000304 // After parsing of source files ended, we want to reuse the
305 // diagnostics objects to emit further diagnostics.
David Blaikiee2eefae2011-09-25 23:39:51 +0000306 // We call BeginSourceFile because DiagnosticConsumer requires that
John McCalld70fb982011-06-15 23:25:17 +0000307 // diagnostics with source range information are emitted only in between
308 // BeginSourceFile() and EndSourceFile().
David Blaikiebbafb8a2012-03-11 07:00:24 +0000309 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
John McCalld70fb982011-06-15 23:25:17 +0000310
311 // No macros will be added since we are just checking and we won't modify
312 // source code.
313 std::vector<SourceLocation> ARCMTMacroLocs;
314
315 TransformActions testAct(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
Argyrios Kyrtzidis03fbe3e2013-01-04 18:30:08 +0000316 MigrationPass pass(Ctx, OrigGCMode, Unit->getSema(), testAct, capturedDiags,
317 ARCMTMacroLocs);
Fariborz Jahanian0c859d62012-01-26 00:08:04 +0000318 pass.setNoFinalizeRemoval(NoFinalizeRemoval);
Alp Toker57cccec2014-05-19 23:48:49 +0000319 if (!NoNSAllocReallocError)
Alp Tokerd576e002014-06-12 11:13:52 +0000320 Diags->setSeverity(diag::warn_arcmt_nsalloc_realloc, diag::Severity::Error,
321 SourceLocation());
John McCalld70fb982011-06-15 23:25:17 +0000322
323 for (unsigned i=0, e = transforms.size(); i != e; ++i)
324 transforms[i](pass);
325
326 capturedDiags.reportDiagnostics(*Diags);
327
328 DiagClient->EndSourceFile();
Jordan Roseb00073d2012-08-10 01:06:16 +0000329 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000330
Argyrios Kyrtzidis73a0d322011-07-18 07:44:45 +0000331 return capturedDiags.hasErrors() || testAct.hasReportedErrors();
John McCalld70fb982011-06-15 23:25:17 +0000332}
333
334//===----------------------------------------------------------------------===//
335// applyTransformations.
336//===----------------------------------------------------------------------===//
337
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000338static bool
339applyTransforms(CompilerInvocation &origCI, const FrontendInputFile &Input,
340 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
341 DiagnosticConsumer *DiagClient, StringRef outputDir,
342 bool emitPremigrationARCErrors, StringRef plistOut) {
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000343 if (!origCI.getLangOpts()->ObjC1)
John McCalld70fb982011-06-15 23:25:17 +0000344 return false;
345
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000346 LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC();
Argyrios Kyrtzidisd208ef92011-11-04 15:58:08 +0000347
John McCalld70fb982011-06-15 23:25:17 +0000348 // Make sure checking is successful first.
349 CompilerInvocation CInvokForCheck(origCI);
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000350 if (arcmt::checkForManualIssues(CInvokForCheck, Input, PCHContainerOps,
351 DiagClient, emitPremigrationARCErrors,
352 plistOut))
John McCalld70fb982011-06-15 23:25:17 +0000353 return true;
354
355 CompilerInvocation CInvok(origCI);
356 CInvok.getFrontendOpts().Inputs.clear();
Douglas Gregor32fbe312012-01-20 16:28:04 +0000357 CInvok.getFrontendOpts().Inputs.push_back(Input);
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000358
359 MigrationProcess migration(CInvok, PCHContainerOps, DiagClient, outputDir);
Fariborz Jahanian48fd81b2012-01-26 20:57:58 +0000360 bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval;
John McCalld70fb982011-06-15 23:25:17 +0000361
Fariborz Jahanian48fd81b2012-01-26 20:57:58 +0000362 std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode,
363 NoFinalizeRemoval);
John McCalld70fb982011-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 Noblesmithc95d8192012-02-20 14:00:23 +0000371 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
372 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000373 new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
374 DiagClient, /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000375
376 if (outputDir.empty()) {
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000377 origCI.getLangOpts()->ObjCAutoRefCount = true;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000378 return migration.getRemapper().overwriteOriginal(*Diags);
Argyrios Kyrtzidise2e40b42011-07-14 00:17:54 +0000379 } else {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000380 return migration.getRemapper().flushToDisk(outputDir, *Diags);
Argyrios Kyrtzidise2e40b42011-07-14 00:17:54 +0000381 }
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000382}
383
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000384bool arcmt::applyTransformations(
385 CompilerInvocation &origCI, const FrontendInputFile &Input,
386 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
387 DiagnosticConsumer *DiagClient) {
388 return applyTransforms(origCI, Input, PCHContainerOps, DiagClient,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000389 StringRef(), false, StringRef());
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000390}
391
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000392bool arcmt::migrateWithTemporaryFiles(
393 CompilerInvocation &origCI, const FrontendInputFile &Input,
394 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
395 DiagnosticConsumer *DiagClient, StringRef outputDir,
396 bool emitPremigrationARCErrors, StringRef plistOut) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000397 assert(!outputDir.empty() && "Expected output directory path");
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000398 return applyTransforms(origCI, Input, PCHContainerOps, DiagClient, outputDir,
399 emitPremigrationARCErrors, plistOut);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000400}
401
402bool arcmt::getFileRemappings(std::vector<std::pair<std::string,std::string> > &
403 remap,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000404 StringRef outputDir,
David Blaikiee2eefae2011-09-25 23:39:51 +0000405 DiagnosticConsumer *DiagClient) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000406 assert(!outputDir.empty());
John McCalld70fb982011-06-15 23:25:17 +0000407
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000408 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
409 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000410 new DiagnosticsEngine(DiagID, new DiagnosticOptions,
411 DiagClient, /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000412
413 FileRemapper remapper;
414 bool err = remapper.initFromDisk(outputDir, *Diags,
415 /*ignoreIfFilesChanged=*/true);
416 if (err)
417 return true;
418
Ted Kremenekf7639e12012-03-06 20:06:33 +0000419 PreprocessorOptions PPOpts;
420 remapper.applyMappings(PPOpts);
421 remap = PPOpts.RemappedFiles;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000422
423 return false;
John McCalld70fb982011-06-15 23:25:17 +0000424}
425
Ted Kremenekf7639e12012-03-06 20:06:33 +0000426
John McCalld70fb982011-06-15 23:25:17 +0000427//===----------------------------------------------------------------------===//
John McCalld70fb982011-06-15 23:25:17 +0000428// CollectTransformActions.
429//===----------------------------------------------------------------------===//
430
431namespace {
432
433class ARCMTMacroTrackerPPCallbacks : public PPCallbacks {
434 std::vector<SourceLocation> &ARCMTMacroLocs;
435
436public:
437 ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs)
438 : ARCMTMacroLocs(ARCMTMacroLocs) { }
439
Richard Smith36bd40d2015-05-04 03:15:40 +0000440 void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
Craig Topperb45acb82014-03-14 06:02:07 +0000441 SourceRange Range, const MacroArgs *Args) override {
John McCalld70fb982011-06-15 23:25:17 +0000442 if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName())
443 ARCMTMacroLocs.push_back(MacroNameTok.getLocation());
444 }
445};
446
447class ARCMTMacroTrackerAction : public ASTFrontendAction {
448 std::vector<SourceLocation> &ARCMTMacroLocs;
449
450public:
451 ARCMTMacroTrackerAction(std::vector<SourceLocation> &ARCMTMacroLocs)
452 : ARCMTMacroLocs(ARCMTMacroLocs) { }
453
David Blaikie6beb6aa2014-08-10 19:56:51 +0000454 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
455 StringRef InFile) override {
John McCalld70fb982011-06-15 23:25:17 +0000456 CI.getPreprocessor().addPPCallbacks(
Craig Topperb8a70532014-09-10 04:53:53 +0000457 llvm::make_unique<ARCMTMacroTrackerPPCallbacks>(ARCMTMacroLocs));
David Blaikie6beb6aa2014-08-10 19:56:51 +0000458 return llvm::make_unique<ASTConsumer>();
John McCalld70fb982011-06-15 23:25:17 +0000459 }
460};
461
462class RewritesApplicator : public TransformActions::RewriteReceiver {
463 Rewriter &rewriter;
John McCalld70fb982011-06-15 23:25:17 +0000464 MigrationProcess::RewriteListener *Listener;
465
466public:
467 RewritesApplicator(Rewriter &rewriter, ASTContext &ctx,
468 MigrationProcess::RewriteListener *listener)
Benjamin Kramerd1d76b22012-06-06 17:32:50 +0000469 : rewriter(rewriter), Listener(listener) {
John McCalld70fb982011-06-15 23:25:17 +0000470 if (Listener)
471 Listener->start(ctx);
472 }
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000473 ~RewritesApplicator() override {
John McCalld70fb982011-06-15 23:25:17 +0000474 if (Listener)
475 Listener->finish();
476 }
477
Craig Topperb45acb82014-03-14 06:02:07 +0000478 void insert(SourceLocation loc, StringRef text) override {
John McCalld70fb982011-06-15 23:25:17 +0000479 bool err = rewriter.InsertText(loc, text, /*InsertAfter=*/true,
480 /*indentNewLines=*/true);
481 if (!err && Listener)
482 Listener->insert(loc, text);
483 }
484
Craig Topperb45acb82014-03-14 06:02:07 +0000485 void remove(CharSourceRange range) override {
John McCalld70fb982011-06-15 23:25:17 +0000486 Rewriter::RewriteOptions removeOpts;
487 removeOpts.IncludeInsertsAtBeginOfRange = false;
488 removeOpts.IncludeInsertsAtEndOfRange = false;
489 removeOpts.RemoveLineIfEmpty = true;
490
491 bool err = rewriter.RemoveText(range, removeOpts);
492 if (!err && Listener)
493 Listener->remove(range);
494 }
495
Craig Topperb45acb82014-03-14 06:02:07 +0000496 void increaseIndentation(CharSourceRange range,
497 SourceLocation parentIndent) override {
John McCalld70fb982011-06-15 23:25:17 +0000498 rewriter.IncreaseIndentation(range, parentIndent);
499 }
500};
501
502} // end anonymous namespace.
503
504/// \brief Anchor for VTable.
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000505MigrationProcess::RewriteListener::~RewriteListener() { }
John McCalld70fb982011-06-15 23:25:17 +0000506
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000507MigrationProcess::MigrationProcess(
508 const CompilerInvocation &CI,
509 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
510 DiagnosticConsumer *diagClient, StringRef outputDir)
511 : OrigCI(CI), PCHContainerOps(PCHContainerOps), DiagClient(diagClient),
512 HadARCErrors(false) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000513 if (!outputDir.empty()) {
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000514 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
515 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000516 new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(),
517 DiagClient, /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000518 Remapper.initFromDisk(outputDir, *Diags, /*ignoreIfFilesChanges=*/true);
519 }
520}
521
John McCalld70fb982011-06-15 23:25:17 +0000522bool MigrationProcess::applyTransform(TransformFn trans,
523 RewriteListener *listener) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000524 std::unique_ptr<CompilerInvocation> CInvok;
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000525 CInvok.reset(
526 createInvocationForMigration(OrigCI, PCHContainerOps->getRawReader()));
John McCalld70fb982011-06-15 23:25:17 +0000527 CInvok->getDiagnosticOpts().IgnoreWarnings = true;
528
Ted Kremenekf7639e12012-03-06 20:06:33 +0000529 Remapper.applyMappings(CInvok->getPreprocessorOpts());
John McCalld70fb982011-06-15 23:25:17 +0000530
531 CapturedDiagList capturedDiags;
532 std::vector<SourceLocation> ARCMTMacroLocs;
533
534 assert(DiagClient);
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000535 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
536 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000537 new DiagnosticsEngine(DiagID, new DiagnosticOptions,
538 DiagClient, /*ShouldOwnClient=*/false));
John McCalld70fb982011-06-15 23:25:17 +0000539
540 // Filter of all diagnostics.
Jordan Roseb00073d2012-08-10 01:06:16 +0000541 CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags);
John McCalld70fb982011-06-15 23:25:17 +0000542 Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
543
Ahmed Charlesb8984322014-03-07 20:03:18 +0000544 std::unique_ptr<ARCMTMacroTrackerAction> ASTAction;
John McCalld70fb982011-06-15 23:25:17 +0000545 ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs));
546
Ahmed Charlesaf94d562014-03-09 11:34:25 +0000547 std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCompilerInvocationAction(
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000548 CInvok.release(), PCHContainerOps, Diags, ASTAction.get()));
Jordan Roseb00073d2012-08-10 01:06:16 +0000549 if (!Unit) {
550 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000551 return true;
Jordan Roseb00073d2012-08-10 01:06:16 +0000552 }
John McCalld70fb982011-06-15 23:25:17 +0000553 Unit->setOwnsRemappedFileBuffers(false); // FileRemapper manages that.
554
Argyrios Kyrtzidisec852d92013-07-22 18:13:54 +0000555 HadARCErrors = HadARCErrors || capturedDiags.hasErrors();
556
John McCalld70fb982011-06-15 23:25:17 +0000557 // Don't filter diagnostics anymore.
558 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
559
560 ASTContext &Ctx = Unit->getASTContext();
561
562 if (Diags->hasFatalErrorOccurred()) {
563 Diags->Reset();
David Blaikiebbafb8a2012-03-11 07:00:24 +0000564 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
John McCalld70fb982011-06-15 23:25:17 +0000565 capturedDiags.reportDiagnostics(*Diags);
566 DiagClient->EndSourceFile();
Jordan Roseb00073d2012-08-10 01:06:16 +0000567 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000568 return true;
569 }
570
571 // After parsing of source files ended, we want to reuse the
572 // diagnostics objects to emit further diagnostics.
David Blaikiee2eefae2011-09-25 23:39:51 +0000573 // We call BeginSourceFile because DiagnosticConsumer requires that
John McCalld70fb982011-06-15 23:25:17 +0000574 // diagnostics with source range information are emitted only in between
575 // BeginSourceFile() and EndSourceFile().
David Blaikiebbafb8a2012-03-11 07:00:24 +0000576 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
John McCalld70fb982011-06-15 23:25:17 +0000577
David Blaikiebbafb8a2012-03-11 07:00:24 +0000578 Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts());
John McCalld70fb982011-06-15 23:25:17 +0000579 TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000580 MigrationPass pass(Ctx, OrigCI.getLangOpts()->getGC(),
Argyrios Kyrtzidis03fbe3e2013-01-04 18:30:08 +0000581 Unit->getSema(), TA, capturedDiags, ARCMTMacroLocs);
John McCalld70fb982011-06-15 23:25:17 +0000582
583 trans(pass);
584
585 {
586 RewritesApplicator applicator(rewriter, Ctx, listener);
587 TA.applyRewrites(applicator);
588 }
589
590 DiagClient->EndSourceFile();
Jordan Roseb00073d2012-08-10 01:06:16 +0000591 errRec.FinishCapture();
John McCalld70fb982011-06-15 23:25:17 +0000592
593 if (DiagClient->getNumErrors())
594 return true;
595
596 for (Rewriter::buffer_iterator
597 I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) {
598 FileID FID = I->first;
599 RewriteBuffer &buf = I->second;
600 const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID);
601 assert(file);
602 std::string newFname = file->getName();
603 newFname += "-trans";
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000604 SmallString<512> newText;
John McCalld70fb982011-06-15 23:25:17 +0000605 llvm::raw_svector_ostream vecOS(newText);
606 buf.write(vecOS);
Rafael Espindola1a1b1562014-08-17 23:12:27 +0000607 std::unique_ptr<llvm::MemoryBuffer> memBuf(
608 llvm::MemoryBuffer::getMemBufferCopy(
609 StringRef(newText.data(), newText.size()), newFname));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000610 SmallString<64> filePath(file->getName());
John McCalld70fb982011-06-15 23:25:17 +0000611 Unit->getFileManager().FixupRelativePath(filePath);
Rafael Espindola1a1b1562014-08-17 23:12:27 +0000612 Remapper.remap(filePath.str(), std::move(memBuf));
John McCalld70fb982011-06-15 23:25:17 +0000613 }
614
615 return false;
616}