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