Changed FrontendActionFactory::create to return a std::unique_ptr

Subscribers: jkorous, arphaman, kadircet, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D66947

llvm-svn: 370379
This commit is contained in:
Dmitri Gribenko 2019-08-29 16:38:36 +00:00
parent 87720ac8c8
commit 907452107d
13 changed files with 114 additions and 58 deletions

View File

@ -29,13 +29,13 @@ namespace doc {
class MapperActionFactory : public tooling::FrontendActionFactory {
public:
MapperActionFactory(ClangDocContext CDCtx) : CDCtx(CDCtx) {}
clang::FrontendAction *create() override;
std::unique_ptr<FrontendAction> create() override;
private:
ClangDocContext CDCtx;
};
clang::FrontendAction *MapperActionFactory::create() {
std::unique_ptr<FrontendAction> MapperActionFactory::create() {
class ClangDocAction : public clang::ASTFrontendAction {
public:
ClangDocAction(ClangDocContext CDCtx) : CDCtx(CDCtx) {}
@ -49,7 +49,7 @@ clang::FrontendAction *MapperActionFactory::create() {
private:
ClangDocContext CDCtx;
};
return new ClangDocAction(CDCtx);
return std::make_unique<ClangDocAction>(CDCtx);
}
std::unique_ptr<tooling::FrontendActionFactory>

View File

@ -47,8 +47,8 @@ public:
const HeaderMapCollector::RegexHeaderMap *RegexHeaderMap = nullptr)
: Reporter(Reporter), RegexHeaderMap(RegexHeaderMap) {}
clang::FrontendAction *create() override {
return new FindAllSymbolsAction(Reporter, RegexHeaderMap);
std::unique_ptr<FrontendAction> create() override {
return std::make_unique<FindAllSymbolsAction>(Reporter, RegexHeaderMap);
}
private:

View File

@ -224,8 +224,8 @@ public:
DeclarationReporter *const Reporter = nullptr)
: Context(Context), Reporter(Reporter) {}
clang::FrontendAction *create() override {
return new ClangMoveAction(Context, Reporter);
std::unique_ptr<clang::FrontendAction> create() override {
return std::make_unique<ClangMoveAction>(Context, Reporter);
}
private:

View File

@ -530,7 +530,9 @@ runClangTidy(clang::tidy::ClangTidyContext &Context,
ActionFactory(ClangTidyContext &Context,
IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> BaseFS)
: ConsumerFactory(Context, BaseFS) {}
FrontendAction *create() override { return new Action(&ConsumerFactory); }
std::unique_ptr<FrontendAction> create() override {
return std::make_unique<Action>(&ConsumerFactory);
}
bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
FileManager *Files,

View File

@ -39,37 +39,36 @@ class IndexActionFactory : public tooling::FrontendActionFactory {
public:
IndexActionFactory(IndexFileIn &Result) : Result(Result) {}
clang::FrontendAction *create() override {
std::unique_ptr<FrontendAction> create() override {
SymbolCollector::Options Opts;
Opts.CountReferences = true;
return createStaticIndexingAction(
Opts,
[&](SymbolSlab S) {
// Merge as we go.
std::lock_guard<std::mutex> Lock(SymbolsMu);
for (const auto &Sym : S) {
if (const auto *Existing = Symbols.find(Sym.ID))
Symbols.insert(mergeSymbol(*Existing, Sym));
else
Symbols.insert(Sym);
}
},
[&](RefSlab S) {
std::lock_guard<std::mutex> Lock(SymbolsMu);
for (const auto &Sym : S) {
// Deduplication happens during insertion.
for (const auto &Ref : Sym.second)
Refs.insert(Sym.first, Ref);
}
},
[&](RelationSlab S) {
std::lock_guard<std::mutex> Lock(SymbolsMu);
for (const auto &R : S) {
Relations.insert(R);
}
},
/*IncludeGraphCallback=*/nullptr)
.release();
Opts,
[&](SymbolSlab S) {
// Merge as we go.
std::lock_guard<std::mutex> Lock(SymbolsMu);
for (const auto &Sym : S) {
if (const auto *Existing = Symbols.find(Sym.ID))
Symbols.insert(mergeSymbol(*Existing, Sym));
else
Symbols.insert(Sym);
}
},
[&](RefSlab S) {
std::lock_guard<std::mutex> Lock(SymbolsMu);
for (const auto &Sym : S) {
// Deduplication happens during insertion.
for (const auto &Ref : Sym.second)
Refs.insert(Sym.first, Ref);
}
},
[&](RelationSlab S) {
std::lock_guard<std::mutex> Lock(SymbolsMu);
for (const auto &R : S) {
Relations.insert(R);
}
},
/*IncludeGraphCallback=*/nullptr);
}
// Awkward: we write the result in the destructor, because the executor

View File

@ -200,7 +200,7 @@ public:
CommentHandler *PragmaHandler)
: COpts(std::move(COpts)), PragmaHandler(PragmaHandler) {}
clang::FrontendAction *create() override {
std::unique_ptr<FrontendAction> create() override {
class IndexAction : public ASTFrontendAction {
public:
IndexAction(std::shared_ptr<index::IndexDataConsumer> DataConsumer,
@ -232,7 +232,8 @@ public:
index::IndexingOptions::SystemSymbolFilterKind::All;
IndexOpts.IndexFunctionLocals = false;
Collector = std::make_shared<SymbolCollector>(COpts);
return new IndexAction(Collector, std::move(IndexOpts), PragmaHandler);
return std::make_unique<IndexAction>(Collector, std::move(IndexOpts),
PragmaHandler);
}
std::shared_ptr<SymbolCollector> Collector;

View File

@ -58,6 +58,7 @@
#include "clang/Basic/SourceManager.h"
#include "clang/Driver/Options.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Preprocessor.h"
@ -129,8 +130,8 @@ public:
CoverageCheckerFrontendActionFactory(CoverageChecker &Checker)
: Checker(Checker) {}
CoverageCheckerAction *create() override {
return new CoverageCheckerAction(Checker);
std::unique_ptr<FrontendAction> create() override {
return std::make_unique<CoverageCheckerAction>(Checker);
}
private:

View File

@ -233,6 +233,7 @@
#include "clang/Basic/SourceManager.h"
#include "clang/Driver/Options.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Tooling/CompilationDatabase.h"
@ -721,8 +722,9 @@ public:
: Entities(Entities), PPTracker(preprocessorTracker),
HadErrors(HadErrors) {}
CollectEntitiesAction *create() override {
return new CollectEntitiesAction(Entities, PPTracker, HadErrors);
std::unique_ptr<FrontendAction> create() override {
return std::make_unique<CollectEntitiesAction>(Entities, PPTracker,
HadErrors);
}
private:
@ -801,8 +803,8 @@ class CompileCheckFrontendActionFactory : public FrontendActionFactory {
public:
CompileCheckFrontendActionFactory() {}
CompileCheckAction *create() override {
return new CompileCheckAction();
std::unique_ptr<FrontendAction> create() override {
return std::make_unique<CompileCheckAction>();
}
};
@ -886,6 +888,7 @@ int main(int Argc, const char **Argv) {
CompileCheckTool.appendArgumentsAdjuster(
getModularizeArgumentsAdjuster(ModUtil->Dependencies));
int CompileCheckFileErrors = 0;
// FIXME: use newFrontendActionFactory.
CompileCheckFrontendActionFactory CompileCheckFactory;
CompileCheckFileErrors |= CompileCheckTool.run(&CompileCheckFactory);
if (CompileCheckFileErrors != 0) {

View File

@ -30,6 +30,7 @@
#include "clang/Basic/SourceManager.h"
#include "clang/Driver/Options.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Tooling/Execution.h"
@ -112,7 +113,9 @@ public:
PPTraceFrontendActionFactory(const FilterType &Filters, raw_ostream &OS)
: Filters(Filters), OS(OS) {}
PPTraceAction *create() override { return new PPTraceAction(Filters, OS); }
std::unique_ptr<FrontendAction> create() override {
return std::make_unique<PPTraceAction>(Filters, OS);
}
private:
const FilterType &Filters;

View File

@ -99,9 +99,7 @@ public:
DiagnosticConsumer *DiagConsumer) override;
/// Returns a new clang::FrontendAction.
///
/// The caller takes ownership of the returned action.
virtual FrontendAction *create() = 0;
virtual std::unique_ptr<FrontendAction> create() = 0;
};
/// Returns a new FrontendActionFactory for a given type.
@ -161,6 +159,14 @@ bool runToolOnCode(FrontendAction *ToolAction, const Twine &Code,
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
std::make_shared<PCHContainerOperations>());
inline bool
runToolOnCode(std::unique_ptr<FrontendAction> ToolAction, const Twine &Code,
const Twine &FileName = "input.cc",
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
std::make_shared<PCHContainerOperations>()) {
return runToolOnCode(ToolAction.release(), Code, FileName, PCHContainerOps);
}
/// The first part of the pair is the filename, the second part the
/// file-content.
using FileContentMappings = std::vector<std::pair<std::string, std::string>>;
@ -186,6 +192,17 @@ bool runToolOnCodeWithArgs(
std::make_shared<PCHContainerOperations>(),
const FileContentMappings &VirtualMappedFiles = FileContentMappings());
inline bool runToolOnCodeWithArgs(
std::unique_ptr<FrontendAction> ToolAction, const Twine &Code,
const std::vector<std::string> &Args, const Twine &FileName = "input.cc",
const Twine &ToolName = "clang-tool",
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
std::make_shared<PCHContainerOperations>(),
const FileContentMappings &VirtualMappedFiles = FileContentMappings()) {
return runToolOnCodeWithArgs(ToolAction.release(), Code, Args, FileName,
ToolName, PCHContainerOps, VirtualMappedFiles);
}
// Similar to the overload except this takes a VFS.
bool runToolOnCodeWithArgs(
FrontendAction *ToolAction, const Twine &Code,
@ -195,6 +212,17 @@ bool runToolOnCodeWithArgs(
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
std::make_shared<PCHContainerOperations>());
inline bool runToolOnCodeWithArgs(
std::unique_ptr<FrontendAction> ToolAction, const Twine &Code,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
const std::vector<std::string> &Args, const Twine &FileName = "input.cc",
const Twine &ToolName = "clang-tool",
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
std::make_shared<PCHContainerOperations>()) {
return runToolOnCodeWithArgs(ToolAction.release(), Code, VFS, Args, FileName,
ToolName, PCHContainerOps);
}
/// Builds an AST for 'Code'.
///
/// \param Code C++ code.
@ -247,6 +275,13 @@ public:
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
std::make_shared<PCHContainerOperations>());
ToolInvocation(std::vector<std::string> CommandLine,
std::unique_ptr<FrontendAction> FAction, FileManager *Files,
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
std::make_shared<PCHContainerOperations>())
: ToolInvocation(std::move(CommandLine), FAction.release(), Files,
PCHContainerOps) {}
/// Create a tool invocation.
///
/// \param CommandLine The command line arguments to clang.
@ -397,7 +432,9 @@ template <typename T>
std::unique_ptr<FrontendActionFactory> newFrontendActionFactory() {
class SimpleFrontendActionFactory : public FrontendActionFactory {
public:
FrontendAction *create() override { return new T; }
std::unique_ptr<FrontendAction> create() override {
return std::make_unique<T>();
}
};
return std::unique_ptr<FrontendActionFactory>(
@ -413,8 +450,9 @@ inline std::unique_ptr<FrontendActionFactory> newFrontendActionFactory(
SourceFileCallbacks *Callbacks)
: ConsumerFactory(ConsumerFactory), Callbacks(Callbacks) {}
FrontendAction *create() override {
return new ConsumerFactoryAdaptor(ConsumerFactory, Callbacks);
std::unique_ptr<FrontendAction> create() override {
return std::make_unique<ConsumerFactoryAdaptor>(ConsumerFactory,
Callbacks);
}
private:

View File

@ -247,12 +247,15 @@ void addTargetAndModeForProgramName(std::vector<std::string> &CommandLine,
namespace {
class SingleFrontendActionFactory : public FrontendActionFactory {
FrontendAction *Action;
std::unique_ptr<FrontendAction> Action;
public:
SingleFrontendActionFactory(FrontendAction *Action) : Action(Action) {}
SingleFrontendActionFactory(std::unique_ptr<FrontendAction> Action)
: Action(std::move(Action)) {}
FrontendAction *create() override { return Action; }
std::unique_ptr<FrontendAction> create() override {
return std::move(Action);
}
};
} // namespace
@ -267,8 +270,10 @@ ToolInvocation::ToolInvocation(
std::vector<std::string> CommandLine, FrontendAction *FAction,
FileManager *Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps)
: CommandLine(std::move(CommandLine)),
Action(new SingleFrontendActionFactory(FAction)), OwnsAction(true),
Files(Files), PCHContainerOps(std::move(PCHContainerOps)) {}
Action(new SingleFrontendActionFactory(
std::unique_ptr<FrontendAction>(FAction))),
OwnsAction(true), Files(Files),
PCHContainerOps(std::move(PCHContainerOps)) {}
ToolInvocation::~ToolInvocation() {
if (OwnsAction)

View File

@ -461,7 +461,9 @@ public:
ToolActionFactory(TUCallbackType Callback)
: Callback(std::move(Callback)) {}
FrontendAction *create() override { return new ToolASTAction(Callback); }
std::unique_ptr<FrontendAction> create() override {
return std::make_unique<ToolASTAction>(Callback);
}
private:
TUCallbackType Callback;

View File

@ -78,7 +78,9 @@ private:
class ReportResultActionFactory : public FrontendActionFactory {
public:
ReportResultActionFactory(ExecutionContext *Context) : Context(Context) {}
FrontendAction *create() override { return new ReportResultAction(Context); }
std::unique_ptr<FrontendAction> create() override {
return std::make_unique<ReportResultAction>(Context);
}
private:
ExecutionContext *const Context;