Skip to content

Commit

Permalink
Add --output to redirect output to a file
Browse files Browse the repository at this point in the history
  • Loading branch information
Freed-Wu committed Nov 18, 2024
1 parent 751c3d9 commit 2fbdc68
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
51 changes: 49 additions & 2 deletions Insights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include "llvm/Support/raw_ostream.h"

#include <vector>
#include <iostream>
#include <fstream>

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

static llvm::cl::opt<bool>
gUseLibCpp("use-libc++", llvm::cl::desc("Use libc++."sv), llvm::cl::init(false), llvm::cl::cat(gInsightCategory));

static llvm::cl::opt<std::string>
gOutput("output", llvm::cl::desc("Redirect output into a file."sv), llvm::cl::init(""), llvm::cl::cat(gInsightCategory));
//-----------------------------------------------------------------------------

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

// https://stackoverflow.com/a/76507816
class hijack_raw_fd_ostream : public llvm::raw_fd_ostream {
public:
std::stringstream mSs;
explicit hijack_raw_fd_ostream()
: llvm::raw_fd_ostream(-1, false, false) {
}

protected:

void write_impl(const char *Ptr, size_t Size) override {
std::string_view sv(Ptr, Ptr + Size);
mSs << sv;
}

uint64_t current_pos() const override {
llvm::report_fatal_error("current_pos not implemented!");
}

size_t preferred_buffer_size() const override {

Check warning on line 325 in Insights.cpp

View check run for this annotation

Codecov / codecov/patch

Insights.cpp#L324-L325

Added lines #L324 - L325 were not covered by tests
return 0;
}
} hijack_stream;

static std::stringstream ss;

class CppInsightFrontendAction final : public ASTFrontendAction
{
Rewriter mRewriter{};
Expand All @@ -307,7 +338,9 @@ class CppInsightFrontendAction final : public ASTFrontendAction
CppInsightFrontendAction() = default;
void EndSourceFileAction() override
{
mRewriter.getEditBuffer(mRewriter.getSourceMgr().getMainFileID()).write(llvm::outs());
mRewriter.getEditBuffer(mRewriter.getSourceMgr().getMainFileID()).write(hijack_stream);
ss << hijack_stream.mSs.str();
hijack_stream.mSs = {};
}

std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance& CI, StringRef /*file*/) override
Expand Down Expand Up @@ -455,6 +488,20 @@ extern struct __mptr* __vtbl_array[];
EnableGlobalInsert(FuncCxaAtExit);
}

return tool.run(newFrontendActionFactory<CppInsightFrontendAction>().get());
int status = tool.run(newFrontendActionFactory<CppInsightFrontendAction>().get());
if (status)
return status;
std::string fname = gOutput.getValue();
if (fname == "") {
std::cout << ss.str();
} else {
std::fstream f;
f.open(fname, std::ios::out);
if (f.fail())
return 1;
f << ss.str();
}
ss = {};

Check warning on line 504 in Insights.cpp

View check run for this annotation

Codecov / codecov/patch

Insights.cpp#L504

Added line #L504 was not covered by tests
return status;
}
//-----------------------------------------------------------------------------
2 changes: 2 additions & 0 deletions tests/testSTDIN.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ echo "" | $1 -stdin $testCppfile -- -std=c++17 > /dev/null
# Testing an option
$1 -version

$1 AutoHandler3Test.cpp -output /tmp/output.cpp && [ -f /tmp/output.cpp ]

# close stdin
exec 0<&-

Expand Down

0 comments on commit 2fbdc68

Please sign in to comment.