14 #include "clang/AST/Decl.h" 20 const Path &OriginalFile,
21 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
VFS) {
22 llvm::StringRef SourceExtensions[] = {
".cpp",
".c",
".cc",
".cxx",
24 llvm::StringRef HeaderExtensions[] = {
".h",
".hh",
".hpp",
".hxx",
".inc"};
26 llvm::StringRef PathExt = llvm::sys::path::extension(OriginalFile);
30 llvm::find_if(SourceExtensions, [&PathExt](
PathRef SourceExt) {
31 return SourceExt.equals_lower(PathExt);
33 bool IsSource = SourceIter != std::end(SourceExtensions);
36 llvm::find_if(HeaderExtensions, [&PathExt](
PathRef HeaderExt) {
37 return HeaderExt.equals_lower(PathExt);
39 bool IsHeader = HeaderIter != std::end(HeaderExtensions);
42 if (!IsSource && !IsHeader)
47 llvm::ArrayRef<llvm::StringRef> NewExts;
49 NewExts = HeaderExtensions;
51 NewExts = SourceExtensions;
54 llvm::SmallString<128> NewPath = llvm::StringRef(OriginalFile);
57 for (llvm::StringRef NewExt : NewExts) {
58 llvm::sys::path::replace_extension(NewPath, NewExt);
59 if (VFS->exists(NewPath))
60 return NewPath.str().str();
65 llvm::sys::path::replace_extension(NewPath, NewExt.upper());
66 if (VFS->exists(NewPath))
67 return NewPath.str().str();
83 Request.
IDs.insert(*ID);
85 llvm::StringMap<int> Candidates;
86 auto AwardTarget = [&](
const char *TargetURI) {
87 if (
auto TargetPath =
URI::resolve(TargetURI, OriginalFile)) {
88 if (*TargetPath != OriginalFile)
89 ++Candidates[*TargetPath];
91 elog(
"Failed to resolve URI {0}: {1}", TargetURI, TargetPath.takeError());
110 if (Candidates.empty())
115 auto Best = Candidates.begin();
116 for (
auto It = Candidates.begin(); It != Candidates.end(); ++It) {
117 if (It->second > Best->second)
119 else if (It->second == Best->second && It->first() < Best->first())
124 return Path(Best->first());
128 std::vector<const Decl *>
Results;
129 std::function<void(Decl *)> TraverseDecl = [&](
Decl *D) {
130 auto *ND = llvm::dyn_cast<NamedDecl>(D);
131 if (!ND || ND->isImplicit())
136 if (!llvm::isa<FunctionDecl>(ND)) {
139 if (
auto *Scope = llvm::dyn_cast<DeclContext>(ND)) {
140 for (
auto *D : Scope->decls())
144 if (llvm::isa<NamespaceDecl>(D))
146 Results.push_back(D);
151 TraverseDecl(TopLevel);
virtual void lookup(const LookupRequest &Req, llvm::function_ref< void(const Symbol &)> Callback) const =0
Looks up symbols with any of the given symbol IDs and applies Callback on each matched symbol...
const FunctionDecl * Decl
const LangOptions & getLangOpts() const
llvm::Optional< SymbolID > getSymbolID(const Decl *D)
Gets the symbol ID for a declaration, if possible.
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
llvm::DenseSet< SymbolID > IDs
llvm::StringRef PathRef
A typedef to represent a ref to file path.
std::vector< CodeCompletionResult > Results
ArrayRef< Decl * > getLocalTopLevelDecls()
This function returns top-level decls present in the main file of the AST.
void elog(const char *Fmt, Ts &&... Vals)
std::vector< const Decl * > getIndexableLocalDecls(ParsedAST &AST)
Returns all indexable decls that are present in the main file of the AST.
SymbolLocation Definition
The location of the symbol's definition, if one was found.
std::string Path
A typedef to represent a file path.
SymbolLocation CanonicalDeclaration
The location of the preferred declaration of the symbol.
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.
The class presents a C++ symbol, e.g.
llvm::Optional< Path > getCorrespondingHeaderOrSource(const Path &OriginalFile, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS)
Given a header file, returns the best matching source file, and vice visa.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
bool isHeaderFile(llvm::StringRef FileName, llvm::Optional< LangOptions > LangOpts)
Infers whether this is a header from the FileName and LangOpts (if presents).
static llvm::Expected< std::string > resolve(const URI &U, llvm::StringRef HintPath="")
Resolves the absolute path of U.
const SymbolIndex * Index