15#include "clang/AST/Decl.h"
22 PathRef OriginalFile, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
23 llvm::StringRef SourceExtensions[] = {
".cpp",
".c",
".cc",
".cxx",
25 llvm::StringRef HeaderExtensions[] = {
".h",
".hh",
".hpp",
".hxx",
".inc"};
27 llvm::StringRef PathExt = llvm::sys::path::extension(OriginalFile);
30 bool IsSource = llvm::any_of(SourceExtensions, [&PathExt](
PathRef SourceExt) {
31 return SourceExt.equals_insensitive(PathExt);
34 bool IsHeader = llvm::any_of(HeaderExtensions, [&PathExt](
PathRef HeaderExt) {
35 return HeaderExt.equals_insensitive(PathExt);
39 if (!IsSource && !IsHeader)
44 llvm::ArrayRef<llvm::StringRef> NewExts;
46 NewExts = HeaderExtensions;
48 NewExts = SourceExtensions;
51 llvm::SmallString<128> NewPath = OriginalFile;
54 for (llvm::StringRef NewExt : NewExts) {
55 llvm::sys::path::replace_extension(NewPath, NewExt);
56 if (VFS->exists(NewPath))
60 llvm::sys::path::replace_extension(NewPath, NewExt.upper());
61 if (VFS->exists(NewPath))
78 Request.
IDs.insert(
ID);
80 llvm::StringMap<int> Candidates;
81 auto AwardTarget = [&](
const char *TargetURI) {
82 if (
auto TargetPath =
URI::resolve(TargetURI, OriginalFile)) {
83 if (!
pathEqual(*TargetPath, OriginalFile))
84 ++Candidates[*TargetPath];
86 elog(
"Failed to resolve URI {0}: {1}", TargetURI, TargetPath.takeError());
96 Index->lookup(Request, [&](
const Symbol &Sym) {
105 if (Candidates.empty())
110 auto Best = Candidates.begin();
111 for (
auto It = Candidates.begin(); It != Candidates.end(); ++It) {
112 if (It->second > Best->second)
114 else if (It->second == Best->second && It->first() < Best->first())
119 return Path(Best->first());
123 std::vector<const Decl *>
Results;
124 std::function<void(
Decl *)> TraverseDecl = [&](
Decl *D) {
125 auto *ND = llvm::dyn_cast<NamedDecl>(D);
126 if (!ND || ND->isImplicit())
131 if (!llvm::isa<FunctionDecl>(ND)) {
134 if (
auto *Scope = llvm::dyn_cast<DeclContext>(ND)) {
135 for (
auto *D : Scope->decls())
139 if (llvm::isa<NamespaceDecl>(D))
145 for (
auto *TopLevel :
AST.getLocalTopLevelDecls())
146 TraverseDecl(TopLevel);
const FunctionDecl * Decl
std::vector< CodeCompletionResult > Results
Stores and provides access to parsed AST.
static bool shouldCollectSymbol(const NamedDecl &ND, const ASTContext &ASTCtx, const Options &Opts, bool IsMainFileSymbol)
Returns true is ND should be collected.
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
static llvm::Expected< std::string > resolve(const URI &U, llvm::StringRef HintPath="")
Resolves the absolute path of U.
SymbolID getSymbolID(const Decl *D)
Gets the symbol ID for a declaration. Returned SymbolID might be null.
bool pathEqual(PathRef A, PathRef B)
std::vector< const Decl * > getIndexableLocalDecls(ParsedAST &AST)
Returns all indexable decls that are present in the main file of the AST.
std::string Path
A typedef to represent a file path.
std::optional< Path > getCorrespondingHeaderOrSource(PathRef OriginalFile, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS)
Given a header file, returns the best matching source file, and vice visa.
llvm::StringRef PathRef
A typedef to represent a ref to file path.
void elog(const char *Fmt, Ts &&... Vals)
bool isHeaderFile(llvm::StringRef FileName, std::optional< LangOptions > LangOpts)
Infers whether this is a header from the FileName and LangOpts (if presents).
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::DenseSet< SymbolID > IDs
The class presents a C++ symbol, e.g.
SymbolLocation Definition
The location of the symbol's definition, if one was found.
SymbolLocation CanonicalDeclaration
The location of the preferred declaration of the symbol.