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"
|
|
|
|
#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"
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/Rewrite/Rewriter.h"
|
|
|
|
#include "clang/Sema/SemaDiagnostic.h"
|
|
|
|
#include "clang/Basic/DiagnosticCategories.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/ADT/Triple.h"
|
|
|
|
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++;
|
2011-09-26 07:23:43 +08:00
|
|
|
while (I != List.end() && I->getLevel() == DiagnosticsEngine::Note)
|
2011-06-16 07:25:17 +08:00
|
|
|
++I;
|
|
|
|
// Clear the diagnostic and any notes following it.
|
|
|
|
List.erase(eraseS, I);
|
|
|
|
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;
|
2011-06-16 07:25:17 +08:00
|
|
|
CapturedDiagList &CapturedDiags;
|
|
|
|
public:
|
2011-09-26 07:54:33 +08:00
|
|
|
CaptureDiagnosticConsumer(DiagnosticsEngine &diags,
|
2011-09-29 08:38:00 +08:00
|
|
|
CapturedDiagList &capturedDiags)
|
2011-06-16 07:25:17 +08:00
|
|
|
: Diags(diags), CapturedDiags(capturedDiags) { }
|
|
|
|
|
2011-09-26 07:23:43 +08:00
|
|
|
virtual void HandleDiagnostic(DiagnosticsEngine::Level level,
|
2011-09-26 09:18:08 +08:00
|
|
|
const Diagnostic &Info) {
|
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) {
|
2011-06-16 07:25:17 +08:00
|
|
|
CapturedDiags.push_back(StoredDiagnostic(level, Info));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Non-ARC warnings are ignored.
|
|
|
|
Diags.setLastDiagnosticIgnored();
|
|
|
|
}
|
2011-09-29 08:38:00 +08:00
|
|
|
|
|
|
|
DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
|
|
|
|
// Just drop any diagnostics that come from cloned consumers; they'll
|
|
|
|
// have different source managers anyway.
|
|
|
|
return new IgnoringDiagConsumer();
|
|
|
|
}
|
2011-06-16 07:25:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2011-10-18 08:22:49 +08:00
|
|
|
static inline StringRef SimulatorVersionDefineName() {
|
|
|
|
return "__IPHONE_OS_VERSION_MIN_REQUIRED=";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Parse the simulator version define:
|
|
|
|
/// __IPHONE_OS_VERSION_MIN_REQUIRED=([0-9])([0-9][0-9])([0-9][0-9])
|
|
|
|
// and return the grouped values as integers, e.g:
|
|
|
|
// __IPHONE_OS_VERSION_MIN_REQUIRED=40201
|
|
|
|
// will return Major=4, Minor=2, Micro=1.
|
|
|
|
static bool GetVersionFromSimulatorDefine(StringRef define,
|
|
|
|
unsigned &Major, unsigned &Minor,
|
|
|
|
unsigned &Micro) {
|
|
|
|
assert(define.startswith(SimulatorVersionDefineName()));
|
|
|
|
StringRef name, version;
|
|
|
|
llvm::tie(name, version) = define.split('=');
|
|
|
|
if (version.empty())
|
|
|
|
return false;
|
|
|
|
std::string verstr = version.str();
|
|
|
|
char *end;
|
|
|
|
unsigned num = (unsigned) strtol(verstr.c_str(), &end, 10);
|
|
|
|
if (*end != '\0')
|
|
|
|
return false;
|
|
|
|
Major = num / 10000;
|
|
|
|
num = num % 10000;
|
|
|
|
Minor = num / 100;
|
|
|
|
Micro = num % 100;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
|
2011-10-18 08:22:49 +08:00
|
|
|
for (unsigned i = 0, e = origCI.getPreprocessorOpts().Macros.size();
|
|
|
|
i != e; ++i) {
|
|
|
|
StringRef define = origCI.getPreprocessorOpts().Macros[i].first;
|
|
|
|
bool isUndef = origCI.getPreprocessorOpts().Macros[i].second;
|
|
|
|
if (isUndef)
|
|
|
|
continue;
|
|
|
|
if (!define.startswith(SimulatorVersionDefineName()))
|
|
|
|
continue;
|
|
|
|
unsigned Major = 0, Minor = 0, Micro = 0;
|
|
|
|
if (GetVersionFromSimulatorDefine(define, Major, Minor, Micro) &&
|
|
|
|
Major < 10 && Minor < 100 && Micro < 100)
|
|
|
|
return Major >= 5;
|
|
|
|
}
|
|
|
|
|
2011-06-21 03:59:52 +08:00
|
|
|
llvm::Triple triple(origCI.getTargetOpts().Triple);
|
|
|
|
|
|
|
|
if (triple.getOS() == llvm::Triple::IOS)
|
|
|
|
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) {
|
2011-06-16 07:25:17 +08:00
|
|
|
llvm::OwningPtr<CompilerInvocation> CInvok;
|
|
|
|
CInvok.reset(new CompilerInvocation(origCI));
|
|
|
|
CInvok->getPreprocessorOpts().ImplicitPCHInclude = std::string();
|
|
|
|
CInvok->getPreprocessorOpts().ImplicitPTHInclude = std::string();
|
|
|
|
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;
|
2011-06-23 02:03:59 +08:00
|
|
|
CInvok->getDiagnosticOpts().Warnings.push_back(
|
|
|
|
"error=arc-unsafe-retained-assign");
|
2011-11-18 07:01:24 +08:00
|
|
|
CInvok->getLangOpts()->ObjCRuntimeHasWeak = HasARCRuntime(origCI);
|
2011-06-16 07:25:17 +08:00
|
|
|
|
|
|
|
return CInvok.take();
|
|
|
|
}
|
|
|
|
|
2011-08-02 12:50:49 +08:00
|
|
|
static void emitPremigrationErrors(const CapturedDiagList &arcDiags,
|
|
|
|
const DiagnosticOptions &diagOpts,
|
|
|
|
Preprocessor &PP) {
|
2011-07-20 01:20:03 +08:00
|
|
|
TextDiagnosticPrinter printer(llvm::errs(), diagOpts);
|
|
|
|
llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
2011-09-26 07:23:43 +08:00
|
|
|
llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
|
|
|
|
new DiagnosticsEngine(DiagID, &printer, /*ShouldOwnClient=*/false));
|
2011-07-20 01:20:03 +08:00
|
|
|
Diags->setSourceManager(&PP.getSourceManager());
|
|
|
|
|
|
|
|
printer.BeginSourceFile(PP.getLangOptions(), &PP);
|
|
|
|
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());
|
|
|
|
|
|
|
|
llvm::OwningPtr<CompilerInvocation> CInvok;
|
|
|
|
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);
|
|
|
|
llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
2011-09-26 07:23:43 +08:00
|
|
|
llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
|
|
|
|
new DiagnosticsEngine(DiagID, DiagClient, /*ShouldOwnClient=*/false));
|
2011-06-16 07:25:17 +08:00
|
|
|
|
|
|
|
// Filter of all diagnostics.
|
2011-09-26 07:54:33 +08:00
|
|
|
CaptureDiagnosticConsumer errRec(*Diags, capturedDiags);
|
2011-06-16 07:25:17 +08:00
|
|
|
Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
|
|
|
|
|
|
|
|
llvm::OwningPtr<ASTUnit> Unit(
|
|
|
|
ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags));
|
|
|
|
if (!Unit)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Don't filter diagnostics anymore.
|
|
|
|
Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
|
|
|
|
|
|
|
|
ASTContext &Ctx = Unit->getASTContext();
|
|
|
|
|
|
|
|
if (Diags->hasFatalErrorOccurred()) {
|
|
|
|
Diags->Reset();
|
|
|
|
DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor());
|
|
|
|
capturedDiags.reportDiagnostics(*Diags);
|
|
|
|
DiagClient->EndSourceFile();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-20 01:20:03 +08:00
|
|
|
if (emitPremigrationARCErrors)
|
|
|
|
emitPremigrationErrors(capturedDiags, origCI.getDiagnosticOpts(),
|
|
|
|
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,
|
|
|
|
Ctx.getSourceManager(), Ctx.getLangOptions());
|
|
|
|
}
|
|
|
|
|
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().
|
|
|
|
DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor());
|
|
|
|
|
|
|
|
// 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());
|
2011-11-04 23:58:08 +08:00
|
|
|
MigrationPass pass(Ctx, OrigGCMode, Unit->getSema(), testAct, ARCMTMacroLocs);
|
2012-01-25 08:20:29 +08:00
|
|
|
pass.setNSAllocReallocError(NoNSAllocReallocError);
|
2012-01-26 08:08:04 +08:00
|
|
|
pass.setNoFinalizeRemoval(NoFinalizeRemoval);
|
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();
|
|
|
|
|
2011-07-14 08:17:54 +08:00
|
|
|
// If we are migrating code that gets the '-fobjc-arc' flag, make sure
|
|
|
|
// to remove it so that we don't get errors from normal compilation.
|
2011-11-18 07:01:24 +08:00
|
|
|
origCI.getLangOpts()->ObjCAutoRefCount = false;
|
2011-07-14 08:17:54 +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;
|
|
|
|
}
|
|
|
|
|
2011-07-10 04:00:58 +08:00
|
|
|
llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
2011-09-26 07:23:43 +08:00
|
|
|
llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
|
|
|
|
new DiagnosticsEngine(DiagID, 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 {
|
|
|
|
// If we are migrating code that gets the '-fobjc-arc' flag, make sure
|
|
|
|
// to remove it so that we don't get errors from normal compilation.
|
2011-11-18 07:01:24 +08:00
|
|
|
origCI.getLangOpts()->ObjCAutoRefCount = false;
|
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
|
|
|
|
|
|
|
llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
2011-09-26 07:23:43 +08:00
|
|
|
llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
|
|
|
|
new DiagnosticsEngine(DiagID, 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;
|
|
|
|
|
|
|
|
CompilerInvocation CI;
|
|
|
|
remapper.applyMappings(CI);
|
|
|
|
remap = CI.getPreprocessorOpts().RemappedFiles;
|
|
|
|
|
|
|
|
return false;
|
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) { }
|
|
|
|
|
2011-08-18 09:05:45 +08:00
|
|
|
virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo *MI,
|
|
|
|
SourceRange Range) {
|
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) { }
|
|
|
|
|
|
|
|
virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef InFile) {
|
2011-06-16 07:25:17 +08:00
|
|
|
CI.getPreprocessor().addPPCallbacks(
|
|
|
|
new ARCMTMacroTrackerPPCallbacks(ARCMTMacroLocs));
|
|
|
|
return new ASTConsumer();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class RewritesApplicator : public TransformActions::RewriteReceiver {
|
|
|
|
Rewriter &rewriter;
|
|
|
|
ASTContext &Ctx;
|
|
|
|
MigrationProcess::RewriteListener *Listener;
|
|
|
|
|
|
|
|
public:
|
|
|
|
RewritesApplicator(Rewriter &rewriter, ASTContext &ctx,
|
|
|
|
MigrationProcess::RewriteListener *listener)
|
|
|
|
: rewriter(rewriter), Ctx(ctx), Listener(listener) {
|
|
|
|
if (Listener)
|
|
|
|
Listener->start(ctx);
|
|
|
|
}
|
|
|
|
~RewritesApplicator() {
|
|
|
|
if (Listener)
|
|
|
|
Listener->finish();
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
virtual void insert(SourceLocation loc, StringRef text) {
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void remove(CharSourceRange range) {
|
|
|
|
Rewriter::RewriteOptions removeOpts;
|
|
|
|
removeOpts.IncludeInsertsAtBeginOfRange = false;
|
|
|
|
removeOpts.IncludeInsertsAtEndOfRange = false;
|
|
|
|
removeOpts.RemoveLineIfEmpty = true;
|
|
|
|
|
|
|
|
bool err = rewriter.RemoveText(range, removeOpts);
|
|
|
|
if (!err && Listener)
|
|
|
|
Listener->remove(range);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void increaseIndentation(CharSourceRange range,
|
|
|
|
SourceLocation parentIndent) {
|
|
|
|
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)
|
2011-07-10 04:00:58 +08:00
|
|
|
: OrigCI(CI), DiagClient(diagClient) {
|
|
|
|
if (!outputDir.empty()) {
|
|
|
|
llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
2011-09-26 07:23:43 +08:00
|
|
|
llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
|
|
|
|
new DiagnosticsEngine(DiagID, 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) {
|
|
|
|
llvm::OwningPtr<CompilerInvocation> CInvok;
|
|
|
|
CInvok.reset(createInvocationForMigration(OrigCI));
|
|
|
|
CInvok->getDiagnosticOpts().IgnoreWarnings = true;
|
|
|
|
|
|
|
|
Remapper.applyMappings(*CInvok);
|
|
|
|
|
|
|
|
CapturedDiagList capturedDiags;
|
|
|
|
std::vector<SourceLocation> ARCMTMacroLocs;
|
|
|
|
|
|
|
|
assert(DiagClient);
|
|
|
|
llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
2011-09-26 07:23:43 +08:00
|
|
|
llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
|
|
|
|
new DiagnosticsEngine(DiagID, DiagClient, /*ShouldOwnClient=*/false));
|
2011-06-16 07:25:17 +08:00
|
|
|
|
|
|
|
// Filter of all diagnostics.
|
2011-09-26 07:54:33 +08:00
|
|
|
CaptureDiagnosticConsumer errRec(*Diags, capturedDiags);
|
2011-06-16 07:25:17 +08:00
|
|
|
Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
|
|
|
|
|
|
|
|
llvm::OwningPtr<ARCMTMacroTrackerAction> ASTAction;
|
|
|
|
ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs));
|
|
|
|
|
|
|
|
llvm::OwningPtr<ASTUnit> Unit(
|
|
|
|
ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags,
|
|
|
|
ASTAction.get()));
|
|
|
|
if (!Unit)
|
|
|
|
return true;
|
|
|
|
Unit->setOwnsRemappedFileBuffers(false); // FileRemapper manages that.
|
|
|
|
|
|
|
|
// Don't filter diagnostics anymore.
|
|
|
|
Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
|
|
|
|
|
|
|
|
ASTContext &Ctx = Unit->getASTContext();
|
|
|
|
|
|
|
|
if (Diags->hasFatalErrorOccurred()) {
|
|
|
|
Diags->Reset();
|
|
|
|
DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor());
|
|
|
|
capturedDiags.reportDiagnostics(*Diags);
|
|
|
|
DiagClient->EndSourceFile();
|
|
|
|
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().
|
|
|
|
DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor());
|
|
|
|
|
|
|
|
Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOptions());
|
|
|
|
TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
|
2011-11-18 07:01:24 +08:00
|
|
|
MigrationPass pass(Ctx, OrigCI.getLangOpts()->getGC(),
|
2011-11-04 23:58:08 +08:00
|
|
|
Unit->getSema(), TA, ARCMTMacroLocs);
|
2011-06-16 07:25:17 +08:00
|
|
|
|
|
|
|
trans(pass);
|
|
|
|
|
|
|
|
{
|
|
|
|
RewritesApplicator applicator(rewriter, Ctx, listener);
|
|
|
|
TA.applyRewrites(applicator);
|
|
|
|
}
|
|
|
|
|
|
|
|
DiagClient->EndSourceFile();
|
|
|
|
|
|
|
|
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";
|
|
|
|
llvm::SmallString<512> newText;
|
|
|
|
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);
|
2011-06-16 07:25:17 +08:00
|
|
|
llvm::SmallString<64> filePath(file->getName());
|
|
|
|
Unit->getFileManager().FixupRelativePath(filePath);
|
|
|
|
Remapper.remap(filePath.str(), memBuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|