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/PassManager.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/SourceMgr.h"
27 #include "llvm/Support/raw_ostream.h"
34 /// Reads a module from a file. On error, messages are written to stderr
35 /// and null is returned.
36 static Module *ReadModule(LLVMContext &Context, StringRef Name) {
38 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 // Initialize PassManager for -time-passes support.
72 initializePassManager();
74 cl::ParseCommandLineOptions(argc, argv);
78 // Load both modules. Die if that fails.
79 Module *LModule = ReadModule(Context, LeftFilename);
80 Module *RModule = ReadModule(Context, RightFilename);
81 if (!LModule || !RModule) return 1;
83 DiffConsumer Consumer;
84 DifferenceEngine Engine(Consumer);
86 // If any global names were given, just diff those.
87 if (!GlobalsToCompare.empty()) {
88 for (unsigned I = 0, E = GlobalsToCompare.size(); I != E; ++I)
89 diffGlobal(Engine, LModule, RModule, GlobalsToCompare[I]);
91 // Otherwise, diff everything in the module.
93 Engine.diff(LModule, RModule);
99 return Consumer.hadDifferences();