1 //===-- SymbolInfo.h - Symbol Info ------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/YAMLTraits.h"
15 #include "llvm/Support/raw_ostream.h"
21 namespace find_all_symbols
{
22 /// Describes a named symbol from a header.
23 /// Symbols with the same qualified name and type (e.g. function overloads)
24 /// that appear in the same header are represented by a single SymbolInfo.
26 /// TODO: keep track of instances, e.g. overload locations and signatures.
29 /// The SymbolInfo Type.
30 enum class SymbolKind
{
42 enum class ContextType
{
43 Namespace
, // Symbols declared in a namespace.
44 Record
, // Symbols declared in a class.
45 EnumDecl
, // Enum constants declared in a enum declaration.
48 /// A pair of <ContextType, ContextName>.
49 typedef std::pair
<ContextType
, std::string
> Context
;
51 // Signals are signals gathered by observing how a symbol is used.
52 // These are used to rank results.
55 Signals(unsigned Seen
, unsigned Used
) : Seen(Seen
), Used(Used
) {}
57 // Number of times this symbol was visible to a TU.
60 // Number of times this symbol was referenced a TU's main file.
63 Signals
&operator+=(const Signals
&RHS
);
64 Signals
operator+(const Signals
&RHS
) const;
65 bool operator==(const Signals
&RHS
) const;
68 using SignalMap
= std::map
<SymbolInfo
, Signals
>;
70 // The default constructor is required by YAML traits in
71 // LLVM_YAML_IS_DOCUMENT_LIST_VECTOR.
72 SymbolInfo() : Type(SymbolKind::Unknown
) {}
74 SymbolInfo(llvm::StringRef Name
, SymbolKind Type
, llvm::StringRef FilePath
,
75 const std::vector
<Context
> &Contexts
);
77 void SetFilePath(llvm::StringRef Path
) { FilePath
= std::string(Path
); }
80 llvm::StringRef
getName() const { return Name
; }
82 /// Get the fully-qualified symbol name.
83 std::string
getQualifiedName() const;
86 SymbolKind
getSymbolKind() const { return Type
; }
88 /// Get a relative file path where symbol comes from.
89 llvm::StringRef
getFilePath() const { return FilePath
; }
91 /// Get symbol contexts.
92 const std::vector
<SymbolInfo::Context
> &getContexts() const {
96 bool operator<(const SymbolInfo
&Symbol
) const;
98 bool operator==(const SymbolInfo
&Symbol
) const;
101 friend struct llvm::yaml::MappingTraits
<struct SymbolAndSignals
>;
109 /// The file path where the symbol comes from. It's a relative file
110 /// path based on the build directory.
111 std::string FilePath
;
113 /// Contains information about symbol contexts. Context information is
114 /// stored from the inner-most level to outer-most level.
116 /// For example, if a symbol 'x' is declared as:
117 /// namespace na { namespace nb { class A { int x; } } }
118 /// The contexts would be { {RECORD, "A"}, {NAMESPACE, "nb"}, {NAMESPACE,
120 /// The name of an anonymous namespace is "".
122 /// If the symbol is declared in `TranslationUnitDecl`, it has no context.
123 std::vector
<Context
> Contexts
;
126 struct SymbolAndSignals
{
128 SymbolInfo::Signals Signals
;
129 bool operator==(const SymbolAndSignals
& RHS
) const;
132 /// Write SymbolInfos to a stream (YAML format).
133 bool WriteSymbolInfosToStream(llvm::raw_ostream
&OS
,
134 const SymbolInfo::SignalMap
&Symbols
);
136 /// Read SymbolInfos from a YAML document.
137 std::vector
<SymbolAndSignals
> ReadSymbolInfosFromYAML(llvm::StringRef Yaml
);
139 } // namespace find_all_symbols
142 #endif // LLVM_CLANG_TOOLS_EXTRA_INCLUDE_FIXER_FIND_ALL_SYMBOLS_SYMBOLINFO_H