blob: e449d6994784e7694bf07f1b8697fcfa2f932558 [file] [log] [blame]
John McCall73b21b72010-07-29 18:08:23 +00001//===-- 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 Golin7d4fc4f2011-03-14 22:22:46 +000014#include "DiffLog.h"
John McCall73b21b72010-07-29 18:08:23 +000015#include "DifferenceEngine.h"
John McCall73b21b72010-07-29 18:08:23 +000016#include "llvm/ADT/StringRef.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000017#include "llvm/IR/LLVMContext.h"
18#include "llvm/IR/Module.h"
19#include "llvm/IR/Type.h"
Chandler Carruth7fc162f2013-03-26 02:25:37 +000020#include "llvm/IRReader/IRReader.h"
John McCall73b21b72010-07-29 18:08:23 +000021#include "llvm/Support/CommandLine.h"
John McCall73b21b72010-07-29 18:08:23 +000022#include "llvm/Support/MemoryBuffer.h"
John McCall73b21b72010-07-29 18:08:23 +000023#include "llvm/Support/SourceMgr.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000024#include "llvm/Support/raw_ostream.h"
John McCall3dd706b2010-07-29 07:53:27 +000025#include <string>
26#include <utility>
27
John McCall3dd706b2010-07-29 07:53:27 +000028
29using namespace llvm;
30
Dan Gohmanc32a2262010-09-13 18:02:47 +000031/// Reads a module from a file. On error, messages are written to stderr
32/// and null is returned.
Rafael Espindola81e49922014-08-26 17:29:46 +000033static std::unique_ptr<Module> readModule(LLVMContext &Context,
34 StringRef Name) {
Dan Gohmanc32a2262010-09-13 18:02:47 +000035 SMDiagnostic Diag;
Rafael Espindola81e49922014-08-26 17:29:46 +000036 std::unique_ptr<Module> M = parseIRFile(Name, Diag, Context);
Dan Gohmanc32a2262010-09-13 18:02:47 +000037 if (!M)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000038 Diag.print("llvm-diff", errs());
Dan Gohmanc32a2262010-09-13 18:02:47 +000039 return M;
John McCall3dd706b2010-07-29 07:53:27 +000040}
41
Rafael Espindola81e49922014-08-26 17:29:46 +000042static void diffGlobal(DifferenceEngine &Engine, Module &L, Module &R,
John McCalle5cbaf12010-07-29 17:55:00 +000043 StringRef Name) {
44 // Drop leading sigils from the global name.
45 if (Name.startswith("@")) Name = Name.substr(1);
John McCall3dd706b2010-07-29 07:53:27 +000046
Rafael Espindola81e49922014-08-26 17:29:46 +000047 Function *LFn = L.getFunction(Name);
48 Function *RFn = R.getFunction(Name);
John McCalle5cbaf12010-07-29 17:55:00 +000049 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 Lattner53b1acd2010-09-05 21:25:43 +000059static cl::opt<std::string> LeftFilename(cl::Positional,
60 cl::desc("<first file>"),
61 cl::Required);
62static cl::opt<std::string> RightFilename(cl::Positional,
63 cl::desc("<second file>"),
64 cl::Required);
65static cl::list<std::string> GlobalsToCompare(cl::Positional,
66 cl::desc("<globals to compare>"));
John McCalle5cbaf12010-07-29 17:55:00 +000067
68int main(int argc, char **argv) {
69 cl::ParseCommandLineOptions(argc, argv);
John McCall3dd706b2010-07-29 07:53:27 +000070
71 LLVMContext Context;
Andrew Trick2cccc622013-09-18 23:31:10 +000072
John McCall3dd706b2010-07-29 07:53:27 +000073 // Load both modules. Die if that fails.
Rafael Espindola81e49922014-08-26 17:29:46 +000074 std::unique_ptr<Module> LModule = readModule(Context, LeftFilename);
75 std::unique_ptr<Module> RModule = readModule(Context, RightFilename);
John McCall3dd706b2010-07-29 07:53:27 +000076 if (!LModule || !RModule) return 1;
77
Benjamin Kramera7542d52012-06-06 18:25:08 +000078 DiffConsumer Consumer;
79 DifferenceEngine Engine(Consumer);
John McCall3dd706b2010-07-29 07:53:27 +000080
John McCalle5cbaf12010-07-29 17:55:00 +000081 // 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 Espindola81e49922014-08-26 17:29:46 +000084 diffGlobal(Engine, *LModule, *RModule, GlobalsToCompare[I]);
John McCall3dd706b2010-07-29 07:53:27 +000085
John McCalle5cbaf12010-07-29 17:55:00 +000086 // Otherwise, diff everything in the module.
John McCall3dd706b2010-07-29 07:53:27 +000087 } else {
Rafael Espindola81e49922014-08-26 17:29:46 +000088 Engine.diff(LModule.get(), RModule.get());
John McCall3dd706b2010-07-29 07:53:27 +000089 }
90
John McCall3dd706b2010-07-29 07:53:27 +000091 return Consumer.hadDifferences();
92}