18
18
#include " llvm/Support/raw_ostream.h"
19
19
20
20
#include < vector>
21
+ #include < iostream>
22
+ #include < fstream>
21
23
22
24
#include " CodeGenerator.h"
23
25
#include " DPrint.h"
@@ -57,6 +59,9 @@ static llvm::cl::opt<bool> gStdinMode("stdin",
57
59
58
60
static llvm::cl::opt<bool >
59
61
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 ));
60
65
// -----------------------------------------------------------------------------
61
66
62
67
#define INSIGHTS_OPT (option, name, deflt, description, category ) \
@@ -298,6 +303,32 @@ class CppInsightASTConsumer final : public ASTConsumer
298
303
};
299
304
// -----------------------------------------------------------------------------
300
305
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
+
301
332
class CppInsightFrontendAction final : public ASTFrontendAction
302
333
{
303
334
Rewriter mRewriter {};
@@ -307,7 +338,8 @@ class CppInsightFrontendAction final : public ASTFrontendAction
307
338
CppInsightFrontendAction () = default ;
308
339
void EndSourceFileAction () override
309
340
{
310
- mRewriter .getEditBuffer (mRewriter .getSourceMgr ().getMainFileID ()).write (llvm::outs ());
341
+ mRewriter .getEditBuffer (mRewriter .getSourceMgr ().getMainFileID ()).write (hijack_stream);
342
+ ss << hijack_stream.mSs .str ();
311
343
}
312
344
313
345
std::unique_ptr<ASTConsumer> CreateASTConsumer (CompilerInstance& CI, StringRef /* file*/ ) override
@@ -455,6 +487,19 @@ extern struct __mptr* __vtbl_array[];
455
487
EnableGlobalInsert (FuncCxaAtExit);
456
488
}
457
489
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;
459
504
}
460
505
// -----------------------------------------------------------------------------
0 commit comments