1 //===-- DiffEngine.h - File comparator --------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This header defines the interface to the llvm-tapi difference engine,
10 // which structurally compares two tbd files.
12 //===----------------------------------------------------------------------===/
13 #ifndef LLVM_TOOLS_LLVM_TAPI_DIFF_DIFFENGINE_H
14 #define LLVM_TOOLS_LLVM_TAPI_DIFF_DIFFENGINE_H
16 #include "llvm/Object/TapiUniversal.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "llvm/TextAPI/Symbol.h"
19 #include "llvm/TextAPI/Target.h"
23 /// InterfaceInputOrder determines from which file the diff attribute belongs
25 enum InterfaceInputOrder
{ lhs
, rhs
};
27 /// DiffAttrKind is the enum that holds the concrete bases for RTTI.
29 AD_Diff_Scalar_PackedVersion
,
30 AD_Diff_Scalar_Unsigned
,
38 /// AttributeDiff is the abstract class for RTTI.
41 AttributeDiff(DiffAttrKind Kind
) : Kind(Kind
){};
42 virtual ~AttributeDiff(){};
43 DiffAttrKind
getKind() const { return Kind
; }
49 /// DiffOutput is the representation of a diff for a single attribute.
51 /// The name of the attribute.
55 /// Different values for the attribute
56 /// from each file where a diff is present.
57 std::vector
<std::unique_ptr
<AttributeDiff
>> Values
;
58 DiffOutput(std::string Name
) : Name(Name
){};
61 /// DiffScalarVal is a template class for the different types of scalar values.
62 template <class T
, DiffAttrKind U
> class DiffScalarVal
: public AttributeDiff
{
64 DiffScalarVal(InterfaceInputOrder Order
, T Val
)
65 : AttributeDiff(U
), Order(Order
), Val(Val
){};
67 static bool classof(const AttributeDiff
*A
) { return A
->getKind() == U
; }
69 void print(raw_ostream
&, std::string
);
71 T
getVal() const { return Val
; }
72 InterfaceInputOrder
getOrder() const { return Order
; }
75 /// The order is the file from which the diff is found.
76 InterfaceInputOrder Order
;
80 /// SymScalar is the diff symbol and the order.
83 SymScalar(InterfaceInputOrder Order
, const MachO::Symbol
*Sym
)
84 : Order(Order
), Val(Sym
){};
86 std::string
getFlagString(const MachO::Symbol
*Sym
);
88 void print(raw_ostream
&OS
, std::string Indent
, MachO::Target Targ
);
90 const MachO::Symbol
*getVal() const { return Val
; }
91 InterfaceInputOrder
getOrder() const { return Order
; }
94 /// The order is the file from which the diff is found.
95 InterfaceInputOrder Order
;
96 const MachO::Symbol
*Val
;
97 StringLiteral
getSymbolNamePrefix(MachO::EncodeKind Kind
);
100 class DiffStrVec
: public AttributeDiff
{
103 /// Values is a vector of StringRef values associated with the target.
104 std::vector
<DiffScalarVal
<StringRef
, AD_Diff_Scalar_Str
>> TargValues
;
105 DiffStrVec(MachO::Target Targ
) : AttributeDiff(AD_Str_Vec
), Targ(Targ
){};
107 static bool classof(const AttributeDiff
*A
) {
108 return A
->getKind() == AD_Str_Vec
;
112 class DiffSymVec
: public AttributeDiff
{
115 /// Values is a vector of symbol values associated with the target.
116 std::vector
<SymScalar
> TargValues
;
117 DiffSymVec(MachO::Target Targ
) : AttributeDiff(AD_Sym_Vec
), Targ(Targ
){};
119 static bool classof(const AttributeDiff
*A
) {
120 return A
->getKind() == AD_Sym_Vec
;
124 /// InlineDoc represents an inlined framework/library in a TBD File.
125 class InlineDoc
: public AttributeDiff
{
127 /// Install name of the framework/library.
128 std::string InstallName
;
129 /// Differences found from each file.
130 std::vector
<DiffOutput
> DocValues
;
131 InlineDoc(StringRef InstName
, std::vector
<DiffOutput
> Diff
)
132 : AttributeDiff(AD_Inline_Doc
), InstallName(InstName
),
133 DocValues(std::move(Diff
)){};
135 static bool classof(const AttributeDiff
*A
) {
136 return A
->getKind() == AD_Inline_Doc
;
140 /// DiffEngine contains the methods to compare the input files and print the
141 /// output of the differences found in the files.
144 DiffEngine(MachO::InterfaceFile
*InputFileNameLHS
,
145 MachO::InterfaceFile
*InputFileNameRHS
)
146 : FileLHS(InputFileNameLHS
), FileRHS(InputFileNameRHS
){};
147 bool compareFiles(raw_ostream
&);
150 MachO::InterfaceFile
*FileLHS
;
151 MachO::InterfaceFile
*FileRHS
;
153 /// Function that prints the differences found in the files.
154 void printDifferences(raw_ostream
&, const std::vector
<DiffOutput
> &, int);
155 /// Function that does the comparison of the TBD files and returns the
157 std::vector
<DiffOutput
> findDifferences(const MachO::InterfaceFile
*,
158 const MachO::InterfaceFile
*);