/
SymbolCollector.cpp
238 lines (211 loc) · 9.19 KB
/
SymbolCollector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//===--- SymbolCollector.cpp -------------------------------------*- C++-*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "SymbolCollector.h"
#include "../CodeCompletionStrings.h"
#include "Logger.h"
#include "clang/AST/DeclCXX.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Index/IndexSymbol.h"
#include "clang/Index/USRGeneration.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
namespace clang {
namespace clangd {
namespace {
// Make the Path absolute using the current working directory of the given
// SourceManager if the Path is not an absolute path. If failed, this combine
// relative paths with \p FallbackDir to get an absolute path.
//
// The Path can be a path relative to the build directory, or retrieved from
// the SourceManager.
std::string makeAbsolutePath(const SourceManager &SM, StringRef Path,
StringRef FallbackDir) {
llvm::SmallString<128> AbsolutePath(Path);
if (std::error_code EC =
SM.getFileManager().getVirtualFileSystem()->makeAbsolute(
AbsolutePath))
llvm::errs() << "Warning: could not make absolute file: '" << EC.message()
<< '\n';
if (llvm::sys::path::is_absolute(AbsolutePath)) {
// Handle the symbolic link path case where the current working directory
// (getCurrentWorkingDirectory) is a symlink./ We always want to the real
// file path (instead of the symlink path) for the C++ symbols.
//
// Consider the following example:
//
// src dir: /project/src/foo.h
// current working directory (symlink): /tmp/build -> /project/src/
//
// The file path of Symbol is "/project/src/foo.h" instead of
// "/tmp/build/foo.h"
if (const DirectoryEntry *Dir = SM.getFileManager().getDirectory(
llvm::sys::path::parent_path(AbsolutePath.str()))) {
StringRef DirName = SM.getFileManager().getCanonicalName(Dir);
SmallString<128> AbsoluteFilename;
llvm::sys::path::append(AbsoluteFilename, DirName,
llvm::sys::path::filename(AbsolutePath.str()));
AbsolutePath = AbsoluteFilename;
}
} else if (!FallbackDir.empty()) {
llvm::sys::fs::make_absolute(FallbackDir, AbsolutePath);
llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/true);
}
return AbsolutePath.str();
}
// "a::b::c", return {"a::b::", "c"}. Scope is empty if there's no qualifier.
std::pair<llvm::StringRef, llvm::StringRef>
splitQualifiedName(llvm::StringRef QName) {
assert(!QName.startswith("::") && "Qualified names should not start with ::");
size_t Pos = QName.rfind("::");
if (Pos == llvm::StringRef::npos)
return {StringRef(), QName};
return {QName.substr(0, Pos + 2), QName.substr(Pos + 2)};
}
bool shouldFilterDecl(const NamedDecl *ND, ASTContext *ASTCtx,
const SymbolCollector::Options &Opts) {
using namespace clang::ast_matchers;
if (ND->isImplicit())
return true;
// Skip anonymous declarations, e.g (anonymous enum/class/struct).
if (ND->getDeclName().isEmpty())
return true;
// FIXME: figure out a way to handle internal linkage symbols (e.g. static
// variables, function) defined in the .cc files. Also we skip the symbols
// in anonymous namespace as the qualifier names of these symbols are like
// `foo::<anonymous>::bar`, which need a special handling.
// In real world projects, we have a relatively large set of header files
// that define static variables (like "static const int A = 1;"), we still
// want to collect these symbols, although they cause potential ODR
// violations.
if (ND->isInAnonymousNamespace())
return true;
// We only want:
// * symbols in namespaces or translation unit scopes (e.g. no class
// members)
// * enum constants in unscoped enum decl (e.g. "red" in "enum {red};")
auto InTopLevelScope = hasDeclContext(
anyOf(namespaceDecl(), translationUnitDecl(), linkageSpecDecl()));
if (match(decl(allOf(Opts.IndexMainFiles
? decl()
: decl(unless(isExpansionInMainFile())),
anyOf(InTopLevelScope,
hasDeclContext(enumDecl(InTopLevelScope,
unless(isScoped())))))),
*ND, *ASTCtx)
.empty())
return true;
return false;
}
// Return the symbol location of the given declaration `D`.
//
// For symbols defined inside macros:
// * use expansion location, if the symbol is formed via macro concatenation.
// * use spelling location, otherwise.
SymbolLocation GetSymbolLocation(const NamedDecl *D, SourceManager &SM,
StringRef FallbackDir,
std::string &FilePathStorage) {
SymbolLocation Location;
SourceLocation Loc = SM.getSpellingLoc(D->getLocation());
if (D->getLocation().isMacroID()) {
// The symbol is formed via macro concatenation, the spelling location will
// be "<scratch space>", which is not interesting to us, use the expansion
// location instead.
if (llvm::StringRef(Loc.printToString(SM)).startswith("<scratch")) {
FilePathStorage = makeAbsolutePath(
SM, SM.getFilename(SM.getExpansionLoc(D->getLocation())),
FallbackDir);
return {FilePathStorage,
SM.getFileOffset(SM.getExpansionRange(D->getLocStart()).first),
SM.getFileOffset(SM.getExpansionRange(D->getLocEnd()).second)};
}
}
FilePathStorage = makeAbsolutePath(SM, SM.getFilename(Loc), FallbackDir);
return {FilePathStorage,
SM.getFileOffset(SM.getSpellingLoc(D->getLocStart())),
SM.getFileOffset(SM.getSpellingLoc(D->getLocEnd()))};
}
} // namespace
SymbolCollector::SymbolCollector(Options Opts) : Opts(std::move(Opts)) {}
void SymbolCollector::initialize(ASTContext &Ctx) {
ASTCtx = &Ctx;
CompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>();
CompletionTUInfo =
llvm::make_unique<CodeCompletionTUInfo>(CompletionAllocator);
}
// Always return true to continue indexing.
bool SymbolCollector::handleDeclOccurence(
const Decl *D, index::SymbolRoleSet Roles,
ArrayRef<index::SymbolRelation> Relations, FileID FID, unsigned Offset,
index::IndexDataConsumer::ASTNodeInfo ASTNode) {
assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
// FIXME: collect all symbol references.
if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) ||
Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
return true;
assert(CompletionAllocator && CompletionTUInfo);
if (const NamedDecl *ND = llvm::dyn_cast<NamedDecl>(D)) {
if (shouldFilterDecl(ND, ASTCtx, Opts))
return true;
llvm::SmallString<128> USR;
if (index::generateUSRForDecl(ND, USR))
return true;
auto ID = SymbolID(USR);
if (Symbols.find(ID) != nullptr)
return true;
auto &SM = ND->getASTContext().getSourceManager();
std::string QName;
llvm::raw_string_ostream OS(QName);
PrintingPolicy Policy(ASTCtx->getLangOpts());
// Note that inline namespaces are treated as transparent scopes. This
// reflects the way they're most commonly used for lookup. Ideally we'd
// include them, but at query time it's hard to find all the inline
// namespaces to query: the preamble doesn't have a dedicated list.
Policy.SuppressUnwrittenScope = true;
ND->printQualifiedName(OS, Policy);
OS.flush();
Symbol S;
S.ID = std::move(ID);
std::tie(S.Scope, S.Name) = splitQualifiedName(QName);
S.SymInfo = index::getSymbolInfo(D);
std::string FilePath;
S.CanonicalDeclaration = GetSymbolLocation(ND, SM, Opts.FallbackDir, FilePath);
// Add completion info.
assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
CodeCompletionResult SymbolCompletion(ND, 0);
const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
*ASTCtx, *PP, CodeCompletionContext::CCC_Name, *CompletionAllocator,
*CompletionTUInfo,
/*IncludeBriefComments*/ true);
std::string Label;
std::string SnippetInsertText;
std::string IgnoredLabel;
std::string PlainInsertText;
getLabelAndInsertText(*CCS, &Label, &SnippetInsertText,
/*EnableSnippets=*/true);
getLabelAndInsertText(*CCS, &IgnoredLabel, &PlainInsertText,
/*EnableSnippets=*/false);
std::string FilterText = getFilterText(*CCS);
std::string Documentation = getDocumentation(*CCS);
std::string CompletionDetail = getDetail(*CCS);
S.CompletionFilterText = FilterText;
S.CompletionLabel = Label;
S.CompletionPlainInsertText = PlainInsertText;
S.CompletionSnippetInsertText = SnippetInsertText;
Symbol::Details Detail;
Detail.Documentation = Documentation;
Detail.CompletionDetail = CompletionDetail;
S.Detail = &Detail;
Symbols.insert(S);
}
return true;
}
} // namespace clangd
} // namespace clang