Skip to content

Commit 9074521

Browse files
committed
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
1 parent 87720ac commit 9074521

File tree

13 files changed

+114
-58
lines changed

13 files changed

+114
-58
lines changed

clang-tools-extra/clang-doc/ClangDoc.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ namespace doc {
2929
class MapperActionFactory : public tooling::FrontendActionFactory {
3030
public:
3131
MapperActionFactory(ClangDocContext CDCtx) : CDCtx(CDCtx) {}
32-
clang::FrontendAction *create() override;
32+
std::unique_ptr<FrontendAction> create() override;
3333

3434
private:
3535
ClangDocContext CDCtx;
3636
};
3737

38-
clang::FrontendAction *MapperActionFactory::create() {
38+
std::unique_ptr<FrontendAction> MapperActionFactory::create() {
3939
class ClangDocAction : public clang::ASTFrontendAction {
4040
public:
4141
ClangDocAction(ClangDocContext CDCtx) : CDCtx(CDCtx) {}
@@ -49,7 +49,7 @@ clang::FrontendAction *MapperActionFactory::create() {
4949
private:
5050
ClangDocContext CDCtx;
5151
};
52-
return new ClangDocAction(CDCtx);
52+
return std::make_unique<ClangDocAction>(CDCtx);
5353
}
5454

5555
std::unique_ptr<tooling::FrontendActionFactory>

clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllSymbolsAction.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ class FindAllSymbolsActionFactory : public tooling::FrontendActionFactory {
4747
const HeaderMapCollector::RegexHeaderMap *RegexHeaderMap = nullptr)
4848
: Reporter(Reporter), RegexHeaderMap(RegexHeaderMap) {}
4949

50-
clang::FrontendAction *create() override {
51-
return new FindAllSymbolsAction(Reporter, RegexHeaderMap);
50+
std::unique_ptr<FrontendAction> create() override {
51+
return std::make_unique<FindAllSymbolsAction>(Reporter, RegexHeaderMap);
5252
}
5353

5454
private:

clang-tools-extra/clang-move/Move.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ class ClangMoveActionFactory : public tooling::FrontendActionFactory {
224224
DeclarationReporter *const Reporter = nullptr)
225225
: Context(Context), Reporter(Reporter) {}
226226

227-
clang::FrontendAction *create() override {
228-
return new ClangMoveAction(Context, Reporter);
227+
std::unique_ptr<clang::FrontendAction> create() override {
228+
return std::make_unique<ClangMoveAction>(Context, Reporter);
229229
}
230230

231231
private:

clang-tools-extra/clang-tidy/ClangTidy.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,9 @@ runClangTidy(clang::tidy::ClangTidyContext &Context,
530530
ActionFactory(ClangTidyContext &Context,
531531
IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> BaseFS)
532532
: ConsumerFactory(Context, BaseFS) {}
533-
FrontendAction *create() override { return new Action(&ConsumerFactory); }
533+
std::unique_ptr<FrontendAction> create() override {
534+
return std::make_unique<Action>(&ConsumerFactory);
535+
}
534536

535537
bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
536538
FileManager *Files,

clang-tools-extra/clangd/indexer/IndexerMain.cpp

+27-28
Original file line numberDiff line numberDiff line change
@@ -39,37 +39,36 @@ class IndexActionFactory : public tooling::FrontendActionFactory {
3939
public:
4040
IndexActionFactory(IndexFileIn &Result) : Result(Result) {}
4141

42-
clang::FrontendAction *create() override {
42+
std::unique_ptr<FrontendAction> create() override {
4343
SymbolCollector::Options Opts;
4444
Opts.CountReferences = true;
4545
return createStaticIndexingAction(
46-
Opts,
47-
[&](SymbolSlab S) {
48-
// Merge as we go.
49-
std::lock_guard<std::mutex> Lock(SymbolsMu);
50-
for (const auto &Sym : S) {
51-
if (const auto *Existing = Symbols.find(Sym.ID))
52-
Symbols.insert(mergeSymbol(*Existing, Sym));
53-
else
54-
Symbols.insert(Sym);
55-
}
56-
},
57-
[&](RefSlab S) {
58-
std::lock_guard<std::mutex> Lock(SymbolsMu);
59-
for (const auto &Sym : S) {
60-
// Deduplication happens during insertion.
61-
for (const auto &Ref : Sym.second)
62-
Refs.insert(Sym.first, Ref);
63-
}
64-
},
65-
[&](RelationSlab S) {
66-
std::lock_guard<std::mutex> Lock(SymbolsMu);
67-
for (const auto &R : S) {
68-
Relations.insert(R);
69-
}
70-
},
71-
/*IncludeGraphCallback=*/nullptr)
72-
.release();
46+
Opts,
47+
[&](SymbolSlab S) {
48+
// Merge as we go.
49+
std::lock_guard<std::mutex> Lock(SymbolsMu);
50+
for (const auto &Sym : S) {
51+
if (const auto *Existing = Symbols.find(Sym.ID))
52+
Symbols.insert(mergeSymbol(*Existing, Sym));
53+
else
54+
Symbols.insert(Sym);
55+
}
56+
},
57+
[&](RefSlab S) {
58+
std::lock_guard<std::mutex> Lock(SymbolsMu);
59+
for (const auto &Sym : S) {
60+
// Deduplication happens during insertion.
61+
for (const auto &Ref : Sym.second)
62+
Refs.insert(Sym.first, Ref);
63+
}
64+
},
65+
[&](RelationSlab S) {
66+
std::lock_guard<std::mutex> Lock(SymbolsMu);
67+
for (const auto &R : S) {
68+
Relations.insert(R);
69+
}
70+
},
71+
/*IncludeGraphCallback=*/nullptr);
7372
}
7473

7574
// Awkward: we write the result in the destructor, because the executor

clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class SymbolIndexActionFactory : public tooling::FrontendActionFactory {
200200
CommentHandler *PragmaHandler)
201201
: COpts(std::move(COpts)), PragmaHandler(PragmaHandler) {}
202202

203-
clang::FrontendAction *create() override {
203+
std::unique_ptr<FrontendAction> create() override {
204204
class IndexAction : public ASTFrontendAction {
205205
public:
206206
IndexAction(std::shared_ptr<index::IndexDataConsumer> DataConsumer,
@@ -232,7 +232,8 @@ class SymbolIndexActionFactory : public tooling::FrontendActionFactory {
232232
index::IndexingOptions::SystemSymbolFilterKind::All;
233233
IndexOpts.IndexFunctionLocals = false;
234234
Collector = std::make_shared<SymbolCollector>(COpts);
235-
return new IndexAction(Collector, std::move(IndexOpts), PragmaHandler);
235+
return std::make_unique<IndexAction>(Collector, std::move(IndexOpts),
236+
PragmaHandler);
236237
}
237238

238239
std::shared_ptr<SymbolCollector> Collector;

clang-tools-extra/modularize/CoverageChecker.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "clang/Basic/SourceManager.h"
5959
#include "clang/Driver/Options.h"
6060
#include "clang/Frontend/CompilerInstance.h"
61+
#include "clang/Frontend/FrontendAction.h"
6162
#include "clang/Frontend/FrontendActions.h"
6263
#include "clang/Lex/PPCallbacks.h"
6364
#include "clang/Lex/Preprocessor.h"
@@ -129,8 +130,8 @@ class CoverageCheckerFrontendActionFactory : public FrontendActionFactory {
129130
CoverageCheckerFrontendActionFactory(CoverageChecker &Checker)
130131
: Checker(Checker) {}
131132

132-
CoverageCheckerAction *create() override {
133-
return new CoverageCheckerAction(Checker);
133+
std::unique_ptr<FrontendAction> create() override {
134+
return std::make_unique<CoverageCheckerAction>(Checker);
134135
}
135136

136137
private:

clang-tools-extra/modularize/Modularize.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@
233233
#include "clang/Basic/SourceManager.h"
234234
#include "clang/Driver/Options.h"
235235
#include "clang/Frontend/CompilerInstance.h"
236+
#include "clang/Frontend/FrontendAction.h"
236237
#include "clang/Frontend/FrontendActions.h"
237238
#include "clang/Lex/Preprocessor.h"
238239
#include "clang/Tooling/CompilationDatabase.h"
@@ -721,8 +722,9 @@ class ModularizeFrontendActionFactory : public FrontendActionFactory {
721722
: Entities(Entities), PPTracker(preprocessorTracker),
722723
HadErrors(HadErrors) {}
723724

724-
CollectEntitiesAction *create() override {
725-
return new CollectEntitiesAction(Entities, PPTracker, HadErrors);
725+
std::unique_ptr<FrontendAction> create() override {
726+
return std::make_unique<CollectEntitiesAction>(Entities, PPTracker,
727+
HadErrors);
726728
}
727729

728730
private:
@@ -801,8 +803,8 @@ class CompileCheckFrontendActionFactory : public FrontendActionFactory {
801803
public:
802804
CompileCheckFrontendActionFactory() {}
803805

804-
CompileCheckAction *create() override {
805-
return new CompileCheckAction();
806+
std::unique_ptr<FrontendAction> create() override {
807+
return std::make_unique<CompileCheckAction>();
806808
}
807809
};
808810

@@ -886,6 +888,7 @@ int main(int Argc, const char **Argv) {
886888
CompileCheckTool.appendArgumentsAdjuster(
887889
getModularizeArgumentsAdjuster(ModUtil->Dependencies));
888890
int CompileCheckFileErrors = 0;
891+
// FIXME: use newFrontendActionFactory.
889892
CompileCheckFrontendActionFactory CompileCheckFactory;
890893
CompileCheckFileErrors |= CompileCheckTool.run(&CompileCheckFactory);
891894
if (CompileCheckFileErrors != 0) {

clang-tools-extra/pp-trace/PPTrace.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "clang/Basic/SourceManager.h"
3131
#include "clang/Driver/Options.h"
3232
#include "clang/Frontend/CompilerInstance.h"
33+
#include "clang/Frontend/FrontendAction.h"
3334
#include "clang/Frontend/FrontendActions.h"
3435
#include "clang/Lex/Preprocessor.h"
3536
#include "clang/Tooling/Execution.h"
@@ -112,7 +113,9 @@ class PPTraceFrontendActionFactory : public tooling::FrontendActionFactory {
112113
PPTraceFrontendActionFactory(const FilterType &Filters, raw_ostream &OS)
113114
: Filters(Filters), OS(OS) {}
114115

115-
PPTraceAction *create() override { return new PPTraceAction(Filters, OS); }
116+
std::unique_ptr<FrontendAction> create() override {
117+
return std::make_unique<PPTraceAction>(Filters, OS);
118+
}
116119

117120
private:
118121
const FilterType &Filters;

clang/include/clang/Tooling/Tooling.h

+44-6
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@ class FrontendActionFactory : public ToolAction {
9999
DiagnosticConsumer *DiagConsumer) override;
100100

101101
/// Returns a new clang::FrontendAction.
102-
///
103-
/// The caller takes ownership of the returned action.
104-
virtual FrontendAction *create() = 0;
102+
virtual std::unique_ptr<FrontendAction> create() = 0;
105103
};
106104

107105
/// Returns a new FrontendActionFactory for a given type.
@@ -161,6 +159,14 @@ bool runToolOnCode(FrontendAction *ToolAction, const Twine &Code,
161159
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
162160
std::make_shared<PCHContainerOperations>());
163161

162+
inline bool
163+
runToolOnCode(std::unique_ptr<FrontendAction> ToolAction, const Twine &Code,
164+
const Twine &FileName = "input.cc",
165+
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
166+
std::make_shared<PCHContainerOperations>()) {
167+
return runToolOnCode(ToolAction.release(), Code, FileName, PCHContainerOps);
168+
}
169+
164170
/// The first part of the pair is the filename, the second part the
165171
/// file-content.
166172
using FileContentMappings = std::vector<std::pair<std::string, std::string>>;
@@ -186,6 +192,17 @@ bool runToolOnCodeWithArgs(
186192
std::make_shared<PCHContainerOperations>(),
187193
const FileContentMappings &VirtualMappedFiles = FileContentMappings());
188194

195+
inline bool runToolOnCodeWithArgs(
196+
std::unique_ptr<FrontendAction> ToolAction, const Twine &Code,
197+
const std::vector<std::string> &Args, const Twine &FileName = "input.cc",
198+
const Twine &ToolName = "clang-tool",
199+
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
200+
std::make_shared<PCHContainerOperations>(),
201+
const FileContentMappings &VirtualMappedFiles = FileContentMappings()) {
202+
return runToolOnCodeWithArgs(ToolAction.release(), Code, Args, FileName,
203+
ToolName, PCHContainerOps, VirtualMappedFiles);
204+
}
205+
189206
// Similar to the overload except this takes a VFS.
190207
bool runToolOnCodeWithArgs(
191208
FrontendAction *ToolAction, const Twine &Code,
@@ -195,6 +212,17 @@ bool runToolOnCodeWithArgs(
195212
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
196213
std::make_shared<PCHContainerOperations>());
197214

215+
inline bool runToolOnCodeWithArgs(
216+
std::unique_ptr<FrontendAction> ToolAction, const Twine &Code,
217+
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
218+
const std::vector<std::string> &Args, const Twine &FileName = "input.cc",
219+
const Twine &ToolName = "clang-tool",
220+
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
221+
std::make_shared<PCHContainerOperations>()) {
222+
return runToolOnCodeWithArgs(ToolAction.release(), Code, VFS, Args, FileName,
223+
ToolName, PCHContainerOps);
224+
}
225+
198226
/// Builds an AST for 'Code'.
199227
///
200228
/// \param Code C++ code.
@@ -247,6 +275,13 @@ class ToolInvocation {
247275
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
248276
std::make_shared<PCHContainerOperations>());
249277

278+
ToolInvocation(std::vector<std::string> CommandLine,
279+
std::unique_ptr<FrontendAction> FAction, FileManager *Files,
280+
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
281+
std::make_shared<PCHContainerOperations>())
282+
: ToolInvocation(std::move(CommandLine), FAction.release(), Files,
283+
PCHContainerOps) {}
284+
250285
/// Create a tool invocation.
251286
///
252287
/// \param CommandLine The command line arguments to clang.
@@ -397,7 +432,9 @@ template <typename T>
397432
std::unique_ptr<FrontendActionFactory> newFrontendActionFactory() {
398433
class SimpleFrontendActionFactory : public FrontendActionFactory {
399434
public:
400-
FrontendAction *create() override { return new T; }
435+
std::unique_ptr<FrontendAction> create() override {
436+
return std::make_unique<T>();
437+
}
401438
};
402439

403440
return std::unique_ptr<FrontendActionFactory>(
@@ -413,8 +450,9 @@ inline std::unique_ptr<FrontendActionFactory> newFrontendActionFactory(
413450
SourceFileCallbacks *Callbacks)
414451
: ConsumerFactory(ConsumerFactory), Callbacks(Callbacks) {}
415452

416-
FrontendAction *create() override {
417-
return new ConsumerFactoryAdaptor(ConsumerFactory, Callbacks);
453+
std::unique_ptr<FrontendAction> create() override {
454+
return std::make_unique<ConsumerFactoryAdaptor>(ConsumerFactory,
455+
Callbacks);
418456
}
419457

420458
private:

clang/lib/Tooling/Tooling.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,15 @@ void addTargetAndModeForProgramName(std::vector<std::string> &CommandLine,
247247
namespace {
248248

249249
class SingleFrontendActionFactory : public FrontendActionFactory {
250-
FrontendAction *Action;
250+
std::unique_ptr<FrontendAction> Action;
251251

252252
public:
253-
SingleFrontendActionFactory(FrontendAction *Action) : Action(Action) {}
253+
SingleFrontendActionFactory(std::unique_ptr<FrontendAction> Action)
254+
: Action(std::move(Action)) {}
254255

255-
FrontendAction *create() override { return Action; }
256+
std::unique_ptr<FrontendAction> create() override {
257+
return std::move(Action);
258+
}
256259
};
257260

258261
} // namespace
@@ -267,8 +270,10 @@ ToolInvocation::ToolInvocation(
267270
std::vector<std::string> CommandLine, FrontendAction *FAction,
268271
FileManager *Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps)
269272
: CommandLine(std::move(CommandLine)),
270-
Action(new SingleFrontendActionFactory(FAction)), OwnsAction(true),
271-
Files(Files), PCHContainerOps(std::move(PCHContainerOps)) {}
273+
Action(new SingleFrontendActionFactory(
274+
std::unique_ptr<FrontendAction>(FAction))),
275+
OwnsAction(true), Files(Files),
276+
PCHContainerOps(std::move(PCHContainerOps)) {}
272277

273278
ToolInvocation::~ToolInvocation() {
274279
if (OwnsAction)

clang/tools/clang-refactor/ClangRefactor.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,9 @@ class ClangRefactorTool {
461461
ToolActionFactory(TUCallbackType Callback)
462462
: Callback(std::move(Callback)) {}
463463

464-
FrontendAction *create() override { return new ToolASTAction(Callback); }
464+
std::unique_ptr<FrontendAction> create() override {
465+
return std::make_unique<ToolASTAction>(Callback);
466+
}
465467

466468
private:
467469
TUCallbackType Callback;

clang/unittests/Tooling/ExecutionTest.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ class ReportResultAction : public ASTFrontendAction {
7878
class ReportResultActionFactory : public FrontendActionFactory {
7979
public:
8080
ReportResultActionFactory(ExecutionContext *Context) : Context(Context) {}
81-
FrontendAction *create() override { return new ReportResultAction(Context); }
81+
std::unique_ptr<FrontendAction> create() override {
82+
return std::make_unique<ReportResultAction>(Context);
83+
}
8284

8385
private:
8486
ExecutionContext *const Context;

0 commit comments

Comments
 (0)