DiffConsumer.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //===-- DiffConsumer.h - Difference Consumer --------------------*- 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 header defines the interface to the LLVM difference Consumer
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_TOOLS_LLVM_DIFF_DIFFCONSUMER_H
  13. #define LLVM_TOOLS_LLVM_DIFF_DIFFCONSUMER_H
  14. #include "DiffLog.h"
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/IR/Value.h"
  18. #include "llvm/Support/Casting.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. namespace llvm {
  21. class StringRef;
  22. class Module;
  23. class Value;
  24. class Function;
  25. /// The interface for consumers of difference data.
  26. class Consumer {
  27. virtual void anchor();
  28. public:
  29. /// Record that a local context has been entered. Left and
  30. /// Right are IR "containers" of some sort which are being
  31. /// considered for structural equivalence: global variables,
  32. /// functions, blocks, instructions, etc.
  33. virtual void enterContext(Value *Left, Value *Right) = 0;
  34. /// Record that a local context has been exited.
  35. virtual void exitContext() = 0;
  36. /// Record a difference within the current context.
  37. virtual void log(StringRef Text) = 0;
  38. /// Record a formatted difference within the current context.
  39. virtual void logf(const LogBuilder &Log) = 0;
  40. /// Record a line-by-line instruction diff.
  41. virtual void logd(const DiffLogBuilder &Log) = 0;
  42. protected:
  43. virtual ~Consumer() {}
  44. };
  45. class DiffConsumer : public Consumer {
  46. private:
  47. struct DiffContext {
  48. DiffContext(Value *L, Value *R)
  49. : L(L), R(R), Differences(false), IsFunction(isa<Function>(L)) {}
  50. Value *L;
  51. Value *R;
  52. bool Differences;
  53. bool IsFunction;
  54. DenseMap<Value*,unsigned> LNumbering;
  55. DenseMap<Value*,unsigned> RNumbering;
  56. };
  57. raw_ostream &out;
  58. SmallVector<DiffContext, 5> contexts;
  59. bool Differences;
  60. unsigned Indent;
  61. void printValue(Value *V, bool isL);
  62. void header();
  63. void indent();
  64. public:
  65. DiffConsumer()
  66. : out(errs()), Differences(false), Indent(0) {}
  67. bool hadDifferences() const;
  68. void enterContext(Value *L, Value *R) override;
  69. void exitContext() override;
  70. void log(StringRef text) override;
  71. void logf(const LogBuilder &Log) override;
  72. void logd(const DiffLogBuilder &Log) override;
  73. };
  74. }
  75. #endif