diff --git a/clang/include/clang/Frontend/FrontendActions.h b/clang/include/clang/Frontend/FrontendActions.h --- a/clang/include/clang/Frontend/FrontendActions.h +++ b/clang/include/clang/Frontend/FrontendActions.h @@ -177,9 +177,8 @@ /// Dump information about the given module file, to be used for /// basic debugging and discovery. class DumpModuleInfoAction : public ASTFrontendAction { -public: // Allow other tools (ex lldb) to direct output for their use. - llvm::raw_ostream *OutputStream = nullptr; + std::shared_ptr OutputStream; protected: std::unique_ptr CreateASTConsumer(CompilerInstance &CI, @@ -188,6 +187,9 @@ void ExecuteAction() override; public: + DumpModuleInfoAction() = default; + explicit DumpModuleInfoAction(std::shared_ptr Out) + : OutputStream(Out) {} bool hasPCHSupport() const override { return false; } bool hasASTFileSupport() const override { return true; } bool hasIRSupport() const override { return false; } diff --git a/clang/lib/Frontend/FrontendActions.cpp b/clang/lib/Frontend/FrontendActions.cpp --- a/clang/lib/Frontend/FrontendActions.cpp +++ b/clang/lib/Frontend/FrontendActions.cpp @@ -780,14 +780,12 @@ void DumpModuleInfoAction::ExecuteAction() { assert(isCurrentFileAST() && "dumping non-AST?"); // Set up the output file. - std::unique_ptr OutFile; CompilerInstance &CI = getCompilerInstance(); StringRef OutputFileName = CI.getFrontendOpts().OutputFile; if (!OutputFileName.empty() && OutputFileName != "-") { std::error_code EC; - OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC, - llvm::sys::fs::OF_TextWithCRLF)); - OutputStream = OutFile.get(); + OutputStream.reset(new llvm::raw_fd_ostream( + OutputFileName.str(), EC, llvm::sys::fs::OF_TextWithCRLF)); } llvm::raw_ostream &Out = OutputStream ? *OutputStream : llvm::outs(); diff --git a/lldb/source/Commands/CommandObjectTarget.cpp b/lldb/source/Commands/CommandObjectTarget.cpp --- a/lldb/source/Commands/CommandObjectTarget.cpp +++ b/lldb/source/Commands/CommandObjectTarget.cpp @@ -2179,8 +2179,11 @@ const char *clang_args[] = {"clang", pcm_path}; compiler.setInvocation(clang::createInvocation(clang_args)); - clang::DumpModuleInfoAction dump_module_info; - dump_module_info.OutputStream = &result.GetOutputStream().AsRawOstream(); + // Pass empty deleter to not attempt to free memory that was allocated + // outside of the current scope, possibly statically. + std::shared_ptr Out( + &result.GetOutputStream().AsRawOstream(), [](llvm::raw_ostream *) {}); + clang::DumpModuleInfoAction dump_module_info(Out); // DumpModuleInfoAction requires ObjectFilePCHContainerReader. compiler.getPCHContainerOperations()->registerReader( std::make_unique());