blob: 00adbd84fd62f50c314ceb8cb46ed80e9de0044f [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())) &&
Haojian Wub221c9d2019-11-15 16:24:19 +0100307 !isHeaderFile(SM.getFileEntryForID(SM.getMainFileID())->getName(),
308 ASTCtx->getLangOpts());
Sam McCall2d02c6d2019-04-10 16:26:58 +0000309 // In C, printf is a redecl of an implicit builtin! So check OrigD instead.
310 if (ASTNode.OrigD->isImplicit() ||
311 !shouldCollectSymbol(*ND, *ASTCtx, Opts, IsMainFileOnly))
Sam McCall93f99bf2018-03-12 14:49:09 +0000312 return true;
Sam McCall0e93b072019-01-14 10:01:17 +0000313 // Do not store references to main-file symbols.
314 if (CollectRef && !IsMainFileOnly && !isa<NamespaceDecl>(ND) &&
Haojian Wu7dd49502018-10-17 08:38:36 +0000315 (Opts.RefsInHeaders || SM.getFileID(SpellingLoc) == SM.getMainFileID()))
Haojian Wue83cacc2018-10-15 11:46:26 +0000316 DeclRefs[ND].emplace_back(SpellingLoc, Roles);
317 // Don't continue indexing if this is a mere reference.
318 if (IsOnlyRef)
319 return true;
Haojian Wu4c1394d2017-12-12 15:42:10 +0000320
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000321 // FIXME: ObjCPropertyDecl are not properly indexed here:
322 // - ObjCPropertyDecl may have an OrigD of ObjCPropertyImplDecl, which is
323 // not a NamedDecl.
324 auto *OriginalDecl = dyn_cast<NamedDecl>(ASTNode.OrigD);
325 if (!OriginalDecl)
326 return true;
327
Haojian Wuc6ddb462018-08-07 08:57:52 +0000328 const Symbol *BasicSymbol = Symbols.find(*ID);
Sam McCall93f99bf2018-03-12 14:49:09 +0000329 if (!BasicSymbol) // Regardless of role, ND is the canonical declaration.
Sam McCall0e93b072019-01-14 10:01:17 +0000330 BasicSymbol = addDeclaration(*ND, std::move(*ID), IsMainFileOnly);
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000331 else if (isPreferredDeclaration(*OriginalDecl, Roles))
Sam McCall93f99bf2018-03-12 14:49:09 +0000332 // If OriginalDecl is preferred, replace the existing canonical
333 // declaration (e.g. a class forward declaration). There should be at most
334 // one duplicate as we expect to see only one preferred declaration per
335 // TU, because in practice they are definitions.
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000336 BasicSymbol = addDeclaration(*OriginalDecl, std::move(*ID), IsMainFileOnly);
Haojian Wu4c1394d2017-12-12 15:42:10 +0000337
Sam McCall93f99bf2018-03-12 14:49:09 +0000338 if (Roles & static_cast<unsigned>(index::SymbolRole::Definition))
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000339 addDefinition(*OriginalDecl, *BasicSymbol);
Nathan Ridge73e6f472019-06-04 04:25:44 +0000340
Haojian Wu4c1394d2017-12-12 15:42:10 +0000341 return true;
342}
343
Eric Liu48db19e2018-07-09 15:31:07 +0000344bool SymbolCollector::handleMacroOccurence(const IdentifierInfo *Name,
345 const MacroInfo *MI,
346 index::SymbolRoleSet Roles,
347 SourceLocation Loc) {
348 if (!Opts.CollectMacro)
349 return true;
350 assert(PP.get());
351
352 const auto &SM = PP->getSourceManager();
Eric Liuad588af2018-11-06 10:55:21 +0000353 auto DefLoc = MI->getDefinitionLoc();
Haojian Wu7b6f8742019-01-28 14:11:49 +0000354
Sam McCallec026532019-05-03 13:17:29 +0000355 // Builtin macros don't have useful locations and aren't needed in completion.
356 if (MI->isBuiltinMacro())
Eric Liu48db19e2018-07-09 15:31:07 +0000357 return true;
358
Haojian Wu7b6f8742019-01-28 14:11:49 +0000359 // Skip main-file symbols if we are not collecting them.
360 bool IsMainFileSymbol = SM.isInMainFile(SM.getExpansionLoc(DefLoc));
361 if (IsMainFileSymbol && !Opts.CollectMainFileSymbols)
362 return false;
363
364 // Also avoid storing predefined macros like __DBL_MIN__.
365 if (SM.isWrittenInBuiltinFile(DefLoc))
366 return true;
367
Eric Liu48db19e2018-07-09 15:31:07 +0000368 // Mark the macro as referenced if this is a reference coming from the main
369 // file. The macro may not be an interesting symbol, but it's cheaper to check
370 // at the end.
371 if (Opts.CountReferences &&
372 (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
373 SM.getFileID(SM.getSpellingLoc(Loc)) == SM.getMainFileID())
374 ReferencedMacros.insert(Name);
375 // Don't continue indexing if this is a mere reference.
376 // FIXME: remove macro with ID if it is undefined.
377 if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) ||
378 Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
379 return true;
380
Utkarsh Saxena02ec6ff2019-11-11 12:38:17 +0100381 auto ID = getSymbolID(Name->getName(), MI, SM);
Eric Liud25f1212018-09-06 09:59:37 +0000382 if (!ID)
Eric Liu48db19e2018-07-09 15:31:07 +0000383 return true;
Eric Liu48db19e2018-07-09 15:31:07 +0000384
385 // Only collect one instance in case there are multiple.
Eric Liud25f1212018-09-06 09:59:37 +0000386 if (Symbols.find(*ID) != nullptr)
Eric Liu48db19e2018-07-09 15:31:07 +0000387 return true;
388
389 Symbol S;
Eric Liud25f1212018-09-06 09:59:37 +0000390 S.ID = std::move(*ID);
Eric Liu48db19e2018-07-09 15:31:07 +0000391 S.Name = Name->getName();
Haojian Wu7b6f8742019-01-28 14:11:49 +0000392 if (!IsMainFileSymbol) {
393 S.Flags |= Symbol::IndexedForCodeCompletion;
394 S.Flags |= Symbol::VisibleOutsideFile;
395 }
Eric Liu48db19e2018-07-09 15:31:07 +0000396 S.SymInfo = index::getSymbolInfoForMacro(*MI);
397 std::string FileURI;
Eric Liuad588af2018-11-06 10:55:21 +0000398 // FIXME: use the result to filter out symbols.
Ilya Biryukov30c86b62019-08-20 08:54:30 +0000399 shouldIndexFile(SM.getFileID(Loc));
Eric Liuad588af2018-11-06 10:55:21 +0000400 if (auto DeclLoc =
401 getTokenLocation(DefLoc, SM, Opts, PP->getLangOpts(), FileURI))
Eric Liu48db19e2018-07-09 15:31:07 +0000402 S.CanonicalDeclaration = *DeclLoc;
403
404 CodeCompletionResult SymbolCompletion(Name);
405 const auto *CCS = SymbolCompletion.CreateCodeCompletionStringForMacro(
406 *PP, *CompletionAllocator, *CompletionTUInfo);
407 std::string Signature;
408 std::string SnippetSuffix;
409 getSignature(*CCS, &Signature, &SnippetSuffix);
Eric Liu48db19e2018-07-09 15:31:07 +0000410 S.Signature = Signature;
411 S.CompletionSnippetSuffix = SnippetSuffix;
Eric Liu83f63e42018-09-03 10:18:21 +0000412
Sam McCallec026532019-05-03 13:17:29 +0000413 IndexedMacros.insert(Name);
414 setIncludeLocation(S, DefLoc);
Eric Liu48db19e2018-07-09 15:31:07 +0000415 Symbols.insert(S);
416 return true;
417}
418
Nathan Ridge73e6f472019-06-04 04:25:44 +0000419void SymbolCollector::processRelations(
420 const NamedDecl &ND, const SymbolID &ID,
421 ArrayRef<index::SymbolRelation> Relations) {
422 // Store subtype relations.
423 if (!dyn_cast<TagDecl>(&ND))
424 return;
425
426 for (const auto &R : Relations) {
427 if (!shouldIndexRelation(R))
428 continue;
429
430 const Decl *Object = R.RelatedSymbol;
431
432 auto ObjectID = getSymbolID(Object);
433 if (!ObjectID)
434 continue;
435
436 // Record the relation.
437 // TODO: There may be cases where the object decl is not indexed for some
438 // reason. Those cases should probably be removed in due course, but for
439 // now there are two possible ways to handle it:
440 // (A) Avoid storing the relation in such cases.
441 // (B) Store it anyways. Clients will likely lookup() the SymbolID
442 // in the index and find nothing, but that's a situation they
443 // probably need to handle for other reasons anyways.
444 // We currently do (B) because it's simpler.
Haojian Wuc8e3f432019-10-17 14:08:28 +0000445 this->Relations.insert(Relation{ID, RelationKind::BaseOf, *ObjectID});
Nathan Ridge73e6f472019-06-04 04:25:44 +0000446 }
447}
448
Nathan Ridgeb2f45ac2019-05-30 23:54:43 +0000449void SymbolCollector::setIncludeLocation(const Symbol &S, SourceLocation Loc) {
Sam McCallec026532019-05-03 13:17:29 +0000450 if (Opts.CollectIncludePath)
451 if (shouldCollectIncludePath(S.SymInfo.Kind))
452 // Use the expansion location to get the #include header since this is
453 // where the symbol is exposed.
454 IncludeFiles[S.ID] =
455 PP->getSourceManager().getDecomposedExpansionLoc(Loc).first;
456}
457
Sam McCall93f99bf2018-03-12 14:49:09 +0000458void SymbolCollector::finish() {
Eric Liu48db19e2018-07-09 15:31:07 +0000459 // At the end of the TU, add 1 to the refcount of all referenced symbols.
460 auto IncRef = [this](const SymbolID &ID) {
461 if (const auto *S = Symbols.find(ID)) {
462 Symbol Inc = *S;
463 ++Inc.References;
464 Symbols.insert(Inc);
465 }
466 };
467 for (const NamedDecl *ND : ReferencedDecls) {
Haojian Wuc6ddb462018-08-07 08:57:52 +0000468 if (auto ID = getSymbolID(ND)) {
469 IncRef(*ID);
470 }
Eric Liu48db19e2018-07-09 15:31:07 +0000471 }
472 if (Opts.CollectMacro) {
473 assert(PP);
Sam McCallec026532019-05-03 13:17:29 +0000474 // First, drop header guards. We can't identify these until EOF.
475 for (const IdentifierInfo *II : IndexedMacros) {
476 if (const auto *MI = PP->getMacroDefinition(II).getMacroInfo())
Utkarsh Saxena02ec6ff2019-11-11 12:38:17 +0100477 if (auto ID = getSymbolID(II->getName(), MI, PP->getSourceManager()))
Sam McCallec026532019-05-03 13:17:29 +0000478 if (MI->isUsedForHeaderGuard())
479 Symbols.erase(*ID);
480 }
481 // Now increment refcounts.
Eric Liu48db19e2018-07-09 15:31:07 +0000482 for (const IdentifierInfo *II : ReferencedMacros) {
Eric Liua62c9d62018-07-09 18:54:51 +0000483 if (const auto *MI = PP->getMacroDefinition(II).getMacroInfo())
Utkarsh Saxena02ec6ff2019-11-11 12:38:17 +0100484 if (auto ID = getSymbolID(II->getName(), MI, PP->getSourceManager()))
Eric Liud25f1212018-09-06 09:59:37 +0000485 IncRef(*ID);
Eric Liu48db19e2018-07-09 15:31:07 +0000486 }
Sam McCall93f99bf2018-03-12 14:49:09 +0000487 }
Haojian Wud81e3142018-08-31 12:54:13 +0000488
Sam McCallec026532019-05-03 13:17:29 +0000489 // Fill in IncludeHeaders.
490 // We delay this until end of TU so header guards are all resolved.
491 // Symbols in slabs aren' mutable, so insert() has to walk all the strings :-(
492 llvm::SmallString<256> QName;
493 for (const auto &Entry : IncludeFiles)
494 if (const Symbol *S = Symbols.find(Entry.first)) {
495 QName = S->Scope;
496 QName.append(S->Name);
497 if (auto Header = getIncludeHeader(QName, Entry.second)) {
498 Symbol NewSym = *S;
499 NewSym.IncludeHeaders.push_back({*Header, 1});
500 Symbols.insert(NewSym);
501 }
502 }
503
Haojian Wud81e3142018-08-31 12:54:13 +0000504 const auto &SM = ASTCtx->getSourceManager();
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000505 llvm::DenseMap<FileID, std::string> URICache;
506 auto GetURI = [&](FileID FID) -> llvm::Optional<std::string> {
Haojian Wu7dd49502018-10-17 08:38:36 +0000507 auto Found = URICache.find(FID);
508 if (Found == URICache.end()) {
Haojian Wu7dd49502018-10-17 08:38:36 +0000509 if (auto *FileEntry = SM.getFileEntryForID(FID)) {
510 auto FileURI = toURI(SM, FileEntry->getName(), Opts);
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000511 Found = URICache.insert({FID, FileURI}).first;
Haojian Wuc014d862018-10-17 08:54:48 +0000512 } else {
513 // Ignore cases where we can not find a corresponding file entry
514 // for the loc, thoses are not interesting, e.g. symbols formed
515 // via macro concatenation.
Sam McCallc008af62018-10-20 15:30:37 +0000516 return None;
Haojian Wu7dd49502018-10-17 08:38:36 +0000517 }
518 }
519 return Found->second;
520 };
Sam McCallec026532019-05-03 13:17:29 +0000521 // Populate Refs slab from DeclRefs.
Haojian Wu7dd49502018-10-17 08:38:36 +0000522 if (auto MainFileURI = GetURI(SM.getMainFileID())) {
Sam McCallb0138312018-09-04 14:39:56 +0000523 for (const auto &It : DeclRefs) {
Haojian Wud81e3142018-08-31 12:54:13 +0000524 if (auto ID = getSymbolID(It.first)) {
Haojian Wue83cacc2018-10-15 11:46:26 +0000525 for (const auto &LocAndRole : It.second) {
Haojian Wu7dd49502018-10-17 08:38:36 +0000526 auto FileID = SM.getFileID(LocAndRole.first);
Eric Liuad588af2018-11-06 10:55:21 +0000527 // FIXME: use the result to filter out references.
Ilya Biryukov30c86b62019-08-20 08:54:30 +0000528 shouldIndexFile(FileID);
Haojian Wu7dd49502018-10-17 08:38:36 +0000529 if (auto FileURI = GetURI(FileID)) {
530 auto Range =
531 getTokenRange(LocAndRole.first, SM, ASTCtx->getLangOpts());
532 Ref R;
533 R.Location.Start = Range.first;
534 R.Location.End = Range.second;
Haojian Wuee54a2b2018-11-14 11:55:45 +0000535 R.Location.FileURI = FileURI->c_str();
Haojian Wu7dd49502018-10-17 08:38:36 +0000536 R.Kind = toRefKind(LocAndRole.second);
537 Refs.insert(*ID, R);
538 }
Haojian Wud81e3142018-08-31 12:54:13 +0000539 }
540 }
541 }
Haojian Wud81e3142018-08-31 12:54:13 +0000542 }
543
Sam McCall93f99bf2018-03-12 14:49:09 +0000544 ReferencedDecls.clear();
Eric Liu48db19e2018-07-09 15:31:07 +0000545 ReferencedMacros.clear();
Sam McCallb0138312018-09-04 14:39:56 +0000546 DeclRefs.clear();
Eric Liuad588af2018-11-06 10:55:21 +0000547 FilesToIndexCache.clear();
Sam McCalla96efb62019-04-17 18:33:07 +0000548 HeaderIsSelfContainedCache.clear();
Sam McCallec026532019-05-03 13:17:29 +0000549 IncludeFiles.clear();
Sam McCall93f99bf2018-03-12 14:49:09 +0000550}
551
Kadir Cetinkaya86658022019-03-19 09:27:04 +0000552const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND, SymbolID ID,
Sam McCall0e93b072019-01-14 10:01:17 +0000553 bool IsMainFileOnly) {
Ilya Biryukov43714502018-05-16 12:32:44 +0000554 auto &Ctx = ND.getASTContext();
555 auto &SM = Ctx.getSourceManager();
Sam McCall60039512018-02-09 14:42:01 +0000556
Sam McCall60039512018-02-09 14:42:01 +0000557 Symbol S;
558 S.ID = std::move(ID);
Eric Liu7ad16962018-06-22 10:46:59 +0000559 std::string QName = printQualifiedName(ND);
Sam McCall032db942018-06-22 06:41:43 +0000560 // FIXME: this returns foo:bar: for objective-C methods, we prefer only foo:
561 // for consistency with CodeCompletionString and a clean name/signature split.
Kadir Cetinkaya79063de2019-04-12 10:09:24 +0000562 std::tie(S.Scope, S.Name) = splitQualifiedName(QName);
563 std::string TemplateSpecializationArgs = printTemplateSpecializationArgs(ND);
564 S.TemplateSpecializationArgs = TemplateSpecializationArgs;
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +0000565
Sam McCall0e93b072019-01-14 10:01:17 +0000566 // We collect main-file symbols, but do not use them for code completion.
567 if (!IsMainFileOnly && isIndexedForCodeCompletion(ND, Ctx))
Eric Liu6df66002018-09-06 18:52:26 +0000568 S.Flags |= Symbol::IndexedForCodeCompletion;
Eric Liu48597382018-10-18 12:23:05 +0000569 if (isImplementationDetail(&ND))
570 S.Flags |= Symbol::ImplementationDetail;
Sam McCall0e93b072019-01-14 10:01:17 +0000571 if (!IsMainFileOnly)
572 S.Flags |= Symbol::VisibleOutsideFile;
Sam McCall60039512018-02-09 14:42:01 +0000573 S.SymInfo = index::getSymbolInfo(&ND);
574 std::string FileURI;
Sam McCall95738072019-08-06 20:25:59 +0000575 auto Loc = spellingLocIfSpelled(findName(&ND), SM);
Kadir Cetinkayabb6cd822019-04-15 14:38:46 +0000576 assert(Loc.isValid() && "Invalid source location for NamedDecl");
Eric Liuad588af2018-11-06 10:55:21 +0000577 // FIXME: use the result to filter out symbols.
Ilya Biryukov30c86b62019-08-20 08:54:30 +0000578 shouldIndexFile(SM.getFileID(Loc));
Eric Liuad588af2018-11-06 10:55:21 +0000579 if (auto DeclLoc =
580 getTokenLocation(Loc, SM, Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000581 S.CanonicalDeclaration = *DeclLoc;
582
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000583 S.Origin = Opts.Origin;
584 if (ND.getAvailability() == AR_Deprecated)
585 S.Flags |= Symbol::Deprecated;
586
Sam McCall60039512018-02-09 14:42:01 +0000587 // Add completion info.
588 // FIXME: we may want to choose a different redecl, or combine from several.
589 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
Ilya Biryukovcf124bd2018-04-13 11:03:07 +0000590 // We use the primary template, as clang does during code completion.
591 CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0);
Sam McCall60039512018-02-09 14:42:01 +0000592 const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
Kadir Cetinkayab9157902018-10-24 15:24:29 +0000593 *ASTCtx, *PP, CodeCompletionContext::CCC_Symbol, *CompletionAllocator,
Sam McCall60039512018-02-09 14:42:01 +0000594 *CompletionTUInfo,
Ilya Biryukov43714502018-05-16 12:32:44 +0000595 /*IncludeBriefComments*/ false);
Ilya Biryukov43714502018-05-16 12:32:44 +0000596 std::string Documentation =
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +0000597 formatDocumentation(*CCS, getDocComment(Ctx, SymbolCompletion,
598 /*CommentsFromHeaders=*/true));
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000599 if (!(S.Flags & Symbol::IndexedForCodeCompletion)) {
Haojian Wuda79dcc2019-02-25 16:00:00 +0000600 if (Opts.StoreAllDocumentation)
601 S.Documentation = Documentation;
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000602 Symbols.insert(S);
603 return Symbols.find(S.ID);
604 }
Haojian Wuda79dcc2019-02-25 16:00:00 +0000605 S.Documentation = Documentation;
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000606 std::string Signature;
607 std::string SnippetSuffix;
608 getSignature(*CCS, &Signature, &SnippetSuffix);
609 S.Signature = Signature;
610 S.CompletionSnippetSuffix = SnippetSuffix;
Sam McCalla68951e2018-06-22 16:11:35 +0000611 std::string ReturnType = getReturnType(*CCS);
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000612 S.ReturnType = ReturnType;
Sam McCall60039512018-02-09 14:42:01 +0000613
Ilya Biryukov4d3d82e2018-11-26 15:52:16 +0000614 llvm::Optional<OpaqueType> TypeStorage;
Ilya Biryukova21392b2018-11-26 15:29:14 +0000615 if (S.Flags & Symbol::IndexedForCodeCompletion) {
Ilya Biryukov4d3d82e2018-11-26 15:52:16 +0000616 TypeStorage = OpaqueType::fromCompletionResult(*ASTCtx, SymbolCompletion);
617 if (TypeStorage)
618 S.Type = TypeStorage->raw();
Ilya Biryukova21392b2018-11-26 15:29:14 +0000619 }
620
Sam McCall60039512018-02-09 14:42:01 +0000621 Symbols.insert(S);
Sam McCallec026532019-05-03 13:17:29 +0000622 setIncludeLocation(S, ND.getLocation());
Sam McCall60039512018-02-09 14:42:01 +0000623 return Symbols.find(S.ID);
624}
625
626void SymbolCollector::addDefinition(const NamedDecl &ND,
627 const Symbol &DeclSym) {
628 if (DeclSym.Definition)
629 return;
630 // If we saw some forward declaration, we end up copying the symbol.
631 // This is not ideal, but avoids duplicating the "is this a definition" check
632 // in clang::index. We should only see one definition.
633 Symbol S = DeclSym;
634 std::string FileURI;
Eric Liuad588af2018-11-06 10:55:21 +0000635 const auto &SM = ND.getASTContext().getSourceManager();
Sam McCall95738072019-08-06 20:25:59 +0000636 auto Loc = spellingLocIfSpelled(findName(&ND), SM);
Eric Liuad588af2018-11-06 10:55:21 +0000637 // FIXME: use the result to filter out symbols.
Ilya Biryukov30c86b62019-08-20 08:54:30 +0000638 shouldIndexFile(SM.getFileID(Loc));
Eric Liuad588af2018-11-06 10:55:21 +0000639 if (auto DefLoc =
640 getTokenLocation(Loc, SM, Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000641 S.Definition = *DefLoc;
642 Symbols.insert(S);
643}
644
Sam McCalla96efb62019-04-17 18:33:07 +0000645/// Gets a canonical include (URI of the header or <header> or "header") for
646/// header of \p FID (which should usually be the *expansion* file).
647/// Returns None if includes should not be inserted for this file.
648llvm::Optional<std::string>
649SymbolCollector::getIncludeHeader(llvm::StringRef QName, FileID FID) {
650 const SourceManager &SM = ASTCtx->getSourceManager();
651 const FileEntry *FE = SM.getFileEntryForID(FID);
652 if (!FE || FE->getName().empty())
653 return llvm::None;
654 llvm::StringRef Filename = FE->getName();
655 // If a file is mapped by canonical headers, use that mapping, regardless
656 // of whether it's an otherwise-good header (header guards etc).
657 if (Opts.Includes) {
658 llvm::StringRef Canonical = Opts.Includes->mapHeader(Filename, QName);
659 // If we had a mapping, always use it.
660 if (Canonical.startswith("<") || Canonical.startswith("\""))
661 return Canonical.str();
662 if (Canonical != Filename)
663 return toURI(SM, Canonical, Opts);
664 }
665 if (!isSelfContainedHeader(FID)) {
666 // A .inc or .def file is often included into a real header to define
667 // symbols (e.g. LLVM tablegen files).
668 if (Filename.endswith(".inc") || Filename.endswith(".def"))
669 return getIncludeHeader(QName, SM.getFileID(SM.getIncludeLoc(FID)));
670 // Conservatively refuse to insert #includes to files without guards.
671 return llvm::None;
672 }
673 // Standard case: just insert the file itself.
674 return toURI(SM, Filename, Opts);
675}
676
677bool SymbolCollector::isSelfContainedHeader(FileID FID) {
678 // The real computation (which will be memoized).
679 auto Compute = [&] {
680 const SourceManager &SM = ASTCtx->getSourceManager();
681 const FileEntry *FE = SM.getFileEntryForID(FID);
682 if (!FE)
683 return false;
684 if (!PP->getHeaderSearchInfo().isFileMultipleIncludeGuarded(FE))
685 return false;
686 // This pattern indicates that a header can't be used without
687 // particular preprocessor state, usually set up by another header.
Sam McCalle3559ee2019-04-25 17:47:07 +0000688 if (isDontIncludeMeHeader(SM.getBufferData(FID)))
Sam McCalla96efb62019-04-17 18:33:07 +0000689 return false;
690 return true;
691 };
692
693 auto R = HeaderIsSelfContainedCache.try_emplace(FID, false);
694 if (R.second)
695 R.first->second = Compute();
696 return R.first->second;
697}
698
Sam McCalle3559ee2019-04-25 17:47:07 +0000699// Is Line an #if or #ifdef directive?
700static bool isIf(llvm::StringRef Line) {
701 Line = Line.ltrim();
702 if (!Line.consume_front("#"))
703 return false;
704 Line = Line.ltrim();
705 return Line.startswith("if");
706}
707// Is Line an #error directive mentioning includes?
708static bool isErrorAboutInclude(llvm::StringRef Line) {
709 Line = Line.ltrim();
710 if (!Line.consume_front("#"))
711 return false;
712 Line = Line.ltrim();
Nathan Ridgeb2f45ac2019-05-30 23:54:43 +0000713 if (!Line.startswith("error"))
Sam McCalle3559ee2019-04-25 17:47:07 +0000714 return false;
715 return Line.contains_lower("includ"); // Matches "include" or "including".
716}
717
718bool SymbolCollector::isDontIncludeMeHeader(llvm::StringRef Content) {
719 llvm::StringRef Line;
720 // Only sniff up to 100 lines or 10KB.
Nathan Ridgeb2f45ac2019-05-30 23:54:43 +0000721 Content = Content.take_front(100 * 100);
Sam McCalle3559ee2019-04-25 17:47:07 +0000722 for (unsigned I = 0; I < 100 && !Content.empty(); ++I) {
723 std::tie(Line, Content) = Content.split('\n');
724 if (isIf(Line) && isErrorAboutInclude(Content.split('\n').first))
725 return true;
726 }
727 return false;
728}
729
Ilya Biryukov30c86b62019-08-20 08:54:30 +0000730bool SymbolCollector::shouldIndexFile(FileID FID) {
731 if (!Opts.FileFilter)
732 return true;
733 auto I = FilesToIndexCache.try_emplace(FID);
734 if (I.second)
735 I.first->second = Opts.FileFilter(ASTCtx->getSourceManager(), FID);
736 return I.first->second;
737}
738
Haojian Wu4c1394d2017-12-12 15:42:10 +0000739} // namespace clangd
740} // namespace clang