Prevent output of bytecode to std::cout unless the --force flag is given.
[oota-llvm.git] / tools / llvm-as / llvm-as.cpp
index 7a777a4bab4638d15c32f24aeaf3470c7260d718..ef1602b6a72c7ee7e84ad67fe9c736838da101bf 100644 (file)
@@ -20,6 +20,7 @@
 #include "llvm/Bytecode/Writer.h"
 #include "llvm/Analysis/Verifier.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/SystemUtils.h"
 #include "llvm/System/Signals.h"
 #include <fstream>
 #include <iostream>
@@ -42,7 +43,7 @@ DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
 
 static cl::opt<bool> 
 NoCompress("disable-compression", cl::init(false),
-           cl::desc("Don't ompress the generated bytecode"));
+           cl::desc("Don't compress the generated bytecode"));
 
 static cl::opt<bool>
 DisableVerify("disable-verify", cl::Hidden,
@@ -52,6 +53,7 @@ int main(int argc, char **argv) {
   cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
   sys::PrintStackTraceOnErrorSignal();
 
+  int exitCode = 0;
   std::ostream *Out = 0;
   try {
     // Parse the file now...
@@ -123,13 +125,21 @@ int main(int argc, char **argv) {
       return 1;
     }
    
-    WriteBytecodeToFile(M.get(), *Out, !NoCompress);
+    if (Force || !CheckBytecodeOutputToConsole(Out,true)) {
+      WriteBytecodeToFile(M.get(), *Out, !NoCompress);
+    }
   } catch (const ParseException &E) {
     std::cerr << argv[0] << ": " << E.getMessage() << "\n";
-    return 1;
+    exitCode = 1;
+  } catch (const std::string& msg) {
+    std::cerr << argv[0] << ": " << msg << "\n";
+    exitCode = 1;
+  } catch (...) {
+    std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
+    exitCode = 1;
   }
 
   if (Out != &std::cout) delete Out;
-  return 0;
+  return exitCode;
 }