blob: ac782c07c7aa1892980bb71e0ee07970d8b3ad82 [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"
Utkarsh Saxena93476552019-11-20 15:18:54 +010019#include "index/SymbolID.h"
Eric Liua57afd02018-09-17 07:43:49 +000020#include "clang/AST/Decl.h"
21#include "clang/AST/DeclBase.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000022#include "clang/AST/DeclCXX.h"
Ilya Biryukovcf124bd2018-04-13 11:03:07 +000023#include "clang/AST/DeclTemplate.h"
Haojian Wu7dd49502018-10-17 08:38:36 +000024#include "clang/Basic/SourceLocation.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000025#include "clang/Basic/SourceManager.h"
Eric Liua57afd02018-09-17 07:43:49 +000026#include "clang/Basic/Specifiers.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000027#include "clang/Index/IndexSymbol.h"
Sam McCall1b29dec2019-05-02 16:12:36 +000028#include "clang/Index/IndexingAction.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000029#include "clang/Index/USRGeneration.h"
Sam McCall62e24722019-04-17 10:36:02 +000030#include "clang/Lex/Preprocessor.h"
Eric Liua57afd02018-09-17 07:43:49 +000031#include "llvm/Support/Casting.h"
Eric Liu278e2d12018-01-29 15:13:29 +000032#include "llvm/Support/FileSystem.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000033#include "llvm/Support/MemoryBuffer.h"
34#include "llvm/Support/Path.h"
35
36namespace clang {
37namespace clangd {
Haojian Wu4c1394d2017-12-12 15:42:10 +000038namespace {
Sam McCallc008af62018-10-20 15:30:37 +000039
Ilya Biryukovf118d512018-04-14 16:27:35 +000040/// If \p ND is a template specialization, returns the described template.
Ilya Biryukovcf124bd2018-04-13 11:03:07 +000041/// Otherwise, returns \p ND.
42const NamedDecl &getTemplateOrThis(const NamedDecl &ND) {
Ilya Biryukovf118d512018-04-14 16:27:35 +000043 if (auto T = ND.getDescribedTemplate())
44 return *T;
Ilya Biryukovcf124bd2018-04-13 11:03:07 +000045 return ND;
46}
47
Eric Liu7f247652018-02-06 16:10:35 +000048// Returns a URI of \p Path. Firstly, this makes the \p Path absolute using the
49// current working directory of the given SourceManager if the Path is not an
50// absolute path. If failed, this resolves relative paths against \p FallbackDir
51// to get an absolute path. Then, this tries creating an URI for the absolute
52// path with schemes specified in \p Opts. This returns an URI with the first
53// working scheme, if there is any; otherwise, this returns None.
Haojian Wu4c1394d2017-12-12 15:42:10 +000054//
55// The Path can be a path relative to the build directory, or retrieved from
56// the SourceManager.
Kadir Cetinkayadd677932018-12-19 10:46:21 +000057std::string toURI(const SourceManager &SM, llvm::StringRef Path,
58 const SymbolCollector::Options &Opts) {
59 llvm::SmallString<128> AbsolutePath(Path);
Harlan Haskinsa02f8572019-08-01 21:32:01 +000060 if (auto File = SM.getFileManager().getFile(Path)) {
61 if (auto CanonPath = getCanonicalPath(*File, SM)) {
62 AbsolutePath = *CanonPath;
63 }
Haojian Wu4c1394d2017-12-12 15:42:10 +000064 }
Kadir Cetinkayadd677932018-12-19 10:46:21 +000065 // We don't perform is_absolute check in an else branch because makeAbsolute
66 // might return a relative path on some InMemoryFileSystems.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000067 if (!llvm::sys::path::is_absolute(AbsolutePath) && !Opts.FallbackDir.empty())
68 llvm::sys::fs::make_absolute(Opts.FallbackDir, AbsolutePath);
69 llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/true);
Eric Liuc0ac4bb2018-11-22 15:02:05 +000070 return URI::create(AbsolutePath).toString();
Haojian Wu4c1394d2017-12-12 15:42:10 +000071}
Eric Liu4feda802017-12-19 11:37:40 +000072
Eric Liud67ec242018-05-16 12:12:30 +000073// All proto generated headers should start with this line.
74static const char *PROTO_HEADER_COMMENT =
75 "// Generated by the protocol buffer compiler. DO NOT EDIT!";
76
77// Checks whether the decl is a private symbol in a header generated by
78// protobuf compiler.
79// To identify whether a proto header is actually generated by proto compiler,
80// we check whether it starts with PROTO_HEADER_COMMENT.
81// FIXME: make filtering extensible when there are more use cases for symbol
82// filters.
83bool isPrivateProtoDecl(const NamedDecl &ND) {
84 const auto &SM = ND.getASTContext().getSourceManager();
Ilya Biryukovb63c35e2019-12-10 10:08:39 +010085 auto Loc = nameLocation(ND, SM);
Eric Liud67ec242018-05-16 12:12:30 +000086 auto FileName = SM.getFilename(Loc);
87 if (!FileName.endswith(".proto.h") && !FileName.endswith(".pb.h"))
88 return false;
89 auto FID = SM.getFileID(Loc);
90 // Double check that this is an actual protobuf header.
91 if (!SM.getBufferData(FID).startswith(PROTO_HEADER_COMMENT))
92 return false;
93
94 // ND without identifier can be operators.
95 if (ND.getIdentifier() == nullptr)
96 return false;
97 auto Name = ND.getIdentifier()->getName();
98 if (!Name.contains('_'))
99 return false;
100 // Nested proto entities (e.g. Message::Nested) have top-level decls
101 // that shouldn't be used (Message_Nested). Ignore them completely.
102 // The nested entities are dangling type aliases, we may want to reconsider
103 // including them in the future.
104 // For enum constants, SOME_ENUM_CONSTANT is not private and should be
105 // indexed. Outer_INNER is private. This heuristic relies on naming style, it
106 // will include OUTER_INNER and exclude some_enum_constant.
107 // FIXME: the heuristic relies on naming style (i.e. no underscore in
108 // user-defined names) and can be improved.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000109 return (ND.getKind() != Decl::EnumConstant) || llvm::any_of(Name, islower);
Eric Liud67ec242018-05-16 12:12:30 +0000110}
111
Eric Liuc5105f92018-02-16 14:15:55 +0000112// We only collect #include paths for symbols that are suitable for global code
113// completion, except for namespaces since #include path for a namespace is hard
114// to define.
115bool shouldCollectIncludePath(index::SymbolKind Kind) {
116 using SK = index::SymbolKind;
117 switch (Kind) {
118 case SK::Macro:
119 case SK::Enum:
120 case SK::Struct:
121 case SK::Class:
122 case SK::Union:
123 case SK::TypeAlias:
124 case SK::Using:
125 case SK::Function:
126 case SK::Variable:
127 case SK::EnumConstant:
128 return true;
129 default:
130 return false;
131 }
132}
133
Haojian Wud81e3142018-08-31 12:54:13 +0000134// Return the symbol range of the token at \p TokLoc.
135std::pair<SymbolLocation::Position, SymbolLocation::Position>
136getTokenRange(SourceLocation TokLoc, const SourceManager &SM,
137 const LangOptions &LangOpts) {
138 auto CreatePosition = [&SM](SourceLocation Loc) {
139 auto LSPLoc = sourceLocToPosition(SM, Loc);
140 SymbolLocation::Position Pos;
Haojian Wub515fab2018-10-18 10:43:50 +0000141 Pos.setLine(LSPLoc.line);
142 Pos.setColumn(LSPLoc.character);
Haojian Wud81e3142018-08-31 12:54:13 +0000143 return Pos;
144 };
145
146 auto TokenLength = clang::Lexer::MeasureTokenLength(TokLoc, SM, LangOpts);
147 return {CreatePosition(TokLoc),
148 CreatePosition(TokLoc.getLocWithOffset(TokenLength))};
149}
150
151// Return the symbol location of the token at \p TokLoc.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000152llvm::Optional<SymbolLocation>
153getTokenLocation(SourceLocation TokLoc, const SourceManager &SM,
154 const SymbolCollector::Options &Opts,
155 const clang::LangOptions &LangOpts,
156 std::string &FileURIStorage) {
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000157 auto Path = SM.getFilename(TokLoc);
158 if (Path.empty())
Sam McCallc008af62018-10-20 15:30:37 +0000159 return None;
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000160 FileURIStorage = toURI(SM, Path, Opts);
Sam McCall60039512018-02-09 14:42:01 +0000161 SymbolLocation Result;
Haojian Wuee54a2b2018-11-14 11:55:45 +0000162 Result.FileURI = FileURIStorage.c_str();
Haojian Wud81e3142018-08-31 12:54:13 +0000163 auto Range = getTokenRange(TokLoc, SM, LangOpts);
164 Result.Start = Range.first;
165 Result.End = Range.second;
Haojian Wu545c02a2018-04-13 08:30:39 +0000166
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000167 return Result;
Haojian Wub0189062018-01-31 12:56:51 +0000168}
169
Eric Liucf8601b2018-02-28 09:33:15 +0000170// Checks whether \p ND is a definition of a TagDecl (class/struct/enum/union)
171// in a header file, in which case clangd would prefer to use ND as a canonical
172// declaration.
173// FIXME: handle symbol types that are not TagDecl (e.g. functions), if using
Fangrui Song943e12e2018-03-29 20:03:16 +0000174// the first seen declaration as canonical declaration is not a good enough
Eric Liucf8601b2018-02-28 09:33:15 +0000175// heuristic.
176bool isPreferredDeclaration(const NamedDecl &ND, index::SymbolRoleSet Roles) {
Kadir Cetinkaya017cc6c2019-03-08 09:54:37 +0000177 const auto &SM = ND.getASTContext().getSourceManager();
Eric Liucf8601b2018-02-28 09:33:15 +0000178 return (Roles & static_cast<unsigned>(index::SymbolRole::Definition)) &&
Haojian Wu6ae86ea2019-07-19 08:33:39 +0000179 isa<TagDecl>(&ND) && !isInsideMainFile(ND.getLocation(), SM);
Eric Liucf8601b2018-02-28 09:33:15 +0000180}
181
Sam McCallb0138312018-09-04 14:39:56 +0000182RefKind toRefKind(index::SymbolRoleSet Roles) {
183 return static_cast<RefKind>(static_cast<unsigned>(RefKind::All) & Roles);
Haojian Wud81e3142018-08-31 12:54:13 +0000184}
185
Nathan Ridge73e6f472019-06-04 04:25:44 +0000186bool shouldIndexRelation(const index::SymbolRelation &R) {
187 // We currently only index BaseOf relations, for type hierarchy subtypes.
188 return R.Roles & static_cast<unsigned>(index::SymbolRole::RelationBaseOf);
189}
190
Haojian Wu4c1394d2017-12-12 15:42:10 +0000191} // namespace
192
Eric Liu9af958f2018-01-10 14:57:58 +0000193SymbolCollector::SymbolCollector(Options Opts) : Opts(std::move(Opts)) {}
194
Eric Liu76f6b442018-01-09 17:32:00 +0000195void SymbolCollector::initialize(ASTContext &Ctx) {
196 ASTCtx = &Ctx;
197 CompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>();
198 CompletionTUInfo =
Jonas Devlieghere1c705d92019-08-14 23:52:23 +0000199 std::make_unique<CodeCompletionTUInfo>(CompletionAllocator);
Eric Liu76f6b442018-01-09 17:32:00 +0000200}
201
Eric Liu8763e482018-06-21 12:12:26 +0000202bool SymbolCollector::shouldCollectSymbol(const NamedDecl &ND,
Haojian Wu7800dbe2018-12-03 13:16:04 +0000203 const ASTContext &ASTCtx,
Sam McCall0e93b072019-01-14 10:01:17 +0000204 const Options &Opts,
205 bool IsMainFileOnly) {
Eric Liu8763e482018-06-21 12:12:26 +0000206 // Skip anonymous declarations, e.g (anonymous enum/class/struct).
207 if (ND.getDeclName().isEmpty())
208 return false;
209
Sam McCall0e93b072019-01-14 10:01:17 +0000210 // Skip main-file symbols if we are not collecting them.
211 if (IsMainFileOnly && !Opts.CollectMainFileSymbols)
212 return false;
213
214 // Skip symbols in anonymous namespaces in header files.
215 if (!IsMainFileOnly && ND.isInAnonymousNamespace())
Eric Liu8763e482018-06-21 12:12:26 +0000216 return false;
217
218 // We want most things but not "local" symbols such as symbols inside
219 // FunctionDecl, BlockDecl, ObjCMethodDecl and OMPDeclareReductionDecl.
220 // FIXME: Need a matcher for ExportDecl in order to include symbols declared
221 // within an export.
Eric Liua57afd02018-09-17 07:43:49 +0000222 const auto *DeclCtx = ND.getDeclContext();
223 switch (DeclCtx->getDeclKind()) {
224 case Decl::TranslationUnit:
225 case Decl::Namespace:
226 case Decl::LinkageSpec:
227 case Decl::Enum:
228 case Decl::ObjCProtocol:
229 case Decl::ObjCInterface:
230 case Decl::ObjCCategory:
231 case Decl::ObjCCategoryImpl:
232 case Decl::ObjCImplementation:
233 break;
234 default:
235 // Record has a few derivations (e.g. CXXRecord, Class specialization), it's
236 // easier to cast.
Sam McCallc008af62018-10-20 15:30:37 +0000237 if (!isa<RecordDecl>(DeclCtx))
Eric Liua57afd02018-09-17 07:43:49 +0000238 return false;
239 }
Eric Liu8763e482018-06-21 12:12:26 +0000240
241 // Avoid indexing internal symbols in protobuf generated headers.
242 if (isPrivateProtoDecl(ND))
243 return false;
244 return true;
245}
246
Haojian Wu4c1394d2017-12-12 15:42:10 +0000247// Always return true to continue indexing.
248bool SymbolCollector::handleDeclOccurence(
249 const Decl *D, index::SymbolRoleSet Roles,
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000250 llvm::ArrayRef<index::SymbolRelation> Relations, SourceLocation Loc,
Haojian Wu4c1394d2017-12-12 15:42:10 +0000251 index::IndexDataConsumer::ASTNodeInfo ASTNode) {
Eric Liu9af958f2018-01-10 14:57:58 +0000252 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
Sam McCall93f99bf2018-03-12 14:49:09 +0000253 assert(CompletionAllocator && CompletionTUInfo);
Eric Liu77d18112018-06-04 11:31:55 +0000254 assert(ASTNode.OrigD);
Kadir Cetinkayabb6cd822019-04-15 14:38:46 +0000255 // Indexing API puts cannonical decl into D, which might not have a valid
256 // source location for implicit/built-in decls. Fallback to original decl in
257 // such cases.
258 if (D->getLocation().isInvalid())
259 D = ASTNode.OrigD;
Eric Liu77d18112018-06-04 11:31:55 +0000260 // If OrigD is an declaration associated with a friend declaration and it's
261 // not a definition, skip it. Note that OrigD is the occurrence that the
262 // collector is currently visiting.
263 if ((ASTNode.OrigD->getFriendObjectKind() !=
264 Decl::FriendObjectKind::FOK_None) &&
265 !(Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
266 return true;
267 // A declaration created for a friend declaration should not be used as the
268 // canonical declaration in the index. Use OrigD instead, unless we've already
269 // picked a replacement for D
270 if (D->getFriendObjectKind() != Decl::FriendObjectKind::FOK_None)
271 D = CanonicalDecls.try_emplace(D, ASTNode.OrigD).first->second;
Sam McCallc008af62018-10-20 15:30:37 +0000272 const NamedDecl *ND = dyn_cast<NamedDecl>(D);
Sam McCall93f99bf2018-03-12 14:49:09 +0000273 if (!ND)
274 return true;
Eric Liu9af958f2018-01-10 14:57:58 +0000275
Sam McCall93f99bf2018-03-12 14:49:09 +0000276 // Mark D as referenced if this is a reference coming from the main file.
277 // D may not be an interesting symbol, but it's cheaper to check at the end.
Sam McCallb9d57112018-04-09 14:28:52 +0000278 auto &SM = ASTCtx->getSourceManager();
Sam McCall93f99bf2018-03-12 14:49:09 +0000279 if (Opts.CountReferences &&
280 (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
Haojian Wudecdbc12019-12-09 12:00:42 +0100281 SM.getFileID(SM.getSpellingLoc(Loc)) == 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.
Haojian Wudecdbc12019-12-09 12:00:42 +0100314 // Unlike other fields, e.g. Symbols (which use spelling locations), we use
315 // file locations for references (as it aligns the behavior of clangd's
316 // AST-based xref).
317 // FIXME: we should try to use the file locations for other fields.
Sam McCall0e93b072019-01-14 10:01:17 +0000318 if (CollectRef && !IsMainFileOnly && !isa<NamespaceDecl>(ND) &&
Haojian Wudecdbc12019-12-09 12:00:42 +0100319 (Opts.RefsInHeaders ||
320 SM.getFileID(SM.getFileLoc(Loc)) == SM.getMainFileID()))
321 DeclRefs[ND].emplace_back(SM.getFileLoc(Loc), Roles);
Haojian Wue83cacc2018-10-15 11:46:26 +0000322 // Don't continue indexing if this is a mere reference.
323 if (IsOnlyRef)
324 return true;
Haojian Wu4c1394d2017-12-12 15:42:10 +0000325
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000326 // FIXME: ObjCPropertyDecl are not properly indexed here:
327 // - ObjCPropertyDecl may have an OrigD of ObjCPropertyImplDecl, which is
328 // not a NamedDecl.
329 auto *OriginalDecl = dyn_cast<NamedDecl>(ASTNode.OrigD);
330 if (!OriginalDecl)
331 return true;
332
Haojian Wuc6ddb462018-08-07 08:57:52 +0000333 const Symbol *BasicSymbol = Symbols.find(*ID);
Sam McCall93f99bf2018-03-12 14:49:09 +0000334 if (!BasicSymbol) // Regardless of role, ND is the canonical declaration.
Sam McCall0e93b072019-01-14 10:01:17 +0000335 BasicSymbol = addDeclaration(*ND, std::move(*ID), IsMainFileOnly);
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000336 else if (isPreferredDeclaration(*OriginalDecl, Roles))
Sam McCall93f99bf2018-03-12 14:49:09 +0000337 // If OriginalDecl is preferred, replace the existing canonical
338 // declaration (e.g. a class forward declaration). There should be at most
339 // one duplicate as we expect to see only one preferred declaration per
340 // TU, because in practice they are definitions.
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000341 BasicSymbol = addDeclaration(*OriginalDecl, std::move(*ID), IsMainFileOnly);
Haojian Wu4c1394d2017-12-12 15:42:10 +0000342
Sam McCall93f99bf2018-03-12 14:49:09 +0000343 if (Roles & static_cast<unsigned>(index::SymbolRole::Definition))
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000344 addDefinition(*OriginalDecl, *BasicSymbol);
Nathan Ridge73e6f472019-06-04 04:25:44 +0000345
Haojian Wu4c1394d2017-12-12 15:42:10 +0000346 return true;
347}
348
Eric Liu48db19e2018-07-09 15:31:07 +0000349bool SymbolCollector::handleMacroOccurence(const IdentifierInfo *Name,
350 const MacroInfo *MI,
351 index::SymbolRoleSet Roles,
352 SourceLocation Loc) {
Eric Liu48db19e2018-07-09 15:31:07 +0000353 assert(PP.get());
354
355 const auto &SM = PP->getSourceManager();
Eric Liuad588af2018-11-06 10:55:21 +0000356 auto DefLoc = MI->getDefinitionLoc();
Utkarsh Saxena93476552019-11-20 15:18:54 +0100357 auto SpellingLoc = SM.getSpellingLoc(Loc);
358 bool IsMainFileSymbol = SM.isInMainFile(SM.getExpansionLoc(DefLoc));
Haojian Wu7b6f8742019-01-28 14:11:49 +0000359
Sam McCallec026532019-05-03 13:17:29 +0000360 // Builtin macros don't have useful locations and aren't needed in completion.
361 if (MI->isBuiltinMacro())
Eric Liu48db19e2018-07-09 15:31:07 +0000362 return true;
363
Haojian Wu7b6f8742019-01-28 14:11:49 +0000364 // Also avoid storing predefined macros like __DBL_MIN__.
365 if (SM.isWrittenInBuiltinFile(DefLoc))
366 return true;
367
Utkarsh Saxena93476552019-11-20 15:18:54 +0100368 auto ID = getSymbolID(Name->getName(), MI, SM);
369 if (!ID)
370 return true;
371
372 // Do not store references to main-file macros.
373 if ((static_cast<unsigned>(Opts.RefFilter) & Roles) && !IsMainFileSymbol &&
374 (Opts.RefsInHeaders || SM.getFileID(SpellingLoc) == SM.getMainFileID()))
375 MacroRefs[*ID].push_back({Loc, Roles});
376
377 // Collect symbols.
378 if (!Opts.CollectMacro)
379 return true;
380
381 // Skip main-file macros if we are not collecting them.
382 if (IsMainFileSymbol && !Opts.CollectMainFileSymbols)
383 return false;
384
Eric Liu48db19e2018-07-09 15:31:07 +0000385 // Mark the macro as referenced if this is a reference coming from the main
386 // file. The macro may not be an interesting symbol, but it's cheaper to check
387 // at the end.
388 if (Opts.CountReferences &&
389 (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
Utkarsh Saxena93476552019-11-20 15:18:54 +0100390 SM.getFileID(SpellingLoc) == SM.getMainFileID())
Eric Liu48db19e2018-07-09 15:31:07 +0000391 ReferencedMacros.insert(Name);
Utkarsh Saxena93476552019-11-20 15:18:54 +0100392
Eric Liu48db19e2018-07-09 15:31:07 +0000393 // Don't continue indexing if this is a mere reference.
394 // FIXME: remove macro with ID if it is undefined.
395 if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) ||
396 Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
397 return true;
398
Eric Liu48db19e2018-07-09 15:31:07 +0000399 // Only collect one instance in case there are multiple.
Eric Liud25f1212018-09-06 09:59:37 +0000400 if (Symbols.find(*ID) != nullptr)
Eric Liu48db19e2018-07-09 15:31:07 +0000401 return true;
402
403 Symbol S;
Eric Liud25f1212018-09-06 09:59:37 +0000404 S.ID = std::move(*ID);
Eric Liu48db19e2018-07-09 15:31:07 +0000405 S.Name = Name->getName();
Haojian Wu7b6f8742019-01-28 14:11:49 +0000406 if (!IsMainFileSymbol) {
407 S.Flags |= Symbol::IndexedForCodeCompletion;
408 S.Flags |= Symbol::VisibleOutsideFile;
409 }
Eric Liu48db19e2018-07-09 15:31:07 +0000410 S.SymInfo = index::getSymbolInfoForMacro(*MI);
411 std::string FileURI;
Eric Liuad588af2018-11-06 10:55:21 +0000412 // FIXME: use the result to filter out symbols.
Ilya Biryukov30c86b62019-08-20 08:54:30 +0000413 shouldIndexFile(SM.getFileID(Loc));
Eric Liuad588af2018-11-06 10:55:21 +0000414 if (auto DeclLoc =
415 getTokenLocation(DefLoc, SM, Opts, PP->getLangOpts(), FileURI))
Eric Liu48db19e2018-07-09 15:31:07 +0000416 S.CanonicalDeclaration = *DeclLoc;
417
418 CodeCompletionResult SymbolCompletion(Name);
419 const auto *CCS = SymbolCompletion.CreateCodeCompletionStringForMacro(
420 *PP, *CompletionAllocator, *CompletionTUInfo);
421 std::string Signature;
422 std::string SnippetSuffix;
423 getSignature(*CCS, &Signature, &SnippetSuffix);
Eric Liu48db19e2018-07-09 15:31:07 +0000424 S.Signature = Signature;
425 S.CompletionSnippetSuffix = SnippetSuffix;
Eric Liu83f63e42018-09-03 10:18:21 +0000426
Sam McCallec026532019-05-03 13:17:29 +0000427 IndexedMacros.insert(Name);
428 setIncludeLocation(S, DefLoc);
Eric Liu48db19e2018-07-09 15:31:07 +0000429 Symbols.insert(S);
430 return true;
431}
432
Nathan Ridge73e6f472019-06-04 04:25:44 +0000433void SymbolCollector::processRelations(
434 const NamedDecl &ND, const SymbolID &ID,
435 ArrayRef<index::SymbolRelation> Relations) {
436 // Store subtype relations.
437 if (!dyn_cast<TagDecl>(&ND))
438 return;
439
440 for (const auto &R : Relations) {
441 if (!shouldIndexRelation(R))
442 continue;
443
444 const Decl *Object = R.RelatedSymbol;
445
446 auto ObjectID = getSymbolID(Object);
447 if (!ObjectID)
448 continue;
449
450 // Record the relation.
451 // TODO: There may be cases where the object decl is not indexed for some
452 // reason. Those cases should probably be removed in due course, but for
453 // now there are two possible ways to handle it:
454 // (A) Avoid storing the relation in such cases.
455 // (B) Store it anyways. Clients will likely lookup() the SymbolID
456 // in the index and find nothing, but that's a situation they
457 // probably need to handle for other reasons anyways.
458 // We currently do (B) because it's simpler.
Haojian Wuc8e3f432019-10-17 14:08:28 +0000459 this->Relations.insert(Relation{ID, RelationKind::BaseOf, *ObjectID});
Nathan Ridge73e6f472019-06-04 04:25:44 +0000460 }
461}
462
Nathan Ridgeb2f45ac2019-05-30 23:54:43 +0000463void SymbolCollector::setIncludeLocation(const Symbol &S, SourceLocation Loc) {
Sam McCallec026532019-05-03 13:17:29 +0000464 if (Opts.CollectIncludePath)
465 if (shouldCollectIncludePath(S.SymInfo.Kind))
466 // Use the expansion location to get the #include header since this is
467 // where the symbol is exposed.
468 IncludeFiles[S.ID] =
469 PP->getSourceManager().getDecomposedExpansionLoc(Loc).first;
470}
471
Sam McCall93f99bf2018-03-12 14:49:09 +0000472void SymbolCollector::finish() {
Eric Liu48db19e2018-07-09 15:31:07 +0000473 // At the end of the TU, add 1 to the refcount of all referenced symbols.
474 auto IncRef = [this](const SymbolID &ID) {
475 if (const auto *S = Symbols.find(ID)) {
476 Symbol Inc = *S;
477 ++Inc.References;
478 Symbols.insert(Inc);
479 }
480 };
481 for (const NamedDecl *ND : ReferencedDecls) {
Haojian Wuc6ddb462018-08-07 08:57:52 +0000482 if (auto ID = getSymbolID(ND)) {
483 IncRef(*ID);
484 }
Eric Liu48db19e2018-07-09 15:31:07 +0000485 }
486 if (Opts.CollectMacro) {
487 assert(PP);
Sam McCallec026532019-05-03 13:17:29 +0000488 // First, drop header guards. We can't identify these until EOF.
489 for (const IdentifierInfo *II : IndexedMacros) {
490 if (const auto *MI = PP->getMacroDefinition(II).getMacroInfo())
Utkarsh Saxena02ec6ff2019-11-11 12:38:17 +0100491 if (auto ID = getSymbolID(II->getName(), MI, PP->getSourceManager()))
Sam McCallec026532019-05-03 13:17:29 +0000492 if (MI->isUsedForHeaderGuard())
493 Symbols.erase(*ID);
494 }
495 // Now increment refcounts.
Eric Liu48db19e2018-07-09 15:31:07 +0000496 for (const IdentifierInfo *II : ReferencedMacros) {
Eric Liua62c9d62018-07-09 18:54:51 +0000497 if (const auto *MI = PP->getMacroDefinition(II).getMacroInfo())
Utkarsh Saxena02ec6ff2019-11-11 12:38:17 +0100498 if (auto ID = getSymbolID(II->getName(), MI, PP->getSourceManager()))
Eric Liud25f1212018-09-06 09:59:37 +0000499 IncRef(*ID);
Eric Liu48db19e2018-07-09 15:31:07 +0000500 }
Sam McCall93f99bf2018-03-12 14:49:09 +0000501 }
Sam McCallec026532019-05-03 13:17:29 +0000502 // Fill in IncludeHeaders.
503 // We delay this until end of TU so header guards are all resolved.
Utkarsh Saxena93476552019-11-20 15:18:54 +0100504 // Symbols in slabs aren' mutable, so insert() has to walk all the strings
505 // :-(
Sam McCallec026532019-05-03 13:17:29 +0000506 llvm::SmallString<256> QName;
507 for (const auto &Entry : IncludeFiles)
508 if (const Symbol *S = Symbols.find(Entry.first)) {
509 QName = S->Scope;
510 QName.append(S->Name);
511 if (auto Header = getIncludeHeader(QName, Entry.second)) {
512 Symbol NewSym = *S;
513 NewSym.IncludeHeaders.push_back({*Header, 1});
514 Symbols.insert(NewSym);
515 }
516 }
517
Haojian Wud81e3142018-08-31 12:54:13 +0000518 const auto &SM = ASTCtx->getSourceManager();
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000519 llvm::DenseMap<FileID, std::string> URICache;
520 auto GetURI = [&](FileID FID) -> llvm::Optional<std::string> {
Haojian Wu7dd49502018-10-17 08:38:36 +0000521 auto Found = URICache.find(FID);
522 if (Found == URICache.end()) {
Haojian Wu7dd49502018-10-17 08:38:36 +0000523 if (auto *FileEntry = SM.getFileEntryForID(FID)) {
524 auto FileURI = toURI(SM, FileEntry->getName(), Opts);
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000525 Found = URICache.insert({FID, FileURI}).first;
Haojian Wuc014d862018-10-17 08:54:48 +0000526 } else {
527 // Ignore cases where we can not find a corresponding file entry
528 // for the loc, thoses are not interesting, e.g. symbols formed
529 // via macro concatenation.
Sam McCallc008af62018-10-20 15:30:37 +0000530 return None;
Haojian Wu7dd49502018-10-17 08:38:36 +0000531 }
532 }
533 return Found->second;
534 };
Utkarsh Saxena93476552019-11-20 15:18:54 +0100535 auto CollectRef =
536 [&](SymbolID ID,
537 const std::pair<SourceLocation, index::SymbolRoleSet> &LocAndRole) {
538 auto FileID = SM.getFileID(LocAndRole.first);
539 // FIXME: use the result to filter out references.
540 shouldIndexFile(FileID);
541 if (auto FileURI = GetURI(FileID)) {
542 auto Range =
543 getTokenRange(LocAndRole.first, SM, ASTCtx->getLangOpts());
544 Ref R;
545 R.Location.Start = Range.first;
546 R.Location.End = Range.second;
547 R.Location.FileURI = FileURI->c_str();
548 R.Kind = toRefKind(LocAndRole.second);
549 Refs.insert(ID, R);
550 }
551 };
552 // Populate Refs slab from MacroRefs.
553 for (const auto &IDAndRefs : MacroRefs) {
554 for (const auto &LocAndRole : IDAndRefs.second)
555 CollectRef(IDAndRefs.first, LocAndRole);
556 }
Sam McCallec026532019-05-03 13:17:29 +0000557 // Populate Refs slab from DeclRefs.
Haojian Wu7dd49502018-10-17 08:38:36 +0000558 if (auto MainFileURI = GetURI(SM.getMainFileID())) {
Sam McCallb0138312018-09-04 14:39:56 +0000559 for (const auto &It : DeclRefs) {
Haojian Wud81e3142018-08-31 12:54:13 +0000560 if (auto ID = getSymbolID(It.first)) {
Utkarsh Saxena93476552019-11-20 15:18:54 +0100561 for (const auto &LocAndRole : It.second)
562 CollectRef(*ID, LocAndRole);
Haojian Wud81e3142018-08-31 12:54:13 +0000563 }
564 }
Haojian Wud81e3142018-08-31 12:54:13 +0000565 }
566
Sam McCall93f99bf2018-03-12 14:49:09 +0000567 ReferencedDecls.clear();
Eric Liu48db19e2018-07-09 15:31:07 +0000568 ReferencedMacros.clear();
Sam McCallb0138312018-09-04 14:39:56 +0000569 DeclRefs.clear();
Eric Liuad588af2018-11-06 10:55:21 +0000570 FilesToIndexCache.clear();
Sam McCalla96efb62019-04-17 18:33:07 +0000571 HeaderIsSelfContainedCache.clear();
Sam McCallec026532019-05-03 13:17:29 +0000572 IncludeFiles.clear();
Sam McCall93f99bf2018-03-12 14:49:09 +0000573}
574
Kadir Cetinkaya86658022019-03-19 09:27:04 +0000575const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND, SymbolID ID,
Sam McCall0e93b072019-01-14 10:01:17 +0000576 bool IsMainFileOnly) {
Ilya Biryukov43714502018-05-16 12:32:44 +0000577 auto &Ctx = ND.getASTContext();
578 auto &SM = Ctx.getSourceManager();
Sam McCall60039512018-02-09 14:42:01 +0000579
Sam McCall60039512018-02-09 14:42:01 +0000580 Symbol S;
581 S.ID = std::move(ID);
Eric Liu7ad16962018-06-22 10:46:59 +0000582 std::string QName = printQualifiedName(ND);
Sam McCall032db942018-06-22 06:41:43 +0000583 // FIXME: this returns foo:bar: for objective-C methods, we prefer only foo:
584 // for consistency with CodeCompletionString and a clean name/signature split.
Kadir Cetinkaya79063de2019-04-12 10:09:24 +0000585 std::tie(S.Scope, S.Name) = splitQualifiedName(QName);
586 std::string TemplateSpecializationArgs = printTemplateSpecializationArgs(ND);
587 S.TemplateSpecializationArgs = TemplateSpecializationArgs;
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +0000588
Sam McCall0e93b072019-01-14 10:01:17 +0000589 // We collect main-file symbols, but do not use them for code completion.
590 if (!IsMainFileOnly && isIndexedForCodeCompletion(ND, Ctx))
Eric Liu6df66002018-09-06 18:52:26 +0000591 S.Flags |= Symbol::IndexedForCodeCompletion;
Eric Liu48597382018-10-18 12:23:05 +0000592 if (isImplementationDetail(&ND))
593 S.Flags |= Symbol::ImplementationDetail;
Sam McCall0e93b072019-01-14 10:01:17 +0000594 if (!IsMainFileOnly)
595 S.Flags |= Symbol::VisibleOutsideFile;
Sam McCall60039512018-02-09 14:42:01 +0000596 S.SymInfo = index::getSymbolInfo(&ND);
597 std::string FileURI;
Ilya Biryukovb63c35e2019-12-10 10:08:39 +0100598 auto Loc = nameLocation(ND, SM);
Kadir Cetinkayabb6cd822019-04-15 14:38:46 +0000599 assert(Loc.isValid() && "Invalid source location for NamedDecl");
Eric Liuad588af2018-11-06 10:55:21 +0000600 // FIXME: use the result to filter out symbols.
Ilya Biryukov30c86b62019-08-20 08:54:30 +0000601 shouldIndexFile(SM.getFileID(Loc));
Eric Liuad588af2018-11-06 10:55:21 +0000602 if (auto DeclLoc =
603 getTokenLocation(Loc, SM, Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000604 S.CanonicalDeclaration = *DeclLoc;
605
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000606 S.Origin = Opts.Origin;
607 if (ND.getAvailability() == AR_Deprecated)
608 S.Flags |= Symbol::Deprecated;
609
Sam McCall60039512018-02-09 14:42:01 +0000610 // Add completion info.
611 // FIXME: we may want to choose a different redecl, or combine from several.
612 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
Ilya Biryukovcf124bd2018-04-13 11:03:07 +0000613 // We use the primary template, as clang does during code completion.
614 CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0);
Sam McCall60039512018-02-09 14:42:01 +0000615 const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
Kadir Cetinkayab9157902018-10-24 15:24:29 +0000616 *ASTCtx, *PP, CodeCompletionContext::CCC_Symbol, *CompletionAllocator,
Sam McCall60039512018-02-09 14:42:01 +0000617 *CompletionTUInfo,
Ilya Biryukov43714502018-05-16 12:32:44 +0000618 /*IncludeBriefComments*/ false);
Ilya Biryukov43714502018-05-16 12:32:44 +0000619 std::string Documentation =
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +0000620 formatDocumentation(*CCS, getDocComment(Ctx, SymbolCompletion,
621 /*CommentsFromHeaders=*/true));
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000622 if (!(S.Flags & Symbol::IndexedForCodeCompletion)) {
Haojian Wuda79dcc2019-02-25 16:00:00 +0000623 if (Opts.StoreAllDocumentation)
624 S.Documentation = Documentation;
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000625 Symbols.insert(S);
626 return Symbols.find(S.ID);
627 }
Haojian Wuda79dcc2019-02-25 16:00:00 +0000628 S.Documentation = Documentation;
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000629 std::string Signature;
630 std::string SnippetSuffix;
631 getSignature(*CCS, &Signature, &SnippetSuffix);
632 S.Signature = Signature;
633 S.CompletionSnippetSuffix = SnippetSuffix;
Sam McCalla68951e2018-06-22 16:11:35 +0000634 std::string ReturnType = getReturnType(*CCS);
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000635 S.ReturnType = ReturnType;
Sam McCall60039512018-02-09 14:42:01 +0000636
Ilya Biryukov4d3d82e2018-11-26 15:52:16 +0000637 llvm::Optional<OpaqueType> TypeStorage;
Ilya Biryukova21392b2018-11-26 15:29:14 +0000638 if (S.Flags & Symbol::IndexedForCodeCompletion) {
Ilya Biryukov4d3d82e2018-11-26 15:52:16 +0000639 TypeStorage = OpaqueType::fromCompletionResult(*ASTCtx, SymbolCompletion);
640 if (TypeStorage)
641 S.Type = TypeStorage->raw();
Ilya Biryukova21392b2018-11-26 15:29:14 +0000642 }
643
Sam McCall60039512018-02-09 14:42:01 +0000644 Symbols.insert(S);
Sam McCallec026532019-05-03 13:17:29 +0000645 setIncludeLocation(S, ND.getLocation());
Sam McCall60039512018-02-09 14:42:01 +0000646 return Symbols.find(S.ID);
647}
648
649void SymbolCollector::addDefinition(const NamedDecl &ND,
650 const Symbol &DeclSym) {
651 if (DeclSym.Definition)
652 return;
653 // If we saw some forward declaration, we end up copying the symbol.
654 // This is not ideal, but avoids duplicating the "is this a definition" check
655 // in clang::index. We should only see one definition.
656 Symbol S = DeclSym;
657 std::string FileURI;
Eric Liuad588af2018-11-06 10:55:21 +0000658 const auto &SM = ND.getASTContext().getSourceManager();
Ilya Biryukovb63c35e2019-12-10 10:08:39 +0100659 auto Loc = nameLocation(ND, SM);
Eric Liuad588af2018-11-06 10:55:21 +0000660 // FIXME: use the result to filter out symbols.
Ilya Biryukov30c86b62019-08-20 08:54:30 +0000661 shouldIndexFile(SM.getFileID(Loc));
Eric Liuad588af2018-11-06 10:55:21 +0000662 if (auto DefLoc =
663 getTokenLocation(Loc, SM, Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000664 S.Definition = *DefLoc;
665 Symbols.insert(S);
666}
667
Sam McCalla96efb62019-04-17 18:33:07 +0000668/// Gets a canonical include (URI of the header or <header> or "header") for
669/// header of \p FID (which should usually be the *expansion* file).
670/// Returns None if includes should not be inserted for this file.
671llvm::Optional<std::string>
672SymbolCollector::getIncludeHeader(llvm::StringRef QName, FileID FID) {
673 const SourceManager &SM = ASTCtx->getSourceManager();
674 const FileEntry *FE = SM.getFileEntryForID(FID);
675 if (!FE || FE->getName().empty())
676 return llvm::None;
677 llvm::StringRef Filename = FE->getName();
678 // If a file is mapped by canonical headers, use that mapping, regardless
679 // of whether it's an otherwise-good header (header guards etc).
680 if (Opts.Includes) {
681 llvm::StringRef Canonical = Opts.Includes->mapHeader(Filename, QName);
682 // If we had a mapping, always use it.
683 if (Canonical.startswith("<") || Canonical.startswith("\""))
684 return Canonical.str();
685 if (Canonical != Filename)
686 return toURI(SM, Canonical, Opts);
687 }
688 if (!isSelfContainedHeader(FID)) {
689 // A .inc or .def file is often included into a real header to define
690 // symbols (e.g. LLVM tablegen files).
691 if (Filename.endswith(".inc") || Filename.endswith(".def"))
692 return getIncludeHeader(QName, SM.getFileID(SM.getIncludeLoc(FID)));
693 // Conservatively refuse to insert #includes to files without guards.
694 return llvm::None;
695 }
696 // Standard case: just insert the file itself.
697 return toURI(SM, Filename, Opts);
698}
699
700bool SymbolCollector::isSelfContainedHeader(FileID FID) {
701 // The real computation (which will be memoized).
702 auto Compute = [&] {
703 const SourceManager &SM = ASTCtx->getSourceManager();
704 const FileEntry *FE = SM.getFileEntryForID(FID);
705 if (!FE)
706 return false;
707 if (!PP->getHeaderSearchInfo().isFileMultipleIncludeGuarded(FE))
708 return false;
709 // This pattern indicates that a header can't be used without
710 // particular preprocessor state, usually set up by another header.
Sam McCalle3559ee2019-04-25 17:47:07 +0000711 if (isDontIncludeMeHeader(SM.getBufferData(FID)))
Sam McCalla96efb62019-04-17 18:33:07 +0000712 return false;
713 return true;
714 };
715
716 auto R = HeaderIsSelfContainedCache.try_emplace(FID, false);
717 if (R.second)
718 R.first->second = Compute();
719 return R.first->second;
720}
721
Sam McCalle3559ee2019-04-25 17:47:07 +0000722// Is Line an #if or #ifdef directive?
723static bool isIf(llvm::StringRef Line) {
724 Line = Line.ltrim();
725 if (!Line.consume_front("#"))
726 return false;
727 Line = Line.ltrim();
728 return Line.startswith("if");
729}
730// Is Line an #error directive mentioning includes?
731static bool isErrorAboutInclude(llvm::StringRef Line) {
732 Line = Line.ltrim();
733 if (!Line.consume_front("#"))
734 return false;
735 Line = Line.ltrim();
Nathan Ridgeb2f45ac2019-05-30 23:54:43 +0000736 if (!Line.startswith("error"))
Sam McCalle3559ee2019-04-25 17:47:07 +0000737 return false;
738 return Line.contains_lower("includ"); // Matches "include" or "including".
739}
740
741bool SymbolCollector::isDontIncludeMeHeader(llvm::StringRef Content) {
742 llvm::StringRef Line;
743 // Only sniff up to 100 lines or 10KB.
Nathan Ridgeb2f45ac2019-05-30 23:54:43 +0000744 Content = Content.take_front(100 * 100);
Sam McCalle3559ee2019-04-25 17:47:07 +0000745 for (unsigned I = 0; I < 100 && !Content.empty(); ++I) {
746 std::tie(Line, Content) = Content.split('\n');
747 if (isIf(Line) && isErrorAboutInclude(Content.split('\n').first))
748 return true;
749 }
750 return false;
751}
752
Ilya Biryukov30c86b62019-08-20 08:54:30 +0000753bool SymbolCollector::shouldIndexFile(FileID FID) {
754 if (!Opts.FileFilter)
755 return true;
756 auto I = FilesToIndexCache.try_emplace(FID);
757 if (I.second)
758 I.first->second = Opts.FileFilter(ASTCtx->getSourceManager(), FID);
759 return I.first->second;
760}
761
Haojian Wu4c1394d2017-12-12 15:42:10 +0000762} // namespace clangd
763} // namespace clang