blob: 0e2186de3e01661b70182562ebf9524c8d99642a [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"
Haojian Wu4c1394d2017-12-12 15:42:10 +000014#include "clang/AST/DeclCXX.h"
Eric Liu9af958f2018-01-10 14:57:58 +000015#include "clang/ASTMatchers/ASTMatchFinder.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000016#include "clang/Basic/SourceManager.h"
17#include "clang/Index/IndexSymbol.h"
18#include "clang/Index/USRGeneration.h"
Eric Liu278e2d12018-01-29 15:13:29 +000019#include "llvm/Support/FileSystem.h"
Haojian Wu4c1394d2017-12-12 15:42:10 +000020#include "llvm/Support/MemoryBuffer.h"
21#include "llvm/Support/Path.h"
22
23namespace clang {
24namespace clangd {
25
26namespace {
Eric Liu7f247652018-02-06 16:10:35 +000027// Returns a URI of \p Path. Firstly, this makes the \p Path absolute using the
28// current working directory of the given SourceManager if the Path is not an
29// absolute path. If failed, this resolves relative paths against \p FallbackDir
30// to get an absolute path. Then, this tries creating an URI for the absolute
31// path with schemes specified in \p Opts. This returns an URI with the first
32// working scheme, if there is any; otherwise, this returns None.
Haojian Wu4c1394d2017-12-12 15:42:10 +000033//
34// The Path can be a path relative to the build directory, or retrieved from
35// the SourceManager.
Eric Liu7f247652018-02-06 16:10:35 +000036llvm::Optional<std::string> toURI(const SourceManager &SM, StringRef Path,
37 const SymbolCollector::Options &Opts) {
Haojian Wu4c1394d2017-12-12 15:42:10 +000038 llvm::SmallString<128> AbsolutePath(Path);
39 if (std::error_code EC =
40 SM.getFileManager().getVirtualFileSystem()->makeAbsolute(
41 AbsolutePath))
42 llvm::errs() << "Warning: could not make absolute file: '" << EC.message()
43 << '\n';
Eric Liu278e2d12018-01-29 15:13:29 +000044 if (llvm::sys::path::is_absolute(AbsolutePath)) {
45 // Handle the symbolic link path case where the current working directory
46 // (getCurrentWorkingDirectory) is a symlink./ We always want to the real
47 // file path (instead of the symlink path) for the C++ symbols.
48 //
49 // Consider the following example:
50 //
51 // src dir: /project/src/foo.h
52 // current working directory (symlink): /tmp/build -> /project/src/
53 //
54 // The file path of Symbol is "/project/src/foo.h" instead of
55 // "/tmp/build/foo.h"
56 if (const DirectoryEntry *Dir = SM.getFileManager().getDirectory(
57 llvm::sys::path::parent_path(AbsolutePath.str()))) {
58 StringRef DirName = SM.getFileManager().getCanonicalName(Dir);
59 SmallString<128> AbsoluteFilename;
60 llvm::sys::path::append(AbsoluteFilename, DirName,
61 llvm::sys::path::filename(AbsolutePath.str()));
62 AbsolutePath = AbsoluteFilename;
63 }
Eric Liu7f247652018-02-06 16:10:35 +000064 } else if (!Opts.FallbackDir.empty()) {
65 llvm::sys::fs::make_absolute(Opts.FallbackDir, AbsolutePath);
Eric Liu278e2d12018-01-29 15:13:29 +000066 llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/true);
Haojian Wu4c1394d2017-12-12 15:42:10 +000067 }
Eric Liu7f247652018-02-06 16:10:35 +000068
69 std::string ErrMsg;
70 for (const auto &Scheme : Opts.URISchemes) {
71 auto U = URI::create(AbsolutePath, Scheme);
72 if (U)
73 return U->toString();
74 ErrMsg += llvm::toString(U.takeError()) + "\n";
75 }
76 log(llvm::Twine("Failed to create an URI for file ") + AbsolutePath + ": " +
77 ErrMsg);
78 return llvm::None;
Haojian Wu4c1394d2017-12-12 15:42:10 +000079}
Eric Liu4feda802017-12-19 11:37:40 +000080
Sam McCall8b2faee2018-01-19 22:18:21 +000081// "a::b::c", return {"a::b::", "c"}. Scope is empty if there's no qualifier.
Eric Liu4feda802017-12-19 11:37:40 +000082std::pair<llvm::StringRef, llvm::StringRef>
83splitQualifiedName(llvm::StringRef QName) {
84 assert(!QName.startswith("::") && "Qualified names should not start with ::");
85 size_t Pos = QName.rfind("::");
86 if (Pos == llvm::StringRef::npos)
87 return {StringRef(), QName};
Sam McCall8b2faee2018-01-19 22:18:21 +000088 return {QName.substr(0, Pos + 2), QName.substr(Pos + 2)};
Eric Liu4feda802017-12-19 11:37:40 +000089}
90
Eric Liu9af958f2018-01-10 14:57:58 +000091bool shouldFilterDecl(const NamedDecl *ND, ASTContext *ASTCtx,
92 const SymbolCollector::Options &Opts) {
93 using namespace clang::ast_matchers;
94 if (ND->isImplicit())
95 return true;
Haojian Wu9873fdd2018-01-19 09:35:55 +000096 // Skip anonymous declarations, e.g (anonymous enum/class/struct).
97 if (ND->getDeclName().isEmpty())
98 return true;
99
Eric Liu9af958f2018-01-10 14:57:58 +0000100 // FIXME: figure out a way to handle internal linkage symbols (e.g. static
101 // variables, function) defined in the .cc files. Also we skip the symbols
102 // in anonymous namespace as the qualifier names of these symbols are like
103 // `foo::<anonymous>::bar`, which need a special handling.
104 // In real world projects, we have a relatively large set of header files
105 // that define static variables (like "static const int A = 1;"), we still
106 // want to collect these symbols, although they cause potential ODR
107 // violations.
108 if (ND->isInAnonymousNamespace())
109 return true;
110
Haojian Wu9873fdd2018-01-19 09:35:55 +0000111 // We only want:
112 // * symbols in namespaces or translation unit scopes (e.g. no class
113 // members)
114 // * enum constants in unscoped enum decl (e.g. "red" in "enum {red};")
Eric Liucf177382018-02-02 10:31:42 +0000115 auto InTopLevelScope = hasDeclContext(
116 anyOf(namespaceDecl(), translationUnitDecl(), linkageSpecDecl()));
Haojian Wu9873fdd2018-01-19 09:35:55 +0000117 if (match(decl(allOf(Opts.IndexMainFiles
118 ? decl()
119 : decl(unless(isExpansionInMainFile())),
120 anyOf(InTopLevelScope,
121 hasDeclContext(enumDecl(InTopLevelScope,
122 unless(isScoped())))))),
Eric Liu9af958f2018-01-10 14:57:58 +0000123 *ND, *ASTCtx)
124 .empty())
125 return true;
126
127 return false;
128}
129
Haojian Wub0189062018-01-31 12:56:51 +0000130// Return the symbol location of the given declaration `D`.
131//
132// For symbols defined inside macros:
133// * use expansion location, if the symbol is formed via macro concatenation.
134// * use spelling location, otherwise.
Eric Liu7f247652018-02-06 16:10:35 +0000135llvm::Optional<SymbolLocation>
Sam McCall60039512018-02-09 14:42:01 +0000136getSymbolLocation(const NamedDecl &D, SourceManager &SM,
Eric Liu7f247652018-02-06 16:10:35 +0000137 const SymbolCollector::Options &Opts,
Haojian Wudc02a3d2018-02-13 09:53:50 +0000138 const clang::LangOptions& LangOpts,
Eric Liu7f247652018-02-06 16:10:35 +0000139 std::string &FileURIStorage) {
Haojian Wudc02a3d2018-02-13 09:53:50 +0000140 SourceLocation SpellingLoc = SM.getSpellingLoc(D.getLocation());
141 if (D.getLocation().isMacroID()) {
142 std::string PrintLoc = SpellingLoc.printToString(SM);
Haojian Wu3b8e00c2018-02-06 09:50:35 +0000143 if (llvm::StringRef(PrintLoc).startswith("<scratch") ||
144 llvm::StringRef(PrintLoc).startswith("<command line>")) {
Eric Liu7f247652018-02-06 16:10:35 +0000145 // We use the expansion location for the following symbols, as spelling
146 // locations of these symbols are not interesting to us:
147 // * symbols formed via macro concatenation, the spelling location will
148 // be "<scratch space>"
149 // * symbols controlled and defined by a compile command-line option
150 // `-DName=foo`, the spelling location will be "<command line>".
Haojian Wudc02a3d2018-02-13 09:53:50 +0000151 SpellingLoc = SM.getExpansionRange(D.getLocation()).first;
Haojian Wub0189062018-01-31 12:56:51 +0000152 }
153 }
154
Haojian Wudc02a3d2018-02-13 09:53:50 +0000155 auto U = toURI(SM, SM.getFilename(SpellingLoc), Opts);
Eric Liu7f247652018-02-06 16:10:35 +0000156 if (!U)
157 return llvm::None;
158 FileURIStorage = std::move(*U);
Sam McCall60039512018-02-09 14:42:01 +0000159 SymbolLocation Result;
160 Result.FileURI = FileURIStorage;
Haojian Wudc02a3d2018-02-13 09:53:50 +0000161 Result.StartOffset = SM.getFileOffset(SpellingLoc);
162 Result.EndOffset = Result.StartOffset + clang::Lexer::MeasureTokenLength(
163 SpellingLoc, SM, LangOpts);
Sam McCall60039512018-02-09 14:42:01 +0000164 return std::move(Result);
Haojian Wub0189062018-01-31 12:56:51 +0000165}
166
Haojian Wu4c1394d2017-12-12 15:42:10 +0000167} // namespace
168
Eric Liu9af958f2018-01-10 14:57:58 +0000169SymbolCollector::SymbolCollector(Options Opts) : Opts(std::move(Opts)) {}
170
Eric Liu76f6b442018-01-09 17:32:00 +0000171void SymbolCollector::initialize(ASTContext &Ctx) {
172 ASTCtx = &Ctx;
173 CompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>();
174 CompletionTUInfo =
175 llvm::make_unique<CodeCompletionTUInfo>(CompletionAllocator);
176}
177
Haojian Wu4c1394d2017-12-12 15:42:10 +0000178// Always return true to continue indexing.
179bool SymbolCollector::handleDeclOccurence(
180 const Decl *D, index::SymbolRoleSet Roles,
181 ArrayRef<index::SymbolRelation> Relations, FileID FID, unsigned Offset,
182 index::IndexDataConsumer::ASTNodeInfo ASTNode) {
Eric Liu9af958f2018-01-10 14:57:58 +0000183 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
184
Haojian Wu4c1394d2017-12-12 15:42:10 +0000185 // FIXME: collect all symbol references.
186 if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) ||
187 Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
188 return true;
189
Eric Liu76f6b442018-01-09 17:32:00 +0000190 assert(CompletionAllocator && CompletionTUInfo);
191
Haojian Wu4c1394d2017-12-12 15:42:10 +0000192 if (const NamedDecl *ND = llvm::dyn_cast<NamedDecl>(D)) {
Eric Liu9af958f2018-01-10 14:57:58 +0000193 if (shouldFilterDecl(ND, ASTCtx, Opts))
Haojian Wu4c1394d2017-12-12 15:42:10 +0000194 return true;
Benjamin Kramer50a967d2017-12-28 14:47:01 +0000195 llvm::SmallString<128> USR;
196 if (index::generateUSRForDecl(ND, USR))
Haojian Wu4c1394d2017-12-12 15:42:10 +0000197 return true;
198
Haojian Wu4c1394d2017-12-12 15:42:10 +0000199 auto ID = SymbolID(USR);
Sam McCall60039512018-02-09 14:42:01 +0000200 const Symbol* BasicSymbol = Symbols.find(ID);
201 if (!BasicSymbol) // Regardless of role, ND is the canonical declaration.
202 BasicSymbol = addDeclaration(*ND, std::move(ID));
203 if (Roles & static_cast<unsigned>(index::SymbolRole::Definition))
204 addDefinition(*cast<NamedDecl>(ASTNode.OrigD), *BasicSymbol);
Haojian Wu4c1394d2017-12-12 15:42:10 +0000205 }
Haojian Wu4c1394d2017-12-12 15:42:10 +0000206 return true;
207}
208
Sam McCall60039512018-02-09 14:42:01 +0000209const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND,
210 SymbolID ID) {
211 auto &SM = ND.getASTContext().getSourceManager();
212
213 std::string QName;
214 llvm::raw_string_ostream OS(QName);
215 PrintingPolicy Policy(ASTCtx->getLangOpts());
216 // Note that inline namespaces are treated as transparent scopes. This
217 // reflects the way they're most commonly used for lookup. Ideally we'd
218 // include them, but at query time it's hard to find all the inline
219 // namespaces to query: the preamble doesn't have a dedicated list.
220 Policy.SuppressUnwrittenScope = true;
221 ND.printQualifiedName(OS, Policy);
222 OS.flush();
223
224 Symbol S;
225 S.ID = std::move(ID);
226 std::tie(S.Scope, S.Name) = splitQualifiedName(QName);
227 S.SymInfo = index::getSymbolInfo(&ND);
228 std::string FileURI;
229 // FIXME: we may want a different "canonical" heuristic than clang chooses.
230 // Clang seems to choose the first, which may not have the most information.
Haojian Wudc02a3d2018-02-13 09:53:50 +0000231 if (auto DeclLoc =
232 getSymbolLocation(ND, SM, Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000233 S.CanonicalDeclaration = *DeclLoc;
234
235 // Add completion info.
236 // FIXME: we may want to choose a different redecl, or combine from several.
237 assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
238 CodeCompletionResult SymbolCompletion(&ND, 0);
239 const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
240 *ASTCtx, *PP, CodeCompletionContext::CCC_Name, *CompletionAllocator,
241 *CompletionTUInfo,
242 /*IncludeBriefComments*/ true);
243 std::string Label;
244 std::string SnippetInsertText;
245 std::string IgnoredLabel;
246 std::string PlainInsertText;
247 getLabelAndInsertText(*CCS, &Label, &SnippetInsertText,
248 /*EnableSnippets=*/true);
249 getLabelAndInsertText(*CCS, &IgnoredLabel, &PlainInsertText,
250 /*EnableSnippets=*/false);
251 std::string FilterText = getFilterText(*CCS);
252 std::string Documentation = getDocumentation(*CCS);
253 std::string CompletionDetail = getDetail(*CCS);
254
255 S.CompletionFilterText = FilterText;
256 S.CompletionLabel = Label;
257 S.CompletionPlainInsertText = PlainInsertText;
258 S.CompletionSnippetInsertText = SnippetInsertText;
259 Symbol::Details Detail;
260 Detail.Documentation = Documentation;
261 Detail.CompletionDetail = CompletionDetail;
262 S.Detail = &Detail;
263
264 Symbols.insert(S);
265 return Symbols.find(S.ID);
266}
267
268void SymbolCollector::addDefinition(const NamedDecl &ND,
269 const Symbol &DeclSym) {
270 if (DeclSym.Definition)
271 return;
272 // If we saw some forward declaration, we end up copying the symbol.
273 // This is not ideal, but avoids duplicating the "is this a definition" check
274 // in clang::index. We should only see one definition.
275 Symbol S = DeclSym;
276 std::string FileURI;
277 if (auto DefLoc = getSymbolLocation(ND, ND.getASTContext().getSourceManager(),
Haojian Wudc02a3d2018-02-13 09:53:50 +0000278 Opts, ASTCtx->getLangOpts(), FileURI))
Sam McCall60039512018-02-09 14:42:01 +0000279 S.Definition = *DefLoc;
280 Symbols.insert(S);
281}
282
Haojian Wu4c1394d2017-12-12 15:42:10 +0000283} // namespace clangd
284} // namespace clang