150
|
1 //===--- FindSymbols.cpp ------------------------------------*- C++-*------===//
|
|
2 //
|
|
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
4 // See https://llvm.org/LICENSE.txt for license information.
|
|
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
6 //
|
|
7 //===----------------------------------------------------------------------===//
|
|
8 #include "FindSymbols.h"
|
|
9
|
|
10 #include "AST.h"
|
|
11 #include "FuzzyMatch.h"
|
|
12 #include "ParsedAST.h"
|
|
13 #include "Quality.h"
|
|
14 #include "SourceCode.h"
|
|
15 #include "index/Index.h"
|
173
|
16 #include "support/Logger.h"
|
150
|
17 #include "clang/AST/DeclTemplate.h"
|
|
18 #include "clang/Index/IndexDataConsumer.h"
|
|
19 #include "clang/Index/IndexSymbol.h"
|
|
20 #include "clang/Index/IndexingAction.h"
|
221
|
21 #include "llvm/ADT/ArrayRef.h"
|
|
22 #include "llvm/ADT/STLExtras.h"
|
|
23 #include "llvm/ADT/SmallVector.h"
|
173
|
24 #include "llvm/ADT/StringRef.h"
|
150
|
25 #include "llvm/Support/FormatVariadic.h"
|
|
26 #include "llvm/Support/Path.h"
|
|
27 #include "llvm/Support/ScopedPrinter.h"
|
221
|
28 #include <limits>
|
|
29 #include <tuple>
|
150
|
30
|
|
31 #define DEBUG_TYPE "FindSymbols"
|
|
32
|
|
33 namespace clang {
|
|
34 namespace clangd {
|
|
35
|
|
36 namespace {
|
|
37 using ScoredSymbolInfo = std::pair<float, SymbolInformation>;
|
|
38 struct ScoredSymbolGreater {
|
|
39 bool operator()(const ScoredSymbolInfo &L, const ScoredSymbolInfo &R) {
|
|
40 if (L.first != R.first)
|
|
41 return L.first > R.first;
|
|
42 return L.second.name < R.second.name; // Earlier name is better.
|
|
43 }
|
|
44 };
|
|
45
|
221
|
46 // Returns true if \p Query can be found as a sub-sequence inside \p Scope.
|
|
47 bool approximateScopeMatch(llvm::StringRef Scope, llvm::StringRef Query) {
|
|
48 assert(Scope.empty() || Scope.endswith("::"));
|
|
49 assert(Query.empty() || Query.endswith("::"));
|
|
50 while (!Scope.empty() && !Query.empty()) {
|
|
51 auto Colons = Scope.find("::");
|
|
52 assert(Colons != llvm::StringRef::npos);
|
|
53
|
|
54 llvm::StringRef LeadingSpecifier = Scope.slice(0, Colons + 2);
|
|
55 Scope = Scope.slice(Colons + 2, llvm::StringRef::npos);
|
|
56 Query.consume_front(LeadingSpecifier);
|
|
57 }
|
|
58 return Query.empty();
|
|
59 }
|
|
60
|
150
|
61 } // namespace
|
|
62
|
173
|
63 llvm::Expected<Location> indexToLSPLocation(const SymbolLocation &Loc,
|
|
64 llvm::StringRef TUPath) {
|
|
65 auto Path = URI::resolve(Loc.FileURI, TUPath);
|
221
|
66 if (!Path)
|
|
67 return error("Could not resolve path for file '{0}': {1}", Loc.FileURI,
|
|
68 Path.takeError());
|
150
|
69 Location L;
|
173
|
70 L.uri = URIForFile::canonicalize(*Path, TUPath);
|
150
|
71 Position Start, End;
|
173
|
72 Start.line = Loc.Start.line();
|
|
73 Start.character = Loc.Start.column();
|
|
74 End.line = Loc.End.line();
|
|
75 End.character = Loc.End.column();
|
150
|
76 L.range = {Start, End};
|
|
77 return L;
|
|
78 }
|
|
79
|
173
|
80 llvm::Expected<Location> symbolToLocation(const Symbol &Sym,
|
|
81 llvm::StringRef TUPath) {
|
|
82 // Prefer the definition over e.g. a function declaration in a header
|
|
83 return indexToLSPLocation(
|
|
84 Sym.Definition ? Sym.Definition : Sym.CanonicalDeclaration, TUPath);
|
|
85 }
|
|
86
|
150
|
87 llvm::Expected<std::vector<SymbolInformation>>
|
|
88 getWorkspaceSymbols(llvm::StringRef Query, int Limit,
|
|
89 const SymbolIndex *const Index, llvm::StringRef HintPath) {
|
|
90 std::vector<SymbolInformation> Result;
|
221
|
91 if (!Index)
|
150
|
92 return Result;
|
|
93
|
221
|
94 // Lookup for qualified names are performed as:
|
|
95 // - Exact namespaces are boosted by the index.
|
|
96 // - Approximate matches are (sub-scope match) included via AnyScope logic.
|
|
97 // - Non-matching namespaces (no sub-scope match) are post-filtered.
|
150
|
98 auto Names = splitQualifiedName(Query);
|
|
99
|
|
100 FuzzyFindRequest Req;
|
|
101 Req.Query = std::string(Names.second);
|
|
102
|
221
|
103 // FuzzyFind doesn't want leading :: qualifier.
|
|
104 auto HasLeadingColons = Names.first.consume_front("::");
|
|
105 // Limit the query to specific namespace if it is fully-qualified.
|
|
106 Req.AnyScope = !HasLeadingColons;
|
|
107 // Boost symbols from desired namespace.
|
|
108 if (HasLeadingColons || !Names.first.empty())
|
150
|
109 Req.Scopes = {std::string(Names.first)};
|
221
|
110 if (Limit) {
|
150
|
111 Req.Limit = Limit;
|
221
|
112 // If we are boosting a specific scope allow more results to be retrieved,
|
|
113 // since some symbols from preferred namespaces might not make the cut.
|
|
114 if (Req.AnyScope && !Req.Scopes.empty())
|
|
115 *Req.Limit *= 5;
|
|
116 }
|
150
|
117 TopN<ScoredSymbolInfo, ScoredSymbolGreater> Top(
|
|
118 Req.Limit ? *Req.Limit : std::numeric_limits<size_t>::max());
|
|
119 FuzzyMatcher Filter(Req.Query);
|
221
|
120
|
|
121 Index->fuzzyFind(Req, [HintPath, &Top, &Filter, AnyScope = Req.AnyScope,
|
|
122 ReqScope = Names.first](const Symbol &Sym) {
|
|
123 llvm::StringRef Scope = Sym.Scope;
|
|
124 // Fuzzyfind might return symbols from irrelevant namespaces if query was
|
|
125 // not fully-qualified, drop those.
|
|
126 if (AnyScope && !approximateScopeMatch(Scope, ReqScope))
|
|
127 return;
|
|
128
|
150
|
129 auto Loc = symbolToLocation(Sym, HintPath);
|
|
130 if (!Loc) {
|
|
131 log("Workspace symbols: {0}", Loc.takeError());
|
|
132 return;
|
|
133 }
|
|
134
|
|
135 SymbolQualitySignals Quality;
|
|
136 Quality.merge(Sym);
|
|
137 SymbolRelevanceSignals Relevance;
|
|
138 Relevance.Name = Sym.Name;
|
|
139 Relevance.Query = SymbolRelevanceSignals::Generic;
|
221
|
140 // If symbol and request scopes do not match exactly, apply a penalty.
|
|
141 Relevance.InBaseClass = AnyScope && Scope != ReqScope;
|
150
|
142 if (auto NameMatch = Filter.match(Sym.Name))
|
|
143 Relevance.NameMatch = *NameMatch;
|
|
144 else {
|
|
145 log("Workspace symbol: {0} didn't match query {1}", Sym.Name,
|
|
146 Filter.pattern());
|
|
147 return;
|
|
148 }
|
|
149 Relevance.merge(Sym);
|
221
|
150 auto QualScore = Quality.evaluateHeuristics();
|
|
151 auto RelScore = Relevance.evaluateHeuristics();
|
|
152 auto Score = evaluateSymbolAndRelevance(QualScore, RelScore);
|
150
|
153 dlog("FindSymbols: {0}{1} = {2}\n{3}{4}\n", Sym.Scope, Sym.Name, Score,
|
|
154 Quality, Relevance);
|
|
155
|
221
|
156 SymbolInformation Info;
|
|
157 Info.name = (Sym.Name + Sym.TemplateSpecializationArgs).str();
|
|
158 Info.kind = indexSymbolKindToSymbolKind(Sym.SymInfo.Kind);
|
|
159 Info.location = *Loc;
|
|
160 Scope.consume_back("::");
|
|
161 Info.containerName = Scope.str();
|
|
162
|
|
163 // Exposed score excludes fuzzy-match component, for client-side re-ranking.
|
|
164 Info.score = Relevance.NameMatch > std::numeric_limits<float>::epsilon()
|
|
165 ? Score / Relevance.NameMatch
|
|
166 : QualScore;
|
150
|
167 Top.push({Score, std::move(Info)});
|
|
168 });
|
|
169 for (auto &R : std::move(Top).items())
|
|
170 Result.push_back(std::move(R.second));
|
|
171 return Result;
|
|
172 }
|
|
173
|
|
174 namespace {
|
221
|
175 std::string getSymbolName(ASTContext &Ctx, const NamedDecl &ND) {
|
|
176 // Print `MyClass(Category)` instead of `Category` and `MyClass()` instead
|
|
177 // of `anonymous`.
|
|
178 if (const auto *Container = dyn_cast<ObjCContainerDecl>(&ND))
|
|
179 return printObjCContainer(*Container);
|
|
180 // Differentiate between class and instance methods: print `-foo` instead of
|
|
181 // `foo` and `+sharedInstance` instead of `sharedInstance`.
|
|
182 if (const auto *Method = dyn_cast<ObjCMethodDecl>(&ND)) {
|
|
183 std::string Name;
|
|
184 llvm::raw_string_ostream OS(Name);
|
|
185
|
|
186 OS << (Method->isInstanceMethod() ? '-' : '+');
|
|
187 Method->getSelector().print(OS);
|
|
188
|
|
189 OS.flush();
|
|
190 return Name;
|
|
191 }
|
|
192 return printName(Ctx, ND);
|
|
193 }
|
|
194
|
|
195 std::string getSymbolDetail(ASTContext &Ctx, const NamedDecl &ND) {
|
|
196 PrintingPolicy P(Ctx.getPrintingPolicy());
|
|
197 P.SuppressScope = true;
|
|
198 P.SuppressUnwrittenScope = true;
|
|
199 P.AnonymousTagLocations = false;
|
|
200 P.PolishForDeclaration = true;
|
|
201 std::string Detail;
|
|
202 llvm::raw_string_ostream OS(Detail);
|
|
203 if (ND.getDescribedTemplateParams()) {
|
|
204 OS << "template ";
|
|
205 }
|
|
206 if (const auto *VD = dyn_cast<ValueDecl>(&ND)) {
|
|
207 // FIXME: better printing for dependent type
|
|
208 if (isa<CXXConstructorDecl>(VD)) {
|
|
209 std::string ConstructorType = VD->getType().getAsString(P);
|
|
210 // Print constructor type as "(int)" instead of "void (int)".
|
|
211 llvm::StringRef WithoutVoid = ConstructorType;
|
|
212 WithoutVoid.consume_front("void ");
|
|
213 OS << WithoutVoid;
|
|
214 } else if (!isa<CXXDestructorDecl>(VD)) {
|
|
215 VD->getType().print(OS, P);
|
|
216 }
|
|
217 } else if (const auto *TD = dyn_cast<TagDecl>(&ND)) {
|
|
218 OS << TD->getKindName();
|
|
219 } else if (isa<TypedefNameDecl>(&ND)) {
|
|
220 OS << "type alias";
|
|
221 } else if (isa<ConceptDecl>(&ND)) {
|
|
222 OS << "concept";
|
|
223 }
|
|
224 return std::move(OS.str());
|
|
225 }
|
|
226
|
150
|
227 llvm::Optional<DocumentSymbol> declToSym(ASTContext &Ctx, const NamedDecl &ND) {
|
|
228 auto &SM = Ctx.getSourceManager();
|
|
229
|
|
230 SourceLocation BeginLoc = SM.getSpellingLoc(SM.getFileLoc(ND.getBeginLoc()));
|
|
231 SourceLocation EndLoc = SM.getSpellingLoc(SM.getFileLoc(ND.getEndLoc()));
|
221
|
232 const auto SymbolRange =
|
|
233 toHalfOpenFileRange(SM, Ctx.getLangOpts(), {BeginLoc, EndLoc});
|
|
234 if (!SymbolRange)
|
150
|
235 return llvm::None;
|
|
236
|
|
237 index::SymbolInfo SymInfo = index::getSymbolInfo(&ND);
|
221
|
238 // FIXME: This is not classifying constructors, destructors and operators
|
|
239 // correctly.
|
150
|
240 SymbolKind SK = indexSymbolKindToSymbolKind(SymInfo.Kind);
|
|
241
|
|
242 DocumentSymbol SI;
|
221
|
243 SI.name = getSymbolName(Ctx, ND);
|
150
|
244 SI.kind = SK;
|
|
245 SI.deprecated = ND.isDeprecated();
|
221
|
246 SI.range = Range{sourceLocToPosition(SM, SymbolRange->getBegin()),
|
|
247 sourceLocToPosition(SM, SymbolRange->getEnd())};
|
|
248 SI.detail = getSymbolDetail(Ctx, ND);
|
|
249
|
|
250 SourceLocation NameLoc = ND.getLocation();
|
|
251 SourceLocation FallbackNameLoc;
|
|
252 if (NameLoc.isMacroID()) {
|
|
253 if (isSpelledInSource(NameLoc, SM)) {
|
|
254 // Prefer the spelling loc, but save the expansion loc as a fallback.
|
|
255 FallbackNameLoc = SM.getExpansionLoc(NameLoc);
|
|
256 NameLoc = SM.getSpellingLoc(NameLoc);
|
|
257 } else {
|
|
258 NameLoc = SM.getExpansionLoc(NameLoc);
|
|
259 }
|
|
260 }
|
|
261 auto ComputeSelectionRange = [&](SourceLocation L) -> Range {
|
|
262 Position NameBegin = sourceLocToPosition(SM, L);
|
|
263 Position NameEnd = sourceLocToPosition(
|
|
264 SM, Lexer::getLocForEndOfToken(L, 0, SM, Ctx.getLangOpts()));
|
|
265 return Range{NameBegin, NameEnd};
|
|
266 };
|
|
267
|
|
268 SI.selectionRange = ComputeSelectionRange(NameLoc);
|
|
269 if (!SI.range.contains(SI.selectionRange) && FallbackNameLoc.isValid()) {
|
|
270 // 'selectionRange' must be contained in 'range'. In cases where clang
|
|
271 // reports unrelated ranges, we first try falling back to the expansion
|
|
272 // loc for the selection range.
|
|
273 SI.selectionRange = ComputeSelectionRange(FallbackNameLoc);
|
|
274 }
|
150
|
275 if (!SI.range.contains(SI.selectionRange)) {
|
221
|
276 // If the containment relationship still doesn't hold, throw away
|
|
277 // 'range' and use 'selectionRange' for both.
|
150
|
278 SI.range = SI.selectionRange;
|
|
279 }
|
|
280 return SI;
|
|
281 }
|
|
282
|
|
283 /// A helper class to build an outline for the parse AST. It traverses the AST
|
|
284 /// directly instead of using RecursiveASTVisitor (RAV) for three main reasons:
|
|
285 /// - there is no way to keep RAV from traversing subtrees we are not
|
|
286 /// interested in. E.g. not traversing function locals or implicit template
|
|
287 /// instantiations.
|
|
288 /// - it's easier to combine results of recursive passes,
|
|
289 /// - visiting decls is actually simple, so we don't hit the complicated
|
|
290 /// cases that RAV mostly helps with (types, expressions, etc.)
|
|
291 class DocumentOutline {
|
221
|
292 // A DocumentSymbol we're constructing.
|
|
293 // We use this instead of DocumentSymbol directly so that we can keep track
|
|
294 // of the nodes we insert for macros.
|
|
295 class SymBuilder {
|
|
296 std::vector<SymBuilder> Children;
|
|
297 DocumentSymbol Symbol; // Symbol.children is empty, use Children instead.
|
|
298 // Macro expansions that this node or its parents are associated with.
|
|
299 // (Thus we will never create further children for these expansions).
|
|
300 llvm::SmallVector<SourceLocation> EnclosingMacroLoc;
|
|
301
|
|
302 public:
|
|
303 DocumentSymbol build() && {
|
|
304 for (SymBuilder &C : Children) {
|
|
305 Symbol.children.push_back(std::move(C).build());
|
|
306 // Expand range to ensure children nest properly, which editors expect.
|
|
307 // This can fix some edge-cases in the AST, but is vital for macros.
|
|
308 // A macro expansion "contains" AST node if it covers the node's primary
|
|
309 // location, but it may not span the node's whole range.
|
|
310 Symbol.range.start =
|
|
311 std::min(Symbol.range.start, Symbol.children.back().range.start);
|
|
312 Symbol.range.end =
|
|
313 std::max(Symbol.range.end, Symbol.children.back().range.end);
|
|
314 }
|
|
315 return std::move(Symbol);
|
|
316 }
|
|
317
|
|
318 // Add a symbol as a child of the current one.
|
|
319 SymBuilder &addChild(DocumentSymbol S) {
|
|
320 Children.emplace_back();
|
|
321 Children.back().EnclosingMacroLoc = EnclosingMacroLoc;
|
|
322 Children.back().Symbol = std::move(S);
|
|
323 return Children.back();
|
|
324 }
|
|
325
|
|
326 // Get an appropriate container for children of this symbol that were
|
|
327 // expanded from a macro (whose spelled name is Tok).
|
|
328 //
|
|
329 // This may return:
|
|
330 // - a macro symbol child of this (either new or previously created)
|
|
331 // - this scope itself, if it *is* the macro symbol or is nested within it
|
|
332 SymBuilder &inMacro(const syntax::Token &Tok, const SourceManager &SM,
|
|
333 llvm::Optional<syntax::TokenBuffer::Expansion> Exp) {
|
|
334 if (llvm::is_contained(EnclosingMacroLoc, Tok.location()))
|
|
335 return *this;
|
|
336 // If there's an existing child for this macro, we expect it to be last.
|
|
337 if (!Children.empty() && !Children.back().EnclosingMacroLoc.empty() &&
|
|
338 Children.back().EnclosingMacroLoc.back() == Tok.location())
|
|
339 return Children.back();
|
|
340
|
|
341 DocumentSymbol Sym;
|
|
342 Sym.name = Tok.text(SM).str();
|
|
343 Sym.kind = SymbolKind::Null; // There's no suitable kind!
|
|
344 Sym.range = Sym.selectionRange =
|
|
345 halfOpenToRange(SM, Tok.range(SM).toCharRange(SM));
|
|
346
|
|
347 // FIXME: Exp is currently unavailable for nested expansions.
|
|
348 if (Exp) {
|
|
349 // Full range covers the macro args.
|
|
350 Sym.range = halfOpenToRange(SM, CharSourceRange::getCharRange(
|
|
351 Exp->Spelled.front().location(),
|
|
352 Exp->Spelled.back().endLocation()));
|
|
353 // Show macro args as detail.
|
|
354 llvm::raw_string_ostream OS(Sym.detail);
|
|
355 const syntax::Token *Prev = nullptr;
|
|
356 for (const auto &Tok : Exp->Spelled.drop_front()) {
|
|
357 // Don't dump arbitrarily long macro args.
|
|
358 if (OS.tell() > 80) {
|
|
359 OS << " ...)";
|
|
360 break;
|
|
361 }
|
|
362 if (Prev && Prev->endLocation() != Tok.location())
|
|
363 OS << ' ';
|
|
364 OS << Tok.text(SM);
|
|
365 Prev = &Tok;
|
|
366 }
|
|
367 }
|
|
368 SymBuilder &Child = addChild(std::move(Sym));
|
|
369 Child.EnclosingMacroLoc.push_back(Tok.location());
|
|
370 return Child;
|
|
371 }
|
|
372 };
|
|
373
|
150
|
374 public:
|
|
375 DocumentOutline(ParsedAST &AST) : AST(AST) {}
|
|
376
|
|
377 /// Builds the document outline for the generated AST.
|
|
378 std::vector<DocumentSymbol> build() {
|
221
|
379 SymBuilder Root;
|
150
|
380 for (auto &TopLevel : AST.getLocalTopLevelDecls())
|
221
|
381 traverseDecl(TopLevel, Root);
|
|
382 return std::move(std::move(Root).build().children);
|
150
|
383 }
|
|
384
|
|
385 private:
|
221
|
386 enum class VisitKind { No, OnlyDecl, OnlyChildren, DeclAndChildren };
|
150
|
387
|
221
|
388 void traverseDecl(Decl *D, SymBuilder &Parent) {
|
|
389 // Skip symbols which do not originate from the main file.
|
|
390 if (!isInsideMainFile(D->getLocation(), AST.getSourceManager()))
|
|
391 return;
|
|
392
|
150
|
393 if (auto *Templ = llvm::dyn_cast<TemplateDecl>(D)) {
|
|
394 // TemplatedDecl might be null, e.g. concepts.
|
|
395 if (auto *TD = Templ->getTemplatedDecl())
|
|
396 D = TD;
|
|
397 }
|
221
|
398
|
|
399 VisitKind Visit = shouldVisit(D);
|
150
|
400 if (Visit == VisitKind::No)
|
|
401 return;
|
221
|
402
|
|
403 if (Visit == VisitKind::OnlyChildren)
|
|
404 return traverseChildren(D, Parent);
|
|
405
|
|
406 auto *ND = llvm::cast<NamedDecl>(D);
|
|
407 auto Sym = declToSym(AST.getASTContext(), *ND);
|
150
|
408 if (!Sym)
|
|
409 return;
|
221
|
410 SymBuilder &MacroParent = possibleMacroContainer(D->getLocation(), Parent);
|
|
411 SymBuilder &Child = MacroParent.addChild(std::move(*Sym));
|
|
412
|
|
413 if (Visit == VisitKind::OnlyDecl)
|
|
414 return;
|
|
415
|
|
416 assert(Visit == VisitKind::DeclAndChildren && "Unexpected VisitKind");
|
|
417 traverseChildren(ND, Child);
|
150
|
418 }
|
|
419
|
221
|
420 // Determines where a decl should appear in the DocumentSymbol hierarchy.
|
|
421 //
|
|
422 // This is usually a direct child of the relevant AST parent.
|
|
423 // But we may also insert nodes for macros. Given:
|
|
424 // #define DECLARE_INT(V) int v;
|
|
425 // namespace a { DECLARE_INT(x) }
|
|
426 // We produce:
|
|
427 // Namespace a
|
|
428 // Macro DECLARE_INT(x)
|
|
429 // Variable x
|
|
430 //
|
|
431 // In the absence of macros, this method simply returns Parent.
|
|
432 // Otherwise it may return a macro expansion node instead.
|
|
433 // Each macro only has at most one node in the hierarchy, even if it expands
|
|
434 // to multiple decls.
|
|
435 SymBuilder &possibleMacroContainer(SourceLocation TargetLoc,
|
|
436 SymBuilder &Parent) {
|
|
437 const auto &SM = AST.getSourceManager();
|
|
438 // Look at the path of macro-callers from the token to the main file.
|
|
439 // Note that along these paths we see the "outer" macro calls first.
|
|
440 SymBuilder *CurParent = &Parent;
|
|
441 for (SourceLocation Loc = TargetLoc; Loc.isMacroID();
|
|
442 Loc = SM.getImmediateMacroCallerLoc(Loc)) {
|
|
443 // Find the virtual macro body that our token is being substituted into.
|
|
444 FileID MacroBody;
|
|
445 if (SM.isMacroArgExpansion(Loc)) {
|
|
446 // Loc is part of a macro arg being substituted into a macro body.
|
|
447 MacroBody = SM.getFileID(SM.getImmediateExpansionRange(Loc).getBegin());
|
|
448 } else {
|
|
449 // Loc is already in the macro body.
|
|
450 MacroBody = SM.getFileID(Loc);
|
|
451 }
|
|
452 // The macro body is being substituted for a macro expansion, whose
|
|
453 // first token is the name of the macro.
|
|
454 SourceLocation MacroName =
|
|
455 SM.getSLocEntry(MacroBody).getExpansion().getExpansionLocStart();
|
|
456 // Only include the macro expansion in the outline if it was written
|
|
457 // directly in the main file, rather than expanded from another macro.
|
|
458 if (!MacroName.isValid() || !MacroName.isFileID())
|
|
459 continue;
|
|
460 // All conditions satisfied, add the macro.
|
|
461 if (auto *Tok = AST.getTokens().spelledTokenAt(MacroName))
|
|
462 CurParent = &CurParent->inMacro(
|
|
463 *Tok, SM, AST.getTokens().expansionStartingAt(Tok));
|
|
464 }
|
|
465 return *CurParent;
|
|
466 }
|
|
467
|
|
468 void traverseChildren(Decl *D, SymBuilder &Builder) {
|
150
|
469 auto *Scope = llvm::dyn_cast<DeclContext>(D);
|
|
470 if (!Scope)
|
|
471 return;
|
|
472 for (auto *C : Scope->decls())
|
221
|
473 traverseDecl(C, Builder);
|
150
|
474 }
|
|
475
|
221
|
476 VisitKind shouldVisit(Decl *D) {
|
150
|
477 if (D->isImplicit())
|
|
478 return VisitKind::No;
|
|
479
|
221
|
480 if (llvm::isa<LinkageSpecDecl>(D) || llvm::isa<ExportDecl>(D))
|
|
481 return VisitKind::OnlyChildren;
|
|
482
|
|
483 if (!llvm::isa<NamedDecl>(D))
|
|
484 return VisitKind::No;
|
|
485
|
150
|
486 if (auto Func = llvm::dyn_cast<FunctionDecl>(D)) {
|
|
487 // Some functions are implicit template instantiations, those should be
|
|
488 // ignored.
|
|
489 if (auto *Info = Func->getTemplateSpecializationInfo()) {
|
|
490 if (!Info->isExplicitInstantiationOrSpecialization())
|
|
491 return VisitKind::No;
|
|
492 }
|
|
493 // Only visit the function itself, do not visit the children (i.e.
|
|
494 // function parameters, etc.)
|
|
495 return VisitKind::OnlyDecl;
|
|
496 }
|
|
497 // Handle template instantiations. We have three cases to consider:
|
|
498 // - explicit instantiations, e.g. 'template class std::vector<int>;'
|
|
499 // Visit the decl itself (it's present in the code), but not the
|
|
500 // children.
|
|
501 // - implicit instantiations, i.e. not written by the user.
|
|
502 // Do not visit at all, they are not present in the code.
|
|
503 // - explicit specialization, e.g. 'template <> class vector<bool> {};'
|
|
504 // Visit both the decl and its children, both are written in the code.
|
|
505 if (auto *TemplSpec = llvm::dyn_cast<ClassTemplateSpecializationDecl>(D)) {
|
|
506 if (TemplSpec->isExplicitInstantiationOrSpecialization())
|
|
507 return TemplSpec->isExplicitSpecialization()
|
|
508 ? VisitKind::DeclAndChildren
|
|
509 : VisitKind::OnlyDecl;
|
|
510 return VisitKind::No;
|
|
511 }
|
|
512 if (auto *TemplSpec = llvm::dyn_cast<VarTemplateSpecializationDecl>(D)) {
|
|
513 if (TemplSpec->isExplicitInstantiationOrSpecialization())
|
|
514 return TemplSpec->isExplicitSpecialization()
|
|
515 ? VisitKind::DeclAndChildren
|
|
516 : VisitKind::OnlyDecl;
|
|
517 return VisitKind::No;
|
|
518 }
|
|
519 // For all other cases, visit both the children and the decl.
|
|
520 return VisitKind::DeclAndChildren;
|
|
521 }
|
|
522
|
|
523 ParsedAST &AST;
|
|
524 };
|
|
525
|
|
526 std::vector<DocumentSymbol> collectDocSymbols(ParsedAST &AST) {
|
|
527 return DocumentOutline(AST).build();
|
|
528 }
|
|
529 } // namespace
|
|
530
|
|
531 llvm::Expected<std::vector<DocumentSymbol>> getDocumentSymbols(ParsedAST &AST) {
|
|
532 return collectDocSymbols(AST);
|
|
533 }
|
|
534
|
|
535 } // namespace clangd
|
|
536 } // namespace clang
|