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