blob: 06fe854b2e0bbaa971d95c6f46f99fab42cb5c28 [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"
Sam McCall1b29dec2019-05-02 16:12:36 +000027#include "clang/Index/IndexingAction.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000028#include "clang/Index/USRGeneration.h"
Sam McCall62e24722019-04-17 10:36:02 +000029#include "clang/Lex/Preprocessor.h"
Eric Liua57afd02018-09-17 07:43:49 +000030#include "llvm/Support/Casting.h"
Eric Liu278e2d12018-01-29 15:13:29 +000031#include "llvm/Support/FileSystem.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000032#include "llvm/Support/MemoryBuffer.h"
33#include "llvm/Support/Path.h"
34
35namespace clang {
36namespace clangd {
Haojian Wu4c1394d2017-12-12 15:42:10 +000037namespace {
Sam McCallc008af62018-10-20 15:30:37 +000038
Ilya Biryukovf118d512018-04-14 16:27:35 +000039/// If \p ND is a template specialization, returns the described template.
Ilya Biryukovcf124bd2018-04-13 11:03:07 +000040/// Otherwise, returns \p ND.
41const NamedDecl &getTemplateOrThis(const NamedDecl &ND) {
Ilya Biryukovf118d512018-04-14 16:27:35 +000042 if (auto T = ND.getDescribedTemplate())
43 return *T;
Ilya Biryukovcf124bd2018-04-13 11:03:07 +000044 return ND;
45}
46
Eric Liu7f247652018-02-06 16:10:35 +000047// Returns a URI of \p Path. Firstly, this makes the \p Path absolute using the
48// current working directory of the given SourceManager if the Path is not an
49// absolute path. If failed, this resolves relative paths against \p FallbackDir
50// to get an absolute path. Then, this tries creating an URI for the absolute
51// path with schemes specified in \p Opts. This returns an URI with the first
52// working scheme, if there is any; otherwise, this returns None.
Haojian Wu4c1394d2017-12-12 15:42:10 +000053//
54// The Path can be a path relative to the build directory, or retrieved from
55// the SourceManager.
Kadir Cetinkayadd677932018-12-19 10:46:21 +000056std::string toURI(const SourceManager &SM, llvm::StringRef Path,
57 const SymbolCollector::Options &Opts) {
58 llvm::SmallString<128> AbsolutePath(Path);
Harlan Haskinsa02f8572019-08-01 21:32:01 +000059 if (auto File = SM.getFileManager().getFile(Path)) {
60 if (auto CanonPath = getCanonicalPath(*File, SM)) {
61 AbsolutePath = *CanonPath;
62 }
Haojian Wu4c1394d2017-12-12 15:42:10 +000063 }
Kadir Cetinkayadd677932018-12-19 10:46:21 +000064 // We don't perform is_absolute check in an else branch because makeAbsolute
65 // might return a relative path on some InMemoryFileSystems.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000066 if (!llvm::sys::path::is_absolute(AbsolutePath) && !Opts.FallbackDir.empty())
67 llvm::sys::fs::make_absolute(Opts.FallbackDir, AbsolutePath);
68 llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/true);
Eric Liuc0ac4bb2018-11-22 15:02:05 +000069 return URI::create(AbsolutePath).toString();
Haojian Wu4c1394d2017-12-12 15:42:10 +000070}
Eric Liu4feda802017-12-19 11:37:40 +000071
Eric Liud67ec242018-05-16 12:12:30 +000072// All proto generated headers should start with this line.
73static const char *PROTO_HEADER_COMMENT =
74 "// Generated by the protocol buffer compiler. DO NOT EDIT!";
75
76// Checks whether the decl is a private symbol in a header generated by
77// protobuf compiler.
78// To identify whether a proto header is actually generated by proto compiler,
79// we check whether it starts with PROTO_HEADER_COMMENT.
80// FIXME: make filtering extensible when there are more use cases for symbol
81// filters.
82bool isPrivateProtoDecl(const NamedDecl &ND) {
83 const auto &SM = ND.getASTContext().getSourceManager();
Sam McCall95738072019-08-06 20:25:59 +000084 auto Loc = spellingLocIfSpelled(findName(&ND), SM);
Eric Liud67ec242018-05-16 12:12:30 +000085 auto FileName = SM.getFilename(Loc);
86 if (!FileName.endswith(".proto.h") && !FileName.endswith(".pb.h"))
87 return false;
88 auto FID = SM.getFileID(Loc);
89 // Double check that this is an actual protobuf header.
90 if (!SM.getBufferData(FID).startswith(PROTO_HEADER_COMMENT))
91 return false;
92
93 // ND without identifier can be operators.
94 if (ND.getIdentifier() == nullptr)
95 return false;
96 auto Name = ND.getIdentifier()->getName();
97 if (!Name.contains('_'))
98 return false;
99 // Nested proto entities (e.g. Message::Nested) have top-level decls
100 // that shouldn't be used (Message_Nested). Ignore them completely.
101 // The nested entities are dangling type aliases, we may want to reconsider
102 // including them in the future.
103 // For enum constants, SOME_ENUM_CONSTANT is not private and should be
104 // indexed. Outer_INNER is private. This heuristic relies on naming style, it
105 // will include OUTER_INNER and exclude some_enum_constant.
106 // FIXME: the heuristic relies on naming style (i.e. no underscore in
107 // user-defined names) and can be improved.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000108 return (ND.getKind() != Decl::EnumConstant) || llvm::any_of(Name, islower);
Eric Liud67ec242018-05-16 12:12:30 +0000109}
110
Eric Liuc5105f92018-02-16 14:15:55 +0000111// We only collect #include paths for symbols that are suitable for global code
112// completion, except for namespaces since #include path for a namespace is hard
113// to define.
114bool shouldCollectIncludePath(index::SymbolKind Kind) {
115 using SK = index::SymbolKind;
116 switch (Kind) {
117 case SK::Macro:
118 case SK::Enum:
119 case SK::Struct:
120 case SK::Class:
121 case SK::Union:
122 case SK::TypeAlias:
123 case SK::Using:
124 case SK::Function:
125 case SK::Variable:
126 case SK::EnumConstant:
127 return true;
128 default:
129 return false;
130 }
131}
132
Haojian Wud81e3142018-08-31 12:54:13 +0000133// Return the symbol range of the token at \p TokLoc.
134std::pair<SymbolLocation::Position, SymbolLocation::Position>
135getTokenRange(SourceLocation TokLoc, const SourceManager &SM,
136 const LangOptions &LangOpts) {
137 auto CreatePosition = [&SM](SourceLocation Loc) {
138 auto LSPLoc = sourceLocToPosition(SM, Loc);
139 SymbolLocation::Position Pos;
Haojian Wub515fab2018-10-18 10:43:50 +0000140 Pos.setLine(LSPLoc.line);
141 Pos.setColumn(LSPLoc.character);
Haojian Wud81e3142018-08-31 12:54:13 +0000142 return Pos;
143 };
144
145 auto TokenLength = clang::Lexer::MeasureTokenLength(TokLoc, SM, LangOpts);
146 return {CreatePosition(TokLoc),
147 CreatePosition(TokLoc.getLocWithOffset(TokenLength))};
148}
149
150// Return the symbol location of the token at \p TokLoc.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000151llvm::Optional<SymbolLocation>
152getTokenLocation(SourceLocation TokLoc, const SourceManager &SM,
153 const SymbolCollector::Options &Opts,
154 const clang::LangOptions &LangOpts,
155 std::string &FileURIStorage) {
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000156 auto Path = SM.getFilename(TokLoc);
157 if (Path.empty())
Sam McCallc008af62018-10-20 15:30:37 +0000158 return None;
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000159 FileURIStorage = toURI(SM, Path, Opts);
Sam McCall60039512018-02-09 14:42:01 +0000160 SymbolLocation Result;
Haojian Wuee54a2b2018-11-14 11:55:45 +0000161 Result.FileURI = FileURIStorage.c_str();
Haojian Wud81e3142018-08-31 12:54:13 +0000162 auto Range = getTokenRange(TokLoc, SM, LangOpts);
163 Result.Start = Range.first;
164 Result.End = Range.second;
Haojian Wu545c02a2018-04-13 08:30:39 +0000165
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000166 return Result;
Haojian Wub0189062018-01-31 12:56:51 +0000167}
168
Eric Liucf8601b2018-02-28 09:33:15 +0000169// Checks whether \p ND is a definition of a TagDecl (class/struct/enum/union)
170// in a header file, in which case clangd would prefer to use ND as a canonical
171// declaration.
172// FIXME: handle symbol types that are not TagDecl (e.g. functions), if using
Fangrui Song943e12e2018-03-29 20:03:16 +0000173// the first seen declaration as canonical declaration is not a good enough
Eric Liucf8601b2018-02-28 09:33:15 +0000174// heuristic.
175bool isPreferredDeclaration(const NamedDecl &ND, index::SymbolRoleSet Roles) {
Kadir Cetinkaya017cc6c2019-03-08 09:54:37 +0000176 const auto &SM = ND.getASTContext().getSourceManager();
Eric Liucf8601b2018-02-28 09:33:15 +0000177 return (Roles & static_cast<unsigned>(index::SymbolRole::Definition)) &&
Haojian Wu6ae86ea2019-07-19 08:33:39 +0000178 isa<TagDecl>(&ND) && !isInsideMainFile(ND.getLocation(), SM);
Eric Liucf8601b2018-02-28 09:33:15 +0000179}
180
Sam McCallb0138312018-09-04 14:39:56 +0000181RefKind toRefKind(index::SymbolRoleSet Roles) {
182 return static_cast<RefKind>(static_cast<unsigned>(RefKind::All) & Roles);
Haojian Wud81e3142018-08-31 12:54:13 +0000183}
184
Nathan Ridge73e6f472019-06-04 04:25:44 +0000185bool shouldIndexRelation(const index::SymbolRelation &R) {
186 // We currently only index BaseOf relations, for type hierarchy subtypes.
187 return R.Roles & static_cast<unsigned>(index::SymbolRole::RelationBaseOf);
188}
189
Haojian Wu4c1394d2017-12-12 15:42:10 +0000190} // namespace
191
Eric Liu9af958f2018-01-10 14:57:58 +0000192SymbolCollector::SymbolCollector(Options Opts) : Opts(std::move(Opts)) {}
193
Eric Liu76f6b442018-01-09 17:32:00 +0000194void SymbolCollector::initialize(ASTContext &Ctx) {
195 ASTCtx = &Ctx;
196 CompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>();
197 CompletionTUInfo =
Jonas Devlieghere1c705d92019-08-14 23:52:23 +0000198 std::make_unique<CodeCompletionTUInfo>(CompletionAllocator);
Eric Liu76f6b442018-01-09 17:32:00 +0000199}
200
Eric Liu8763e482018-06-21 12:12:26 +0000201bool SymbolCollector::shouldCollectSymbol(const NamedDecl &ND,
Haojian Wu7800dbe2018-12-03 13:16:04 +0000202 const ASTContext &ASTCtx,
Sam McCall0e93b072019-01-14 10:01:17 +0000203 const Options &Opts,
204 bool IsMainFileOnly) {
Eric Liu8763e482018-06-21 12:12:26 +0000205 // Skip anonymous declarations, e.g (anonymous enum/class/struct).
206 if (ND.getDeclName().isEmpty())
207 return false;
208
Sam McCall0e93b072019-01-14 10:01:17 +0000209 // Skip main-file symbols if we are not collecting them.
210 if (IsMainFileOnly && !Opts.CollectMainFileSymbols)
211 return false;
212
213 // Skip symbols in anonymous namespaces in header files.
214 if (!IsMainFileOnly && ND.isInAnonymousNamespace())
Eric Liu8763e482018-06-21 12:12:26 +0000215 return false;
216
217 // We want most things but not "local" symbols such as symbols inside
218 // FunctionDecl, BlockDecl, ObjCMethodDecl and OMPDeclareReductionDecl.
219 // FIXME: Need a matcher for ExportDecl in order to include symbols declared
220 // within an export.
Eric Liua57afd02018-09-17 07:43:49 +0000221 const auto *DeclCtx = ND.getDeclContext();
222 switch (DeclCtx->getDeclKind()) {
223 case Decl::TranslationUnit:
224 case Decl::Namespace:
225 case Decl::LinkageSpec:
226 case Decl::Enum:
227 case Decl::ObjCProtocol:
228 case Decl::ObjCInterface:
229 case Decl::ObjCCategory:
230 case Decl::ObjCCategoryImpl:
231 case Decl::ObjCImplementation:
232 break;
233 default:
234 // Record has a few derivations (e.g. CXXRecord, Class specialization), it's
235 // easier to cast.
Sam McCallc008af62018-10-20 15:30:37 +0000236 if (!isa<RecordDecl>(DeclCtx))
Eric Liua57afd02018-09-17 07:43:49 +0000237 return false;
238 }
Eric Liu8763e482018-06-21 12:12:26 +0000239
240 // Avoid indexing internal symbols in protobuf generated headers.
241 if (isPrivateProtoDecl(ND))
242 return false;
243 return true;
244}
245
Haojian Wu4c1394d2017-12-12 15:42:10 +0000246// Always return true to continue indexing.
247bool SymbolCollector::handleDeclOccurence(
248 const Decl *D, index::SymbolRoleSet Roles,
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000249 llvm::ArrayRef<index::SymbolRelation> Relations, SourceLocation Loc,
Haojian Wu4c1394d2017-12-12 15:42:10 +0000250 index::IndexDataConsumer::ASTNodeInfo ASTNode) {
Eric Liu9af958f2018-01-10 14:57:58 +0000251 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
Sam McCall93f99bf2018-03-12 14:49:09 +0000252 assert(CompletionAllocator && CompletionTUInfo);
Eric Liu77d18112018-06-04 11:31:55 +0000253 assert(ASTNode.OrigD);
Kadir Cetinkayabb6cd822019-04-15 14:38:46 +0000254 // Indexing API puts cannonical decl into D, which might not have a valid
255 // source location for implicit/built-in decls. Fallback to original decl in
256 // such cases.
257 if (D->getLocation().isInvalid())
258 D = ASTNode.OrigD;
Eric Liu77d18112018-06-04 11:31:55 +0000259 // If OrigD is an declaration associated with a friend declaration and it's
260 // not a definition, skip it. Note that OrigD is the occurrence that the
261 // collector is currently visiting.
262 if ((ASTNode.OrigD->getFriendObjectKind() !=
263 Decl::FriendObjectKind::FOK_None) &&
264 !(Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
265 return true;
266 // A declaration created for a friend declaration should not be used as the
267 // canonical declaration in the index. Use OrigD instead, unless we've already
268 // picked a replacement for D
269 if (D->getFriendObjectKind() != Decl::FriendObjectKind::FOK_None)
270 D = CanonicalDecls.try_emplace(D, ASTNode.OrigD).first->second;
Sam McCallc008af62018-10-20 15:30:37 +0000271 const NamedDecl *ND = dyn_cast<NamedDecl>(D);
Sam McCall93f99bf2018-03-12 14:49:09 +0000272 if (!ND)
273 return true;
Eric Liu9af958f2018-01-10 14:57:58 +0000274
Sam McCall93f99bf2018-03-12 14:49:09 +0000275 // Mark D as referenced if this is a reference coming from the main file.
276 // D may not be an interesting symbol, but it's cheaper to check at the end.
Sam McCallb9d57112018-04-09 14:28:52 +0000277 auto &SM = ASTCtx->getSourceManager();
Haojian Wud81e3142018-08-31 12:54:13 +0000278 auto SpellingLoc = SM.getSpellingLoc(Loc);
Sam McCall93f99bf2018-03-12 14:49:09 +0000279 if (Opts.CountReferences &&
280 (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
Haojian Wud81e3142018-08-31 12:54:13 +0000281 SM.getFileID(SpellingLoc) == SM.getMainFileID())
Sam McCall93f99bf2018-03-12 14:49:09 +0000282 ReferencedDecls.insert(ND);
283
Nathan Ridge73e6f472019-06-04 04:25:44 +0000284 auto ID = getSymbolID(ND);
285 if (!ID)
286 return true;
287
288 // Note: we need to process relations for all decl occurrences, including
289 // refs, because the indexing code only populates relations for specific
290 // occurrences. For example, RelationBaseOf is only populated for the
291 // occurrence inside the base-specifier.
292 processRelations(*ND, *ID, Relations);
293
Haojian Wue83cacc2018-10-15 11:46:26 +0000294 bool CollectRef = static_cast<unsigned>(Opts.RefFilter) & Roles;
295 bool IsOnlyRef =
296 !(Roles & (static_cast<unsigned>(index::SymbolRole::Declaration) |
297 static_cast<unsigned>(index::SymbolRole::Definition)));
Haojian Wud81e3142018-08-31 12:54:13 +0000298
Haojian Wue83cacc2018-10-15 11:46:26 +0000299 if (IsOnlyRef && !CollectRef)
Haojian Wu4c1394d2017-12-12 15:42:10 +0000300 return true;
Sam McCall0e93b072019-01-14 10:01:17 +0000301
Haojian Wu7c251fa2019-07-02 09:16:21 +0000302 // ND is the canonical (i.e. first) declaration. If it's in the main file
303 // (which is not a header), then no public declaration was visible, so assume
304 // it's main-file only.
Kadir Cetinkaya86658022019-03-19 09:27:04 +0000305 bool IsMainFileOnly =
Haojian Wu7c251fa2019-07-02 09:16:21 +0000306 SM.isWrittenInMainFile(SM.getExpansionLoc(ND->getBeginLoc())) &&
307 !ASTCtx->getLangOpts().IsHeaderFile;
Sam McCall2d02c6d2019-04-10 16:26:58 +0000308 // In C, printf is a redecl of an implicit builtin! So check OrigD instead.
309 if (ASTNode.OrigD->isImplicit() ||
310 !shouldCollectSymbol(*ND, *ASTCtx, Opts, IsMainFileOnly))
Sam McCall93f99bf2018-03-12 14:49:09 +0000311 return true;
Sam McCall0e93b072019-01-14 10:01:17 +0000312 // Do not store references to main-file symbols.
313 if (CollectRef && !IsMainFileOnly && !isa<NamespaceDecl>(ND) &&
Haojian Wu7dd49502018-10-17 08:38:36 +0000314 (Opts.RefsInHeaders || SM.getFileID(SpellingLoc) == SM.getMainFileID()))
Haojian Wue83cacc2018-10-15 11:46:26 +0000315 DeclRefs[ND].emplace_back(SpellingLoc, Roles);
316 // Don't continue indexing if this is a mere reference.
317 if (IsOnlyRef)
318 return true;
Haojian Wu4c1394d2017-12-12 15:42:10 +0000319
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000320 // FIXME: ObjCPropertyDecl are not properly indexed here:
321 // - ObjCPropertyDecl may have an OrigD of ObjCPropertyImplDecl, which is
322 // not a NamedDecl.
323 auto *OriginalDecl = dyn_cast<NamedDecl>(ASTNode.OrigD);
324 if (!OriginalDecl)
325 return true;
326
Haojian Wuc6ddb462018-08-07 08:57:52 +0000327 const Symbol *BasicSymbol = Symbols.find(*ID);
Sam McCall93f99bf2018-03-12 14:49:09 +0000328 if (!BasicSymbol) // Regardless of role, ND is the canonical declaration.
Sam McCall0e93b072019-01-14 10:01:17 +0000329 BasicSymbol = addDeclaration(*ND, std::move(*ID), IsMainFileOnly);
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000330 else if (isPreferredDeclaration(*OriginalDecl, Roles))
Sam McCall93f99bf2018-03-12 14:49:09 +0000331 // If OriginalDecl is preferred, replace the existing canonical
332 // declaration (e.g. a class forward declaration). There should be at most
333 // one duplicate as we expect to see only one preferred declaration per
334 // TU, because in practice they are definitions.
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000335 BasicSymbol = addDeclaration(*OriginalDecl, std::move(*ID), IsMainFileOnly);
Haojian Wu4c1394d2017-12-12 15:42:10 +0000336
Sam McCall93f99bf2018-03-12 14:49:09 +0000337 if (Roles & static_cast<unsigned>(index::SymbolRole::Definition))
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000338 addDefinition(*OriginalDecl, *BasicSymbol);
Nathan Ridge73e6f472019-06-04 04:25:44 +0000339
Haojian Wu4c1394d2017-12-12 15:42:10 +0000340 return true;
341}
342
Eric Liu48db19e2018-07-09 15:31:07 +0000343bool SymbolCollector::handleMacroOccurence(const IdentifierInfo *Name,
344 const MacroInfo *MI,
345 index::SymbolRoleSet Roles,
346 SourceLocation Loc) {
347 if (!Opts.CollectMacro)
348 return true;
349 assert(PP.get());
350
351 const auto &SM = PP->getSourceManager();
Eric Liuad588af2018-11-06 10:55:21 +0000352 auto DefLoc = MI->getDefinitionLoc();
Haojian Wu7b6f8742019-01-28 14:11:49 +0000353
Sam McCallec026532019-05-03 13:17:29 +0000354 // Builtin macros don't have useful locations and aren't needed in completion.
355 if (MI->isBuiltinMacro())
Eric Liu48db19e2018-07-09 15:31:07 +0000356 return true;
357
Haojian Wu7b6f8742019-01-28 14:11:49 +0000358 // Skip main-file symbols if we are not collecting them.
359 bool IsMainFileSymbol = SM.isInMainFile(SM.getExpansionLoc(DefLoc));
360 if (IsMainFileSymbol && !Opts.CollectMainFileSymbols)
361 return false;
362
363 // Also avoid storing predefined macros like __DBL_MIN__.
364 if (SM.isWrittenInBuiltinFile(DefLoc))
365 return true;
366
Eric Liu48db19e2018-07-09 15:31:07 +0000367 // Mark the macro as referenced if this is a reference coming from the main
368 // file. The macro may not be an interesting symbol, but it's cheaper to check
369 // at the end.
370 if (Opts.CountReferences &&
371 (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
372 SM.getFileID(SM.getSpellingLoc(Loc)) == SM.getMainFileID())
373 ReferencedMacros.insert(Name);
374 // Don't continue indexing if this is a mere reference.
375 // FIXME: remove macro with ID if it is undefined.
376 if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) ||
377 Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
378 return true;
379
Utkarsh Saxena02ec6ff2019-11-11 12:38:17 +0100380 auto ID = getSymbolID(Name->getName(), MI, SM);
Eric Liud25f1212018-09-06 09:59:37 +0000381 if (!ID)
Eric Liu48db19e2018-07-09 15:31:07 +0000382 return true;
Eric Liu48db19e2018-07-09 15:31:07 +0000383
384 // Only collect one instance in case there are multiple.
Eric Liud25f1212018-09-06 09:59:37 +0000385 if (Symbols.find(*ID) != nullptr)
Eric Liu48db19e2018-07-09 15:31:07 +0000386 return true;
387
388 Symbol S;
Eric Liud25f1212018-09-06 09:59:37 +0000389 S.ID = std::move(*ID);
Eric Liu48db19e2018-07-09 15:31:07 +0000390 S.Name = Name->getName();
Haojian Wu7b6f8742019-01-28 14:11:49 +0000391 if (!IsMainFileSymbol) {
392 S.Flags |= Symbol::IndexedForCodeCompletion;
393 S.Flags |= Symbol::VisibleOutsideFile;
394 }
Eric Liu48db19e2018-07-09 15:31:07 +0000395 S.SymInfo = index::getSymbolInfoForMacro(*MI);
396 std::string FileURI;
Eric Liuad588af2018-11-06 10:55:21 +0000397 // FIXME: use the result to filter out symbols.
Ilya Biryukov30c86b62019-08-20 08:54:30 +0000398 shouldIndexFile(SM.getFileID(Loc));
Eric Liuad588af2018-11-06 10:55:21 +0000399 if (auto DeclLoc =
400 getTokenLocation(DefLoc, SM, Opts, PP->getLangOpts(), FileURI))
Eric Liu48db19e2018-07-09 15:31:07 +0000401 S.CanonicalDeclaration = *DeclLoc;
402
403 CodeCompletionResult SymbolCompletion(Name);
404 const auto *CCS = SymbolCompletion.CreateCodeCompletionStringForMacro(
405 *PP, *CompletionAllocator, *CompletionTUInfo);
406 std::string Signature;
407 std::string SnippetSuffix;
408 getSignature(*CCS, &Signature, &SnippetSuffix);
Eric Liu48db19e2018-07-09 15:31:07 +0000409 S.Signature = Signature;
410 S.CompletionSnippetSuffix = SnippetSuffix;
Eric Liu83f63e42018-09-03 10:18:21 +0000411
Sam McCallec026532019-05-03 13:17:29 +0000412 IndexedMacros.insert(Name);
413 setIncludeLocation(S, DefLoc);
Eric Liu48db19e2018-07-09 15:31:07 +0000414 Symbols.insert(S);
415 return true;
416}
417
Nathan Ridge73e6f472019-06-04 04:25:44 +0000418void SymbolCollector::processRelations(
419 const NamedDecl &ND, const SymbolID &ID,
420 ArrayRef<index::SymbolRelation> Relations) {
421 // Store subtype relations.
422 if (!dyn_cast<TagDecl>(&ND))
423 return;
424
425 for (const auto &R : Relations) {
426 if (!shouldIndexRelation(R))
427 continue;
428
429 const Decl *Object = R.RelatedSymbol;
430
431 auto ObjectID = getSymbolID(Object);
432 if (!ObjectID)
433 continue;
434
435 // Record the relation.
436 // TODO: There may be cases where the object decl is not indexed for some
437 // reason. Those cases should probably be removed in due course, but for
438 // now there are two possible ways to handle it:
439 // (A) Avoid storing the relation in such cases.
440 // (B) Store it anyways. Clients will likely lookup() the SymbolID
441 // in the index and find nothing, but that's a situation they
442 // probably need to handle for other reasons anyways.
443 // We currently do (B) because it's simpler.
Haojian Wuc8e3f432019-10-17 14:08:28 +0000444 this->Relations.insert(Relation{ID, RelationKind::BaseOf, *ObjectID});
Nathan Ridge73e6f472019-06-04 04:25:44 +0000445 }
446}
447
Nathan Ridgeb2f45ac2019-05-30 23:54:43 +0000448void SymbolCollector::setIncludeLocation(const Symbol &S, SourceLocation Loc) {
Sam McCallec026532019-05-03 13:17:29 +0000449 if (Opts.CollectIncludePath)
450 if (shouldCollectIncludePath(S.SymInfo.Kind))
451 // Use the expansion location to get the #include header since this is
452 // where the symbol is exposed.
453 IncludeFiles[S.ID] =
454 PP->getSourceManager().getDecomposedExpansionLoc(Loc).first;
455}
456
Sam McCall93f99bf2018-03-12 14:49:09 +0000457void SymbolCollector::finish() {
Eric Liu48db19e2018-07-09 15:31:07 +0000458 // At the end of the TU, add 1 to the refcount of all referenced symbols.
459 auto IncRef = [this](const SymbolID &ID) {
460 if (const auto *S = Symbols.find(ID)) {
461 Symbol Inc = *S;
462 ++Inc.References;
463 Symbols.insert(Inc);
464 }
465 };
466 for (const NamedDecl *ND : ReferencedDecls) {
Haojian Wuc6ddb462018-08-07 08:57:52 +0000467 if (auto ID = getSymbolID(ND)) {
468 IncRef(*ID);
469 }
Eric Liu48db19e2018-07-09 15:31:07 +0000470 }
471 if (Opts.CollectMacro) {
472 assert(PP);
Sam McCallec026532019-05-03 13:17:29 +0000473 // First, drop header guards. We can't identify these until EOF.
474 for (const IdentifierInfo *II : IndexedMacros) {
475 if (const auto *MI = PP->getMacroDefinition(II).getMacroInfo())
Utkarsh Saxena02ec6ff2019-11-11 12:38:17 +0100476 if (auto ID = getSymbolID(II->getName(), MI, PP->getSourceManager()))
Sam McCallec026532019-05-03 13:17:29 +0000477 if (MI->isUsedForHeaderGuard())
478 Symbols.erase(*ID);
479 }
480 // Now increment refcounts.
Eric Liu48db19e2018-07-09 15:31:07 +0000481 for (const IdentifierInfo *II : ReferencedMacros) {
Eric Liua62c9d62018-07-09 18:54:51 +0000482 if (const auto *MI = PP->getMacroDefinition(II).getMacroInfo())
Utkarsh Saxena02ec6ff2019-11-11 12:38:17 +0100483 if (auto ID = getSymbolID(II->getName(), MI, PP->getSourceManager()))
Eric Liud25f1212018-09-06 09:59:37 +0000484 IncRef(*ID);
Eric Liu48db19e2018-07-09 15:31:07 +0000485 }
Sam McCall93f99bf2018-03-12 14:49:09 +0000486 }
Haojian Wud81e3142018-08-31 12:54:13 +0000487
Sam McCallec026532019-05-03 13:17:29 +0000488 // Fill in IncludeHeaders.
489 // We delay this until end of TU so header guards are all resolved.
490 // Symbols in slabs aren' mutable, so insert() has to walk all the strings :-(
491 llvm::SmallString<256> QName;
492 for (const auto &Entry : IncludeFiles)
493 if (const Symbol *S = Symbols.find(Entry.first)) {
494 QName = S->Scope;
495 QName.append(S->Name);
496 if (auto Header = getIncludeHeader(QName, Entry.second)) {
497 Symbol NewSym = *S;
498 NewSym.IncludeHeaders.push_back({*Header, 1});
499 Symbols.insert(NewSym);
500 }
501 }
502
Haojian Wud81e3142018-08-31 12:54:13 +0000503 const auto &SM = ASTCtx->getSourceManager();
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000504 llvm::DenseMap<FileID, std::string> URICache;
505 auto GetURI = [&](FileID FID) -> llvm::Optional<std::string> {
Haojian Wu7dd49502018-10-17 08:38:36 +0000506 auto Found = URICache.find(FID);
507 if (Found == URICache.end()) {
Haojian Wu7dd49502018-10-17 08:38:36 +0000508 if (auto *FileEntry = SM.getFileEntryForID(FID)) {
509 auto FileURI = toURI(SM, FileEntry->getName(), Opts);
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000510 Found = URICache.insert({FID, FileURI}).first;
Haojian Wuc014d862018-10-17 08:54:48 +0000511 } else {
512 // Ignore cases where we can not find a corresponding file entry
513 // for the loc, thoses are not interesting, e.g. symbols formed
514 // via macro concatenation.
Sam McCallc008af62018-10-20 15:30:37 +0000515 return None;
Haojian Wu7dd49502018-10-17 08:38:36 +0000516 }
517 }
518 return Found->second;
519 };
Sam McCallec026532019-05-03 13:17:29 +0000520 // Populate Refs slab from DeclRefs.
Haojian Wu7dd49502018-10-17 08:38:36 +0000521 if (auto MainFileURI = GetURI(SM.getMainFileID())) {
Sam McCallb0138312018-09-04 14:39:56 +0000522 for (const auto &It : DeclRefs) {
Haojian Wud81e3142018-08-31 12:54:13 +0000523 if (auto ID = getSymbolID(It.first)) {
Haojian Wue83cacc2018-10-15 11:46:26 +0000524 for (const auto &LocAndRole : It.second) {
Haojian Wu7dd49502018-10-17 08:38:36 +0000525 auto FileID = SM.getFileID(LocAndRole.first);
Eric Liuad588af2018-11-06 10:55:21 +0000526 // FIXME: use the result to filter out references.
Ilya Biryukov30c86b62019-08-20 08:54:30 +0000527 shouldIndexFile(FileID);
Haojian Wu7dd49502018-10-17 08:38:36 +0000528 if (auto FileURI = GetURI(FileID)) {
529 auto Range =
530 getTokenRange(LocAndRole.first, SM, ASTCtx->getLangOpts());
531 Ref R;
532 R.Location.Start = Range.first;
533 R.Location.End = Range.second;
Haojian Wuee54a2b2018-11-14 11:55:45 +0000534 R.Location.FileURI = FileURI->c_str();
Haojian Wu7dd49502018-10-17 08:38:36 +0000535 R.Kind = toRefKind(LocAndRole.second);
536 Refs.insert(*ID, R);
537 }
Haojian Wud81e3142018-08-31 12:54:13 +0000538 }
539 }
540 }
Haojian Wud81e3142018-08-31 12:54:13 +0000541 }
542
Sam McCall93f99bf2018-03-12 14:49:09 +0000543 ReferencedDecls.clear();
Eric Liu48db19e2018-07-09 15:31:07 +0000544 ReferencedMacros.clear();
Sam McCallb0138312018-09-04 14:39:56 +0000545 DeclRefs.clear();
Eric Liuad588af2018-11-06 10:55:21 +0000546 FilesToIndexCache.clear();
Sam McCalla96efb62019-04-17 18:33:07 +0000547 HeaderIsSelfContainedCache.clear();
Sam McCallec026532019-05-03 13:17:29 +0000548 IncludeFiles.clear();
Sam McCall93f99bf2018-03-12 14:49:09 +0000549}
550
Kadir Cetinkaya86658022019-03-19 09:27:04 +0000551const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND, SymbolID ID,
Sam McCall0e93b072019-01-14 10:01:17 +0000552 bool IsMainFileOnly) {
Ilya Biryukov43714502018-05-16 12:32:44 +0000553 auto &Ctx = ND.getASTContext();
554 auto &SM = Ctx.getSourceManager();
Sam McCall60039512018-02-09 14:42:01 +0000555
Sam McCall60039512018-02-09 14:42:01 +0000556 Symbol S;
557 S.ID = std::move(ID);
Eric Liu7ad16962018-06-22 10:46:59 +0000558 std::string QName = printQualifiedName(ND);
Sam McCall032db942018-06-22 06:41:43 +0000559 // FIXME: this returns foo:bar: for objective-C methods, we prefer only foo:
560 // for consistency with CodeCompletionString and a clean name/signature split.
Kadir Cetinkaya79063de2019-04-12 10:09:24 +0000561 std::tie(S.Scope, S.Name) = splitQualifiedName(QName);
562 std::string TemplateSpecializationArgs = printTemplateSpecializationArgs(ND);
563 S.TemplateSpecializationArgs = TemplateSpecializationArgs;
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +0000564
Sam McCall0e93b072019-01-14 10:01:17 +0000565 // We collect main-file symbols, but do not use them for code completion.
566 if (!IsMainFileOnly && isIndexedForCodeCompletion(ND, Ctx))
Eric Liu6df66002018-09-06 18:52:26 +0000567 S.Flags |= Symbol::IndexedForCodeCompletion;
Eric Liu48597382018-10-18 12:23:05 +0000568 if (isImplementationDetail(&ND))
569 S.Flags |= Symbol::ImplementationDetail;
Sam McCall0e93b072019-01-14 10:01:17 +0000570 if (!IsMainFileOnly)
571 S.Flags |= Symbol::VisibleOutsideFile;
Sam McCall60039512018-02-09 14:42:01 +0000572 S.SymInfo = index::getSymbolInfo(&ND);
573 std::string FileURI;
Sam McCall95738072019-08-06 20:25:59 +0000574 auto Loc = spellingLocIfSpelled(findName(&ND), SM);
Kadir Cetinkayabb6cd822019-04-15 14:38:46 +0000575 assert(Loc.isValid() && "Invalid source location for NamedDecl");
Eric Liuad588af2018-11-06 10:55:21 +0000576 // FIXME: use the result to filter out symbols.
Ilya Biryukov30c86b62019-08-20 08:54:30 +0000577 shouldIndexFile(SM.getFileID(Loc));
Eric Liuad588af2018-11-06 10:55:21 +0000578 if (auto DeclLoc =
579 getTokenLocation(Loc, SM, Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000580 S.CanonicalDeclaration = *DeclLoc;
581
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000582 S.Origin = Opts.Origin;
583 if (ND.getAvailability() == AR_Deprecated)
584 S.Flags |= Symbol::Deprecated;
585
Sam McCall60039512018-02-09 14:42:01 +0000586 // Add completion info.
587 // FIXME: we may want to choose a different redecl, or combine from several.
588 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
Ilya Biryukovcf124bd2018-04-13 11:03:07 +0000589 // We use the primary template, as clang does during code completion.
590 CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0);
Sam McCall60039512018-02-09 14:42:01 +0000591 const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
Kadir Cetinkayab9157902018-10-24 15:24:29 +0000592 *ASTCtx, *PP, CodeCompletionContext::CCC_Symbol, *CompletionAllocator,
Sam McCall60039512018-02-09 14:42:01 +0000593 *CompletionTUInfo,
Ilya Biryukov43714502018-05-16 12:32:44 +0000594 /*IncludeBriefComments*/ false);
Ilya Biryukov43714502018-05-16 12:32:44 +0000595 std::string Documentation =
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +0000596 formatDocumentation(*CCS, getDocComment(Ctx, SymbolCompletion,
597 /*CommentsFromHeaders=*/true));
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000598 if (!(S.Flags & Symbol::IndexedForCodeCompletion)) {
Haojian Wuda79dcc2019-02-25 16:00:00 +0000599 if (Opts.StoreAllDocumentation)
600 S.Documentation = Documentation;
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000601 Symbols.insert(S);
602 return Symbols.find(S.ID);
603 }
Haojian Wuda79dcc2019-02-25 16:00:00 +0000604 S.Documentation = Documentation;
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000605 std::string Signature;
606 std::string SnippetSuffix;
607 getSignature(*CCS, &Signature, &SnippetSuffix);
608 S.Signature = Signature;
609 S.CompletionSnippetSuffix = SnippetSuffix;
Sam McCalla68951e2018-06-22 16:11:35 +0000610 std::string ReturnType = getReturnType(*CCS);
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000611 S.ReturnType = ReturnType;
Sam McCall60039512018-02-09 14:42:01 +0000612
Ilya Biryukov4d3d82e2018-11-26 15:52:16 +0000613 llvm::Optional<OpaqueType> TypeStorage;
Ilya Biryukova21392b2018-11-26 15:29:14 +0000614 if (S.Flags & Symbol::IndexedForCodeCompletion) {
Ilya Biryukov4d3d82e2018-11-26 15:52:16 +0000615 TypeStorage = OpaqueType::fromCompletionResult(*ASTCtx, SymbolCompletion);
616 if (TypeStorage)
617 S.Type = TypeStorage->raw();
Ilya Biryukova21392b2018-11-26 15:29:14 +0000618 }
619
Sam McCall60039512018-02-09 14:42:01 +0000620 Symbols.insert(S);
Sam McCallec026532019-05-03 13:17:29 +0000621 setIncludeLocation(S, ND.getLocation());
Sam McCall60039512018-02-09 14:42:01 +0000622 return Symbols.find(S.ID);
623}
624
625void SymbolCollector::addDefinition(const NamedDecl &ND,
626 const Symbol &DeclSym) {
627 if (DeclSym.Definition)
628 return;
629 // If we saw some forward declaration, we end up copying the symbol.
630 // This is not ideal, but avoids duplicating the "is this a definition" check
631 // in clang::index. We should only see one definition.
632 Symbol S = DeclSym;
633 std::string FileURI;
Eric Liuad588af2018-11-06 10:55:21 +0000634 const auto &SM = ND.getASTContext().getSourceManager();
Sam McCall95738072019-08-06 20:25:59 +0000635 auto Loc = spellingLocIfSpelled(findName(&ND), SM);
Eric Liuad588af2018-11-06 10:55:21 +0000636 // FIXME: use the result to filter out symbols.
Ilya Biryukov30c86b62019-08-20 08:54:30 +0000637 shouldIndexFile(SM.getFileID(Loc));
Eric Liuad588af2018-11-06 10:55:21 +0000638 if (auto DefLoc =
639 getTokenLocation(Loc, SM, Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000640 S.Definition = *DefLoc;
641 Symbols.insert(S);
642}
643
Sam McCalla96efb62019-04-17 18:33:07 +0000644/// Gets a canonical include (URI of the header or <header> or "header") for
645/// header of \p FID (which should usually be the *expansion* file).
646/// Returns None if includes should not be inserted for this file.
647llvm::Optional<std::string>
648SymbolCollector::getIncludeHeader(llvm::StringRef QName, FileID FID) {
649 const SourceManager &SM = ASTCtx->getSourceManager();
650 const FileEntry *FE = SM.getFileEntryForID(FID);
651 if (!FE || FE->getName().empty())
652 return llvm::None;
653 llvm::StringRef Filename = FE->getName();
654 // If a file is mapped by canonical headers, use that mapping, regardless
655 // of whether it's an otherwise-good header (header guards etc).
656 if (Opts.Includes) {
657 llvm::StringRef Canonical = Opts.Includes->mapHeader(Filename, QName);
658 // If we had a mapping, always use it.
659 if (Canonical.startswith("<") || Canonical.startswith("\""))
660 return Canonical.str();
661 if (Canonical != Filename)
662 return toURI(SM, Canonical, Opts);
663 }
664 if (!isSelfContainedHeader(FID)) {
665 // A .inc or .def file is often included into a real header to define
666 // symbols (e.g. LLVM tablegen files).
667 if (Filename.endswith(".inc") || Filename.endswith(".def"))
668 return getIncludeHeader(QName, SM.getFileID(SM.getIncludeLoc(FID)));
669 // Conservatively refuse to insert #includes to files without guards.
670 return llvm::None;
671 }
672 // Standard case: just insert the file itself.
673 return toURI(SM, Filename, Opts);
674}
675
676bool SymbolCollector::isSelfContainedHeader(FileID FID) {
677 // The real computation (which will be memoized).
678 auto Compute = [&] {
679 const SourceManager &SM = ASTCtx->getSourceManager();
680 const FileEntry *FE = SM.getFileEntryForID(FID);
681 if (!FE)
682 return false;
683 if (!PP->getHeaderSearchInfo().isFileMultipleIncludeGuarded(FE))
684 return false;
685 // This pattern indicates that a header can't be used without
686 // particular preprocessor state, usually set up by another header.
Sam McCalle3559ee2019-04-25 17:47:07 +0000687 if (isDontIncludeMeHeader(SM.getBufferData(FID)))
Sam McCalla96efb62019-04-17 18:33:07 +0000688 return false;
689 return true;
690 };
691
692 auto R = HeaderIsSelfContainedCache.try_emplace(FID, false);
693 if (R.second)
694 R.first->second = Compute();
695 return R.first->second;
696}
697
Sam McCalle3559ee2019-04-25 17:47:07 +0000698// Is Line an #if or #ifdef directive?
699static bool isIf(llvm::StringRef Line) {
700 Line = Line.ltrim();
701 if (!Line.consume_front("#"))
702 return false;
703 Line = Line.ltrim();
704 return Line.startswith("if");
705}
706// Is Line an #error directive mentioning includes?
707static bool isErrorAboutInclude(llvm::StringRef Line) {
708 Line = Line.ltrim();
709 if (!Line.consume_front("#"))
710 return false;
711 Line = Line.ltrim();
Nathan Ridgeb2f45ac2019-05-30 23:54:43 +0000712 if (!Line.startswith("error"))
Sam McCalle3559ee2019-04-25 17:47:07 +0000713 return false;
714 return Line.contains_lower("includ"); // Matches "include" or "including".
715}
716
717bool SymbolCollector::isDontIncludeMeHeader(llvm::StringRef Content) {
718 llvm::StringRef Line;
719 // Only sniff up to 100 lines or 10KB.
Nathan Ridgeb2f45ac2019-05-30 23:54:43 +0000720 Content = Content.take_front(100 * 100);
Sam McCalle3559ee2019-04-25 17:47:07 +0000721 for (unsigned I = 0; I < 100 && !Content.empty(); ++I) {
722 std::tie(Line, Content) = Content.split('\n');
723 if (isIf(Line) && isErrorAboutInclude(Content.split('\n').first))
724 return true;
725 }
726 return false;
727}
728
Ilya Biryukov30c86b62019-08-20 08:54:30 +0000729bool SymbolCollector::shouldIndexFile(FileID FID) {
730 if (!Opts.FileFilter)
731 return true;
732 auto I = FilesToIndexCache.try_emplace(FID);
733 if (I.second)
734 I.first->second = Opts.FileFilter(ASTCtx->getSourceManager(), FID);
735 return I.first->second;
736}
737
Haojian Wu4c1394d2017-12-12 15:42:10 +0000738} // namespace clangd
739} // namespace clang