3838#include " llvm/Support/Debug.h"
3939#include " llvm/Support/Errc.h"
4040#include " llvm/Support/InitLLVM.h"
41+ #include " llvm/Support/MemoryBuffer.h"
4142#include " llvm/Support/SourceMgr.h"
4243#include " llvm/Support/raw_ostream.h"
4344
@@ -48,10 +49,10 @@ namespace ir2vec {
4849
4950static cl::OptionCategory IR2VecToolCategory (" IR2Vec Tool Options" );
5051
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));
5556
5657static cl::opt<std::string> OutputFilename (" o" , cl::desc(" Output filename" ),
5758 cl::value_desc(" filename" ),
@@ -283,10 +284,24 @@ int main(int argc, char **argv) {
283284 if (Mode == TripletMode && Level.getNumOccurrences () > 0 )
284285 errs () << " Warning: --level option is ignored in triplet mode\n " ;
285286
286- // Parse the input LLVM IR file
287+ // Parse the input LLVM IR file or stdin
287288 SMDiagnostic Err;
288289 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+
290305 if (!M) {
291306 Err.print (argv[0 ], errs ());
292307 return 1 ;
0 commit comments