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 Module *ReadModule(LLVMContext &Context, StringRef Name) {
37 Module *M = ParseIRFile(Name, Diag, Context);
39 Diag.print("llvm-diff", errs());
43 static void diffGlobal(DifferenceEngine &Engine, Module *L, Module *R,
45 // Drop leading sigils from the global name.
46 if (Name.startswith("@")) Name = Name.substr(1);
48 Function *LFn = L->getFunction(Name);
49 Function *RFn = R->getFunction(Name);
51 Engine.diff(LFn, RFn);
52 else if (!LFn && !RFn)
53 errs() << "No function named @" << Name << " in either module\n";
55 errs() << "No function named @" << Name << " in left module\n";
57 errs() << "No function named @" << Name << " in right module\n";
60 static cl::opt<std::string> LeftFilename(cl::Positional,
61 cl::desc("<first file>"),
63 static cl::opt<std::string> RightFilename(cl::Positional,
64 cl::desc("<second file>"),
66 static cl::list<std::string> GlobalsToCompare(cl::Positional,
67 cl::desc("<globals to compare>"));
69 int main(int argc, char **argv) {
70 cl::ParseCommandLineOptions(argc, argv);
74 // Load both modules. Die if that fails.
75 Module *LModule = ReadModule(Context, LeftFilename);
76 Module *RModule = ReadModule(Context, RightFilename);
77 if (!LModule || !RModule) return 1;
79 DiffConsumer Consumer;
80 DifferenceEngine Engine(Consumer);
82 // If any global names were given, just diff those.
83 if (!GlobalsToCompare.empty()) {
84 for (unsigned I = 0, E = GlobalsToCompare.size(); I != E; ++I)
85 diffGlobal(Engine, LModule, RModule, GlobalsToCompare[I]);
87 // Otherwise, diff everything in the module.
89 Engine.diff(LModule, RModule);
95 return Consumer.hadDifferences();