38
38
#include " llvm/Support/Debug.h"
39
39
#include " llvm/Support/Errc.h"
40
40
#include " llvm/Support/InitLLVM.h"
41
+ #include " llvm/Support/MemoryBuffer.h"
41
42
#include " llvm/Support/SourceMgr.h"
42
43
#include " llvm/Support/raw_ostream.h"
43
44
@@ -48,10 +49,10 @@ namespace ir2vec {
48
49
49
50
static cl::OptionCategory IR2VecToolCategory (" IR2Vec Tool Options" );
50
51
51
- static cl::opt<std::string> InputFilename (cl::Positional,
52
- cl::desc ( " <input bitcode file> " ) ,
53
- cl::Required ,
54
- cl::cat(IR2VecToolCategory));
52
+ static cl::opt<std::string>
53
+ InputFilename ( cl::Positional ,
54
+ cl::desc ( " <input bitcode file or '-' for stdin> " ) ,
55
+ cl::init( " - " ), cl::cat(IR2VecToolCategory));
55
56
56
57
static cl::opt<std::string> OutputFilename (" o" , cl::desc(" Output filename" ),
57
58
cl::value_desc(" filename" ),
@@ -283,10 +284,24 @@ int main(int argc, char **argv) {
283
284
if (Mode == TripletMode && Level.getNumOccurrences () > 0 )
284
285
errs () << " Warning: --level option is ignored in triplet mode\n " ;
285
286
286
- // Parse the input LLVM IR file
287
+ // Parse the input LLVM IR file or stdin
287
288
SMDiagnostic Err;
288
289
LLVMContext Context;
289
- std::unique_ptr<Module> M = parseIRFile (InputFilename, Err, Context);
290
+ std::unique_ptr<Module> M;
291
+
292
+ if (InputFilename == " -" ) {
293
+ // Read from stdin
294
+ auto StdinBuffer = MemoryBuffer::getSTDIN ();
295
+ if (std::error_code EC = StdinBuffer.getError ()) {
296
+ errs () << " Error reading from stdin: " << EC.message () << " \n " ;
297
+ return 1 ;
298
+ }
299
+ M = parseIR (StdinBuffer.get ()->getMemBufferRef (), Err, Context);
300
+ } else {
301
+ // Read from file
302
+ M = parseIRFile (InputFilename, Err, Context);
303
+ }
304
+
290
305
if (!M) {
291
306
Err.print (argv[0 ], errs ());
292
307
return 1 ;
0 commit comments