Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --output to redirect output to a file #667

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
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 @@
};
//-----------------------------------------------------------------------------

// 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 @@
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 @@
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
Loading