xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-diff/llvm-diff.cpp (revision 6226477a462f5ffbeacafdc9461524c95a7eb154)
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 "DiffLog.h"
14  #include "DifferenceEngine.h"
15  #include "llvm/ADT/StringRef.h"
16  #include "llvm/IR/LLVMContext.h"
17  #include "llvm/IR/Module.h"
18  #include "llvm/IR/Type.h"
19  #include "llvm/IRReader/IRReader.h"
20  #include "llvm/Support/CommandLine.h"
21  #include "llvm/Support/MemoryBuffer.h"
22  #include "llvm/Support/SourceMgr.h"
23  #include "llvm/Support/raw_ostream.h"
24  #include <string>
25  #include <utility>
26  
27  
28  using namespace llvm;
29  
30  /// Reads a module from a file.  On error, messages are written to stderr
31  /// and null is returned.
32  static std::unique_ptr<Module> readModule(LLVMContext &Context,
33                                            StringRef Name) {
34    SMDiagnostic Diag;
35    std::unique_ptr<Module> M = parseIRFile(Name, Diag, Context);
36    if (!M)
37      Diag.print("llvm-diff", errs());
38    return M;
39  }
40  
41  static void diffGlobal(DifferenceEngine &Engine, Module &L, Module &R,
42                         StringRef Name) {
43    // Drop leading sigils from the global name.
44    if (Name.startswith("@")) Name = Name.substr(1);
45  
46    Function *LFn = L.getFunction(Name);
47    Function *RFn = R.getFunction(Name);
48    if (LFn && RFn)
49      Engine.diff(LFn, RFn);
50    else if (!LFn && !RFn)
51      errs() << "No function named @" << Name << " in either module\n";
52    else if (!LFn)
53      errs() << "No function named @" << Name << " in left module\n";
54    else
55      errs() << "No function named @" << Name << " in right module\n";
56  }
57  
58  cl::OptionCategory DiffCategory("Diff Options");
59  
60  static cl::opt<std::string> LeftFilename(cl::Positional,
61                                           cl::desc("<first file>"), cl::Required,
62                                           cl::cat(DiffCategory));
63  static cl::opt<std::string> RightFilename(cl::Positional,
64                                            cl::desc("<second file>"),
65                                            cl::Required, cl::cat(DiffCategory));
66  static cl::list<std::string> GlobalsToCompare(cl::Positional,
67                                                cl::desc("<globals to compare>"),
68                                                cl::cat(DiffCategory));
69  
70  int main(int argc, char **argv) {
71    cl::HideUnrelatedOptions({&DiffCategory, &getColorCategory()});
72    cl::ParseCommandLineOptions(argc, argv);
73  
74    LLVMContext Context;
75  
76    // Load both modules.  Die if that fails.
77    std::unique_ptr<Module> LModule = readModule(Context, LeftFilename);
78    std::unique_ptr<Module> RModule = readModule(Context, RightFilename);
79    if (!LModule || !RModule) return 1;
80  
81    DiffConsumer Consumer;
82    DifferenceEngine Engine(Consumer);
83  
84    // 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]);
88  
89    // Otherwise, diff everything in the module.
90    } else {
91      Engine.diff(LModule.get(), RModule.get());
92    }
93  
94    return Consumer.hadDifferences();
95  }
96