1//===-- llvm-diff.cpp - Module comparator command-line driver ---*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the command-line driver for the difference engine.
10//
11//===----------------------------------------------------------------------===//
12
13#include "lib/DifferenceEngine.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/IR/LLVMContext.h"
16#include "llvm/IR/Module.h"
17#include "llvm/IR/Type.h"
18#include "llvm/IRReader/IRReader.h"
19#include "llvm/Support/CommandLine.h"
20#include "llvm/Support/SourceMgr.h"
21#include "llvm/Support/WithColor.h"
22#include "llvm/Support/raw_ostream.h"
23#include <string>
24
25using namespace llvm;
26
27/// Reads a module from a file. On error, messages are written to stderr
28/// and null is returned.
29static std::unique_ptr<Module> readModule(LLVMContext &Context,
30 StringRef Name) {
31 SMDiagnostic Diag;
32 std::unique_ptr<Module> M = parseIRFile(Filename: Name, Err&: Diag, Context);
33 if (!M)
34 Diag.print(ProgName: "llvm-diff", S&: errs());
35 return M;
36}
37
38static void diffGlobal(DifferenceEngine &Engine, Module &L, Module &R,
39 StringRef Name) {
40 // Drop leading sigils from the global name.
41 Name.consume_front(Prefix: "@");
42
43 Function *LFn = L.getFunction(Name);
44 Function *RFn = R.getFunction(Name);
45 if (LFn && RFn)
46 Engine.diff(L: LFn, R: RFn);
47 else if (!LFn && !RFn)
48 errs() << "No function named @" << Name << " in either module\n";
49 else if (!LFn)
50 errs() << "No function named @" << Name << " in left module\n";
51 else
52 errs() << "No function named @" << Name << " in right module\n";
53}
54
55static cl::OptionCategory DiffCategory("Diff Options");
56
57static cl::opt<std::string> LeftFilename(cl::Positional,
58 cl::desc("<first file>"), cl::Required,
59 cl::cat(DiffCategory));
60static cl::opt<std::string> RightFilename(cl::Positional,
61 cl::desc("<second file>"),
62 cl::Required, cl::cat(DiffCategory));
63static cl::list<std::string> GlobalsToCompare(cl::Positional,
64 cl::desc("<globals to compare>"),
65 cl::cat(DiffCategory));
66
67int main(int argc, char **argv) {
68 cl::HideUnrelatedOptions(Categories: {&DiffCategory, &getColorCategory()});
69 cl::ParseCommandLineOptions(argc, argv);
70
71 LLVMContext Context;
72
73 // Load both modules. Die if that fails.
74 std::unique_ptr<Module> LModule = readModule(Context, Name: LeftFilename);
75 std::unique_ptr<Module> RModule = readModule(Context, Name: RightFilename);
76 if (!LModule || !RModule) return 1;
77
78 DiffConsumer Consumer;
79 DifferenceEngine Engine(Consumer);
80
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, L&: *LModule, R&: *RModule, Name: GlobalsToCompare[I]);
85
86 // Otherwise, diff everything in the module.
87 } else {
88 Engine.diff(L: LModule.get(), R: RModule.get());
89 }
90
91 return Consumer.hadDifferences();
92}
93