clang-tools  14.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;
16 using llvm::yaml::IO;
17 using llvm::yaml::Input;
22 
23 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(SymbolAndSignals)
24 LLVM_YAML_IS_SEQUENCE_VECTOR(SymbolInfo::Context)
25 
26 namespace llvm {
27 namespace yaml {
28 template <> struct MappingTraits<SymbolAndSignals> {
29  static void mapping(IO &io, SymbolAndSignals &Symbol) {
30  io.mapRequired("Name", Symbol.Symbol.Name);
31  io.mapRequired("Contexts", Symbol.Symbol.Contexts);
32  io.mapRequired("FilePath", Symbol.Symbol.FilePath);
33  io.mapRequired("Type", Symbol.Symbol.Type);
34  io.mapRequired("Seen", Symbol.Signals.Seen);
35  io.mapRequired("Used", Symbol.Signals.Used);
36  }
37 };
38 
39 template <> struct ScalarEnumerationTraits<ContextType> {
40  static void enumeration(IO &io, ContextType &value) {
41  io.enumCase(value, "Record", ContextType::Record);
42  io.enumCase(value, "Namespace", ContextType::Namespace);
43  io.enumCase(value, "EnumDecl", ContextType::EnumDecl);
44  }
45 };
46 
47 template <> struct ScalarEnumerationTraits<SymbolKind> {
48  static void enumeration(IO &io, SymbolKind &value) {
49  io.enumCase(value, "Variable", SymbolKind::Variable);
50  io.enumCase(value, "Function", SymbolKind::Function);
51  io.enumCase(value, "Class", SymbolKind::Class);
52  io.enumCase(value, "TypedefName", SymbolKind::TypedefName);
53  io.enumCase(value, "EnumDecl", SymbolKind::EnumDecl);
54  io.enumCase(value, "EnumConstantDecl", SymbolKind::EnumConstantDecl);
55  io.enumCase(value, "Macro", SymbolKind::Macro);
56  io.enumCase(value, "Unknown", SymbolKind::Unknown);
57  }
58 };
59 
60 template <> struct MappingTraits<SymbolInfo::Context> {
61  static void mapping(IO &io, SymbolInfo::Context &Context) {
62  io.mapRequired("ContextType", Context.first);
63  io.mapRequired("ContextName", Context.second);
64  }
65 };
66 
67 } // namespace yaml
68 } // namespace llvm
69 
70 namespace clang {
71 namespace find_all_symbols {
72 
73 SymbolInfo::SymbolInfo(llvm::StringRef Name, SymbolKind Type,
74  llvm::StringRef FilePath,
75  const std::vector<Context> &Contexts)
76  : Name(Name), Type(Type), FilePath(FilePath), Contexts(Contexts) {}
77 
78 bool SymbolInfo::operator==(const SymbolInfo &Symbol) const {
79  return std::tie(Name, Type, FilePath, Contexts) ==
80  std::tie(Symbol.Name, Symbol.Type, Symbol.FilePath, Symbol.Contexts);
81 }
82 
83 bool SymbolInfo::operator<(const SymbolInfo &Symbol) const {
84  return std::tie(Name, Type, FilePath, Contexts) <
85  std::tie(Symbol.Name, Symbol.Type, Symbol.FilePath, Symbol.Contexts);
86 }
87 
88 std::string SymbolInfo::getQualifiedName() const {
89  std::string QualifiedName = Name;
90  for (const auto &Context : Contexts) {
91  if (Context.first == ContextType::EnumDecl)
92  continue;
93  QualifiedName = Context.second + "::" + QualifiedName;
94  }
95  return QualifiedName;
96 }
97 
99  Seen += RHS.Seen;
100  Used += RHS.Used;
101  return *this;
102 }
103 
105  Signals Result = *this;
106  Result += RHS;
107  return Result;
108 }
109 
110 bool SymbolInfo::Signals::operator==(const Signals &RHS) const {
111  return std::tie(Seen, Used) == std::tie(RHS.Seen, RHS.Used);
112 }
113 
115  return std::tie(Symbol, Signals) == std::tie(RHS.Symbol, RHS.Signals);
116 }
117 
118 bool WriteSymbolInfosToStream(llvm::raw_ostream &OS,
119  const SymbolInfo::SignalMap &Symbols) {
120  llvm::yaml::Output yout(OS);
121  for (const auto &Symbol : Symbols) {
122  SymbolAndSignals S{Symbol.first, Symbol.second};
123  yout << S;
124  }
125  return true;
126 }
127 
128 std::vector<SymbolAndSignals> ReadSymbolInfosFromYAML(llvm::StringRef Yaml) {
129  std::vector<SymbolAndSignals> Symbols;
130  llvm::yaml::Input yin(Yaml);
131  yin >> Symbols;
132  return Symbols;
133 }
134 
135 } // namespace find_all_symbols
136 } // 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:61
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:28
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:29
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:83
clang::find_all_symbols::SymbolInfo::Signals::operator+=
Signals & operator+=(const Signals &RHS)
Definition: SymbolInfo.cpp:98
llvm::yaml::ScalarEnumerationTraits< SymbolKind >::enumeration
static void enumeration(IO &io, SymbolKind &value)
Definition: SymbolInfo.cpp:48
clang::find_all_symbols::SymbolAndSignals::operator==
bool operator==(const SymbolAndSignals &RHS) const
Definition: SymbolInfo.cpp:114
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:88
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:78
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
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
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:118
Output
std::string Output
Definition: TraceTests.cpp:162
clang::find_all_symbols::ReadSymbolInfosFromYAML
std::vector< SymbolAndSignals > ReadSymbolInfosFromYAML(llvm::StringRef Yaml)
Read SymbolInfos from a YAML document.
Definition: SymbolInfo.cpp:128
SymbolKind
clang::find_all_symbols::SymbolInfo::SymbolKind SymbolKind
Definition: SymbolInfo.cpp:21
clang::find_all_symbols::SymbolInfo::Signals::operator+
Signals operator+(const Signals &RHS) const
Definition: SymbolInfo.cpp:104
llvm::yaml::ScalarEnumerationTraits< ContextType >::enumeration
static void enumeration(IO &io, ContextType &value)
Definition: SymbolInfo.cpp:40
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:110
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
OS
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:163
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