1 //===-- llvm-diff.cpp - Module comparator command-line driver ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the command-line driver for the difference engine.
12 //===----------------------------------------------------------------------===//
15 #include "DifferenceEngine.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/IR/Type.h"
20 #include "llvm/IRReader/IRReader.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include "llvm/Support/SourceMgr.h"
24 #include "llvm/Support/raw_ostream.h"
31 /// Reads a module from a file. On error, messages are written to stderr
32 /// and null is returned.
33 static std::unique_ptr
<Module
> readModule(LLVMContext
&Context
,
36 std::unique_ptr
<Module
> M
= parseIRFile(Name
, Diag
, Context
);
38 Diag
.print("llvm-diff", errs());
42 static void diffGlobal(DifferenceEngine
&Engine
, Module
&L
, Module
&R
,
44 // Drop leading sigils from the global name.
45 if (Name
.startswith("@")) Name
= Name
.substr(1);
47 Function
*LFn
= L
.getFunction(Name
);
48 Function
*RFn
= R
.getFunction(Name
);
50 Engine
.diff(LFn
, RFn
);
51 else if (!LFn
&& !RFn
)
52 errs() << "No function named @" << Name
<< " in either module\n";
54 errs() << "No function named @" << Name
<< " in left module\n";
56 errs() << "No function named @" << Name
<< " in right module\n";
59 static cl::opt
<std::string
> LeftFilename(cl::Positional
,
60 cl::desc("<first file>"),
62 static cl::opt
<std::string
> RightFilename(cl::Positional
,
63 cl::desc("<second file>"),
65 static cl::list
<std::string
> GlobalsToCompare(cl::Positional
,
66 cl::desc("<globals to compare>"));
68 int main(int argc
, char **argv
) {
69 cl::ParseCommandLineOptions(argc
, argv
);
73 // Load both modules. Die if that fails.
74 std::unique_ptr
<Module
> LModule
= readModule(Context
, LeftFilename
);
75 std::unique_ptr
<Module
> RModule
= readModule(Context
, RightFilename
);
76 if (!LModule
|| !RModule
) return 1;
78 DiffConsumer Consumer
;
79 DifferenceEngine
Engine(Consumer
);
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
)
84 diffGlobal(Engine
, *LModule
, *RModule
, GlobalsToCompare
[I
]);
86 // Otherwise, diff everything in the module.
88 Engine
.diff(LModule
.get(), RModule
.get());
91 return Consumer
.hadDifferences();