Skip to content

Commit d35e98f

Browse files
committed
[Frontend] Make the memory management of FrontendAction pointers explicit by using unique_ptr.
llvm-svn: 260048
1 parent a0a35d7 commit d35e98f

File tree

9 files changed

+73
-65
lines changed

9 files changed

+73
-65
lines changed

clang/include/clang/ARCMigrate/ARCMTActions.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ class CheckAction : public WrapperFrontendAction {
2222
bool BeginInvocation(CompilerInstance &CI) override;
2323

2424
public:
25-
CheckAction(FrontendAction *WrappedAction);
25+
CheckAction(std::unique_ptr<FrontendAction> WrappedAction);
2626
};
2727

2828
class ModifyAction : public WrapperFrontendAction {
2929
protected:
3030
bool BeginInvocation(CompilerInstance &CI) override;
3131

3232
public:
33-
ModifyAction(FrontendAction *WrappedAction);
33+
ModifyAction(std::unique_ptr<FrontendAction> WrappedAction);
3434
};
3535

3636
class MigrateSourceAction : public ASTFrontendAction {
@@ -49,7 +49,8 @@ class MigrateAction : public WrapperFrontendAction {
4949
bool BeginInvocation(CompilerInstance &CI) override;
5050

5151
public:
52-
MigrateAction(FrontendAction *WrappedAction, StringRef migrateDir,
52+
MigrateAction(std::unique_ptr<FrontendAction> WrappedAction,
53+
StringRef migrateDir,
5354
StringRef plistOut,
5455
bool emitPremigrationARCErrors);
5556
};
@@ -61,8 +62,8 @@ class ObjCMigrateAction : public WrapperFrontendAction {
6162
FileRemapper Remapper;
6263
CompilerInstance *CompInst;
6364
public:
64-
ObjCMigrateAction(FrontendAction *WrappedAction, StringRef migrateDir,
65-
unsigned migrateAction);
65+
ObjCMigrateAction(std::unique_ptr<FrontendAction> WrappedAction,
66+
StringRef migrateDir, unsigned migrateAction);
6667

6768
protected:
6869
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,

clang/include/clang/Frontend/FrontendAction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ class WrapperFrontendAction : public FrontendAction {
283283
public:
284284
/// Construct a WrapperFrontendAction from an existing action, taking
285285
/// ownership of it.
286-
WrapperFrontendAction(FrontendAction *WrappedAction);
286+
WrapperFrontendAction(std::unique_ptr<FrontendAction> WrappedAction);
287287

288288
bool usesPreprocessorOnly() const override;
289289
TranslationUnitKind getTranslationUnitKind() override;

clang/include/clang/Frontend/FrontendActions.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class VerifyPCHAction : public ASTFrontendAction {
168168
*/
169169
class ASTMergeAction : public FrontendAction {
170170
/// \brief The action that the merge action adapts.
171-
FrontendAction *AdaptedAction;
171+
std::unique_ptr<FrontendAction> AdaptedAction;
172172

173173
/// \brief The set of AST files to merge.
174174
std::vector<std::string> ASTFiles;
@@ -184,7 +184,8 @@ class ASTMergeAction : public FrontendAction {
184184
void EndSourceFileAction() override;
185185

186186
public:
187-
ASTMergeAction(FrontendAction *AdaptedAction, ArrayRef<std::string> ASTFiles);
187+
ASTMergeAction(std::unique_ptr<FrontendAction> AdaptedAction,
188+
ArrayRef<std::string> ASTFiles);
188189
~ASTMergeAction() override;
189190

190191
bool usesPreprocessorOnly() const override;

clang/include/clang/Rewrite/Frontend/FrontendActions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ class FixItAction : public ASTFrontendAction {
5050
/// frontend action.
5151
class FixItRecompile : public WrapperFrontendAction {
5252
public:
53-
FixItRecompile(FrontendAction *WrappedAction)
54-
: WrapperFrontendAction(WrappedAction) {}
53+
FixItRecompile(std::unique_ptr<FrontendAction> WrappedAction)
54+
: WrapperFrontendAction(std::move(WrappedAction)) {}
5555

5656
protected:
5757
bool BeginInvocation(CompilerInstance &CI) override;

clang/lib/ARCMigrate/ARCMTActions.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ bool CheckAction::BeginInvocation(CompilerInstance &CI) {
2525
return true;
2626
}
2727

28-
CheckAction::CheckAction(FrontendAction *WrappedAction)
29-
: WrapperFrontendAction(WrappedAction) {}
28+
CheckAction::CheckAction(std::unique_ptr<FrontendAction> WrappedAction)
29+
: WrapperFrontendAction(std::move(WrappedAction)) {}
3030

3131
bool ModifyAction::BeginInvocation(CompilerInstance &CI) {
3232
return !arcmt::applyTransformations(CI.getInvocation(), getCurrentInput(),
3333
CI.getPCHContainerOperations(),
3434
CI.getDiagnostics().getClient());
3535
}
3636

37-
ModifyAction::ModifyAction(FrontendAction *WrappedAction)
38-
: WrapperFrontendAction(WrappedAction) {}
37+
ModifyAction::ModifyAction(std::unique_ptr<FrontendAction> WrappedAction)
38+
: WrapperFrontendAction(std::move(WrappedAction)) {}
3939

4040
bool MigrateAction::BeginInvocation(CompilerInstance &CI) {
4141
if (arcmt::migrateWithTemporaryFiles(
@@ -49,11 +49,11 @@ bool MigrateAction::BeginInvocation(CompilerInstance &CI) {
4949
return true;
5050
}
5151

52-
MigrateAction::MigrateAction(FrontendAction *WrappedAction,
52+
MigrateAction::MigrateAction(std::unique_ptr<FrontendAction> WrappedAction,
5353
StringRef migrateDir,
5454
StringRef plistOut,
5555
bool emitPremigrationARCErrors)
56-
: WrapperFrontendAction(WrappedAction), MigrateDir(migrateDir),
56+
: WrapperFrontendAction(std::move(WrappedAction)), MigrateDir(migrateDir),
5757
PlistOut(plistOut), EmitPremigrationARCErros(emitPremigrationARCErrors) {
5858
if (MigrateDir.empty())
5959
MigrateDir = "."; // user current directory if none is given.

clang/lib/ARCMigrate/ObjCMT.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,11 @@ class ObjCMigrateASTConsumer : public ASTConsumer {
179179

180180
}
181181

182-
ObjCMigrateAction::ObjCMigrateAction(FrontendAction *WrappedAction,
182+
ObjCMigrateAction::ObjCMigrateAction(
183+
std::unique_ptr<FrontendAction> WrappedAction,
183184
StringRef migrateDir,
184185
unsigned migrateAction)
185-
: WrapperFrontendAction(WrappedAction), MigrateDir(migrateDir),
186+
: WrapperFrontendAction(std::move(WrappedAction)), MigrateDir(migrateDir),
186187
ObjCMigAction(migrateAction),
187188
CompInst(nullptr) {
188189
if (MigrateDir.empty())

clang/lib/Frontend/ASTMerge.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,13 @@ void ASTMergeAction::EndSourceFileAction() {
8383
return AdaptedAction->EndSourceFileAction();
8484
}
8585

86-
ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
86+
ASTMergeAction::ASTMergeAction(std::unique_ptr<FrontendAction> adaptedAction,
8787
ArrayRef<std::string> ASTFiles)
88-
: AdaptedAction(AdaptedAction), ASTFiles(ASTFiles.begin(), ASTFiles.end()) {
88+
: AdaptedAction(std::move(adaptedAction)), ASTFiles(ASTFiles.begin(), ASTFiles.end()) {
8989
assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
9090
}
9191

9292
ASTMergeAction::~ASTMergeAction() {
93-
delete AdaptedAction;
9493
}
9594

9695
bool ASTMergeAction::usesPreprocessorOnly() const {

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ bool WrapperFrontendAction::hasCodeCompletionSupport() const {
587587
return WrappedAction->hasCodeCompletionSupport();
588588
}
589589

590-
WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
591-
: WrappedAction(WrappedAction) {}
590+
WrapperFrontendAction::WrapperFrontendAction(
591+
std::unique_ptr<FrontendAction> WrappedAction)
592+
: WrappedAction(std::move(WrappedAction)) {}
592593

clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,34 @@
3131
using namespace clang;
3232
using namespace llvm::opt;
3333

34-
static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
34+
static std::unique_ptr<FrontendAction>
35+
CreateFrontendBaseAction(CompilerInstance &CI) {
3536
using namespace clang::frontend;
3637
StringRef Action("unknown");
3738
(void)Action;
3839

3940
switch (CI.getFrontendOpts().ProgramAction) {
40-
case ASTDeclList: return new ASTDeclListAction();
41-
case ASTDump: return new ASTDumpAction();
42-
case ASTPrint: return new ASTPrintAction();
43-
case ASTView: return new ASTViewAction();
44-
case DumpRawTokens: return new DumpRawTokensAction();
45-
case DumpTokens: return new DumpTokensAction();
46-
case EmitAssembly: return new EmitAssemblyAction();
47-
case EmitBC: return new EmitBCAction();
48-
case EmitHTML: return new HTMLPrintAction();
49-
case EmitLLVM: return new EmitLLVMAction();
50-
case EmitLLVMOnly: return new EmitLLVMOnlyAction();
51-
case EmitCodeGenOnly: return new EmitCodeGenOnlyAction();
52-
case EmitObj: return new EmitObjAction();
53-
case FixIt: return new FixItAction();
54-
case GenerateModule: return new GenerateModuleAction;
55-
case GeneratePCH: return new GeneratePCHAction;
56-
case GeneratePTH: return new GeneratePTHAction();
57-
case InitOnly: return new InitOnlyAction();
58-
case ParseSyntaxOnly: return new SyntaxOnlyAction();
59-
case ModuleFileInfo: return new DumpModuleInfoAction();
60-
case VerifyPCH: return new VerifyPCHAction();
41+
case ASTDeclList: return llvm::make_unique<ASTDeclListAction>();
42+
case ASTDump: return llvm::make_unique<ASTDumpAction>();
43+
case ASTPrint: return llvm::make_unique<ASTPrintAction>();
44+
case ASTView: return llvm::make_unique<ASTViewAction>();
45+
case DumpRawTokens: return llvm::make_unique<DumpRawTokensAction>();
46+
case DumpTokens: return llvm::make_unique<DumpTokensAction>();
47+
case EmitAssembly: return llvm::make_unique<EmitAssemblyAction>();
48+
case EmitBC: return llvm::make_unique<EmitBCAction>();
49+
case EmitHTML: return llvm::make_unique<HTMLPrintAction>();
50+
case EmitLLVM: return llvm::make_unique<EmitLLVMAction>();
51+
case EmitLLVMOnly: return llvm::make_unique<EmitLLVMOnlyAction>();
52+
case EmitCodeGenOnly: return llvm::make_unique<EmitCodeGenOnlyAction>();
53+
case EmitObj: return llvm::make_unique<EmitObjAction>();
54+
case FixIt: return llvm::make_unique<FixItAction>();
55+
case GenerateModule: return llvm::make_unique<GenerateModuleAction>();
56+
case GeneratePCH: return llvm::make_unique<GeneratePCHAction>();
57+
case GeneratePTH: return llvm::make_unique<GeneratePTHAction>();
58+
case InitOnly: return llvm::make_unique<InitOnlyAction>();
59+
case ParseSyntaxOnly: return llvm::make_unique<SyntaxOnlyAction>();
60+
case ModuleFileInfo: return llvm::make_unique<DumpModuleInfoAction>();
61+
case VerifyPCH: return llvm::make_unique<VerifyPCHAction>();
6162

6263
case PluginAction: {
6364
for (FrontendPluginRegistry::iterator it =
@@ -67,7 +68,7 @@ static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
6768
std::unique_ptr<PluginASTAction> P(it->instantiate());
6869
if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs))
6970
return nullptr;
70-
return P.release();
71+
return std::move(P);
7172
}
7273
}
7374

@@ -76,32 +77,33 @@ static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
7677
return nullptr;
7778
}
7879

79-
case PrintDeclContext: return new DeclContextPrintAction();
80-
case PrintPreamble: return new PrintPreambleAction();
80+
case PrintDeclContext: return llvm::make_unique<DeclContextPrintAction>();
81+
case PrintPreamble: return llvm::make_unique<PrintPreambleAction>();
8182
case PrintPreprocessedInput: {
8283
if (CI.getPreprocessorOutputOpts().RewriteIncludes)
83-
return new RewriteIncludesAction();
84-
return new PrintPreprocessedAction();
84+
return llvm::make_unique<RewriteIncludesAction>();
85+
return llvm::make_unique<PrintPreprocessedAction>();
8586
}
8687

87-
case RewriteMacros: return new RewriteMacrosAction();
88-
case RewriteTest: return new RewriteTestAction();
88+
case RewriteMacros: return llvm::make_unique<RewriteMacrosAction>();
89+
case RewriteTest: return llvm::make_unique<RewriteTestAction>();
8990
#ifdef CLANG_ENABLE_OBJC_REWRITER
90-
case RewriteObjC: return new RewriteObjCAction();
91+
case RewriteObjC: return llvm::make_unique<RewriteObjCAction>();
9192
#else
9293
case RewriteObjC: Action = "RewriteObjC"; break;
9394
#endif
9495
#ifdef CLANG_ENABLE_ARCMT
95-
case MigrateSource: return new arcmt::MigrateSourceAction();
96+
case MigrateSource:
97+
return llvm::make_unique<arcmt::MigrateSourceAction>();
9698
#else
9799
case MigrateSource: Action = "MigrateSource"; break;
98100
#endif
99101
#ifdef CLANG_ENABLE_STATIC_ANALYZER
100-
case RunAnalysis: return new ento::AnalysisAction();
102+
case RunAnalysis: return llvm::make_unique<ento::AnalysisAction>();
101103
#else
102104
case RunAnalysis: Action = "RunAnalysis"; break;
103105
#endif
104-
case RunPreprocessorOnly: return new PreprocessOnlyAction();
106+
case RunPreprocessorOnly: return llvm::make_unique<PreprocessOnlyAction>();
105107
}
106108

107109
#if !defined(CLANG_ENABLE_ARCMT) || !defined(CLANG_ENABLE_STATIC_ANALYZER) \
@@ -113,16 +115,17 @@ static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
113115
#endif
114116
}
115117

116-
static FrontendAction *CreateFrontendAction(CompilerInstance &CI) {
118+
static std::unique_ptr<FrontendAction>
119+
CreateFrontendAction(CompilerInstance &CI) {
117120
// Create the underlying action.
118-
FrontendAction *Act = CreateFrontendBaseAction(CI);
121+
std::unique_ptr<FrontendAction> Act = CreateFrontendBaseAction(CI);
119122
if (!Act)
120123
return nullptr;
121124

122125
const FrontendOptions &FEOpts = CI.getFrontendOpts();
123126

124127
if (FEOpts.FixAndRecompile) {
125-
Act = new FixItRecompile(Act);
128+
Act = llvm::make_unique<FixItRecompile>(std::move(Act));
126129
}
127130

128131
#ifdef CLANG_ENABLE_ARCMT
@@ -133,30 +136,32 @@ static FrontendAction *CreateFrontendAction(CompilerInstance &CI) {
133136
case FrontendOptions::ARCMT_None:
134137
break;
135138
case FrontendOptions::ARCMT_Check:
136-
Act = new arcmt::CheckAction(Act);
139+
Act = llvm::make_unique<arcmt::CheckAction>(std::move(Act));
137140
break;
138141
case FrontendOptions::ARCMT_Modify:
139-
Act = new arcmt::ModifyAction(Act);
142+
Act = llvm::make_unique<arcmt::ModifyAction>(std::move(Act));
140143
break;
141144
case FrontendOptions::ARCMT_Migrate:
142-
Act = new arcmt::MigrateAction(Act,
145+
Act = llvm::make_unique<arcmt::MigrateAction>(std::move(Act),
143146
FEOpts.MTMigrateDir,
144147
FEOpts.ARCMTMigrateReportOut,
145148
FEOpts.ARCMTMigrateEmitARCErrors);
146149
break;
147150
}
148151

149152
if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
150-
Act = new arcmt::ObjCMigrateAction(Act, FEOpts.MTMigrateDir,
151-
FEOpts.ObjCMTAction);
153+
Act = llvm::make_unique<arcmt::ObjCMigrateAction>(std::move(Act),
154+
FEOpts.MTMigrateDir,
155+
FEOpts.ObjCMTAction);
152156
}
153157
}
154158
#endif
155159

156160
// If there are any AST files to merge, create a frontend action
157161
// adaptor to perform the merge.
158162
if (!FEOpts.ASTMergeFiles.empty())
159-
Act = new ASTMergeAction(Act, FEOpts.ASTMergeFiles);
163+
Act = llvm::make_unique<ASTMergeAction>(std::move(Act),
164+
FEOpts.ASTMergeFiles);
160165

161166
return Act;
162167
}

0 commit comments

Comments
 (0)