clang-tools  7.0.0
SymbolCollector.h
Go to the documentation of this file.
1 //===--- SymbolCollector.h ---------------------------------------*- 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 "CanonicalIncludes.h"
11 #include "Index.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/AST/Decl.h"
14 #include "clang/Index/IndexDataConsumer.h"
15 #include "clang/Index/IndexSymbol.h"
16 #include "clang/Sema/CodeCompleteConsumer.h"
17 
18 namespace clang {
19 namespace clangd {
20 
21 /// \brief Collect declarations (symbols) from an AST.
22 /// It collects most declarations except:
23 /// - Implicit declarations
24 /// - Anonymous declarations (anonymous enum/class/struct, etc)
25 /// - Declarations in anonymous namespaces
26 /// - Local declarations (in function bodies, blocks, etc)
27 /// - Declarations in main files
28 /// - Template specializations
29 /// - Library-specific private declarations (e.g. private declaration generated
30 /// by protobuf compiler)
31 ///
32 /// See also shouldCollectSymbol(...).
33 ///
34 /// Clients (e.g. clangd) can use SymbolCollector together with
35 /// index::indexTopLevelDecls to retrieve all symbols when the source file is
36 /// changed.
37 class SymbolCollector : public index::IndexDataConsumer {
38 public:
39  struct Options {
40  /// When symbol paths cannot be resolved to absolute paths (e.g. files in
41  /// VFS that does not have absolute path), combine the fallback directory
42  /// with symbols' paths to get absolute paths. This must be an absolute
43  /// path.
44  std::string FallbackDir;
45  /// Specifies URI schemes that can be used to generate URIs for file paths
46  /// in symbols. The list of schemes will be tried in order until a working
47  /// scheme is found. If no scheme works, symbol location will be dropped.
48  std::vector<std::string> URISchemes = {"file"};
49  bool CollectIncludePath = false;
50  /// If set, this is used to map symbol #include path to a potentially
51  /// different #include path.
52  const CanonicalIncludes *Includes = nullptr;
53  // Populate the Symbol.References field.
54  bool CountReferences = false;
55  // Every symbol collected will be stamped with this origin.
57  /// Collect macros.
58  /// Note that SymbolCollector must be run with preprocessor in order to
59  /// collect macros. For example, `indexTopLevelDecls` will not index any
60  /// macro even if this is true.
61  bool CollectMacro = false;
62  };
63 
65 
66  /// Returns true is \p ND should be collected.
67  /// AST matchers require non-const ASTContext.
68  static bool shouldCollectSymbol(const NamedDecl &ND, ASTContext &ASTCtx,
69  const Options &Opts);
70 
71  void initialize(ASTContext &Ctx) override;
72 
73  void setPreprocessor(std::shared_ptr<Preprocessor> PP) override {
74  this->PP = std::move(PP);
75  }
76 
77  bool
78  handleDeclOccurence(const Decl *D, index::SymbolRoleSet Roles,
79  ArrayRef<index::SymbolRelation> Relations,
80  SourceLocation Loc,
81  index::IndexDataConsumer::ASTNodeInfo ASTNode) override;
82 
83  bool handleMacroOccurence(const IdentifierInfo *Name, const MacroInfo *MI,
84  index::SymbolRoleSet Roles,
85  SourceLocation Loc) override;
86 
87  SymbolSlab takeSymbols() { return std::move(Symbols).build(); }
88 
89  void finish() override;
90 
91 private:
92  const Symbol *addDeclaration(const NamedDecl &, SymbolID);
93  void addDefinition(const NamedDecl &, const Symbol &DeclSymbol);
94 
95  // All Symbols collected from the AST.
96  SymbolSlab::Builder Symbols;
97  ASTContext *ASTCtx;
98  std::shared_ptr<Preprocessor> PP;
99  std::shared_ptr<GlobalCodeCompletionAllocator> CompletionAllocator;
100  std::unique_ptr<CodeCompletionTUInfo> CompletionTUInfo;
101  Options Opts;
102  // Symbols referenced from the current TU, flushed on finish().
103  llvm::DenseSet<const NamedDecl *> ReferencedDecls;
104  llvm::DenseSet<const IdentifierInfo *> ReferencedMacros;
105  // Maps canonical declaration provided by clang to canonical declaration for
106  // an index symbol, if clangd prefers a different declaration than that
107  // provided by clang. For example, friend declaration might be considered
108  // canonical by clang but should not be considered canonical in the index
109  // unless it's a definition.
110  llvm::DenseMap<const Decl *, const Decl *> CanonicalDecls;
111 };
112 
113 } // namespace clangd
114 } // namespace clang
SourceLocation Loc
&#39;#&#39; location in the include directive
llvm::StringRef Name
bool handleMacroOccurence(const IdentifierInfo *Name, const MacroInfo *MI, index::SymbolRoleSet Roles, SourceLocation Loc) override
std::vector< std::string > URISchemes
Specifies URI schemes that can be used to generate URIs for file paths in symbols.
Collect declarations (symbols) from an AST.
void initialize(ASTContext &Ctx) override
Maps a definition location onto an #include file, based on a set of filename rules.
std::string FallbackDir
When symbol paths cannot be resolved to absolute paths (e.g.
bool handleDeclOccurence(const Decl *D, index::SymbolRoleSet Roles, ArrayRef< index::SymbolRelation > Relations, SourceLocation Loc, index::IndexDataConsumer::ASTNodeInfo ASTNode) override
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
void setPreprocessor(std::shared_ptr< Preprocessor > PP) override
static bool shouldCollectSymbol(const NamedDecl &ND, ASTContext &ASTCtx, const Options &Opts)
Returns true is ND should be collected.
tooling::ExecutionContext * Ctx
const CanonicalIncludes * Includes
If set, this is used to map symbol #include path to a potentially different #include path...