1
//===-- llvm-diff.cpp - Module comparator command-line driver ---*- C++ -*-===//
3
// The LLVM Compiler Infrastructure
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
8
//===----------------------------------------------------------------------===//
10
// This file defines the command-line driver for the difference engine.
12
//===----------------------------------------------------------------------===//
15
#include "DifferenceEngine.h"
16
#include "llvm/ADT/DenseMap.h"
17
#include "llvm/ADT/SmallVector.h"
18
#include "llvm/ADT/StringRef.h"
19
#include "llvm/IR/LLVMContext.h"
20
#include "llvm/IR/Module.h"
21
#include "llvm/IR/Type.h"
22
#include "llvm/IRReader/IRReader.h"
23
#include "llvm/Support/CommandLine.h"
24
#include "llvm/Support/MemoryBuffer.h"
25
#include "llvm/Support/SourceMgr.h"
26
#include "llvm/Support/raw_ostream.h"
33
/// Reads a module from a file. On error, messages are written to stderr
34
/// and null is returned.
35
static std::unique_ptr<Module> readModule(LLVMContext &Context,
38
std::unique_ptr<Module> M = parseIRFile(Name, Diag, Context);
40
Diag.print("llvm-diff", errs());
44
static void diffGlobal(DifferenceEngine &Engine, Module &L, Module &R,
46
// Drop leading sigils from the global name.
47
if (Name.startswith("@")) Name = Name.substr(1);
49
Function *LFn = L.getFunction(Name);
50
Function *RFn = R.getFunction(Name);
52
Engine.diff(LFn, RFn);
53
else if (!LFn && !RFn)
54
errs() << "No function named @" << Name << " in either module\n";
56
errs() << "No function named @" << Name << " in left module\n";
58
errs() << "No function named @" << Name << " in right module\n";
61
static cl::opt<std::string> LeftFilename(cl::Positional,
62
cl::desc("<first file>"),
64
static cl::opt<std::string> RightFilename(cl::Positional,
65
cl::desc("<second file>"),
67
static cl::list<std::string> GlobalsToCompare(cl::Positional,
68
cl::desc("<globals to compare>"));
70
int main(int argc, char **argv) {
71
cl::ParseCommandLineOptions(argc, argv);
75
// Load both modules. Die if that fails.
76
std::unique_ptr<Module> LModule = readModule(Context, LeftFilename);
77
std::unique_ptr<Module> RModule = readModule(Context, RightFilename);
78
if (!LModule || !RModule) return 1;
80
DiffConsumer Consumer;
81
DifferenceEngine Engine(Consumer);
83
// If any global names were given, just diff those.
84
if (!GlobalsToCompare.empty()) {
85
for (unsigned I = 0, E = GlobalsToCompare.size(); I != E; ++I)
86
diffGlobal(Engine, *LModule, *RModule, GlobalsToCompare[I]);
88
// Otherwise, diff everything in the module.
90
Engine.diff(LModule.get(), RModule.get());
93
return Consumer.hadDifferences();