1 //===-- llvm-diff.cpp - Module comparator command-line driver ---*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the command-line driver for the difference engine. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "lib/DiffLog.h" 14 #include "lib/DifferenceEngine.h" 15 #include "llvm/ADT/StringRef.h" 16 #include "llvm/IR/LLVMContext.h" 17 #include "llvm/IR/Module.h" 18 #include "llvm/IR/Type.h" 19 #include "llvm/IRReader/IRReader.h" 20 #include "llvm/Support/CommandLine.h" 21 #include "llvm/Support/MemoryBuffer.h" 22 #include "llvm/Support/SourceMgr.h" 23 #include "llvm/Support/raw_ostream.h" 24 #include "llvm/Support/WithColor.h" 25 #include <string> 26 #include <utility> 27 28 29 using namespace llvm; 30 31 /// Reads a module from a file. On error, messages are written to stderr 32 /// and null is returned. 33 static std::unique_ptr<Module> readModule(LLVMContext &Context, 34 StringRef Name) { 35 SMDiagnostic Diag; 36 std::unique_ptr<Module> M = parseIRFile(Name, Diag, Context); 37 if (!M) 38 Diag.print("llvm-diff", errs()); 39 return M; 40 } 41 42 static void diffGlobal(DifferenceEngine &Engine, Module &L, Module &R, 43 StringRef Name) { 44 // Drop leading sigils from the global name. 45 if (Name.starts_with("@")) 46 Name = Name.substr(1); 47 48 Function *LFn = L.getFunction(Name); 49 Function *RFn = R.getFunction(Name); 50 if (LFn && RFn) 51 Engine.diff(LFn, RFn); 52 else if (!LFn && !RFn) 53 errs() << "No function named @" << Name << " in either module\n"; 54 else if (!LFn) 55 errs() << "No function named @" << Name << " in left module\n"; 56 else 57 errs() << "No function named @" << Name << " in right module\n"; 58 } 59 60 cl::OptionCategory DiffCategory("Diff Options"); 61 62 static cl::opt<std::string> LeftFilename(cl::Positional, 63 cl::desc("<first file>"), cl::Required, 64 cl::cat(DiffCategory)); 65 static cl::opt<std::string> RightFilename(cl::Positional, 66 cl::desc("<second file>"), 67 cl::Required, cl::cat(DiffCategory)); 68 static cl::list<std::string> GlobalsToCompare(cl::Positional, 69 cl::desc("<globals to compare>"), 70 cl::cat(DiffCategory)); 71 72 int main(int argc, char **argv) { 73 cl::HideUnrelatedOptions({&DiffCategory, &getColorCategory()}); 74 cl::ParseCommandLineOptions(argc, argv); 75 76 LLVMContext Context; 77 78 // Load both modules. Die if that fails. 79 std::unique_ptr<Module> LModule = readModule(Context, LeftFilename); 80 std::unique_ptr<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.get(), RModule.get()); 94 } 95 96 return Consumer.hadDifferences(); 97 } 98