blob: bd3db975b9cab4290862b43b87cecca1938ba04b [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 Wu7dd49502018-10-17 08:38:36 +000022#include "clang/Basic/SourceLocation.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000023#include "clang/Basic/SourceManager.h"
Eric Liua57afd02018-09-17 07:43:49 +000024#include "clang/Basic/Specifiers.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000025#include "clang/Index/IndexSymbol.h"
26#include "clang/Index/USRGeneration.h"
Eric Liua57afd02018-09-17 07:43:49 +000027#include "llvm/Support/Casting.h"
Eric Liu278e2d12018-01-29 15:13:29 +000028#include "llvm/Support/FileSystem.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000029#include "llvm/Support/MemoryBuffer.h"
30#include "llvm/Support/Path.h"
31
Sam McCallc008af62018-10-20 15:30:37 +000032using namespace llvm;
Haojian Wu4c1394d2017-12-12 15:42:10 +000033namespace 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.
Sam McCallc008af62018-10-20 15:30:37 +000054Optional<std::string> toURI(const SourceManager &SM, StringRef Path,
55 const SymbolCollector::Options &Opts) {
56 SmallString<128> AbsolutePath(Path);
Haojian Wu4c1394d2017-12-12 15:42:10 +000057 if (std::error_code EC =
58 SM.getFileManager().getVirtualFileSystem()->makeAbsolute(
59 AbsolutePath))
Sam McCallbed58852018-07-11 10:35:11 +000060 log("Warning: could not make absolute file: {0}", EC.message());
Sam McCallc008af62018-10-20 15:30:37 +000061 if (sys::path::is_absolute(AbsolutePath)) {
Eric Liu278e2d12018-01-29 15:13:29 +000062 // Handle the symbolic link path case where the current working directory
63 // (getCurrentWorkingDirectory) is a symlink./ We always want to the real
64 // file path (instead of the symlink path) for the C++ symbols.
65 //
66 // Consider the following example:
67 //
68 // src dir: /project/src/foo.h
69 // current working directory (symlink): /tmp/build -> /project/src/
70 //
71 // The file path of Symbol is "/project/src/foo.h" instead of
72 // "/tmp/build/foo.h"
73 if (const DirectoryEntry *Dir = SM.getFileManager().getDirectory(
Sam McCallc008af62018-10-20 15:30:37 +000074 sys::path::parent_path(AbsolutePath.str()))) {
Eric Liu278e2d12018-01-29 15:13:29 +000075 StringRef DirName = SM.getFileManager().getCanonicalName(Dir);
76 SmallString<128> AbsoluteFilename;
Sam McCallc008af62018-10-20 15:30:37 +000077 sys::path::append(AbsoluteFilename, DirName,
78 sys::path::filename(AbsolutePath.str()));
Eric Liu278e2d12018-01-29 15:13:29 +000079 AbsolutePath = AbsoluteFilename;
80 }
Eric Liu7f247652018-02-06 16:10:35 +000081 } else if (!Opts.FallbackDir.empty()) {
Sam McCallc008af62018-10-20 15:30:37 +000082 sys::fs::make_absolute(Opts.FallbackDir, AbsolutePath);
Haojian Wu4c1394d2017-12-12 15:42:10 +000083 }
Eric Liu7f247652018-02-06 16:10:35 +000084
Sam McCallc008af62018-10-20 15:30:37 +000085 sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/true);
Eric Liua0957702018-06-25 11:50:11 +000086
Eric Liu7f247652018-02-06 16:10:35 +000087 std::string ErrMsg;
88 for (const auto &Scheme : Opts.URISchemes) {
89 auto U = URI::create(AbsolutePath, Scheme);
90 if (U)
91 return U->toString();
Sam McCallc008af62018-10-20 15:30:37 +000092 ErrMsg += toString(U.takeError()) + "\n";
Eric Liu7f247652018-02-06 16:10:35 +000093 }
Sam McCallbed58852018-07-11 10:35:11 +000094 log("Failed to create an URI for file {0}: {1}", AbsolutePath, ErrMsg);
Sam McCallc008af62018-10-20 15:30:37 +000095 return None;
Haojian Wu4c1394d2017-12-12 15:42:10 +000096}
Eric Liu4feda802017-12-19 11:37:40 +000097
Eric Liud67ec242018-05-16 12:12:30 +000098// All proto generated headers should start with this line.
99static const char *PROTO_HEADER_COMMENT =
100 "// Generated by the protocol buffer compiler. DO NOT EDIT!";
101
102// Checks whether the decl is a private symbol in a header generated by
103// protobuf compiler.
104// To identify whether a proto header is actually generated by proto compiler,
105// we check whether it starts with PROTO_HEADER_COMMENT.
106// FIXME: make filtering extensible when there are more use cases for symbol
107// filters.
108bool isPrivateProtoDecl(const NamedDecl &ND) {
109 const auto &SM = ND.getASTContext().getSourceManager();
110 auto Loc = findNameLoc(&ND);
111 auto FileName = SM.getFilename(Loc);
112 if (!FileName.endswith(".proto.h") && !FileName.endswith(".pb.h"))
113 return false;
114 auto FID = SM.getFileID(Loc);
115 // Double check that this is an actual protobuf header.
116 if (!SM.getBufferData(FID).startswith(PROTO_HEADER_COMMENT))
117 return false;
118
119 // ND without identifier can be operators.
120 if (ND.getIdentifier() == nullptr)
121 return false;
122 auto Name = ND.getIdentifier()->getName();
123 if (!Name.contains('_'))
124 return false;
125 // Nested proto entities (e.g. Message::Nested) have top-level decls
126 // that shouldn't be used (Message_Nested). Ignore them completely.
127 // The nested entities are dangling type aliases, we may want to reconsider
128 // including them in the future.
129 // For enum constants, SOME_ENUM_CONSTANT is not private and should be
130 // indexed. Outer_INNER is private. This heuristic relies on naming style, it
131 // will include OUTER_INNER and exclude some_enum_constant.
132 // FIXME: the heuristic relies on naming style (i.e. no underscore in
133 // user-defined names) and can be improved.
Sam McCallc008af62018-10-20 15:30:37 +0000134 return (ND.getKind() != Decl::EnumConstant) || any_of(Name, islower);
Eric Liud67ec242018-05-16 12:12:30 +0000135}
136
Eric Liuc5105f92018-02-16 14:15:55 +0000137// We only collect #include paths for symbols that are suitable for global code
138// completion, except for namespaces since #include path for a namespace is hard
139// to define.
140bool shouldCollectIncludePath(index::SymbolKind Kind) {
141 using SK = index::SymbolKind;
142 switch (Kind) {
143 case SK::Macro:
144 case SK::Enum:
145 case SK::Struct:
146 case SK::Class:
147 case SK::Union:
148 case SK::TypeAlias:
149 case SK::Using:
150 case SK::Function:
151 case SK::Variable:
152 case SK::EnumConstant:
153 return true;
154 default:
155 return false;
156 }
157}
158
Eric Liu02ce01f2018-02-22 10:14:05 +0000159/// Gets a canonical include (URI of the header or <header> or "header") for
160/// header of \p Loc.
161/// Returns None if fails to get include header for \p Loc.
Sam McCallc008af62018-10-20 15:30:37 +0000162Optional<std::string> getIncludeHeader(StringRef QName, const SourceManager &SM,
163 SourceLocation Loc,
164 const SymbolCollector::Options &Opts) {
Eric Liu3cee95e2018-05-24 14:40:24 +0000165 std::vector<std::string> Headers;
166 // Collect the #include stack.
167 while (true) {
168 if (!Loc.isValid())
169 break;
170 auto FilePath = SM.getFilename(Loc);
171 if (FilePath.empty())
172 break;
173 Headers.push_back(FilePath);
174 if (SM.isInMainFile(Loc))
175 break;
176 Loc = SM.getIncludeLoc(SM.getFileID(Loc));
Eric Liuc5105f92018-02-16 14:15:55 +0000177 }
Eric Liu3cee95e2018-05-24 14:40:24 +0000178 if (Headers.empty())
Sam McCallc008af62018-10-20 15:30:37 +0000179 return None;
180 StringRef Header = Headers[0];
Eric Liu3cee95e2018-05-24 14:40:24 +0000181 if (Opts.Includes) {
182 Header = Opts.Includes->mapHeader(Headers, QName);
183 if (Header.startswith("<") || Header.startswith("\""))
184 return Header.str();
185 }
186 return toURI(SM, Header, Opts);
Eric Liuc5105f92018-02-16 14:15:55 +0000187}
188
Haojian Wud81e3142018-08-31 12:54:13 +0000189// Return the symbol range of the token at \p TokLoc.
190std::pair<SymbolLocation::Position, SymbolLocation::Position>
191getTokenRange(SourceLocation TokLoc, const SourceManager &SM,
192 const LangOptions &LangOpts) {
193 auto CreatePosition = [&SM](SourceLocation Loc) {
194 auto LSPLoc = sourceLocToPosition(SM, Loc);
195 SymbolLocation::Position Pos;
Haojian Wub515fab2018-10-18 10:43:50 +0000196 Pos.setLine(LSPLoc.line);
197 Pos.setColumn(LSPLoc.character);
Haojian Wud81e3142018-08-31 12:54:13 +0000198 return Pos;
199 };
200
201 auto TokenLength = clang::Lexer::MeasureTokenLength(TokLoc, SM, LangOpts);
202 return {CreatePosition(TokLoc),
203 CreatePosition(TokLoc.getLocWithOffset(TokenLength))};
204}
205
Eric Liuad588af2018-11-06 10:55:21 +0000206bool shouldIndexFile(const SourceManager &SM, FileID FID,
207 const SymbolCollector::Options &Opts,
208 llvm::DenseMap<FileID, bool> *FilesToIndexCache) {
209 if (!Opts.FileFilter)
210 return true;
211 auto I = FilesToIndexCache->try_emplace(FID);
212 if (I.second)
213 I.first->second = Opts.FileFilter(SM, FID);
214 return I.first->second;
215}
216
Haojian Wud81e3142018-08-31 12:54:13 +0000217// Return the symbol location of the token at \p TokLoc.
Sam McCallc008af62018-10-20 15:30:37 +0000218Optional<SymbolLocation> getTokenLocation(SourceLocation TokLoc,
219 const SourceManager &SM,
220 const SymbolCollector::Options &Opts,
221 const clang::LangOptions &LangOpts,
222 std::string &FileURIStorage) {
Eric Liu48db19e2018-07-09 15:31:07 +0000223 auto U = toURI(SM, SM.getFilename(TokLoc), Opts);
Eric Liu7f247652018-02-06 16:10:35 +0000224 if (!U)
Sam McCallc008af62018-10-20 15:30:37 +0000225 return None;
Eric Liu7f247652018-02-06 16:10:35 +0000226 FileURIStorage = std::move(*U);
Sam McCall60039512018-02-09 14:42:01 +0000227 SymbolLocation Result;
228 Result.FileURI = FileURIStorage;
Haojian Wud81e3142018-08-31 12:54:13 +0000229 auto Range = getTokenRange(TokLoc, SM, LangOpts);
230 Result.Start = Range.first;
231 Result.End = Range.second;
Haojian Wu545c02a2018-04-13 08:30:39 +0000232
Sam McCall60039512018-02-09 14:42:01 +0000233 return std::move(Result);
Haojian Wub0189062018-01-31 12:56:51 +0000234}
235
Eric Liucf8601b2018-02-28 09:33:15 +0000236// Checks whether \p ND is a definition of a TagDecl (class/struct/enum/union)
237// in a header file, in which case clangd would prefer to use ND as a canonical
238// declaration.
239// FIXME: handle symbol types that are not TagDecl (e.g. functions), if using
Fangrui Song943e12e2018-03-29 20:03:16 +0000240// the first seen declaration as canonical declaration is not a good enough
Eric Liucf8601b2018-02-28 09:33:15 +0000241// heuristic.
242bool isPreferredDeclaration(const NamedDecl &ND, index::SymbolRoleSet Roles) {
Sam McCall5fb97462018-10-05 14:03:04 +0000243 const auto& SM = ND.getASTContext().getSourceManager();
Eric Liucf8601b2018-02-28 09:33:15 +0000244 return (Roles & static_cast<unsigned>(index::SymbolRole::Definition)) &&
Sam McCallc008af62018-10-20 15:30:37 +0000245 isa<TagDecl>(&ND) &&
Sam McCall5fb97462018-10-05 14:03:04 +0000246 !SM.isWrittenInMainFile(SM.getExpansionLoc(ND.getLocation()));
Eric Liucf8601b2018-02-28 09:33:15 +0000247}
248
Sam McCallb0138312018-09-04 14:39:56 +0000249RefKind toRefKind(index::SymbolRoleSet Roles) {
250 return static_cast<RefKind>(static_cast<unsigned>(RefKind::All) & Roles);
Haojian Wud81e3142018-08-31 12:54:13 +0000251}
252
Eric Liua57afd02018-09-17 07:43:49 +0000253template <class T> bool explicitTemplateSpecialization(const NamedDecl &ND) {
Sam McCallc008af62018-10-20 15:30:37 +0000254 if (const auto *TD = dyn_cast<T>(&ND))
Eric Liua57afd02018-09-17 07:43:49 +0000255 if (TD->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
256 return true;
257 return false;
258}
259
Haojian Wu4c1394d2017-12-12 15:42:10 +0000260} // namespace
261
Eric Liu9af958f2018-01-10 14:57:58 +0000262SymbolCollector::SymbolCollector(Options Opts) : Opts(std::move(Opts)) {}
263
Eric Liu76f6b442018-01-09 17:32:00 +0000264void SymbolCollector::initialize(ASTContext &Ctx) {
265 ASTCtx = &Ctx;
266 CompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>();
267 CompletionTUInfo =
268 llvm::make_unique<CodeCompletionTUInfo>(CompletionAllocator);
269}
270
Eric Liu8763e482018-06-21 12:12:26 +0000271bool SymbolCollector::shouldCollectSymbol(const NamedDecl &ND,
272 ASTContext &ASTCtx,
273 const Options &Opts) {
Eric Liu8763e482018-06-21 12:12:26 +0000274 if (ND.isImplicit())
275 return false;
276 // Skip anonymous declarations, e.g (anonymous enum/class/struct).
277 if (ND.getDeclName().isEmpty())
278 return false;
279
280 // FIXME: figure out a way to handle internal linkage symbols (e.g. static
281 // variables, function) defined in the .cc files. Also we skip the symbols
282 // in anonymous namespace as the qualifier names of these symbols are like
283 // `foo::<anonymous>::bar`, which need a special handling.
284 // In real world projects, we have a relatively large set of header files
285 // that define static variables (like "static const int A = 1;"), we still
286 // want to collect these symbols, although they cause potential ODR
287 // violations.
288 if (ND.isInAnonymousNamespace())
289 return false;
290
291 // We want most things but not "local" symbols such as symbols inside
292 // FunctionDecl, BlockDecl, ObjCMethodDecl and OMPDeclareReductionDecl.
293 // FIXME: Need a matcher for ExportDecl in order to include symbols declared
294 // within an export.
Eric Liua57afd02018-09-17 07:43:49 +0000295 const auto *DeclCtx = ND.getDeclContext();
296 switch (DeclCtx->getDeclKind()) {
297 case Decl::TranslationUnit:
298 case Decl::Namespace:
299 case Decl::LinkageSpec:
300 case Decl::Enum:
301 case Decl::ObjCProtocol:
302 case Decl::ObjCInterface:
303 case Decl::ObjCCategory:
304 case Decl::ObjCCategoryImpl:
305 case Decl::ObjCImplementation:
306 break;
307 default:
308 // Record has a few derivations (e.g. CXXRecord, Class specialization), it's
309 // easier to cast.
Sam McCallc008af62018-10-20 15:30:37 +0000310 if (!isa<RecordDecl>(DeclCtx))
Eric Liua57afd02018-09-17 07:43:49 +0000311 return false;
312 }
313 if (explicitTemplateSpecialization<FunctionDecl>(ND) ||
314 explicitTemplateSpecialization<CXXRecordDecl>(ND) ||
315 explicitTemplateSpecialization<VarDecl>(ND))
Eric Liu8763e482018-06-21 12:12:26 +0000316 return false;
317
Eric Liua57afd02018-09-17 07:43:49 +0000318 const auto &SM = ASTCtx.getSourceManager();
319 // Skip decls in the main file.
320 if (SM.isInMainFile(SM.getExpansionLoc(ND.getBeginLoc())))
321 return false;
Eric Liu8763e482018-06-21 12:12:26 +0000322 // Avoid indexing internal symbols in protobuf generated headers.
323 if (isPrivateProtoDecl(ND))
324 return false;
325 return true;
326}
327
Haojian Wu4c1394d2017-12-12 15:42:10 +0000328// Always return true to continue indexing.
329bool SymbolCollector::handleDeclOccurence(
330 const Decl *D, index::SymbolRoleSet Roles,
Sam McCallb9d57112018-04-09 14:28:52 +0000331 ArrayRef<index::SymbolRelation> Relations, SourceLocation Loc,
Haojian Wu4c1394d2017-12-12 15:42:10 +0000332 index::IndexDataConsumer::ASTNodeInfo ASTNode) {
Eric Liu9af958f2018-01-10 14:57:58 +0000333 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
Sam McCall93f99bf2018-03-12 14:49:09 +0000334 assert(CompletionAllocator && CompletionTUInfo);
Eric Liu77d18112018-06-04 11:31:55 +0000335 assert(ASTNode.OrigD);
336 // If OrigD is an declaration associated with a friend declaration and it's
337 // not a definition, skip it. Note that OrigD is the occurrence that the
338 // collector is currently visiting.
339 if ((ASTNode.OrigD->getFriendObjectKind() !=
340 Decl::FriendObjectKind::FOK_None) &&
341 !(Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
342 return true;
343 // A declaration created for a friend declaration should not be used as the
344 // canonical declaration in the index. Use OrigD instead, unless we've already
345 // picked a replacement for D
346 if (D->getFriendObjectKind() != Decl::FriendObjectKind::FOK_None)
347 D = CanonicalDecls.try_emplace(D, ASTNode.OrigD).first->second;
Sam McCallc008af62018-10-20 15:30:37 +0000348 const NamedDecl *ND = dyn_cast<NamedDecl>(D);
Sam McCall93f99bf2018-03-12 14:49:09 +0000349 if (!ND)
350 return true;
Eric Liu9af958f2018-01-10 14:57:58 +0000351
Sam McCall93f99bf2018-03-12 14:49:09 +0000352 // Mark D as referenced if this is a reference coming from the main file.
353 // D may not be an interesting symbol, but it's cheaper to check at the end.
Sam McCallb9d57112018-04-09 14:28:52 +0000354 auto &SM = ASTCtx->getSourceManager();
Haojian Wud81e3142018-08-31 12:54:13 +0000355 auto SpellingLoc = SM.getSpellingLoc(Loc);
Sam McCall93f99bf2018-03-12 14:49:09 +0000356 if (Opts.CountReferences &&
357 (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
Haojian Wud81e3142018-08-31 12:54:13 +0000358 SM.getFileID(SpellingLoc) == SM.getMainFileID())
Sam McCall93f99bf2018-03-12 14:49:09 +0000359 ReferencedDecls.insert(ND);
360
Haojian Wue83cacc2018-10-15 11:46:26 +0000361 bool CollectRef = static_cast<unsigned>(Opts.RefFilter) & Roles;
362 bool IsOnlyRef =
363 !(Roles & (static_cast<unsigned>(index::SymbolRole::Declaration) |
364 static_cast<unsigned>(index::SymbolRole::Definition)));
Haojian Wud81e3142018-08-31 12:54:13 +0000365
Haojian Wue83cacc2018-10-15 11:46:26 +0000366 if (IsOnlyRef && !CollectRef)
Haojian Wu4c1394d2017-12-12 15:42:10 +0000367 return true;
Eric Liu8763e482018-06-21 12:12:26 +0000368 if (!shouldCollectSymbol(*ND, *ASTCtx, Opts))
Sam McCall93f99bf2018-03-12 14:49:09 +0000369 return true;
Haojian Wu7dd49502018-10-17 08:38:36 +0000370 if (CollectRef &&
371 (Opts.RefsInHeaders || SM.getFileID(SpellingLoc) == SM.getMainFileID()))
Haojian Wue83cacc2018-10-15 11:46:26 +0000372 DeclRefs[ND].emplace_back(SpellingLoc, Roles);
373 // Don't continue indexing if this is a mere reference.
374 if (IsOnlyRef)
375 return true;
Haojian Wu4c1394d2017-12-12 15:42:10 +0000376
Haojian Wuc6ddb462018-08-07 08:57:52 +0000377 auto ID = getSymbolID(ND);
378 if (!ID)
Sam McCall93f99bf2018-03-12 14:49:09 +0000379 return true;
Eric Liu76f6b442018-01-09 17:32:00 +0000380
Sam McCall93f99bf2018-03-12 14:49:09 +0000381 const NamedDecl &OriginalDecl = *cast<NamedDecl>(ASTNode.OrigD);
Haojian Wuc6ddb462018-08-07 08:57:52 +0000382 const Symbol *BasicSymbol = Symbols.find(*ID);
Sam McCall93f99bf2018-03-12 14:49:09 +0000383 if (!BasicSymbol) // Regardless of role, ND is the canonical declaration.
Haojian Wuc6ddb462018-08-07 08:57:52 +0000384 BasicSymbol = addDeclaration(*ND, std::move(*ID));
Sam McCall93f99bf2018-03-12 14:49:09 +0000385 else if (isPreferredDeclaration(OriginalDecl, Roles))
386 // If OriginalDecl is preferred, replace the existing canonical
387 // declaration (e.g. a class forward declaration). There should be at most
388 // one duplicate as we expect to see only one preferred declaration per
389 // TU, because in practice they are definitions.
Haojian Wuc6ddb462018-08-07 08:57:52 +0000390 BasicSymbol = addDeclaration(OriginalDecl, std::move(*ID));
Haojian Wu4c1394d2017-12-12 15:42:10 +0000391
Sam McCall93f99bf2018-03-12 14:49:09 +0000392 if (Roles & static_cast<unsigned>(index::SymbolRole::Definition))
393 addDefinition(OriginalDecl, *BasicSymbol);
Haojian Wu4c1394d2017-12-12 15:42:10 +0000394 return true;
395}
396
Eric Liu48db19e2018-07-09 15:31:07 +0000397bool SymbolCollector::handleMacroOccurence(const IdentifierInfo *Name,
398 const MacroInfo *MI,
399 index::SymbolRoleSet Roles,
400 SourceLocation Loc) {
401 if (!Opts.CollectMacro)
402 return true;
403 assert(PP.get());
404
405 const auto &SM = PP->getSourceManager();
Eric Liuad588af2018-11-06 10:55:21 +0000406 auto DefLoc = MI->getDefinitionLoc();
407 if (SM.isInMainFile(SM.getExpansionLoc(DefLoc)))
Eric Liu48db19e2018-07-09 15:31:07 +0000408 return true;
409 // Header guards are not interesting in index. Builtin macros don't have
410 // useful locations and are not needed for code completions.
411 if (MI->isUsedForHeaderGuard() || MI->isBuiltinMacro())
412 return true;
413
414 // Mark the macro as referenced if this is a reference coming from the main
415 // file. The macro may not be an interesting symbol, but it's cheaper to check
416 // at the end.
417 if (Opts.CountReferences &&
418 (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
419 SM.getFileID(SM.getSpellingLoc(Loc)) == SM.getMainFileID())
420 ReferencedMacros.insert(Name);
421 // Don't continue indexing if this is a mere reference.
422 // FIXME: remove macro with ID if it is undefined.
423 if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) ||
424 Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
425 return true;
426
Eric Liud25f1212018-09-06 09:59:37 +0000427 auto ID = getSymbolID(*Name, MI, SM);
428 if (!ID)
Eric Liu48db19e2018-07-09 15:31:07 +0000429 return true;
Eric Liu48db19e2018-07-09 15:31:07 +0000430
431 // Only collect one instance in case there are multiple.
Eric Liud25f1212018-09-06 09:59:37 +0000432 if (Symbols.find(*ID) != nullptr)
Eric Liu48db19e2018-07-09 15:31:07 +0000433 return true;
434
435 Symbol S;
Eric Liud25f1212018-09-06 09:59:37 +0000436 S.ID = std::move(*ID);
Eric Liu48db19e2018-07-09 15:31:07 +0000437 S.Name = Name->getName();
Eric Liu6df66002018-09-06 18:52:26 +0000438 S.Flags |= Symbol::IndexedForCodeCompletion;
Eric Liu48db19e2018-07-09 15:31:07 +0000439 S.SymInfo = index::getSymbolInfoForMacro(*MI);
440 std::string FileURI;
Eric Liuad588af2018-11-06 10:55:21 +0000441 // FIXME: use the result to filter out symbols.
442 shouldIndexFile(SM, SM.getFileID(Loc), Opts, &FilesToIndexCache);
443 if (auto DeclLoc =
444 getTokenLocation(DefLoc, SM, Opts, PP->getLangOpts(), FileURI))
Eric Liu48db19e2018-07-09 15:31:07 +0000445 S.CanonicalDeclaration = *DeclLoc;
446
447 CodeCompletionResult SymbolCompletion(Name);
448 const auto *CCS = SymbolCompletion.CreateCodeCompletionStringForMacro(
449 *PP, *CompletionAllocator, *CompletionTUInfo);
450 std::string Signature;
451 std::string SnippetSuffix;
452 getSignature(*CCS, &Signature, &SnippetSuffix);
453
454 std::string Include;
455 if (Opts.CollectIncludePath && shouldCollectIncludePath(S.SymInfo.Kind)) {
Eric Liuad588af2018-11-06 10:55:21 +0000456 if (auto Header = getIncludeHeader(Name->getName(), SM,
457 SM.getExpansionLoc(DefLoc), Opts))
Eric Liu48db19e2018-07-09 15:31:07 +0000458 Include = std::move(*Header);
459 }
460 S.Signature = Signature;
461 S.CompletionSnippetSuffix = SnippetSuffix;
Eric Liu83f63e42018-09-03 10:18:21 +0000462 if (!Include.empty())
463 S.IncludeHeaders.emplace_back(Include, 1);
464
Eric Liu48db19e2018-07-09 15:31:07 +0000465 Symbols.insert(S);
466 return true;
467}
468
Sam McCall93f99bf2018-03-12 14:49:09 +0000469void SymbolCollector::finish() {
Eric Liu48db19e2018-07-09 15:31:07 +0000470 // At the end of the TU, add 1 to the refcount of all referenced symbols.
471 auto IncRef = [this](const SymbolID &ID) {
472 if (const auto *S = Symbols.find(ID)) {
473 Symbol Inc = *S;
474 ++Inc.References;
475 Symbols.insert(Inc);
476 }
477 };
478 for (const NamedDecl *ND : ReferencedDecls) {
Haojian Wuc6ddb462018-08-07 08:57:52 +0000479 if (auto ID = getSymbolID(ND)) {
480 IncRef(*ID);
481 }
Eric Liu48db19e2018-07-09 15:31:07 +0000482 }
483 if (Opts.CollectMacro) {
484 assert(PP);
485 for (const IdentifierInfo *II : ReferencedMacros) {
Eric Liua62c9d62018-07-09 18:54:51 +0000486 if (const auto *MI = PP->getMacroDefinition(II).getMacroInfo())
Eric Liud25f1212018-09-06 09:59:37 +0000487 if (auto ID = getSymbolID(*II, MI, PP->getSourceManager()))
488 IncRef(*ID);
Eric Liu48db19e2018-07-09 15:31:07 +0000489 }
Sam McCall93f99bf2018-03-12 14:49:09 +0000490 }
Haojian Wud81e3142018-08-31 12:54:13 +0000491
492 const auto &SM = ASTCtx->getSourceManager();
Sam McCallc008af62018-10-20 15:30:37 +0000493 DenseMap<FileID, std::string> URICache;
494 auto GetURI = [&](FileID FID) -> Optional<std::string> {
Haojian Wu7dd49502018-10-17 08:38:36 +0000495 auto Found = URICache.find(FID);
496 if (Found == URICache.end()) {
Haojian Wu7dd49502018-10-17 08:38:36 +0000497 if (auto *FileEntry = SM.getFileEntryForID(FID)) {
498 auto FileURI = toURI(SM, FileEntry->getName(), Opts);
499 if (!FileURI) {
500 log("Failed to create URI for file: {0}\n", FileEntry);
501 FileURI = ""; // reset to empty as we also want to cache this case.
502 }
503 Found = URICache.insert({FID, *FileURI}).first;
Haojian Wuc014d862018-10-17 08:54:48 +0000504 } else {
505 // Ignore cases where we can not find a corresponding file entry
506 // for the loc, thoses are not interesting, e.g. symbols formed
507 // via macro concatenation.
Sam McCallc008af62018-10-20 15:30:37 +0000508 return None;
Haojian Wu7dd49502018-10-17 08:38:36 +0000509 }
510 }
511 return Found->second;
512 };
Haojian Wud81e3142018-08-31 12:54:13 +0000513
Haojian Wu7dd49502018-10-17 08:38:36 +0000514 if (auto MainFileURI = GetURI(SM.getMainFileID())) {
Sam McCallb0138312018-09-04 14:39:56 +0000515 for (const auto &It : DeclRefs) {
Haojian Wud81e3142018-08-31 12:54:13 +0000516 if (auto ID = getSymbolID(It.first)) {
Haojian Wue83cacc2018-10-15 11:46:26 +0000517 for (const auto &LocAndRole : It.second) {
Haojian Wu7dd49502018-10-17 08:38:36 +0000518 auto FileID = SM.getFileID(LocAndRole.first);
Eric Liuad588af2018-11-06 10:55:21 +0000519 // FIXME: use the result to filter out references.
520 shouldIndexFile(SM, FileID, Opts, &FilesToIndexCache);
Haojian Wu7dd49502018-10-17 08:38:36 +0000521 if (auto FileURI = GetURI(FileID)) {
522 auto Range =
523 getTokenRange(LocAndRole.first, SM, ASTCtx->getLangOpts());
524 Ref R;
525 R.Location.Start = Range.first;
526 R.Location.End = Range.second;
527 R.Location.FileURI = *FileURI;
528 R.Kind = toRefKind(LocAndRole.second);
529 Refs.insert(*ID, R);
530 }
Haojian Wud81e3142018-08-31 12:54:13 +0000531 }
532 }
533 }
Haojian Wud81e3142018-08-31 12:54:13 +0000534 }
535
Sam McCall93f99bf2018-03-12 14:49:09 +0000536 ReferencedDecls.clear();
Eric Liu48db19e2018-07-09 15:31:07 +0000537 ReferencedMacros.clear();
Sam McCallb0138312018-09-04 14:39:56 +0000538 DeclRefs.clear();
Eric Liuad588af2018-11-06 10:55:21 +0000539 FilesToIndexCache.clear();
Sam McCall93f99bf2018-03-12 14:49:09 +0000540}
541
Sam McCall60039512018-02-09 14:42:01 +0000542const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND,
543 SymbolID ID) {
Ilya Biryukov43714502018-05-16 12:32:44 +0000544 auto &Ctx = ND.getASTContext();
545 auto &SM = Ctx.getSourceManager();
Sam McCall60039512018-02-09 14:42:01 +0000546
Sam McCall60039512018-02-09 14:42:01 +0000547 Symbol S;
548 S.ID = std::move(ID);
Eric Liu7ad16962018-06-22 10:46:59 +0000549 std::string QName = printQualifiedName(ND);
Sam McCall60039512018-02-09 14:42:01 +0000550 std::tie(S.Scope, S.Name) = splitQualifiedName(QName);
Sam McCall032db942018-06-22 06:41:43 +0000551 // FIXME: this returns foo:bar: for objective-C methods, we prefer only foo:
552 // for consistency with CodeCompletionString and a clean name/signature split.
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +0000553
Eric Liu6df66002018-09-06 18:52:26 +0000554 if (isIndexedForCodeCompletion(ND, Ctx))
555 S.Flags |= Symbol::IndexedForCodeCompletion;
Eric Liu48597382018-10-18 12:23:05 +0000556 if (isImplementationDetail(&ND))
557 S.Flags |= Symbol::ImplementationDetail;
Sam McCall60039512018-02-09 14:42:01 +0000558 S.SymInfo = index::getSymbolInfo(&ND);
559 std::string FileURI;
Eric Liuad588af2018-11-06 10:55:21 +0000560 auto Loc = findNameLoc(&ND);
561 // FIXME: use the result to filter out symbols.
562 shouldIndexFile(SM, SM.getFileID(Loc), Opts, &FilesToIndexCache);
563 if (auto DeclLoc =
564 getTokenLocation(Loc, SM, Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000565 S.CanonicalDeclaration = *DeclLoc;
566
567 // Add completion info.
568 // FIXME: we may want to choose a different redecl, or combine from several.
569 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
Ilya Biryukovcf124bd2018-04-13 11:03:07 +0000570 // We use the primary template, as clang does during code completion.
571 CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0);
Sam McCall60039512018-02-09 14:42:01 +0000572 const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
Kadir Cetinkayab9157902018-10-24 15:24:29 +0000573 *ASTCtx, *PP, CodeCompletionContext::CCC_Symbol, *CompletionAllocator,
Sam McCall60039512018-02-09 14:42:01 +0000574 *CompletionTUInfo,
Ilya Biryukov43714502018-05-16 12:32:44 +0000575 /*IncludeBriefComments*/ false);
Sam McCalla68951e2018-06-22 16:11:35 +0000576 std::string Signature;
577 std::string SnippetSuffix;
578 getSignature(*CCS, &Signature, &SnippetSuffix);
Ilya Biryukov43714502018-05-16 12:32:44 +0000579 std::string Documentation =
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +0000580 formatDocumentation(*CCS, getDocComment(Ctx, SymbolCompletion,
581 /*CommentsFromHeaders=*/true));
Sam McCalla68951e2018-06-22 16:11:35 +0000582 std::string ReturnType = getReturnType(*CCS);
Sam McCall60039512018-02-09 14:42:01 +0000583
Eric Liuc5105f92018-02-16 14:15:55 +0000584 std::string Include;
585 if (Opts.CollectIncludePath && shouldCollectIncludePath(S.SymInfo.Kind)) {
586 // Use the expansion location to get the #include header since this is
587 // where the symbol is exposed.
Eric Liub96363d2018-03-01 18:06:40 +0000588 if (auto Header = getIncludeHeader(
589 QName, SM, SM.getExpansionLoc(ND.getLocation()), Opts))
Eric Liuc5105f92018-02-16 14:15:55 +0000590 Include = std::move(*Header);
591 }
Sam McCalla68951e2018-06-22 16:11:35 +0000592 S.Signature = Signature;
593 S.CompletionSnippetSuffix = SnippetSuffix;
Sam McCall2e5700f2018-08-31 13:55:01 +0000594 S.Documentation = Documentation;
595 S.ReturnType = ReturnType;
Eric Liu83f63e42018-09-03 10:18:21 +0000596 if (!Include.empty())
597 S.IncludeHeaders.emplace_back(Include, 1);
Sam McCall60039512018-02-09 14:42:01 +0000598
Sam McCall2161ec72018-07-05 06:20:41 +0000599 S.Origin = Opts.Origin;
Eric Liu6df66002018-09-06 18:52:26 +0000600 if (ND.getAvailability() == AR_Deprecated)
601 S.Flags |= Symbol::Deprecated;
Sam McCall60039512018-02-09 14:42:01 +0000602 Symbols.insert(S);
603 return Symbols.find(S.ID);
604}
605
606void SymbolCollector::addDefinition(const NamedDecl &ND,
607 const Symbol &DeclSym) {
608 if (DeclSym.Definition)
609 return;
610 // If we saw some forward declaration, we end up copying the symbol.
611 // This is not ideal, but avoids duplicating the "is this a definition" check
612 // in clang::index. We should only see one definition.
613 Symbol S = DeclSym;
614 std::string FileURI;
Eric Liuad588af2018-11-06 10:55:21 +0000615 auto Loc = findNameLoc(&ND);
616 const auto &SM = ND.getASTContext().getSourceManager();
617 // FIXME: use the result to filter out symbols.
618 shouldIndexFile(SM, SM.getFileID(Loc), Opts, &FilesToIndexCache);
619 if (auto DefLoc =
620 getTokenLocation(Loc, SM, Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000621 S.Definition = *DefLoc;
622 Symbols.insert(S);
623}
624
Haojian Wu4c1394d2017-12-12 15:42:10 +0000625} // namespace clangd
626} // namespace clang