blob: 774169bcde170b2f511672f30eb3307cca5567be [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"
16
John McCall73b21b72010-07-29 18:08:23 +000017#include "llvm/LLVMContext.h"
18#include "llvm/Module.h"
19#include "llvm/Type.h"
John McCall73b21b72010-07-29 18:08:23 +000020#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/Support/CommandLine.h"
Dan Gohmanc32a2262010-09-13 18:02:47 +000024#include "llvm/Support/IRReader.h"
John McCall73b21b72010-07-29 18:08:23 +000025#include "llvm/Support/MemoryBuffer.h"
26#include "llvm/Support/raw_ostream.h"
27#include "llvm/Support/SourceMgr.h"
28
John McCall3dd706b2010-07-29 07:53:27 +000029#include <string>
30#include <utility>
31
John McCall3dd706b2010-07-29 07:53:27 +000032
33using namespace llvm;
34
Dan Gohmanc32a2262010-09-13 18:02:47 +000035/// Reads a module from a file. On error, messages are written to stderr
36/// and null is returned.
John McCall3dd706b2010-07-29 07:53:27 +000037static Module *ReadModule(LLVMContext &Context, StringRef Name) {
Dan Gohmanc32a2262010-09-13 18:02:47 +000038 SMDiagnostic Diag;
39 Module *M = ParseIRFile(Name, Diag, Context);
40 if (!M)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000041 Diag.print("llvm-diff", errs());
Dan Gohmanc32a2262010-09-13 18:02:47 +000042 return M;
John McCall3dd706b2010-07-29 07:53:27 +000043}
44
John McCalle5cbaf12010-07-29 17:55:00 +000045static 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);
John McCall3dd706b2010-07-29 07:53:27 +000049
John McCalle5cbaf12010-07-29 17:55:00 +000050 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
Chris Lattner53b1acd2010-09-05 21:25:43 +000062static 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>"));
John McCalle5cbaf12010-07-29 17:55:00 +000070
71int main(int argc, char **argv) {
72 cl::ParseCommandLineOptions(argc, argv);
John McCall3dd706b2010-07-29 07:53:27 +000073
74 LLVMContext Context;
75
76 // Load both modules. Die if that fails.
John McCalle5cbaf12010-07-29 17:55:00 +000077 Module *LModule = ReadModule(Context, LeftFilename);
78 Module *RModule = ReadModule(Context, RightFilename);
John McCall3dd706b2010-07-29 07:53:27 +000079 if (!LModule || !RModule) return 1;
80
81 DiffConsumer Consumer(LModule, RModule);
82 DifferenceEngine Engine(Context, Consumer);
83
John McCalle5cbaf12010-07-29 17:55:00 +000084 // 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]);
John McCall3dd706b2010-07-29 07:53:27 +000088
John McCalle5cbaf12010-07-29 17:55:00 +000089 // Otherwise, diff everything in the module.
John McCall3dd706b2010-07-29 07:53:27 +000090 } else {
John McCall3dd706b2010-07-29 07:53:27 +000091 Engine.diff(LModule, RModule);
92 }
93
94 delete LModule;
95 delete RModule;
96
97 return Consumer.hadDifferences();
98}