blob: 142e0f4cefeafcb211535d52a9d18608479de75b [file] [log] [blame]
Haojian Wu4c1394d2017-12-12 15:42:10 +00001//===--- SymbolCollector.cpp -------------------------------------*- C++-*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Haojian Wu4c1394d2017-12-12 15:42:10 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "SymbolCollector.h"
Eric Liuf7688682018-09-07 09:40:36 +000010#include "AST.h"
Eric Liuc5105f92018-02-16 14:15:55 +000011#include "CanonicalIncludes.h"
Eric Liuf7688682018-09-07 09:40:36 +000012#include "CodeComplete.h"
13#include "CodeCompletionStrings.h"
Dmitri Gribenkocb83ea62019-02-28 13:49:25 +000014#include "ExpectedTypes.h"
Eric Liuf7688682018-09-07 09:40:36 +000015#include "Logger.h"
16#include "SourceCode.h"
Dmitri Gribenko5306a712019-02-28 11:02:01 +000017#include "SymbolLocation.h"
Eric Liuf7688682018-09-07 09:40:36 +000018#include "URI.h"
Eric Liua57afd02018-09-17 07:43:49 +000019#include "clang/AST/Decl.h"
20#include "clang/AST/DeclBase.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000021#include "clang/AST/DeclCXX.h"
Ilya Biryukovcf124bd2018-04-13 11:03:07 +000022#include "clang/AST/DeclTemplate.h"
Haojian Wu7dd49502018-10-17 08:38:36 +000023#include "clang/Basic/SourceLocation.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000024#include "clang/Basic/SourceManager.h"
Eric Liua57afd02018-09-17 07:43:49 +000025#include "clang/Basic/Specifiers.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000026#include "clang/Index/IndexSymbol.h"
27#include "clang/Index/USRGeneration.h"
Eric Liua57afd02018-09-17 07:43:49 +000028#include "llvm/Support/Casting.h"
Eric Liu278e2d12018-01-29 15:13:29 +000029#include "llvm/Support/FileSystem.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000030#include "llvm/Support/MemoryBuffer.h"
31#include "llvm/Support/Path.h"
32
33namespace clang {
34namespace clangd {
Haojian Wu4c1394d2017-12-12 15:42:10 +000035namespace {
Sam McCallc008af62018-10-20 15:30:37 +000036
Ilya Biryukovf118d512018-04-14 16:27:35 +000037/// If \p ND is a template specialization, returns the described template.
Ilya Biryukovcf124bd2018-04-13 11:03:07 +000038/// Otherwise, returns \p ND.
39const NamedDecl &getTemplateOrThis(const NamedDecl &ND) {
Ilya Biryukovf118d512018-04-14 16:27:35 +000040 if (auto T = ND.getDescribedTemplate())
41 return *T;
Ilya Biryukovcf124bd2018-04-13 11:03:07 +000042 return ND;
43}
44
Eric Liu7f247652018-02-06 16:10:35 +000045// Returns a URI of \p Path. Firstly, this makes the \p Path absolute using the
46// current working directory of the given SourceManager if the Path is not an
47// absolute path. If failed, this resolves relative paths against \p FallbackDir
48// to get an absolute path. Then, this tries creating an URI for the absolute
49// path with schemes specified in \p Opts. This returns an URI with the first
50// working scheme, if there is any; otherwise, this returns None.
Haojian Wu4c1394d2017-12-12 15:42:10 +000051//
52// The Path can be a path relative to the build directory, or retrieved from
53// the SourceManager.
Kadir Cetinkayadd677932018-12-19 10:46:21 +000054std::string toURI(const SourceManager &SM, llvm::StringRef Path,
55 const SymbolCollector::Options &Opts) {
56 llvm::SmallString<128> AbsolutePath(Path);
57 if (auto CanonPath =
58 getCanonicalPath(SM.getFileManager().getFile(Path), SM)) {
59 AbsolutePath = *CanonPath;
Haojian Wu4c1394d2017-12-12 15:42:10 +000060 }
Kadir Cetinkayadd677932018-12-19 10:46:21 +000061 // We don't perform is_absolute check in an else branch because makeAbsolute
62 // might return a relative path on some InMemoryFileSystems.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000063 if (!llvm::sys::path::is_absolute(AbsolutePath) && !Opts.FallbackDir.empty())
64 llvm::sys::fs::make_absolute(Opts.FallbackDir, AbsolutePath);
65 llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/true);
Eric Liuc0ac4bb2018-11-22 15:02:05 +000066 return URI::create(AbsolutePath).toString();
Haojian Wu4c1394d2017-12-12 15:42:10 +000067}
Eric Liu4feda802017-12-19 11:37:40 +000068
Eric Liud67ec242018-05-16 12:12:30 +000069// All proto generated headers should start with this line.
70static const char *PROTO_HEADER_COMMENT =
71 "// Generated by the protocol buffer compiler. DO NOT EDIT!";
72
73// Checks whether the decl is a private symbol in a header generated by
74// protobuf compiler.
75// To identify whether a proto header is actually generated by proto compiler,
76// we check whether it starts with PROTO_HEADER_COMMENT.
77// FIXME: make filtering extensible when there are more use cases for symbol
78// filters.
79bool isPrivateProtoDecl(const NamedDecl &ND) {
80 const auto &SM = ND.getASTContext().getSourceManager();
81 auto Loc = findNameLoc(&ND);
82 auto FileName = SM.getFilename(Loc);
83 if (!FileName.endswith(".proto.h") && !FileName.endswith(".pb.h"))
84 return false;
85 auto FID = SM.getFileID(Loc);
86 // Double check that this is an actual protobuf header.
87 if (!SM.getBufferData(FID).startswith(PROTO_HEADER_COMMENT))
88 return false;
89
90 // ND without identifier can be operators.
91 if (ND.getIdentifier() == nullptr)
92 return false;
93 auto Name = ND.getIdentifier()->getName();
94 if (!Name.contains('_'))
95 return false;
96 // Nested proto entities (e.g. Message::Nested) have top-level decls
97 // that shouldn't be used (Message_Nested). Ignore them completely.
98 // The nested entities are dangling type aliases, we may want to reconsider
99 // including them in the future.
100 // For enum constants, SOME_ENUM_CONSTANT is not private and should be
101 // indexed. Outer_INNER is private. This heuristic relies on naming style, it
102 // will include OUTER_INNER and exclude some_enum_constant.
103 // FIXME: the heuristic relies on naming style (i.e. no underscore in
104 // user-defined names) and can be improved.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000105 return (ND.getKind() != Decl::EnumConstant) || llvm::any_of(Name, islower);
Eric Liud67ec242018-05-16 12:12:30 +0000106}
107
Eric Liuc5105f92018-02-16 14:15:55 +0000108// We only collect #include paths for symbols that are suitable for global code
109// completion, except for namespaces since #include path for a namespace is hard
110// to define.
111bool shouldCollectIncludePath(index::SymbolKind Kind) {
112 using SK = index::SymbolKind;
113 switch (Kind) {
114 case SK::Macro:
115 case SK::Enum:
116 case SK::Struct:
117 case SK::Class:
118 case SK::Union:
119 case SK::TypeAlias:
120 case SK::Using:
121 case SK::Function:
122 case SK::Variable:
123 case SK::EnumConstant:
124 return true;
125 default:
126 return false;
127 }
128}
129
Eric Liu02ce01f2018-02-22 10:14:05 +0000130/// Gets a canonical include (URI of the header or <header> or "header") for
131/// header of \p Loc.
132/// Returns None if fails to get include header for \p Loc.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000133llvm::Optional<std::string>
134getIncludeHeader(llvm::StringRef QName, const SourceManager &SM,
135 SourceLocation Loc, const SymbolCollector::Options &Opts) {
Eric Liu3cee95e2018-05-24 14:40:24 +0000136 std::vector<std::string> Headers;
137 // Collect the #include stack.
138 while (true) {
139 if (!Loc.isValid())
140 break;
141 auto FilePath = SM.getFilename(Loc);
142 if (FilePath.empty())
143 break;
144 Headers.push_back(FilePath);
145 if (SM.isInMainFile(Loc))
146 break;
147 Loc = SM.getIncludeLoc(SM.getFileID(Loc));
Eric Liuc5105f92018-02-16 14:15:55 +0000148 }
Eric Liu3cee95e2018-05-24 14:40:24 +0000149 if (Headers.empty())
Sam McCallc008af62018-10-20 15:30:37 +0000150 return None;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000151 llvm::StringRef Header = Headers[0];
Eric Liu3cee95e2018-05-24 14:40:24 +0000152 if (Opts.Includes) {
153 Header = Opts.Includes->mapHeader(Headers, QName);
154 if (Header.startswith("<") || Header.startswith("\""))
155 return Header.str();
156 }
157 return toURI(SM, Header, Opts);
Eric Liuc5105f92018-02-16 14:15:55 +0000158}
159
Haojian Wud81e3142018-08-31 12:54:13 +0000160// Return the symbol range of the token at \p TokLoc.
161std::pair<SymbolLocation::Position, SymbolLocation::Position>
162getTokenRange(SourceLocation TokLoc, const SourceManager &SM,
163 const LangOptions &LangOpts) {
164 auto CreatePosition = [&SM](SourceLocation Loc) {
165 auto LSPLoc = sourceLocToPosition(SM, Loc);
166 SymbolLocation::Position Pos;
Haojian Wub515fab2018-10-18 10:43:50 +0000167 Pos.setLine(LSPLoc.line);
168 Pos.setColumn(LSPLoc.character);
Haojian Wud81e3142018-08-31 12:54:13 +0000169 return Pos;
170 };
171
172 auto TokenLength = clang::Lexer::MeasureTokenLength(TokLoc, SM, LangOpts);
173 return {CreatePosition(TokLoc),
174 CreatePosition(TokLoc.getLocWithOffset(TokenLength))};
175}
176
Eric Liuad588af2018-11-06 10:55:21 +0000177bool shouldIndexFile(const SourceManager &SM, FileID FID,
178 const SymbolCollector::Options &Opts,
179 llvm::DenseMap<FileID, bool> *FilesToIndexCache) {
180 if (!Opts.FileFilter)
181 return true;
182 auto I = FilesToIndexCache->try_emplace(FID);
183 if (I.second)
184 I.first->second = Opts.FileFilter(SM, FID);
185 return I.first->second;
186}
187
Haojian Wud81e3142018-08-31 12:54:13 +0000188// Return the symbol location of the token at \p TokLoc.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000189llvm::Optional<SymbolLocation>
190getTokenLocation(SourceLocation TokLoc, const SourceManager &SM,
191 const SymbolCollector::Options &Opts,
192 const clang::LangOptions &LangOpts,
193 std::string &FileURIStorage) {
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000194 auto Path = SM.getFilename(TokLoc);
195 if (Path.empty())
Sam McCallc008af62018-10-20 15:30:37 +0000196 return None;
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000197 FileURIStorage = toURI(SM, Path, Opts);
Sam McCall60039512018-02-09 14:42:01 +0000198 SymbolLocation Result;
Haojian Wuee54a2b2018-11-14 11:55:45 +0000199 Result.FileURI = FileURIStorage.c_str();
Haojian Wud81e3142018-08-31 12:54:13 +0000200 auto Range = getTokenRange(TokLoc, SM, LangOpts);
201 Result.Start = Range.first;
202 Result.End = Range.second;
Haojian Wu545c02a2018-04-13 08:30:39 +0000203
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000204 return Result;
Haojian Wub0189062018-01-31 12:56:51 +0000205}
206
Eric Liucf8601b2018-02-28 09:33:15 +0000207// Checks whether \p ND is a definition of a TagDecl (class/struct/enum/union)
208// in a header file, in which case clangd would prefer to use ND as a canonical
209// declaration.
210// FIXME: handle symbol types that are not TagDecl (e.g. functions), if using
Fangrui Song943e12e2018-03-29 20:03:16 +0000211// the first seen declaration as canonical declaration is not a good enough
Eric Liucf8601b2018-02-28 09:33:15 +0000212// heuristic.
213bool isPreferredDeclaration(const NamedDecl &ND, index::SymbolRoleSet Roles) {
Kadir Cetinkaya017cc6c2019-03-08 09:54:37 +0000214 const auto &SM = ND.getASTContext().getSourceManager();
Eric Liucf8601b2018-02-28 09:33:15 +0000215 return (Roles & static_cast<unsigned>(index::SymbolRole::Definition)) &&
Sam McCallc008af62018-10-20 15:30:37 +0000216 isa<TagDecl>(&ND) &&
Sam McCall5fb97462018-10-05 14:03:04 +0000217 !SM.isWrittenInMainFile(SM.getExpansionLoc(ND.getLocation()));
Eric Liucf8601b2018-02-28 09:33:15 +0000218}
219
Sam McCallb0138312018-09-04 14:39:56 +0000220RefKind toRefKind(index::SymbolRoleSet Roles) {
221 return static_cast<RefKind>(static_cast<unsigned>(RefKind::All) & Roles);
Haojian Wud81e3142018-08-31 12:54:13 +0000222}
223
Haojian Wu4c1394d2017-12-12 15:42:10 +0000224} // namespace
225
Eric Liu9af958f2018-01-10 14:57:58 +0000226SymbolCollector::SymbolCollector(Options Opts) : Opts(std::move(Opts)) {}
227
Eric Liu76f6b442018-01-09 17:32:00 +0000228void SymbolCollector::initialize(ASTContext &Ctx) {
229 ASTCtx = &Ctx;
230 CompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>();
231 CompletionTUInfo =
232 llvm::make_unique<CodeCompletionTUInfo>(CompletionAllocator);
233}
234
Eric Liu8763e482018-06-21 12:12:26 +0000235bool SymbolCollector::shouldCollectSymbol(const NamedDecl &ND,
Haojian Wu7800dbe2018-12-03 13:16:04 +0000236 const ASTContext &ASTCtx,
Sam McCall0e93b072019-01-14 10:01:17 +0000237 const Options &Opts,
238 bool IsMainFileOnly) {
Eric Liu8763e482018-06-21 12:12:26 +0000239 // Skip anonymous declarations, e.g (anonymous enum/class/struct).
240 if (ND.getDeclName().isEmpty())
241 return false;
242
Sam McCall0e93b072019-01-14 10:01:17 +0000243 // Skip main-file symbols if we are not collecting them.
244 if (IsMainFileOnly && !Opts.CollectMainFileSymbols)
245 return false;
246
247 // Skip symbols in anonymous namespaces in header files.
248 if (!IsMainFileOnly && ND.isInAnonymousNamespace())
Eric Liu8763e482018-06-21 12:12:26 +0000249 return false;
250
251 // We want most things but not "local" symbols such as symbols inside
252 // FunctionDecl, BlockDecl, ObjCMethodDecl and OMPDeclareReductionDecl.
253 // FIXME: Need a matcher for ExportDecl in order to include symbols declared
254 // within an export.
Eric Liua57afd02018-09-17 07:43:49 +0000255 const auto *DeclCtx = ND.getDeclContext();
256 switch (DeclCtx->getDeclKind()) {
257 case Decl::TranslationUnit:
258 case Decl::Namespace:
259 case Decl::LinkageSpec:
260 case Decl::Enum:
261 case Decl::ObjCProtocol:
262 case Decl::ObjCInterface:
263 case Decl::ObjCCategory:
264 case Decl::ObjCCategoryImpl:
265 case Decl::ObjCImplementation:
266 break;
267 default:
268 // Record has a few derivations (e.g. CXXRecord, Class specialization), it's
269 // easier to cast.
Sam McCallc008af62018-10-20 15:30:37 +0000270 if (!isa<RecordDecl>(DeclCtx))
Eric Liua57afd02018-09-17 07:43:49 +0000271 return false;
272 }
Eric Liu8763e482018-06-21 12:12:26 +0000273
274 // Avoid indexing internal symbols in protobuf generated headers.
275 if (isPrivateProtoDecl(ND))
276 return false;
277 return true;
278}
279
Haojian Wu4c1394d2017-12-12 15:42:10 +0000280// Always return true to continue indexing.
281bool SymbolCollector::handleDeclOccurence(
282 const Decl *D, index::SymbolRoleSet Roles,
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000283 llvm::ArrayRef<index::SymbolRelation> Relations, SourceLocation Loc,
Haojian Wu4c1394d2017-12-12 15:42:10 +0000284 index::IndexDataConsumer::ASTNodeInfo ASTNode) {
Eric Liu9af958f2018-01-10 14:57:58 +0000285 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
Sam McCall93f99bf2018-03-12 14:49:09 +0000286 assert(CompletionAllocator && CompletionTUInfo);
Eric Liu77d18112018-06-04 11:31:55 +0000287 assert(ASTNode.OrigD);
Kadir Cetinkayabb6cd822019-04-15 14:38:46 +0000288 // Indexing API puts cannonical decl into D, which might not have a valid
289 // source location for implicit/built-in decls. Fallback to original decl in
290 // such cases.
291 if (D->getLocation().isInvalid())
292 D = ASTNode.OrigD;
Eric Liu77d18112018-06-04 11:31:55 +0000293 // If OrigD is an declaration associated with a friend declaration and it's
294 // not a definition, skip it. Note that OrigD is the occurrence that the
295 // collector is currently visiting.
296 if ((ASTNode.OrigD->getFriendObjectKind() !=
297 Decl::FriendObjectKind::FOK_None) &&
298 !(Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
299 return true;
Kadir Cetinkaya017cc6c2019-03-08 09:54:37 +0000300 // Skip non-semantic references, we should start processing these when we
301 // decide to implement renaming with index support.
302 if ((Roles & static_cast<unsigned>(index::SymbolRole::NameReference)))
303 return true;
Eric Liu77d18112018-06-04 11:31:55 +0000304 // A declaration created for a friend declaration should not be used as the
305 // canonical declaration in the index. Use OrigD instead, unless we've already
306 // picked a replacement for D
307 if (D->getFriendObjectKind() != Decl::FriendObjectKind::FOK_None)
308 D = CanonicalDecls.try_emplace(D, ASTNode.OrigD).first->second;
Sam McCallc008af62018-10-20 15:30:37 +0000309 const NamedDecl *ND = dyn_cast<NamedDecl>(D);
Sam McCall93f99bf2018-03-12 14:49:09 +0000310 if (!ND)
311 return true;
Eric Liu9af958f2018-01-10 14:57:58 +0000312
Sam McCall93f99bf2018-03-12 14:49:09 +0000313 // Mark D as referenced if this is a reference coming from the main file.
314 // D may not be an interesting symbol, but it's cheaper to check at the end.
Sam McCallb9d57112018-04-09 14:28:52 +0000315 auto &SM = ASTCtx->getSourceManager();
Haojian Wud81e3142018-08-31 12:54:13 +0000316 auto SpellingLoc = SM.getSpellingLoc(Loc);
Sam McCall93f99bf2018-03-12 14:49:09 +0000317 if (Opts.CountReferences &&
318 (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
Haojian Wud81e3142018-08-31 12:54:13 +0000319 SM.getFileID(SpellingLoc) == SM.getMainFileID())
Sam McCall93f99bf2018-03-12 14:49:09 +0000320 ReferencedDecls.insert(ND);
321
Haojian Wue83cacc2018-10-15 11:46:26 +0000322 bool CollectRef = static_cast<unsigned>(Opts.RefFilter) & Roles;
323 bool IsOnlyRef =
324 !(Roles & (static_cast<unsigned>(index::SymbolRole::Declaration) |
325 static_cast<unsigned>(index::SymbolRole::Definition)));
Haojian Wud81e3142018-08-31 12:54:13 +0000326
Haojian Wue83cacc2018-10-15 11:46:26 +0000327 if (IsOnlyRef && !CollectRef)
Haojian Wu4c1394d2017-12-12 15:42:10 +0000328 return true;
Sam McCall0e93b072019-01-14 10:01:17 +0000329
330 // ND is the canonical (i.e. first) declaration. If it's in the main file,
331 // then no public declaration was visible, so assume it's main-file only.
Kadir Cetinkaya86658022019-03-19 09:27:04 +0000332 bool IsMainFileOnly =
333 SM.isWrittenInMainFile(SM.getExpansionLoc(ND->getBeginLoc()));
Sam McCall2d02c6d2019-04-10 16:26:58 +0000334 // In C, printf is a redecl of an implicit builtin! So check OrigD instead.
335 if (ASTNode.OrigD->isImplicit() ||
336 !shouldCollectSymbol(*ND, *ASTCtx, Opts, IsMainFileOnly))
Sam McCall93f99bf2018-03-12 14:49:09 +0000337 return true;
Sam McCall0e93b072019-01-14 10:01:17 +0000338 // Do not store references to main-file symbols.
339 if (CollectRef && !IsMainFileOnly && !isa<NamespaceDecl>(ND) &&
Haojian Wu7dd49502018-10-17 08:38:36 +0000340 (Opts.RefsInHeaders || SM.getFileID(SpellingLoc) == SM.getMainFileID()))
Haojian Wue83cacc2018-10-15 11:46:26 +0000341 DeclRefs[ND].emplace_back(SpellingLoc, Roles);
342 // Don't continue indexing if this is a mere reference.
343 if (IsOnlyRef)
344 return true;
Haojian Wu4c1394d2017-12-12 15:42:10 +0000345
Haojian Wuc6ddb462018-08-07 08:57:52 +0000346 auto ID = getSymbolID(ND);
347 if (!ID)
Sam McCall93f99bf2018-03-12 14:49:09 +0000348 return true;
Eric Liu76f6b442018-01-09 17:32:00 +0000349
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000350 // FIXME: ObjCPropertyDecl are not properly indexed here:
351 // - ObjCPropertyDecl may have an OrigD of ObjCPropertyImplDecl, which is
352 // not a NamedDecl.
353 auto *OriginalDecl = dyn_cast<NamedDecl>(ASTNode.OrigD);
354 if (!OriginalDecl)
355 return true;
356
Haojian Wuc6ddb462018-08-07 08:57:52 +0000357 const Symbol *BasicSymbol = Symbols.find(*ID);
Sam McCall93f99bf2018-03-12 14:49:09 +0000358 if (!BasicSymbol) // Regardless of role, ND is the canonical declaration.
Sam McCall0e93b072019-01-14 10:01:17 +0000359 BasicSymbol = addDeclaration(*ND, std::move(*ID), IsMainFileOnly);
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000360 else if (isPreferredDeclaration(*OriginalDecl, Roles))
Sam McCall93f99bf2018-03-12 14:49:09 +0000361 // If OriginalDecl is preferred, replace the existing canonical
362 // declaration (e.g. a class forward declaration). There should be at most
363 // one duplicate as we expect to see only one preferred declaration per
364 // TU, because in practice they are definitions.
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000365 BasicSymbol = addDeclaration(*OriginalDecl, std::move(*ID), IsMainFileOnly);
Haojian Wu4c1394d2017-12-12 15:42:10 +0000366
Sam McCall93f99bf2018-03-12 14:49:09 +0000367 if (Roles & static_cast<unsigned>(index::SymbolRole::Definition))
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000368 addDefinition(*OriginalDecl, *BasicSymbol);
Haojian Wu4c1394d2017-12-12 15:42:10 +0000369 return true;
370}
371
Eric Liu48db19e2018-07-09 15:31:07 +0000372bool SymbolCollector::handleMacroOccurence(const IdentifierInfo *Name,
373 const MacroInfo *MI,
374 index::SymbolRoleSet Roles,
375 SourceLocation Loc) {
376 if (!Opts.CollectMacro)
377 return true;
378 assert(PP.get());
379
380 const auto &SM = PP->getSourceManager();
Eric Liuad588af2018-11-06 10:55:21 +0000381 auto DefLoc = MI->getDefinitionLoc();
Haojian Wu7b6f8742019-01-28 14:11:49 +0000382
Eric Liu48db19e2018-07-09 15:31:07 +0000383 // Header guards are not interesting in index. Builtin macros don't have
384 // useful locations and are not needed for code completions.
385 if (MI->isUsedForHeaderGuard() || MI->isBuiltinMacro())
386 return true;
387
Haojian Wu7b6f8742019-01-28 14:11:49 +0000388 // Skip main-file symbols if we are not collecting them.
389 bool IsMainFileSymbol = SM.isInMainFile(SM.getExpansionLoc(DefLoc));
390 if (IsMainFileSymbol && !Opts.CollectMainFileSymbols)
391 return false;
392
393 // Also avoid storing predefined macros like __DBL_MIN__.
394 if (SM.isWrittenInBuiltinFile(DefLoc))
395 return true;
396
Eric Liu48db19e2018-07-09 15:31:07 +0000397 // Mark the macro as referenced if this is a reference coming from the main
398 // file. The macro may not be an interesting symbol, but it's cheaper to check
399 // at the end.
400 if (Opts.CountReferences &&
401 (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
402 SM.getFileID(SM.getSpellingLoc(Loc)) == SM.getMainFileID())
403 ReferencedMacros.insert(Name);
404 // Don't continue indexing if this is a mere reference.
405 // FIXME: remove macro with ID if it is undefined.
406 if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) ||
407 Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
408 return true;
409
Eric Liud25f1212018-09-06 09:59:37 +0000410 auto ID = getSymbolID(*Name, MI, SM);
411 if (!ID)
Eric Liu48db19e2018-07-09 15:31:07 +0000412 return true;
Eric Liu48db19e2018-07-09 15:31:07 +0000413
414 // Only collect one instance in case there are multiple.
Eric Liud25f1212018-09-06 09:59:37 +0000415 if (Symbols.find(*ID) != nullptr)
Eric Liu48db19e2018-07-09 15:31:07 +0000416 return true;
417
418 Symbol S;
Eric Liud25f1212018-09-06 09:59:37 +0000419 S.ID = std::move(*ID);
Eric Liu48db19e2018-07-09 15:31:07 +0000420 S.Name = Name->getName();
Haojian Wu7b6f8742019-01-28 14:11:49 +0000421 if (!IsMainFileSymbol) {
422 S.Flags |= Symbol::IndexedForCodeCompletion;
423 S.Flags |= Symbol::VisibleOutsideFile;
424 }
Eric Liu48db19e2018-07-09 15:31:07 +0000425 S.SymInfo = index::getSymbolInfoForMacro(*MI);
426 std::string FileURI;
Eric Liuad588af2018-11-06 10:55:21 +0000427 // FIXME: use the result to filter out symbols.
428 shouldIndexFile(SM, SM.getFileID(Loc), Opts, &FilesToIndexCache);
429 if (auto DeclLoc =
430 getTokenLocation(DefLoc, SM, Opts, PP->getLangOpts(), FileURI))
Eric Liu48db19e2018-07-09 15:31:07 +0000431 S.CanonicalDeclaration = *DeclLoc;
432
433 CodeCompletionResult SymbolCompletion(Name);
434 const auto *CCS = SymbolCompletion.CreateCodeCompletionStringForMacro(
435 *PP, *CompletionAllocator, *CompletionTUInfo);
436 std::string Signature;
437 std::string SnippetSuffix;
438 getSignature(*CCS, &Signature, &SnippetSuffix);
439
440 std::string Include;
441 if (Opts.CollectIncludePath && shouldCollectIncludePath(S.SymInfo.Kind)) {
Eric Liuad588af2018-11-06 10:55:21 +0000442 if (auto Header = getIncludeHeader(Name->getName(), SM,
443 SM.getExpansionLoc(DefLoc), Opts))
Eric Liu48db19e2018-07-09 15:31:07 +0000444 Include = std::move(*Header);
445 }
446 S.Signature = Signature;
447 S.CompletionSnippetSuffix = SnippetSuffix;
Eric Liu83f63e42018-09-03 10:18:21 +0000448 if (!Include.empty())
449 S.IncludeHeaders.emplace_back(Include, 1);
450
Eric Liu48db19e2018-07-09 15:31:07 +0000451 Symbols.insert(S);
452 return true;
453}
454
Sam McCall93f99bf2018-03-12 14:49:09 +0000455void SymbolCollector::finish() {
Eric Liu48db19e2018-07-09 15:31:07 +0000456 // At the end of the TU, add 1 to the refcount of all referenced symbols.
457 auto IncRef = [this](const SymbolID &ID) {
458 if (const auto *S = Symbols.find(ID)) {
459 Symbol Inc = *S;
460 ++Inc.References;
461 Symbols.insert(Inc);
462 }
463 };
464 for (const NamedDecl *ND : ReferencedDecls) {
Haojian Wuc6ddb462018-08-07 08:57:52 +0000465 if (auto ID = getSymbolID(ND)) {
466 IncRef(*ID);
467 }
Eric Liu48db19e2018-07-09 15:31:07 +0000468 }
469 if (Opts.CollectMacro) {
470 assert(PP);
471 for (const IdentifierInfo *II : ReferencedMacros) {
Eric Liua62c9d62018-07-09 18:54:51 +0000472 if (const auto *MI = PP->getMacroDefinition(II).getMacroInfo())
Eric Liud25f1212018-09-06 09:59:37 +0000473 if (auto ID = getSymbolID(*II, MI, PP->getSourceManager()))
474 IncRef(*ID);
Eric Liu48db19e2018-07-09 15:31:07 +0000475 }
Sam McCall93f99bf2018-03-12 14:49:09 +0000476 }
Haojian Wud81e3142018-08-31 12:54:13 +0000477
478 const auto &SM = ASTCtx->getSourceManager();
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000479 llvm::DenseMap<FileID, std::string> URICache;
480 auto GetURI = [&](FileID FID) -> llvm::Optional<std::string> {
Haojian Wu7dd49502018-10-17 08:38:36 +0000481 auto Found = URICache.find(FID);
482 if (Found == URICache.end()) {
Haojian Wu7dd49502018-10-17 08:38:36 +0000483 if (auto *FileEntry = SM.getFileEntryForID(FID)) {
484 auto FileURI = toURI(SM, FileEntry->getName(), Opts);
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000485 Found = URICache.insert({FID, FileURI}).first;
Haojian Wuc014d862018-10-17 08:54:48 +0000486 } else {
487 // Ignore cases where we can not find a corresponding file entry
488 // for the loc, thoses are not interesting, e.g. symbols formed
489 // via macro concatenation.
Sam McCallc008af62018-10-20 15:30:37 +0000490 return None;
Haojian Wu7dd49502018-10-17 08:38:36 +0000491 }
492 }
493 return Found->second;
494 };
Haojian Wud81e3142018-08-31 12:54:13 +0000495
Haojian Wu7dd49502018-10-17 08:38:36 +0000496 if (auto MainFileURI = GetURI(SM.getMainFileID())) {
Sam McCallb0138312018-09-04 14:39:56 +0000497 for (const auto &It : DeclRefs) {
Haojian Wud81e3142018-08-31 12:54:13 +0000498 if (auto ID = getSymbolID(It.first)) {
Haojian Wue83cacc2018-10-15 11:46:26 +0000499 for (const auto &LocAndRole : It.second) {
Haojian Wu7dd49502018-10-17 08:38:36 +0000500 auto FileID = SM.getFileID(LocAndRole.first);
Eric Liuad588af2018-11-06 10:55:21 +0000501 // FIXME: use the result to filter out references.
502 shouldIndexFile(SM, FileID, Opts, &FilesToIndexCache);
Haojian Wu7dd49502018-10-17 08:38:36 +0000503 if (auto FileURI = GetURI(FileID)) {
504 auto Range =
505 getTokenRange(LocAndRole.first, SM, ASTCtx->getLangOpts());
506 Ref R;
507 R.Location.Start = Range.first;
508 R.Location.End = Range.second;
Haojian Wuee54a2b2018-11-14 11:55:45 +0000509 R.Location.FileURI = FileURI->c_str();
Haojian Wu7dd49502018-10-17 08:38:36 +0000510 R.Kind = toRefKind(LocAndRole.second);
511 Refs.insert(*ID, R);
512 }
Haojian Wud81e3142018-08-31 12:54:13 +0000513 }
514 }
515 }
Haojian Wud81e3142018-08-31 12:54:13 +0000516 }
517
Sam McCall93f99bf2018-03-12 14:49:09 +0000518 ReferencedDecls.clear();
Eric Liu48db19e2018-07-09 15:31:07 +0000519 ReferencedMacros.clear();
Sam McCallb0138312018-09-04 14:39:56 +0000520 DeclRefs.clear();
Eric Liuad588af2018-11-06 10:55:21 +0000521 FilesToIndexCache.clear();
Sam McCall93f99bf2018-03-12 14:49:09 +0000522}
523
Kadir Cetinkaya86658022019-03-19 09:27:04 +0000524const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND, SymbolID ID,
Sam McCall0e93b072019-01-14 10:01:17 +0000525 bool IsMainFileOnly) {
Ilya Biryukov43714502018-05-16 12:32:44 +0000526 auto &Ctx = ND.getASTContext();
527 auto &SM = Ctx.getSourceManager();
Sam McCall60039512018-02-09 14:42:01 +0000528
Sam McCall60039512018-02-09 14:42:01 +0000529 Symbol S;
530 S.ID = std::move(ID);
Eric Liu7ad16962018-06-22 10:46:59 +0000531 std::string QName = printQualifiedName(ND);
Sam McCall032db942018-06-22 06:41:43 +0000532 // FIXME: this returns foo:bar: for objective-C methods, we prefer only foo:
533 // for consistency with CodeCompletionString and a clean name/signature split.
Kadir Cetinkaya79063de2019-04-12 10:09:24 +0000534 std::tie(S.Scope, S.Name) = splitQualifiedName(QName);
535 std::string TemplateSpecializationArgs = printTemplateSpecializationArgs(ND);
536 S.TemplateSpecializationArgs = TemplateSpecializationArgs;
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +0000537
Sam McCall0e93b072019-01-14 10:01:17 +0000538 // We collect main-file symbols, but do not use them for code completion.
539 if (!IsMainFileOnly && isIndexedForCodeCompletion(ND, Ctx))
Eric Liu6df66002018-09-06 18:52:26 +0000540 S.Flags |= Symbol::IndexedForCodeCompletion;
Eric Liu48597382018-10-18 12:23:05 +0000541 if (isImplementationDetail(&ND))
542 S.Flags |= Symbol::ImplementationDetail;
Sam McCall0e93b072019-01-14 10:01:17 +0000543 if (!IsMainFileOnly)
544 S.Flags |= Symbol::VisibleOutsideFile;
Sam McCall60039512018-02-09 14:42:01 +0000545 S.SymInfo = index::getSymbolInfo(&ND);
546 std::string FileURI;
Eric Liuad588af2018-11-06 10:55:21 +0000547 auto Loc = findNameLoc(&ND);
Kadir Cetinkayabb6cd822019-04-15 14:38:46 +0000548 assert(Loc.isValid() && "Invalid source location for NamedDecl");
Eric Liuad588af2018-11-06 10:55:21 +0000549 // FIXME: use the result to filter out symbols.
550 shouldIndexFile(SM, SM.getFileID(Loc), Opts, &FilesToIndexCache);
551 if (auto DeclLoc =
552 getTokenLocation(Loc, SM, Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000553 S.CanonicalDeclaration = *DeclLoc;
554
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000555 S.Origin = Opts.Origin;
556 if (ND.getAvailability() == AR_Deprecated)
557 S.Flags |= Symbol::Deprecated;
558
Sam McCall60039512018-02-09 14:42:01 +0000559 // Add completion info.
560 // FIXME: we may want to choose a different redecl, or combine from several.
561 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
Ilya Biryukovcf124bd2018-04-13 11:03:07 +0000562 // We use the primary template, as clang does during code completion.
563 CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0);
Sam McCall60039512018-02-09 14:42:01 +0000564 const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
Kadir Cetinkayab9157902018-10-24 15:24:29 +0000565 *ASTCtx, *PP, CodeCompletionContext::CCC_Symbol, *CompletionAllocator,
Sam McCall60039512018-02-09 14:42:01 +0000566 *CompletionTUInfo,
Ilya Biryukov43714502018-05-16 12:32:44 +0000567 /*IncludeBriefComments*/ false);
Ilya Biryukov43714502018-05-16 12:32:44 +0000568 std::string Documentation =
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +0000569 formatDocumentation(*CCS, getDocComment(Ctx, SymbolCompletion,
570 /*CommentsFromHeaders=*/true));
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000571 if (!(S.Flags & Symbol::IndexedForCodeCompletion)) {
Haojian Wuda79dcc2019-02-25 16:00:00 +0000572 if (Opts.StoreAllDocumentation)
573 S.Documentation = Documentation;
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000574 Symbols.insert(S);
575 return Symbols.find(S.ID);
576 }
Haojian Wuda79dcc2019-02-25 16:00:00 +0000577 S.Documentation = Documentation;
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000578 std::string Signature;
579 std::string SnippetSuffix;
580 getSignature(*CCS, &Signature, &SnippetSuffix);
581 S.Signature = Signature;
582 S.CompletionSnippetSuffix = SnippetSuffix;
Sam McCalla68951e2018-06-22 16:11:35 +0000583 std::string ReturnType = getReturnType(*CCS);
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000584 S.ReturnType = ReturnType;
Sam McCall60039512018-02-09 14:42:01 +0000585
Eric Liuc5105f92018-02-16 14:15:55 +0000586 std::string Include;
587 if (Opts.CollectIncludePath && shouldCollectIncludePath(S.SymInfo.Kind)) {
588 // Use the expansion location to get the #include header since this is
589 // where the symbol is exposed.
Eric Liub96363d2018-03-01 18:06:40 +0000590 if (auto Header = getIncludeHeader(
591 QName, SM, SM.getExpansionLoc(ND.getLocation()), Opts))
Eric Liuc5105f92018-02-16 14:15:55 +0000592 Include = std::move(*Header);
593 }
Eric Liu83f63e42018-09-03 10:18:21 +0000594 if (!Include.empty())
595 S.IncludeHeaders.emplace_back(Include, 1);
Sam McCall60039512018-02-09 14:42:01 +0000596
Ilya Biryukov4d3d82e2018-11-26 15:52:16 +0000597 llvm::Optional<OpaqueType> TypeStorage;
Ilya Biryukova21392b2018-11-26 15:29:14 +0000598 if (S.Flags & Symbol::IndexedForCodeCompletion) {
Ilya Biryukov4d3d82e2018-11-26 15:52:16 +0000599 TypeStorage = OpaqueType::fromCompletionResult(*ASTCtx, SymbolCompletion);
600 if (TypeStorage)
601 S.Type = TypeStorage->raw();
Ilya Biryukova21392b2018-11-26 15:29:14 +0000602 }
603
Sam McCall60039512018-02-09 14:42:01 +0000604 Symbols.insert(S);
605 return Symbols.find(S.ID);
606}
607
608void SymbolCollector::addDefinition(const NamedDecl &ND,
609 const Symbol &DeclSym) {
610 if (DeclSym.Definition)
611 return;
612 // If we saw some forward declaration, we end up copying the symbol.
613 // This is not ideal, but avoids duplicating the "is this a definition" check
614 // in clang::index. We should only see one definition.
615 Symbol S = DeclSym;
616 std::string FileURI;
Eric Liuad588af2018-11-06 10:55:21 +0000617 auto Loc = findNameLoc(&ND);
618 const auto &SM = ND.getASTContext().getSourceManager();
619 // FIXME: use the result to filter out symbols.
620 shouldIndexFile(SM, SM.getFileID(Loc), Opts, &FilesToIndexCache);
621 if (auto DefLoc =
622 getTokenLocation(Loc, SM, Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000623 S.Definition = *DefLoc;
624 Symbols.insert(S);
625}
626
Haojian Wu4c1394d2017-12-12 15:42:10 +0000627} // namespace clangd
628} // namespace clang