blob: fe994d0137ecd5d2350b5f9cd39f13f7e704fb72 [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"
Eric Liu76f6b442018-01-09 17:32:00 +000011#include "../CodeCompletionStrings.h"
Eric Liu7f247652018-02-06 16:10:35 +000012#include "../Logger.h"
13#include "../URI.h"
Eric Liuc5105f92018-02-16 14:15:55 +000014#include "CanonicalIncludes.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000015#include "clang/AST/DeclCXX.h"
Eric Liu9af958f2018-01-10 14:57:58 +000016#include "clang/ASTMatchers/ASTMatchFinder.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000017#include "clang/Basic/SourceManager.h"
18#include "clang/Index/IndexSymbol.h"
19#include "clang/Index/USRGeneration.h"
Eric Liu278e2d12018-01-29 15:13:29 +000020#include "llvm/Support/FileSystem.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000021#include "llvm/Support/MemoryBuffer.h"
22#include "llvm/Support/Path.h"
23
24namespace clang {
25namespace clangd {
26
27namespace {
Eric Liu7f247652018-02-06 16:10:35 +000028// Returns a URI of \p Path. Firstly, this makes the \p Path absolute using the
29// current working directory of the given SourceManager if the Path is not an
30// absolute path. If failed, this resolves relative paths against \p FallbackDir
31// to get an absolute path. Then, this tries creating an URI for the absolute
32// path with schemes specified in \p Opts. This returns an URI with the first
33// working scheme, if there is any; otherwise, this returns None.
Haojian Wu4c1394d2017-12-12 15:42:10 +000034//
35// The Path can be a path relative to the build directory, or retrieved from
36// the SourceManager.
Eric Liu7f247652018-02-06 16:10:35 +000037llvm::Optional<std::string> toURI(const SourceManager &SM, StringRef Path,
38 const SymbolCollector::Options &Opts) {
Haojian Wu4c1394d2017-12-12 15:42:10 +000039 llvm::SmallString<128> AbsolutePath(Path);
40 if (std::error_code EC =
41 SM.getFileManager().getVirtualFileSystem()->makeAbsolute(
42 AbsolutePath))
43 llvm::errs() << "Warning: could not make absolute file: '" << EC.message()
44 << '\n';
Eric Liu278e2d12018-01-29 15:13:29 +000045 if (llvm::sys::path::is_absolute(AbsolutePath)) {
46 // Handle the symbolic link path case where the current working directory
47 // (getCurrentWorkingDirectory) is a symlink./ We always want to the real
48 // file path (instead of the symlink path) for the C++ symbols.
49 //
50 // Consider the following example:
51 //
52 // src dir: /project/src/foo.h
53 // current working directory (symlink): /tmp/build -> /project/src/
54 //
55 // The file path of Symbol is "/project/src/foo.h" instead of
56 // "/tmp/build/foo.h"
57 if (const DirectoryEntry *Dir = SM.getFileManager().getDirectory(
58 llvm::sys::path::parent_path(AbsolutePath.str()))) {
59 StringRef DirName = SM.getFileManager().getCanonicalName(Dir);
60 SmallString<128> AbsoluteFilename;
61 llvm::sys::path::append(AbsoluteFilename, DirName,
62 llvm::sys::path::filename(AbsolutePath.str()));
63 AbsolutePath = AbsoluteFilename;
64 }
Eric Liu7f247652018-02-06 16:10:35 +000065 } else if (!Opts.FallbackDir.empty()) {
66 llvm::sys::fs::make_absolute(Opts.FallbackDir, AbsolutePath);
Eric Liu278e2d12018-01-29 15:13:29 +000067 llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/true);
Haojian Wu4c1394d2017-12-12 15:42:10 +000068 }
Eric Liu7f247652018-02-06 16:10:35 +000069
70 std::string ErrMsg;
71 for (const auto &Scheme : Opts.URISchemes) {
72 auto U = URI::create(AbsolutePath, Scheme);
73 if (U)
74 return U->toString();
75 ErrMsg += llvm::toString(U.takeError()) + "\n";
76 }
77 log(llvm::Twine("Failed to create an URI for file ") + AbsolutePath + ": " +
78 ErrMsg);
79 return llvm::None;
Haojian Wu4c1394d2017-12-12 15:42:10 +000080}
Eric Liu4feda802017-12-19 11:37:40 +000081
Sam McCall8b2faee2018-01-19 22:18:21 +000082// "a::b::c", return {"a::b::", "c"}. Scope is empty if there's no qualifier.
Eric Liu4feda802017-12-19 11:37:40 +000083std::pair<llvm::StringRef, llvm::StringRef>
84splitQualifiedName(llvm::StringRef QName) {
85 assert(!QName.startswith("::") && "Qualified names should not start with ::");
86 size_t Pos = QName.rfind("::");
87 if (Pos == llvm::StringRef::npos)
88 return {StringRef(), QName};
Sam McCall8b2faee2018-01-19 22:18:21 +000089 return {QName.substr(0, Pos + 2), QName.substr(Pos + 2)};
Eric Liu4feda802017-12-19 11:37:40 +000090}
91
Eric Liu9af958f2018-01-10 14:57:58 +000092bool shouldFilterDecl(const NamedDecl *ND, ASTContext *ASTCtx,
93 const SymbolCollector::Options &Opts) {
94 using namespace clang::ast_matchers;
95 if (ND->isImplicit())
96 return true;
Haojian Wu9873fdd2018-01-19 09:35:55 +000097 // Skip anonymous declarations, e.g (anonymous enum/class/struct).
98 if (ND->getDeclName().isEmpty())
99 return true;
100
Eric Liu9af958f2018-01-10 14:57:58 +0000101 // FIXME: figure out a way to handle internal linkage symbols (e.g. static
102 // variables, function) defined in the .cc files. Also we skip the symbols
103 // in anonymous namespace as the qualifier names of these symbols are like
104 // `foo::<anonymous>::bar`, which need a special handling.
105 // In real world projects, we have a relatively large set of header files
106 // that define static variables (like "static const int A = 1;"), we still
107 // want to collect these symbols, although they cause potential ODR
108 // violations.
109 if (ND->isInAnonymousNamespace())
110 return true;
111
Haojian Wu9873fdd2018-01-19 09:35:55 +0000112 // We only want:
113 // * symbols in namespaces or translation unit scopes (e.g. no class
114 // members)
115 // * enum constants in unscoped enum decl (e.g. "red" in "enum {red};")
Eric Liucf177382018-02-02 10:31:42 +0000116 auto InTopLevelScope = hasDeclContext(
117 anyOf(namespaceDecl(), translationUnitDecl(), linkageSpecDecl()));
Eric Liucf8601b2018-02-28 09:33:15 +0000118 if (match(decl(allOf(unless(isExpansionInMainFile()),
Haojian Wu9873fdd2018-01-19 09:35:55 +0000119 anyOf(InTopLevelScope,
120 hasDeclContext(enumDecl(InTopLevelScope,
121 unless(isScoped())))))),
Eric Liu9af958f2018-01-10 14:57:58 +0000122 *ND, *ASTCtx)
123 .empty())
124 return true;
125
126 return false;
127}
128
Eric Liuc5105f92018-02-16 14:15:55 +0000129// We only collect #include paths for symbols that are suitable for global code
130// completion, except for namespaces since #include path for a namespace is hard
131// to define.
132bool shouldCollectIncludePath(index::SymbolKind Kind) {
133 using SK = index::SymbolKind;
134 switch (Kind) {
135 case SK::Macro:
136 case SK::Enum:
137 case SK::Struct:
138 case SK::Class:
139 case SK::Union:
140 case SK::TypeAlias:
141 case SK::Using:
142 case SK::Function:
143 case SK::Variable:
144 case SK::EnumConstant:
145 return true;
146 default:
147 return false;
148 }
149}
150
Eric Liu02ce01f2018-02-22 10:14:05 +0000151/// Gets a canonical include (URI of the header or <header> or "header") for
152/// header of \p Loc.
153/// Returns None if fails to get include header for \p Loc.
Eric Liuc5105f92018-02-16 14:15:55 +0000154/// FIXME: we should handle .inc files whose symbols are expected be exported by
155/// their containing headers.
156llvm::Optional<std::string>
Eric Liub96363d2018-03-01 18:06:40 +0000157getIncludeHeader(llvm::StringRef QName, const SourceManager &SM,
158 SourceLocation Loc, const SymbolCollector::Options &Opts) {
Eric Liuc5105f92018-02-16 14:15:55 +0000159 llvm::StringRef FilePath = SM.getFilename(Loc);
160 if (FilePath.empty())
161 return llvm::None;
162 if (Opts.Includes) {
Eric Liub96363d2018-03-01 18:06:40 +0000163 llvm::StringRef Mapped = Opts.Includes->mapHeader(FilePath, QName);
Eric Liuc5105f92018-02-16 14:15:55 +0000164 if (Mapped != FilePath)
165 return (Mapped.startswith("<") || Mapped.startswith("\""))
166 ? Mapped.str()
167 : ("\"" + Mapped + "\"").str();
168 }
Eric Liu02ce01f2018-02-22 10:14:05 +0000169
170 return toURI(SM, SM.getFilename(Loc), Opts);
Eric Liuc5105f92018-02-16 14:15:55 +0000171}
172
Haojian Wub0189062018-01-31 12:56:51 +0000173// Return the symbol location of the given declaration `D`.
174//
175// For symbols defined inside macros:
176// * use expansion location, if the symbol is formed via macro concatenation.
177// * use spelling location, otherwise.
Eric Liucf8601b2018-02-28 09:33:15 +0000178llvm::Optional<SymbolLocation> getSymbolLocation(
179 const NamedDecl &D, SourceManager &SM, const SymbolCollector::Options &Opts,
180 const clang::LangOptions &LangOpts, std::string &FileURIStorage) {
Haojian Wudc02a3d2018-02-13 09:53:50 +0000181 SourceLocation SpellingLoc = SM.getSpellingLoc(D.getLocation());
182 if (D.getLocation().isMacroID()) {
183 std::string PrintLoc = SpellingLoc.printToString(SM);
Haojian Wu3b8e00c2018-02-06 09:50:35 +0000184 if (llvm::StringRef(PrintLoc).startswith("<scratch") ||
185 llvm::StringRef(PrintLoc).startswith("<command line>")) {
Eric Liu7f247652018-02-06 16:10:35 +0000186 // We use the expansion location for the following symbols, as spelling
187 // locations of these symbols are not interesting to us:
188 // * symbols formed via macro concatenation, the spelling location will
189 // be "<scratch space>"
190 // * symbols controlled and defined by a compile command-line option
191 // `-DName=foo`, the spelling location will be "<command line>".
Haojian Wudc02a3d2018-02-13 09:53:50 +0000192 SpellingLoc = SM.getExpansionRange(D.getLocation()).first;
Haojian Wub0189062018-01-31 12:56:51 +0000193 }
194 }
195
Haojian Wudc02a3d2018-02-13 09:53:50 +0000196 auto U = toURI(SM, SM.getFilename(SpellingLoc), Opts);
Eric Liu7f247652018-02-06 16:10:35 +0000197 if (!U)
198 return llvm::None;
199 FileURIStorage = std::move(*U);
Sam McCall60039512018-02-09 14:42:01 +0000200 SymbolLocation Result;
201 Result.FileURI = FileURIStorage;
Haojian Wudc02a3d2018-02-13 09:53:50 +0000202 Result.StartOffset = SM.getFileOffset(SpellingLoc);
203 Result.EndOffset = Result.StartOffset + clang::Lexer::MeasureTokenLength(
204 SpellingLoc, SM, LangOpts);
Sam McCall60039512018-02-09 14:42:01 +0000205 return std::move(Result);
Haojian Wub0189062018-01-31 12:56:51 +0000206}
207
Eric Liucf8601b2018-02-28 09:33:15 +0000208// Checks whether \p ND is a definition of a TagDecl (class/struct/enum/union)
209// in a header file, in which case clangd would prefer to use ND as a canonical
210// declaration.
211// FIXME: handle symbol types that are not TagDecl (e.g. functions), if using
212// the the first seen declaration as canonical declaration is not a good enough
213// heuristic.
214bool isPreferredDeclaration(const NamedDecl &ND, index::SymbolRoleSet Roles) {
215 using namespace clang::ast_matchers;
216 return (Roles & static_cast<unsigned>(index::SymbolRole::Definition)) &&
217 llvm::isa<TagDecl>(&ND) &&
218 match(decl(isExpansionInMainFile()), ND, ND.getASTContext()).empty();
219}
220
Haojian Wu4c1394d2017-12-12 15:42:10 +0000221} // namespace
222
Eric Liu9af958f2018-01-10 14:57:58 +0000223SymbolCollector::SymbolCollector(Options Opts) : Opts(std::move(Opts)) {}
224
Eric Liu76f6b442018-01-09 17:32:00 +0000225void SymbolCollector::initialize(ASTContext &Ctx) {
226 ASTCtx = &Ctx;
227 CompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>();
228 CompletionTUInfo =
229 llvm::make_unique<CodeCompletionTUInfo>(CompletionAllocator);
230}
231
Haojian Wu4c1394d2017-12-12 15:42:10 +0000232// Always return true to continue indexing.
233bool SymbolCollector::handleDeclOccurence(
234 const Decl *D, index::SymbolRoleSet Roles,
235 ArrayRef<index::SymbolRelation> Relations, FileID FID, unsigned Offset,
236 index::IndexDataConsumer::ASTNodeInfo ASTNode) {
Eric Liu9af958f2018-01-10 14:57:58 +0000237 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
238
Haojian Wu4c1394d2017-12-12 15:42:10 +0000239 // FIXME: collect all symbol references.
240 if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) ||
241 Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
242 return true;
243
Eric Liu76f6b442018-01-09 17:32:00 +0000244 assert(CompletionAllocator && CompletionTUInfo);
245
Haojian Wu4c1394d2017-12-12 15:42:10 +0000246 if (const NamedDecl *ND = llvm::dyn_cast<NamedDecl>(D)) {
Eric Liu9af958f2018-01-10 14:57:58 +0000247 if (shouldFilterDecl(ND, ASTCtx, Opts))
Haojian Wu4c1394d2017-12-12 15:42:10 +0000248 return true;
Benjamin Kramer50a967d2017-12-28 14:47:01 +0000249 llvm::SmallString<128> USR;
250 if (index::generateUSRForDecl(ND, USR))
Haojian Wu4c1394d2017-12-12 15:42:10 +0000251 return true;
252
Eric Liucf8601b2018-02-28 09:33:15 +0000253 const NamedDecl &OriginalDecl = *cast<NamedDecl>(ASTNode.OrigD);
Haojian Wu4c1394d2017-12-12 15:42:10 +0000254 auto ID = SymbolID(USR);
Eric Liucf8601b2018-02-28 09:33:15 +0000255 const Symbol *BasicSymbol = Symbols.find(ID);
Sam McCall60039512018-02-09 14:42:01 +0000256 if (!BasicSymbol) // Regardless of role, ND is the canonical declaration.
257 BasicSymbol = addDeclaration(*ND, std::move(ID));
Eric Liucf8601b2018-02-28 09:33:15 +0000258 else if (isPreferredDeclaration(OriginalDecl, Roles))
259 // If OriginalDecl is preferred, replace the existing canonical
260 // declaration (e.g. a class forward declaration). There should be at most
261 // one duplicate as we expect to see only one preferred declaration per
262 // TU, because in practice they are definitions.
263 BasicSymbol = addDeclaration(OriginalDecl, std::move(ID));
264
Sam McCall60039512018-02-09 14:42:01 +0000265 if (Roles & static_cast<unsigned>(index::SymbolRole::Definition))
Eric Liucf8601b2018-02-28 09:33:15 +0000266 addDefinition(OriginalDecl, *BasicSymbol);
Haojian Wu4c1394d2017-12-12 15:42:10 +0000267 }
Haojian Wu4c1394d2017-12-12 15:42:10 +0000268 return true;
269}
270
Sam McCall60039512018-02-09 14:42:01 +0000271const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND,
272 SymbolID ID) {
273 auto &SM = ND.getASTContext().getSourceManager();
274
275 std::string QName;
276 llvm::raw_string_ostream OS(QName);
277 PrintingPolicy Policy(ASTCtx->getLangOpts());
278 // Note that inline namespaces are treated as transparent scopes. This
279 // reflects the way they're most commonly used for lookup. Ideally we'd
280 // include them, but at query time it's hard to find all the inline
281 // namespaces to query: the preamble doesn't have a dedicated list.
282 Policy.SuppressUnwrittenScope = true;
283 ND.printQualifiedName(OS, Policy);
284 OS.flush();
285
286 Symbol S;
287 S.ID = std::move(ID);
288 std::tie(S.Scope, S.Name) = splitQualifiedName(QName);
289 S.SymInfo = index::getSymbolInfo(&ND);
290 std::string FileURI;
Haojian Wudc02a3d2018-02-13 09:53:50 +0000291 if (auto DeclLoc =
292 getSymbolLocation(ND, SM, Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000293 S.CanonicalDeclaration = *DeclLoc;
294
295 // Add completion info.
296 // FIXME: we may want to choose a different redecl, or combine from several.
297 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
298 CodeCompletionResult SymbolCompletion(&ND, 0);
299 const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
300 *ASTCtx, *PP, CodeCompletionContext::CCC_Name, *CompletionAllocator,
301 *CompletionTUInfo,
302 /*IncludeBriefComments*/ true);
303 std::string Label;
304 std::string SnippetInsertText;
305 std::string IgnoredLabel;
306 std::string PlainInsertText;
307 getLabelAndInsertText(*CCS, &Label, &SnippetInsertText,
308 /*EnableSnippets=*/true);
309 getLabelAndInsertText(*CCS, &IgnoredLabel, &PlainInsertText,
310 /*EnableSnippets=*/false);
311 std::string FilterText = getFilterText(*CCS);
312 std::string Documentation = getDocumentation(*CCS);
313 std::string CompletionDetail = getDetail(*CCS);
314
Eric Liuc5105f92018-02-16 14:15:55 +0000315 std::string Include;
316 if (Opts.CollectIncludePath && shouldCollectIncludePath(S.SymInfo.Kind)) {
317 // Use the expansion location to get the #include header since this is
318 // where the symbol is exposed.
Eric Liub96363d2018-03-01 18:06:40 +0000319 if (auto Header = getIncludeHeader(
320 QName, SM, SM.getExpansionLoc(ND.getLocation()), Opts))
Eric Liuc5105f92018-02-16 14:15:55 +0000321 Include = std::move(*Header);
322 }
Sam McCall60039512018-02-09 14:42:01 +0000323 S.CompletionFilterText = FilterText;
324 S.CompletionLabel = Label;
325 S.CompletionPlainInsertText = PlainInsertText;
326 S.CompletionSnippetInsertText = SnippetInsertText;
327 Symbol::Details Detail;
328 Detail.Documentation = Documentation;
329 Detail.CompletionDetail = CompletionDetail;
Eric Liuc5105f92018-02-16 14:15:55 +0000330 Detail.IncludeHeader = Include;
Sam McCall60039512018-02-09 14:42:01 +0000331 S.Detail = &Detail;
332
333 Symbols.insert(S);
334 return Symbols.find(S.ID);
335}
336
337void SymbolCollector::addDefinition(const NamedDecl &ND,
338 const Symbol &DeclSym) {
339 if (DeclSym.Definition)
340 return;
341 // If we saw some forward declaration, we end up copying the symbol.
342 // This is not ideal, but avoids duplicating the "is this a definition" check
343 // in clang::index. We should only see one definition.
344 Symbol S = DeclSym;
345 std::string FileURI;
346 if (auto DefLoc = getSymbolLocation(ND, ND.getASTContext().getSourceManager(),
Haojian Wudc02a3d2018-02-13 09:53:50 +0000347 Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000348 S.Definition = *DefLoc;
349 Symbols.insert(S);
350}
351
Haojian Wu4c1394d2017-12-12 15:42:10 +0000352} // namespace clangd
353} // namespace clang