2010-07-29 20:08:23 +02:00
|
|
|
//===-- llvm-diff.cpp - Module comparator command-line driver ---*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the command-line driver for the difference engine.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2010-07-29 09:53:27 +02:00
|
|
|
|
2011-03-14 23:22:46 +01:00
|
|
|
#include "DiffLog.h"
|
2010-07-29 20:08:23 +02:00
|
|
|
#include "DifferenceEngine.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
2013-03-26 03:25:37 +01:00
|
|
|
#include "llvm/IRReader/IRReader.h"
|
2010-07-29 20:08:23 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2012-12-04 11:44:52 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-07-29 20:08:23 +02:00
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
2010-07-29 09:53:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2010-09-13 20:02:47 +02:00
|
|
|
/// Reads a module from a file. On error, messages are written to stderr
|
|
|
|
/// and null is returned.
|
2010-07-29 09:53:27 +02:00
|
|
|
static Module *ReadModule(LLVMContext &Context, StringRef Name) {
|
2010-09-13 20:02:47 +02:00
|
|
|
SMDiagnostic Diag;
|
|
|
|
Module *M = ParseIRFile(Name, Diag, Context);
|
|
|
|
if (!M)
|
2011-10-16 06:47:35 +02:00
|
|
|
Diag.print("llvm-diff", errs());
|
2010-09-13 20:02:47 +02:00
|
|
|
return M;
|
2010-07-29 09:53:27 +02:00
|
|
|
}
|
|
|
|
|
2010-07-29 19:55:00 +02:00
|
|
|
static void diffGlobal(DifferenceEngine &Engine, Module *L, Module *R,
|
|
|
|
StringRef Name) {
|
|
|
|
// Drop leading sigils from the global name.
|
|
|
|
if (Name.startswith("@")) Name = Name.substr(1);
|
|
|
|
|
|
|
|
Function *LFn = L->getFunction(Name);
|
|
|
|
Function *RFn = R->getFunction(Name);
|
|
|
|
if (LFn && RFn)
|
|
|
|
Engine.diff(LFn, RFn);
|
|
|
|
else if (!LFn && !RFn)
|
|
|
|
errs() << "No function named @" << Name << " in either module\n";
|
|
|
|
else if (!LFn)
|
|
|
|
errs() << "No function named @" << Name << " in left module\n";
|
|
|
|
else
|
|
|
|
errs() << "No function named @" << Name << " in right module\n";
|
|
|
|
}
|
|
|
|
|
2010-09-05 23:25:43 +02:00
|
|
|
static cl::opt<std::string> LeftFilename(cl::Positional,
|
|
|
|
cl::desc("<first file>"),
|
|
|
|
cl::Required);
|
|
|
|
static cl::opt<std::string> RightFilename(cl::Positional,
|
|
|
|
cl::desc("<second file>"),
|
|
|
|
cl::Required);
|
|
|
|
static cl::list<std::string> GlobalsToCompare(cl::Positional,
|
|
|
|
cl::desc("<globals to compare>"));
|
2010-07-29 09:53:27 +02:00
|
|
|
|
2010-07-29 19:55:00 +02:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
cl::ParseCommandLineOptions(argc, argv);
|
2010-07-29 09:53:27 +02:00
|
|
|
|
|
|
|
LLVMContext Context;
|
2013-09-19 01:31:10 +02:00
|
|
|
|
2010-07-29 09:53:27 +02:00
|
|
|
// Load both modules. Die if that fails.
|
2010-07-29 19:55:00 +02:00
|
|
|
Module *LModule = ReadModule(Context, LeftFilename);
|
|
|
|
Module *RModule = ReadModule(Context, RightFilename);
|
2010-07-29 09:53:27 +02:00
|
|
|
if (!LModule || !RModule) return 1;
|
|
|
|
|
2012-06-06 20:25:08 +02:00
|
|
|
DiffConsumer Consumer;
|
|
|
|
DifferenceEngine Engine(Consumer);
|
2010-07-29 09:53:27 +02:00
|
|
|
|
2010-07-29 19:55:00 +02:00
|
|
|
// If any global names were given, just diff those.
|
|
|
|
if (!GlobalsToCompare.empty()) {
|
|
|
|
for (unsigned I = 0, E = GlobalsToCompare.size(); I != E; ++I)
|
|
|
|
diffGlobal(Engine, LModule, RModule, GlobalsToCompare[I]);
|
|
|
|
|
|
|
|
// Otherwise, diff everything in the module.
|
2010-07-29 09:53:27 +02:00
|
|
|
} else {
|
|
|
|
Engine.diff(LModule, RModule);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete LModule;
|
|
|
|
delete RModule;
|
|
|
|
|
|
|
|
return Consumer.hadDifferences();
|
|
|
|
}
|