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"
|
2010-07-29 09:53:27 +02:00
|
|
|
|
2010-07-29 20:08:23 +02:00
|
|
|
#include "llvm/LLVMContext.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Type.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2010-09-13 20:02:47 +02:00
|
|
|
#include "llvm/Support/IRReader.h"
|
2010-07-29 20:08:23 +02:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2010-07-29 09:53:27 +02:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
DiffConsumer Consumer(LModule, RModule);
|
|
|
|
DifferenceEngine Engine(Context, Consumer);
|
|
|
|
|
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();
|
|
|
|
}
|