1//===-- llvm-diff.cpp - Module comparator command-line driver ---*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the command-line driver for the difference engine.
11//
12//===----------------------------------------------------------------------===//
13
14#include "DiffLog.h"
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"
27#include <string>
28#include <utility>
29
30
31using namespace llvm;
32
33/// Reads a module from a file.  On error, messages are written to stderr
34/// and null is returned.
35static std::unique_ptr<Module> readModule(LLVMContext &Context,
36                                          StringRef Name) {
37  SMDiagnostic Diag;
38  std::unique_ptr<Module> M = parseIRFile(Name, Diag, Context);
39  if (!M)
40    Diag.print("llvm-diff", errs());
41  return M;
42}
43
44static void diffGlobal(DifferenceEngine &Engine, Module &L, Module &R,
45                       StringRef Name) {
46  // Drop leading sigils from the global name.
47  if (Name.startswith("@")) Name = Name.substr(1);
48
49  Function *LFn = L.getFunction(Name);
50  Function *RFn = R.getFunction(Name);
51  if (LFn && RFn)
52    Engine.diff(LFn, RFn);
53  else if (!LFn && !RFn)
54    errs() << "No function named @" << Name << " in either module\n";
55  else if (!LFn)
56    errs() << "No function named @" << Name << " in left module\n";
57  else
58    errs() << "No function named @" << Name << " in right module\n";
59}
60
61static cl::opt<std::string> LeftFilename(cl::Positional,
62                                         cl::desc("<first file>"),
63                                         cl::Required);
64static cl::opt<std::string> RightFilename(cl::Positional,
65                                          cl::desc("<second file>"),
66                                          cl::Required);
67static cl::list<std::string> GlobalsToCompare(cl::Positional,
68                                              cl::desc("<globals to compare>"));
69
70int main(int argc, char **argv) {
71  cl::ParseCommandLineOptions(argc, argv);
72
73  LLVMContext Context;
74
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;
79
80  DiffConsumer Consumer;
81  DifferenceEngine Engine(Consumer);
82
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]);
87
88  // Otherwise, diff everything in the module.
89  } else {
90    Engine.diff(LModule.get(), RModule.get());
91  }
92
93  return Consumer.hadDifferences();
94}
95