Skip to content

Commit 98949fd

Browse files
committed
Add --output to redirect output to a file
1 parent 751c3d9 commit 98949fd

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

Insights.cpp

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "llvm/Support/raw_ostream.h"
1919

2020
#include <vector>
21+
#include <iostream>
22+
#include <fstream>
2123

2224
#include "CodeGenerator.h"
2325
#include "DPrint.h"
@@ -57,6 +59,9 @@ static llvm::cl::opt<bool> gStdinMode("stdin",
5759

5860
static llvm::cl::opt<bool>
5961
gUseLibCpp("use-libc++", llvm::cl::desc("Use libc++."sv), llvm::cl::init(false), llvm::cl::cat(gInsightCategory));
62+
63+
static llvm::cl::opt<std::string>
64+
gOutput("output", llvm::cl::desc("output file."sv), llvm::cl::init(""), llvm::cl::cat(gInsightCategory));
6065
//-----------------------------------------------------------------------------
6166

6267
#define INSIGHTS_OPT(option, name, deflt, description, category) \
@@ -298,6 +303,32 @@ class CppInsightASTConsumer final : public ASTConsumer
298303
};
299304
//-----------------------------------------------------------------------------
300305

306+
// https://stackoverflow.com/a/76507816
307+
class hijack_raw_fd_ostream : public llvm::raw_fd_ostream {
308+
public:
309+
std::stringstream mSs;
310+
explicit hijack_raw_fd_ostream()
311+
: llvm::raw_fd_ostream(-1, false, false) {
312+
}
313+
314+
protected:
315+
316+
void write_impl(const char *Ptr, size_t Size) override {
317+
std::string_view sv(Ptr, Ptr + Size);
318+
mSs << sv;
319+
}
320+
321+
uint64_t current_pos() const override {
322+
llvm::report_fatal_error("current_pos not implemented!");
323+
}
324+
325+
size_t preferred_buffer_size() const override {
326+
return 0;
327+
}
328+
} hijack_stream;
329+
330+
static std::stringstream ss;
331+
301332
class CppInsightFrontendAction final : public ASTFrontendAction
302333
{
303334
Rewriter mRewriter{};
@@ -307,7 +338,8 @@ class CppInsightFrontendAction final : public ASTFrontendAction
307338
CppInsightFrontendAction() = default;
308339
void EndSourceFileAction() override
309340
{
310-
mRewriter.getEditBuffer(mRewriter.getSourceMgr().getMainFileID()).write(llvm::outs());
341+
mRewriter.getEditBuffer(mRewriter.getSourceMgr().getMainFileID()).write(hijack_stream);
342+
ss << hijack_stream.mSs.str();
311343
}
312344

313345
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance& CI, StringRef /*file*/) override
@@ -455,6 +487,19 @@ extern struct __mptr* __vtbl_array[];
455487
EnableGlobalInsert(FuncCxaAtExit);
456488
}
457489

458-
return tool.run(newFrontendActionFactory<CppInsightFrontendAction>().get());
490+
int status = tool.run(newFrontendActionFactory<CppInsightFrontendAction>().get());
491+
if (status)
492+
return status;
493+
std::string fname = gOutput.getValue();
494+
if (fname == "") {
495+
std::cout << ss.str();
496+
} else {
497+
std::fstream f;
498+
f.open(fname, std::ios::out);
499+
if (f.fail())
500+
return 1;
501+
f << ss.str();
502+
}
503+
return status;
459504
}
460505
//-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)