clang-tools 8.0.0

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