blob: f381dae6308411d9995f13dc6dda5ae39954b048 [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/DenseMap.h"
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/StringRef.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000019#include "llvm/LLVMContext.h"
20#include "llvm/Module.h"
John McCall73b21b72010-07-29 18:08:23 +000021#include "llvm/Support/CommandLine.h"
Dan Gohmanc32a2262010-09-13 18:02:47 +000022#include "llvm/Support/IRReader.h"
John McCall73b21b72010-07-29 18:08:23 +000023#include "llvm/Support/MemoryBuffer.h"
John McCall73b21b72010-07-29 18:08:23 +000024#include "llvm/Support/SourceMgr.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000025#include "llvm/Support/raw_ostream.h"
26#include "llvm/Type.h"
John McCall3dd706b2010-07-29 07:53:27 +000027#include <string>
28#include <utility>
29
John McCall3dd706b2010-07-29 07:53:27 +000030
31using namespace llvm;
32
Dan Gohmanc32a2262010-09-13 18:02:47 +000033/// Reads a module from a file. On error, messages are written to stderr
34/// and null is returned.
John McCall3dd706b2010-07-29 07:53:27 +000035static Module *ReadModule(LLVMContext &Context, StringRef Name) {
Dan Gohmanc32a2262010-09-13 18:02:47 +000036 SMDiagnostic Diag;
37 Module *M = ParseIRFile(Name, Diag, Context);
38 if (!M)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000039 Diag.print("llvm-diff", errs());
Dan Gohmanc32a2262010-09-13 18:02:47 +000040 return M;
John McCall3dd706b2010-07-29 07:53:27 +000041}
42
John McCalle5cbaf12010-07-29 17:55:00 +000043static 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 McCall3dd706b2010-07-29 07:53:27 +000047
John McCalle5cbaf12010-07-29 17:55:00 +000048 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 Lattner53b1acd2010-09-05 21:25:43 +000060static cl::opt<std::string> LeftFilename(cl::Positional,
61 cl::desc("<first file>"),
62 cl::Required);
63static cl::opt<std::string> RightFilename(cl::Positional,
64 cl::desc("<second file>"),
65 cl::Required);
66static cl::list<std::string> GlobalsToCompare(cl::Positional,
67 cl::desc("<globals to compare>"));
John McCalle5cbaf12010-07-29 17:55:00 +000068
69int main(int argc, char **argv) {
70 cl::ParseCommandLineOptions(argc, argv);
John McCall3dd706b2010-07-29 07:53:27 +000071
72 LLVMContext Context;
73
74 // Load both modules. Die if that fails.
John McCalle5cbaf12010-07-29 17:55:00 +000075 Module *LModule = ReadModule(Context, LeftFilename);
76 Module *RModule = ReadModule(Context, RightFilename);
John McCall3dd706b2010-07-29 07:53:27 +000077 if (!LModule || !RModule) return 1;
78
Benjamin Kramera7542d52012-06-06 18:25:08 +000079 DiffConsumer Consumer;
80 DifferenceEngine Engine(Consumer);
John McCall3dd706b2010-07-29 07:53:27 +000081
John McCalle5cbaf12010-07-29 17:55:00 +000082 // 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 McCall3dd706b2010-07-29 07:53:27 +000086
John McCalle5cbaf12010-07-29 17:55:00 +000087 // Otherwise, diff everything in the module.
John McCall3dd706b2010-07-29 07:53:27 +000088 } else {
John McCall3dd706b2010-07-29 07:53:27 +000089 Engine.diff(LModule, RModule);
90 }
91
92 delete LModule;
93 delete RModule;
94
95 return Consumer.hadDifferences();
96}