clang-tools  7.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,
36  EnumDecl,
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.
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 
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
llvm::StringRef Name
bool operator<(const SymbolInfo &Symbol) const
Definition: SymbolInfo.cpp:84
Signals & operator+=(const Signals &RHS)
Definition: SymbolInfo.cpp:99
bool WriteSymbolInfosToStream(llvm::raw_ostream &OS, const SymbolInfo::SignalMap &Symbols)
Write SymbolInfos to a stream (YAML format).
Definition: SymbolInfo.cpp:119
std::vector< HeaderHandle > Path
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
const std::vector< SymbolInfo::Context > & getContexts() const
Get symbol contexts.
Definition: SymbolInfo.h:93
void SetFilePath(llvm::StringRef Path)
Definition: SymbolInfo.h:78
SymbolKind
The SymbolInfo Type.
Definition: SymbolInfo.h:31
std::pair< ContextType, std::string > Context
A pair of <ContextType, ContextName>.
Definition: SymbolInfo.h:50
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Signals(unsigned Seen, unsigned Used)
Definition: SymbolInfo.h:56
llvm::StringRef getFilePath() const
Get a relative file path where symbol comes from.
Definition: SymbolInfo.h:90
llvm::StringRef getName() const
Get symbol name.
Definition: SymbolInfo.h:81
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
SymbolKind getSymbolKind() const
Get symbol type.
Definition: SymbolInfo.h:87
bool operator==(const Signals &RHS) const
Definition: SymbolInfo.cpp:111