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