Path: blob/main/contrib/llvm-project/llvm/tools/llvm-diff/llvm-diff.cpp
35260 views
//===-- llvm-diff.cpp - Module comparator command-line driver ---*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file defines the command-line driver for the difference engine.9//10//===----------------------------------------------------------------------===//1112#include "lib/DiffLog.h"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/MemoryBuffer.h"21#include "llvm/Support/SourceMgr.h"22#include "llvm/Support/raw_ostream.h"23#include "llvm/Support/WithColor.h"24#include <string>25#include <utility>262728using namespace llvm;2930/// Reads a module from a file. On error, messages are written to stderr31/// and null is returned.32static std::unique_ptr<Module> readModule(LLVMContext &Context,33StringRef Name) {34SMDiagnostic Diag;35std::unique_ptr<Module> M = parseIRFile(Name, Diag, Context);36if (!M)37Diag.print("llvm-diff", errs());38return M;39}4041static void diffGlobal(DifferenceEngine &Engine, Module &L, Module &R,42StringRef Name) {43// Drop leading sigils from the global name.44Name.consume_front("@");4546Function *LFn = L.getFunction(Name);47Function *RFn = R.getFunction(Name);48if (LFn && RFn)49Engine.diff(LFn, RFn);50else if (!LFn && !RFn)51errs() << "No function named @" << Name << " in either module\n";52else if (!LFn)53errs() << "No function named @" << Name << " in left module\n";54else55errs() << "No function named @" << Name << " in right module\n";56}5758cl::OptionCategory DiffCategory("Diff Options");5960static cl::opt<std::string> LeftFilename(cl::Positional,61cl::desc("<first file>"), cl::Required,62cl::cat(DiffCategory));63static cl::opt<std::string> RightFilename(cl::Positional,64cl::desc("<second file>"),65cl::Required, cl::cat(DiffCategory));66static cl::list<std::string> GlobalsToCompare(cl::Positional,67cl::desc("<globals to compare>"),68cl::cat(DiffCategory));6970int main(int argc, char **argv) {71cl::HideUnrelatedOptions({&DiffCategory, &getColorCategory()});72cl::ParseCommandLineOptions(argc, argv);7374LLVMContext Context;7576// Load both modules. Die if that fails.77std::unique_ptr<Module> LModule = readModule(Context, LeftFilename);78std::unique_ptr<Module> RModule = readModule(Context, RightFilename);79if (!LModule || !RModule) return 1;8081DiffConsumer Consumer;82DifferenceEngine Engine(Consumer);8384// If any global names were given, just diff those.85if (!GlobalsToCompare.empty()) {86for (unsigned I = 0, E = GlobalsToCompare.size(); I != E; ++I)87diffGlobal(Engine, *LModule, *RModule, GlobalsToCompare[I]);8889// Otherwise, diff everything in the module.90} else {91Engine.diff(LModule.get(), RModule.get());92}9394return Consumer.hadDifferences();95}969798