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/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" 28 #include <string> 29 #include <utility> 30 31 32 using namespace llvm; 33 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) { 37 SMDiagnostic Diag; 38 Module *M = ParseIRFile(Name, Diag, Context); 39 if (!M) 40 Diag.print("llvm-diff", errs()); 41 return M; 42 } 43 44 static 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 61 static cl::opt<std::string> LeftFilename(cl::Positional, 62 cl::desc("<first file>"), 63 cl::Required); 64 static cl::opt<std::string> RightFilename(cl::Positional, 65 cl::desc("<second file>"), 66 cl::Required); 67 static cl::list<std::string> GlobalsToCompare(cl::Positional, 68 cl::desc("<globals to compare>")); 69 70 int main(int argc, char **argv) { 71 // Initialize PassManager for -time-passes support. 72 initializePassManager(); 73 74 cl::ParseCommandLineOptions(argc, argv); 75 76 LLVMContext Context; 77 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; 82 83 DiffConsumer Consumer; 84 DifferenceEngine Engine(Consumer); 85 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]); 90 91 // Otherwise, diff everything in the module. 92 } else { 93 Engine.diff(LModule, RModule); 94 } 95 96 delete LModule; 97 delete RModule; 98 99 return Consumer.hadDifferences(); 100 } 101