forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiffEngine.h
169 lines (141 loc) · 5.25 KB
/
DiffEngine.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//===-- DiffEngine.h - File comparator --------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This header defines the interface to the llvm-tapi difference engine,
// which structurally compares two tbd files.
//
//===----------------------------------------------------------------------===/
#ifndef LLVM_TOOLS_LLVM_TAPI_DIFF_DIFFENGINE_H
#define LLVM_TOOLS_LLVM_TAPI_DIFF_DIFFENGINE_H
#include "llvm/ADT/Optional.h"
#include "llvm/Object/TapiUniversal.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TextAPI/Symbol.h"
#include "llvm/TextAPI/Target.h"
namespace llvm {
/// InterfaceInputOrder determines from which file the diff attribute belongs
/// to.
enum InterfaceInputOrder { lhs, rhs };
/// DiffAttrKind is the enum that holds the concrete bases for RTTI.
enum DiffAttrKind {
AD_Diff_Scalar_PackedVersion,
AD_Diff_Scalar_Unsigned,
AD_Diff_Scalar_Bool,
AD_Diff_Scalar_Str,
AD_Str_Vec,
AD_Sym_Vec,
AD_Inline_Doc,
};
/// AttributeDiff is the abstract class for RTTI.
class AttributeDiff {
public:
AttributeDiff(DiffAttrKind Kind) : Kind(Kind){};
virtual ~AttributeDiff(){};
DiffAttrKind getKind() const { return Kind; }
private:
DiffAttrKind Kind;
};
/// DiffOutput is the representation of a diff for a single attribute.
struct DiffOutput {
/// The name of the attribute.
std::string Name;
/// The kind for RTTI
DiffAttrKind Kind;
/// Different values for the attribute
/// from each file where a diff is present.
std::vector<std::unique_ptr<AttributeDiff>> Values;
DiffOutput(std::string Name) : Name(Name){};
};
/// DiffScalarVal is a template class for the different types of scalar values.
template <class T, DiffAttrKind U> class DiffScalarVal : public AttributeDiff {
public:
DiffScalarVal(InterfaceInputOrder Order, T Val)
: AttributeDiff(U), Order(Order), Val(Val){};
static bool classof(const AttributeDiff *A) { return A->getKind() == U; }
void print(raw_ostream &, std::string);
T getVal() const { return Val; }
InterfaceInputOrder getOrder() const { return Order; }
private:
/// The order is the file from which the diff is found.
InterfaceInputOrder Order;
T Val;
};
/// SymScalar is the diff symbol and the order.
class SymScalar {
public:
SymScalar(InterfaceInputOrder Order, const MachO::Symbol *Sym)
: Order(Order), Val(Sym){};
std::string getFlagString(MachO::SymbolFlags Flags) {
return Flags != MachO::SymbolFlags::None
? " - " + stringifySymbolFlag(Flags)
: stringifySymbolFlag(Flags);
}
void print(raw_ostream &OS, std::string Indent, MachO::Target Targ);
const MachO::Symbol *getVal() const { return Val; }
InterfaceInputOrder getOrder() const { return Order; }
private:
/// The order is the file from which the diff is found.
InterfaceInputOrder Order;
const MachO::Symbol *Val;
StringLiteral getSymbolNamePrefix(MachO::SymbolKind Kind);
std::string stringifySymbolFlag(MachO::SymbolFlags Flag);
};
class DiffStrVec : public AttributeDiff {
public:
MachO::Target Targ;
/// Values is a vector of StringRef values associated with the target.
std::vector<DiffScalarVal<StringRef, AD_Diff_Scalar_Str>> TargValues;
DiffStrVec(MachO::Target Targ) : AttributeDiff(AD_Str_Vec), Targ(Targ){};
static bool classof(const AttributeDiff *A) {
return A->getKind() == AD_Str_Vec;
}
};
class DiffSymVec : public AttributeDiff {
public:
MachO::Target Targ;
/// Values is a vector of symbol values associated with the target.
std::vector<SymScalar> TargValues;
DiffSymVec(MachO::Target Targ) : AttributeDiff(AD_Sym_Vec), Targ(Targ){};
static bool classof(const AttributeDiff *A) {
return A->getKind() == AD_Sym_Vec;
}
};
/// InlineDoc represents an inlined framework/library in a TBD File.
class InlineDoc : public AttributeDiff {
public:
/// Install name of the framework/library.
std::string InstallName;
/// Differences found from each file.
std::vector<DiffOutput> DocValues;
InlineDoc(StringRef InstName, std::vector<DiffOutput> Diff)
: AttributeDiff(AD_Inline_Doc), InstallName(InstName),
DocValues(std::move(Diff)){};
static bool classof(const AttributeDiff *A) {
return A->getKind() == AD_Inline_Doc;
}
};
/// DiffEngine contains the methods to compare the input files and print the
/// output of the differences found in the files.
class DiffEngine {
public:
DiffEngine(object::TapiUniversal *InputFileNameLHS,
object::TapiUniversal *InputFileNameRHS)
: FileLHS(InputFileNameLHS), FileRHS(InputFileNameRHS){};
bool compareFiles(raw_ostream &);
private:
object::TapiUniversal *FileLHS;
object::TapiUniversal *FileRHS;
/// Function that prints the differences found in the files.
void printDifferences(raw_ostream &, const std::vector<DiffOutput> &, int);
/// Function that does the comparison of the TBD files and returns the
/// differences.
std::vector<DiffOutput> findDifferences(const MachO::InterfaceFile *,
const MachO::InterfaceFile *);
};
} // namespace llvm
#endif