John McCall | 73b21b7 | 2010-07-29 18:08:23 +0000 | [diff] [blame] | 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 | |
Renato Golin | 7d4fc4f | 2011-03-14 22:22:46 +0000 | [diff] [blame] | 14 | #include "DiffLog.h" |
John McCall | 73b21b7 | 2010-07-29 18:08:23 +0000 | [diff] [blame] | 15 | #include "DifferenceEngine.h" |
John McCall | 73b21b7 | 2010-07-29 18:08:23 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseMap.h" |
| 17 | #include "llvm/ADT/SmallVector.h" |
| 18 | #include "llvm/ADT/StringRef.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame^] | 19 | #include "llvm/LLVMContext.h" |
| 20 | #include "llvm/Module.h" |
John McCall | 73b21b7 | 2010-07-29 18:08:23 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | c32a226 | 2010-09-13 18:02:47 +0000 | [diff] [blame] | 22 | #include "llvm/Support/IRReader.h" |
John McCall | 73b21b7 | 2010-07-29 18:08:23 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
John McCall | 73b21b7 | 2010-07-29 18:08:23 +0000 | [diff] [blame] | 24 | #include "llvm/Support/SourceMgr.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame^] | 25 | #include "llvm/Support/raw_ostream.h" |
| 26 | #include "llvm/Type.h" |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 27 | #include <string> |
| 28 | #include <utility> |
| 29 | |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace llvm; |
| 32 | |
Dan Gohman | c32a226 | 2010-09-13 18:02:47 +0000 | [diff] [blame] | 33 | /// Reads a module from a file. On error, messages are written to stderr |
| 34 | /// and null is returned. |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 35 | static Module *ReadModule(LLVMContext &Context, StringRef Name) { |
Dan Gohman | c32a226 | 2010-09-13 18:02:47 +0000 | [diff] [blame] | 36 | SMDiagnostic Diag; |
| 37 | Module *M = ParseIRFile(Name, Diag, Context); |
| 38 | if (!M) |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 39 | Diag.print("llvm-diff", errs()); |
Dan Gohman | c32a226 | 2010-09-13 18:02:47 +0000 | [diff] [blame] | 40 | return M; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 41 | } |
| 42 | |
John McCall | e5cbaf1 | 2010-07-29 17:55:00 +0000 | [diff] [blame] | 43 | static void diffGlobal(DifferenceEngine &Engine, Module *L, Module *R, |
| 44 | StringRef Name) { |
| 45 | // Drop leading sigils from the global name. |
| 46 | if (Name.startswith("@")) Name = Name.substr(1); |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 47 | |
John McCall | e5cbaf1 | 2010-07-29 17:55:00 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 53b1acd | 2010-09-05 21:25:43 +0000 | [diff] [blame] | 60 | static cl::opt<std::string> LeftFilename(cl::Positional, |
| 61 | cl::desc("<first file>"), |
| 62 | cl::Required); |
| 63 | static cl::opt<std::string> RightFilename(cl::Positional, |
| 64 | cl::desc("<second file>"), |
| 65 | cl::Required); |
| 66 | static cl::list<std::string> GlobalsToCompare(cl::Positional, |
| 67 | cl::desc("<globals to compare>")); |
John McCall | e5cbaf1 | 2010-07-29 17:55:00 +0000 | [diff] [blame] | 68 | |
| 69 | int main(int argc, char **argv) { |
| 70 | cl::ParseCommandLineOptions(argc, argv); |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 71 | |
| 72 | LLVMContext Context; |
| 73 | |
| 74 | // Load both modules. Die if that fails. |
John McCall | e5cbaf1 | 2010-07-29 17:55:00 +0000 | [diff] [blame] | 75 | Module *LModule = ReadModule(Context, LeftFilename); |
| 76 | Module *RModule = ReadModule(Context, RightFilename); |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 77 | if (!LModule || !RModule) return 1; |
| 78 | |
Benjamin Kramer | a7542d5 | 2012-06-06 18:25:08 +0000 | [diff] [blame] | 79 | DiffConsumer Consumer; |
| 80 | DifferenceEngine Engine(Consumer); |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 81 | |
John McCall | e5cbaf1 | 2010-07-29 17:55:00 +0000 | [diff] [blame] | 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]); |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 86 | |
John McCall | e5cbaf1 | 2010-07-29 17:55:00 +0000 | [diff] [blame] | 87 | // Otherwise, diff everything in the module. |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 88 | } else { |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 89 | Engine.diff(LModule, RModule); |
| 90 | } |
| 91 | |
| 92 | delete LModule; |
| 93 | delete RModule; |
| 94 | |
| 95 | return Consumer.hadDifferences(); |
| 96 | } |