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 17#include "llvm/LLVMContext.h" 18#include "llvm/Module.h" 19#include "llvm/Type.h" 20#include "llvm/ADT/DenseMap.h" 21#include "llvm/ADT/SmallVector.h" 22#include "llvm/ADT/StringRef.h" 23#include "llvm/Support/CommandLine.h" 24#include "llvm/Support/IRReader.h" 25#include "llvm/Support/MemoryBuffer.h" 26#include "llvm/Support/raw_ostream.h" 27#include "llvm/Support/SourceMgr.h" 28 29#include <string> 30#include <utility> 31 32 33using namespace llvm; 34 35/// Reads a module from a file. On error, messages are written to stderr 36/// and null is returned. 37static Module *ReadModule(LLVMContext &Context, StringRef Name) { 38 SMDiagnostic Diag; 39 Module *M = ParseIRFile(Name, Diag, Context); 40 if (!M) 41 Diag.print("llvm-diff", errs()); 42 return M; 43} 44 45static void diffGlobal(DifferenceEngine &Engine, Module *L, Module *R, 46 StringRef Name) { 47 // Drop leading sigils from the global name. 48 if (Name.startswith("@")) Name = Name.substr(1); 49 50 Function *LFn = L->getFunction(Name); 51 Function *RFn = R->getFunction(Name); 52 if (LFn && RFn) 53 Engine.diff(LFn, RFn); 54 else if (!LFn && !RFn) 55 errs() << "No function named @" << Name << " in either module\n"; 56 else if (!LFn) 57 errs() << "No function named @" << Name << " in left module\n"; 58 else 59 errs() << "No function named @" << Name << " in right module\n"; 60} 61 62static cl::opt<std::string> LeftFilename(cl::Positional, 63 cl::desc("<first file>"), 64 cl::Required); 65static cl::opt<std::string> RightFilename(cl::Positional, 66 cl::desc("<second file>"), 67 cl::Required); 68static cl::list<std::string> GlobalsToCompare(cl::Positional, 69 cl::desc("<globals to compare>")); 70 71int main(int argc, char **argv) { 72 cl::ParseCommandLineOptions(argc, argv); 73 74 LLVMContext Context; 75 76 // Load both modules. Die if that fails. 77 Module *LModule = ReadModule(Context, LeftFilename); 78 Module *RModule = ReadModule(Context, RightFilename); 79 if (!LModule || !RModule) return 1; 80 81 DiffConsumer Consumer; 82 DifferenceEngine Engine(Consumer); 83 84 // If any global names were given, just diff those. 85 if (!GlobalsToCompare.empty()) { 86 for (unsigned I = 0, E = GlobalsToCompare.size(); I != E; ++I) 87 diffGlobal(Engine, LModule, RModule, GlobalsToCompare[I]); 88 89 // Otherwise, diff everything in the module. 90 } else { 91 Engine.diff(LModule, RModule); 92 } 93 94 delete LModule; 95 delete RModule; 96 97 return Consumer.hadDifferences(); 98} 99