blob: 26941d2dcc29d3b8ca3dd57fa06423fe566a6a8c [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"
Eric Liuf7688682018-09-07 09:40:36 +000011#include "AST.h"
Eric Liuc5105f92018-02-16 14:15:55 +000012#include "CanonicalIncludes.h"
Eric Liuf7688682018-09-07 09:40:36 +000013#include "CodeComplete.h"
14#include "CodeCompletionStrings.h"
15#include "Logger.h"
16#include "SourceCode.h"
17#include "URI.h"
Eric Liua57afd02018-09-17 07:43:49 +000018#include "clang/AST/Decl.h"
19#include "clang/AST/DeclBase.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000020#include "clang/AST/DeclCXX.h"
Ilya Biryukovcf124bd2018-04-13 11:03:07 +000021#include "clang/AST/DeclTemplate.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000022#include "clang/Basic/SourceManager.h"
Eric Liua57afd02018-09-17 07:43:49 +000023#include "clang/Basic/Specifiers.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000024#include "clang/Index/IndexSymbol.h"
25#include "clang/Index/USRGeneration.h"
Eric Liua57afd02018-09-17 07:43:49 +000026#include "llvm/Support/Casting.h"
Eric Liu278e2d12018-01-29 15:13:29 +000027#include "llvm/Support/FileSystem.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000028#include "llvm/Support/MemoryBuffer.h"
29#include "llvm/Support/Path.h"
30
31namespace clang {
32namespace clangd {
33
34namespace {
Ilya Biryukovf118d512018-04-14 16:27:35 +000035/// If \p ND is a template specialization, returns the described template.
Ilya Biryukovcf124bd2018-04-13 11:03:07 +000036/// Otherwise, returns \p ND.
37const NamedDecl &getTemplateOrThis(const NamedDecl &ND) {
Ilya Biryukovf118d512018-04-14 16:27:35 +000038 if (auto T = ND.getDescribedTemplate())
39 return *T;
Ilya Biryukovcf124bd2018-04-13 11:03:07 +000040 return ND;
41}
42
Eric Liu7f247652018-02-06 16:10:35 +000043// Returns a URI of \p Path. Firstly, this makes the \p Path absolute using the
44// current working directory of the given SourceManager if the Path is not an
45// absolute path. If failed, this resolves relative paths against \p FallbackDir
46// to get an absolute path. Then, this tries creating an URI for the absolute
47// path with schemes specified in \p Opts. This returns an URI with the first
48// working scheme, if there is any; otherwise, this returns None.
Haojian Wu4c1394d2017-12-12 15:42:10 +000049//
50// The Path can be a path relative to the build directory, or retrieved from
51// the SourceManager.
Eric Liu7f247652018-02-06 16:10:35 +000052llvm::Optional<std::string> toURI(const SourceManager &SM, StringRef Path,
53 const SymbolCollector::Options &Opts) {
Haojian Wu4c1394d2017-12-12 15:42:10 +000054 llvm::SmallString<128> AbsolutePath(Path);
55 if (std::error_code EC =
56 SM.getFileManager().getVirtualFileSystem()->makeAbsolute(
57 AbsolutePath))
Sam McCallbed58852018-07-11 10:35:11 +000058 log("Warning: could not make absolute file: {0}", EC.message());
Eric Liu278e2d12018-01-29 15:13:29 +000059 if (llvm::sys::path::is_absolute(AbsolutePath)) {
60 // Handle the symbolic link path case where the current working directory
61 // (getCurrentWorkingDirectory) is a symlink./ We always want to the real
62 // file path (instead of the symlink path) for the C++ symbols.
63 //
64 // Consider the following example:
65 //
66 // src dir: /project/src/foo.h
67 // current working directory (symlink): /tmp/build -> /project/src/
68 //
69 // The file path of Symbol is "/project/src/foo.h" instead of
70 // "/tmp/build/foo.h"
71 if (const DirectoryEntry *Dir = SM.getFileManager().getDirectory(
72 llvm::sys::path::parent_path(AbsolutePath.str()))) {
73 StringRef DirName = SM.getFileManager().getCanonicalName(Dir);
74 SmallString<128> AbsoluteFilename;
75 llvm::sys::path::append(AbsoluteFilename, DirName,
76 llvm::sys::path::filename(AbsolutePath.str()));
77 AbsolutePath = AbsoluteFilename;
78 }
Eric Liu7f247652018-02-06 16:10:35 +000079 } else if (!Opts.FallbackDir.empty()) {
80 llvm::sys::fs::make_absolute(Opts.FallbackDir, AbsolutePath);
Haojian Wu4c1394d2017-12-12 15:42:10 +000081 }
Eric Liu7f247652018-02-06 16:10:35 +000082
Eric Liua0957702018-06-25 11:50:11 +000083 llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/true);
84
Eric Liu7f247652018-02-06 16:10:35 +000085 std::string ErrMsg;
86 for (const auto &Scheme : Opts.URISchemes) {
87 auto U = URI::create(AbsolutePath, Scheme);
88 if (U)
89 return U->toString();
90 ErrMsg += llvm::toString(U.takeError()) + "\n";
91 }
Sam McCallbed58852018-07-11 10:35:11 +000092 log("Failed to create an URI for file {0}: {1}", AbsolutePath, ErrMsg);
Eric Liu7f247652018-02-06 16:10:35 +000093 return llvm::None;
Haojian Wu4c1394d2017-12-12 15:42:10 +000094}
Eric Liu4feda802017-12-19 11:37:40 +000095
Eric Liud67ec242018-05-16 12:12:30 +000096// All proto generated headers should start with this line.
97static const char *PROTO_HEADER_COMMENT =
98 "// Generated by the protocol buffer compiler. DO NOT EDIT!";
99
100// Checks whether the decl is a private symbol in a header generated by
101// protobuf compiler.
102// To identify whether a proto header is actually generated by proto compiler,
103// we check whether it starts with PROTO_HEADER_COMMENT.
104// FIXME: make filtering extensible when there are more use cases for symbol
105// filters.
106bool isPrivateProtoDecl(const NamedDecl &ND) {
107 const auto &SM = ND.getASTContext().getSourceManager();
108 auto Loc = findNameLoc(&ND);
109 auto FileName = SM.getFilename(Loc);
110 if (!FileName.endswith(".proto.h") && !FileName.endswith(".pb.h"))
111 return false;
112 auto FID = SM.getFileID(Loc);
113 // Double check that this is an actual protobuf header.
114 if (!SM.getBufferData(FID).startswith(PROTO_HEADER_COMMENT))
115 return false;
116
117 // ND without identifier can be operators.
118 if (ND.getIdentifier() == nullptr)
119 return false;
120 auto Name = ND.getIdentifier()->getName();
121 if (!Name.contains('_'))
122 return false;
123 // Nested proto entities (e.g. Message::Nested) have top-level decls
124 // that shouldn't be used (Message_Nested). Ignore them completely.
125 // The nested entities are dangling type aliases, we may want to reconsider
126 // including them in the future.
127 // For enum constants, SOME_ENUM_CONSTANT is not private and should be
128 // indexed. Outer_INNER is private. This heuristic relies on naming style, it
129 // will include OUTER_INNER and exclude some_enum_constant.
130 // FIXME: the heuristic relies on naming style (i.e. no underscore in
131 // user-defined names) and can be improved.
Kirill Bobyrev4a5ff882018-10-07 14:49:41 +0000132 return (ND.getKind() != Decl::EnumConstant) || llvm::any_of(Name, islower);
Eric Liud67ec242018-05-16 12:12:30 +0000133}
134
Eric Liuc5105f92018-02-16 14:15:55 +0000135// We only collect #include paths for symbols that are suitable for global code
136// completion, except for namespaces since #include path for a namespace is hard
137// to define.
138bool shouldCollectIncludePath(index::SymbolKind Kind) {
139 using SK = index::SymbolKind;
140 switch (Kind) {
141 case SK::Macro:
142 case SK::Enum:
143 case SK::Struct:
144 case SK::Class:
145 case SK::Union:
146 case SK::TypeAlias:
147 case SK::Using:
148 case SK::Function:
149 case SK::Variable:
150 case SK::EnumConstant:
151 return true;
152 default:
153 return false;
154 }
155}
156
Eric Liu02ce01f2018-02-22 10:14:05 +0000157/// Gets a canonical include (URI of the header or <header> or "header") for
158/// header of \p Loc.
159/// Returns None if fails to get include header for \p Loc.
Eric Liuc5105f92018-02-16 14:15:55 +0000160llvm::Optional<std::string>
Eric Liub96363d2018-03-01 18:06:40 +0000161getIncludeHeader(llvm::StringRef QName, const SourceManager &SM,
162 SourceLocation Loc, const SymbolCollector::Options &Opts) {
Eric Liu3cee95e2018-05-24 14:40:24 +0000163 std::vector<std::string> Headers;
164 // Collect the #include stack.
165 while (true) {
166 if (!Loc.isValid())
167 break;
168 auto FilePath = SM.getFilename(Loc);
169 if (FilePath.empty())
170 break;
171 Headers.push_back(FilePath);
172 if (SM.isInMainFile(Loc))
173 break;
174 Loc = SM.getIncludeLoc(SM.getFileID(Loc));
Eric Liuc5105f92018-02-16 14:15:55 +0000175 }
Eric Liu3cee95e2018-05-24 14:40:24 +0000176 if (Headers.empty())
177 return llvm::None;
178 llvm::StringRef Header = Headers[0];
179 if (Opts.Includes) {
180 Header = Opts.Includes->mapHeader(Headers, QName);
181 if (Header.startswith("<") || Header.startswith("\""))
182 return Header.str();
183 }
184 return toURI(SM, Header, Opts);
Eric Liuc5105f92018-02-16 14:15:55 +0000185}
186
Haojian Wud81e3142018-08-31 12:54:13 +0000187// Return the symbol range of the token at \p TokLoc.
188std::pair<SymbolLocation::Position, SymbolLocation::Position>
189getTokenRange(SourceLocation TokLoc, const SourceManager &SM,
190 const LangOptions &LangOpts) {
191 auto CreatePosition = [&SM](SourceLocation Loc) {
192 auto LSPLoc = sourceLocToPosition(SM, Loc);
193 SymbolLocation::Position Pos;
194 Pos.Line = LSPLoc.line;
195 Pos.Column = LSPLoc.character;
196 return Pos;
197 };
198
199 auto TokenLength = clang::Lexer::MeasureTokenLength(TokLoc, SM, LangOpts);
200 return {CreatePosition(TokLoc),
201 CreatePosition(TokLoc.getLocWithOffset(TokenLength))};
202}
203
204// Return the symbol location of the token at \p TokLoc.
Eric Liu48db19e2018-07-09 15:31:07 +0000205llvm::Optional<SymbolLocation>
206getTokenLocation(SourceLocation TokLoc, const SourceManager &SM,
207 const SymbolCollector::Options &Opts,
208 const clang::LangOptions &LangOpts,
209 std::string &FileURIStorage) {
210 auto U = toURI(SM, SM.getFilename(TokLoc), Opts);
Eric Liu7f247652018-02-06 16:10:35 +0000211 if (!U)
212 return llvm::None;
213 FileURIStorage = std::move(*U);
Sam McCall60039512018-02-09 14:42:01 +0000214 SymbolLocation Result;
215 Result.FileURI = FileURIStorage;
Haojian Wud81e3142018-08-31 12:54:13 +0000216 auto Range = getTokenRange(TokLoc, SM, LangOpts);
217 Result.Start = Range.first;
218 Result.End = Range.second;
Haojian Wu545c02a2018-04-13 08:30:39 +0000219
Sam McCall60039512018-02-09 14:42:01 +0000220 return std::move(Result);
Haojian Wub0189062018-01-31 12:56:51 +0000221}
222
Eric Liucf8601b2018-02-28 09:33:15 +0000223// Checks whether \p ND is a definition of a TagDecl (class/struct/enum/union)
224// in a header file, in which case clangd would prefer to use ND as a canonical
225// declaration.
226// FIXME: handle symbol types that are not TagDecl (e.g. functions), if using
Fangrui Song943e12e2018-03-29 20:03:16 +0000227// the first seen declaration as canonical declaration is not a good enough
Eric Liucf8601b2018-02-28 09:33:15 +0000228// heuristic.
229bool isPreferredDeclaration(const NamedDecl &ND, index::SymbolRoleSet Roles) {
Sam McCall5fb97462018-10-05 14:03:04 +0000230 const auto& SM = ND.getASTContext().getSourceManager();
Eric Liucf8601b2018-02-28 09:33:15 +0000231 return (Roles & static_cast<unsigned>(index::SymbolRole::Definition)) &&
232 llvm::isa<TagDecl>(&ND) &&
Sam McCall5fb97462018-10-05 14:03:04 +0000233 !SM.isWrittenInMainFile(SM.getExpansionLoc(ND.getLocation()));
Eric Liucf8601b2018-02-28 09:33:15 +0000234}
235
Sam McCallb0138312018-09-04 14:39:56 +0000236RefKind toRefKind(index::SymbolRoleSet Roles) {
237 return static_cast<RefKind>(static_cast<unsigned>(RefKind::All) & Roles);
Haojian Wud81e3142018-08-31 12:54:13 +0000238}
239
Eric Liua57afd02018-09-17 07:43:49 +0000240template <class T> bool explicitTemplateSpecialization(const NamedDecl &ND) {
241 if (const auto *TD = llvm::dyn_cast<T>(&ND))
242 if (TD->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
243 return true;
244 return false;
245}
246
Haojian Wu4c1394d2017-12-12 15:42:10 +0000247} // namespace
248
Eric Liu9af958f2018-01-10 14:57:58 +0000249SymbolCollector::SymbolCollector(Options Opts) : Opts(std::move(Opts)) {}
250
Eric Liu76f6b442018-01-09 17:32:00 +0000251void SymbolCollector::initialize(ASTContext &Ctx) {
252 ASTCtx = &Ctx;
253 CompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>();
254 CompletionTUInfo =
255 llvm::make_unique<CodeCompletionTUInfo>(CompletionAllocator);
256}
257
Eric Liu8763e482018-06-21 12:12:26 +0000258bool SymbolCollector::shouldCollectSymbol(const NamedDecl &ND,
259 ASTContext &ASTCtx,
260 const Options &Opts) {
Eric Liu8763e482018-06-21 12:12:26 +0000261 if (ND.isImplicit())
262 return false;
263 // Skip anonymous declarations, e.g (anonymous enum/class/struct).
264 if (ND.getDeclName().isEmpty())
265 return false;
266
267 // FIXME: figure out a way to handle internal linkage symbols (e.g. static
268 // variables, function) defined in the .cc files. Also we skip the symbols
269 // in anonymous namespace as the qualifier names of these symbols are like
270 // `foo::<anonymous>::bar`, which need a special handling.
271 // In real world projects, we have a relatively large set of header files
272 // that define static variables (like "static const int A = 1;"), we still
273 // want to collect these symbols, although they cause potential ODR
274 // violations.
275 if (ND.isInAnonymousNamespace())
276 return false;
277
278 // We want most things but not "local" symbols such as symbols inside
279 // FunctionDecl, BlockDecl, ObjCMethodDecl and OMPDeclareReductionDecl.
280 // FIXME: Need a matcher for ExportDecl in order to include symbols declared
281 // within an export.
Eric Liua57afd02018-09-17 07:43:49 +0000282 const auto *DeclCtx = ND.getDeclContext();
283 switch (DeclCtx->getDeclKind()) {
284 case Decl::TranslationUnit:
285 case Decl::Namespace:
286 case Decl::LinkageSpec:
287 case Decl::Enum:
288 case Decl::ObjCProtocol:
289 case Decl::ObjCInterface:
290 case Decl::ObjCCategory:
291 case Decl::ObjCCategoryImpl:
292 case Decl::ObjCImplementation:
293 break;
294 default:
295 // Record has a few derivations (e.g. CXXRecord, Class specialization), it's
296 // easier to cast.
297 if (!llvm::isa<RecordDecl>(DeclCtx))
298 return false;
299 }
300 if (explicitTemplateSpecialization<FunctionDecl>(ND) ||
301 explicitTemplateSpecialization<CXXRecordDecl>(ND) ||
302 explicitTemplateSpecialization<VarDecl>(ND))
Eric Liu8763e482018-06-21 12:12:26 +0000303 return false;
304
Eric Liua57afd02018-09-17 07:43:49 +0000305 const auto &SM = ASTCtx.getSourceManager();
306 // Skip decls in the main file.
307 if (SM.isInMainFile(SM.getExpansionLoc(ND.getBeginLoc())))
308 return false;
Eric Liu8763e482018-06-21 12:12:26 +0000309 // Avoid indexing internal symbols in protobuf generated headers.
310 if (isPrivateProtoDecl(ND))
311 return false;
312 return true;
313}
314
Haojian Wu4c1394d2017-12-12 15:42:10 +0000315// Always return true to continue indexing.
316bool SymbolCollector::handleDeclOccurence(
317 const Decl *D, index::SymbolRoleSet Roles,
Sam McCallb9d57112018-04-09 14:28:52 +0000318 ArrayRef<index::SymbolRelation> Relations, SourceLocation Loc,
Haojian Wu4c1394d2017-12-12 15:42:10 +0000319 index::IndexDataConsumer::ASTNodeInfo ASTNode) {
Eric Liu9af958f2018-01-10 14:57:58 +0000320 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
Sam McCall93f99bf2018-03-12 14:49:09 +0000321 assert(CompletionAllocator && CompletionTUInfo);
Eric Liu77d18112018-06-04 11:31:55 +0000322 assert(ASTNode.OrigD);
323 // If OrigD is an declaration associated with a friend declaration and it's
324 // not a definition, skip it. Note that OrigD is the occurrence that the
325 // collector is currently visiting.
326 if ((ASTNode.OrigD->getFriendObjectKind() !=
327 Decl::FriendObjectKind::FOK_None) &&
328 !(Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
329 return true;
330 // A declaration created for a friend declaration should not be used as the
331 // canonical declaration in the index. Use OrigD instead, unless we've already
332 // picked a replacement for D
333 if (D->getFriendObjectKind() != Decl::FriendObjectKind::FOK_None)
334 D = CanonicalDecls.try_emplace(D, ASTNode.OrigD).first->second;
Sam McCall93f99bf2018-03-12 14:49:09 +0000335 const NamedDecl *ND = llvm::dyn_cast<NamedDecl>(D);
336 if (!ND)
337 return true;
Eric Liu9af958f2018-01-10 14:57:58 +0000338
Sam McCall93f99bf2018-03-12 14:49:09 +0000339 // Mark D as referenced if this is a reference coming from the main file.
340 // D may not be an interesting symbol, but it's cheaper to check at the end.
Sam McCallb9d57112018-04-09 14:28:52 +0000341 auto &SM = ASTCtx->getSourceManager();
Haojian Wud81e3142018-08-31 12:54:13 +0000342 auto SpellingLoc = SM.getSpellingLoc(Loc);
Sam McCall93f99bf2018-03-12 14:49:09 +0000343 if (Opts.CountReferences &&
344 (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
Haojian Wud81e3142018-08-31 12:54:13 +0000345 SM.getFileID(SpellingLoc) == SM.getMainFileID())
Sam McCall93f99bf2018-03-12 14:49:09 +0000346 ReferencedDecls.insert(ND);
347
Sam McCallb0138312018-09-04 14:39:56 +0000348 if ((static_cast<unsigned>(Opts.RefFilter) & Roles) &&
Haojian Wud81e3142018-08-31 12:54:13 +0000349 SM.getFileID(SpellingLoc) == SM.getMainFileID())
Sam McCallb0138312018-09-04 14:39:56 +0000350 DeclRefs[ND].emplace_back(SpellingLoc, Roles);
Haojian Wud81e3142018-08-31 12:54:13 +0000351
Sam McCall93f99bf2018-03-12 14:49:09 +0000352 // Don't continue indexing if this is a mere reference.
Haojian Wu4c1394d2017-12-12 15:42:10 +0000353 if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) ||
354 Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
355 return true;
Eric Liu8763e482018-06-21 12:12:26 +0000356 if (!shouldCollectSymbol(*ND, *ASTCtx, Opts))
Sam McCall93f99bf2018-03-12 14:49:09 +0000357 return true;
Haojian Wu4c1394d2017-12-12 15:42:10 +0000358
Haojian Wuc6ddb462018-08-07 08:57:52 +0000359 auto ID = getSymbolID(ND);
360 if (!ID)
Sam McCall93f99bf2018-03-12 14:49:09 +0000361 return true;
Eric Liu76f6b442018-01-09 17:32:00 +0000362
Sam McCall93f99bf2018-03-12 14:49:09 +0000363 const NamedDecl &OriginalDecl = *cast<NamedDecl>(ASTNode.OrigD);
Haojian Wuc6ddb462018-08-07 08:57:52 +0000364 const Symbol *BasicSymbol = Symbols.find(*ID);
Sam McCall93f99bf2018-03-12 14:49:09 +0000365 if (!BasicSymbol) // Regardless of role, ND is the canonical declaration.
Haojian Wuc6ddb462018-08-07 08:57:52 +0000366 BasicSymbol = addDeclaration(*ND, std::move(*ID));
Sam McCall93f99bf2018-03-12 14:49:09 +0000367 else if (isPreferredDeclaration(OriginalDecl, Roles))
368 // If OriginalDecl is preferred, replace the existing canonical
369 // declaration (e.g. a class forward declaration). There should be at most
370 // one duplicate as we expect to see only one preferred declaration per
371 // TU, because in practice they are definitions.
Haojian Wuc6ddb462018-08-07 08:57:52 +0000372 BasicSymbol = addDeclaration(OriginalDecl, std::move(*ID));
Haojian Wu4c1394d2017-12-12 15:42:10 +0000373
Sam McCall93f99bf2018-03-12 14:49:09 +0000374 if (Roles & static_cast<unsigned>(index::SymbolRole::Definition))
375 addDefinition(OriginalDecl, *BasicSymbol);
Haojian Wu4c1394d2017-12-12 15:42:10 +0000376 return true;
377}
378
Eric Liu48db19e2018-07-09 15:31:07 +0000379bool SymbolCollector::handleMacroOccurence(const IdentifierInfo *Name,
380 const MacroInfo *MI,
381 index::SymbolRoleSet Roles,
382 SourceLocation Loc) {
383 if (!Opts.CollectMacro)
384 return true;
385 assert(PP.get());
386
387 const auto &SM = PP->getSourceManager();
388 if (SM.isInMainFile(SM.getExpansionLoc(MI->getDefinitionLoc())))
389 return true;
390 // Header guards are not interesting in index. Builtin macros don't have
391 // useful locations and are not needed for code completions.
392 if (MI->isUsedForHeaderGuard() || MI->isBuiltinMacro())
393 return true;
394
395 // Mark the macro as referenced if this is a reference coming from the main
396 // file. The macro may not be an interesting symbol, but it's cheaper to check
397 // at the end.
398 if (Opts.CountReferences &&
399 (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
400 SM.getFileID(SM.getSpellingLoc(Loc)) == SM.getMainFileID())
401 ReferencedMacros.insert(Name);
402 // Don't continue indexing if this is a mere reference.
403 // FIXME: remove macro with ID if it is undefined.
404 if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) ||
405 Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
406 return true;
407
Eric Liud25f1212018-09-06 09:59:37 +0000408 auto ID = getSymbolID(*Name, MI, SM);
409 if (!ID)
Eric Liu48db19e2018-07-09 15:31:07 +0000410 return true;
Eric Liu48db19e2018-07-09 15:31:07 +0000411
412 // Only collect one instance in case there are multiple.
Eric Liud25f1212018-09-06 09:59:37 +0000413 if (Symbols.find(*ID) != nullptr)
Eric Liu48db19e2018-07-09 15:31:07 +0000414 return true;
415
416 Symbol S;
Eric Liud25f1212018-09-06 09:59:37 +0000417 S.ID = std::move(*ID);
Eric Liu48db19e2018-07-09 15:31:07 +0000418 S.Name = Name->getName();
Eric Liu6df66002018-09-06 18:52:26 +0000419 S.Flags |= Symbol::IndexedForCodeCompletion;
Eric Liu48db19e2018-07-09 15:31:07 +0000420 S.SymInfo = index::getSymbolInfoForMacro(*MI);
421 std::string FileURI;
422 if (auto DeclLoc = getTokenLocation(MI->getDefinitionLoc(), SM, Opts,
423 PP->getLangOpts(), FileURI))
424 S.CanonicalDeclaration = *DeclLoc;
425
426 CodeCompletionResult SymbolCompletion(Name);
427 const auto *CCS = SymbolCompletion.CreateCodeCompletionStringForMacro(
428 *PP, *CompletionAllocator, *CompletionTUInfo);
429 std::string Signature;
430 std::string SnippetSuffix;
431 getSignature(*CCS, &Signature, &SnippetSuffix);
432
433 std::string Include;
434 if (Opts.CollectIncludePath && shouldCollectIncludePath(S.SymInfo.Kind)) {
435 if (auto Header =
436 getIncludeHeader(Name->getName(), SM,
437 SM.getExpansionLoc(MI->getDefinitionLoc()), Opts))
438 Include = std::move(*Header);
439 }
440 S.Signature = Signature;
441 S.CompletionSnippetSuffix = SnippetSuffix;
Eric Liu83f63e42018-09-03 10:18:21 +0000442 if (!Include.empty())
443 S.IncludeHeaders.emplace_back(Include, 1);
444
Eric Liu48db19e2018-07-09 15:31:07 +0000445 Symbols.insert(S);
446 return true;
447}
448
Sam McCall93f99bf2018-03-12 14:49:09 +0000449void SymbolCollector::finish() {
Eric Liu48db19e2018-07-09 15:31:07 +0000450 // At the end of the TU, add 1 to the refcount of all referenced symbols.
451 auto IncRef = [this](const SymbolID &ID) {
452 if (const auto *S = Symbols.find(ID)) {
453 Symbol Inc = *S;
454 ++Inc.References;
455 Symbols.insert(Inc);
456 }
457 };
458 for (const NamedDecl *ND : ReferencedDecls) {
Haojian Wuc6ddb462018-08-07 08:57:52 +0000459 if (auto ID = getSymbolID(ND)) {
460 IncRef(*ID);
461 }
Eric Liu48db19e2018-07-09 15:31:07 +0000462 }
463 if (Opts.CollectMacro) {
464 assert(PP);
465 for (const IdentifierInfo *II : ReferencedMacros) {
Eric Liua62c9d62018-07-09 18:54:51 +0000466 if (const auto *MI = PP->getMacroDefinition(II).getMacroInfo())
Eric Liud25f1212018-09-06 09:59:37 +0000467 if (auto ID = getSymbolID(*II, MI, PP->getSourceManager()))
468 IncRef(*ID);
Eric Liu48db19e2018-07-09 15:31:07 +0000469 }
Sam McCall93f99bf2018-03-12 14:49:09 +0000470 }
Haojian Wud81e3142018-08-31 12:54:13 +0000471
472 const auto &SM = ASTCtx->getSourceManager();
473 auto* MainFileEntry = SM.getFileEntryForID(SM.getMainFileID());
474
475 if (auto MainFileURI = toURI(SM, MainFileEntry->getName(), Opts)) {
476 std::string MainURI = *MainFileURI;
Sam McCallb0138312018-09-04 14:39:56 +0000477 for (const auto &It : DeclRefs) {
Haojian Wud81e3142018-08-31 12:54:13 +0000478 if (auto ID = getSymbolID(It.first)) {
479 if (Symbols.find(*ID)) {
480 for (const auto &LocAndRole : It.second) {
Sam McCallb0138312018-09-04 14:39:56 +0000481 Ref R;
Haojian Wud81e3142018-08-31 12:54:13 +0000482 auto Range =
483 getTokenRange(LocAndRole.first, SM, ASTCtx->getLangOpts());
Sam McCallb0138312018-09-04 14:39:56 +0000484 R.Location.Start = Range.first;
485 R.Location.End = Range.second;
486 R.Location.FileURI = MainURI;
487 R.Kind = toRefKind(LocAndRole.second);
488 Refs.insert(*ID, R);
Haojian Wud81e3142018-08-31 12:54:13 +0000489 }
490 }
491 }
492 }
493 } else {
494 log("Failed to create URI for main file: {0}", MainFileEntry->getName());
495 }
496
Sam McCall93f99bf2018-03-12 14:49:09 +0000497 ReferencedDecls.clear();
Eric Liu48db19e2018-07-09 15:31:07 +0000498 ReferencedMacros.clear();
Sam McCallb0138312018-09-04 14:39:56 +0000499 DeclRefs.clear();
Sam McCall93f99bf2018-03-12 14:49:09 +0000500}
501
Sam McCall60039512018-02-09 14:42:01 +0000502const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND,
503 SymbolID ID) {
Ilya Biryukov43714502018-05-16 12:32:44 +0000504 auto &Ctx = ND.getASTContext();
505 auto &SM = Ctx.getSourceManager();
Sam McCall60039512018-02-09 14:42:01 +0000506
Sam McCall60039512018-02-09 14:42:01 +0000507 Symbol S;
508 S.ID = std::move(ID);
Eric Liu7ad16962018-06-22 10:46:59 +0000509 std::string QName = printQualifiedName(ND);
Sam McCall60039512018-02-09 14:42:01 +0000510 std::tie(S.Scope, S.Name) = splitQualifiedName(QName);
Sam McCall032db942018-06-22 06:41:43 +0000511 // FIXME: this returns foo:bar: for objective-C methods, we prefer only foo:
512 // for consistency with CodeCompletionString and a clean name/signature split.
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +0000513
Eric Liu6df66002018-09-06 18:52:26 +0000514 if (isIndexedForCodeCompletion(ND, Ctx))
515 S.Flags |= Symbol::IndexedForCodeCompletion;
Sam McCall60039512018-02-09 14:42:01 +0000516 S.SymInfo = index::getSymbolInfo(&ND);
517 std::string FileURI;
Eric Liu48db19e2018-07-09 15:31:07 +0000518 if (auto DeclLoc = getTokenLocation(findNameLoc(&ND), SM, Opts,
519 ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000520 S.CanonicalDeclaration = *DeclLoc;
521
522 // Add completion info.
523 // FIXME: we may want to choose a different redecl, or combine from several.
524 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
Ilya Biryukovcf124bd2018-04-13 11:03:07 +0000525 // We use the primary template, as clang does during code completion.
526 CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0);
Sam McCall60039512018-02-09 14:42:01 +0000527 const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
528 *ASTCtx, *PP, CodeCompletionContext::CCC_Name, *CompletionAllocator,
529 *CompletionTUInfo,
Ilya Biryukov43714502018-05-16 12:32:44 +0000530 /*IncludeBriefComments*/ false);
Sam McCalla68951e2018-06-22 16:11:35 +0000531 std::string Signature;
532 std::string SnippetSuffix;
533 getSignature(*CCS, &Signature, &SnippetSuffix);
Ilya Biryukov43714502018-05-16 12:32:44 +0000534 std::string Documentation =
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +0000535 formatDocumentation(*CCS, getDocComment(Ctx, SymbolCompletion,
536 /*CommentsFromHeaders=*/true));
Sam McCalla68951e2018-06-22 16:11:35 +0000537 std::string ReturnType = getReturnType(*CCS);
Sam McCall60039512018-02-09 14:42:01 +0000538
Eric Liuc5105f92018-02-16 14:15:55 +0000539 std::string Include;
540 if (Opts.CollectIncludePath && shouldCollectIncludePath(S.SymInfo.Kind)) {
541 // Use the expansion location to get the #include header since this is
542 // where the symbol is exposed.
Eric Liub96363d2018-03-01 18:06:40 +0000543 if (auto Header = getIncludeHeader(
544 QName, SM, SM.getExpansionLoc(ND.getLocation()), Opts))
Eric Liuc5105f92018-02-16 14:15:55 +0000545 Include = std::move(*Header);
546 }
Sam McCalla68951e2018-06-22 16:11:35 +0000547 S.Signature = Signature;
548 S.CompletionSnippetSuffix = SnippetSuffix;
Sam McCall2e5700f2018-08-31 13:55:01 +0000549 S.Documentation = Documentation;
550 S.ReturnType = ReturnType;
Eric Liu83f63e42018-09-03 10:18:21 +0000551 if (!Include.empty())
552 S.IncludeHeaders.emplace_back(Include, 1);
Sam McCall60039512018-02-09 14:42:01 +0000553
Sam McCall2161ec72018-07-05 06:20:41 +0000554 S.Origin = Opts.Origin;
Eric Liu6df66002018-09-06 18:52:26 +0000555 if (ND.getAvailability() == AR_Deprecated)
556 S.Flags |= Symbol::Deprecated;
Sam McCall60039512018-02-09 14:42:01 +0000557 Symbols.insert(S);
558 return Symbols.find(S.ID);
559}
560
561void SymbolCollector::addDefinition(const NamedDecl &ND,
562 const Symbol &DeclSym) {
563 if (DeclSym.Definition)
564 return;
565 // If we saw some forward declaration, we end up copying the symbol.
566 // This is not ideal, but avoids duplicating the "is this a definition" check
567 // in clang::index. We should only see one definition.
568 Symbol S = DeclSym;
569 std::string FileURI;
Eric Liu48db19e2018-07-09 15:31:07 +0000570 if (auto DefLoc = getTokenLocation(findNameLoc(&ND),
571 ND.getASTContext().getSourceManager(),
572 Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000573 S.Definition = *DefLoc;
574 Symbols.insert(S);
575}
576
Haojian Wu4c1394d2017-12-12 15:42:10 +0000577} // namespace clangd
578} // namespace clang