clang-tools  16.0.0git
SymbolInfo.cpp
Go to the documentation of this file.
1 //===-- SymbolInfo.cpp - Symbol Info ----------------------------*- 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 #include "SymbolInfo.h"
10 #include "llvm/Support/CommandLine.h"
11 #include "llvm/Support/FileSystem.h"
12 #include "llvm/Support/YAMLTraits.h"
13 #include "llvm/Support/raw_ostream.h"
14 
15 using llvm::yaml::MappingTraits;
20 
21 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(SymbolAndSignals)
22 LLVM_YAML_IS_SEQUENCE_VECTOR(SymbolInfo::Context)
23 
24 namespace llvm {
25 namespace yaml {
26 template <> struct MappingTraits<SymbolAndSignals> {
27  static void mapping(IO &io, SymbolAndSignals &Symbol) {
28  io.mapRequired("Name", Symbol.Symbol.Name);
29  io.mapRequired("Contexts", Symbol.Symbol.Contexts);
30  io.mapRequired("FilePath", Symbol.Symbol.FilePath);
31  io.mapRequired("Type", Symbol.Symbol.Type);
32  io.mapRequired("Seen", Symbol.Signals.Seen);
33  io.mapRequired("Used", Symbol.Signals.Used);
34  }
35 };
36 
37 template <> struct ScalarEnumerationTraits<ContextType> {
38  static void enumeration(IO &io, ContextType &value) {
39  io.enumCase(value, "Record", ContextType::Record);
40  io.enumCase(value, "Namespace", ContextType::Namespace);
41  io.enumCase(value, "EnumDecl", ContextType::EnumDecl);
42  }
43 };
44 
45 template <> struct ScalarEnumerationTraits<SymbolKind> {
46  static void enumeration(IO &io, SymbolKind &value) {
47  io.enumCase(value, "Variable", SymbolKind::Variable);
48  io.enumCase(value, "Function", SymbolKind::Function);
49  io.enumCase(value, "Class", SymbolKind::Class);
50  io.enumCase(value, "TypedefName", SymbolKind::TypedefName);
51  io.enumCase(value, "EnumDecl", SymbolKind::EnumDecl);
52  io.enumCase(value, "EnumConstantDecl", SymbolKind::EnumConstantDecl);
53  io.enumCase(value, "Macro", SymbolKind::Macro);
54  io.enumCase(value, "Unknown", SymbolKind::Unknown);
55  }
56 };
57 
58 template <> struct MappingTraits<SymbolInfo::Context> {
59  static void mapping(IO &io, SymbolInfo::Context &Context) {
60  io.mapRequired("ContextType", Context.first);
61  io.mapRequired("ContextName", Context.second);
62  }
63 };
64 
65 } // namespace yaml
66 } // namespace llvm
67 
68 namespace clang {
69 namespace find_all_symbols {
70 
71 SymbolInfo::SymbolInfo(llvm::StringRef Name, SymbolKind Type,
72  llvm::StringRef FilePath,
73  const std::vector<Context> &Contexts)
74  : Name(Name), Type(Type), FilePath(FilePath), Contexts(Contexts) {}
75 
76 bool SymbolInfo::operator==(const SymbolInfo &Symbol) const {
77  return std::tie(Name, Type, FilePath, Contexts) ==
78  std::tie(Symbol.Name, Symbol.Type, Symbol.FilePath, Symbol.Contexts);
79 }
80 
81 bool SymbolInfo::operator<(const SymbolInfo &Symbol) const {
82  return std::tie(Name, Type, FilePath, Contexts) <
83  std::tie(Symbol.Name, Symbol.Type, Symbol.FilePath, Symbol.Contexts);
84 }
85 
86 std::string SymbolInfo::getQualifiedName() const {
87  std::string QualifiedName = Name;
88  for (const auto &Context : Contexts) {
89  if (Context.first == ContextType::EnumDecl)
90  continue;
91  QualifiedName = Context.second + "::" + QualifiedName;
92  }
93  return QualifiedName;
94 }
95 
97  Seen += RHS.Seen;
98  Used += RHS.Used;
99  return *this;
100 }
101 
103  Signals Result = *this;
104  Result += RHS;
105  return Result;
106 }
107 
108 bool SymbolInfo::Signals::operator==(const Signals &RHS) const {
109  return std::tie(Seen, Used) == std::tie(RHS.Seen, RHS.Used);
110 }
111 
113  return std::tie(Symbol, Signals) == std::tie(RHS.Symbol, RHS.Signals);
114 }
115 
116 bool WriteSymbolInfosToStream(llvm::raw_ostream &OS,
117  const SymbolInfo::SignalMap &Symbols) {
118  llvm::yaml::Output yout(OS);
119  for (const auto &Symbol : Symbols) {
120  SymbolAndSignals S{Symbol.first, Symbol.second};
121  yout << S;
122  }
123  return true;
124 }
125 
126 std::vector<SymbolAndSignals> ReadSymbolInfosFromYAML(llvm::StringRef Yaml) {
127  std::vector<SymbolAndSignals> Symbols;
128  llvm::yaml::Input yin(Yaml);
129  yin >> Symbols;
130  return Symbols;
131 }
132 
133 } // namespace find_all_symbols
134 } // namespace clang
clang::find_all_symbols::SymbolAndSignals
Definition: SymbolInfo.h:126
llvm::yaml::MappingTraits< SymbolInfo::Context >::mapping
static void mapping(IO &io, SymbolInfo::Context &Context)
Definition: SymbolInfo.cpp:59
clang::find_all_symbols::SymbolInfo
Describes a named symbol from a header.
Definition: SymbolInfo.h:27
clang::find_all_symbols::SymbolInfo::SymbolKind::EnumConstantDecl
@ EnumConstantDecl
llvm
Some operations such as code completion produce a set of candidates.
Definition: YAMLGenerator.cpp:31
clang::find_all_symbols::SymbolInfo::ContextType::Record
@ Record
clang::find_all_symbols::SymbolInfo::Signals::Used
unsigned Used
Definition: SymbolInfo.h:61
clang::find_all_symbols::SymbolInfo::SymbolKind::Variable
@ Variable
Type
NodeType Type
Definition: HTMLGenerator.cpp:73
clang::find_all_symbols::SymbolInfo::ContextType
ContextType
The Context Type.
Definition: SymbolInfo.h:42
llvm::yaml::MappingTraits< SymbolAndSignals >::mapping
static void mapping(IO &io, SymbolAndSignals &Symbol)
Definition: SymbolInfo.cpp:27
clang::find_all_symbols::SymbolAndSignals::Signals
SymbolInfo::Signals Signals
Definition: SymbolInfo.h:128
clang::find_all_symbols::SymbolInfo::operator<
bool operator<(const SymbolInfo &Symbol) const
Definition: SymbolInfo.cpp:81
clang::find_all_symbols::SymbolInfo::Signals::operator+=
Signals & operator+=(const Signals &RHS)
Definition: SymbolInfo.cpp:96
llvm::yaml::ScalarEnumerationTraits< SymbolKind >::enumeration
static void enumeration(IO &io, SymbolKind &value)
Definition: SymbolInfo.cpp:46
clang::find_all_symbols::SymbolAndSignals::operator==
bool operator==(const SymbolAndSignals &RHS) const
Definition: SymbolInfo.cpp:112
clang::find_all_symbols::SymbolInfo::SymbolKind::Class
@ Class
clang::find_all_symbols::SymbolInfo::SymbolKind::TypedefName
@ TypedefName
clang::find_all_symbols::SymbolInfo::Signals::Seen
unsigned Seen
Definition: SymbolInfo.h:58
clang::find_all_symbols::SymbolInfo::getQualifiedName
std::string getQualifiedName() const
Get the fully-qualified symbol name.
Definition: SymbolInfo.cpp:86
clang::find_all_symbols::SymbolInfo::SymbolKind::Function
@ Function
clang::clangd::Symbol
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
clang::find_all_symbols::SymbolInfo::operator==
bool operator==(const SymbolInfo &Symbol) const
Definition: SymbolInfo.cpp:76
clang::find_all_symbols::SymbolInfo::SymbolKind
SymbolKind
The SymbolInfo Type.
Definition: SymbolInfo.h:30
clang::find_all_symbols::SymbolInfo::SymbolKind::Macro
@ Macro
clang::find_all_symbols::SymbolAndSignals::Symbol
SymbolInfo Symbol
Definition: SymbolInfo.h:127
clang::clangd::Symbol::Name
llvm::StringRef Name
The unqualified name of the symbol, e.g. "bar" (for ns::bar).
Definition: Symbol.h:42
clang::find_all_symbols::SymbolInfo::ContextType::Namespace
@ Namespace
Name
Token Name
Definition: MacroToEnumCheck.cpp:89
clang::find_all_symbols::WriteSymbolInfosToStream
bool WriteSymbolInfosToStream(llvm::raw_ostream &OS, const SymbolInfo::SignalMap &Symbols)
Write SymbolInfos to a stream (YAML format).
Definition: SymbolInfo.cpp:116
Output
std::string Output
Definition: TraceTests.cpp:159
clang::find_all_symbols::ReadSymbolInfosFromYAML
std::vector< SymbolAndSignals > ReadSymbolInfosFromYAML(llvm::StringRef Yaml)
Read SymbolInfos from a YAML document.
Definition: SymbolInfo.cpp:126
SymbolKind
clang::find_all_symbols::SymbolInfo::SymbolKind SymbolKind
Definition: SymbolInfo.cpp:19
clang::find_all_symbols::SymbolInfo::Signals::operator+
Signals operator+(const Signals &RHS) const
Definition: SymbolInfo.cpp:102
llvm::yaml::ScalarEnumerationTraits< ContextType >::enumeration
static void enumeration(IO &io, ContextType &value)
Definition: SymbolInfo.cpp:38
SymbolInfo.h
clang::find_all_symbols::SymbolInfo::Signals
Definition: SymbolInfo.h:53
clang::find_all_symbols::SymbolInfo::Signals::operator==
bool operator==(const Signals &RHS) const
Definition: SymbolInfo.cpp:108
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
OS
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:160
clang::find_all_symbols::SymbolInfo::SymbolKind::EnumDecl
@ EnumDecl
clang::find_all_symbols::SymbolInfo::SymbolKind::Unknown
@ Unknown
SymbolInfo
clang::find_all_symbols::SymbolInfo SymbolInfo
Definition: FindAllSymbolsMain.cpp:38
clang::find_all_symbols::SymbolInfo::Context
std::pair< ContextType, std::string > Context
A pair of <ContextType, ContextName>.
Definition: SymbolInfo.h:49
clang::find_all_symbols::SymbolInfo::SignalMap
std::map< SymbolInfo, Signals > SignalMap
Definition: SymbolInfo.h:68
clang::find_all_symbols::SymbolInfo::ContextType::EnumDecl
@ EnumDecl
clang::find_all_symbols::SymbolInfo::SymbolInfo
SymbolInfo()
Definition: SymbolInfo.h:72
clang::clangd::Symbol::Type
llvm::StringRef Type
Raw representation of the OpaqueType of the symbol, used for scoring purposes.
Definition: Symbol.h:85
yaml
@ yaml
Definition: ClangDocMain.cpp:92