blob: 412a0a37eab38747287a1869f28ff1b3bd8c263b [file] [log] [blame]
Haojian Wu4c1394d2017-12-12 15:42:10 +00001//===--- SymbolCollector.cpp -------------------------------------*- C++-*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SymbolCollector.h"
Haojian Wu5f100262018-03-09 14:00:34 +000011#include "../AST.h"
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +000012#include "../CodeComplete.h"
Eric Liu76f6b442018-01-09 17:32:00 +000013#include "../CodeCompletionStrings.h"
Eric Liu7f247652018-02-06 16:10:35 +000014#include "../Logger.h"
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +000015#include "../SourceCode.h"
Eric Liu7f247652018-02-06 16:10:35 +000016#include "../URI.h"
Eric Liuc5105f92018-02-16 14:15:55 +000017#include "CanonicalIncludes.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000018#include "clang/AST/DeclCXX.h"
Ilya Biryukovcf124bd2018-04-13 11:03:07 +000019#include "clang/AST/DeclTemplate.h"
Eric Liu9af958f2018-01-10 14:57:58 +000020#include "clang/ASTMatchers/ASTMatchFinder.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000021#include "clang/Basic/SourceManager.h"
22#include "clang/Index/IndexSymbol.h"
23#include "clang/Index/USRGeneration.h"
Eric Liu278e2d12018-01-29 15:13:29 +000024#include "llvm/Support/FileSystem.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000025#include "llvm/Support/MemoryBuffer.h"
26#include "llvm/Support/Path.h"
27
28namespace clang {
29namespace clangd {
30
31namespace {
Ilya Biryukovf118d512018-04-14 16:27:35 +000032/// If \p ND is a template specialization, returns the described template.
Ilya Biryukovcf124bd2018-04-13 11:03:07 +000033/// Otherwise, returns \p ND.
34const NamedDecl &getTemplateOrThis(const NamedDecl &ND) {
Ilya Biryukovf118d512018-04-14 16:27:35 +000035 if (auto T = ND.getDescribedTemplate())
36 return *T;
Ilya Biryukovcf124bd2018-04-13 11:03:07 +000037 return ND;
38}
39
Eric Liu7f247652018-02-06 16:10:35 +000040// Returns a URI of \p Path. Firstly, this makes the \p Path absolute using the
41// current working directory of the given SourceManager if the Path is not an
42// absolute path. If failed, this resolves relative paths against \p FallbackDir
43// to get an absolute path. Then, this tries creating an URI for the absolute
44// path with schemes specified in \p Opts. This returns an URI with the first
45// working scheme, if there is any; otherwise, this returns None.
Haojian Wu4c1394d2017-12-12 15:42:10 +000046//
47// The Path can be a path relative to the build directory, or retrieved from
48// the SourceManager.
Eric Liu7f247652018-02-06 16:10:35 +000049llvm::Optional<std::string> toURI(const SourceManager &SM, StringRef Path,
50 const SymbolCollector::Options &Opts) {
Haojian Wu4c1394d2017-12-12 15:42:10 +000051 llvm::SmallString<128> AbsolutePath(Path);
52 if (std::error_code EC =
53 SM.getFileManager().getVirtualFileSystem()->makeAbsolute(
54 AbsolutePath))
Eric Liu20defe12018-05-15 16:22:43 +000055 log("Warning: could not make absolute file: " + EC.message());
Eric Liu278e2d12018-01-29 15:13:29 +000056 if (llvm::sys::path::is_absolute(AbsolutePath)) {
57 // Handle the symbolic link path case where the current working directory
58 // (getCurrentWorkingDirectory) is a symlink./ We always want to the real
59 // file path (instead of the symlink path) for the C++ symbols.
60 //
61 // Consider the following example:
62 //
63 // src dir: /project/src/foo.h
64 // current working directory (symlink): /tmp/build -> /project/src/
65 //
66 // The file path of Symbol is "/project/src/foo.h" instead of
67 // "/tmp/build/foo.h"
68 if (const DirectoryEntry *Dir = SM.getFileManager().getDirectory(
69 llvm::sys::path::parent_path(AbsolutePath.str()))) {
70 StringRef DirName = SM.getFileManager().getCanonicalName(Dir);
71 SmallString<128> AbsoluteFilename;
72 llvm::sys::path::append(AbsoluteFilename, DirName,
73 llvm::sys::path::filename(AbsolutePath.str()));
74 AbsolutePath = AbsoluteFilename;
75 }
Eric Liu7f247652018-02-06 16:10:35 +000076 } else if (!Opts.FallbackDir.empty()) {
77 llvm::sys::fs::make_absolute(Opts.FallbackDir, AbsolutePath);
Eric Liu278e2d12018-01-29 15:13:29 +000078 llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/true);
Haojian Wu4c1394d2017-12-12 15:42:10 +000079 }
Eric Liu7f247652018-02-06 16:10:35 +000080
81 std::string ErrMsg;
82 for (const auto &Scheme : Opts.URISchemes) {
83 auto U = URI::create(AbsolutePath, Scheme);
84 if (U)
85 return U->toString();
86 ErrMsg += llvm::toString(U.takeError()) + "\n";
87 }
88 log(llvm::Twine("Failed to create an URI for file ") + AbsolutePath + ": " +
89 ErrMsg);
90 return llvm::None;
Haojian Wu4c1394d2017-12-12 15:42:10 +000091}
Eric Liu4feda802017-12-19 11:37:40 +000092
Eric Liud67ec242018-05-16 12:12:30 +000093// All proto generated headers should start with this line.
94static const char *PROTO_HEADER_COMMENT =
95 "// Generated by the protocol buffer compiler. DO NOT EDIT!";
96
97// Checks whether the decl is a private symbol in a header generated by
98// protobuf compiler.
99// To identify whether a proto header is actually generated by proto compiler,
100// we check whether it starts with PROTO_HEADER_COMMENT.
101// FIXME: make filtering extensible when there are more use cases for symbol
102// filters.
103bool isPrivateProtoDecl(const NamedDecl &ND) {
104 const auto &SM = ND.getASTContext().getSourceManager();
105 auto Loc = findNameLoc(&ND);
106 auto FileName = SM.getFilename(Loc);
107 if (!FileName.endswith(".proto.h") && !FileName.endswith(".pb.h"))
108 return false;
109 auto FID = SM.getFileID(Loc);
110 // Double check that this is an actual protobuf header.
111 if (!SM.getBufferData(FID).startswith(PROTO_HEADER_COMMENT))
112 return false;
113
114 // ND without identifier can be operators.
115 if (ND.getIdentifier() == nullptr)
116 return false;
117 auto Name = ND.getIdentifier()->getName();
118 if (!Name.contains('_'))
119 return false;
120 // Nested proto entities (e.g. Message::Nested) have top-level decls
121 // that shouldn't be used (Message_Nested). Ignore them completely.
122 // The nested entities are dangling type aliases, we may want to reconsider
123 // including them in the future.
124 // For enum constants, SOME_ENUM_CONSTANT is not private and should be
125 // indexed. Outer_INNER is private. This heuristic relies on naming style, it
126 // will include OUTER_INNER and exclude some_enum_constant.
127 // FIXME: the heuristic relies on naming style (i.e. no underscore in
128 // user-defined names) and can be improved.
129 return (ND.getKind() != Decl::EnumConstant) ||
130 std::any_of(Name.begin(), Name.end(), islower);
131}
132
Eric Liu9af958f2018-01-10 14:57:58 +0000133bool shouldFilterDecl(const NamedDecl *ND, ASTContext *ASTCtx,
134 const SymbolCollector::Options &Opts) {
135 using namespace clang::ast_matchers;
136 if (ND->isImplicit())
137 return true;
Haojian Wu9873fdd2018-01-19 09:35:55 +0000138 // Skip anonymous declarations, e.g (anonymous enum/class/struct).
139 if (ND->getDeclName().isEmpty())
140 return true;
141
Eric Liu9af958f2018-01-10 14:57:58 +0000142 // FIXME: figure out a way to handle internal linkage symbols (e.g. static
143 // variables, function) defined in the .cc files. Also we skip the symbols
144 // in anonymous namespace as the qualifier names of these symbols are like
145 // `foo::<anonymous>::bar`, which need a special handling.
146 // In real world projects, we have a relatively large set of header files
147 // that define static variables (like "static const int A = 1;"), we still
148 // want to collect these symbols, although they cause potential ODR
149 // violations.
150 if (ND->isInAnonymousNamespace())
151 return true;
152
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +0000153 // We want most things but not "local" symbols such as symbols inside
154 // FunctionDecl, BlockDecl, ObjCMethodDecl and OMPDeclareReductionDecl.
155 // FIXME: Need a matcher for ExportDecl in order to include symbols declared
156 // within an export.
157 auto InNonLocalContext = hasDeclContext(anyOf(
158 translationUnitDecl(), namespaceDecl(), linkageSpecDecl(), recordDecl(),
159 enumDecl(), objcProtocolDecl(), objcInterfaceDecl(), objcCategoryDecl(),
160 objcCategoryImplDecl(), objcImplementationDecl()));
161 // Don't index template specializations and expansions in main files.
Sam McCall824913b2018-03-09 13:25:29 +0000162 auto IsSpecialization =
163 anyOf(functionDecl(isExplicitTemplateSpecialization()),
164 cxxRecordDecl(isExplicitTemplateSpecialization()),
165 varDecl(isExplicitTemplateSpecialization()));
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +0000166 if (match(decl(allOf(unless(isExpansionInMainFile()), InNonLocalContext,
Sam McCall824913b2018-03-09 13:25:29 +0000167 unless(IsSpecialization))),
Eric Liu9af958f2018-01-10 14:57:58 +0000168 *ND, *ASTCtx)
169 .empty())
170 return true;
171
Eric Liud67ec242018-05-16 12:12:30 +0000172 // Avoid indexing internal symbols in protobuf generated headers.
173 if (isPrivateProtoDecl(*ND))
174 return true;
Eric Liu9af958f2018-01-10 14:57:58 +0000175 return false;
176}
177
Eric Liuc5105f92018-02-16 14:15:55 +0000178// We only collect #include paths for symbols that are suitable for global code
179// completion, except for namespaces since #include path for a namespace is hard
180// to define.
181bool shouldCollectIncludePath(index::SymbolKind Kind) {
182 using SK = index::SymbolKind;
183 switch (Kind) {
184 case SK::Macro:
185 case SK::Enum:
186 case SK::Struct:
187 case SK::Class:
188 case SK::Union:
189 case SK::TypeAlias:
190 case SK::Using:
191 case SK::Function:
192 case SK::Variable:
193 case SK::EnumConstant:
194 return true;
195 default:
196 return false;
197 }
198}
199
Eric Liu02ce01f2018-02-22 10:14:05 +0000200/// Gets a canonical include (URI of the header or <header> or "header") for
201/// header of \p Loc.
202/// Returns None if fails to get include header for \p Loc.
Eric Liuc5105f92018-02-16 14:15:55 +0000203llvm::Optional<std::string>
Eric Liub96363d2018-03-01 18:06:40 +0000204getIncludeHeader(llvm::StringRef QName, const SourceManager &SM,
205 SourceLocation Loc, const SymbolCollector::Options &Opts) {
Eric Liu3cee95e2018-05-24 14:40:24 +0000206 std::vector<std::string> Headers;
207 // Collect the #include stack.
208 while (true) {
209 if (!Loc.isValid())
210 break;
211 auto FilePath = SM.getFilename(Loc);
212 if (FilePath.empty())
213 break;
214 Headers.push_back(FilePath);
215 if (SM.isInMainFile(Loc))
216 break;
217 Loc = SM.getIncludeLoc(SM.getFileID(Loc));
Eric Liuc5105f92018-02-16 14:15:55 +0000218 }
Eric Liu3cee95e2018-05-24 14:40:24 +0000219 if (Headers.empty())
220 return llvm::None;
221 llvm::StringRef Header = Headers[0];
222 if (Opts.Includes) {
223 Header = Opts.Includes->mapHeader(Headers, QName);
224 if (Header.startswith("<") || Header.startswith("\""))
225 return Header.str();
226 }
227 return toURI(SM, Header, Opts);
Eric Liuc5105f92018-02-16 14:15:55 +0000228}
229
Haojian Wub0189062018-01-31 12:56:51 +0000230// Return the symbol location of the given declaration `D`.
231//
232// For symbols defined inside macros:
233// * use expansion location, if the symbol is formed via macro concatenation.
234// * use spelling location, otherwise.
Eric Liucf8601b2018-02-28 09:33:15 +0000235llvm::Optional<SymbolLocation> getSymbolLocation(
236 const NamedDecl &D, SourceManager &SM, const SymbolCollector::Options &Opts,
237 const clang::LangOptions &LangOpts, std::string &FileURIStorage) {
Haojian Wu5f100262018-03-09 14:00:34 +0000238 SourceLocation NameLoc = findNameLoc(&D);
239 auto U = toURI(SM, SM.getFilename(NameLoc), Opts);
Eric Liu7f247652018-02-06 16:10:35 +0000240 if (!U)
241 return llvm::None;
242 FileURIStorage = std::move(*U);
Sam McCall60039512018-02-09 14:42:01 +0000243 SymbolLocation Result;
244 Result.FileURI = FileURIStorage;
Haojian Wu545c02a2018-04-13 08:30:39 +0000245 auto TokenLength = clang::Lexer::MeasureTokenLength(NameLoc, SM, LangOpts);
246
247 auto CreatePosition = [&SM](SourceLocation Loc) {
Haojian Wuc5340012018-04-30 11:40:02 +0000248 auto LSPLoc = sourceLocToPosition(SM, Loc);
Haojian Wu545c02a2018-04-13 08:30:39 +0000249 SymbolLocation::Position Pos;
Haojian Wuc5340012018-04-30 11:40:02 +0000250 Pos.Line = LSPLoc.line;
251 Pos.Column = LSPLoc.character;
Haojian Wu545c02a2018-04-13 08:30:39 +0000252 return Pos;
253 };
254
255 Result.Start = CreatePosition(NameLoc);
256 auto EndLoc = NameLoc.getLocWithOffset(TokenLength);
257 Result.End = CreatePosition(EndLoc);
258
Sam McCall60039512018-02-09 14:42:01 +0000259 return std::move(Result);
Haojian Wub0189062018-01-31 12:56:51 +0000260}
261
Eric Liucf8601b2018-02-28 09:33:15 +0000262// Checks whether \p ND is a definition of a TagDecl (class/struct/enum/union)
263// in a header file, in which case clangd would prefer to use ND as a canonical
264// declaration.
265// FIXME: handle symbol types that are not TagDecl (e.g. functions), if using
Fangrui Song943e12e2018-03-29 20:03:16 +0000266// the first seen declaration as canonical declaration is not a good enough
Eric Liucf8601b2018-02-28 09:33:15 +0000267// heuristic.
268bool isPreferredDeclaration(const NamedDecl &ND, index::SymbolRoleSet Roles) {
269 using namespace clang::ast_matchers;
270 return (Roles & static_cast<unsigned>(index::SymbolRole::Definition)) &&
271 llvm::isa<TagDecl>(&ND) &&
272 match(decl(isExpansionInMainFile()), ND, ND.getASTContext()).empty();
273}
274
Haojian Wu4c1394d2017-12-12 15:42:10 +0000275} // namespace
276
Eric Liu9af958f2018-01-10 14:57:58 +0000277SymbolCollector::SymbolCollector(Options Opts) : Opts(std::move(Opts)) {}
278
Eric Liu76f6b442018-01-09 17:32:00 +0000279void SymbolCollector::initialize(ASTContext &Ctx) {
280 ASTCtx = &Ctx;
281 CompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>();
282 CompletionTUInfo =
283 llvm::make_unique<CodeCompletionTUInfo>(CompletionAllocator);
284}
285
Haojian Wu4c1394d2017-12-12 15:42:10 +0000286// Always return true to continue indexing.
287bool SymbolCollector::handleDeclOccurence(
288 const Decl *D, index::SymbolRoleSet Roles,
Sam McCallb9d57112018-04-09 14:28:52 +0000289 ArrayRef<index::SymbolRelation> Relations, SourceLocation Loc,
Haojian Wu4c1394d2017-12-12 15:42:10 +0000290 index::IndexDataConsumer::ASTNodeInfo ASTNode) {
Eric Liu9af958f2018-01-10 14:57:58 +0000291 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
Sam McCall93f99bf2018-03-12 14:49:09 +0000292 assert(CompletionAllocator && CompletionTUInfo);
Eric Liu77d18112018-06-04 11:31:55 +0000293 assert(ASTNode.OrigD);
294 // If OrigD is an declaration associated with a friend declaration and it's
295 // not a definition, skip it. Note that OrigD is the occurrence that the
296 // collector is currently visiting.
297 if ((ASTNode.OrigD->getFriendObjectKind() !=
298 Decl::FriendObjectKind::FOK_None) &&
299 !(Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
300 return true;
301 // A declaration created for a friend declaration should not be used as the
302 // canonical declaration in the index. Use OrigD instead, unless we've already
303 // picked a replacement for D
304 if (D->getFriendObjectKind() != Decl::FriendObjectKind::FOK_None)
305 D = CanonicalDecls.try_emplace(D, ASTNode.OrigD).first->second;
Sam McCall93f99bf2018-03-12 14:49:09 +0000306 const NamedDecl *ND = llvm::dyn_cast<NamedDecl>(D);
307 if (!ND)
308 return true;
Eric Liu9af958f2018-01-10 14:57:58 +0000309
Sam McCall93f99bf2018-03-12 14:49:09 +0000310 // Mark D as referenced if this is a reference coming from the main file.
311 // D may not be an interesting symbol, but it's cheaper to check at the end.
Sam McCallb9d57112018-04-09 14:28:52 +0000312 auto &SM = ASTCtx->getSourceManager();
Sam McCall93f99bf2018-03-12 14:49:09 +0000313 if (Opts.CountReferences &&
314 (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
Sam McCallb9d57112018-04-09 14:28:52 +0000315 SM.getFileID(SM.getSpellingLoc(Loc)) == SM.getMainFileID())
Sam McCall93f99bf2018-03-12 14:49:09 +0000316 ReferencedDecls.insert(ND);
317
318 // Don't continue indexing if this is a mere reference.
Haojian Wu4c1394d2017-12-12 15:42:10 +0000319 if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) ||
320 Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
321 return true;
Sam McCall93f99bf2018-03-12 14:49:09 +0000322 if (shouldFilterDecl(ND, ASTCtx, Opts))
323 return true;
Haojian Wu4c1394d2017-12-12 15:42:10 +0000324
Sam McCall93f99bf2018-03-12 14:49:09 +0000325 llvm::SmallString<128> USR;
326 if (index::generateUSRForDecl(ND, USR))
327 return true;
328 SymbolID ID(USR);
Eric Liu76f6b442018-01-09 17:32:00 +0000329
Sam McCall93f99bf2018-03-12 14:49:09 +0000330 const NamedDecl &OriginalDecl = *cast<NamedDecl>(ASTNode.OrigD);
331 const Symbol *BasicSymbol = Symbols.find(ID);
332 if (!BasicSymbol) // Regardless of role, ND is the canonical declaration.
333 BasicSymbol = addDeclaration(*ND, std::move(ID));
334 else if (isPreferredDeclaration(OriginalDecl, Roles))
335 // If OriginalDecl is preferred, replace the existing canonical
336 // declaration (e.g. a class forward declaration). There should be at most
337 // one duplicate as we expect to see only one preferred declaration per
338 // TU, because in practice they are definitions.
339 BasicSymbol = addDeclaration(OriginalDecl, std::move(ID));
Haojian Wu4c1394d2017-12-12 15:42:10 +0000340
Sam McCall93f99bf2018-03-12 14:49:09 +0000341 if (Roles & static_cast<unsigned>(index::SymbolRole::Definition))
342 addDefinition(OriginalDecl, *BasicSymbol);
Haojian Wu4c1394d2017-12-12 15:42:10 +0000343 return true;
344}
345
Sam McCall93f99bf2018-03-12 14:49:09 +0000346void SymbolCollector::finish() {
347 // At the end of the TU, add 1 to the refcount of the ReferencedDecls.
348 for (const auto *ND : ReferencedDecls) {
349 llvm::SmallString<128> USR;
350 if (!index::generateUSRForDecl(ND, USR))
351 if (const auto *S = Symbols.find(SymbolID(USR))) {
352 Symbol Inc = *S;
353 ++Inc.References;
354 Symbols.insert(Inc);
355 }
356 }
357 ReferencedDecls.clear();
358}
359
Sam McCall60039512018-02-09 14:42:01 +0000360const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND,
361 SymbolID ID) {
Ilya Biryukov43714502018-05-16 12:32:44 +0000362 auto &Ctx = ND.getASTContext();
363 auto &SM = Ctx.getSourceManager();
Sam McCall60039512018-02-09 14:42:01 +0000364
365 std::string QName;
366 llvm::raw_string_ostream OS(QName);
367 PrintingPolicy Policy(ASTCtx->getLangOpts());
368 // Note that inline namespaces are treated as transparent scopes. This
369 // reflects the way they're most commonly used for lookup. Ideally we'd
370 // include them, but at query time it's hard to find all the inline
371 // namespaces to query: the preamble doesn't have a dedicated list.
372 Policy.SuppressUnwrittenScope = true;
373 ND.printQualifiedName(OS, Policy);
374 OS.flush();
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000375 assert(!StringRef(QName).startswith("::"));
Sam McCall60039512018-02-09 14:42:01 +0000376
377 Symbol S;
378 S.ID = std::move(ID);
379 std::tie(S.Scope, S.Name) = splitQualifiedName(QName);
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +0000380
381 S.IsIndexedForCodeCompletion = isIndexedForCodeCompletion(ND, Ctx);
Sam McCall60039512018-02-09 14:42:01 +0000382 S.SymInfo = index::getSymbolInfo(&ND);
383 std::string FileURI;
Haojian Wudc02a3d2018-02-13 09:53:50 +0000384 if (auto DeclLoc =
385 getSymbolLocation(ND, SM, Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000386 S.CanonicalDeclaration = *DeclLoc;
387
388 // Add completion info.
389 // FIXME: we may want to choose a different redecl, or combine from several.
390 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
Ilya Biryukovcf124bd2018-04-13 11:03:07 +0000391 // We use the primary template, as clang does during code completion.
392 CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0);
Sam McCall60039512018-02-09 14:42:01 +0000393 const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
394 *ASTCtx, *PP, CodeCompletionContext::CCC_Name, *CompletionAllocator,
395 *CompletionTUInfo,
Ilya Biryukov43714502018-05-16 12:32:44 +0000396 /*IncludeBriefComments*/ false);
Sam McCall60039512018-02-09 14:42:01 +0000397 std::string Label;
398 std::string SnippetInsertText;
399 std::string IgnoredLabel;
400 std::string PlainInsertText;
401 getLabelAndInsertText(*CCS, &Label, &SnippetInsertText,
402 /*EnableSnippets=*/true);
403 getLabelAndInsertText(*CCS, &IgnoredLabel, &PlainInsertText,
404 /*EnableSnippets=*/false);
405 std::string FilterText = getFilterText(*CCS);
Ilya Biryukov43714502018-05-16 12:32:44 +0000406 std::string Documentation =
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +0000407 formatDocumentation(*CCS, getDocComment(Ctx, SymbolCompletion,
408 /*CommentsFromHeaders=*/true));
Sam McCall60039512018-02-09 14:42:01 +0000409 std::string CompletionDetail = getDetail(*CCS);
410
Eric Liuc5105f92018-02-16 14:15:55 +0000411 std::string Include;
412 if (Opts.CollectIncludePath && shouldCollectIncludePath(S.SymInfo.Kind)) {
413 // Use the expansion location to get the #include header since this is
414 // where the symbol is exposed.
Eric Liub96363d2018-03-01 18:06:40 +0000415 if (auto Header = getIncludeHeader(
416 QName, SM, SM.getExpansionLoc(ND.getLocation()), Opts))
Eric Liuc5105f92018-02-16 14:15:55 +0000417 Include = std::move(*Header);
418 }
Sam McCall60039512018-02-09 14:42:01 +0000419 S.CompletionFilterText = FilterText;
420 S.CompletionLabel = Label;
421 S.CompletionPlainInsertText = PlainInsertText;
422 S.CompletionSnippetInsertText = SnippetInsertText;
423 Symbol::Details Detail;
424 Detail.Documentation = Documentation;
425 Detail.CompletionDetail = CompletionDetail;
Eric Liuc5105f92018-02-16 14:15:55 +0000426 Detail.IncludeHeader = Include;
Sam McCall60039512018-02-09 14:42:01 +0000427 S.Detail = &Detail;
428
429 Symbols.insert(S);
430 return Symbols.find(S.ID);
431}
432
433void SymbolCollector::addDefinition(const NamedDecl &ND,
434 const Symbol &DeclSym) {
435 if (DeclSym.Definition)
436 return;
437 // If we saw some forward declaration, we end up copying the symbol.
438 // This is not ideal, but avoids duplicating the "is this a definition" check
439 // in clang::index. We should only see one definition.
440 Symbol S = DeclSym;
441 std::string FileURI;
442 if (auto DefLoc = getSymbolLocation(ND, ND.getASTContext().getSourceManager(),
Haojian Wudc02a3d2018-02-13 09:53:50 +0000443 Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000444 S.Definition = *DefLoc;
445 Symbols.insert(S);
446}
447
Haojian Wu4c1394d2017-12-12 15:42:10 +0000448} // namespace clangd
449} // namespace clang