blob: cc4217ad741feff5ff720747253e78494b6b8687 [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();
84 auto Loc = findNameLoc(&ND);
85 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
Eric Liuad588af2018-11-06 10:55:21 +0000150bool shouldIndexFile(const SourceManager &SM, FileID FID,
151 const SymbolCollector::Options &Opts,
152 llvm::DenseMap<FileID, bool> *FilesToIndexCache) {
153 if (!Opts.FileFilter)
154 return true;
155 auto I = FilesToIndexCache->try_emplace(FID);
156 if (I.second)
157 I.first->second = Opts.FileFilter(SM, FID);
158 return I.first->second;
159}
160
Haojian Wud81e3142018-08-31 12:54:13 +0000161// Return the symbol location of the token at \p TokLoc.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000162llvm::Optional<SymbolLocation>
163getTokenLocation(SourceLocation TokLoc, const SourceManager &SM,
164 const SymbolCollector::Options &Opts,
165 const clang::LangOptions &LangOpts,
166 std::string &FileURIStorage) {
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000167 auto Path = SM.getFilename(TokLoc);
168 if (Path.empty())
Sam McCallc008af62018-10-20 15:30:37 +0000169 return None;
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000170 FileURIStorage = toURI(SM, Path, Opts);
Sam McCall60039512018-02-09 14:42:01 +0000171 SymbolLocation Result;
Haojian Wuee54a2b2018-11-14 11:55:45 +0000172 Result.FileURI = FileURIStorage.c_str();
Haojian Wud81e3142018-08-31 12:54:13 +0000173 auto Range = getTokenRange(TokLoc, SM, LangOpts);
174 Result.Start = Range.first;
175 Result.End = Range.second;
Haojian Wu545c02a2018-04-13 08:30:39 +0000176
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000177 return Result;
Haojian Wub0189062018-01-31 12:56:51 +0000178}
179
Eric Liucf8601b2018-02-28 09:33:15 +0000180// Checks whether \p ND is a definition of a TagDecl (class/struct/enum/union)
181// in a header file, in which case clangd would prefer to use ND as a canonical
182// declaration.
183// FIXME: handle symbol types that are not TagDecl (e.g. functions), if using
Fangrui Song943e12e2018-03-29 20:03:16 +0000184// the first seen declaration as canonical declaration is not a good enough
Eric Liucf8601b2018-02-28 09:33:15 +0000185// heuristic.
186bool isPreferredDeclaration(const NamedDecl &ND, index::SymbolRoleSet Roles) {
Kadir Cetinkaya017cc6c2019-03-08 09:54:37 +0000187 const auto &SM = ND.getASTContext().getSourceManager();
Eric Liucf8601b2018-02-28 09:33:15 +0000188 return (Roles & static_cast<unsigned>(index::SymbolRole::Definition)) &&
Haojian Wu6ae86ea2019-07-19 08:33:39 +0000189 isa<TagDecl>(&ND) && !isInsideMainFile(ND.getLocation(), SM);
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
Nathan Ridge73e6f472019-06-04 04:25:44 +0000196bool shouldIndexRelation(const index::SymbolRelation &R) {
197 // We currently only index BaseOf relations, for type hierarchy subtypes.
198 return R.Roles & static_cast<unsigned>(index::SymbolRole::RelationBaseOf);
199}
200
Haojian Wu4c1394d2017-12-12 15:42:10 +0000201} // namespace
202
Eric Liu9af958f2018-01-10 14:57:58 +0000203SymbolCollector::SymbolCollector(Options Opts) : Opts(std::move(Opts)) {}
204
Eric Liu76f6b442018-01-09 17:32:00 +0000205void SymbolCollector::initialize(ASTContext &Ctx) {
206 ASTCtx = &Ctx;
207 CompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>();
208 CompletionTUInfo =
209 llvm::make_unique<CodeCompletionTUInfo>(CompletionAllocator);
210}
211
Eric Liu8763e482018-06-21 12:12:26 +0000212bool SymbolCollector::shouldCollectSymbol(const NamedDecl &ND,
Haojian Wu7800dbe2018-12-03 13:16:04 +0000213 const ASTContext &ASTCtx,
Sam McCall0e93b072019-01-14 10:01:17 +0000214 const Options &Opts,
215 bool IsMainFileOnly) {
Eric Liu8763e482018-06-21 12:12:26 +0000216 // Skip anonymous declarations, e.g (anonymous enum/class/struct).
217 if (ND.getDeclName().isEmpty())
218 return false;
219
Sam McCall0e93b072019-01-14 10:01:17 +0000220 // Skip main-file symbols if we are not collecting them.
221 if (IsMainFileOnly && !Opts.CollectMainFileSymbols)
222 return false;
223
224 // Skip symbols in anonymous namespaces in header files.
225 if (!IsMainFileOnly && ND.isInAnonymousNamespace())
Eric Liu8763e482018-06-21 12:12:26 +0000226 return false;
227
228 // We want most things but not "local" symbols such as symbols inside
229 // FunctionDecl, BlockDecl, ObjCMethodDecl and OMPDeclareReductionDecl.
230 // FIXME: Need a matcher for ExportDecl in order to include symbols declared
231 // within an export.
Eric Liua57afd02018-09-17 07:43:49 +0000232 const auto *DeclCtx = ND.getDeclContext();
233 switch (DeclCtx->getDeclKind()) {
234 case Decl::TranslationUnit:
235 case Decl::Namespace:
236 case Decl::LinkageSpec:
237 case Decl::Enum:
238 case Decl::ObjCProtocol:
239 case Decl::ObjCInterface:
240 case Decl::ObjCCategory:
241 case Decl::ObjCCategoryImpl:
242 case Decl::ObjCImplementation:
243 break;
244 default:
245 // Record has a few derivations (e.g. CXXRecord, Class specialization), it's
246 // easier to cast.
Sam McCallc008af62018-10-20 15:30:37 +0000247 if (!isa<RecordDecl>(DeclCtx))
Eric Liua57afd02018-09-17 07:43:49 +0000248 return false;
249 }
Eric Liu8763e482018-06-21 12:12:26 +0000250
251 // Avoid indexing internal symbols in protobuf generated headers.
252 if (isPrivateProtoDecl(ND))
253 return false;
254 return true;
255}
256
Haojian Wu4c1394d2017-12-12 15:42:10 +0000257// Always return true to continue indexing.
258bool SymbolCollector::handleDeclOccurence(
259 const Decl *D, index::SymbolRoleSet Roles,
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000260 llvm::ArrayRef<index::SymbolRelation> Relations, SourceLocation Loc,
Haojian Wu4c1394d2017-12-12 15:42:10 +0000261 index::IndexDataConsumer::ASTNodeInfo ASTNode) {
Eric Liu9af958f2018-01-10 14:57:58 +0000262 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
Sam McCall93f99bf2018-03-12 14:49:09 +0000263 assert(CompletionAllocator && CompletionTUInfo);
Eric Liu77d18112018-06-04 11:31:55 +0000264 assert(ASTNode.OrigD);
Kadir Cetinkayabb6cd822019-04-15 14:38:46 +0000265 // Indexing API puts cannonical decl into D, which might not have a valid
266 // source location for implicit/built-in decls. Fallback to original decl in
267 // such cases.
268 if (D->getLocation().isInvalid())
269 D = ASTNode.OrigD;
Eric Liu77d18112018-06-04 11:31:55 +0000270 // If OrigD is an declaration associated with a friend declaration and it's
271 // not a definition, skip it. Note that OrigD is the occurrence that the
272 // collector is currently visiting.
273 if ((ASTNode.OrigD->getFriendObjectKind() !=
274 Decl::FriendObjectKind::FOK_None) &&
275 !(Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
276 return true;
Kadir Cetinkaya017cc6c2019-03-08 09:54:37 +0000277 // Skip non-semantic references, we should start processing these when we
278 // decide to implement renaming with index support.
279 if ((Roles & static_cast<unsigned>(index::SymbolRole::NameReference)))
280 return true;
Eric Liu77d18112018-06-04 11:31:55 +0000281 // A declaration created for a friend declaration should not be used as the
282 // canonical declaration in the index. Use OrigD instead, unless we've already
283 // picked a replacement for D
284 if (D->getFriendObjectKind() != Decl::FriendObjectKind::FOK_None)
285 D = CanonicalDecls.try_emplace(D, ASTNode.OrigD).first->second;
Sam McCallc008af62018-10-20 15:30:37 +0000286 const NamedDecl *ND = dyn_cast<NamedDecl>(D);
Sam McCall93f99bf2018-03-12 14:49:09 +0000287 if (!ND)
288 return true;
Eric Liu9af958f2018-01-10 14:57:58 +0000289
Sam McCall93f99bf2018-03-12 14:49:09 +0000290 // Mark D as referenced if this is a reference coming from the main file.
291 // D may not be an interesting symbol, but it's cheaper to check at the end.
Sam McCallb9d57112018-04-09 14:28:52 +0000292 auto &SM = ASTCtx->getSourceManager();
Haojian Wud81e3142018-08-31 12:54:13 +0000293 auto SpellingLoc = SM.getSpellingLoc(Loc);
Sam McCall93f99bf2018-03-12 14:49:09 +0000294 if (Opts.CountReferences &&
295 (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
Haojian Wud81e3142018-08-31 12:54:13 +0000296 SM.getFileID(SpellingLoc) == SM.getMainFileID())
Sam McCall93f99bf2018-03-12 14:49:09 +0000297 ReferencedDecls.insert(ND);
298
Nathan Ridge73e6f472019-06-04 04:25:44 +0000299 auto ID = getSymbolID(ND);
300 if (!ID)
301 return true;
302
303 // Note: we need to process relations for all decl occurrences, including
304 // refs, because the indexing code only populates relations for specific
305 // occurrences. For example, RelationBaseOf is only populated for the
306 // occurrence inside the base-specifier.
307 processRelations(*ND, *ID, Relations);
308
Haojian Wue83cacc2018-10-15 11:46:26 +0000309 bool CollectRef = static_cast<unsigned>(Opts.RefFilter) & Roles;
310 bool IsOnlyRef =
311 !(Roles & (static_cast<unsigned>(index::SymbolRole::Declaration) |
312 static_cast<unsigned>(index::SymbolRole::Definition)));
Haojian Wud81e3142018-08-31 12:54:13 +0000313
Haojian Wue83cacc2018-10-15 11:46:26 +0000314 if (IsOnlyRef && !CollectRef)
Haojian Wu4c1394d2017-12-12 15:42:10 +0000315 return true;
Sam McCall0e93b072019-01-14 10:01:17 +0000316
Haojian Wu7c251fa2019-07-02 09:16:21 +0000317 // ND is the canonical (i.e. first) declaration. If it's in the main file
318 // (which is not a header), then no public declaration was visible, so assume
319 // it's main-file only.
Kadir Cetinkaya86658022019-03-19 09:27:04 +0000320 bool IsMainFileOnly =
Haojian Wu7c251fa2019-07-02 09:16:21 +0000321 SM.isWrittenInMainFile(SM.getExpansionLoc(ND->getBeginLoc())) &&
322 !ASTCtx->getLangOpts().IsHeaderFile;
Sam McCall2d02c6d2019-04-10 16:26:58 +0000323 // In C, printf is a redecl of an implicit builtin! So check OrigD instead.
324 if (ASTNode.OrigD->isImplicit() ||
325 !shouldCollectSymbol(*ND, *ASTCtx, Opts, IsMainFileOnly))
Sam McCall93f99bf2018-03-12 14:49:09 +0000326 return true;
Sam McCall0e93b072019-01-14 10:01:17 +0000327 // Do not store references to main-file symbols.
328 if (CollectRef && !IsMainFileOnly && !isa<NamespaceDecl>(ND) &&
Haojian Wu7dd49502018-10-17 08:38:36 +0000329 (Opts.RefsInHeaders || SM.getFileID(SpellingLoc) == SM.getMainFileID()))
Haojian Wue83cacc2018-10-15 11:46:26 +0000330 DeclRefs[ND].emplace_back(SpellingLoc, Roles);
331 // Don't continue indexing if this is a mere reference.
332 if (IsOnlyRef)
333 return true;
Haojian Wu4c1394d2017-12-12 15:42:10 +0000334
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000335 // FIXME: ObjCPropertyDecl are not properly indexed here:
336 // - ObjCPropertyDecl may have an OrigD of ObjCPropertyImplDecl, which is
337 // not a NamedDecl.
338 auto *OriginalDecl = dyn_cast<NamedDecl>(ASTNode.OrigD);
339 if (!OriginalDecl)
340 return true;
341
Haojian Wuc6ddb462018-08-07 08:57:52 +0000342 const Symbol *BasicSymbol = Symbols.find(*ID);
Sam McCall93f99bf2018-03-12 14:49:09 +0000343 if (!BasicSymbol) // Regardless of role, ND is the canonical declaration.
Sam McCall0e93b072019-01-14 10:01:17 +0000344 BasicSymbol = addDeclaration(*ND, std::move(*ID), IsMainFileOnly);
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000345 else if (isPreferredDeclaration(*OriginalDecl, Roles))
Sam McCall93f99bf2018-03-12 14:49:09 +0000346 // If OriginalDecl is preferred, replace the existing canonical
347 // declaration (e.g. a class forward declaration). There should be at most
348 // one duplicate as we expect to see only one preferred declaration per
349 // TU, because in practice they are definitions.
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000350 BasicSymbol = addDeclaration(*OriginalDecl, std::move(*ID), IsMainFileOnly);
Haojian Wu4c1394d2017-12-12 15:42:10 +0000351
Sam McCall93f99bf2018-03-12 14:49:09 +0000352 if (Roles & static_cast<unsigned>(index::SymbolRole::Definition))
Ilya Biryukov4e0c4002019-01-23 10:35:12 +0000353 addDefinition(*OriginalDecl, *BasicSymbol);
Nathan Ridge73e6f472019-06-04 04:25:44 +0000354
Haojian Wu4c1394d2017-12-12 15:42:10 +0000355 return true;
356}
357
Eric Liu48db19e2018-07-09 15:31:07 +0000358bool SymbolCollector::handleMacroOccurence(const IdentifierInfo *Name,
359 const MacroInfo *MI,
360 index::SymbolRoleSet Roles,
361 SourceLocation Loc) {
362 if (!Opts.CollectMacro)
363 return true;
364 assert(PP.get());
365
366 const auto &SM = PP->getSourceManager();
Eric Liuad588af2018-11-06 10:55:21 +0000367 auto DefLoc = MI->getDefinitionLoc();
Haojian Wu7b6f8742019-01-28 14:11:49 +0000368
Sam McCallec026532019-05-03 13:17:29 +0000369 // Builtin macros don't have useful locations and aren't needed in completion.
370 if (MI->isBuiltinMacro())
Eric Liu48db19e2018-07-09 15:31:07 +0000371 return true;
372
Haojian Wu7b6f8742019-01-28 14:11:49 +0000373 // Skip main-file symbols if we are not collecting them.
374 bool IsMainFileSymbol = SM.isInMainFile(SM.getExpansionLoc(DefLoc));
375 if (IsMainFileSymbol && !Opts.CollectMainFileSymbols)
376 return false;
377
378 // Also avoid storing predefined macros like __DBL_MIN__.
379 if (SM.isWrittenInBuiltinFile(DefLoc))
380 return true;
381
Eric Liu48db19e2018-07-09 15:31:07 +0000382 // Mark the macro as referenced if this is a reference coming from the main
383 // file. The macro may not be an interesting symbol, but it's cheaper to check
384 // at the end.
385 if (Opts.CountReferences &&
386 (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
387 SM.getFileID(SM.getSpellingLoc(Loc)) == SM.getMainFileID())
388 ReferencedMacros.insert(Name);
389 // Don't continue indexing if this is a mere reference.
390 // FIXME: remove macro with ID if it is undefined.
391 if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) ||
392 Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
393 return true;
394
Eric Liud25f1212018-09-06 09:59:37 +0000395 auto ID = getSymbolID(*Name, MI, SM);
396 if (!ID)
Eric Liu48db19e2018-07-09 15:31:07 +0000397 return true;
Eric Liu48db19e2018-07-09 15:31:07 +0000398
399 // 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.
413 shouldIndexFile(SM, SM.getFileID(Loc), Opts, &FilesToIndexCache);
414 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.
459 this->Relations.insert(
460 Relation{ID, index::SymbolRole::RelationBaseOf, *ObjectID});
461 }
462}
463
Nathan Ridgeb2f45ac2019-05-30 23:54:43 +0000464void SymbolCollector::setIncludeLocation(const Symbol &S, SourceLocation Loc) {
Sam McCallec026532019-05-03 13:17:29 +0000465 if (Opts.CollectIncludePath)
466 if (shouldCollectIncludePath(S.SymInfo.Kind))
467 // Use the expansion location to get the #include header since this is
468 // where the symbol is exposed.
469 IncludeFiles[S.ID] =
470 PP->getSourceManager().getDecomposedExpansionLoc(Loc).first;
471}
472
Sam McCall93f99bf2018-03-12 14:49:09 +0000473void SymbolCollector::finish() {
Eric Liu48db19e2018-07-09 15:31:07 +0000474 // At the end of the TU, add 1 to the refcount of all referenced symbols.
475 auto IncRef = [this](const SymbolID &ID) {
476 if (const auto *S = Symbols.find(ID)) {
477 Symbol Inc = *S;
478 ++Inc.References;
479 Symbols.insert(Inc);
480 }
481 };
482 for (const NamedDecl *ND : ReferencedDecls) {
Haojian Wuc6ddb462018-08-07 08:57:52 +0000483 if (auto ID = getSymbolID(ND)) {
484 IncRef(*ID);
485 }
Eric Liu48db19e2018-07-09 15:31:07 +0000486 }
487 if (Opts.CollectMacro) {
488 assert(PP);
Sam McCallec026532019-05-03 13:17:29 +0000489 // First, drop header guards. We can't identify these until EOF.
490 for (const IdentifierInfo *II : IndexedMacros) {
491 if (const auto *MI = PP->getMacroDefinition(II).getMacroInfo())
492 if (auto ID = getSymbolID(*II, MI, PP->getSourceManager()))
493 if (MI->isUsedForHeaderGuard())
494 Symbols.erase(*ID);
495 }
496 // Now increment refcounts.
Eric Liu48db19e2018-07-09 15:31:07 +0000497 for (const IdentifierInfo *II : ReferencedMacros) {
Eric Liua62c9d62018-07-09 18:54:51 +0000498 if (const auto *MI = PP->getMacroDefinition(II).getMacroInfo())
Eric Liud25f1212018-09-06 09:59:37 +0000499 if (auto ID = getSymbolID(*II, MI, PP->getSourceManager()))
500 IncRef(*ID);
Eric Liu48db19e2018-07-09 15:31:07 +0000501 }
Sam McCall93f99bf2018-03-12 14:49:09 +0000502 }
Haojian Wud81e3142018-08-31 12:54:13 +0000503
Sam McCallec026532019-05-03 13:17:29 +0000504 // Fill in IncludeHeaders.
505 // We delay this until end of TU so header guards are all resolved.
506 // Symbols in slabs aren' mutable, so insert() has to walk all the strings :-(
507 llvm::SmallString<256> QName;
508 for (const auto &Entry : IncludeFiles)
509 if (const Symbol *S = Symbols.find(Entry.first)) {
510 QName = S->Scope;
511 QName.append(S->Name);
512 if (auto Header = getIncludeHeader(QName, Entry.second)) {
513 Symbol NewSym = *S;
514 NewSym.IncludeHeaders.push_back({*Header, 1});
515 Symbols.insert(NewSym);
516 }
517 }
518
Haojian Wud81e3142018-08-31 12:54:13 +0000519 const auto &SM = ASTCtx->getSourceManager();
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000520 llvm::DenseMap<FileID, std::string> URICache;
521 auto GetURI = [&](FileID FID) -> llvm::Optional<std::string> {
Haojian Wu7dd49502018-10-17 08:38:36 +0000522 auto Found = URICache.find(FID);
523 if (Found == URICache.end()) {
Haojian Wu7dd49502018-10-17 08:38:36 +0000524 if (auto *FileEntry = SM.getFileEntryForID(FID)) {
525 auto FileURI = toURI(SM, FileEntry->getName(), Opts);
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000526 Found = URICache.insert({FID, FileURI}).first;
Haojian Wuc014d862018-10-17 08:54:48 +0000527 } else {
528 // Ignore cases where we can not find a corresponding file entry
529 // for the loc, thoses are not interesting, e.g. symbols formed
530 // via macro concatenation.
Sam McCallc008af62018-10-20 15:30:37 +0000531 return None;
Haojian Wu7dd49502018-10-17 08:38:36 +0000532 }
533 }
534 return Found->second;
535 };
Sam McCallec026532019-05-03 13:17:29 +0000536 // Populate Refs slab from DeclRefs.
Haojian Wu7dd49502018-10-17 08:38:36 +0000537 if (auto MainFileURI = GetURI(SM.getMainFileID())) {
Sam McCallb0138312018-09-04 14:39:56 +0000538 for (const auto &It : DeclRefs) {
Haojian Wud81e3142018-08-31 12:54:13 +0000539 if (auto ID = getSymbolID(It.first)) {
Haojian Wue83cacc2018-10-15 11:46:26 +0000540 for (const auto &LocAndRole : It.second) {
Haojian Wu7dd49502018-10-17 08:38:36 +0000541 auto FileID = SM.getFileID(LocAndRole.first);
Eric Liuad588af2018-11-06 10:55:21 +0000542 // FIXME: use the result to filter out references.
543 shouldIndexFile(SM, FileID, Opts, &FilesToIndexCache);
Haojian Wu7dd49502018-10-17 08:38:36 +0000544 if (auto FileURI = GetURI(FileID)) {
545 auto Range =
546 getTokenRange(LocAndRole.first, SM, ASTCtx->getLangOpts());
547 Ref R;
548 R.Location.Start = Range.first;
549 R.Location.End = Range.second;
Haojian Wuee54a2b2018-11-14 11:55:45 +0000550 R.Location.FileURI = FileURI->c_str();
Haojian Wu7dd49502018-10-17 08:38:36 +0000551 R.Kind = toRefKind(LocAndRole.second);
552 Refs.insert(*ID, R);
553 }
Haojian Wud81e3142018-08-31 12:54:13 +0000554 }
555 }
556 }
Haojian Wud81e3142018-08-31 12:54:13 +0000557 }
558
Sam McCall93f99bf2018-03-12 14:49:09 +0000559 ReferencedDecls.clear();
Eric Liu48db19e2018-07-09 15:31:07 +0000560 ReferencedMacros.clear();
Sam McCallb0138312018-09-04 14:39:56 +0000561 DeclRefs.clear();
Eric Liuad588af2018-11-06 10:55:21 +0000562 FilesToIndexCache.clear();
Sam McCalla96efb62019-04-17 18:33:07 +0000563 HeaderIsSelfContainedCache.clear();
Sam McCallec026532019-05-03 13:17:29 +0000564 IncludeFiles.clear();
Sam McCall93f99bf2018-03-12 14:49:09 +0000565}
566
Kadir Cetinkaya86658022019-03-19 09:27:04 +0000567const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND, SymbolID ID,
Sam McCall0e93b072019-01-14 10:01:17 +0000568 bool IsMainFileOnly) {
Ilya Biryukov43714502018-05-16 12:32:44 +0000569 auto &Ctx = ND.getASTContext();
570 auto &SM = Ctx.getSourceManager();
Sam McCall60039512018-02-09 14:42:01 +0000571
Sam McCall60039512018-02-09 14:42:01 +0000572 Symbol S;
573 S.ID = std::move(ID);
Eric Liu7ad16962018-06-22 10:46:59 +0000574 std::string QName = printQualifiedName(ND);
Sam McCall032db942018-06-22 06:41:43 +0000575 // FIXME: this returns foo:bar: for objective-C methods, we prefer only foo:
576 // for consistency with CodeCompletionString and a clean name/signature split.
Kadir Cetinkaya79063de2019-04-12 10:09:24 +0000577 std::tie(S.Scope, S.Name) = splitQualifiedName(QName);
578 std::string TemplateSpecializationArgs = printTemplateSpecializationArgs(ND);
579 S.TemplateSpecializationArgs = TemplateSpecializationArgs;
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +0000580
Sam McCall0e93b072019-01-14 10:01:17 +0000581 // We collect main-file symbols, but do not use them for code completion.
582 if (!IsMainFileOnly && isIndexedForCodeCompletion(ND, Ctx))
Eric Liu6df66002018-09-06 18:52:26 +0000583 S.Flags |= Symbol::IndexedForCodeCompletion;
Eric Liu48597382018-10-18 12:23:05 +0000584 if (isImplementationDetail(&ND))
585 S.Flags |= Symbol::ImplementationDetail;
Sam McCall0e93b072019-01-14 10:01:17 +0000586 if (!IsMainFileOnly)
587 S.Flags |= Symbol::VisibleOutsideFile;
Sam McCall60039512018-02-09 14:42:01 +0000588 S.SymInfo = index::getSymbolInfo(&ND);
589 std::string FileURI;
Eric Liuad588af2018-11-06 10:55:21 +0000590 auto Loc = findNameLoc(&ND);
Kadir Cetinkayabb6cd822019-04-15 14:38:46 +0000591 assert(Loc.isValid() && "Invalid source location for NamedDecl");
Eric Liuad588af2018-11-06 10:55:21 +0000592 // FIXME: use the result to filter out symbols.
593 shouldIndexFile(SM, SM.getFileID(Loc), Opts, &FilesToIndexCache);
594 if (auto DeclLoc =
595 getTokenLocation(Loc, SM, Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000596 S.CanonicalDeclaration = *DeclLoc;
597
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000598 S.Origin = Opts.Origin;
599 if (ND.getAvailability() == AR_Deprecated)
600 S.Flags |= Symbol::Deprecated;
601
Sam McCall60039512018-02-09 14:42:01 +0000602 // Add completion info.
603 // FIXME: we may want to choose a different redecl, or combine from several.
604 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
Ilya Biryukovcf124bd2018-04-13 11:03:07 +0000605 // We use the primary template, as clang does during code completion.
606 CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0);
Sam McCall60039512018-02-09 14:42:01 +0000607 const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
Kadir Cetinkayab9157902018-10-24 15:24:29 +0000608 *ASTCtx, *PP, CodeCompletionContext::CCC_Symbol, *CompletionAllocator,
Sam McCall60039512018-02-09 14:42:01 +0000609 *CompletionTUInfo,
Ilya Biryukov43714502018-05-16 12:32:44 +0000610 /*IncludeBriefComments*/ false);
Ilya Biryukov43714502018-05-16 12:32:44 +0000611 std::string Documentation =
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +0000612 formatDocumentation(*CCS, getDocComment(Ctx, SymbolCompletion,
613 /*CommentsFromHeaders=*/true));
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000614 if (!(S.Flags & Symbol::IndexedForCodeCompletion)) {
Haojian Wuda79dcc2019-02-25 16:00:00 +0000615 if (Opts.StoreAllDocumentation)
616 S.Documentation = Documentation;
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000617 Symbols.insert(S);
618 return Symbols.find(S.ID);
619 }
Haojian Wuda79dcc2019-02-25 16:00:00 +0000620 S.Documentation = Documentation;
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000621 std::string Signature;
622 std::string SnippetSuffix;
623 getSignature(*CCS, &Signature, &SnippetSuffix);
624 S.Signature = Signature;
625 S.CompletionSnippetSuffix = SnippetSuffix;
Sam McCalla68951e2018-06-22 16:11:35 +0000626 std::string ReturnType = getReturnType(*CCS);
Haojian Wu8f85b9f2019-01-10 09:22:40 +0000627 S.ReturnType = ReturnType;
Sam McCall60039512018-02-09 14:42:01 +0000628
Ilya Biryukov4d3d82e2018-11-26 15:52:16 +0000629 llvm::Optional<OpaqueType> TypeStorage;
Ilya Biryukova21392b2018-11-26 15:29:14 +0000630 if (S.Flags & Symbol::IndexedForCodeCompletion) {
Ilya Biryukov4d3d82e2018-11-26 15:52:16 +0000631 TypeStorage = OpaqueType::fromCompletionResult(*ASTCtx, SymbolCompletion);
632 if (TypeStorage)
633 S.Type = TypeStorage->raw();
Ilya Biryukova21392b2018-11-26 15:29:14 +0000634 }
635
Sam McCall60039512018-02-09 14:42:01 +0000636 Symbols.insert(S);
Sam McCallec026532019-05-03 13:17:29 +0000637 setIncludeLocation(S, ND.getLocation());
Sam McCall60039512018-02-09 14:42:01 +0000638 return Symbols.find(S.ID);
639}
640
641void SymbolCollector::addDefinition(const NamedDecl &ND,
642 const Symbol &DeclSym) {
643 if (DeclSym.Definition)
644 return;
645 // If we saw some forward declaration, we end up copying the symbol.
646 // This is not ideal, but avoids duplicating the "is this a definition" check
647 // in clang::index. We should only see one definition.
648 Symbol S = DeclSym;
649 std::string FileURI;
Eric Liuad588af2018-11-06 10:55:21 +0000650 auto Loc = findNameLoc(&ND);
651 const auto &SM = ND.getASTContext().getSourceManager();
652 // FIXME: use the result to filter out symbols.
653 shouldIndexFile(SM, SM.getFileID(Loc), Opts, &FilesToIndexCache);
654 if (auto DefLoc =
655 getTokenLocation(Loc, SM, Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000656 S.Definition = *DefLoc;
657 Symbols.insert(S);
658}
659
Sam McCalla96efb62019-04-17 18:33:07 +0000660/// Gets a canonical include (URI of the header or <header> or "header") for
661/// header of \p FID (which should usually be the *expansion* file).
662/// Returns None if includes should not be inserted for this file.
663llvm::Optional<std::string>
664SymbolCollector::getIncludeHeader(llvm::StringRef QName, FileID FID) {
665 const SourceManager &SM = ASTCtx->getSourceManager();
666 const FileEntry *FE = SM.getFileEntryForID(FID);
667 if (!FE || FE->getName().empty())
668 return llvm::None;
669 llvm::StringRef Filename = FE->getName();
670 // If a file is mapped by canonical headers, use that mapping, regardless
671 // of whether it's an otherwise-good header (header guards etc).
672 if (Opts.Includes) {
673 llvm::StringRef Canonical = Opts.Includes->mapHeader(Filename, QName);
674 // If we had a mapping, always use it.
675 if (Canonical.startswith("<") || Canonical.startswith("\""))
676 return Canonical.str();
677 if (Canonical != Filename)
678 return toURI(SM, Canonical, Opts);
679 }
680 if (!isSelfContainedHeader(FID)) {
681 // A .inc or .def file is often included into a real header to define
682 // symbols (e.g. LLVM tablegen files).
683 if (Filename.endswith(".inc") || Filename.endswith(".def"))
684 return getIncludeHeader(QName, SM.getFileID(SM.getIncludeLoc(FID)));
685 // Conservatively refuse to insert #includes to files without guards.
686 return llvm::None;
687 }
688 // Standard case: just insert the file itself.
689 return toURI(SM, Filename, Opts);
690}
691
692bool SymbolCollector::isSelfContainedHeader(FileID FID) {
693 // The real computation (which will be memoized).
694 auto Compute = [&] {
695 const SourceManager &SM = ASTCtx->getSourceManager();
696 const FileEntry *FE = SM.getFileEntryForID(FID);
697 if (!FE)
698 return false;
699 if (!PP->getHeaderSearchInfo().isFileMultipleIncludeGuarded(FE))
700 return false;
701 // This pattern indicates that a header can't be used without
702 // particular preprocessor state, usually set up by another header.
Sam McCalle3559ee2019-04-25 17:47:07 +0000703 if (isDontIncludeMeHeader(SM.getBufferData(FID)))
Sam McCalla96efb62019-04-17 18:33:07 +0000704 return false;
705 return true;
706 };
707
708 auto R = HeaderIsSelfContainedCache.try_emplace(FID, false);
709 if (R.second)
710 R.first->second = Compute();
711 return R.first->second;
712}
713
Sam McCalle3559ee2019-04-25 17:47:07 +0000714// Is Line an #if or #ifdef directive?
715static bool isIf(llvm::StringRef Line) {
716 Line = Line.ltrim();
717 if (!Line.consume_front("#"))
718 return false;
719 Line = Line.ltrim();
720 return Line.startswith("if");
721}
722// Is Line an #error directive mentioning includes?
723static bool isErrorAboutInclude(llvm::StringRef Line) {
724 Line = Line.ltrim();
725 if (!Line.consume_front("#"))
726 return false;
727 Line = Line.ltrim();
Nathan Ridgeb2f45ac2019-05-30 23:54:43 +0000728 if (!Line.startswith("error"))
Sam McCalle3559ee2019-04-25 17:47:07 +0000729 return false;
730 return Line.contains_lower("includ"); // Matches "include" or "including".
731}
732
733bool SymbolCollector::isDontIncludeMeHeader(llvm::StringRef Content) {
734 llvm::StringRef Line;
735 // Only sniff up to 100 lines or 10KB.
Nathan Ridgeb2f45ac2019-05-30 23:54:43 +0000736 Content = Content.take_front(100 * 100);
Sam McCalle3559ee2019-04-25 17:47:07 +0000737 for (unsigned I = 0; I < 100 && !Content.empty(); ++I) {
738 std::tie(Line, Content) = Content.split('\n');
739 if (isIf(Line) && isErrorAboutInclude(Content.split('\n').first))
740 return true;
741 }
742 return false;
743}
744
Haojian Wu4c1394d2017-12-12 15:42:10 +0000745} // namespace clangd
746} // namespace clang