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/StringRef.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/LLVMContext.h" |
| 18 | #include "llvm/IR/Module.h" |
| 19 | #include "llvm/IR/Type.h" |
Chandler Carruth | 7fc162f | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 20 | #include "llvm/IRReader/IRReader.h" |
John McCall | 73b21b7 | 2010-07-29 18:08:23 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CommandLine.h" |
John McCall | 73b21b7 | 2010-07-29 18:08:23 +0000 | [diff] [blame] | 22 | #include "llvm/Support/MemoryBuffer.h" |
John McCall | 73b21b7 | 2010-07-29 18:08:23 +0000 | [diff] [blame] | 23 | #include "llvm/Support/SourceMgr.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 25 | #include <string> |
| 26 | #include <utility> |
| 27 | |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace llvm; |
| 30 | |
Dan Gohman | c32a226 | 2010-09-13 18:02:47 +0000 | [diff] [blame] | 31 | /// Reads a module from a file. On error, messages are written to stderr |
| 32 | /// and null is returned. |
Rafael Espindola | 81e4992 | 2014-08-26 17:29:46 +0000 | [diff] [blame] | 33 | static std::unique_ptr<Module> readModule(LLVMContext &Context, |
| 34 | StringRef Name) { |
Dan Gohman | c32a226 | 2010-09-13 18:02:47 +0000 | [diff] [blame] | 35 | SMDiagnostic Diag; |
Rafael Espindola | 81e4992 | 2014-08-26 17:29:46 +0000 | [diff] [blame] | 36 | std::unique_ptr<Module> M = parseIRFile(Name, Diag, Context); |
Dan Gohman | c32a226 | 2010-09-13 18:02:47 +0000 | [diff] [blame] | 37 | if (!M) |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 38 | Diag.print("llvm-diff", errs()); |
Dan Gohman | c32a226 | 2010-09-13 18:02:47 +0000 | [diff] [blame] | 39 | return M; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Rafael Espindola | 81e4992 | 2014-08-26 17:29:46 +0000 | [diff] [blame] | 42 | static void diffGlobal(DifferenceEngine &Engine, Module &L, Module &R, |
John McCall | e5cbaf1 | 2010-07-29 17:55:00 +0000 | [diff] [blame] | 43 | StringRef Name) { |
| 44 | // Drop leading sigils from the global name. |
| 45 | if (Name.startswith("@")) Name = Name.substr(1); |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 46 | |
Rafael Espindola | 81e4992 | 2014-08-26 17:29:46 +0000 | [diff] [blame] | 47 | Function *LFn = L.getFunction(Name); |
| 48 | Function *RFn = R.getFunction(Name); |
John McCall | e5cbaf1 | 2010-07-29 17:55:00 +0000 | [diff] [blame] | 49 | if (LFn && RFn) |
| 50 | Engine.diff(LFn, RFn); |
| 51 | else if (!LFn && !RFn) |
| 52 | errs() << "No function named @" << Name << " in either module\n"; |
| 53 | else if (!LFn) |
| 54 | errs() << "No function named @" << Name << " in left module\n"; |
| 55 | else |
| 56 | errs() << "No function named @" << Name << " in right module\n"; |
| 57 | } |
| 58 | |
Chris Lattner | 53b1acd | 2010-09-05 21:25:43 +0000 | [diff] [blame] | 59 | static cl::opt<std::string> LeftFilename(cl::Positional, |
| 60 | cl::desc("<first file>"), |
| 61 | cl::Required); |
| 62 | static cl::opt<std::string> RightFilename(cl::Positional, |
| 63 | cl::desc("<second file>"), |
| 64 | cl::Required); |
| 65 | static cl::list<std::string> GlobalsToCompare(cl::Positional, |
| 66 | cl::desc("<globals to compare>")); |
John McCall | e5cbaf1 | 2010-07-29 17:55:00 +0000 | [diff] [blame] | 67 | |
| 68 | int main(int argc, char **argv) { |
| 69 | cl::ParseCommandLineOptions(argc, argv); |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 70 | |
| 71 | LLVMContext Context; |
Andrew Trick | 2cccc62 | 2013-09-18 23:31:10 +0000 | [diff] [blame] | 72 | |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 73 | // Load both modules. Die if that fails. |
Rafael Espindola | 81e4992 | 2014-08-26 17:29:46 +0000 | [diff] [blame] | 74 | std::unique_ptr<Module> LModule = readModule(Context, LeftFilename); |
| 75 | std::unique_ptr<Module> RModule = readModule(Context, RightFilename); |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 76 | if (!LModule || !RModule) return 1; |
| 77 | |
Benjamin Kramer | a7542d5 | 2012-06-06 18:25:08 +0000 | [diff] [blame] | 78 | DiffConsumer Consumer; |
| 79 | DifferenceEngine Engine(Consumer); |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 80 | |
John McCall | e5cbaf1 | 2010-07-29 17:55:00 +0000 | [diff] [blame] | 81 | // If any global names were given, just diff those. |
| 82 | if (!GlobalsToCompare.empty()) { |
| 83 | for (unsigned I = 0, E = GlobalsToCompare.size(); I != E; ++I) |
Rafael Espindola | 81e4992 | 2014-08-26 17:29:46 +0000 | [diff] [blame] | 84 | diffGlobal(Engine, *LModule, *RModule, GlobalsToCompare[I]); |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 85 | |
John McCall | e5cbaf1 | 2010-07-29 17:55:00 +0000 | [diff] [blame] | 86 | // Otherwise, diff everything in the module. |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 87 | } else { |
Rafael Espindola | 81e4992 | 2014-08-26 17:29:46 +0000 | [diff] [blame] | 88 | Engine.diff(LModule.get(), RModule.get()); |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 89 | } |
| 90 | |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 91 | return Consumer.hadDifferences(); |
| 92 | } |