#include "llvm/ADT/Optional.h"
#include "llvm/Bitcode/BitcodeAnalyzer.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/WithColor.h"
#include "llvm/Support/raw_ostream.h"
#include <memory>
using namespace llvm;
static cl::OptionCategory BCAnalyzerCategory("BC Analyzer Options");
static cl::opt<std::string> InputFilename(cl::Positional,
cl::desc("<input bitcode>"),
cl::init("-"),
cl::cat(BCAnalyzerCategory));
static cl::opt<bool> Dump("dump", cl::desc("Dump low level bitcode trace"),
cl::cat(BCAnalyzerCategory));
static cl::opt<bool> DumpBlockinfo("dump-blockinfo",
cl::desc("Include BLOCKINFO details in low"
" level dump"),
cl::cat(BCAnalyzerCategory));
static cl::opt<bool> NoHistogram("disable-histogram",
cl::desc("Do not print per-code histogram"),
cl::cat(BCAnalyzerCategory));
static cl::opt<bool> NonSymbolic("non-symbolic",
cl::desc("Emit numeric info in dump even if"
" symbolic info is available"),
cl::cat(BCAnalyzerCategory));
static cl::opt<std::string>
BlockInfoFilename("block-info",
cl::desc("Use the BLOCK_INFO from the given file"),
cl::cat(BCAnalyzerCategory));
static cl::opt<bool>
ShowBinaryBlobs("show-binary-blobs",
cl::desc("Print binary blobs using hex escapes"),
cl::cat(BCAnalyzerCategory));
static cl::opt<std::string> CheckHash(
"check-hash",
cl::desc("Check module hash using the argument as a string table"),
cl::cat(BCAnalyzerCategory));
static Error reportError(StringRef Message) {
return createStringError(std::errc::illegal_byte_sequence, Message.data());
}
static Expected<std::unique_ptr<MemoryBuffer>> openBitcodeFile(StringRef Path) {
Expected<std::unique_ptr<MemoryBuffer>> MemBufOrErr =
errorOrToExpected(MemoryBuffer::getFileOrSTDIN(Path));
if (Error E = MemBufOrErr.takeError())
return std::move(E);
std::unique_ptr<MemoryBuffer> MemBuf = std::move(*MemBufOrErr);
if (MemBuf->getBufferSize() & 3)
return reportError(
"Bitcode stream should be a multiple of 4 bytes in length");
return std::move(MemBuf);
}
int main(int argc, char **argv) {
InitLLVM X(argc, argv);
cl::HideUnrelatedOptions({&BCAnalyzerCategory, &getColorCategory()});
cl::ParseCommandLineOptions(argc, argv, "llvm-bcanalyzer file analyzer\n");
ExitOnError ExitOnErr("llvm-bcanalyzer: ");
std::unique_ptr<MemoryBuffer> MB = ExitOnErr(openBitcodeFile(InputFilename));
std::unique_ptr<MemoryBuffer> BlockInfoMB = nullptr;
if (!BlockInfoFilename.empty())
BlockInfoMB = ExitOnErr(openBitcodeFile(BlockInfoFilename));
BitcodeAnalyzer BA(MB->getBuffer(),
BlockInfoMB ? Optional<StringRef>(BlockInfoMB->getBuffer())
: None);
BCDumpOptions O(outs());
O.Histogram = !NoHistogram;
O.Symbolic = !NonSymbolic;
O.ShowBinaryBlobs = ShowBinaryBlobs;
O.DumpBlockinfo = DumpBlockinfo;
ExitOnErr(BA.analyze(
Dump ? Optional<BCDumpOptions>(O) : Optional<BCDumpOptions>(None),
CheckHash.empty() ? None : Optional<StringRef>(CheckHash)));
if (Dump)
outs() << "\n\n";
BA.printStats(O, StringRef(InputFilename.getValue()));
return 0;
}