DiffLog.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //===-- DiffLog.h - Difference Log Builder and accessories ------*- 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 log builder.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_TOOLS_LLVM_DIFF_DIFFLOG_H
  13. #define LLVM_TOOLS_LLVM_DIFF_DIFFLOG_H
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "llvm/ADT/StringRef.h"
  16. namespace llvm {
  17. class Instruction;
  18. class Value;
  19. class Consumer;
  20. /// Trichotomy assumption
  21. enum DiffChange { DC_match, DC_left, DC_right };
  22. /// A temporary-object class for building up log messages.
  23. class LogBuilder {
  24. Consumer *consumer;
  25. /// The use of a stored StringRef here is okay because
  26. /// LogBuilder should be used only as a temporary, and as a
  27. /// temporary it will be destructed before whatever temporary
  28. /// might be initializing this format.
  29. StringRef Format;
  30. SmallVector<const Value *, 4> Arguments;
  31. public:
  32. LogBuilder(Consumer &c, StringRef Format) : consumer(&c), Format(Format) {}
  33. LogBuilder(LogBuilder &&L)
  34. : consumer(L.consumer), Format(L.Format),
  35. Arguments(std::move(L.Arguments)) {
  36. L.consumer = nullptr;
  37. }
  38. LogBuilder &operator<<(const Value *V) {
  39. Arguments.push_back(V);
  40. return *this;
  41. }
  42. ~LogBuilder();
  43. StringRef getFormat() const;
  44. unsigned getNumArguments() const;
  45. const Value *getArgument(unsigned I) const;
  46. };
  47. /// A temporary-object class for building up diff messages.
  48. class DiffLogBuilder {
  49. typedef std::pair<const Instruction *, const Instruction *> DiffRecord;
  50. SmallVector<DiffRecord, 20> Diff;
  51. Consumer &consumer;
  52. public:
  53. DiffLogBuilder(Consumer &c) : consumer(c) {}
  54. ~DiffLogBuilder();
  55. void addMatch(const Instruction *L, const Instruction *R);
  56. // HACK: VS 2010 has a bug in the stdlib that requires this.
  57. void addLeft(const Instruction *L);
  58. void addRight(const Instruction *R);
  59. unsigned getNumLines() const;
  60. DiffChange getLineKind(unsigned I) const;
  61. const Instruction *getLeft(unsigned I) const;
  62. const Instruction *getRight(unsigned I) const;
  63. };
  64. }
  65. #endif