15#include "clang/AST/Decl.h"
22 PathRef OriginalFile, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
23 static constexpr llvm::StringRef SourceExtensions[] = {
24 ".cpp",
".c",
".cc",
".cxx",
".c++",
".m",
".mm"};
25 static constexpr llvm::StringRef HeaderExtensions[] = {
26 ".h",
".hh",
".hpp",
".hxx",
".inc",
27 ".cppm",
".ccm",
".cxxm",
".c++m",
".ixx"};
29 llvm::StringRef PathExt = llvm::sys::path::extension(OriginalFile);
33 llvm::any_of(SourceExtensions, [&PathExt](
PathRef SourceExt) {
34 return SourceExt.equals_insensitive(PathExt);
38 llvm::any_of(HeaderExtensions, [&PathExt](
PathRef HeaderExt) {
39 return HeaderExt.equals_insensitive(PathExt);
43 if (!IsSource && !IsHeader)
48 llvm::ArrayRef<llvm::StringRef> NewExts;
50 NewExts = HeaderExtensions;
52 NewExts = SourceExtensions;
55 llvm::SmallString<128> NewPath = OriginalFile;
58 for (llvm::StringRef NewExt : NewExts) {
59 llvm::sys::path::replace_extension(NewPath, NewExt);
60 if (VFS->exists(NewPath))
64 llvm::sys::path::replace_extension(NewPath, NewExt.upper());
65 if (VFS->exists(NewPath))
82 Request.
IDs.insert(ID);
84 llvm::StringMap<int> Candidates;
85 auto AwardTarget = [&](
const char *TargetURI) {
86 if (
auto TargetPath =
URI::resolve(TargetURI, OriginalFile)) {
87 if (!
pathEqual(*TargetPath, OriginalFile))
88 ++Candidates[*TargetPath];
90 elog(
"Failed to resolve URI {0}: {1}", TargetURI, TargetPath.takeError());
109 if (Candidates.empty())
114 auto Best = Candidates.begin();
115 for (
auto It = Candidates.begin(); It != Candidates.end(); ++It) {
116 if (It->second > Best->second)
118 else if (It->second == Best->second && It->first() < Best->first())
123 return Path(Best->first());
127 std::vector<const Decl *> Results;
128 std::function<void(Decl *)> TraverseDecl = [&](Decl *D) {
129 auto *ND = llvm::dyn_cast<NamedDecl>(D);
130 if (!ND || ND->isImplicit())
135 if (!llvm::isa<FunctionDecl>(ND)) {
138 if (
auto *Scope = llvm::dyn_cast<DeclContext>(ND)) {
139 for (
auto *D : Scope->decls())
143 if (llvm::isa<NamespaceDecl>(D))
145 Results.push_back(D);
149 for (
auto *TopLevel :
AST.getLocalTopLevelDecls())
150 TraverseDecl(TopLevel);
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...
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.
static llvm::Expected< std::string > resolve(const URI &U, llvm::StringRef HintPath="")
Resolves the absolute path of U.
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
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::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.
std::string Path
A typedef to represent a 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.