xref: /llvm-project/clang-tools-extra/clang-include-fixer/find-all-symbols/SymbolInfo.cpp (revision 255ee643a8c559375a9c9787ef7d121790040818)
1*43356f56SNico Weber //===-- SymbolInfo.cpp - Symbol Info ----------------------------*- C++ -*-===//
2*43356f56SNico Weber //
3*43356f56SNico Weber // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*43356f56SNico Weber // See https://llvm.org/LICENSE.txt for license information.
5*43356f56SNico Weber // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*43356f56SNico Weber //
7*43356f56SNico Weber //===----------------------------------------------------------------------===//
8*43356f56SNico Weber 
9*43356f56SNico Weber #include "SymbolInfo.h"
10*43356f56SNico Weber #include "llvm/Support/CommandLine.h"
11*43356f56SNico Weber #include "llvm/Support/FileSystem.h"
12*43356f56SNico Weber #include "llvm/Support/YAMLTraits.h"
13*43356f56SNico Weber #include "llvm/Support/raw_ostream.h"
14*43356f56SNico Weber 
15*43356f56SNico Weber using llvm::yaml::MappingTraits;
16*43356f56SNico Weber using ContextType = clang::find_all_symbols::SymbolInfo::ContextType;
17*43356f56SNico Weber using clang::find_all_symbols::SymbolInfo;
18*43356f56SNico Weber using clang::find_all_symbols::SymbolAndSignals;
19*43356f56SNico Weber using SymbolKind = clang::find_all_symbols::SymbolInfo::SymbolKind;
20*43356f56SNico Weber 
21*43356f56SNico Weber LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(SymbolAndSignals)
22*43356f56SNico Weber LLVM_YAML_IS_SEQUENCE_VECTOR(SymbolInfo::Context)
23*43356f56SNico Weber 
24*43356f56SNico Weber namespace llvm {
25*43356f56SNico Weber namespace yaml {
26*43356f56SNico Weber template <> struct MappingTraits<SymbolAndSignals> {
mappingllvm::yaml::MappingTraits27*43356f56SNico Weber   static void mapping(IO &io, SymbolAndSignals &Symbol) {
28*43356f56SNico Weber     io.mapRequired("Name", Symbol.Symbol.Name);
29*43356f56SNico Weber     io.mapRequired("Contexts", Symbol.Symbol.Contexts);
30*43356f56SNico Weber     io.mapRequired("FilePath", Symbol.Symbol.FilePath);
31*43356f56SNico Weber     io.mapRequired("Type", Symbol.Symbol.Type);
32*43356f56SNico Weber     io.mapRequired("Seen", Symbol.Signals.Seen);
33*43356f56SNico Weber     io.mapRequired("Used", Symbol.Signals.Used);
34*43356f56SNico Weber   }
35*43356f56SNico Weber };
36*43356f56SNico Weber 
37*43356f56SNico Weber template <> struct ScalarEnumerationTraits<ContextType> {
enumerationllvm::yaml::ScalarEnumerationTraits38*43356f56SNico Weber   static void enumeration(IO &io, ContextType &value) {
39*43356f56SNico Weber     io.enumCase(value, "Record", ContextType::Record);
40*43356f56SNico Weber     io.enumCase(value, "Namespace", ContextType::Namespace);
41*43356f56SNico Weber     io.enumCase(value, "EnumDecl", ContextType::EnumDecl);
42*43356f56SNico Weber   }
43*43356f56SNico Weber };
44*43356f56SNico Weber 
45*43356f56SNico Weber template <> struct ScalarEnumerationTraits<SymbolKind> {
enumerationllvm::yaml::ScalarEnumerationTraits46*43356f56SNico Weber   static void enumeration(IO &io, SymbolKind &value) {
47*43356f56SNico Weber     io.enumCase(value, "Variable", SymbolKind::Variable);
48*43356f56SNico Weber     io.enumCase(value, "Function", SymbolKind::Function);
49*43356f56SNico Weber     io.enumCase(value, "Class", SymbolKind::Class);
50*43356f56SNico Weber     io.enumCase(value, "TypedefName", SymbolKind::TypedefName);
51*43356f56SNico Weber     io.enumCase(value, "EnumDecl", SymbolKind::EnumDecl);
52*43356f56SNico Weber     io.enumCase(value, "EnumConstantDecl", SymbolKind::EnumConstantDecl);
53*43356f56SNico Weber     io.enumCase(value, "Macro", SymbolKind::Macro);
54*43356f56SNico Weber     io.enumCase(value, "Unknown", SymbolKind::Unknown);
55*43356f56SNico Weber   }
56*43356f56SNico Weber };
57*43356f56SNico Weber 
58*43356f56SNico Weber template <> struct MappingTraits<SymbolInfo::Context> {
mappingllvm::yaml::MappingTraits59*43356f56SNico Weber   static void mapping(IO &io, SymbolInfo::Context &Context) {
60*43356f56SNico Weber     io.mapRequired("ContextType", Context.first);
61*43356f56SNico Weber     io.mapRequired("ContextName", Context.second);
62*43356f56SNico Weber   }
63*43356f56SNico Weber };
64*43356f56SNico Weber 
65*43356f56SNico Weber } // namespace yaml
66*43356f56SNico Weber } // namespace llvm
67*43356f56SNico Weber 
68*43356f56SNico Weber namespace clang {
69*43356f56SNico Weber namespace find_all_symbols {
70*43356f56SNico Weber 
SymbolInfo(llvm::StringRef Name,SymbolKind Type,llvm::StringRef FilePath,const std::vector<Context> & Contexts)71*43356f56SNico Weber SymbolInfo::SymbolInfo(llvm::StringRef Name, SymbolKind Type,
72*43356f56SNico Weber                        llvm::StringRef FilePath,
73*43356f56SNico Weber                        const std::vector<Context> &Contexts)
74*43356f56SNico Weber     : Name(Name), Type(Type), FilePath(FilePath), Contexts(Contexts) {}
75*43356f56SNico Weber 
operator ==(const SymbolInfo & Symbol) const76*43356f56SNico Weber bool SymbolInfo::operator==(const SymbolInfo &Symbol) const {
77*43356f56SNico Weber   return std::tie(Name, Type, FilePath, Contexts) ==
78*43356f56SNico Weber          std::tie(Symbol.Name, Symbol.Type, Symbol.FilePath, Symbol.Contexts);
79*43356f56SNico Weber }
80*43356f56SNico Weber 
operator <(const SymbolInfo & Symbol) const81*43356f56SNico Weber bool SymbolInfo::operator<(const SymbolInfo &Symbol) const {
82*43356f56SNico Weber   return std::tie(Name, Type, FilePath, Contexts) <
83*43356f56SNico Weber          std::tie(Symbol.Name, Symbol.Type, Symbol.FilePath, Symbol.Contexts);
84*43356f56SNico Weber }
85*43356f56SNico Weber 
getQualifiedName() const86*43356f56SNico Weber std::string SymbolInfo::getQualifiedName() const {
87*43356f56SNico Weber   std::string QualifiedName = Name;
88*43356f56SNico Weber   for (const auto &Context : Contexts) {
89*43356f56SNico Weber     if (Context.first == ContextType::EnumDecl)
90*43356f56SNico Weber       continue;
91*43356f56SNico Weber     QualifiedName = Context.second + "::" + QualifiedName;
92*43356f56SNico Weber   }
93*43356f56SNico Weber   return QualifiedName;
94*43356f56SNico Weber }
95*43356f56SNico Weber 
operator +=(const Signals & RHS)96*43356f56SNico Weber SymbolInfo::Signals &SymbolInfo::Signals::operator+=(const Signals &RHS) {
97*43356f56SNico Weber   Seen += RHS.Seen;
98*43356f56SNico Weber   Used += RHS.Used;
99*43356f56SNico Weber   return *this;
100*43356f56SNico Weber }
101*43356f56SNico Weber 
operator +(const Signals & RHS) const102*43356f56SNico Weber SymbolInfo::Signals SymbolInfo::Signals::operator+(const Signals &RHS) const {
103*43356f56SNico Weber   Signals Result = *this;
104*43356f56SNico Weber   Result += RHS;
105*43356f56SNico Weber   return Result;
106*43356f56SNico Weber }
107*43356f56SNico Weber 
operator ==(const Signals & RHS) const108*43356f56SNico Weber bool SymbolInfo::Signals::operator==(const Signals &RHS) const {
109*43356f56SNico Weber   return std::tie(Seen, Used) == std::tie(RHS.Seen, RHS.Used);
110*43356f56SNico Weber }
111*43356f56SNico Weber 
operator ==(const SymbolAndSignals & RHS) const112*43356f56SNico Weber bool SymbolAndSignals::operator==(const SymbolAndSignals& RHS) const {
113*43356f56SNico Weber   return std::tie(Symbol, Signals) == std::tie(RHS.Symbol, RHS.Signals);
114*43356f56SNico Weber }
115*43356f56SNico Weber 
WriteSymbolInfosToStream(llvm::raw_ostream & OS,const SymbolInfo::SignalMap & Symbols)116*43356f56SNico Weber bool WriteSymbolInfosToStream(llvm::raw_ostream &OS,
117*43356f56SNico Weber                               const SymbolInfo::SignalMap &Symbols) {
118*43356f56SNico Weber   llvm::yaml::Output yout(OS);
119*43356f56SNico Weber   for (const auto &Symbol : Symbols) {
120*43356f56SNico Weber     SymbolAndSignals S{Symbol.first, Symbol.second};
121*43356f56SNico Weber     yout << S;
122*43356f56SNico Weber   }
123*43356f56SNico Weber   return true;
124*43356f56SNico Weber }
125*43356f56SNico Weber 
ReadSymbolInfosFromYAML(llvm::StringRef Yaml)126*43356f56SNico Weber std::vector<SymbolAndSignals> ReadSymbolInfosFromYAML(llvm::StringRef Yaml) {
127*43356f56SNico Weber   std::vector<SymbolAndSignals> Symbols;
128*43356f56SNico Weber   llvm::yaml::Input yin(Yaml);
129*43356f56SNico Weber   yin >> Symbols;
130*43356f56SNico Weber   return Symbols;
131*43356f56SNico Weber }
132*43356f56SNico Weber 
133*43356f56SNico Weber } // namespace find_all_symbols
134*43356f56SNico Weber } // namespace clang
135