llvm-diff.cpp 3.1 KB

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