LCOV - code coverage report
Current view: top level - clang/tools/extra/clangd/index - SymbolCollector.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 0 4 0.0 %
Date: 2018-09-23 13:06:45 Functions: 0 1 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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             :     /// The symbol ref kinds that will be collected.
      56             :     /// If not set, SymbolCollector will not collect refs.
      57             :     RefKind RefFilter = RefKind::Unknown;
      58             :     // Every symbol collected will be stamped with this origin.
      59             :     SymbolOrigin Origin = SymbolOrigin::Unknown;
      60             :     /// Collect macros.
      61             :     /// Note that SymbolCollector must be run with preprocessor in order to
      62             :     /// collect macros. For example, `indexTopLevelDecls` will not index any
      63             :     /// macro even if this is true.
      64             :     bool CollectMacro = false;
      65             :   };
      66             : 
      67             :   SymbolCollector(Options Opts);
      68             : 
      69             :   /// Returns true is \p ND should be collected.
      70             :   /// AST matchers require non-const ASTContext.
      71             :   static bool shouldCollectSymbol(const NamedDecl &ND, ASTContext &ASTCtx,
      72             :                                   const Options &Opts);
      73             : 
      74             :   void initialize(ASTContext &Ctx) override;
      75             : 
      76           0 :   void setPreprocessor(std::shared_ptr<Preprocessor> PP) override {
      77             :     this->PP = std::move(PP);
      78           0 :   }
      79             : 
      80             :   bool
      81             :   handleDeclOccurence(const Decl *D, index::SymbolRoleSet Roles,
      82             :                       ArrayRef<index::SymbolRelation> Relations,
      83             :                       SourceLocation Loc,
      84             :                       index::IndexDataConsumer::ASTNodeInfo ASTNode) override;
      85             : 
      86             :   bool handleMacroOccurence(const IdentifierInfo *Name, const MacroInfo *MI,
      87             :                             index::SymbolRoleSet Roles,
      88             :                             SourceLocation Loc) override;
      89             : 
      90           0 :   SymbolSlab takeSymbols() { return std::move(Symbols).build(); }
      91           0 :   RefSlab takeRefs() { return std::move(Refs).build(); }
      92             : 
      93             :   void finish() override;
      94             : 
      95             : private:
      96             :   const Symbol *addDeclaration(const NamedDecl &, SymbolID);
      97             :   void addDefinition(const NamedDecl &, const Symbol &DeclSymbol);
      98             : 
      99             :   // All Symbols collected from the AST.
     100             :   SymbolSlab::Builder Symbols;
     101             :   // All refs collected from the AST.
     102             :   // Only symbols declared in preamble (from #include) and referenced from the
     103             :   // main file will be included.
     104             :   RefSlab::Builder Refs;
     105             :   ASTContext *ASTCtx;
     106             :   std::shared_ptr<Preprocessor> PP;
     107             :   std::shared_ptr<GlobalCodeCompletionAllocator> CompletionAllocator;
     108             :   std::unique_ptr<CodeCompletionTUInfo> CompletionTUInfo;
     109             :   Options Opts;
     110             :   using DeclRef = std::pair<SourceLocation, index::SymbolRoleSet>;
     111             :   // Symbols referenced from the current TU, flushed on finish().
     112             :   llvm::DenseSet<const NamedDecl *> ReferencedDecls;
     113             :   llvm::DenseSet<const IdentifierInfo *> ReferencedMacros;
     114             :   llvm::DenseMap<const NamedDecl *, std::vector<DeclRef>> DeclRefs;
     115             :   // Maps canonical declaration provided by clang to canonical declaration for
     116             :   // an index symbol, if clangd prefers a different declaration than that
     117             :   // provided by clang. For example, friend declaration might be considered
     118             :   // canonical by clang but should not be considered canonical in the index
     119             :   // unless it's a definition.
     120             :   llvm::DenseMap<const Decl *, const Decl *> CanonicalDecls;
     121             : };
     122             : 
     123             : } // namespace clangd
     124             : } // namespace clang

Generated by: LCOV version 1.13