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;
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 
100  Seen += RHS.Seen;
101  Used += RHS.Used;
102  return *this;
103 }
104 
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 
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
static void enumeration(IO &io, SymbolKind &value)
Definition: SymbolInfo.cpp:49
Some operations such as code completion produce a set of candidates.
bool operator<(const SymbolInfo &Symbol) const
Definition: SymbolInfo.cpp:84
Signals & operator+=(const Signals &RHS)
Definition: SymbolInfo.cpp:99
static void enumeration(IO &io, ContextType &value)
Definition: SymbolInfo.cpp:41
bool WriteSymbolInfosToStream(llvm::raw_ostream &OS, const SymbolInfo::SignalMap &Symbols)
Write SymbolInfos to a stream (YAML format).
Definition: SymbolInfo.cpp:119
std::vector< SymbolAndSignals > ReadSymbolInfosFromYAML(llvm::StringRef Yaml)
Read SymbolInfos from a YAML document.
Definition: SymbolInfo.cpp:129
std::string getQualifiedName() const
Get the fully-qualified symbol name.
Definition: SymbolInfo.cpp:89
clang::find_all_symbols::SymbolInfo SymbolInfo
static constexpr llvm::StringLiteral Name
SymbolKind
The SymbolInfo Type.
Definition: SymbolInfo.h:31
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
static void mapping(IO &io, SymbolInfo::Context &Context)
Definition: SymbolInfo.cpp:62
bool operator==(const SymbolAndSignals &RHS) const
Definition: SymbolInfo.cpp:115
bool operator==(const SymbolInfo &Symbol) const
Definition: SymbolInfo.cpp:79
std::pair< ContextType, std::string > Context
A pair of <ContextType, ContextName>.
Definition: SymbolInfo.h:50
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::map< SymbolInfo, Signals > SignalMap
Definition: SymbolInfo.h:69
Signals operator+(const Signals &RHS) const
Definition: SymbolInfo.cpp:105
Describes a named symbol from a header.
Definition: SymbolInfo.h:28
static void mapping(IO &io, SymbolAndSignals &Symbol)
Definition: SymbolInfo.cpp:30
bool operator==(const Signals &RHS) const
Definition: SymbolInfo.cpp:111