llvm-diff.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #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>
  26. using namespace llvm;
  27. /// Reads a module from a file. On error, messages are written to stderr
  28. /// and null is returned.
  29. static std::unique_ptr<Module> readModule(LLVMContext &Context,
  30. StringRef Name) {
  31. SMDiagnostic Diag;
  32. std::unique_ptr<Module> M = parseIRFile(Name, Diag, Context);
  33. if (!M)
  34. Diag.print("llvm-diff", errs());
  35. return M;
  36. }
  37. static void diffGlobal(DifferenceEngine &Engine, Module &L, Module &R,
  38. StringRef Name) {
  39. // Drop leading sigils from the global name.
  40. if (Name.startswith("@")) Name = Name.substr(1);
  41. Function *LFn = L.getFunction(Name);
  42. Function *RFn = R.getFunction(Name);
  43. if (LFn && RFn)
  44. Engine.diff(LFn, RFn);
  45. else if (!LFn && !RFn)
  46. errs() << "No function named @" << Name << " in either module\n";
  47. else if (!LFn)
  48. errs() << "No function named @" << Name << " in left module\n";
  49. else
  50. errs() << "No function named @" << Name << " in right module\n";
  51. }
  52. cl::OptionCategory DiffCategory("Diff Options");
  53. static cl::opt<std::string> LeftFilename(cl::Positional,
  54. cl::desc("<first file>"), cl::Required,
  55. cl::cat(DiffCategory));
  56. static cl::opt<std::string> RightFilename(cl::Positional,
  57. cl::desc("<second file>"),
  58. cl::Required, cl::cat(DiffCategory));
  59. static cl::list<std::string> GlobalsToCompare(cl::Positional,
  60. cl::desc("<globals to compare>"),
  61. cl::cat(DiffCategory));
  62. int main(int argc, char **argv) {
  63. cl::HideUnrelatedOptions({&DiffCategory, &getColorCategory()});
  64. cl::ParseCommandLineOptions(argc, argv);
  65. LLVMContext Context;
  66. // Load both modules. Die if that fails.
  67. std::unique_ptr<Module> LModule = readModule(Context, LeftFilename);
  68. std::unique_ptr<Module> RModule = readModule(Context, RightFilename);
  69. if (!LModule || !RModule) return 1;
  70. DiffConsumer Consumer;
  71. DifferenceEngine Engine(Consumer);
  72. // If any global names were given, just diff those.
  73. if (!GlobalsToCompare.empty()) {
  74. for (unsigned I = 0, E = GlobalsToCompare.size(); I != E; ++I)
  75. diffGlobal(Engine, *LModule, *RModule, GlobalsToCompare[I]);
  76. // Otherwise, diff everything in the module.
  77. } else {
  78. Engine.diff(LModule.get(), RModule.get());
  79. }
  80. return Consumer.hadDifferences();
  81. }