xref: /llvm-project/llvm/tools/llvm-readtapi/DiffEngine.h (revision d9a9872ec4760762fdc467ef283cea302a3742e5)
15656d797SCyndy Ishida //===-- DiffEngine.h - File comparator --------------------------*- C++ -*-===//
25656d797SCyndy Ishida //
35656d797SCyndy Ishida // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45656d797SCyndy Ishida // See https://llvm.org/LICENSE.txt for license information.
55656d797SCyndy Ishida // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65656d797SCyndy Ishida //
75656d797SCyndy Ishida //===----------------------------------------------------------------------===//
85656d797SCyndy Ishida //
95656d797SCyndy Ishida // This header defines the interface to the llvm-tapi difference engine,
105656d797SCyndy Ishida // which structurally compares two tbd files.
115656d797SCyndy Ishida //
125656d797SCyndy Ishida //===----------------------------------------------------------------------===/
135656d797SCyndy Ishida #ifndef LLVM_TOOLS_LLVM_TAPI_DIFF_DIFFENGINE_H
145656d797SCyndy Ishida #define LLVM_TOOLS_LLVM_TAPI_DIFF_DIFFENGINE_H
155656d797SCyndy Ishida 
165656d797SCyndy Ishida #include "llvm/Object/TapiUniversal.h"
175656d797SCyndy Ishida #include "llvm/Support/raw_ostream.h"
185656d797SCyndy Ishida #include "llvm/TextAPI/Symbol.h"
195656d797SCyndy Ishida #include "llvm/TextAPI/Target.h"
205656d797SCyndy Ishida 
215656d797SCyndy Ishida namespace llvm {
225656d797SCyndy Ishida 
235656d797SCyndy Ishida /// InterfaceInputOrder determines from which file the diff attribute belongs
245656d797SCyndy Ishida /// to.
255656d797SCyndy Ishida enum InterfaceInputOrder { lhs, rhs };
265656d797SCyndy Ishida 
275656d797SCyndy Ishida /// DiffAttrKind is the enum that holds the concrete bases for RTTI.
285656d797SCyndy Ishida enum DiffAttrKind {
295656d797SCyndy Ishida   AD_Diff_Scalar_PackedVersion,
305656d797SCyndy Ishida   AD_Diff_Scalar_Unsigned,
315656d797SCyndy Ishida   AD_Diff_Scalar_Bool,
325656d797SCyndy Ishida   AD_Diff_Scalar_Str,
335656d797SCyndy Ishida   AD_Str_Vec,
345656d797SCyndy Ishida   AD_Sym_Vec,
355656d797SCyndy Ishida   AD_Inline_Doc,
365656d797SCyndy Ishida };
375656d797SCyndy Ishida 
385656d797SCyndy Ishida /// AttributeDiff is the abstract class for RTTI.
395656d797SCyndy Ishida class AttributeDiff {
405656d797SCyndy Ishida public:
AttributeDiff(DiffAttrKind Kind)415656d797SCyndy Ishida   AttributeDiff(DiffAttrKind Kind) : Kind(Kind){};
~AttributeDiff()425656d797SCyndy Ishida   virtual ~AttributeDiff(){};
getKind()435656d797SCyndy Ishida   DiffAttrKind getKind() const { return Kind; }
445656d797SCyndy Ishida 
455656d797SCyndy Ishida private:
465656d797SCyndy Ishida   DiffAttrKind Kind;
475656d797SCyndy Ishida };
485656d797SCyndy Ishida 
495656d797SCyndy Ishida /// DiffOutput is the representation of a diff for a single attribute.
505656d797SCyndy Ishida struct DiffOutput {
515656d797SCyndy Ishida   /// The name of the attribute.
525656d797SCyndy Ishida   std::string Name;
535656d797SCyndy Ishida   /// The kind for RTTI
545656d797SCyndy Ishida   DiffAttrKind Kind;
555656d797SCyndy Ishida   /// Different values for the attribute
565656d797SCyndy Ishida   /// from each file where a diff is present.
575656d797SCyndy Ishida   std::vector<std::unique_ptr<AttributeDiff>> Values;
DiffOutputDiffOutput585656d797SCyndy Ishida   DiffOutput(std::string Name) : Name(Name){};
595656d797SCyndy Ishida };
605656d797SCyndy Ishida 
615656d797SCyndy Ishida /// DiffScalarVal is a template class for the different types of scalar values.
625656d797SCyndy Ishida template <class T, DiffAttrKind U> class DiffScalarVal : public AttributeDiff {
635656d797SCyndy Ishida public:
DiffScalarVal(InterfaceInputOrder Order,T Val)645656d797SCyndy Ishida   DiffScalarVal(InterfaceInputOrder Order, T Val)
655656d797SCyndy Ishida       : AttributeDiff(U), Order(Order), Val(Val){};
665656d797SCyndy Ishida 
classof(const AttributeDiff * A)675656d797SCyndy Ishida   static bool classof(const AttributeDiff *A) { return A->getKind() == U; }
685656d797SCyndy Ishida 
695656d797SCyndy Ishida   void print(raw_ostream &, std::string);
705656d797SCyndy Ishida 
getVal()715656d797SCyndy Ishida   T getVal() const { return Val; }
getOrder()725656d797SCyndy Ishida   InterfaceInputOrder getOrder() const { return Order; }
735656d797SCyndy Ishida 
745656d797SCyndy Ishida private:
755656d797SCyndy Ishida   /// The order is the file from which the diff is found.
765656d797SCyndy Ishida   InterfaceInputOrder Order;
775656d797SCyndy Ishida   T Val;
785656d797SCyndy Ishida };
795656d797SCyndy Ishida 
805656d797SCyndy Ishida /// SymScalar is the diff symbol and the order.
815656d797SCyndy Ishida class SymScalar {
825656d797SCyndy Ishida public:
SymScalar(InterfaceInputOrder Order,const MachO::Symbol * Sym)835656d797SCyndy Ishida   SymScalar(InterfaceInputOrder Order, const MachO::Symbol *Sym)
845656d797SCyndy Ishida       : Order(Order), Val(Sym){};
855656d797SCyndy Ishida 
865656d797SCyndy Ishida   std::string getFlagString(const MachO::Symbol *Sym);
875656d797SCyndy Ishida 
885656d797SCyndy Ishida   void print(raw_ostream &OS, std::string Indent, MachO::Target Targ);
895656d797SCyndy Ishida 
getVal()905656d797SCyndy Ishida   const MachO::Symbol *getVal() const { return Val; }
getOrder()915656d797SCyndy Ishida   InterfaceInputOrder getOrder() const { return Order; }
925656d797SCyndy Ishida 
935656d797SCyndy Ishida private:
945656d797SCyndy Ishida   /// The order is the file from which the diff is found.
955656d797SCyndy Ishida   InterfaceInputOrder Order;
965656d797SCyndy Ishida   const MachO::Symbol *Val;
97*d9a9872eSCyndy Ishida   StringLiteral getSymbolNamePrefix(MachO::EncodeKind Kind);
985656d797SCyndy Ishida };
995656d797SCyndy Ishida 
1005656d797SCyndy Ishida class DiffStrVec : public AttributeDiff {
1015656d797SCyndy Ishida public:
1025656d797SCyndy Ishida   MachO::Target Targ;
1035656d797SCyndy Ishida   /// Values is a vector of StringRef values associated with the target.
1045656d797SCyndy Ishida   std::vector<DiffScalarVal<StringRef, AD_Diff_Scalar_Str>> TargValues;
DiffStrVec(MachO::Target Targ)1055656d797SCyndy Ishida   DiffStrVec(MachO::Target Targ) : AttributeDiff(AD_Str_Vec), Targ(Targ){};
1065656d797SCyndy Ishida 
classof(const AttributeDiff * A)1075656d797SCyndy Ishida   static bool classof(const AttributeDiff *A) {
1085656d797SCyndy Ishida     return A->getKind() == AD_Str_Vec;
1095656d797SCyndy Ishida   }
1105656d797SCyndy Ishida };
1115656d797SCyndy Ishida 
1125656d797SCyndy Ishida class DiffSymVec : public AttributeDiff {
1135656d797SCyndy Ishida public:
1145656d797SCyndy Ishida   MachO::Target Targ;
1155656d797SCyndy Ishida   /// Values is a vector of symbol values associated with the target.
1165656d797SCyndy Ishida   std::vector<SymScalar> TargValues;
DiffSymVec(MachO::Target Targ)1175656d797SCyndy Ishida   DiffSymVec(MachO::Target Targ) : AttributeDiff(AD_Sym_Vec), Targ(Targ){};
1185656d797SCyndy Ishida 
classof(const AttributeDiff * A)1195656d797SCyndy Ishida   static bool classof(const AttributeDiff *A) {
1205656d797SCyndy Ishida     return A->getKind() == AD_Sym_Vec;
1215656d797SCyndy Ishida   }
1225656d797SCyndy Ishida };
1235656d797SCyndy Ishida 
1245656d797SCyndy Ishida /// InlineDoc represents an inlined framework/library in a TBD File.
1255656d797SCyndy Ishida class InlineDoc : public AttributeDiff {
1265656d797SCyndy Ishida public:
1275656d797SCyndy Ishida   /// Install name of the framework/library.
1285656d797SCyndy Ishida   std::string InstallName;
1295656d797SCyndy Ishida   /// Differences found from each file.
1305656d797SCyndy Ishida   std::vector<DiffOutput> DocValues;
InlineDoc(StringRef InstName,std::vector<DiffOutput> Diff)1315656d797SCyndy Ishida   InlineDoc(StringRef InstName, std::vector<DiffOutput> Diff)
1325656d797SCyndy Ishida       : AttributeDiff(AD_Inline_Doc), InstallName(InstName),
1335656d797SCyndy Ishida         DocValues(std::move(Diff)){};
1345656d797SCyndy Ishida 
classof(const AttributeDiff * A)1355656d797SCyndy Ishida   static bool classof(const AttributeDiff *A) {
1365656d797SCyndy Ishida     return A->getKind() == AD_Inline_Doc;
1375656d797SCyndy Ishida   }
1385656d797SCyndy Ishida };
1395656d797SCyndy Ishida 
1405656d797SCyndy Ishida /// DiffEngine contains the methods to compare the input files and print the
1415656d797SCyndy Ishida /// output of the differences found in the files.
1425656d797SCyndy Ishida class DiffEngine {
1435656d797SCyndy Ishida public:
DiffEngine(MachO::InterfaceFile * InputFileNameLHS,MachO::InterfaceFile * InputFileNameRHS)144ae182dbbSCyndy Ishida   DiffEngine(MachO::InterfaceFile *InputFileNameLHS,
145ae182dbbSCyndy Ishida              MachO::InterfaceFile *InputFileNameRHS)
1465656d797SCyndy Ishida       : FileLHS(InputFileNameLHS), FileRHS(InputFileNameRHS){};
1475656d797SCyndy Ishida   bool compareFiles(raw_ostream &);
1485656d797SCyndy Ishida 
1495656d797SCyndy Ishida private:
150ae182dbbSCyndy Ishida   MachO::InterfaceFile *FileLHS;
151ae182dbbSCyndy Ishida   MachO::InterfaceFile *FileRHS;
1525656d797SCyndy Ishida 
1535656d797SCyndy Ishida   /// Function that prints the differences found in the files.
1545656d797SCyndy Ishida   void printDifferences(raw_ostream &, const std::vector<DiffOutput> &, int);
1555656d797SCyndy Ishida   /// Function that does the comparison of the TBD files and returns the
1565656d797SCyndy Ishida   /// differences.
1575656d797SCyndy Ishida   std::vector<DiffOutput> findDifferences(const MachO::InterfaceFile *,
1585656d797SCyndy Ishida                                           const MachO::InterfaceFile *);
1595656d797SCyndy Ishida };
1605656d797SCyndy Ishida 
1615656d797SCyndy Ishida } // namespace llvm
1625656d797SCyndy Ishida 
1635656d797SCyndy Ishida #endif
164