2008-10-21 23:33:38 +00:00
|
|
|
//===--- VMCore/PrintModulePass.cpp - Module/Function Printer -------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// PrintModulePass and PrintFunctionPass implementations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Assembly/PrintModulePass.h"
|
|
|
|
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
2010-01-05 01:30:18 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2008-10-22 03:25:22 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-10-21 23:33:38 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2009-10-25 06:33:48 +00:00
|
|
|
class PrintModulePass : public ModulePass {
|
2008-10-22 03:25:22 +00:00
|
|
|
raw_ostream *Out; // raw_ostream to print on
|
2008-10-21 23:33:38 +00:00
|
|
|
bool DeleteStream; // Delete the ostream in our dtor?
|
|
|
|
public:
|
|
|
|
static char ID;
|
2010-01-05 01:30:18 +00:00
|
|
|
PrintModulePass() : ModulePass(&ID), Out(&dbgs()),
|
2008-10-21 23:33:38 +00:00
|
|
|
DeleteStream(false) {}
|
2008-10-22 03:25:22 +00:00
|
|
|
PrintModulePass(raw_ostream *o, bool DS)
|
2009-02-18 05:09:16 +00:00
|
|
|
: ModulePass(&ID), Out(o), DeleteStream(DS) {}
|
2008-10-21 23:33:38 +00:00
|
|
|
|
|
|
|
~PrintModulePass() {
|
|
|
|
if (DeleteStream) delete Out;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnModule(Module &M) {
|
2008-10-22 03:25:22 +00:00
|
|
|
(*Out) << M;
|
2008-10-21 23:33:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class PrintFunctionPass : public FunctionPass {
|
|
|
|
std::string Banner; // String to print before each function
|
2008-10-22 03:25:22 +00:00
|
|
|
raw_ostream *Out; // raw_ostream to print on
|
2008-10-21 23:33:38 +00:00
|
|
|
bool DeleteStream; // Delete the ostream in our dtor?
|
|
|
|
public:
|
|
|
|
static char ID;
|
2010-01-05 01:30:18 +00:00
|
|
|
PrintFunctionPass() : FunctionPass(&ID), Banner(""), Out(&dbgs()),
|
2008-10-21 23:33:38 +00:00
|
|
|
DeleteStream(false) {}
|
2008-10-22 03:25:22 +00:00
|
|
|
PrintFunctionPass(const std::string &B, raw_ostream *o, bool DS)
|
2009-02-18 05:09:16 +00:00
|
|
|
: FunctionPass(&ID), Banner(B), Out(o), DeleteStream(DS) {}
|
2008-10-21 23:33:38 +00:00
|
|
|
|
|
|
|
inline ~PrintFunctionPass() {
|
|
|
|
if (DeleteStream) delete Out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// runOnFunction - This pass just prints a banner followed by the
|
|
|
|
// function as it's processed.
|
|
|
|
//
|
|
|
|
bool runOnFunction(Function &F) {
|
|
|
|
(*Out) << Banner << static_cast<Value&>(F);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
char PrintModulePass::ID = 0;
|
|
|
|
static RegisterPass<PrintModulePass>
|
|
|
|
X("print-module", "Print module to stderr");
|
|
|
|
char PrintFunctionPass::ID = 0;
|
|
|
|
static RegisterPass<PrintFunctionPass>
|
|
|
|
Y("print-function","Print function to stderr");
|
|
|
|
|
|
|
|
/// createPrintModulePass - Create and return a pass that writes the
|
2008-10-22 03:25:22 +00:00
|
|
|
/// module to the specified raw_ostream.
|
|
|
|
ModulePass *llvm::createPrintModulePass(llvm::raw_ostream *OS,
|
|
|
|
bool DeleteStream) {
|
2008-10-21 23:33:38 +00:00
|
|
|
return new PrintModulePass(OS, DeleteStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// createPrintFunctionPass - Create and return a pass that prints
|
2008-10-22 03:25:22 +00:00
|
|
|
/// functions to the specified raw_ostream as they are processed.
|
2008-10-21 23:33:38 +00:00
|
|
|
FunctionPass *llvm::createPrintFunctionPass(const std::string &Banner,
|
2008-10-22 03:25:22 +00:00
|
|
|
llvm::raw_ostream *OS,
|
|
|
|
bool DeleteStream) {
|
2008-10-21 23:33:38 +00:00
|
|
|
return new PrintFunctionPass(Banner, OS, DeleteStream);
|
|
|
|
}
|
|
|
|
|