blob: 507d0ea6ba64d1cb74dc542bac3d511c102c8a9e [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);
59 if (auto CanonPath =
60 getCanonicalPath(SM.getFileManager().getFile(Path), SM)) {
61 AbsolutePath = *CanonPath;
Haojian Wu4c1394d2017-12-12 15:42:10 +000062 }
Kadir Cetinkayadd677932018-12-19 10:46:21 +000063 // We don't perform is_absolute check in an else branch because makeAbsolute
64 // might return a relative path on some InMemoryFileSystems.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000065 if (!llvm::sys::path::is_absolute(AbsolutePath) && !Opts.FallbackDir.empty())
66 llvm::sys::fs::make_absolute(Opts.FallbackDir, AbsolutePath);
67 llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/true);
Eric Liuc0ac4bb2018-11-22 15:02:05 +000068 return URI::create(AbsolutePath).toString();
Haojian Wu4c1394d2017-12-12 15:42:10 +000069}
Eric Liu4feda802017-12-19 11:37:40 +000070
Eric Liud67ec242018-05-16 12:12:30 +000071// All proto generated headers should start with this line.
72static const char *PROTO_HEADER_COMMENT =
73 "// Generated by the protocol buffer compiler. DO NOT EDIT!";
74
75// Checks whether the decl is a private symbol in a header generated by
76// protobuf compiler.
77// To identify whether a proto header is actually generated by proto compiler,
78// we check whether it starts with PROTO_HEADER_COMMENT.
79// FIXME: make filtering extensible when there are more use cases for symbol
80// filters.
81bool isPrivateProtoDecl(const NamedDecl &ND) {
82 const auto &SM = ND.getASTContext().getSourceManager();
83 auto Loc = findNameLoc(&ND);
84 auto FileName = SM.getFilename(Loc);
85 if (!FileName.endswith(".proto.h") && !FileName.endswith(".pb.h"))
86 return false;
87 auto FID = SM.getFileID(Loc);
88 // Double check that this is an actual protobuf header.
89 if (!SM.getBufferData(FID).startswith(PROTO_HEADER_COMMENT))
90 return false;
91
92 // ND without identifier can be operators.
93 if (ND.getIdentifier() == nullptr)
94 return false;
95 auto Name = ND.getIdentifier()->getName();
96 if (!Name.contains('_'))
97 return false;
98 // Nested proto entities (e.g. Message::Nested) have top-level decls
99 // that shouldn't be used (Message_Nested). Ignore them completely.
100 // The nested entities are dangling type aliases, we may want to reconsider
101 // including them in the future.
102 // For enum constants, SOME_ENUM_CONSTANT is not private and should be
103 // indexed. Outer_INNER is private. This heuristic relies on naming style, it
104 // will include OUTER_INNER and exclude some_enum_constant.
105 // FIXME: the heuristic relies on naming style (i.e. no underscore in
106 // user-defined names) and can be improved.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000107 return (ND.getKind() != Decl::EnumConstant) || llvm::any_of(Name, islower);
Eric Liud67ec242018-05-16 12:12:30 +0000108}
109
Eric Liuc5105f92018-02-16 14:15:55 +0000110// We only collect #include paths for symbols that are suitable for global code
111// completion, except for namespaces since #include path for a namespace is hard
112// to define.
113bool shouldCollectIncludePath(index::SymbolKind Kind) {
114 using SK = index::SymbolKind;
115 switch (Kind) {
116 case SK::Macro:
117 case SK::Enum:
118 case SK::Struct:
119 case SK::Class:
120 case SK::Union:
121 case SK::TypeAlias:
122 case SK::Using:
123 case SK::Function:
124 case SK::Variable:
125 case SK::EnumConstant:
126 return true;
127 default:
128 return false;
129 }
130}
131
Haojian Wud81e3142018-08-31 12:54:13 +0000132// Return the symbol range of the token at \p TokLoc.
133std::pair<SymbolLocation::Position, SymbolLocation::Position>
134getTokenRange(SourceLocation TokLoc, const SourceManager &SM,
135 const LangOptions &LangOpts) {
136 auto CreatePosition = [&SM](SourceLocation Loc) {
137 auto LSPLoc = sourceLocToPosition(SM, Loc);
138 SymbolLocation::Position Pos;
Haojian Wub515fab2018-10-18 10:43:50 +0000139 Pos.setLine(LSPLoc.line);
140 Pos.setColumn(LSPLoc.character);
Haojian Wud81e3142018-08-31 12:54:13 +0000141 return Pos;
142 };
143
144 auto TokenLength = clang::Lexer::MeasureTokenLength(TokLoc, SM, LangOpts);
145 return {CreatePosition(TokLoc),
146 CreatePosition(TokLoc.getLocWithOffset(TokenLength))};
147}
148
Eric Liuad588af2018-11-06 10:55:21 +0000149bool shouldIndexFile(const SourceManager &SM, FileID FID,
150 const SymbolCollector::Options &Opts,
151 llvm::DenseMap<FileID, bool> *FilesToIndexCache) {
152 if (!Opts.FileFilter)
153 return true;
154 auto I = FilesToIndexCache->try_emplace(FID);
155 if (I.second)
156 I.first->second = Opts.FileFilter(SM, FID);
157 return I.first->second;
158}
159
Haojian Wud81e3142018-08-31 12:54:13 +0000160// Return the symbol location of the token at \p TokLoc.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000161llvm::Optional<SymbolLocation>
162getTokenLocation(SourceLocation TokLoc, const SourceManager &SM,
163 const SymbolCollector::Options &Opts,
164 const clang::LangOptions &LangOpts,
165 std::string &FileURIStorage) {
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000166 auto Path = SM.getFilename(TokLoc);
167 if (Path.empty())
Sam McCallc008af62018-10-20 15:30:37 +0000168 return None;
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000169 FileURIStorage = toURI(SM, Path, Opts);
Sam McCall60039512018-02-09 14:42:01 +0000170 SymbolLocation Result;
Haojian Wuee54a2b2018-11-14 11:55:45 +0000171 Result.FileURI = FileURIStorage.c_str();
Haojian Wud81e3142018-08-31 12:54:13 +0000172 auto Range = getTokenRange(TokLoc, SM, LangOpts);
173 Result.Start = Range.first;
174 Result.End = Range.second;
Haojian Wu545c02a2018-04-13 08:30:39 +0000175
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000176 return Result;
Haojian Wub0189062018-01-31 12:56:51 +0000177}
178
Eric Liucf8601b2018-02-28 09:33:15 +0000179// Checks whether \p ND is a definition of a TagDecl (class/struct/enum/union)
180// in a header file, in which case clangd would prefer to use ND as a canonical
181// declaration.
182// FIXME: handle symbol types that are not TagDecl (e.g. functions), if using
Fangrui Song943e12e2018-03-29 20:03:16 +0000183// the first seen declaration as canonical declaration is not a good enough
Eric Liucf8601b2018-02-28 09:33:15 +0000184// heuristic.
185bool isPreferredDeclaration(const NamedDecl &ND, index::SymbolRoleSet Roles) {
Kadir Cetinkaya017cc6c2019-03-08 09:54:37 +0000186 const auto &SM = ND.getASTContext().getSourceManager();
Eric Liucf8601b2018-02-28 09:33:15 +0000187 return (Roles & static_cast<unsigned>(index::SymbolRole::Definition)) &&
Sam McCallc008af62018-10-20 15:30:37 +0000188 isa<TagDecl>(&ND) &&
Sam McCall5fb97462018-10-05 14:03:04 +0000189 !SM.isWrittenInMainFile(SM.getExpansionLoc(ND.getLocation()));
Eric Liucf8601b2018-02-28 09:33:15 +0000190}
191
Sam McCallb0138312018-09-04 14:39:56 +0000192RefKind toRefKind(index::SymbolRoleSet Roles) {
193 return static_cast<RefKind>(static_cast<unsigned>(RefKind::All) & Roles);
Haojian Wud81e3142018-08-31 12:54:13 +0000194}
195
Haojian Wu4c1394d2017-12-12 15:42:10 +0000196} // namespace
197
Eric Liu9af958f2018-01-10 14:57:58 +0000198SymbolCollector::SymbolCollector(Options Opts) : Opts(std::move(Opts)) {}
199
Eric Liu76f6b442018-01-09 17:32:00 +0000200void SymbolCollector::initialize(ASTContext &Ctx) {
201 ASTCtx = &Ctx;
202 CompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>();
203 CompletionTUInfo =
204 llvm::make_unique<CodeCompletionTUInfo>(CompletionAllocator);
205}
206
Eric Liu8763e482018-06-21 12:12:26 +0000207bool SymbolCollector::shouldCollectSymbol(const NamedDecl &ND,
Haojian Wu7800dbe2018-12-03 13:16:04 +0000208 const ASTContext &ASTCtx,
Sam McCall0e93b072019-01-14 10:01:17 +0000209 const Options &Opts,
210 bool IsMainFileOnly) {
Eric Liu8763e482018-06-21 12:12:26 +0000211 // Skip anonymous declarations, e.g (anonymous enum/class/struct).
212 if (ND.getDeclName().isEmpty())
213 return false;
214
Sam McCall0e93b072019-01-14 10:01:17 +0000215 // Skip main-file symbols if we are not collecting them.
216 if (IsMainFileOnly && !Opts.CollectMainFileSymbols)
217 return false;
218
219 // Skip symbols in anonymous namespaces in header files.
220 if (!IsMainFileOnly && ND.isInAnonymousNamespace())
Eric Liu8763e482018-06-21 12:12:26 +0000221 return false;
222
223 // We want most things but not "local" symbols such as symbols inside
224 // FunctionDecl, BlockDecl, ObjCMethodDecl and OMPDeclareReductionDecl.
225 // FIXME: Need a matcher for ExportDecl in order to include symbols declared
226 // within an export.
Eric Liua57afd02018-09-17 07:43:49 +0000227 const auto *DeclCtx = ND.getDeclContext();
228 switch (DeclCtx->getDeclKind()) {
229 case Decl::TranslationUnit:
230 case Decl::Namespace:
231 case Decl::LinkageSpec:
232 case Decl::Enum:
233 case Decl::ObjCProtocol:
234 case Decl::ObjCInterface:
235 case Decl::ObjCCategory:
236 case Decl::ObjCCategoryImpl:
237 case Decl::ObjCImplementation:
238 break;
239 default:
240 // Record has a few derivations (e.g. CXXRecord, Class specialization), it's
241 // easier to cast.
Sam McCallc008af62018-10-20 15:30:37 +0000242 if (!isa<RecordDecl>(DeclCtx))
Eric Liua57afd02018-09-17 07:43:49 +0000243 return false;
244 }
Eric Liu8763e482018-06-21 12:12:26 +0000245
246 // Avoid indexing internal symbols in protobuf generated headers.
247 if (isPrivateProtoDecl(ND))
248 return false;
249 return true;
250}
251
Haojian Wu4c1394d2017-12-12 15:42:10 +0000252// Always return true to continue indexing.
253bool SymbolCollector::handleDeclOccurence(
254 const Decl *D, index::SymbolRoleSet Roles,
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000255 llvm::ArrayRef<index::SymbolRelation> Relations, SourceLocation Loc,
Haojian Wu4c1394d2017-12-12 15:42:10 +0000256 index::IndexDataConsumer::ASTNodeInfo ASTNode) {
Eric Liu9af958f2018-01-10 14:57:58 +0000257 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
Sam McCall93f99bf2018-03-12 14:49:09 +0000258 assert(CompletionAllocator && CompletionTUInfo);
Eric Liu77d18112018-06-04 11:31:55 +0000259 assert(ASTNode.OrigD);
Kadir Cetinkayabb6cd822019-04-15 14:38:46 +0000260 // Indexing API puts cannonical decl into D, which might not have a valid
261 // source location for implicit/built-in decls. Fallback to original decl in
262 // such cases.
263 if (D->getLocation().isInvalid())
264 D = ASTNode.OrigD;
Eric Liu77d18112018-06-04 11:31:55 +0000265 // If OrigD is an declaration associated with a friend declaration and it's
266 // not a definition, skip it. Note that OrigD is the occurrence that the
267 // collector is currently visiting.
268 if ((ASTNode.OrigD->getFriendObjectKind() !=
269 Decl::FriendObjectKind::FOK_None) &&
270 !(Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
271 return true;
Kadir Cetinkaya017cc6c2019-03-08 09:54:37 +0000272 // Skip non-semantic references, we should start processing these when we
273 // decide to implement renaming with index support.
274 if ((Roles & static_cast<unsigned>(index::SymbolRole::NameReference)))
275 return true;
Eric Liu77d18112018-06-04 11:31:55 +0000276 // A declaration created for a friend declaration should not be used as the
277 // canonical declaration in the index. Use OrigD instead, unless we've already
278 // picked a replacement for D
279 if (D->getFriendObjectKind() != Decl::FriendObjectKind::FOK_None)
280 D = CanonicalDecls.try_emplace(D, ASTNode.OrigD).first->second;
Sam McCallc008af62018-10-20 15:30:37 +0000281 const NamedDecl *ND = dyn_cast<NamedDecl>(D);
Sam McCall93f99bf2018-03-12 14:49:09 +0000282 if (!ND)
283 return true;
Eric Liu9af958f2018-01-10 14:57:58 +0000284
Sam McCall93f99bf2018-03-12 14:49:09 +0000285 // Mark D as referenced if this is a reference coming from the main file.
286 // D may not be an interesting symbol, but it's cheaper to check at the end.
Sam McCallb9d57112018-04-09 14:28:52 +0000287 auto &SM = ASTCtx->getSourceManager();
Haojian Wud81e3142018-08-31 12:54:13 +0000288 auto SpellingLoc = SM.getSpellingLoc(Loc);
Sam McCall93f99bf2018-03-12 14:49:09 +0000289 if (Opts.CountReferences &&
290 (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
Haojian Wud81e3142018-08-31 12:54:13 +0000291 SM.getFileID(SpellingLoc) == SM.getMainFileID())
Sam McCall93f99bf2018-03-12 14:49:09 +0000292 ReferencedDecls.insert(ND);
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
302 // ND is the canonical (i.e. first) declaration. If it's in the main file,
303 // then no public declaration was visible, so assume it's main-file only.
Kadir Cetinkaya86658022019-03-19 09:27:04 +0000304 bool IsMainFileOnly =
305 SM.isWrittenInMainFile(SM.getExpansionLoc(ND->getBeginLoc()));
Sam McCall2d02c6d2019-04-10 16:26:58 +0000306 // In C, printf is a redecl of an implicit builtin! So check OrigD instead.
307 if (ASTNode.OrigD->isImplicit() ||
308 !shouldCollectSymbol(*ND, *ASTCtx, Opts, IsMainFileOnly))
Sam McCall93f99bf2018-03-12 14:49:09 +0000309 return true;
Sam McCall0e93b072019-01-14 10:01:17 +0000310 // Do not store references to main-file symbols.
311 if (CollectRef && !IsMainFileOnly && !isa<NamespaceDecl>(ND) &&
Haojian Wu7dd49502018-10-17 08:38:36 +0000312 (Opts.RefsInHeaders || SM.getFileID(SpellingLoc) == SM.getMainFileID()))
Haojian Wue83cacc2018-10-15 11:46:26 +0000313 DeclRefs[ND].emplace_back(SpellingLoc, Roles);
314 // Don't continue indexing if this is a mere reference.
315 if (IsOnlyRef)
316 return true;
Haojian Wu4c1394d2017-12-12 15:42:10 +0000317
Haojian Wuc6ddb462018-08-07 08:57:52 +0000318 auto ID = getSymbolID(ND);
319 if (!ID)
Sam McCall93f99bf2018-03-12 14:49:09 +0000320 return true;
Eric Liu76f6b442018-01-09 17:32:00 +0000321
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000322 // FIXME: ObjCPropertyDecl are not properly indexed here:
323 // - ObjCPropertyDecl may have an OrigD of ObjCPropertyImplDecl, which is
324 // not a NamedDecl.
325 auto *OriginalDecl = dyn_cast<NamedDecl>(ASTNode.OrigD);
326 if (!OriginalDecl)
327 return true;
328
Haojian Wuc6ddb462018-08-07 08:57:52 +0000329 const Symbol *BasicSymbol = Symbols.find(*ID);
Sam McCall93f99bf2018-03-12 14:49:09 +0000330 if (!BasicSymbol) // Regardless of role, ND is the canonical declaration.
Sam McCall0e93b072019-01-14 10:01:17 +0000331 BasicSymbol = addDeclaration(*ND, std::move(*ID), IsMainFileOnly);
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000332 else if (isPreferredDeclaration(*OriginalDecl, Roles))
Sam McCall93f99bf2018-03-12 14:49:09 +0000333 // If OriginalDecl is preferred, replace the existing canonical
334 // declaration (e.g. a class forward declaration). There should be at most
335 // one duplicate as we expect to see only one preferred declaration per
336 // TU, because in practice they are definitions.
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000337 BasicSymbol = addDeclaration(*OriginalDecl, std::move(*ID), IsMainFileOnly);
Haojian Wu4c1394d2017-12-12 15:42:10 +0000338
Sam McCall93f99bf2018-03-12 14:49:09 +0000339 if (Roles & static_cast<unsigned>(index::SymbolRole::Definition))
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000340 addDefinition(*OriginalDecl, *BasicSymbol);
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
Eric Liud25f1212018-09-06 09:59:37 +0000381 auto ID = getSymbolID(*Name, MI, SM);
382 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.
399 shouldIndexFile(SM, SM.getFileID(Loc), Opts, &FilesToIndexCache);
400 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 Ridgeb2f45ac2019-05-30 23:54:43 +0000419void SymbolCollector::setIncludeLocation(const Symbol &S, SourceLocation Loc) {
Sam McCallec026532019-05-03 13:17:29 +0000420 if (Opts.CollectIncludePath)
421 if (shouldCollectIncludePath(S.SymInfo.Kind))
422 // Use the expansion location to get the #include header since this is
423 // where the symbol is exposed.
424 IncludeFiles[S.ID] =
425 PP->getSourceManager().getDecomposedExpansionLoc(Loc).first;
426}
427
Sam McCall93f99bf2018-03-12 14:49:09 +0000428void SymbolCollector::finish() {
Eric Liu48db19e2018-07-09 15:31:07 +0000429 // At the end of the TU, add 1 to the refcount of all referenced symbols.
430 auto IncRef = [this](const SymbolID &ID) {
431 if (const auto *S = Symbols.find(ID)) {
432 Symbol Inc = *S;
433 ++Inc.References;
434 Symbols.insert(Inc);
435 }
436 };
437 for (const NamedDecl *ND : ReferencedDecls) {
Haojian Wuc6ddb462018-08-07 08:57:52 +0000438 if (auto ID = getSymbolID(ND)) {
439 IncRef(*ID);
440 }
Eric Liu48db19e2018-07-09 15:31:07 +0000441 }
442 if (Opts.CollectMacro) {
443 assert(PP);
Sam McCallec026532019-05-03 13:17:29 +0000444 // First, drop header guards. We can't identify these until EOF.
445 for (const IdentifierInfo *II : IndexedMacros) {
446 if (const auto *MI = PP->getMacroDefinition(II).getMacroInfo())
447 if (auto ID = getSymbolID(*II, MI, PP->getSourceManager()))
448 if (MI->isUsedForHeaderGuard())
449 Symbols.erase(*ID);
450 }
451 // Now increment refcounts.
Eric Liu48db19e2018-07-09 15:31:07 +0000452 for (const IdentifierInfo *II : ReferencedMacros) {
Eric Liua62c9d62018-07-09 18:54:51 +0000453 if (const auto *MI = PP->getMacroDefinition(II).getMacroInfo())
Eric Liud25f1212018-09-06 09:59:37 +0000454 if (auto ID = getSymbolID(*II, MI, PP->getSourceManager()))
455 IncRef(*ID);
Eric Liu48db19e2018-07-09 15:31:07 +0000456 }
Sam McCall93f99bf2018-03-12 14:49:09 +0000457 }
Haojian Wud81e3142018-08-31 12:54:13 +0000458
Sam McCallec026532019-05-03 13:17:29 +0000459 // Fill in IncludeHeaders.
460 // We delay this until end of TU so header guards are all resolved.
461 // Symbols in slabs aren' mutable, so insert() has to walk all the strings :-(
462 llvm::SmallString<256> QName;
463 for (const auto &Entry : IncludeFiles)
464 if (const Symbol *S = Symbols.find(Entry.first)) {
465 QName = S->Scope;
466 QName.append(S->Name);
467 if (auto Header = getIncludeHeader(QName, Entry.second)) {
468 Symbol NewSym = *S;
469 NewSym.IncludeHeaders.push_back({*Header, 1});
470 Symbols.insert(NewSym);
471 }
472 }
473
Haojian Wud81e3142018-08-31 12:54:13 +0000474 const auto &SM = ASTCtx->getSourceManager();
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000475 llvm::DenseMap<FileID, std::string> URICache;
476 auto GetURI = [&](FileID FID) -> llvm::Optional<std::string> {
Haojian Wu7dd49502018-10-17 08:38:36 +0000477 auto Found = URICache.find(FID);
478 if (Found == URICache.end()) {
Haojian Wu7dd49502018-10-17 08:38:36 +0000479 if (auto *FileEntry = SM.getFileEntryForID(FID)) {
480 auto FileURI = toURI(SM, FileEntry->getName(), Opts);
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000481 Found = URICache.insert({FID, FileURI}).first;
Haojian Wuc014d862018-10-17 08:54:48 +0000482 } else {
483 // Ignore cases where we can not find a corresponding file entry
484 // for the loc, thoses are not interesting, e.g. symbols formed
485 // via macro concatenation.
Sam McCallc008af62018-10-20 15:30:37 +0000486 return None;
Haojian Wu7dd49502018-10-17 08:38:36 +0000487 }
488 }
489 return Found->second;
490 };
Sam McCallec026532019-05-03 13:17:29 +0000491 // Populate Refs slab from DeclRefs.
Haojian Wu7dd49502018-10-17 08:38:36 +0000492 if (auto MainFileURI = GetURI(SM.getMainFileID())) {
Sam McCallb0138312018-09-04 14:39:56 +0000493 for (const auto &It : DeclRefs) {
Haojian Wud81e3142018-08-31 12:54:13 +0000494 if (auto ID = getSymbolID(It.first)) {
Haojian Wue83cacc2018-10-15 11:46:26 +0000495 for (const auto &LocAndRole : It.second) {
Haojian Wu7dd49502018-10-17 08:38:36 +0000496 auto FileID = SM.getFileID(LocAndRole.first);
Eric Liuad588af2018-11-06 10:55:21 +0000497 // FIXME: use the result to filter out references.
498 shouldIndexFile(SM, FileID, Opts, &FilesToIndexCache);
Haojian Wu7dd49502018-10-17 08:38:36 +0000499 if (auto FileURI = GetURI(FileID)) {
500 auto Range =
501 getTokenRange(LocAndRole.first, SM, ASTCtx->getLangOpts());
502 Ref R;
503 R.Location.Start = Range.first;
504 R.Location.End = Range.second;
Haojian Wuee54a2b2018-11-14 11:55:45 +0000505 R.Location.FileURI = FileURI->c_str();
Haojian Wu7dd49502018-10-17 08:38:36 +0000506 R.Kind = toRefKind(LocAndRole.second);
507 Refs.insert(*ID, R);
508 }
Haojian Wud81e3142018-08-31 12:54:13 +0000509 }
510 }
511 }
Haojian Wud81e3142018-08-31 12:54:13 +0000512 }
513
Sam McCall93f99bf2018-03-12 14:49:09 +0000514 ReferencedDecls.clear();
Eric Liu48db19e2018-07-09 15:31:07 +0000515 ReferencedMacros.clear();
Sam McCallb0138312018-09-04 14:39:56 +0000516 DeclRefs.clear();
Eric Liuad588af2018-11-06 10:55:21 +0000517 FilesToIndexCache.clear();
Sam McCalla96efb62019-04-17 18:33:07 +0000518 HeaderIsSelfContainedCache.clear();
Sam McCallec026532019-05-03 13:17:29 +0000519 IncludeFiles.clear();
Sam McCall93f99bf2018-03-12 14:49:09 +0000520}
521
Kadir Cetinkaya86658022019-03-19 09:27:04 +0000522const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND, SymbolID ID,
Sam McCall0e93b072019-01-14 10:01:17 +0000523 bool IsMainFileOnly) {
Ilya Biryukov43714502018-05-16 12:32:44 +0000524 auto &Ctx = ND.getASTContext();
525 auto &SM = Ctx.getSourceManager();
Sam McCall60039512018-02-09 14:42:01 +0000526
Sam McCall60039512018-02-09 14:42:01 +0000527 Symbol S;
528 S.ID = std::move(ID);
Eric Liu7ad16962018-06-22 10:46:59 +0000529 std::string QName = printQualifiedName(ND);
Sam McCall032db942018-06-22 06:41:43 +0000530 // FIXME: this returns foo:bar: for objective-C methods, we prefer only foo:
531 // for consistency with CodeCompletionString and a clean name/signature split.
Kadir Cetinkaya79063de2019-04-12 10:09:24 +0000532 std::tie(S.Scope, S.Name) = splitQualifiedName(QName);
533 std::string TemplateSpecializationArgs = printTemplateSpecializationArgs(ND);
534 S.TemplateSpecializationArgs = TemplateSpecializationArgs;
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +0000535
Sam McCall0e93b072019-01-14 10:01:17 +0000536 // We collect main-file symbols, but do not use them for code completion.
537 if (!IsMainFileOnly && isIndexedForCodeCompletion(ND, Ctx))
Eric Liu6df66002018-09-06 18:52:26 +0000538 S.Flags |= Symbol::IndexedForCodeCompletion;
Eric Liu48597382018-10-18 12:23:05 +0000539 if (isImplementationDetail(&ND))
540 S.Flags |= Symbol::ImplementationDetail;
Sam McCall0e93b072019-01-14 10:01:17 +0000541 if (!IsMainFileOnly)
542 S.Flags |= Symbol::VisibleOutsideFile;
Sam McCall60039512018-02-09 14:42:01 +0000543 S.SymInfo = index::getSymbolInfo(&ND);
544 std::string FileURI;
Eric Liuad588af2018-11-06 10:55:21 +0000545 auto Loc = findNameLoc(&ND);
Kadir Cetinkayabb6cd822019-04-15 14:38:46 +0000546 assert(Loc.isValid() && "Invalid source location for NamedDecl");
Eric Liuad588af2018-11-06 10:55:21 +0000547 // FIXME: use the result to filter out symbols.
548 shouldIndexFile(SM, SM.getFileID(Loc), Opts, &FilesToIndexCache);
549 if (auto DeclLoc =
550 getTokenLocation(Loc, SM, Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000551 S.CanonicalDeclaration = *DeclLoc;
552
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000553 S.Origin = Opts.Origin;
554 if (ND.getAvailability() == AR_Deprecated)
555 S.Flags |= Symbol::Deprecated;
556
Sam McCall60039512018-02-09 14:42:01 +0000557 // Add completion info.
558 // FIXME: we may want to choose a different redecl, or combine from several.
559 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
Ilya Biryukovcf124bd2018-04-13 11:03:07 +0000560 // We use the primary template, as clang does during code completion.
561 CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0);
Sam McCall60039512018-02-09 14:42:01 +0000562 const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
Kadir Cetinkayab9157902018-10-24 15:24:29 +0000563 *ASTCtx, *PP, CodeCompletionContext::CCC_Symbol, *CompletionAllocator,
Sam McCall60039512018-02-09 14:42:01 +0000564 *CompletionTUInfo,
Ilya Biryukov43714502018-05-16 12:32:44 +0000565 /*IncludeBriefComments*/ false);
Ilya Biryukov43714502018-05-16 12:32:44 +0000566 std::string Documentation =
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +0000567 formatDocumentation(*CCS, getDocComment(Ctx, SymbolCompletion,
568 /*CommentsFromHeaders=*/true));
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000569 if (!(S.Flags & Symbol::IndexedForCodeCompletion)) {
Haojian Wuda79dcc2019-02-25 16:00:00 +0000570 if (Opts.StoreAllDocumentation)
571 S.Documentation = Documentation;
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000572 Symbols.insert(S);
573 return Symbols.find(S.ID);
574 }
Haojian Wuda79dcc2019-02-25 16:00:00 +0000575 S.Documentation = Documentation;
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000576 std::string Signature;
577 std::string SnippetSuffix;
578 getSignature(*CCS, &Signature, &SnippetSuffix);
579 S.Signature = Signature;
580 S.CompletionSnippetSuffix = SnippetSuffix;
Sam McCalla68951e2018-06-22 16:11:35 +0000581 std::string ReturnType = getReturnType(*CCS);
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000582 S.ReturnType = ReturnType;
Sam McCall60039512018-02-09 14:42:01 +0000583
Ilya Biryukov4d3d82e2018-11-26 15:52:16 +0000584 llvm::Optional<OpaqueType> TypeStorage;
Ilya Biryukova21392b2018-11-26 15:29:14 +0000585 if (S.Flags & Symbol::IndexedForCodeCompletion) {
Ilya Biryukov4d3d82e2018-11-26 15:52:16 +0000586 TypeStorage = OpaqueType::fromCompletionResult(*ASTCtx, SymbolCompletion);
587 if (TypeStorage)
588 S.Type = TypeStorage->raw();
Ilya Biryukova21392b2018-11-26 15:29:14 +0000589 }
590
Sam McCall60039512018-02-09 14:42:01 +0000591 Symbols.insert(S);
Sam McCallec026532019-05-03 13:17:29 +0000592 setIncludeLocation(S, ND.getLocation());
Sam McCall60039512018-02-09 14:42:01 +0000593 return Symbols.find(S.ID);
594}
595
596void SymbolCollector::addDefinition(const NamedDecl &ND,
597 const Symbol &DeclSym) {
598 if (DeclSym.Definition)
599 return;
600 // If we saw some forward declaration, we end up copying the symbol.
601 // This is not ideal, but avoids duplicating the "is this a definition" check
602 // in clang::index. We should only see one definition.
603 Symbol S = DeclSym;
604 std::string FileURI;
Eric Liuad588af2018-11-06 10:55:21 +0000605 auto Loc = findNameLoc(&ND);
606 const auto &SM = ND.getASTContext().getSourceManager();
607 // FIXME: use the result to filter out symbols.
608 shouldIndexFile(SM, SM.getFileID(Loc), Opts, &FilesToIndexCache);
609 if (auto DefLoc =
610 getTokenLocation(Loc, SM, Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000611 S.Definition = *DefLoc;
612 Symbols.insert(S);
613}
614
Sam McCalla96efb62019-04-17 18:33:07 +0000615/// Gets a canonical include (URI of the header or <header> or "header") for
616/// header of \p FID (which should usually be the *expansion* file).
617/// Returns None if includes should not be inserted for this file.
618llvm::Optional<std::string>
619SymbolCollector::getIncludeHeader(llvm::StringRef QName, FileID FID) {
620 const SourceManager &SM = ASTCtx->getSourceManager();
621 const FileEntry *FE = SM.getFileEntryForID(FID);
622 if (!FE || FE->getName().empty())
623 return llvm::None;
624 llvm::StringRef Filename = FE->getName();
625 // If a file is mapped by canonical headers, use that mapping, regardless
626 // of whether it's an otherwise-good header (header guards etc).
627 if (Opts.Includes) {
628 llvm::StringRef Canonical = Opts.Includes->mapHeader(Filename, QName);
629 // If we had a mapping, always use it.
630 if (Canonical.startswith("<") || Canonical.startswith("\""))
631 return Canonical.str();
632 if (Canonical != Filename)
633 return toURI(SM, Canonical, Opts);
634 }
635 if (!isSelfContainedHeader(FID)) {
636 // A .inc or .def file is often included into a real header to define
637 // symbols (e.g. LLVM tablegen files).
638 if (Filename.endswith(".inc") || Filename.endswith(".def"))
639 return getIncludeHeader(QName, SM.getFileID(SM.getIncludeLoc(FID)));
640 // Conservatively refuse to insert #includes to files without guards.
641 return llvm::None;
642 }
643 // Standard case: just insert the file itself.
644 return toURI(SM, Filename, Opts);
645}
646
647bool SymbolCollector::isSelfContainedHeader(FileID FID) {
648 // The real computation (which will be memoized).
649 auto Compute = [&] {
650 const SourceManager &SM = ASTCtx->getSourceManager();
651 const FileEntry *FE = SM.getFileEntryForID(FID);
652 if (!FE)
653 return false;
654 if (!PP->getHeaderSearchInfo().isFileMultipleIncludeGuarded(FE))
655 return false;
656 // This pattern indicates that a header can't be used without
657 // particular preprocessor state, usually set up by another header.
Sam McCalle3559ee2019-04-25 17:47:07 +0000658 if (isDontIncludeMeHeader(SM.getBufferData(FID)))
Sam McCalla96efb62019-04-17 18:33:07 +0000659 return false;
660 return true;
661 };
662
663 auto R = HeaderIsSelfContainedCache.try_emplace(FID, false);
664 if (R.second)
665 R.first->second = Compute();
666 return R.first->second;
667}
668
Sam McCalle3559ee2019-04-25 17:47:07 +0000669// Is Line an #if or #ifdef directive?
670static bool isIf(llvm::StringRef Line) {
671 Line = Line.ltrim();
672 if (!Line.consume_front("#"))
673 return false;
674 Line = Line.ltrim();
675 return Line.startswith("if");
676}
677// Is Line an #error directive mentioning includes?
678static bool isErrorAboutInclude(llvm::StringRef Line) {
679 Line = Line.ltrim();
680 if (!Line.consume_front("#"))
681 return false;
682 Line = Line.ltrim();
Nathan Ridgeb2f45ac2019-05-30 23:54:43 +0000683 if (!Line.startswith("error"))
Sam McCalle3559ee2019-04-25 17:47:07 +0000684 return false;
685 return Line.contains_lower("includ"); // Matches "include" or "including".
686}
687
688bool SymbolCollector::isDontIncludeMeHeader(llvm::StringRef Content) {
689 llvm::StringRef Line;
690 // Only sniff up to 100 lines or 10KB.
Nathan Ridgeb2f45ac2019-05-30 23:54:43 +0000691 Content = Content.take_front(100 * 100);
Sam McCalle3559ee2019-04-25 17:47:07 +0000692 for (unsigned I = 0; I < 100 && !Content.empty(); ++I) {
693 std::tie(Line, Content) = Content.split('\n');
694 if (isIf(Line) && isErrorAboutInclude(Content.split('\n').first))
695 return true;
696 }
697 return false;
698}
699
Haojian Wu4c1394d2017-12-12 15:42:10 +0000700} // namespace clangd
701} // namespace clang