blob: 4e2e7d91138520288474f96c7ddf031227f5b626 [file] [log] [blame]
Haojian Wu4c1394d2017-12-12 15:42:10 +00001//===--- SymbolCollector.cpp -------------------------------------*- C++-*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SymbolCollector.h"
Haojian Wu5f100262018-03-09 14:00:34 +000011#include "../AST.h"
Eric Liu76f6b442018-01-09 17:32:00 +000012#include "../CodeCompletionStrings.h"
Eric Liu7f247652018-02-06 16:10:35 +000013#include "../Logger.h"
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +000014#include "../SourceCode.h"
Eric Liu7f247652018-02-06 16:10:35 +000015#include "../URI.h"
Eric Liuc5105f92018-02-16 14:15:55 +000016#include "CanonicalIncludes.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000017#include "clang/AST/DeclCXX.h"
Ilya Biryukovcf124bd2018-04-13 11:03:07 +000018#include "clang/AST/DeclTemplate.h"
Eric Liu9af958f2018-01-10 14:57:58 +000019#include "clang/ASTMatchers/ASTMatchFinder.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000020#include "clang/Basic/SourceManager.h"
21#include "clang/Index/IndexSymbol.h"
22#include "clang/Index/USRGeneration.h"
Eric Liu278e2d12018-01-29 15:13:29 +000023#include "llvm/Support/FileSystem.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000024#include "llvm/Support/MemoryBuffer.h"
25#include "llvm/Support/Path.h"
26
27namespace clang {
28namespace clangd {
29
30namespace {
Ilya Biryukovf118d512018-04-14 16:27:35 +000031/// If \p ND is a template specialization, returns the described template.
Ilya Biryukovcf124bd2018-04-13 11:03:07 +000032/// Otherwise, returns \p ND.
33const NamedDecl &getTemplateOrThis(const NamedDecl &ND) {
Ilya Biryukovf118d512018-04-14 16:27:35 +000034 if (auto T = ND.getDescribedTemplate())
35 return *T;
Ilya Biryukovcf124bd2018-04-13 11:03:07 +000036 return ND;
37}
38
Eric Liu7f247652018-02-06 16:10:35 +000039// Returns a URI of \p Path. Firstly, this makes the \p Path absolute using the
40// current working directory of the given SourceManager if the Path is not an
41// absolute path. If failed, this resolves relative paths against \p FallbackDir
42// to get an absolute path. Then, this tries creating an URI for the absolute
43// path with schemes specified in \p Opts. This returns an URI with the first
44// working scheme, if there is any; otherwise, this returns None.
Haojian Wu4c1394d2017-12-12 15:42:10 +000045//
46// The Path can be a path relative to the build directory, or retrieved from
47// the SourceManager.
Eric Liu7f247652018-02-06 16:10:35 +000048llvm::Optional<std::string> toURI(const SourceManager &SM, StringRef Path,
49 const SymbolCollector::Options &Opts) {
Haojian Wu4c1394d2017-12-12 15:42:10 +000050 llvm::SmallString<128> AbsolutePath(Path);
51 if (std::error_code EC =
52 SM.getFileManager().getVirtualFileSystem()->makeAbsolute(
53 AbsolutePath))
54 llvm::errs() << "Warning: could not make absolute file: '" << EC.message()
55 << '\n';
Eric Liu278e2d12018-01-29 15:13:29 +000056 if (llvm::sys::path::is_absolute(AbsolutePath)) {
57 // Handle the symbolic link path case where the current working directory
58 // (getCurrentWorkingDirectory) is a symlink./ We always want to the real
59 // file path (instead of the symlink path) for the C++ symbols.
60 //
61 // Consider the following example:
62 //
63 // src dir: /project/src/foo.h
64 // current working directory (symlink): /tmp/build -> /project/src/
65 //
66 // The file path of Symbol is "/project/src/foo.h" instead of
67 // "/tmp/build/foo.h"
68 if (const DirectoryEntry *Dir = SM.getFileManager().getDirectory(
69 llvm::sys::path::parent_path(AbsolutePath.str()))) {
70 StringRef DirName = SM.getFileManager().getCanonicalName(Dir);
71 SmallString<128> AbsoluteFilename;
72 llvm::sys::path::append(AbsoluteFilename, DirName,
73 llvm::sys::path::filename(AbsolutePath.str()));
74 AbsolutePath = AbsoluteFilename;
75 }
Eric Liu7f247652018-02-06 16:10:35 +000076 } else if (!Opts.FallbackDir.empty()) {
77 llvm::sys::fs::make_absolute(Opts.FallbackDir, AbsolutePath);
Eric Liu278e2d12018-01-29 15:13:29 +000078 llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/true);
Haojian Wu4c1394d2017-12-12 15:42:10 +000079 }
Eric Liu7f247652018-02-06 16:10:35 +000080
81 std::string ErrMsg;
82 for (const auto &Scheme : Opts.URISchemes) {
83 auto U = URI::create(AbsolutePath, Scheme);
84 if (U)
85 return U->toString();
86 ErrMsg += llvm::toString(U.takeError()) + "\n";
87 }
88 log(llvm::Twine("Failed to create an URI for file ") + AbsolutePath + ": " +
89 ErrMsg);
90 return llvm::None;
Haojian Wu4c1394d2017-12-12 15:42:10 +000091}
Eric Liu4feda802017-12-19 11:37:40 +000092
Eric Liu9af958f2018-01-10 14:57:58 +000093bool shouldFilterDecl(const NamedDecl *ND, ASTContext *ASTCtx,
94 const SymbolCollector::Options &Opts) {
95 using namespace clang::ast_matchers;
96 if (ND->isImplicit())
97 return true;
Haojian Wu9873fdd2018-01-19 09:35:55 +000098 // Skip anonymous declarations, e.g (anonymous enum/class/struct).
99 if (ND->getDeclName().isEmpty())
100 return true;
101
Eric Liu9af958f2018-01-10 14:57:58 +0000102 // FIXME: figure out a way to handle internal linkage symbols (e.g. static
103 // variables, function) defined in the .cc files. Also we skip the symbols
104 // in anonymous namespace as the qualifier names of these symbols are like
105 // `foo::<anonymous>::bar`, which need a special handling.
106 // In real world projects, we have a relatively large set of header files
107 // that define static variables (like "static const int A = 1;"), we still
108 // want to collect these symbols, although they cause potential ODR
109 // violations.
110 if (ND->isInAnonymousNamespace())
111 return true;
112
Haojian Wu9873fdd2018-01-19 09:35:55 +0000113 // We only want:
114 // * symbols in namespaces or translation unit scopes (e.g. no class
115 // members)
116 // * enum constants in unscoped enum decl (e.g. "red" in "enum {red};")
Eric Liucf177382018-02-02 10:31:42 +0000117 auto InTopLevelScope = hasDeclContext(
118 anyOf(namespaceDecl(), translationUnitDecl(), linkageSpecDecl()));
Sam McCall824913b2018-03-09 13:25:29 +0000119 // Don't index template specializations.
120 auto IsSpecialization =
121 anyOf(functionDecl(isExplicitTemplateSpecialization()),
122 cxxRecordDecl(isExplicitTemplateSpecialization()),
123 varDecl(isExplicitTemplateSpecialization()));
Eric Liucf8601b2018-02-28 09:33:15 +0000124 if (match(decl(allOf(unless(isExpansionInMainFile()),
Haojian Wu9873fdd2018-01-19 09:35:55 +0000125 anyOf(InTopLevelScope,
126 hasDeclContext(enumDecl(InTopLevelScope,
Sam McCall824913b2018-03-09 13:25:29 +0000127 unless(isScoped())))),
128 unless(IsSpecialization))),
Eric Liu9af958f2018-01-10 14:57:58 +0000129 *ND, *ASTCtx)
130 .empty())
131 return true;
132
133 return false;
134}
135
Eric Liuc5105f92018-02-16 14:15:55 +0000136// We only collect #include paths for symbols that are suitable for global code
137// completion, except for namespaces since #include path for a namespace is hard
138// to define.
139bool shouldCollectIncludePath(index::SymbolKind Kind) {
140 using SK = index::SymbolKind;
141 switch (Kind) {
142 case SK::Macro:
143 case SK::Enum:
144 case SK::Struct:
145 case SK::Class:
146 case SK::Union:
147 case SK::TypeAlias:
148 case SK::Using:
149 case SK::Function:
150 case SK::Variable:
151 case SK::EnumConstant:
152 return true;
153 default:
154 return false;
155 }
156}
157
Eric Liu02ce01f2018-02-22 10:14:05 +0000158/// Gets a canonical include (URI of the header or <header> or "header") for
159/// header of \p Loc.
160/// Returns None if fails to get include header for \p Loc.
Eric Liuc5105f92018-02-16 14:15:55 +0000161/// FIXME: we should handle .inc files whose symbols are expected be exported by
162/// their containing headers.
163llvm::Optional<std::string>
Eric Liub96363d2018-03-01 18:06:40 +0000164getIncludeHeader(llvm::StringRef QName, const SourceManager &SM,
165 SourceLocation Loc, const SymbolCollector::Options &Opts) {
Eric Liuc5105f92018-02-16 14:15:55 +0000166 llvm::StringRef FilePath = SM.getFilename(Loc);
167 if (FilePath.empty())
168 return llvm::None;
169 if (Opts.Includes) {
Eric Liub96363d2018-03-01 18:06:40 +0000170 llvm::StringRef Mapped = Opts.Includes->mapHeader(FilePath, QName);
Eric Liuc5105f92018-02-16 14:15:55 +0000171 if (Mapped != FilePath)
172 return (Mapped.startswith("<") || Mapped.startswith("\""))
173 ? Mapped.str()
174 : ("\"" + Mapped + "\"").str();
175 }
Eric Liu02ce01f2018-02-22 10:14:05 +0000176
177 return toURI(SM, SM.getFilename(Loc), Opts);
Eric Liuc5105f92018-02-16 14:15:55 +0000178}
179
Haojian Wub0189062018-01-31 12:56:51 +0000180// Return the symbol location of the given declaration `D`.
181//
182// For symbols defined inside macros:
183// * use expansion location, if the symbol is formed via macro concatenation.
184// * use spelling location, otherwise.
Eric Liucf8601b2018-02-28 09:33:15 +0000185llvm::Optional<SymbolLocation> getSymbolLocation(
186 const NamedDecl &D, SourceManager &SM, const SymbolCollector::Options &Opts,
187 const clang::LangOptions &LangOpts, std::string &FileURIStorage) {
Haojian Wu5f100262018-03-09 14:00:34 +0000188 SourceLocation NameLoc = findNameLoc(&D);
189 auto U = toURI(SM, SM.getFilename(NameLoc), Opts);
Eric Liu7f247652018-02-06 16:10:35 +0000190 if (!U)
191 return llvm::None;
192 FileURIStorage = std::move(*U);
Sam McCall60039512018-02-09 14:42:01 +0000193 SymbolLocation Result;
194 Result.FileURI = FileURIStorage;
Haojian Wu545c02a2018-04-13 08:30:39 +0000195 auto TokenLength = clang::Lexer::MeasureTokenLength(NameLoc, SM, LangOpts);
196
197 auto CreatePosition = [&SM](SourceLocation Loc) {
Haojian Wuc5340012018-04-30 11:40:02 +0000198 auto LSPLoc = sourceLocToPosition(SM, Loc);
Haojian Wu545c02a2018-04-13 08:30:39 +0000199 SymbolLocation::Position Pos;
Haojian Wuc5340012018-04-30 11:40:02 +0000200 Pos.Line = LSPLoc.line;
201 Pos.Column = LSPLoc.character;
Haojian Wu545c02a2018-04-13 08:30:39 +0000202 return Pos;
203 };
204
205 Result.Start = CreatePosition(NameLoc);
206 auto EndLoc = NameLoc.getLocWithOffset(TokenLength);
207 Result.End = CreatePosition(EndLoc);
208
Sam McCall60039512018-02-09 14:42:01 +0000209 return std::move(Result);
Haojian Wub0189062018-01-31 12:56:51 +0000210}
211
Eric Liucf8601b2018-02-28 09:33:15 +0000212// Checks whether \p ND is a definition of a TagDecl (class/struct/enum/union)
213// in a header file, in which case clangd would prefer to use ND as a canonical
214// declaration.
215// FIXME: handle symbol types that are not TagDecl (e.g. functions), if using
Fangrui Song943e12e2018-03-29 20:03:16 +0000216// the first seen declaration as canonical declaration is not a good enough
Eric Liucf8601b2018-02-28 09:33:15 +0000217// heuristic.
218bool isPreferredDeclaration(const NamedDecl &ND, index::SymbolRoleSet Roles) {
219 using namespace clang::ast_matchers;
220 return (Roles & static_cast<unsigned>(index::SymbolRole::Definition)) &&
221 llvm::isa<TagDecl>(&ND) &&
222 match(decl(isExpansionInMainFile()), ND, ND.getASTContext()).empty();
223}
224
Haojian Wu4c1394d2017-12-12 15:42:10 +0000225} // namespace
226
Eric Liu9af958f2018-01-10 14:57:58 +0000227SymbolCollector::SymbolCollector(Options Opts) : Opts(std::move(Opts)) {}
228
Eric Liu76f6b442018-01-09 17:32:00 +0000229void SymbolCollector::initialize(ASTContext &Ctx) {
230 ASTCtx = &Ctx;
231 CompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>();
232 CompletionTUInfo =
233 llvm::make_unique<CodeCompletionTUInfo>(CompletionAllocator);
234}
235
Haojian Wu4c1394d2017-12-12 15:42:10 +0000236// Always return true to continue indexing.
237bool SymbolCollector::handleDeclOccurence(
238 const Decl *D, index::SymbolRoleSet Roles,
Sam McCallb9d57112018-04-09 14:28:52 +0000239 ArrayRef<index::SymbolRelation> Relations, SourceLocation Loc,
Haojian Wu4c1394d2017-12-12 15:42:10 +0000240 index::IndexDataConsumer::ASTNodeInfo ASTNode) {
Eric Liu9af958f2018-01-10 14:57:58 +0000241 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
Sam McCall93f99bf2018-03-12 14:49:09 +0000242 assert(CompletionAllocator && CompletionTUInfo);
243 const NamedDecl *ND = llvm::dyn_cast<NamedDecl>(D);
244 if (!ND)
245 return true;
Eric Liu9af958f2018-01-10 14:57:58 +0000246
Sam McCall93f99bf2018-03-12 14:49:09 +0000247 // Mark D as referenced if this is a reference coming from the main file.
248 // D may not be an interesting symbol, but it's cheaper to check at the end.
Sam McCallb9d57112018-04-09 14:28:52 +0000249 auto &SM = ASTCtx->getSourceManager();
Sam McCall93f99bf2018-03-12 14:49:09 +0000250 if (Opts.CountReferences &&
251 (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
Sam McCallb9d57112018-04-09 14:28:52 +0000252 SM.getFileID(SM.getSpellingLoc(Loc)) == SM.getMainFileID())
Sam McCall93f99bf2018-03-12 14:49:09 +0000253 ReferencedDecls.insert(ND);
254
255 // Don't continue indexing if this is a mere reference.
Haojian Wu4c1394d2017-12-12 15:42:10 +0000256 if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) ||
257 Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
258 return true;
Sam McCall93f99bf2018-03-12 14:49:09 +0000259 if (shouldFilterDecl(ND, ASTCtx, Opts))
260 return true;
Haojian Wu4c1394d2017-12-12 15:42:10 +0000261
Sam McCall93f99bf2018-03-12 14:49:09 +0000262 llvm::SmallString<128> USR;
263 if (index::generateUSRForDecl(ND, USR))
264 return true;
265 SymbolID ID(USR);
Eric Liu76f6b442018-01-09 17:32:00 +0000266
Sam McCall93f99bf2018-03-12 14:49:09 +0000267 const NamedDecl &OriginalDecl = *cast<NamedDecl>(ASTNode.OrigD);
268 const Symbol *BasicSymbol = Symbols.find(ID);
269 if (!BasicSymbol) // Regardless of role, ND is the canonical declaration.
270 BasicSymbol = addDeclaration(*ND, std::move(ID));
271 else if (isPreferredDeclaration(OriginalDecl, Roles))
272 // If OriginalDecl is preferred, replace the existing canonical
273 // declaration (e.g. a class forward declaration). There should be at most
274 // one duplicate as we expect to see only one preferred declaration per
275 // TU, because in practice they are definitions.
276 BasicSymbol = addDeclaration(OriginalDecl, std::move(ID));
Haojian Wu4c1394d2017-12-12 15:42:10 +0000277
Sam McCall93f99bf2018-03-12 14:49:09 +0000278 if (Roles & static_cast<unsigned>(index::SymbolRole::Definition))
279 addDefinition(OriginalDecl, *BasicSymbol);
Haojian Wu4c1394d2017-12-12 15:42:10 +0000280 return true;
281}
282
Sam McCall93f99bf2018-03-12 14:49:09 +0000283void SymbolCollector::finish() {
284 // At the end of the TU, add 1 to the refcount of the ReferencedDecls.
285 for (const auto *ND : ReferencedDecls) {
286 llvm::SmallString<128> USR;
287 if (!index::generateUSRForDecl(ND, USR))
288 if (const auto *S = Symbols.find(SymbolID(USR))) {
289 Symbol Inc = *S;
290 ++Inc.References;
291 Symbols.insert(Inc);
292 }
293 }
294 ReferencedDecls.clear();
295}
296
Sam McCall60039512018-02-09 14:42:01 +0000297const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND,
298 SymbolID ID) {
299 auto &SM = ND.getASTContext().getSourceManager();
300
301 std::string QName;
302 llvm::raw_string_ostream OS(QName);
303 PrintingPolicy Policy(ASTCtx->getLangOpts());
304 // Note that inline namespaces are treated as transparent scopes. This
305 // reflects the way they're most commonly used for lookup. Ideally we'd
306 // include them, but at query time it's hard to find all the inline
307 // namespaces to query: the preamble doesn't have a dedicated list.
308 Policy.SuppressUnwrittenScope = true;
309 ND.printQualifiedName(OS, Policy);
310 OS.flush();
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000311 assert(!StringRef(QName).startswith("::"));
Sam McCall60039512018-02-09 14:42:01 +0000312
313 Symbol S;
314 S.ID = std::move(ID);
315 std::tie(S.Scope, S.Name) = splitQualifiedName(QName);
316 S.SymInfo = index::getSymbolInfo(&ND);
317 std::string FileURI;
Haojian Wudc02a3d2018-02-13 09:53:50 +0000318 if (auto DeclLoc =
319 getSymbolLocation(ND, SM, Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000320 S.CanonicalDeclaration = *DeclLoc;
321
322 // Add completion info.
323 // FIXME: we may want to choose a different redecl, or combine from several.
324 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
Ilya Biryukovcf124bd2018-04-13 11:03:07 +0000325 // We use the primary template, as clang does during code completion.
326 CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0);
Sam McCall60039512018-02-09 14:42:01 +0000327 const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
328 *ASTCtx, *PP, CodeCompletionContext::CCC_Name, *CompletionAllocator,
329 *CompletionTUInfo,
330 /*IncludeBriefComments*/ true);
331 std::string Label;
332 std::string SnippetInsertText;
333 std::string IgnoredLabel;
334 std::string PlainInsertText;
335 getLabelAndInsertText(*CCS, &Label, &SnippetInsertText,
336 /*EnableSnippets=*/true);
337 getLabelAndInsertText(*CCS, &IgnoredLabel, &PlainInsertText,
338 /*EnableSnippets=*/false);
339 std::string FilterText = getFilterText(*CCS);
340 std::string Documentation = getDocumentation(*CCS);
341 std::string CompletionDetail = getDetail(*CCS);
342
Eric Liuc5105f92018-02-16 14:15:55 +0000343 std::string Include;
344 if (Opts.CollectIncludePath && shouldCollectIncludePath(S.SymInfo.Kind)) {
345 // Use the expansion location to get the #include header since this is
346 // where the symbol is exposed.
Eric Liub96363d2018-03-01 18:06:40 +0000347 if (auto Header = getIncludeHeader(
348 QName, SM, SM.getExpansionLoc(ND.getLocation()), Opts))
Eric Liuc5105f92018-02-16 14:15:55 +0000349 Include = std::move(*Header);
350 }
Sam McCall60039512018-02-09 14:42:01 +0000351 S.CompletionFilterText = FilterText;
352 S.CompletionLabel = Label;
353 S.CompletionPlainInsertText = PlainInsertText;
354 S.CompletionSnippetInsertText = SnippetInsertText;
355 Symbol::Details Detail;
356 Detail.Documentation = Documentation;
357 Detail.CompletionDetail = CompletionDetail;
Eric Liuc5105f92018-02-16 14:15:55 +0000358 Detail.IncludeHeader = Include;
Sam McCall60039512018-02-09 14:42:01 +0000359 S.Detail = &Detail;
360
361 Symbols.insert(S);
362 return Symbols.find(S.ID);
363}
364
365void SymbolCollector::addDefinition(const NamedDecl &ND,
366 const Symbol &DeclSym) {
367 if (DeclSym.Definition)
368 return;
369 // If we saw some forward declaration, we end up copying the symbol.
370 // This is not ideal, but avoids duplicating the "is this a definition" check
371 // in clang::index. We should only see one definition.
372 Symbol S = DeclSym;
373 std::string FileURI;
374 if (auto DefLoc = getSymbolLocation(ND, ND.getASTContext().getSourceManager(),
Haojian Wudc02a3d2018-02-13 09:53:50 +0000375 Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000376 S.Definition = *DefLoc;
377 Symbols.insert(S);
378}
379
Haojian Wu4c1394d2017-12-12 15:42:10 +0000380} // namespace clangd
381} // namespace clang