brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.3 KiB · 45b8ed9 Raw
95 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// This file defines the command-line driver for the difference engine.10//11//===----------------------------------------------------------------------===//12 13#include "lib/DiffLog.h"14#include "lib/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/WithColor.h"24#include "llvm/Support/raw_ostream.h"25#include <string>26 27using namespace llvm;28 29/// Reads a module from a file.  On error, messages are written to stderr30/// and null is returned.31static std::unique_ptr<Module> readModule(LLVMContext &Context,32                                          StringRef Name) {33  SMDiagnostic Diag;34  std::unique_ptr<Module> M = parseIRFile(Name, Diag, Context);35  if (!M)36    Diag.print("llvm-diff", errs());37  return M;38}39 40static void diffGlobal(DifferenceEngine &Engine, Module &L, Module &R,41                       StringRef Name) {42  // Drop leading sigils from the global name.43  Name.consume_front("@");44 45  Function *LFn = L.getFunction(Name);46  Function *RFn = R.getFunction(Name);47  if (LFn && RFn)48    Engine.diff(LFn, RFn);49  else if (!LFn && !RFn)50    errs() << "No function named @" << Name << " in either module\n";51  else if (!LFn)52    errs() << "No function named @" << Name << " in left module\n";53  else54    errs() << "No function named @" << Name << " in right module\n";55}56 57static cl::OptionCategory DiffCategory("Diff Options");58 59static cl::opt<std::string> LeftFilename(cl::Positional,60                                         cl::desc("<first file>"), cl::Required,61                                         cl::cat(DiffCategory));62static cl::opt<std::string> RightFilename(cl::Positional,63                                          cl::desc("<second file>"),64                                          cl::Required, cl::cat(DiffCategory));65static cl::list<std::string> GlobalsToCompare(cl::Positional,66                                              cl::desc("<globals to compare>"),67                                              cl::cat(DiffCategory));68 69int main(int argc, char **argv) {70  cl::HideUnrelatedOptions({&DiffCategory, &getColorCategory()});71  cl::ParseCommandLineOptions(argc, argv);72 73  LLVMContext Context;74 75  // Load both modules.  Die if that fails.76  std::unique_ptr<Module> LModule = readModule(Context, LeftFilename);77  std::unique_ptr<Module> RModule = readModule(Context, RightFilename);78  if (!LModule || !RModule) return 1;79 80  DiffConsumer Consumer;81  DifferenceEngine Engine(Consumer);82 83  // If any global names were given, just diff those.84  if (!GlobalsToCompare.empty()) {85    for (unsigned I = 0, E = GlobalsToCompare.size(); I != E; ++I)86      diffGlobal(Engine, *LModule, *RModule, GlobalsToCompare[I]);87 88  // Otherwise, diff everything in the module.89  } else {90    Engine.diff(LModule.get(), RModule.get());91  }92 93  return Consumer.hadDifferences();94}95