2011-06-16 07:25:17 +08:00
|
|
|
//===--- ARCMT.cpp - Migration to ARC mode --------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Internals.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/Basic/DiagnosticCategories.h"
|
2011-06-16 07:25:17 +08:00
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
2012-01-21 00:28:04 +08:00
|
|
|
#include "clang/Frontend/FrontendAction.h"
|
2011-07-20 01:20:03 +08:00
|
|
|
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
2011-06-16 07:25:17 +08:00
|
|
|
#include "clang/Frontend/Utils.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2012-09-01 13:09:24 +08:00
|
|
|
#include "clang/Rewrite/Core/Rewriter.h"
|
2011-06-16 07:25:17 +08:00
|
|
|
#include "clang/Sema/SemaDiagnostic.h"
|
2013-02-06 00:37:00 +08:00
|
|
|
#include "clang/Serialization/ASTReader.h"
|
2011-06-16 07:25:17 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2011-06-16 07:25:17 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace arcmt;
|
|
|
|
|
2011-07-24 01:14:25 +08:00
|
|
|
bool CapturedDiagList::clearDiagnostic(ArrayRef<unsigned> IDs,
|
2011-06-16 07:25:17 +08:00
|
|
|
SourceRange range) {
|
|
|
|
if (range.isInvalid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool cleared = false;
|
|
|
|
ListTy::iterator I = List.begin();
|
|
|
|
while (I != List.end()) {
|
|
|
|
FullSourceLoc diagLoc = I->getLocation();
|
|
|
|
if ((IDs.empty() || // empty means clear all diagnostics in the range.
|
|
|
|
std::find(IDs.begin(), IDs.end(), I->getID()) != IDs.end()) &&
|
|
|
|
!diagLoc.isBeforeInTranslationUnitThan(range.getBegin()) &&
|
|
|
|
(diagLoc == range.getEnd() ||
|
|
|
|
diagLoc.isBeforeInTranslationUnitThan(range.getEnd()))) {
|
|
|
|
cleared = true;
|
|
|
|
ListTy::iterator eraseS = I++;
|
2013-01-05 02:30:08 +08:00
|
|
|
if (eraseS->getLevel() != DiagnosticsEngine::Note)
|
|
|
|
while (I != List.end() && I->getLevel() == DiagnosticsEngine::Note)
|
|
|
|
++I;
|
2011-06-16 07:25:17 +08:00
|
|
|
// Clear the diagnostic and any notes following it.
|
2012-09-01 01:28:09 +08:00
|
|
|
I = List.erase(eraseS, I);
|
2011-06-16 07:25:17 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cleared;
|
|
|
|
}
|
|
|
|
|
2011-07-24 01:14:25 +08:00
|
|
|
bool CapturedDiagList::hasDiagnostic(ArrayRef<unsigned> IDs,
|
2011-06-18 08:53:34 +08:00
|
|
|
SourceRange range) const {
|
2011-06-16 07:25:17 +08:00
|
|
|
if (range.isInvalid())
|
|
|
|
return false;
|
|
|
|
|
2011-06-18 08:53:34 +08:00
|
|
|
ListTy::const_iterator I = List.begin();
|
2011-06-16 07:25:17 +08:00
|
|
|
while (I != List.end()) {
|
|
|
|
FullSourceLoc diagLoc = I->getLocation();
|
|
|
|
if ((IDs.empty() || // empty means any diagnostic in the range.
|
|
|
|
std::find(IDs.begin(), IDs.end(), I->getID()) != IDs.end()) &&
|
|
|
|
!diagLoc.isBeforeInTranslationUnitThan(range.getBegin()) &&
|
|
|
|
(diagLoc == range.getEnd() ||
|
|
|
|
diagLoc.isBeforeInTranslationUnitThan(range.getEnd()))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-26 07:23:43 +08:00
|
|
|
void CapturedDiagList::reportDiagnostics(DiagnosticsEngine &Diags) const {
|
2011-06-18 08:53:34 +08:00
|
|
|
for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I)
|
2011-06-16 07:25:17 +08:00
|
|
|
Diags.Report(*I);
|
|
|
|
}
|
|
|
|
|
2011-06-18 08:53:41 +08:00
|
|
|
bool CapturedDiagList::hasErrors() const {
|
|
|
|
for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I)
|
2011-09-26 07:23:43 +08:00
|
|
|
if (I->getLevel() >= DiagnosticsEngine::Error)
|
2011-06-18 08:53:41 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-16 07:25:17 +08:00
|
|
|
namespace {
|
|
|
|
|
2011-09-26 07:54:33 +08:00
|
|
|
class CaptureDiagnosticConsumer : public DiagnosticConsumer {
|
2011-09-26 07:23:43 +08:00
|
|
|
DiagnosticsEngine &Diags;
|
2012-08-10 09:06:16 +08:00
|
|
|
DiagnosticConsumer &DiagClient;
|
2011-06-16 07:25:17 +08:00
|
|
|
CapturedDiagList &CapturedDiags;
|
2012-08-10 09:06:16 +08:00
|
|
|
bool HasBegunSourceFile;
|
2011-06-16 07:25:17 +08:00
|
|
|
public:
|
2011-09-26 07:54:33 +08:00
|
|
|
CaptureDiagnosticConsumer(DiagnosticsEngine &diags,
|
2012-08-10 09:06:16 +08:00
|
|
|
DiagnosticConsumer &client,
|
|
|
|
CapturedDiagList &capturedDiags)
|
|
|
|
: Diags(diags), DiagClient(client), CapturedDiags(capturedDiags),
|
|
|
|
HasBegunSourceFile(false) { }
|
|
|
|
|
2014-03-14 14:02:07 +08:00
|
|
|
void BeginSourceFile(const LangOptions &Opts,
|
|
|
|
const Preprocessor *PP) override {
|
2012-08-10 09:06:16 +08:00
|
|
|
// Pass BeginSourceFile message onto DiagClient on first call.
|
|
|
|
// The corresponding EndSourceFile call will be made from an
|
|
|
|
// explicit call to FinishCapture.
|
|
|
|
if (!HasBegunSourceFile) {
|
|
|
|
DiagClient.BeginSourceFile(Opts, PP);
|
|
|
|
HasBegunSourceFile = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FinishCapture() {
|
|
|
|
// Call EndSourceFile on DiagClient on completion of capture to
|
|
|
|
// enable VerifyDiagnosticConsumer to check diagnostics *after*
|
|
|
|
// it has received the diagnostic list.
|
|
|
|
if (HasBegunSourceFile) {
|
|
|
|
DiagClient.EndSourceFile();
|
|
|
|
HasBegunSourceFile = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~CaptureDiagnosticConsumer() {
|
|
|
|
assert(!HasBegunSourceFile && "FinishCapture not called!");
|
|
|
|
}
|
2011-06-16 07:25:17 +08:00
|
|
|
|
2014-03-14 14:02:07 +08:00
|
|
|
void HandleDiagnostic(DiagnosticsEngine::Level level,
|
|
|
|
const Diagnostic &Info) override {
|
2011-10-20 13:07:47 +08:00
|
|
|
if (DiagnosticIDs::isARCDiagnostic(Info.getID()) ||
|
2011-09-26 07:23:43 +08:00
|
|
|
level >= DiagnosticsEngine::Error || level == DiagnosticsEngine::Note) {
|
2012-12-13 06:48:28 +08:00
|
|
|
if (Info.getLocation().isValid())
|
|
|
|
CapturedDiags.push_back(StoredDiagnostic(level, Info));
|
2011-06-16 07:25:17 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Non-ARC warnings are ignored.
|
|
|
|
Diags.setLastDiagnosticIgnored();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2011-06-21 03:59:52 +08:00
|
|
|
static bool HasARCRuntime(CompilerInvocation &origCI) {
|
|
|
|
// This duplicates some functionality from Darwin::AddDeploymentTarget
|
|
|
|
// but this function is well defined, so keep it decoupled from the driver
|
|
|
|
// and avoid unrelated complications.
|
|
|
|
llvm::Triple triple(origCI.getTargetOpts().Triple);
|
|
|
|
|
2013-09-14 09:09:11 +08:00
|
|
|
if (triple.isiOS())
|
2011-06-21 03:59:52 +08:00
|
|
|
return triple.getOSMajorVersion() >= 5;
|
|
|
|
|
|
|
|
if (triple.getOS() == llvm::Triple::Darwin)
|
|
|
|
return triple.getOSMajorVersion() >= 11;
|
|
|
|
|
|
|
|
if (triple.getOS() == llvm::Triple::MacOSX) {
|
|
|
|
unsigned Major, Minor, Micro;
|
|
|
|
triple.getOSVersion(Major, Minor, Micro);
|
|
|
|
return Major > 10 || (Major == 10 && Minor >= 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-08-02 12:50:49 +08:00
|
|
|
static CompilerInvocation *
|
|
|
|
createInvocationForMigration(CompilerInvocation &origCI) {
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<CompilerInvocation> CInvok;
|
2011-06-16 07:25:17 +08:00
|
|
|
CInvok.reset(new CompilerInvocation(origCI));
|
2013-02-06 00:37:00 +08:00
|
|
|
PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts();
|
|
|
|
if (!PPOpts.ImplicitPCHInclude.empty()) {
|
|
|
|
// We can't use a PCH because it was likely built in non-ARC mode and we
|
|
|
|
// want to parse in ARC. Include the original header.
|
|
|
|
FileManager FileMgr(origCI.getFileSystemOpts());
|
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
|
|
|
|
new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
|
|
|
|
new IgnoringDiagConsumer()));
|
|
|
|
std::string OriginalFile =
|
|
|
|
ASTReader::getOriginalSourceFile(PPOpts.ImplicitPCHInclude,
|
|
|
|
FileMgr, *Diags);
|
|
|
|
if (!OriginalFile.empty())
|
|
|
|
PPOpts.Includes.insert(PPOpts.Includes.begin(), OriginalFile);
|
|
|
|
PPOpts.ImplicitPCHInclude.clear();
|
|
|
|
}
|
|
|
|
// FIXME: Get the original header of a PTH as well.
|
|
|
|
CInvok->getPreprocessorOpts().ImplicitPTHInclude.clear();
|
2011-06-16 07:25:17 +08:00
|
|
|
std::string define = getARCMTMacroName();
|
|
|
|
define += '=';
|
|
|
|
CInvok->getPreprocessorOpts().addMacroDef(define);
|
2011-11-18 07:01:24 +08:00
|
|
|
CInvok->getLangOpts()->ObjCAutoRefCount = true;
|
|
|
|
CInvok->getLangOpts()->setGC(LangOptions::NonGC);
|
2011-06-16 07:25:17 +08:00
|
|
|
CInvok->getDiagnosticOpts().ErrorLimit = 0;
|
2012-06-20 09:46:26 +08:00
|
|
|
CInvok->getDiagnosticOpts().PedanticErrors = 0;
|
2012-06-20 09:10:40 +08:00
|
|
|
|
|
|
|
// Ignore -Werror flags when migrating.
|
|
|
|
std::vector<std::string> WarnOpts;
|
|
|
|
for (std::vector<std::string>::iterator
|
|
|
|
I = CInvok->getDiagnosticOpts().Warnings.begin(),
|
|
|
|
E = CInvok->getDiagnosticOpts().Warnings.end(); I != E; ++I) {
|
|
|
|
if (!StringRef(*I).startswith("error"))
|
|
|
|
WarnOpts.push_back(*I);
|
|
|
|
}
|
|
|
|
WarnOpts.push_back("error=arc-unsafe-retained-assign");
|
2014-03-02 12:02:40 +08:00
|
|
|
CInvok->getDiagnosticOpts().Warnings = std::move(WarnOpts);
|
2012-06-20 09:10:40 +08:00
|
|
|
|
2012-08-21 10:47:43 +08:00
|
|
|
CInvok->getLangOpts()->ObjCARCWeak = HasARCRuntime(origCI);
|
2011-06-16 07:25:17 +08:00
|
|
|
|
2014-03-08 03:33:25 +08:00
|
|
|
return CInvok.release();
|
2011-06-16 07:25:17 +08:00
|
|
|
}
|
|
|
|
|
2011-08-02 12:50:49 +08:00
|
|
|
static void emitPremigrationErrors(const CapturedDiagList &arcDiags,
|
2012-10-24 06:26:28 +08:00
|
|
|
DiagnosticOptions *diagOpts,
|
2011-08-02 12:50:49 +08:00
|
|
|
Preprocessor &PP) {
|
2011-07-20 01:20:03 +08:00
|
|
|
TextDiagnosticPrinter printer(llvm::errs(), diagOpts);
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
|
2012-10-24 06:26:28 +08:00
|
|
|
new DiagnosticsEngine(DiagID, diagOpts, &printer,
|
|
|
|
/*ShouldOwnClient=*/false));
|
2011-07-20 01:20:03 +08:00
|
|
|
Diags->setSourceManager(&PP.getSourceManager());
|
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
printer.BeginSourceFile(PP.getLangOpts(), &PP);
|
2011-07-20 01:20:03 +08:00
|
|
|
arcDiags.reportDiagnostics(*Diags);
|
|
|
|
printer.EndSourceFile();
|
|
|
|
}
|
|
|
|
|
2011-06-16 07:25:17 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// checkForManualIssues.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
bool arcmt::checkForManualIssues(CompilerInvocation &origCI,
|
2012-01-21 00:28:04 +08:00
|
|
|
const FrontendInputFile &Input,
|
2011-09-26 07:39:51 +08:00
|
|
|
DiagnosticConsumer *DiagClient,
|
2011-07-20 01:20:03 +08:00
|
|
|
bool emitPremigrationARCErrors,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef plistOut) {
|
2011-11-18 07:01:24 +08:00
|
|
|
if (!origCI.getLangOpts()->ObjC1)
|
2011-06-16 07:25:17 +08:00
|
|
|
return false;
|
|
|
|
|
2011-11-18 07:01:24 +08:00
|
|
|
LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC();
|
2012-01-25 08:20:29 +08:00
|
|
|
bool NoNSAllocReallocError = origCI.getMigratorOpts().NoNSAllocReallocError;
|
2012-01-26 08:08:04 +08:00
|
|
|
bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval;
|
2011-11-04 23:58:08 +08:00
|
|
|
|
2012-01-27 04:57:58 +08:00
|
|
|
std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode,
|
|
|
|
NoFinalizeRemoval);
|
2011-06-16 07:25:17 +08:00
|
|
|
assert(!transforms.empty());
|
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<CompilerInvocation> CInvok;
|
2011-06-16 07:25:17 +08:00
|
|
|
CInvok.reset(createInvocationForMigration(origCI));
|
|
|
|
CInvok->getFrontendOpts().Inputs.clear();
|
2012-01-21 00:28:04 +08:00
|
|
|
CInvok->getFrontendOpts().Inputs.push_back(Input);
|
2011-06-16 07:25:17 +08:00
|
|
|
|
|
|
|
CapturedDiagList capturedDiags;
|
|
|
|
|
|
|
|
assert(DiagClient);
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
|
2012-10-24 06:26:28 +08:00
|
|
|
new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
|
|
|
|
DiagClient, /*ShouldOwnClient=*/false));
|
2011-06-16 07:25:17 +08:00
|
|
|
|
|
|
|
// Filter of all diagnostics.
|
2012-08-10 09:06:16 +08:00
|
|
|
CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags);
|
2011-06-16 07:25:17 +08:00
|
|
|
Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
|
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<ASTUnit> Unit(
|
2014-03-08 03:33:25 +08:00
|
|
|
ASTUnit::LoadFromCompilerInvocationAction(CInvok.release(), Diags));
|
2012-08-10 09:06:16 +08:00
|
|
|
if (!Unit) {
|
|
|
|
errRec.FinishCapture();
|
2011-06-16 07:25:17 +08:00
|
|
|
return true;
|
2012-08-10 09:06:16 +08:00
|
|
|
}
|
2011-06-16 07:25:17 +08:00
|
|
|
|
|
|
|
// Don't filter diagnostics anymore.
|
|
|
|
Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
|
|
|
|
|
|
|
|
ASTContext &Ctx = Unit->getASTContext();
|
|
|
|
|
|
|
|
if (Diags->hasFatalErrorOccurred()) {
|
|
|
|
Diags->Reset();
|
2012-03-11 15:00:24 +08:00
|
|
|
DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
|
2011-06-16 07:25:17 +08:00
|
|
|
capturedDiags.reportDiagnostics(*Diags);
|
|
|
|
DiagClient->EndSourceFile();
|
2012-08-10 09:06:16 +08:00
|
|
|
errRec.FinishCapture();
|
2011-06-16 07:25:17 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-20 01:20:03 +08:00
|
|
|
if (emitPremigrationARCErrors)
|
2012-10-24 06:26:28 +08:00
|
|
|
emitPremigrationErrors(capturedDiags, &origCI.getDiagnosticOpts(),
|
2011-07-20 01:20:03 +08:00
|
|
|
Unit->getPreprocessor());
|
|
|
|
if (!plistOut.empty()) {
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<StoredDiagnostic, 8> arcDiags;
|
2011-07-20 01:20:03 +08:00
|
|
|
for (CapturedDiagList::iterator
|
|
|
|
I = capturedDiags.begin(), E = capturedDiags.end(); I != E; ++I)
|
|
|
|
arcDiags.push_back(*I);
|
|
|
|
writeARCDiagsToPlist(plistOut, arcDiags,
|
2012-03-11 15:00:24 +08:00
|
|
|
Ctx.getSourceManager(), Ctx.getLangOpts());
|
2011-07-20 01:20:03 +08:00
|
|
|
}
|
|
|
|
|
2011-06-16 07:25:17 +08:00
|
|
|
// After parsing of source files ended, we want to reuse the
|
|
|
|
// diagnostics objects to emit further diagnostics.
|
2011-09-26 07:39:51 +08:00
|
|
|
// We call BeginSourceFile because DiagnosticConsumer requires that
|
2011-06-16 07:25:17 +08:00
|
|
|
// diagnostics with source range information are emitted only in between
|
|
|
|
// BeginSourceFile() and EndSourceFile().
|
2012-03-11 15:00:24 +08:00
|
|
|
DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
|
2011-06-16 07:25:17 +08:00
|
|
|
|
|
|
|
// No macros will be added since we are just checking and we won't modify
|
|
|
|
// source code.
|
|
|
|
std::vector<SourceLocation> ARCMTMacroLocs;
|
|
|
|
|
|
|
|
TransformActions testAct(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
|
2013-01-05 02:30:08 +08:00
|
|
|
MigrationPass pass(Ctx, OrigGCMode, Unit->getSema(), testAct, capturedDiags,
|
|
|
|
ARCMTMacroLocs);
|
2012-01-26 08:08:04 +08:00
|
|
|
pass.setNoFinalizeRemoval(NoFinalizeRemoval);
|
2014-05-20 07:48:49 +08:00
|
|
|
if (!NoNSAllocReallocError)
|
2014-05-20 06:51:11 +08:00
|
|
|
Diags->setDiagnosticMapping(diag::warn_arcmt_nsalloc_realloc,
|
2014-05-20 07:48:49 +08:00
|
|
|
diag::MAP_ERROR, SourceLocation());
|
2011-06-16 07:25:17 +08:00
|
|
|
|
|
|
|
for (unsigned i=0, e = transforms.size(); i != e; ++i)
|
|
|
|
transforms[i](pass);
|
|
|
|
|
|
|
|
capturedDiags.reportDiagnostics(*Diags);
|
|
|
|
|
|
|
|
DiagClient->EndSourceFile();
|
2012-08-10 09:06:16 +08:00
|
|
|
errRec.FinishCapture();
|
2011-06-16 07:25:17 +08:00
|
|
|
|
2011-07-18 15:44:45 +08:00
|
|
|
return capturedDiags.hasErrors() || testAct.hasReportedErrors();
|
2011-06-16 07:25:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// applyTransformations.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-07-10 04:00:58 +08:00
|
|
|
static bool applyTransforms(CompilerInvocation &origCI,
|
2012-01-21 00:28:04 +08:00
|
|
|
const FrontendInputFile &Input,
|
2011-09-26 07:39:51 +08:00
|
|
|
DiagnosticConsumer *DiagClient,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef outputDir,
|
2011-07-20 01:20:03 +08:00
|
|
|
bool emitPremigrationARCErrors,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef plistOut) {
|
2011-11-18 07:01:24 +08:00
|
|
|
if (!origCI.getLangOpts()->ObjC1)
|
2011-06-16 07:25:17 +08:00
|
|
|
return false;
|
|
|
|
|
2011-11-18 07:01:24 +08:00
|
|
|
LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC();
|
2011-11-04 23:58:08 +08:00
|
|
|
|
2011-06-16 07:25:17 +08:00
|
|
|
// Make sure checking is successful first.
|
|
|
|
CompilerInvocation CInvokForCheck(origCI);
|
2012-01-21 00:28:04 +08:00
|
|
|
if (arcmt::checkForManualIssues(CInvokForCheck, Input, DiagClient,
|
2011-07-20 01:20:03 +08:00
|
|
|
emitPremigrationARCErrors, plistOut))
|
2011-06-16 07:25:17 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
CompilerInvocation CInvok(origCI);
|
|
|
|
CInvok.getFrontendOpts().Inputs.clear();
|
2012-01-21 00:28:04 +08:00
|
|
|
CInvok.getFrontendOpts().Inputs.push_back(Input);
|
2011-06-16 07:25:17 +08:00
|
|
|
|
2011-07-10 04:00:58 +08:00
|
|
|
MigrationProcess migration(CInvok, DiagClient, outputDir);
|
2012-01-27 04:57:58 +08:00
|
|
|
bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval;
|
2011-06-16 07:25:17 +08:00
|
|
|
|
2012-01-27 04:57:58 +08:00
|
|
|
std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode,
|
|
|
|
NoFinalizeRemoval);
|
2011-06-16 07:25:17 +08:00
|
|
|
assert(!transforms.empty());
|
|
|
|
|
|
|
|
for (unsigned i=0, e = transforms.size(); i != e; ++i) {
|
|
|
|
bool err = migration.applyTransform(transforms[i]);
|
|
|
|
if (err) return true;
|
|
|
|
}
|
|
|
|
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
|
2012-10-24 06:26:28 +08:00
|
|
|
new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
|
|
|
|
DiagClient, /*ShouldOwnClient=*/false));
|
2011-07-10 04:00:58 +08:00
|
|
|
|
|
|
|
if (outputDir.empty()) {
|
2011-11-18 07:01:24 +08:00
|
|
|
origCI.getLangOpts()->ObjCAutoRefCount = true;
|
2011-07-10 04:00:58 +08:00
|
|
|
return migration.getRemapper().overwriteOriginal(*Diags);
|
2011-07-14 08:17:54 +08:00
|
|
|
} else {
|
2011-07-10 04:00:58 +08:00
|
|
|
return migration.getRemapper().flushToDisk(outputDir, *Diags);
|
2011-07-14 08:17:54 +08:00
|
|
|
}
|
2011-07-10 04:00:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool arcmt::applyTransformations(CompilerInvocation &origCI,
|
2012-01-21 00:28:04 +08:00
|
|
|
const FrontendInputFile &Input,
|
2011-09-26 07:39:51 +08:00
|
|
|
DiagnosticConsumer *DiagClient) {
|
2012-01-21 00:28:04 +08:00
|
|
|
return applyTransforms(origCI, Input, DiagClient,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef(), false, StringRef());
|
2011-07-10 04:00:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool arcmt::migrateWithTemporaryFiles(CompilerInvocation &origCI,
|
2012-01-21 00:28:04 +08:00
|
|
|
const FrontendInputFile &Input,
|
2011-09-26 07:39:51 +08:00
|
|
|
DiagnosticConsumer *DiagClient,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef outputDir,
|
2011-07-20 01:20:03 +08:00
|
|
|
bool emitPremigrationARCErrors,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef plistOut) {
|
2011-07-10 04:00:58 +08:00
|
|
|
assert(!outputDir.empty() && "Expected output directory path");
|
2012-01-21 00:28:04 +08:00
|
|
|
return applyTransforms(origCI, Input, DiagClient,
|
2011-07-20 01:20:03 +08:00
|
|
|
outputDir, emitPremigrationARCErrors, plistOut);
|
2011-07-10 04:00:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool arcmt::getFileRemappings(std::vector<std::pair<std::string,std::string> > &
|
|
|
|
remap,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef outputDir,
|
2011-09-26 07:39:51 +08:00
|
|
|
DiagnosticConsumer *DiagClient) {
|
2011-07-10 04:00:58 +08:00
|
|
|
assert(!outputDir.empty());
|
2011-06-16 07:25:17 +08:00
|
|
|
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
|
2012-10-24 06:26:28 +08:00
|
|
|
new DiagnosticsEngine(DiagID, new DiagnosticOptions,
|
|
|
|
DiagClient, /*ShouldOwnClient=*/false));
|
2011-07-10 04:00:58 +08:00
|
|
|
|
|
|
|
FileRemapper remapper;
|
|
|
|
bool err = remapper.initFromDisk(outputDir, *Diags,
|
|
|
|
/*ignoreIfFilesChanged=*/true);
|
|
|
|
if (err)
|
|
|
|
return true;
|
|
|
|
|
2012-03-07 04:06:33 +08:00
|
|
|
PreprocessorOptions PPOpts;
|
|
|
|
remapper.applyMappings(PPOpts);
|
|
|
|
remap = PPOpts.RemappedFiles;
|
2011-07-10 04:00:58 +08:00
|
|
|
|
|
|
|
return false;
|
2011-06-16 07:25:17 +08:00
|
|
|
}
|
|
|
|
|
2012-03-07 04:06:33 +08:00
|
|
|
|
2011-06-16 07:25:17 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CollectTransformActions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ARCMTMacroTrackerPPCallbacks : public PPCallbacks {
|
|
|
|
std::vector<SourceLocation> &ARCMTMacroLocs;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs)
|
|
|
|
: ARCMTMacroLocs(ARCMTMacroLocs) { }
|
|
|
|
|
2014-03-14 14:02:07 +08:00
|
|
|
void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
|
|
|
|
SourceRange Range, const MacroArgs *Args) override {
|
2011-06-16 07:25:17 +08:00
|
|
|
if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName())
|
|
|
|
ARCMTMacroLocs.push_back(MacroNameTok.getLocation());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ARCMTMacroTrackerAction : public ASTFrontendAction {
|
|
|
|
std::vector<SourceLocation> &ARCMTMacroLocs;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ARCMTMacroTrackerAction(std::vector<SourceLocation> &ARCMTMacroLocs)
|
|
|
|
: ARCMTMacroLocs(ARCMTMacroLocs) { }
|
|
|
|
|
2014-03-14 14:02:07 +08:00
|
|
|
ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
|
|
|
|
StringRef InFile) override {
|
2011-06-16 07:25:17 +08:00
|
|
|
CI.getPreprocessor().addPPCallbacks(
|
|
|
|
new ARCMTMacroTrackerPPCallbacks(ARCMTMacroLocs));
|
|
|
|
return new ASTConsumer();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class RewritesApplicator : public TransformActions::RewriteReceiver {
|
|
|
|
Rewriter &rewriter;
|
|
|
|
MigrationProcess::RewriteListener *Listener;
|
|
|
|
|
|
|
|
public:
|
|
|
|
RewritesApplicator(Rewriter &rewriter, ASTContext &ctx,
|
|
|
|
MigrationProcess::RewriteListener *listener)
|
2012-06-07 01:32:50 +08:00
|
|
|
: rewriter(rewriter), Listener(listener) {
|
2011-06-16 07:25:17 +08:00
|
|
|
if (Listener)
|
|
|
|
Listener->start(ctx);
|
|
|
|
}
|
|
|
|
~RewritesApplicator() {
|
|
|
|
if (Listener)
|
|
|
|
Listener->finish();
|
|
|
|
}
|
|
|
|
|
2014-03-14 14:02:07 +08:00
|
|
|
void insert(SourceLocation loc, StringRef text) override {
|
2011-06-16 07:25:17 +08:00
|
|
|
bool err = rewriter.InsertText(loc, text, /*InsertAfter=*/true,
|
|
|
|
/*indentNewLines=*/true);
|
|
|
|
if (!err && Listener)
|
|
|
|
Listener->insert(loc, text);
|
|
|
|
}
|
|
|
|
|
2014-03-14 14:02:07 +08:00
|
|
|
void remove(CharSourceRange range) override {
|
2011-06-16 07:25:17 +08:00
|
|
|
Rewriter::RewriteOptions removeOpts;
|
|
|
|
removeOpts.IncludeInsertsAtBeginOfRange = false;
|
|
|
|
removeOpts.IncludeInsertsAtEndOfRange = false;
|
|
|
|
removeOpts.RemoveLineIfEmpty = true;
|
|
|
|
|
|
|
|
bool err = rewriter.RemoveText(range, removeOpts);
|
|
|
|
if (!err && Listener)
|
|
|
|
Listener->remove(range);
|
|
|
|
}
|
|
|
|
|
2014-03-14 14:02:07 +08:00
|
|
|
void increaseIndentation(CharSourceRange range,
|
|
|
|
SourceLocation parentIndent) override {
|
2011-06-16 07:25:17 +08:00
|
|
|
rewriter.IncreaseIndentation(range, parentIndent);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace.
|
|
|
|
|
|
|
|
/// \brief Anchor for VTable.
|
|
|
|
MigrationProcess::RewriteListener::~RewriteListener() { }
|
|
|
|
|
2011-07-10 04:00:58 +08:00
|
|
|
MigrationProcess::MigrationProcess(const CompilerInvocation &CI,
|
2011-09-26 07:39:51 +08:00
|
|
|
DiagnosticConsumer *diagClient,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef outputDir)
|
2013-07-23 02:13:54 +08:00
|
|
|
: OrigCI(CI), DiagClient(diagClient), HadARCErrors(false) {
|
2011-07-10 04:00:58 +08:00
|
|
|
if (!outputDir.empty()) {
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
|
2012-10-24 06:26:28 +08:00
|
|
|
new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(),
|
|
|
|
DiagClient, /*ShouldOwnClient=*/false));
|
2011-07-10 04:00:58 +08:00
|
|
|
Remapper.initFromDisk(outputDir, *Diags, /*ignoreIfFilesChanges=*/true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-16 07:25:17 +08:00
|
|
|
bool MigrationProcess::applyTransform(TransformFn trans,
|
|
|
|
RewriteListener *listener) {
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<CompilerInvocation> CInvok;
|
2011-06-16 07:25:17 +08:00
|
|
|
CInvok.reset(createInvocationForMigration(OrigCI));
|
|
|
|
CInvok->getDiagnosticOpts().IgnoreWarnings = true;
|
|
|
|
|
2012-03-07 04:06:33 +08:00
|
|
|
Remapper.applyMappings(CInvok->getPreprocessorOpts());
|
2011-06-16 07:25:17 +08:00
|
|
|
|
|
|
|
CapturedDiagList capturedDiags;
|
|
|
|
std::vector<SourceLocation> ARCMTMacroLocs;
|
|
|
|
|
|
|
|
assert(DiagClient);
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
|
2012-10-24 06:26:28 +08:00
|
|
|
new DiagnosticsEngine(DiagID, new DiagnosticOptions,
|
|
|
|
DiagClient, /*ShouldOwnClient=*/false));
|
2011-06-16 07:25:17 +08:00
|
|
|
|
|
|
|
// Filter of all diagnostics.
|
2012-08-10 09:06:16 +08:00
|
|
|
CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags);
|
2011-06-16 07:25:17 +08:00
|
|
|
Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
|
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<ARCMTMacroTrackerAction> ASTAction;
|
2011-06-16 07:25:17 +08:00
|
|
|
ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs));
|
|
|
|
|
2014-03-09 19:34:25 +08:00
|
|
|
std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCompilerInvocationAction(
|
2014-03-08 03:33:25 +08:00
|
|
|
CInvok.release(), Diags, ASTAction.get()));
|
2012-08-10 09:06:16 +08:00
|
|
|
if (!Unit) {
|
|
|
|
errRec.FinishCapture();
|
2011-06-16 07:25:17 +08:00
|
|
|
return true;
|
2012-08-10 09:06:16 +08:00
|
|
|
}
|
2011-06-16 07:25:17 +08:00
|
|
|
Unit->setOwnsRemappedFileBuffers(false); // FileRemapper manages that.
|
|
|
|
|
2013-07-23 02:13:54 +08:00
|
|
|
HadARCErrors = HadARCErrors || capturedDiags.hasErrors();
|
|
|
|
|
2011-06-16 07:25:17 +08:00
|
|
|
// Don't filter diagnostics anymore.
|
|
|
|
Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
|
|
|
|
|
|
|
|
ASTContext &Ctx = Unit->getASTContext();
|
|
|
|
|
|
|
|
if (Diags->hasFatalErrorOccurred()) {
|
|
|
|
Diags->Reset();
|
2012-03-11 15:00:24 +08:00
|
|
|
DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
|
2011-06-16 07:25:17 +08:00
|
|
|
capturedDiags.reportDiagnostics(*Diags);
|
|
|
|
DiagClient->EndSourceFile();
|
2012-08-10 09:06:16 +08:00
|
|
|
errRec.FinishCapture();
|
2011-06-16 07:25:17 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// After parsing of source files ended, we want to reuse the
|
|
|
|
// diagnostics objects to emit further diagnostics.
|
2011-09-26 07:39:51 +08:00
|
|
|
// We call BeginSourceFile because DiagnosticConsumer requires that
|
2011-06-16 07:25:17 +08:00
|
|
|
// diagnostics with source range information are emitted only in between
|
|
|
|
// BeginSourceFile() and EndSourceFile().
|
2012-03-11 15:00:24 +08:00
|
|
|
DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
|
2011-06-16 07:25:17 +08:00
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts());
|
2011-06-16 07:25:17 +08:00
|
|
|
TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
|
2011-11-18 07:01:24 +08:00
|
|
|
MigrationPass pass(Ctx, OrigCI.getLangOpts()->getGC(),
|
2013-01-05 02:30:08 +08:00
|
|
|
Unit->getSema(), TA, capturedDiags, ARCMTMacroLocs);
|
2011-06-16 07:25:17 +08:00
|
|
|
|
|
|
|
trans(pass);
|
|
|
|
|
|
|
|
{
|
|
|
|
RewritesApplicator applicator(rewriter, Ctx, listener);
|
|
|
|
TA.applyRewrites(applicator);
|
|
|
|
}
|
|
|
|
|
|
|
|
DiagClient->EndSourceFile();
|
2012-08-10 09:06:16 +08:00
|
|
|
errRec.FinishCapture();
|
2011-06-16 07:25:17 +08:00
|
|
|
|
|
|
|
if (DiagClient->getNumErrors())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (Rewriter::buffer_iterator
|
|
|
|
I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) {
|
|
|
|
FileID FID = I->first;
|
|
|
|
RewriteBuffer &buf = I->second;
|
|
|
|
const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID);
|
|
|
|
assert(file);
|
|
|
|
std::string newFname = file->getName();
|
|
|
|
newFname += "-trans";
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<512> newText;
|
2011-06-16 07:25:17 +08:00
|
|
|
llvm::raw_svector_ostream vecOS(newText);
|
|
|
|
buf.write(vecOS);
|
|
|
|
vecOS.flush();
|
|
|
|
llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy(
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef(newText.data(), newText.size()), newFname);
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<64> filePath(file->getName());
|
2011-06-16 07:25:17 +08:00
|
|
|
Unit->getFileManager().FixupRelativePath(filePath);
|
|
|
|
Remapper.remap(filePath.str(), memBuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|