blob: 7a83243f8f0d0ac1709108c7326931b549909d47 [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))
Sam McCallbed58852018-07-11 10:35:11 +000055 log("Warning: could not make absolute file: {0}", 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);
Haojian Wu4c1394d2017-12-12 15:42:10 +000078 }
Eric Liu7f247652018-02-06 16:10:35 +000079
Eric Liua0957702018-06-25 11:50:11 +000080 llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/true);
81
Eric Liu7f247652018-02-06 16:10:35 +000082 std::string ErrMsg;
83 for (const auto &Scheme : Opts.URISchemes) {
84 auto U = URI::create(AbsolutePath, Scheme);
85 if (U)
86 return U->toString();
87 ErrMsg += llvm::toString(U.takeError()) + "\n";
88 }
Sam McCallbed58852018-07-11 10:35:11 +000089 log("Failed to create an URI for file {0}: {1}", AbsolutePath, ErrMsg);
Eric Liu7f247652018-02-06 16:10:35 +000090 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 Liuc5105f92018-02-16 14:15:55 +0000133// We only collect #include paths for symbols that are suitable for global code
134// completion, except for namespaces since #include path for a namespace is hard
135// to define.
136bool shouldCollectIncludePath(index::SymbolKind Kind) {
137 using SK = index::SymbolKind;
138 switch (Kind) {
139 case SK::Macro:
140 case SK::Enum:
141 case SK::Struct:
142 case SK::Class:
143 case SK::Union:
144 case SK::TypeAlias:
145 case SK::Using:
146 case SK::Function:
147 case SK::Variable:
148 case SK::EnumConstant:
149 return true;
150 default:
151 return false;
152 }
153}
154
Eric Liu02ce01f2018-02-22 10:14:05 +0000155/// Gets a canonical include (URI of the header or <header> or "header") for
156/// header of \p Loc.
157/// Returns None if fails to get include header for \p Loc.
Eric Liuc5105f92018-02-16 14:15:55 +0000158llvm::Optional<std::string>
Eric Liub96363d2018-03-01 18:06:40 +0000159getIncludeHeader(llvm::StringRef QName, const SourceManager &SM,
160 SourceLocation Loc, const SymbolCollector::Options &Opts) {
Eric Liu3cee95e2018-05-24 14:40:24 +0000161 std::vector<std::string> Headers;
162 // Collect the #include stack.
163 while (true) {
164 if (!Loc.isValid())
165 break;
166 auto FilePath = SM.getFilename(Loc);
167 if (FilePath.empty())
168 break;
169 Headers.push_back(FilePath);
170 if (SM.isInMainFile(Loc))
171 break;
172 Loc = SM.getIncludeLoc(SM.getFileID(Loc));
Eric Liuc5105f92018-02-16 14:15:55 +0000173 }
Eric Liu3cee95e2018-05-24 14:40:24 +0000174 if (Headers.empty())
175 return llvm::None;
176 llvm::StringRef Header = Headers[0];
177 if (Opts.Includes) {
178 Header = Opts.Includes->mapHeader(Headers, QName);
179 if (Header.startswith("<") || Header.startswith("\""))
180 return Header.str();
181 }
182 return toURI(SM, Header, Opts);
Eric Liuc5105f92018-02-16 14:15:55 +0000183}
184
Haojian Wud81e3142018-08-31 12:54:13 +0000185// Return the symbol range of the token at \p TokLoc.
186std::pair<SymbolLocation::Position, SymbolLocation::Position>
187getTokenRange(SourceLocation TokLoc, const SourceManager &SM,
188 const LangOptions &LangOpts) {
189 auto CreatePosition = [&SM](SourceLocation Loc) {
190 auto LSPLoc = sourceLocToPosition(SM, Loc);
191 SymbolLocation::Position Pos;
192 Pos.Line = LSPLoc.line;
193 Pos.Column = LSPLoc.character;
194 return Pos;
195 };
196
197 auto TokenLength = clang::Lexer::MeasureTokenLength(TokLoc, SM, LangOpts);
198 return {CreatePosition(TokLoc),
199 CreatePosition(TokLoc.getLocWithOffset(TokenLength))};
200}
201
202// Return the symbol location of the token at \p TokLoc.
Eric Liu48db19e2018-07-09 15:31:07 +0000203llvm::Optional<SymbolLocation>
204getTokenLocation(SourceLocation TokLoc, const SourceManager &SM,
205 const SymbolCollector::Options &Opts,
206 const clang::LangOptions &LangOpts,
207 std::string &FileURIStorage) {
208 auto U = toURI(SM, SM.getFilename(TokLoc), Opts);
Eric Liu7f247652018-02-06 16:10:35 +0000209 if (!U)
210 return llvm::None;
211 FileURIStorage = std::move(*U);
Sam McCall60039512018-02-09 14:42:01 +0000212 SymbolLocation Result;
213 Result.FileURI = FileURIStorage;
Haojian Wud81e3142018-08-31 12:54:13 +0000214 auto Range = getTokenRange(TokLoc, SM, LangOpts);
215 Result.Start = Range.first;
216 Result.End = Range.second;
Haojian Wu545c02a2018-04-13 08:30:39 +0000217
Sam McCall60039512018-02-09 14:42:01 +0000218 return std::move(Result);
Haojian Wub0189062018-01-31 12:56:51 +0000219}
220
Eric Liucf8601b2018-02-28 09:33:15 +0000221// Checks whether \p ND is a definition of a TagDecl (class/struct/enum/union)
222// in a header file, in which case clangd would prefer to use ND as a canonical
223// declaration.
224// FIXME: handle symbol types that are not TagDecl (e.g. functions), if using
Fangrui Song943e12e2018-03-29 20:03:16 +0000225// the first seen declaration as canonical declaration is not a good enough
Eric Liucf8601b2018-02-28 09:33:15 +0000226// heuristic.
227bool isPreferredDeclaration(const NamedDecl &ND, index::SymbolRoleSet Roles) {
228 using namespace clang::ast_matchers;
229 return (Roles & static_cast<unsigned>(index::SymbolRole::Definition)) &&
230 llvm::isa<TagDecl>(&ND) &&
231 match(decl(isExpansionInMainFile()), ND, ND.getASTContext()).empty();
232}
233
Sam McCallb0138312018-09-04 14:39:56 +0000234RefKind toRefKind(index::SymbolRoleSet Roles) {
235 return static_cast<RefKind>(static_cast<unsigned>(RefKind::All) & Roles);
Haojian Wud81e3142018-08-31 12:54:13 +0000236}
237
Haojian Wu4c1394d2017-12-12 15:42:10 +0000238} // namespace
239
Eric Liu9af958f2018-01-10 14:57:58 +0000240SymbolCollector::SymbolCollector(Options Opts) : Opts(std::move(Opts)) {}
241
Eric Liu76f6b442018-01-09 17:32:00 +0000242void SymbolCollector::initialize(ASTContext &Ctx) {
243 ASTCtx = &Ctx;
244 CompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>();
245 CompletionTUInfo =
246 llvm::make_unique<CodeCompletionTUInfo>(CompletionAllocator);
247}
248
Eric Liu8763e482018-06-21 12:12:26 +0000249bool SymbolCollector::shouldCollectSymbol(const NamedDecl &ND,
250 ASTContext &ASTCtx,
251 const Options &Opts) {
252 using namespace clang::ast_matchers;
253 if (ND.isImplicit())
254 return false;
255 // Skip anonymous declarations, e.g (anonymous enum/class/struct).
256 if (ND.getDeclName().isEmpty())
257 return false;
258
259 // FIXME: figure out a way to handle internal linkage symbols (e.g. static
260 // variables, function) defined in the .cc files. Also we skip the symbols
261 // in anonymous namespace as the qualifier names of these symbols are like
262 // `foo::<anonymous>::bar`, which need a special handling.
263 // In real world projects, we have a relatively large set of header files
264 // that define static variables (like "static const int A = 1;"), we still
265 // want to collect these symbols, although they cause potential ODR
266 // violations.
267 if (ND.isInAnonymousNamespace())
268 return false;
269
270 // We want most things but not "local" symbols such as symbols inside
271 // FunctionDecl, BlockDecl, ObjCMethodDecl and OMPDeclareReductionDecl.
272 // FIXME: Need a matcher for ExportDecl in order to include symbols declared
273 // within an export.
274 auto InNonLocalContext = hasDeclContext(anyOf(
275 translationUnitDecl(), namespaceDecl(), linkageSpecDecl(), recordDecl(),
276 enumDecl(), objcProtocolDecl(), objcInterfaceDecl(), objcCategoryDecl(),
277 objcCategoryImplDecl(), objcImplementationDecl()));
278 // Don't index template specializations and expansions in main files.
279 auto IsSpecialization =
280 anyOf(functionDecl(isExplicitTemplateSpecialization()),
281 cxxRecordDecl(isExplicitTemplateSpecialization()),
282 varDecl(isExplicitTemplateSpecialization()));
283 if (match(decl(allOf(unless(isExpansionInMainFile()), InNonLocalContext,
284 unless(IsSpecialization))),
285 ND, ASTCtx)
286 .empty())
287 return false;
288
289 // Avoid indexing internal symbols in protobuf generated headers.
290 if (isPrivateProtoDecl(ND))
291 return false;
292 return true;
293}
294
Haojian Wu4c1394d2017-12-12 15:42:10 +0000295// Always return true to continue indexing.
296bool SymbolCollector::handleDeclOccurence(
297 const Decl *D, index::SymbolRoleSet Roles,
Sam McCallb9d57112018-04-09 14:28:52 +0000298 ArrayRef<index::SymbolRelation> Relations, SourceLocation Loc,
Haojian Wu4c1394d2017-12-12 15:42:10 +0000299 index::IndexDataConsumer::ASTNodeInfo ASTNode) {
Eric Liu9af958f2018-01-10 14:57:58 +0000300 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
Sam McCall93f99bf2018-03-12 14:49:09 +0000301 assert(CompletionAllocator && CompletionTUInfo);
Eric Liu77d18112018-06-04 11:31:55 +0000302 assert(ASTNode.OrigD);
303 // If OrigD is an declaration associated with a friend declaration and it's
304 // not a definition, skip it. Note that OrigD is the occurrence that the
305 // collector is currently visiting.
306 if ((ASTNode.OrigD->getFriendObjectKind() !=
307 Decl::FriendObjectKind::FOK_None) &&
308 !(Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
309 return true;
310 // A declaration created for a friend declaration should not be used as the
311 // canonical declaration in the index. Use OrigD instead, unless we've already
312 // picked a replacement for D
313 if (D->getFriendObjectKind() != Decl::FriendObjectKind::FOK_None)
314 D = CanonicalDecls.try_emplace(D, ASTNode.OrigD).first->second;
Sam McCall93f99bf2018-03-12 14:49:09 +0000315 const NamedDecl *ND = llvm::dyn_cast<NamedDecl>(D);
316 if (!ND)
317 return true;
Eric Liu9af958f2018-01-10 14:57:58 +0000318
Sam McCall93f99bf2018-03-12 14:49:09 +0000319 // Mark D as referenced if this is a reference coming from the main file.
320 // D may not be an interesting symbol, but it's cheaper to check at the end.
Sam McCallb9d57112018-04-09 14:28:52 +0000321 auto &SM = ASTCtx->getSourceManager();
Haojian Wud81e3142018-08-31 12:54:13 +0000322 auto SpellingLoc = SM.getSpellingLoc(Loc);
Sam McCall93f99bf2018-03-12 14:49:09 +0000323 if (Opts.CountReferences &&
324 (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
Haojian Wud81e3142018-08-31 12:54:13 +0000325 SM.getFileID(SpellingLoc) == SM.getMainFileID())
Sam McCall93f99bf2018-03-12 14:49:09 +0000326 ReferencedDecls.insert(ND);
327
Sam McCallb0138312018-09-04 14:39:56 +0000328 if ((static_cast<unsigned>(Opts.RefFilter) & Roles) &&
Haojian Wud81e3142018-08-31 12:54:13 +0000329 SM.getFileID(SpellingLoc) == SM.getMainFileID())
Sam McCallb0138312018-09-04 14:39:56 +0000330 DeclRefs[ND].emplace_back(SpellingLoc, Roles);
Haojian Wud81e3142018-08-31 12:54:13 +0000331
Sam McCall93f99bf2018-03-12 14:49:09 +0000332 // Don't continue indexing if this is a mere reference.
Haojian Wu4c1394d2017-12-12 15:42:10 +0000333 if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) ||
334 Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
335 return true;
Eric Liu8763e482018-06-21 12:12:26 +0000336 if (!shouldCollectSymbol(*ND, *ASTCtx, Opts))
Sam McCall93f99bf2018-03-12 14:49:09 +0000337 return true;
Haojian Wu4c1394d2017-12-12 15:42:10 +0000338
Haojian Wuc6ddb462018-08-07 08:57:52 +0000339 auto ID = getSymbolID(ND);
340 if (!ID)
Sam McCall93f99bf2018-03-12 14:49:09 +0000341 return true;
Eric Liu76f6b442018-01-09 17:32:00 +0000342
Sam McCall93f99bf2018-03-12 14:49:09 +0000343 const NamedDecl &OriginalDecl = *cast<NamedDecl>(ASTNode.OrigD);
Haojian Wuc6ddb462018-08-07 08:57:52 +0000344 const Symbol *BasicSymbol = Symbols.find(*ID);
Sam McCall93f99bf2018-03-12 14:49:09 +0000345 if (!BasicSymbol) // Regardless of role, ND is the canonical declaration.
Haojian Wuc6ddb462018-08-07 08:57:52 +0000346 BasicSymbol = addDeclaration(*ND, std::move(*ID));
Sam McCall93f99bf2018-03-12 14:49:09 +0000347 else if (isPreferredDeclaration(OriginalDecl, Roles))
348 // If OriginalDecl is preferred, replace the existing canonical
349 // declaration (e.g. a class forward declaration). There should be at most
350 // one duplicate as we expect to see only one preferred declaration per
351 // TU, because in practice they are definitions.
Haojian Wuc6ddb462018-08-07 08:57:52 +0000352 BasicSymbol = addDeclaration(OriginalDecl, std::move(*ID));
Haojian Wu4c1394d2017-12-12 15:42:10 +0000353
Sam McCall93f99bf2018-03-12 14:49:09 +0000354 if (Roles & static_cast<unsigned>(index::SymbolRole::Definition))
355 addDefinition(OriginalDecl, *BasicSymbol);
Haojian Wu4c1394d2017-12-12 15:42:10 +0000356 return true;
357}
358
Eric Liu48db19e2018-07-09 15:31:07 +0000359bool SymbolCollector::handleMacroOccurence(const IdentifierInfo *Name,
360 const MacroInfo *MI,
361 index::SymbolRoleSet Roles,
362 SourceLocation Loc) {
363 if (!Opts.CollectMacro)
364 return true;
365 assert(PP.get());
366
367 const auto &SM = PP->getSourceManager();
368 if (SM.isInMainFile(SM.getExpansionLoc(MI->getDefinitionLoc())))
369 return true;
370 // Header guards are not interesting in index. Builtin macros don't have
371 // useful locations and are not needed for code completions.
372 if (MI->isUsedForHeaderGuard() || MI->isBuiltinMacro())
373 return true;
374
375 // Mark the macro as referenced if this is a reference coming from the main
376 // file. The macro may not be an interesting symbol, but it's cheaper to check
377 // at the end.
378 if (Opts.CountReferences &&
379 (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
380 SM.getFileID(SM.getSpellingLoc(Loc)) == SM.getMainFileID())
381 ReferencedMacros.insert(Name);
382 // Don't continue indexing if this is a mere reference.
383 // FIXME: remove macro with ID if it is undefined.
384 if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) ||
385 Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
386 return true;
387
Eric Liu48db19e2018-07-09 15:31:07 +0000388 llvm::SmallString<128> USR;
389 if (index::generateUSRForMacro(Name->getName(), MI->getDefinitionLoc(), SM,
390 USR))
391 return true;
392 SymbolID ID(USR);
393
394 // Only collect one instance in case there are multiple.
395 if (Symbols.find(ID) != nullptr)
396 return true;
397
398 Symbol S;
399 S.ID = std::move(ID);
400 S.Name = Name->getName();
401 S.IsIndexedForCodeCompletion = true;
402 S.SymInfo = index::getSymbolInfoForMacro(*MI);
403 std::string FileURI;
404 if (auto DeclLoc = getTokenLocation(MI->getDefinitionLoc(), SM, Opts,
405 PP->getLangOpts(), FileURI))
406 S.CanonicalDeclaration = *DeclLoc;
407
408 CodeCompletionResult SymbolCompletion(Name);
409 const auto *CCS = SymbolCompletion.CreateCodeCompletionStringForMacro(
410 *PP, *CompletionAllocator, *CompletionTUInfo);
411 std::string Signature;
412 std::string SnippetSuffix;
413 getSignature(*CCS, &Signature, &SnippetSuffix);
414
415 std::string Include;
416 if (Opts.CollectIncludePath && shouldCollectIncludePath(S.SymInfo.Kind)) {
417 if (auto Header =
418 getIncludeHeader(Name->getName(), SM,
419 SM.getExpansionLoc(MI->getDefinitionLoc()), Opts))
420 Include = std::move(*Header);
421 }
422 S.Signature = Signature;
423 S.CompletionSnippetSuffix = SnippetSuffix;
Eric Liu83f63e42018-09-03 10:18:21 +0000424 if (!Include.empty())
425 S.IncludeHeaders.emplace_back(Include, 1);
426
Eric Liu48db19e2018-07-09 15:31:07 +0000427 Symbols.insert(S);
428 return true;
429}
430
Sam McCall93f99bf2018-03-12 14:49:09 +0000431void SymbolCollector::finish() {
Eric Liu48db19e2018-07-09 15:31:07 +0000432 // At the end of the TU, add 1 to the refcount of all referenced symbols.
433 auto IncRef = [this](const SymbolID &ID) {
434 if (const auto *S = Symbols.find(ID)) {
435 Symbol Inc = *S;
436 ++Inc.References;
437 Symbols.insert(Inc);
438 }
439 };
440 for (const NamedDecl *ND : ReferencedDecls) {
Haojian Wuc6ddb462018-08-07 08:57:52 +0000441 if (auto ID = getSymbolID(ND)) {
442 IncRef(*ID);
443 }
Eric Liu48db19e2018-07-09 15:31:07 +0000444 }
445 if (Opts.CollectMacro) {
446 assert(PP);
447 for (const IdentifierInfo *II : ReferencedMacros) {
448 llvm::SmallString<128> USR;
Eric Liua62c9d62018-07-09 18:54:51 +0000449 if (const auto *MI = PP->getMacroDefinition(II).getMacroInfo())
450 if (!index::generateUSRForMacro(II->getName(), MI->getDefinitionLoc(),
451 PP->getSourceManager(), USR))
452 IncRef(SymbolID(USR));
Eric Liu48db19e2018-07-09 15:31:07 +0000453 }
Sam McCall93f99bf2018-03-12 14:49:09 +0000454 }
Haojian Wud81e3142018-08-31 12:54:13 +0000455
456 const auto &SM = ASTCtx->getSourceManager();
457 auto* MainFileEntry = SM.getFileEntryForID(SM.getMainFileID());
458
459 if (auto MainFileURI = toURI(SM, MainFileEntry->getName(), Opts)) {
460 std::string MainURI = *MainFileURI;
Sam McCallb0138312018-09-04 14:39:56 +0000461 for (const auto &It : DeclRefs) {
Haojian Wud81e3142018-08-31 12:54:13 +0000462 if (auto ID = getSymbolID(It.first)) {
463 if (Symbols.find(*ID)) {
464 for (const auto &LocAndRole : It.second) {
Sam McCallb0138312018-09-04 14:39:56 +0000465 Ref R;
Haojian Wud81e3142018-08-31 12:54:13 +0000466 auto Range =
467 getTokenRange(LocAndRole.first, SM, ASTCtx->getLangOpts());
Sam McCallb0138312018-09-04 14:39:56 +0000468 R.Location.Start = Range.first;
469 R.Location.End = Range.second;
470 R.Location.FileURI = MainURI;
471 R.Kind = toRefKind(LocAndRole.second);
472 Refs.insert(*ID, R);
Haojian Wud81e3142018-08-31 12:54:13 +0000473 }
474 }
475 }
476 }
477 } else {
478 log("Failed to create URI for main file: {0}", MainFileEntry->getName());
479 }
480
Sam McCall93f99bf2018-03-12 14:49:09 +0000481 ReferencedDecls.clear();
Eric Liu48db19e2018-07-09 15:31:07 +0000482 ReferencedMacros.clear();
Sam McCallb0138312018-09-04 14:39:56 +0000483 DeclRefs.clear();
Sam McCall93f99bf2018-03-12 14:49:09 +0000484}
485
Sam McCall60039512018-02-09 14:42:01 +0000486const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND,
487 SymbolID ID) {
Ilya Biryukov43714502018-05-16 12:32:44 +0000488 auto &Ctx = ND.getASTContext();
489 auto &SM = Ctx.getSourceManager();
Sam McCall60039512018-02-09 14:42:01 +0000490
Sam McCall60039512018-02-09 14:42:01 +0000491 Symbol S;
492 S.ID = std::move(ID);
Eric Liu7ad16962018-06-22 10:46:59 +0000493 std::string QName = printQualifiedName(ND);
Sam McCall60039512018-02-09 14:42:01 +0000494 std::tie(S.Scope, S.Name) = splitQualifiedName(QName);
Sam McCall032db942018-06-22 06:41:43 +0000495 // FIXME: this returns foo:bar: for objective-C methods, we prefer only foo:
496 // for consistency with CodeCompletionString and a clean name/signature split.
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +0000497
498 S.IsIndexedForCodeCompletion = isIndexedForCodeCompletion(ND, Ctx);
Sam McCall60039512018-02-09 14:42:01 +0000499 S.SymInfo = index::getSymbolInfo(&ND);
500 std::string FileURI;
Eric Liu48db19e2018-07-09 15:31:07 +0000501 if (auto DeclLoc = getTokenLocation(findNameLoc(&ND), SM, Opts,
502 ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000503 S.CanonicalDeclaration = *DeclLoc;
504
505 // Add completion info.
506 // FIXME: we may want to choose a different redecl, or combine from several.
507 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
Ilya Biryukovcf124bd2018-04-13 11:03:07 +0000508 // We use the primary template, as clang does during code completion.
509 CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0);
Sam McCall60039512018-02-09 14:42:01 +0000510 const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
511 *ASTCtx, *PP, CodeCompletionContext::CCC_Name, *CompletionAllocator,
512 *CompletionTUInfo,
Ilya Biryukov43714502018-05-16 12:32:44 +0000513 /*IncludeBriefComments*/ false);
Sam McCalla68951e2018-06-22 16:11:35 +0000514 std::string Signature;
515 std::string SnippetSuffix;
516 getSignature(*CCS, &Signature, &SnippetSuffix);
Ilya Biryukov43714502018-05-16 12:32:44 +0000517 std::string Documentation =
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +0000518 formatDocumentation(*CCS, getDocComment(Ctx, SymbolCompletion,
519 /*CommentsFromHeaders=*/true));
Sam McCalla68951e2018-06-22 16:11:35 +0000520 std::string ReturnType = getReturnType(*CCS);
Sam McCall60039512018-02-09 14:42:01 +0000521
Eric Liuc5105f92018-02-16 14:15:55 +0000522 std::string Include;
523 if (Opts.CollectIncludePath && shouldCollectIncludePath(S.SymInfo.Kind)) {
524 // Use the expansion location to get the #include header since this is
525 // where the symbol is exposed.
Eric Liub96363d2018-03-01 18:06:40 +0000526 if (auto Header = getIncludeHeader(
527 QName, SM, SM.getExpansionLoc(ND.getLocation()), Opts))
Eric Liuc5105f92018-02-16 14:15:55 +0000528 Include = std::move(*Header);
529 }
Sam McCalla68951e2018-06-22 16:11:35 +0000530 S.Signature = Signature;
531 S.CompletionSnippetSuffix = SnippetSuffix;
Sam McCall2e5700f2018-08-31 13:55:01 +0000532 S.Documentation = Documentation;
533 S.ReturnType = ReturnType;
Eric Liu83f63e42018-09-03 10:18:21 +0000534 if (!Include.empty())
535 S.IncludeHeaders.emplace_back(Include, 1);
Sam McCall60039512018-02-09 14:42:01 +0000536
Sam McCall2161ec72018-07-05 06:20:41 +0000537 S.Origin = Opts.Origin;
Sam McCall60039512018-02-09 14:42:01 +0000538 Symbols.insert(S);
539 return Symbols.find(S.ID);
540}
541
542void SymbolCollector::addDefinition(const NamedDecl &ND,
543 const Symbol &DeclSym) {
544 if (DeclSym.Definition)
545 return;
546 // If we saw some forward declaration, we end up copying the symbol.
547 // This is not ideal, but avoids duplicating the "is this a definition" check
548 // in clang::index. We should only see one definition.
549 Symbol S = DeclSym;
550 std::string FileURI;
Eric Liu48db19e2018-07-09 15:31:07 +0000551 if (auto DefLoc = getTokenLocation(findNameLoc(&ND),
552 ND.getASTContext().getSourceManager(),
553 Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000554 S.Definition = *DefLoc;
555 Symbols.insert(S);
556}
557
Haojian Wu4c1394d2017-12-12 15:42:10 +0000558} // namespace clangd
559} // namespace clang