clang-tools 20.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
15using llvm::yaml::MappingTraits;
20
21LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(SymbolAndSignals)
22LLVM_YAML_IS_SEQUENCE_VECTOR(SymbolInfo::Context)
23
24namespace llvm {
25namespace yaml {
26template <> 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
37template <> 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
45template <> 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
58template <> 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
68namespace clang {
69namespace find_all_symbols {
70
72 llvm::StringRef FilePath,
73 const std::vector<Context> &Contexts)
74 : Name(Name), Type(Type), FilePath(FilePath), Contexts(Contexts) {}
75
76bool 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
81bool 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
86std::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
96SymbolInfo::Signals &SymbolInfo::Signals::operator+=(const Signals &RHS) {
97 Seen += RHS.Seen;
98 Used += RHS.Used;
99 return *this;
100}
101
102SymbolInfo::Signals SymbolInfo::Signals::operator+(const Signals &RHS) const {
103 Signals Result = *this;
104 Result += RHS;
105 return Result;
106}
107
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
116bool 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
126std::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
llvm::SmallString< 256U > Name
@ yaml
clang::find_all_symbols::SymbolInfo SymbolInfo
NodeType Type
clang::find_all_symbols::SymbolInfo::SymbolKind SymbolKind
Definition: SymbolInfo.cpp:19
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:160
Describes a named symbol from a header.
Definition: SymbolInfo.h:26
bool operator==(const SymbolInfo &Symbol) const
Definition: SymbolInfo.cpp:76
std::pair< ContextType, std::string > Context
A pair of <ContextType, ContextName>.
Definition: SymbolInfo.h:48
SymbolKind
The SymbolInfo Type.
Definition: SymbolInfo.h:29
std::string getQualifiedName() const
Get the fully-qualified symbol name.
Definition: SymbolInfo.cpp:86
bool operator<(const SymbolInfo &Symbol) const
Definition: SymbolInfo.cpp:81
std::map< SymbolInfo, Signals > SignalMap
Definition: SymbolInfo.h:67
bool WriteSymbolInfosToStream(llvm::raw_ostream &OS, const SymbolInfo::SignalMap &Symbols)
Write SymbolInfos to a stream (YAML format).
Definition: SymbolInfo.cpp:116
std::vector< SymbolAndSignals > ReadSymbolInfosFromYAML(llvm::StringRef Yaml)
Read SymbolInfos from a YAML document.
Definition: SymbolInfo.cpp:126
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Some operations such as code completion produce a set of candidates.
The class presents a C++ symbol, e.g.
Definition: Symbol.h:39
llvm::StringRef Type
Raw representation of the OpaqueType of the symbol, used for scoring purposes.
Definition: Symbol.h:88
llvm::StringRef Name
The unqualified name of the symbol, e.g. "bar" (for ns::bar).
Definition: Symbol.h:45
bool operator==(const SymbolAndSignals &RHS) const
Definition: SymbolInfo.cpp:112
static void mapping(IO &io, SymbolAndSignals &Symbol)
Definition: SymbolInfo.cpp:27
static void mapping(IO &io, SymbolInfo::Context &Context)
Definition: SymbolInfo.cpp:59
static void enumeration(IO &io, ContextType &value)
Definition: SymbolInfo.cpp:38
static void enumeration(IO &io, SymbolKind &value)
Definition: SymbolInfo.cpp:46