12 #include "clang/Index/IndexingAction.h" 13 #include "clang/Lex/Preprocessor.h" 19 llvm::ArrayRef<std::string> URISchemes) {
28 if (!URISchemes.empty())
34 index::IndexingOptions IndexOpts;
36 IndexOpts.SystemSymbolFilter =
37 index::IndexingOptions::SystemSymbolFilterKind::DeclarationsOnly;
38 IndexOpts.IndexFunctionLocals =
false;
40 std::vector<const Decl *> TopLevelDecls(
41 AST.getTranslationUnitDecl()->decls().begin(),
42 AST.getTranslationUnitDecl()->decls().end());
43 index::indexTopLevelDecls(AST, TopLevelDecls, Collector, IndexOpts);
45 return Collector.takeSymbols();
49 : URISchemes(std::move(URISchemes)) {}
52 std::lock_guard<std::mutex> Lock(Mutex);
54 FileToSlabs.erase(Path);
56 FileToSlabs[
Path] = std::move(Slab);
63 std::vector<const Symbol *> Pointers;
64 std::vector<std::shared_ptr<SymbolSlab>> KeepAlive;
66 auto Snap = std::make_shared<Snapshot>();
68 std::lock_guard<std::mutex> Lock(Mutex);
70 for (
const auto &FileAndSlab : FileToSlabs) {
71 Snap->KeepAlive.push_back(FileAndSlab.second);
72 for (
const auto &Iter : *FileAndSlab.second)
73 Snap->Pointers.push_back(&Iter);
76 auto *Pointers = &Snap->Pointers;
79 return {std::move(Snap), Pointers};
83 std::shared_ptr<Preprocessor> PP) {
85 FSymbols.update(Path,
nullptr);
88 auto Slab = llvm::make_unique<SymbolSlab>();
89 *Slab =
indexAST(*AST, PP, URISchemes);
90 FSymbols.update(Path, std::move(Slab));
92 auto Symbols = FSymbols.allSymbols();
93 Index.build(std::move(Symbols));
99 return Index.fuzzyFind(Req,
Callback);
SymbolSlab indexAST(ASTContext &AST, std::shared_ptr< Preprocessor > PP, llvm::ArrayRef< std::string > URISchemes)
Retrieves namespace and class level symbols in AST.
FileIndex(std::vector< std::string > URISchemes={})
If URISchemes is empty, the default schemes in SymbolCollector will be used.
std::vector< std::string > URISchemes
Specifies URI schemes that can be used to generate URIs for file paths in symbols.
std::shared_ptr< std::vector< const Symbol * > > allSymbols()
void update(PathRef Path, std::unique_ptr< SymbolSlab > Slab)
Updates all symbols in a file.
Collect declarations (symbols) from an AST.
llvm::StringRef PathRef
A typedef to represent a ref to file path.
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
std::string Path
A typedef to represent a file path.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
void setPreprocessor(std::shared_ptr< Preprocessor > PP) override
void update(PathRef Path, ASTContext *AST, std::shared_ptr< Preprocessor > PP)
Update symbols in Path with symbols in AST.
void lookup(const LookupRequest &Req, llvm::function_ref< void(const Symbol &)> Callback) const override
Looks up symbols with any of the given symbol IDs and applies Callback on each matched symbol...
bool fuzzyFind(const FuzzyFindRequest &Req, llvm::function_ref< void(const Symbol &)> Callback) const override
Matches symbols in the index fuzzily and applies Callback on each matched symbol before returning...