2
0

llvm-diff.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //===-- llvm-diff.cpp - Module comparator command-line driver ---*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the command-line driver for the difference engine.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "DiffLog.h"
  14. #include "DifferenceEngine.h"
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/IR/LLVMContext.h"
  19. #include "llvm/IR/Module.h"
  20. #include "llvm/IR/Type.h"
  21. #include "llvm/IRReader/IRReader.h"
  22. #include "llvm/Support/CommandLine.h"
  23. #include "llvm/Support/MemoryBuffer.h"
  24. #include "llvm/Support/SourceMgr.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include <string>
  27. #include <utility>
  28. using namespace llvm;
  29. /// Reads a module from a file. On error, messages are written to stderr
  30. /// and null is returned.
  31. static 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. static void diffGlobal(DifferenceEngine &Engine, Module &L, Module &R,
  40. StringRef Name) {
  41. // Drop leading sigils from the global name.
  42. if (Name.startswith("@")) Name = Name.substr(1);
  43. Function *LFn = L.getFunction(Name);
  44. Function *RFn = R.getFunction(Name);
  45. if (LFn && RFn)
  46. Engine.diff(LFn, RFn);
  47. else if (!LFn && !RFn)
  48. errs() << "No function named @" << Name << " in either module\n";
  49. else if (!LFn)
  50. errs() << "No function named @" << Name << " in left module\n";
  51. else
  52. errs() << "No function named @" << Name << " in right module\n";
  53. }
  54. static cl::opt<std::string> LeftFilename(cl::Positional,
  55. cl::desc("<first file>"),
  56. cl::Required);
  57. static cl::opt<std::string> RightFilename(cl::Positional,
  58. cl::desc("<second file>"),
  59. cl::Required);
  60. static cl::list<std::string> GlobalsToCompare(cl::Positional,
  61. cl::desc("<globals to compare>"));
  62. // HLSL Change: changed calling convention to __cdecl
  63. int __cdecl main(int argc, char **argv) {
  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. }