2011-06-16 07:25:17 +08:00
|
|
|
//===--- ARCMT.cpp - Migration to ARC mode --------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-06-16 07:25:17 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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"
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "clang/Lex/PreprocessorOptions.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"
|
2016-05-27 22:27:13 +08:00
|
|
|
#include <utility>
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-11 10:00:23 +08:00
|
|
|
~CaptureDiagnosticConsumer() override {
|
2012-08-10 09:06:16 +08:00
|
|
|
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;
|
|
|
|
|
2015-10-31 00:30:36 +08:00
|
|
|
if (triple.isWatchOS())
|
|
|
|
return true;
|
|
|
|
|
2011-06-21 03:59:52 +08:00
|
|
|
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 *
|
2015-06-21 02:53:08 +08:00
|
|
|
createInvocationForMigration(CompilerInvocation &origCI,
|
2015-07-17 09:19:54 +08:00
|
|
|
const PCHContainerReader &PCHContainerRdr) {
|
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()));
|
2015-06-21 02:53:08 +08:00
|
|
|
std::string OriginalFile = ASTReader::getOriginalSourceFile(
|
2015-07-17 09:19:54 +08:00
|
|
|
PPOpts.ImplicitPCHInclude, FileMgr, PCHContainerRdr, *Diags);
|
2013-02-06 00:37:00 +08:00
|
|
|
if (!OriginalFile.empty())
|
|
|
|
PPOpts.Includes.insert(PPOpts.Includes.begin(), OriginalFile);
|
|
|
|
PPOpts.ImplicitPCHInclude.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
|
|
|
|
Define weak and __weak to mean ARC-style weak references, even in MRC.
Previously, __weak was silently accepted and ignored in MRC mode.
That makes this a potentially source-breaking change that we have to
roll out cautiously. Accordingly, for the time being, actual support
for __weak references in MRC is experimental, and the compiler will
reject attempts to actually form such references. The intent is to
eventually enable the feature by default in all non-GC modes.
(It is, of course, incompatible with ObjC GC's interpretation of
__weak.)
If you like, you can enable this feature with
-Xclang -fobjc-weak
but like any -Xclang option, this option may be removed at any point,
e.g. if/when it is eventually enabled by default.
This patch also enables the use of the ARC __unsafe_unretained qualifier
in MRC. Unlike __weak, this is being enabled immediately. Since
variables are essentially __unsafe_unretained by default in MRC,
the only practical uses are (1) communication and (2) changing the
default behavior of by-value block capture.
As an implementation matter, this means that the ObjC ownership
qualifiers may appear in any ObjC language mode, and so this patch
removes a number of checks for getLangOpts().ObjCAutoRefCount
that were guarding the processing of these qualifiers. I don't
expect this to be a significant drain on performance; it may even
be faster to just check for these qualifiers directly on a type
(since it's probably in a register anyway) than to do N dependent
loads to grab the LangOptions.
rdar://9674298
llvm-svn: 251041
2015-10-23 02:38:17 +08:00
|
|
|
CInvok->getLangOpts()->ObjCWeakRuntime = HasARCRuntime(origCI);
|
|
|
|
CInvok->getLangOpts()->ObjCWeak = CInvok->getLangOpts()->ObjCWeakRuntime;
|
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());
|
2018-07-31 03:24:48 +08:00
|
|
|
|
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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-06-21 02:53:08 +08:00
|
|
|
bool arcmt::checkForManualIssues(
|
|
|
|
CompilerInvocation &origCI, const FrontendInputFile &Input,
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
|
|
|
|
DiagnosticConsumer *DiagClient, bool emitPremigrationARCErrors,
|
|
|
|
StringRef plistOut) {
|
2018-10-31 04:31:30 +08:00
|
|
|
if (!origCI.getLangOpts()->ObjC)
|
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;
|
2015-07-17 09:19:54 +08:00
|
|
|
CInvok.reset(
|
|
|
|
createInvocationForMigration(origCI, PCHContainerOps->getRawReader()));
|
2011-06-16 07:25:17 +08:00
|
|
|
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);
|
|
|
|
|
2015-06-21 02:53:08 +08:00
|
|
|
std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCompilerInvocationAction(
|
2017-01-07 03:49:01 +08:00
|
|
|
std::move(CInvok), PCHContainerOps, 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.
|
2018-07-31 03:24:48 +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-06-12 19:13:52 +08:00
|
|
|
Diags->setSeverity(diag::warn_arcmt_nsalloc_realloc, diag::Severity::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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-06-21 02:53:08 +08:00
|
|
|
static bool
|
|
|
|
applyTransforms(CompilerInvocation &origCI, const FrontendInputFile &Input,
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
|
|
|
|
DiagnosticConsumer *DiagClient, StringRef outputDir,
|
|
|
|
bool emitPremigrationARCErrors, StringRef plistOut) {
|
2018-10-31 04:31:30 +08:00
|
|
|
if (!origCI.getLangOpts()->ObjC)
|
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);
|
2015-06-21 02:53:08 +08:00
|
|
|
if (arcmt::checkForManualIssues(CInvokForCheck, Input, PCHContainerOps,
|
|
|
|
DiagClient, 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);
|
2015-06-21 02:53:08 +08:00
|
|
|
|
|
|
|
MigrationProcess migration(CInvok, PCHContainerOps, 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
|
|
|
}
|
|
|
|
|
2015-06-21 02:53:08 +08:00
|
|
|
bool arcmt::applyTransformations(
|
|
|
|
CompilerInvocation &origCI, const FrontendInputFile &Input,
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
|
|
|
|
DiagnosticConsumer *DiagClient) {
|
|
|
|
return applyTransforms(origCI, Input, PCHContainerOps, DiagClient,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef(), false, StringRef());
|
2011-07-10 04:00:58 +08:00
|
|
|
}
|
|
|
|
|
2015-06-21 02:53:08 +08:00
|
|
|
bool arcmt::migrateWithTemporaryFiles(
|
|
|
|
CompilerInvocation &origCI, const FrontendInputFile &Input,
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
|
|
|
|
DiagnosticConsumer *DiagClient, StringRef outputDir,
|
|
|
|
bool emitPremigrationARCErrors, StringRef plistOut) {
|
2011-07-10 04:00:58 +08:00
|
|
|
assert(!outputDir.empty() && "Expected output directory path");
|
2015-06-21 02:53:08 +08:00
|
|
|
return applyTransforms(origCI, Input, PCHContainerOps, DiagClient, 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) { }
|
|
|
|
|
2015-05-04 11:15:40 +08:00
|
|
|
void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
|
2014-03-14 14:02:07 +08:00
|
|
|
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-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
|
|
|
|
StringRef InFile) override {
|
2011-06-16 07:25:17 +08:00
|
|
|
CI.getPreprocessor().addPPCallbacks(
|
2014-09-10 12:53:53 +08:00
|
|
|
llvm::make_unique<ARCMTMacroTrackerPPCallbacks>(ARCMTMacroLocs));
|
2014-08-11 03:56:51 +08:00
|
|
|
return llvm::make_unique<ASTConsumer>();
|
2011-06-16 07:25:17 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2015-04-11 10:00:23 +08:00
|
|
|
~RewritesApplicator() override {
|
2011-06-16 07:25:17 +08:00
|
|
|
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.
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Anchor for VTable.
|
2015-10-20 21:23:58 +08:00
|
|
|
MigrationProcess::RewriteListener::~RewriteListener() { }
|
2011-06-16 07:25:17 +08:00
|
|
|
|
2015-06-21 02:53:08 +08:00
|
|
|
MigrationProcess::MigrationProcess(
|
|
|
|
const CompilerInvocation &CI,
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
|
|
|
|
DiagnosticConsumer *diagClient, StringRef outputDir)
|
2016-05-27 22:27:13 +08:00
|
|
|
: OrigCI(CI), PCHContainerOps(std::move(PCHContainerOps)),
|
|
|
|
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;
|
2015-07-17 09:19:54 +08:00
|
|
|
CInvok.reset(
|
|
|
|
createInvocationForMigration(OrigCI, PCHContainerOps->getRawReader()));
|
2011-06-16 07:25:17 +08:00
|
|
|
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(
|
2017-01-07 03:49:01 +08:00
|
|
|
std::move(CInvok), PCHContainerOps, 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.
|
2018-07-31 03:24:48 +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);
|
2014-08-18 07:12:27 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> memBuf(
|
|
|
|
llvm::MemoryBuffer::getMemBufferCopy(
|
|
|
|
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);
|
2014-08-18 07:12:27 +08:00
|
|
|
Remapper.remap(filePath.str(), std::move(memBuf));
|
2011-06-16 07:25:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|