#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/PluginLoader.h"
#include "llvm/Support/StandardPasses.h"
-#include "llvm/Support/Streams.h"
#include "llvm/Support/SystemUtils.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/LinkAllPasses.h"
#include "llvm/LinkAllVMCore.h"
-#include <iostream>
-#include <fstream>
#include <memory>
#include <algorithm>
using namespace llvm;
virtual bool runOnSCC(const std::vector<CallGraphNode *>&SCC) {
if (!Quiet) {
- cout << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
+ outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
Function *F = SCC[i]->getFunction();
- if (F)
+ if (F) {
+ outs().flush();
getAnalysisID<Pass>(PassToPrint).print(cout, F->getParent());
+ cout << std::flush;
+ }
}
}
// Get and print pass...
virtual bool runOnModule(Module &M) {
if (!Quiet) {
- cout << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
+ outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
+ outs().flush();
getAnalysisID<Pass>(PassToPrint).print(cout, &M);
+ cout << std::flush;
}
// Get and print pass...
virtual bool runOnFunction(Function &F) {
if (!Quiet) {
- cout << "Printing analysis '" << PassToPrint->getPassName()
- << "' for function '" << F.getName() << "':\n";
+ outs() << "Printing analysis '" << PassToPrint->getPassName()
+ << "' for function '" << F.getName() << "':\n";
}
// Get and print pass...
+ outs().flush();
getAnalysisID<Pass>(PassToPrint).print(cout, F.getParent());
+ cout << std::flush;
return false;
}
virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
if (!Quiet) {
- cout << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
- getAnalysisID<Pass>(PassToPrint).print(cout,
+ outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
+ outs().flush();
+ getAnalysisID<Pass>(PassToPrint).print(cout,
L->getHeader()->getParent()->getParent());
+ cout << std::flush;
}
// Get and print pass...
return false;
virtual bool runOnBasicBlock(BasicBlock &BB) {
if (!Quiet) {
- cout << "Printing Analysis info for BasicBlock '" << BB.getName()
- << "': Pass " << PassToPrint->getPassName() << ":\n";
+ outs() << "Printing Analysis info for BasicBlock '" << BB.getName()
+ << "': Pass " << PassToPrint->getPassName() << ":\n";
}
// Get and print pass...
+ outs().flush();
getAnalysisID<Pass>(PassToPrint).print(cout, BB.getParent()->getParent());
+ cout << std::flush;
return false;
}
//
int main(int argc, char **argv) {
llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
- LLVMContext Context;
+ LLVMContext &Context = getGlobalContext();
try {
cl::ParseCommandLineOptions(argc, argv,
"llvm .bc -> .bc modular optimizer and analysis printer\n");
}
if (M.get() == 0) {
- cerr << argv[0] << ": ";
+ errs() << argv[0] << ": ";
if (ErrorMessage.size())
- cerr << ErrorMessage << "\n";
+ errs() << ErrorMessage << "\n";
else
- cerr << "bitcode didn't read correctly.\n";
+ errs() << "bitcode didn't read correctly.\n";
return 1;
}
// Figure out what stream we are supposed to write to...
- // FIXME: cout is not binary!
- std::ostream *Out = &std::cout; // Default to printing to stdout...
+ // FIXME: outs() is not binary!
+ raw_ostream *Out = &outs(); // Default to printing to stdout...
if (OutputFilename != "-") {
- if (!Force && std::ifstream(OutputFilename.c_str())) {
- // If force is not specified, make sure not to overwrite a file!
- cerr << argv[0] << ": error opening '" << OutputFilename
- << "': file exists!\n"
- << "Use -f command line argument to force output\n";
- return 1;
- }
- std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
- std::ios::binary;
- Out = new std::ofstream(OutputFilename.c_str(), io_mode);
-
- if (!Out->good()) {
- cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
+ std::string ErrorInfo;
+ Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true,
+ Force, ErrorInfo);
+ if (!ErrorInfo.empty()) {
+ errs() << ErrorInfo << '\n';
+ if (!Force)
+ errs() << "Use -f command line argument to force output\n";
+ delete Out;
return 1;
}
if (PassInf->getNormalCtor())
P = PassInf->getNormalCtor()();
else
- cerr << argv[0] << ": cannot create pass: "
- << PassInf->getPassName() << "\n";
+ errs() << argv[0] << ": cannot create pass: "
+ << PassInf->getPassName() << "\n";
if (P) {
bool isBBPass = dynamic_cast<BasicBlockPass*>(P) != 0;
bool isLPass = !isBBPass && dynamic_cast<LoopPass*>(P) != 0;
if (!NoVerify && !VerifyEach)
Passes.add(createVerifierPass());
- // Write bitcode out to disk or cout as the last step...
+ // Write bitcode out to disk or outs() as the last step...
if (!NoOutput && !AnalyzeOnly)
- Passes.add(CreateBitcodeWriterPass(*Out));
+ Passes.add(createBitcodeWriterPass(*Out));
// Now that we have all of the passes ready, run them.
Passes.run(*M.get());
- // Delete the ofstream.
- if (Out != &std::cout)
+ // Delete the raw_fd_ostream.
+ if (Out != &outs())
delete Out;
return 0;
} catch (const std::string& msg) {
- cerr << argv[0] << ": " << msg << "\n";
+ errs() << argv[0] << ": " << msg << "\n";
} catch (...) {
- cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
+ errs() << argv[0] << ": Unexpected unknown exception occurred.\n";
}
llvm_shutdown();
return 1;