clang-tools 8.0.0

SymbolInfo.h
Go to the documentation of this file.
1 //===-- SymbolInfo.h - 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 #ifndef LLVM_CLANG_TOOLS_EXTRA_INCLUDE_FIXER_FIND_ALL_SYMBOLS_SYMBOLINFO_H
11 #define LLVM_CLANG_TOOLS_EXTRA_INCLUDE_FIXER_FIND_ALL_SYMBOLS_SYMBOLINFO_H
12
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/YAMLTraits.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include <set>
18 #include <string>
19 #include <vector>
20
21 namespace clang {
22 namespace find_all_symbols {
23 /// \brief Describes a named symbol from a header.
24 /// Symbols with the same qualified name and type (e.g. function overloads)
25 /// that appear in the same header are represented by a single SymbolInfo.
26 ///
27 /// TODO: keep track of instances, e.g. overload locations and signatures.
28 class SymbolInfo {
29 public:
30 /// \brief The SymbolInfo Type.
31 enum class SymbolKind {
32 Function,
33 Class,
34 Variable,
35 TypedefName,
36 EnumDecl,
37 EnumConstantDecl,
38 Macro,
39 Unknown,
40 };
41
42 /// \brief The Context Type.
43 enum class ContextType {
44 Namespace, // Symbols declared in a namespace.
45 Record, // Symbols declared in a class.
46 EnumDecl, // Enum constants declared in a enum declaration.
47 };
48
49 /// \brief A pair of <ContextType, ContextName>.
50 typedef std::pair<ContextType, std::string> Context;
51
52 // \brief Signals are signals gathered by observing how a symbol is used.
53 // These are used to rank results.
54 struct Signals {
55 Signals() {}
56 Signals(unsigned Seen, unsigned Used) : Seen(Seen), Used(Used) {}
57
58 // Number of times this symbol was visible to a TU.
59 unsigned Seen = 0;
60
61 // Number of times this symbol was referenced a TU's main file.
62 unsigned Used = 0;
63
64 Signals &operator+=(const Signals &RHS);
65 Signals operator+(const Signals &RHS) const;
66 bool operator==(const Signals &RHS) const;
67 };
68
69 using SignalMap = std::map<SymbolInfo, Signals>;
70
71 // The default constructor is required by YAML traits in
72 // LLVM_YAML_IS_DOCUMENT_LIST_VECTOR.
73 SymbolInfo() : Type(SymbolKind::Unknown) {}
74
75 SymbolInfo(llvm::StringRef Name, SymbolKind Type, llvm::StringRef FilePath,
76 const std::vector<Context> &Contexts);
77
78 void SetFilePath(llvm::StringRef Path) { FilePath = Path; }
79
80 /// \brief Get symbol name.
81 llvm::StringRef getName() const { return Name; }
82
83 /// \brief Get the fully-qualified symbol name.
84 std::string getQualifiedName() const;
85
86 /// \brief Get symbol type.
87 SymbolKind getSymbolKind() const { return Type; }
88
89 /// \brief Get a relative file path where symbol comes from.
90 llvm::StringRef getFilePath() const { return FilePath; }
91
92 /// \brief Get symbol contexts.
93 const std::vector<SymbolInfo::Context> &getContexts() const {
94 return Contexts;
95 }
96
97 bool operator<(const SymbolInfo &Symbol) const;
98
99 bool operator==(const SymbolInfo &Symbol) const;
100
101 private:
102 friend struct llvm::yaml::MappingTraits<struct SymbolAndSignals>;
103
104 /// \brief Identifier name.
105 std::string Name;
106
107 /// \brief Symbol type.
108 SymbolKind Type;
109
110 /// \brief The file path where the symbol comes from. It's a relative file
111 /// path based on the build directory.
112 std::string FilePath;
113
114 /// \brief Contains information about symbol contexts. Context information is
115 /// stored from the inner-most level to outer-most level.
116 ///
117 /// For example, if a symbol 'x' is declared as:
118 /// namespace na { namespace nb { class A { int x; } } }
119 /// The contexts would be { {RECORD, "A"}, {NAMESPACE, "nb"}, {NAMESPACE,
120 /// "na"} }.
121 /// The name of an anonymous namespace is "".
122 ///
123 /// If the symbol is declared in `TranslationUnitDecl`, it has no context.
124 std::vector<Context> Contexts;
125 };
126
127 struct SymbolAndSignals {
128 SymbolInfo Symbol;
129 SymbolInfo::Signals Signals;
130 bool operator==(const SymbolAndSignals& RHS) const;
131 };
132
133 /// \brief Write SymbolInfos to a stream (YAML format).
134 bool WriteSymbolInfosToStream(llvm::raw_ostream &OS,
135 const SymbolInfo::SignalMap &Symbols);
136
137 /// \brief Read SymbolInfos from a YAML document.
138 std::vector<SymbolAndSignals> ReadSymbolInfosFromYAML(llvm::StringRef Yaml);
139
140 } // namespace find_all_symbols
141 } // namespace clang
142
143 #endif // LLVM_CLANG_TOOLS_EXTRA_INCLUDE_FIXER_FIND_ALL_SYMBOLS_SYMBOLINFO_H
Name
llvm::StringRef Name
Definition: CodeComplete.cpp:206
clang::find_all_symbols::SymbolInfo::SymbolKind::Function
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
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
Path
std::vector< HeaderHandle > Path
Definition: PreprocessorTracker.cpp:538
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
clang::find_all_symbols::SymbolInfo::getContexts
const std::vector< SymbolInfo::Context > & getContexts() const
Get symbol contexts.
Definition: SymbolInfo.h:93
clang::find_all_symbols::SymbolInfo::Signals
Definition: SymbolInfo.h:54
clang::find_all_symbols::SymbolInfo::SetFilePath
void SetFilePath(llvm::StringRef Path)
Definition: SymbolInfo.h:78
clang::find_all_symbols::SymbolAndSignals::Signals
SymbolInfo::Signals Signals
Definition: SymbolInfo.h:129
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
clang::find_all_symbols::SymbolInfo::ContextType::Namespace
clang::find_all_symbols::SymbolInfo::SymbolKind::TypedefName
clang::find_all_symbols::SymbolInfo::SymbolKind::Unknown
clang::find_all_symbols::SymbolAndSignals::Symbol
SymbolInfo Symbol
Definition: SymbolInfo.h:128
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::Signals::Signals
Signals(unsigned Seen, unsigned Used)
Definition: SymbolInfo.h:56
clang::find_all_symbols::SymbolInfo::getFilePath
llvm::StringRef getFilePath() const
Get a relative file path where symbol comes from.
Definition: SymbolInfo.h:90
clang::find_all_symbols::SymbolInfo::ContextType::Record
clang::find_all_symbols::SymbolInfo::getName
llvm::StringRef getName() const
Get symbol name.
Definition: SymbolInfo.h:81
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
clang::find_all_symbols::SymbolInfo::getSymbolKind
SymbolKind getSymbolKind() const
Get symbol type.
Definition: SymbolInfo.h:87
clang::find_all_symbols::SymbolInfo::Signals::operator==
bool operator==(const Signals &RHS) const
Definition: SymbolInfo.cpp:111