From d8dbff6413a2aff20c5a00a0b082bd4e95770313 Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Sun, 7 Feb 2016 19:28:36 +0000 Subject: [Frontend] Make the memory management of FrontendAction pointers explicit by using unique_ptr. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@260048 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/ARCMigrate/ARCMTActions.cpp | 12 ++-- lib/ARCMigrate/ObjCMT.cpp | 5 +- lib/Frontend/ASTMerge.cpp | 5 +- lib/Frontend/FrontendAction.cpp | 5 +- lib/FrontendTool/ExecuteCompilerInvocation.cpp | 89 ++++++++++++++------------ 5 files changed, 61 insertions(+), 55 deletions(-) (limited to 'lib') diff --git a/lib/ARCMigrate/ARCMTActions.cpp b/lib/ARCMigrate/ARCMTActions.cpp index 39a922f426..0a5473ab19 100644 --- a/lib/ARCMigrate/ARCMTActions.cpp +++ b/lib/ARCMigrate/ARCMTActions.cpp @@ -25,8 +25,8 @@ bool CheckAction::BeginInvocation(CompilerInstance &CI) { return true; } -CheckAction::CheckAction(FrontendAction *WrappedAction) - : WrapperFrontendAction(WrappedAction) {} +CheckAction::CheckAction(std::unique_ptr WrappedAction) + : WrapperFrontendAction(std::move(WrappedAction)) {} bool ModifyAction::BeginInvocation(CompilerInstance &CI) { return !arcmt::applyTransformations(CI.getInvocation(), getCurrentInput(), @@ -34,8 +34,8 @@ bool ModifyAction::BeginInvocation(CompilerInstance &CI) { CI.getDiagnostics().getClient()); } -ModifyAction::ModifyAction(FrontendAction *WrappedAction) - : WrapperFrontendAction(WrappedAction) {} +ModifyAction::ModifyAction(std::unique_ptr WrappedAction) + : WrapperFrontendAction(std::move(WrappedAction)) {} bool MigrateAction::BeginInvocation(CompilerInstance &CI) { if (arcmt::migrateWithTemporaryFiles( @@ -49,11 +49,11 @@ bool MigrateAction::BeginInvocation(CompilerInstance &CI) { return true; } -MigrateAction::MigrateAction(FrontendAction *WrappedAction, +MigrateAction::MigrateAction(std::unique_ptr WrappedAction, StringRef migrateDir, StringRef plistOut, bool emitPremigrationARCErrors) - : WrapperFrontendAction(WrappedAction), MigrateDir(migrateDir), + : WrapperFrontendAction(std::move(WrappedAction)), MigrateDir(migrateDir), PlistOut(plistOut), EmitPremigrationARCErros(emitPremigrationARCErrors) { if (MigrateDir.empty()) MigrateDir = "."; // user current directory if none is given. diff --git a/lib/ARCMigrate/ObjCMT.cpp b/lib/ARCMigrate/ObjCMT.cpp index 1be724c38a..3737914a7c 100644 --- a/lib/ARCMigrate/ObjCMT.cpp +++ b/lib/ARCMigrate/ObjCMT.cpp @@ -179,10 +179,11 @@ protected: } -ObjCMigrateAction::ObjCMigrateAction(FrontendAction *WrappedAction, +ObjCMigrateAction::ObjCMigrateAction( + std::unique_ptr WrappedAction, StringRef migrateDir, unsigned migrateAction) - : WrapperFrontendAction(WrappedAction), MigrateDir(migrateDir), + : WrapperFrontendAction(std::move(WrappedAction)), MigrateDir(migrateDir), ObjCMigAction(migrateAction), CompInst(nullptr) { if (MigrateDir.empty()) diff --git a/lib/Frontend/ASTMerge.cpp b/lib/Frontend/ASTMerge.cpp index b499fa2b0e..51064da270 100644 --- a/lib/Frontend/ASTMerge.cpp +++ b/lib/Frontend/ASTMerge.cpp @@ -83,14 +83,13 @@ void ASTMergeAction::EndSourceFileAction() { return AdaptedAction->EndSourceFileAction(); } -ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction, +ASTMergeAction::ASTMergeAction(std::unique_ptr adaptedAction, ArrayRef ASTFiles) - : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles.begin(), ASTFiles.end()) { +: AdaptedAction(std::move(adaptedAction)), ASTFiles(ASTFiles.begin(), ASTFiles.end()) { assert(AdaptedAction && "ASTMergeAction needs an action to adapt"); } ASTMergeAction::~ASTMergeAction() { - delete AdaptedAction; } bool ASTMergeAction::usesPreprocessorOnly() const { diff --git a/lib/Frontend/FrontendAction.cpp b/lib/Frontend/FrontendAction.cpp index ecef92e0a7..a8166a647d 100644 --- a/lib/Frontend/FrontendAction.cpp +++ b/lib/Frontend/FrontendAction.cpp @@ -587,6 +587,7 @@ bool WrapperFrontendAction::hasCodeCompletionSupport() const { return WrappedAction->hasCodeCompletionSupport(); } -WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction) - : WrappedAction(WrappedAction) {} +WrapperFrontendAction::WrapperFrontendAction( + std::unique_ptr WrappedAction) + : WrappedAction(std::move(WrappedAction)) {} diff --git a/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/lib/FrontendTool/ExecuteCompilerInvocation.cpp index 79cf0049a7..116590e537 100644 --- a/lib/FrontendTool/ExecuteCompilerInvocation.cpp +++ b/lib/FrontendTool/ExecuteCompilerInvocation.cpp @@ -31,33 +31,34 @@ using namespace clang; using namespace llvm::opt; -static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) { +static std::unique_ptr +CreateFrontendBaseAction(CompilerInstance &CI) { using namespace clang::frontend; StringRef Action("unknown"); (void)Action; switch (CI.getFrontendOpts().ProgramAction) { - case ASTDeclList: return new ASTDeclListAction(); - case ASTDump: return new ASTDumpAction(); - case ASTPrint: return new ASTPrintAction(); - case ASTView: return new ASTViewAction(); - case DumpRawTokens: return new DumpRawTokensAction(); - case DumpTokens: return new DumpTokensAction(); - case EmitAssembly: return new EmitAssemblyAction(); - case EmitBC: return new EmitBCAction(); - case EmitHTML: return new HTMLPrintAction(); - case EmitLLVM: return new EmitLLVMAction(); - case EmitLLVMOnly: return new EmitLLVMOnlyAction(); - case EmitCodeGenOnly: return new EmitCodeGenOnlyAction(); - case EmitObj: return new EmitObjAction(); - case FixIt: return new FixItAction(); - case GenerateModule: return new GenerateModuleAction; - case GeneratePCH: return new GeneratePCHAction; - case GeneratePTH: return new GeneratePTHAction(); - case InitOnly: return new InitOnlyAction(); - case ParseSyntaxOnly: return new SyntaxOnlyAction(); - case ModuleFileInfo: return new DumpModuleInfoAction(); - case VerifyPCH: return new VerifyPCHAction(); + case ASTDeclList: return llvm::make_unique(); + case ASTDump: return llvm::make_unique(); + case ASTPrint: return llvm::make_unique(); + case ASTView: return llvm::make_unique(); + case DumpRawTokens: return llvm::make_unique(); + case DumpTokens: return llvm::make_unique(); + case EmitAssembly: return llvm::make_unique(); + case EmitBC: return llvm::make_unique(); + case EmitHTML: return llvm::make_unique(); + case EmitLLVM: return llvm::make_unique(); + case EmitLLVMOnly: return llvm::make_unique(); + case EmitCodeGenOnly: return llvm::make_unique(); + case EmitObj: return llvm::make_unique(); + case FixIt: return llvm::make_unique(); + case GenerateModule: return llvm::make_unique(); + case GeneratePCH: return llvm::make_unique(); + case GeneratePTH: return llvm::make_unique(); + case InitOnly: return llvm::make_unique(); + case ParseSyntaxOnly: return llvm::make_unique(); + case ModuleFileInfo: return llvm::make_unique(); + case VerifyPCH: return llvm::make_unique(); case PluginAction: { for (FrontendPluginRegistry::iterator it = @@ -67,7 +68,7 @@ static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) { std::unique_ptr P(it->instantiate()); if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs)) return nullptr; - return P.release(); + return std::move(P); } } @@ -76,32 +77,33 @@ static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) { return nullptr; } - case PrintDeclContext: return new DeclContextPrintAction(); - case PrintPreamble: return new PrintPreambleAction(); + case PrintDeclContext: return llvm::make_unique(); + case PrintPreamble: return llvm::make_unique(); case PrintPreprocessedInput: { if (CI.getPreprocessorOutputOpts().RewriteIncludes) - return new RewriteIncludesAction(); - return new PrintPreprocessedAction(); + return llvm::make_unique(); + return llvm::make_unique(); } - case RewriteMacros: return new RewriteMacrosAction(); - case RewriteTest: return new RewriteTestAction(); + case RewriteMacros: return llvm::make_unique(); + case RewriteTest: return llvm::make_unique(); #ifdef CLANG_ENABLE_OBJC_REWRITER - case RewriteObjC: return new RewriteObjCAction(); + case RewriteObjC: return llvm::make_unique(); #else case RewriteObjC: Action = "RewriteObjC"; break; #endif #ifdef CLANG_ENABLE_ARCMT - case MigrateSource: return new arcmt::MigrateSourceAction(); + case MigrateSource: + return llvm::make_unique(); #else case MigrateSource: Action = "MigrateSource"; break; #endif #ifdef CLANG_ENABLE_STATIC_ANALYZER - case RunAnalysis: return new ento::AnalysisAction(); + case RunAnalysis: return llvm::make_unique(); #else case RunAnalysis: Action = "RunAnalysis"; break; #endif - case RunPreprocessorOnly: return new PreprocessOnlyAction(); + case RunPreprocessorOnly: return llvm::make_unique(); } #if !defined(CLANG_ENABLE_ARCMT) || !defined(CLANG_ENABLE_STATIC_ANALYZER) \ @@ -113,16 +115,17 @@ static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) { #endif } -static FrontendAction *CreateFrontendAction(CompilerInstance &CI) { +static std::unique_ptr +CreateFrontendAction(CompilerInstance &CI) { // Create the underlying action. - FrontendAction *Act = CreateFrontendBaseAction(CI); + std::unique_ptr Act = CreateFrontendBaseAction(CI); if (!Act) return nullptr; const FrontendOptions &FEOpts = CI.getFrontendOpts(); if (FEOpts.FixAndRecompile) { - Act = new FixItRecompile(Act); + Act = llvm::make_unique(std::move(Act)); } #ifdef CLANG_ENABLE_ARCMT @@ -133,13 +136,13 @@ static FrontendAction *CreateFrontendAction(CompilerInstance &CI) { case FrontendOptions::ARCMT_None: break; case FrontendOptions::ARCMT_Check: - Act = new arcmt::CheckAction(Act); + Act = llvm::make_unique(std::move(Act)); break; case FrontendOptions::ARCMT_Modify: - Act = new arcmt::ModifyAction(Act); + Act = llvm::make_unique(std::move(Act)); break; case FrontendOptions::ARCMT_Migrate: - Act = new arcmt::MigrateAction(Act, + Act = llvm::make_unique(std::move(Act), FEOpts.MTMigrateDir, FEOpts.ARCMTMigrateReportOut, FEOpts.ARCMTMigrateEmitARCErrors); @@ -147,8 +150,9 @@ static FrontendAction *CreateFrontendAction(CompilerInstance &CI) { } if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) { - Act = new arcmt::ObjCMigrateAction(Act, FEOpts.MTMigrateDir, - FEOpts.ObjCMTAction); + Act = llvm::make_unique(std::move(Act), + FEOpts.MTMigrateDir, + FEOpts.ObjCMTAction); } } #endif @@ -156,7 +160,8 @@ static FrontendAction *CreateFrontendAction(CompilerInstance &CI) { // If there are any AST files to merge, create a frontend action // adaptor to perform the merge. if (!FEOpts.ASTMergeFiles.empty()) - Act = new ASTMergeAction(Act, FEOpts.ASTMergeFiles); + Act = llvm::make_unique(std::move(Act), + FEOpts.ASTMergeFiles); return Act; } -- cgit v1.2.1