blob: 647e02da72884de7582f0ab86d5979a8e23c1540 [file] [log] [blame]
Sam McCall98775c52017-12-04 13:49:59 +00001//===--- CodeComplete.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//
Sam McCallc18c2802018-06-15 11:06:29 +000010// Code completion has several moving parts:
11// - AST-based completions are provided using the completion hooks in Sema.
12// - external completions are retrieved from the index (using hints from Sema)
13// - the two sources overlap, and must be merged and overloads bundled
14// - results must be scored and ranked (see Quality.h) before rendering
Sam McCall98775c52017-12-04 13:49:59 +000015//
Sam McCallc18c2802018-06-15 11:06:29 +000016// Signature help works in a similar way as code completion, but it is simpler:
17// it's purely AST-based, and there are few candidates.
Sam McCall98775c52017-12-04 13:49:59 +000018//
19//===---------------------------------------------------------------------===//
20
21#include "CodeComplete.h"
Eric Liu7ad16962018-06-22 10:46:59 +000022#include "AST.h"
Eric Liu63696e12017-12-20 17:24:31 +000023#include "CodeCompletionStrings.h"
Sam McCall98775c52017-12-04 13:49:59 +000024#include "Compiler.h"
Sam McCall84652cc2018-01-12 16:16:09 +000025#include "FuzzyMatch.h"
Eric Liu63f419a2018-05-15 15:29:32 +000026#include "Headers.h"
Eric Liu6f648df2017-12-19 16:50:37 +000027#include "Logger.h"
Sam McCallc5707b62018-05-15 17:43:27 +000028#include "Quality.h"
Eric Liuc5105f92018-02-16 14:15:55 +000029#include "SourceCode.h"
Sam McCall2b780162018-01-30 17:20:54 +000030#include "Trace.h"
Eric Liu63f419a2018-05-15 15:29:32 +000031#include "URI.h"
Eric Liu6f648df2017-12-19 16:50:37 +000032#include "index/Index.h"
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +000033#include "clang/ASTMatchers/ASTMatchFinder.h"
Ilya Biryukovc22d3442018-05-16 12:32:49 +000034#include "clang/Basic/LangOptions.h"
Eric Liuc5105f92018-02-16 14:15:55 +000035#include "clang/Format/Format.h"
Sam McCall98775c52017-12-04 13:49:59 +000036#include "clang/Frontend/CompilerInstance.h"
37#include "clang/Frontend/FrontendActions.h"
Sam McCall545a20d2018-01-19 14:34:02 +000038#include "clang/Index/USRGeneration.h"
Sam McCall98775c52017-12-04 13:49:59 +000039#include "clang/Sema/CodeCompleteConsumer.h"
40#include "clang/Sema/Sema.h"
Eric Liuc5105f92018-02-16 14:15:55 +000041#include "clang/Tooling/Core/Replacement.h"
Haojian Wuba28e9a2018-01-10 14:44:34 +000042#include "llvm/Support/Format.h"
Sam McCall98775c52017-12-04 13:49:59 +000043#include <queue>
44
Sam McCallc5707b62018-05-15 17:43:27 +000045// We log detailed candidate here if you run with -debug-only=codecomplete.
46#define DEBUG_TYPE "codecomplete"
47
Sam McCall98775c52017-12-04 13:49:59 +000048namespace clang {
49namespace clangd {
50namespace {
51
Eric Liu6f648df2017-12-19 16:50:37 +000052CompletionItemKind toCompletionItemKind(index::SymbolKind Kind) {
53 using SK = index::SymbolKind;
54 switch (Kind) {
55 case SK::Unknown:
56 return CompletionItemKind::Missing;
57 case SK::Module:
58 case SK::Namespace:
59 case SK::NamespaceAlias:
60 return CompletionItemKind::Module;
61 case SK::Macro:
62 return CompletionItemKind::Text;
63 case SK::Enum:
64 return CompletionItemKind::Enum;
65 // FIXME(ioeric): use LSP struct instead of class when it is suppoted in the
66 // protocol.
67 case SK::Struct:
68 case SK::Class:
69 case SK::Protocol:
70 case SK::Extension:
71 case SK::Union:
72 return CompletionItemKind::Class;
73 // FIXME(ioeric): figure out whether reference is the right type for aliases.
74 case SK::TypeAlias:
75 case SK::Using:
76 return CompletionItemKind::Reference;
77 case SK::Function:
78 // FIXME(ioeric): this should probably be an operator. This should be fixed
79 // when `Operator` is support type in the protocol.
80 case SK::ConversionFunction:
81 return CompletionItemKind::Function;
82 case SK::Variable:
83 case SK::Parameter:
84 return CompletionItemKind::Variable;
85 case SK::Field:
86 return CompletionItemKind::Field;
87 // FIXME(ioeric): use LSP enum constant when it is supported in the protocol.
88 case SK::EnumConstant:
89 return CompletionItemKind::Value;
90 case SK::InstanceMethod:
91 case SK::ClassMethod:
92 case SK::StaticMethod:
93 case SK::Destructor:
94 return CompletionItemKind::Method;
95 case SK::InstanceProperty:
96 case SK::ClassProperty:
97 case SK::StaticProperty:
98 return CompletionItemKind::Property;
99 case SK::Constructor:
100 return CompletionItemKind::Constructor;
101 }
102 llvm_unreachable("Unhandled clang::index::SymbolKind.");
103}
104
Sam McCall83305892018-06-08 21:17:19 +0000105CompletionItemKind
106toCompletionItemKind(CodeCompletionResult::ResultKind ResKind,
107 const NamedDecl *Decl) {
108 if (Decl)
109 return toCompletionItemKind(index::getSymbolInfo(Decl).Kind);
110 switch (ResKind) {
111 case CodeCompletionResult::RK_Declaration:
112 llvm_unreachable("RK_Declaration without Decl");
113 case CodeCompletionResult::RK_Keyword:
114 return CompletionItemKind::Keyword;
115 case CodeCompletionResult::RK_Macro:
116 return CompletionItemKind::Text; // unfortunately, there's no 'Macro'
117 // completion items in LSP.
118 case CodeCompletionResult::RK_Pattern:
119 return CompletionItemKind::Snippet;
120 }
121 llvm_unreachable("Unhandled CodeCompletionResult::ResultKind.");
122}
123
Sam McCall98775c52017-12-04 13:49:59 +0000124/// Get the optional chunk as a string. This function is possibly recursive.
125///
126/// The parameter info for each parameter is appended to the Parameters.
127std::string
128getOptionalParameters(const CodeCompletionString &CCS,
129 std::vector<ParameterInformation> &Parameters) {
130 std::string Result;
131 for (const auto &Chunk : CCS) {
132 switch (Chunk.Kind) {
133 case CodeCompletionString::CK_Optional:
134 assert(Chunk.Optional &&
135 "Expected the optional code completion string to be non-null.");
136 Result += getOptionalParameters(*Chunk.Optional, Parameters);
137 break;
138 case CodeCompletionString::CK_VerticalSpace:
139 break;
140 case CodeCompletionString::CK_Placeholder:
141 // A string that acts as a placeholder for, e.g., a function call
142 // argument.
143 // Intentional fallthrough here.
144 case CodeCompletionString::CK_CurrentParameter: {
145 // A piece of text that describes the parameter that corresponds to
146 // the code-completion location within a function call, message send,
147 // macro invocation, etc.
148 Result += Chunk.Text;
149 ParameterInformation Info;
150 Info.label = Chunk.Text;
151 Parameters.push_back(std::move(Info));
152 break;
153 }
154 default:
155 Result += Chunk.Text;
156 break;
157 }
158 }
159 return Result;
160}
161
Eric Liu63f419a2018-05-15 15:29:32 +0000162/// Creates a `HeaderFile` from \p Header which can be either a URI or a literal
163/// include.
164static llvm::Expected<HeaderFile> toHeaderFile(StringRef Header,
165 llvm::StringRef HintPath) {
166 if (isLiteralInclude(Header))
167 return HeaderFile{Header.str(), /*Verbatim=*/true};
168 auto U = URI::parse(Header);
169 if (!U)
170 return U.takeError();
171
172 auto IncludePath = URI::includeSpelling(*U);
173 if (!IncludePath)
174 return IncludePath.takeError();
175 if (!IncludePath->empty())
176 return HeaderFile{std::move(*IncludePath), /*Verbatim=*/true};
177
178 auto Resolved = URI::resolve(*U, HintPath);
179 if (!Resolved)
180 return Resolved.takeError();
181 return HeaderFile{std::move(*Resolved), /*Verbatim=*/false};
182}
183
Sam McCall545a20d2018-01-19 14:34:02 +0000184/// A code completion result, in clang-native form.
Sam McCall98775c52017-12-04 13:49:59 +0000185/// It may be promoted to a CompletionItem if it's among the top-ranked results.
186struct CompletionCandidate {
Sam McCall545a20d2018-01-19 14:34:02 +0000187 llvm::StringRef Name; // Used for filtering and sorting.
188 // We may have a result from Sema, from the index, or both.
189 const CodeCompletionResult *SemaResult = nullptr;
190 const Symbol *IndexResult = nullptr;
Sam McCall98775c52017-12-04 13:49:59 +0000191
Sam McCallc18c2802018-06-15 11:06:29 +0000192 // Returns a token identifying the overload set this is part of.
193 // 0 indicates it's not part of any overload set.
194 size_t overloadSet() const {
195 SmallString<256> Scratch;
196 if (IndexResult) {
197 switch (IndexResult->SymInfo.Kind) {
198 case index::SymbolKind::ClassMethod:
199 case index::SymbolKind::InstanceMethod:
200 case index::SymbolKind::StaticMethod:
201 assert(false && "Don't expect members from index in code completion");
202 // fall through
203 case index::SymbolKind::Function:
204 // We can't group overloads together that need different #includes.
205 // This could break #include insertion.
206 return hash_combine(
207 (IndexResult->Scope + IndexResult->Name).toStringRef(Scratch),
208 headerToInsertIfNotPresent().getValueOr(""));
209 default:
210 return 0;
211 }
212 }
213 assert(SemaResult);
214 // We need to make sure we're consistent with the IndexResult case!
215 const NamedDecl *D = SemaResult->Declaration;
216 if (!D || !D->isFunctionOrFunctionTemplate())
217 return 0;
218 {
219 llvm::raw_svector_ostream OS(Scratch);
220 D->printQualifiedName(OS);
221 }
222 return hash_combine(Scratch, headerToInsertIfNotPresent().getValueOr(""));
223 }
224
225 llvm::Optional<llvm::StringRef> headerToInsertIfNotPresent() const {
226 if (!IndexResult || !IndexResult->Detail ||
227 IndexResult->Detail->IncludeHeader.empty())
228 return llvm::None;
229 if (SemaResult && SemaResult->Declaration) {
230 // Avoid inserting new #include if the declaration is found in the current
231 // file e.g. the symbol is forward declared.
232 auto &SM = SemaResult->Declaration->getASTContext().getSourceManager();
233 for (const Decl *RD : SemaResult->Declaration->redecls())
234 if (SM.isInMainFile(SM.getExpansionLoc(RD->getLocStart())))
235 return llvm::None;
236 }
237 return IndexResult->Detail->IncludeHeader;
238 }
239
Sam McCall545a20d2018-01-19 14:34:02 +0000240 // Builds an LSP completion item.
Eric Liu63f419a2018-05-15 15:29:32 +0000241 CompletionItem build(StringRef FileName, const CompletionItemScores &Scores,
Sam McCall545a20d2018-01-19 14:34:02 +0000242 const CodeCompleteOptions &Opts,
Eric Liu63f419a2018-05-15 15:29:32 +0000243 CodeCompletionString *SemaCCS,
Eric Liu8f3678d2018-06-15 13:34:18 +0000244 const IncludeInserter &Includes,
Ilya Biryukov43714502018-05-16 12:32:44 +0000245 llvm::StringRef SemaDocComment) const {
Sam McCall545a20d2018-01-19 14:34:02 +0000246 assert(bool(SemaResult) == bool(SemaCCS));
Eric Liu7ad16962018-06-22 10:46:59 +0000247 assert(SemaResult || IndexResult);
248
Sam McCall545a20d2018-01-19 14:34:02 +0000249 CompletionItem I;
Eric Liu8f3678d2018-06-15 13:34:18 +0000250 bool InsertingInclude = false; // Whether a new #include will be added.
Sam McCall545a20d2018-01-19 14:34:02 +0000251 if (SemaResult) {
Sam McCall83305892018-06-08 21:17:19 +0000252 I.kind = toCompletionItemKind(SemaResult->Kind, SemaResult->Declaration);
Sam McCall545a20d2018-01-19 14:34:02 +0000253 getLabelAndInsertText(*SemaCCS, &I.label, &I.insertText,
254 Opts.EnableSnippets);
Sam McCall032db942018-06-22 06:41:43 +0000255 if (const char* Text = SemaCCS->getTypedText())
256 I.filterText = Text;
Ilya Biryukov43714502018-05-16 12:32:44 +0000257 I.documentation = formatDocumentation(*SemaCCS, SemaDocComment);
Sam McCall545a20d2018-01-19 14:34:02 +0000258 I.detail = getDetail(*SemaCCS);
Eric Liu7ad16962018-06-22 10:46:59 +0000259 if (SemaResult->Kind == CodeCompletionResult::RK_Declaration)
260 if (const auto *D = SemaResult->getDeclaration())
261 if (const auto *ND = llvm::dyn_cast<NamedDecl>(D))
262 I.SymbolScope = splitQualifiedName(printQualifiedName(*ND)).first;
Sam McCall545a20d2018-01-19 14:34:02 +0000263 }
264 if (IndexResult) {
Eric Liu7ad16962018-06-22 10:46:59 +0000265 if (I.SymbolScope.empty())
266 I.SymbolScope = IndexResult->Scope;
Sam McCall545a20d2018-01-19 14:34:02 +0000267 if (I.kind == CompletionItemKind::Missing)
268 I.kind = toCompletionItemKind(IndexResult->SymInfo.Kind);
269 // FIXME: reintroduce a way to show the index source for debugging.
270 if (I.label.empty())
271 I.label = IndexResult->CompletionLabel;
272 if (I.filterText.empty())
273 I.filterText = IndexResult->Name;
Sam McCall98775c52017-12-04 13:49:59 +0000274
Sam McCall545a20d2018-01-19 14:34:02 +0000275 // FIXME(ioeric): support inserting/replacing scope qualifiers.
276 if (I.insertText.empty())
277 I.insertText = Opts.EnableSnippets
278 ? IndexResult->CompletionSnippetInsertText
279 : IndexResult->CompletionPlainInsertText;
280
281 if (auto *D = IndexResult->Detail) {
282 if (I.documentation.empty())
283 I.documentation = D->Documentation;
284 if (I.detail.empty())
285 I.detail = D->CompletionDetail;
Sam McCallc18c2802018-06-15 11:06:29 +0000286 if (auto Inserted = headerToInsertIfNotPresent()) {
Eric Liu8f3678d2018-06-15 13:34:18 +0000287 auto IncludePath = [&]() -> Expected<std::string> {
Eric Liu63f419a2018-05-15 15:29:32 +0000288 auto ResolvedDeclaring = toHeaderFile(
289 IndexResult->CanonicalDeclaration.FileURI, FileName);
290 if (!ResolvedDeclaring)
291 return ResolvedDeclaring.takeError();
Sam McCallc18c2802018-06-15 11:06:29 +0000292 auto ResolvedInserted = toHeaderFile(*Inserted, FileName);
Eric Liu63f419a2018-05-15 15:29:32 +0000293 if (!ResolvedInserted)
294 return ResolvedInserted.takeError();
Eric Liu8f3678d2018-06-15 13:34:18 +0000295 if (!Includes.shouldInsertInclude(*ResolvedDeclaring,
296 *ResolvedInserted))
297 return "";
298 return Includes.calculateIncludePath(*ResolvedDeclaring,
299 *ResolvedInserted);
Eric Liu63f419a2018-05-15 15:29:32 +0000300 }();
Eric Liu8f3678d2018-06-15 13:34:18 +0000301 if (!IncludePath) {
Eric Liu63f419a2018-05-15 15:29:32 +0000302 std::string ErrMsg =
303 ("Failed to generate include insertion edits for adding header "
304 "(FileURI=\"" +
305 IndexResult->CanonicalDeclaration.FileURI +
306 "\", IncludeHeader=\"" + D->IncludeHeader + "\") into " +
307 FileName)
308 .str();
Eric Liu8f3678d2018-06-15 13:34:18 +0000309 log(ErrMsg + ":" + llvm::toString(IncludePath.takeError()));
310 } else if (!IncludePath->empty()) {
311 // FIXME: consider what to show when there is no #include insertion,
312 // and for sema results, for consistency.
313 if (auto Edit = Includes.insert(*IncludePath)) {
314 I.detail += ("\n" + StringRef(*IncludePath).trim('"')).str();
315 I.additionalTextEdits = {std::move(*Edit)};
316 InsertingInclude = true;
317 }
Eric Liu63f419a2018-05-15 15:29:32 +0000318 }
319 }
Sam McCall545a20d2018-01-19 14:34:02 +0000320 }
321 }
Eric Liu8f3678d2018-06-15 13:34:18 +0000322 I.label = (InsertingInclude ? Opts.IncludeIndicator.Insert
323 : Opts.IncludeIndicator.NoInsert) +
324 I.label;
Sam McCall545a20d2018-01-19 14:34:02 +0000325 I.scoreInfo = Scores;
326 I.sortText = sortText(Scores.finalScore, Name);
327 I.insertTextFormat = Opts.EnableSnippets ? InsertTextFormat::Snippet
328 : InsertTextFormat::PlainText;
329 return I;
Sam McCall98775c52017-12-04 13:49:59 +0000330 }
Sam McCallc18c2802018-06-15 11:06:29 +0000331
332 using Bundle = llvm::SmallVector<CompletionCandidate, 4>;
333
334 static CompletionItem build(const Bundle &Bundle, CompletionItem First,
335 const clangd::CodeCompleteOptions &Opts) {
336 if (Bundle.size() == 1)
337 return First;
338 // Patch up the completion item to make it look like a bundle.
339 // This is a bit of a hack but most things are the same.
340
341 // Need to erase the signature. All bundles are function calls.
342 llvm::StringRef Name = Bundle.front().Name;
343 First.insertText =
344 Opts.EnableSnippets ? (Name + "(${0})").str() : Name.str();
Eric Liu8f3678d2018-06-15 13:34:18 +0000345 // Keep the visual indicator of the original label.
346 bool InsertingInclude =
347 StringRef(First.label).startswith(Opts.IncludeIndicator.Insert);
348 First.label = (Twine(InsertingInclude ? Opts.IncludeIndicator.Insert
349 : Opts.IncludeIndicator.NoInsert) +
350 Name + "(…)")
351 .str();
Sam McCallc18c2802018-06-15 11:06:29 +0000352 First.detail = llvm::formatv("[{0} overloads]", Bundle.size());
353 return First;
354 }
Sam McCall98775c52017-12-04 13:49:59 +0000355};
Sam McCallc18c2802018-06-15 11:06:29 +0000356using ScoredBundle =
357 std::pair<CompletionCandidate::Bundle, CompletionItemScores>;
358struct ScoredBundleGreater {
359 bool operator()(const ScoredBundle &L, const ScoredBundle &R) {
360 if (L.second.finalScore != R.second.finalScore)
361 return L.second.finalScore > R.second.finalScore;
362 return L.first.front().Name <
363 R.first.front().Name; // Earlier name is better.
364 }
365};
Sam McCall98775c52017-12-04 13:49:59 +0000366
Sam McCall545a20d2018-01-19 14:34:02 +0000367// Determine the symbol ID for a Sema code completion result, if possible.
368llvm::Optional<SymbolID> getSymbolID(const CodeCompletionResult &R) {
369 switch (R.Kind) {
370 case CodeCompletionResult::RK_Declaration:
371 case CodeCompletionResult::RK_Pattern: {
372 llvm::SmallString<128> USR;
373 if (/*Ignore=*/clang::index::generateUSRForDecl(R.Declaration, USR))
374 return None;
375 return SymbolID(USR);
376 }
377 case CodeCompletionResult::RK_Macro:
378 // FIXME: Macros do have USRs, but the CCR doesn't contain enough info.
379 case CodeCompletionResult::RK_Keyword:
380 return None;
381 }
382 llvm_unreachable("unknown CodeCompletionResult kind");
383}
384
Haojian Wu061c73e2018-01-23 11:37:26 +0000385// Scopes of the paritial identifier we're trying to complete.
386// It is used when we query the index for more completion results.
Eric Liu6f648df2017-12-19 16:50:37 +0000387struct SpecifiedScope {
Haojian Wu061c73e2018-01-23 11:37:26 +0000388 // The scopes we should look in, determined by Sema.
389 //
390 // If the qualifier was fully resolved, we look for completions in these
391 // scopes; if there is an unresolved part of the qualifier, it should be
392 // resolved within these scopes.
393 //
394 // Examples of qualified completion:
395 //
396 // "::vec" => {""}
397 // "using namespace std; ::vec^" => {"", "std::"}
398 // "namespace ns {using namespace std;} ns::^" => {"ns::", "std::"}
399 // "std::vec^" => {""} // "std" unresolved
400 //
401 // Examples of unqualified completion:
402 //
403 // "vec^" => {""}
404 // "using namespace std; vec^" => {"", "std::"}
405 // "using namespace std; namespace ns { vec^ }" => {"ns::", "std::", ""}
406 //
407 // "" for global namespace, "ns::" for normal namespace.
408 std::vector<std::string> AccessibleScopes;
409 // The full scope qualifier as typed by the user (without the leading "::").
410 // Set if the qualifier is not fully resolved by Sema.
411 llvm::Optional<std::string> UnresolvedQualifier;
Sam McCall545a20d2018-01-19 14:34:02 +0000412
Haojian Wu061c73e2018-01-23 11:37:26 +0000413 // Construct scopes being queried in indexes.
414 // This method format the scopes to match the index request representation.
415 std::vector<std::string> scopesForIndexQuery() {
416 std::vector<std::string> Results;
417 for (llvm::StringRef AS : AccessibleScopes) {
418 Results.push_back(AS);
419 if (UnresolvedQualifier)
420 Results.back() += *UnresolvedQualifier;
421 }
422 return Results;
Sam McCall545a20d2018-01-19 14:34:02 +0000423 }
Eric Liu6f648df2017-12-19 16:50:37 +0000424};
425
Haojian Wu061c73e2018-01-23 11:37:26 +0000426// Get all scopes that will be queried in indexes.
427std::vector<std::string> getQueryScopes(CodeCompletionContext &CCContext,
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000428 const SourceManager &SM) {
429 auto GetAllAccessibleScopes = [](CodeCompletionContext &CCContext) {
Haojian Wu061c73e2018-01-23 11:37:26 +0000430 SpecifiedScope Info;
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000431 for (auto *Context : CCContext.getVisitedContexts()) {
Haojian Wu061c73e2018-01-23 11:37:26 +0000432 if (isa<TranslationUnitDecl>(Context))
433 Info.AccessibleScopes.push_back(""); // global namespace
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000434 else if (const auto *NS = dyn_cast<NamespaceDecl>(Context))
Haojian Wu061c73e2018-01-23 11:37:26 +0000435 Info.AccessibleScopes.push_back(NS->getQualifiedNameAsString() + "::");
436 }
437 return Info;
438 };
439
440 auto SS = CCContext.getCXXScopeSpecifier();
441
442 // Unqualified completion (e.g. "vec^").
443 if (!SS) {
444 // FIXME: Once we can insert namespace qualifiers and use the in-scope
445 // namespaces for scoring, search in all namespaces.
446 // FIXME: Capture scopes and use for scoring, for example,
447 // "using namespace std; namespace foo {v^}" =>
448 // foo::value > std::vector > boost::variant
449 return GetAllAccessibleScopes(CCContext).scopesForIndexQuery();
450 }
451
452 // Qualified completion ("std::vec^"), we have two cases depending on whether
453 // the qualifier can be resolved by Sema.
454 if ((*SS)->isValid()) { // Resolved qualifier.
Haojian Wu061c73e2018-01-23 11:37:26 +0000455 return GetAllAccessibleScopes(CCContext).scopesForIndexQuery();
456 }
457
458 // Unresolved qualifier.
459 // FIXME: When Sema can resolve part of a scope chain (e.g.
460 // "known::unknown::id"), we should expand the known part ("known::") rather
461 // than treating the whole thing as unknown.
462 SpecifiedScope Info;
463 Info.AccessibleScopes.push_back(""); // global namespace
464
465 Info.UnresolvedQualifier =
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000466 Lexer::getSourceText(CharSourceRange::getCharRange((*SS)->getRange()), SM,
467 clang::LangOptions())
468 .ltrim("::");
Haojian Wu061c73e2018-01-23 11:37:26 +0000469 // Sema excludes the trailing "::".
470 if (!Info.UnresolvedQualifier->empty())
471 *Info.UnresolvedQualifier += "::";
472
473 return Info.scopesForIndexQuery();
474}
475
Eric Liu42abe412018-05-24 11:20:19 +0000476// Should we perform index-based completion in a context of the specified kind?
477// FIXME: consider allowing completion, but restricting the result types.
478bool contextAllowsIndex(enum CodeCompletionContext::Kind K) {
479 switch (K) {
480 case CodeCompletionContext::CCC_TopLevel:
481 case CodeCompletionContext::CCC_ObjCInterface:
482 case CodeCompletionContext::CCC_ObjCImplementation:
483 case CodeCompletionContext::CCC_ObjCIvarList:
484 case CodeCompletionContext::CCC_ClassStructUnion:
485 case CodeCompletionContext::CCC_Statement:
486 case CodeCompletionContext::CCC_Expression:
487 case CodeCompletionContext::CCC_ObjCMessageReceiver:
488 case CodeCompletionContext::CCC_EnumTag:
489 case CodeCompletionContext::CCC_UnionTag:
490 case CodeCompletionContext::CCC_ClassOrStructTag:
491 case CodeCompletionContext::CCC_ObjCProtocolName:
492 case CodeCompletionContext::CCC_Namespace:
493 case CodeCompletionContext::CCC_Type:
494 case CodeCompletionContext::CCC_Name: // FIXME: why does ns::^ give this?
495 case CodeCompletionContext::CCC_PotentiallyQualifiedName:
496 case CodeCompletionContext::CCC_ParenthesizedExpression:
497 case CodeCompletionContext::CCC_ObjCInterfaceName:
498 case CodeCompletionContext::CCC_ObjCCategoryName:
499 return true;
500 case CodeCompletionContext::CCC_Other: // Be conservative.
501 case CodeCompletionContext::CCC_OtherWithMacros:
502 case CodeCompletionContext::CCC_DotMemberAccess:
503 case CodeCompletionContext::CCC_ArrowMemberAccess:
504 case CodeCompletionContext::CCC_ObjCPropertyAccess:
505 case CodeCompletionContext::CCC_MacroName:
506 case CodeCompletionContext::CCC_MacroNameUse:
507 case CodeCompletionContext::CCC_PreprocessorExpression:
508 case CodeCompletionContext::CCC_PreprocessorDirective:
509 case CodeCompletionContext::CCC_NaturalLanguage:
510 case CodeCompletionContext::CCC_SelectorName:
511 case CodeCompletionContext::CCC_TypeQualifiers:
512 case CodeCompletionContext::CCC_ObjCInstanceMessage:
513 case CodeCompletionContext::CCC_ObjCClassMessage:
514 case CodeCompletionContext::CCC_Recovery:
515 return false;
516 }
517 llvm_unreachable("unknown code completion context");
518}
519
Sam McCall4caa8512018-06-07 12:49:17 +0000520// Some member calls are blacklisted because they're so rarely useful.
521static bool isBlacklistedMember(const NamedDecl &D) {
522 // Destructor completion is rarely useful, and works inconsistently.
523 // (s.^ completes ~string, but s.~st^ is an error).
524 if (D.getKind() == Decl::CXXDestructor)
525 return true;
526 // Injected name may be useful for A::foo(), but who writes A::A::foo()?
527 if (auto *R = dyn_cast_or_null<RecordDecl>(&D))
528 if (R->isInjectedClassName())
529 return true;
530 // Explicit calls to operators are also rare.
531 auto NameKind = D.getDeclName().getNameKind();
532 if (NameKind == DeclarationName::CXXOperatorName ||
533 NameKind == DeclarationName::CXXLiteralOperatorName ||
534 NameKind == DeclarationName::CXXConversionFunctionName)
535 return true;
536 return false;
537}
538
Sam McCall545a20d2018-01-19 14:34:02 +0000539// The CompletionRecorder captures Sema code-complete output, including context.
540// It filters out ignored results (but doesn't apply fuzzy-filtering yet).
541// It doesn't do scoring or conversion to CompletionItem yet, as we want to
542// merge with index results first.
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000543// Generally the fields and methods of this object should only be used from
544// within the callback.
Sam McCall545a20d2018-01-19 14:34:02 +0000545struct CompletionRecorder : public CodeCompleteConsumer {
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000546 CompletionRecorder(const CodeCompleteOptions &Opts,
547 UniqueFunction<void()> ResultsCallback)
Sam McCall545a20d2018-01-19 14:34:02 +0000548 : CodeCompleteConsumer(Opts.getClangCompleteOpts(),
Sam McCall98775c52017-12-04 13:49:59 +0000549 /*OutputIsBinary=*/false),
Sam McCall545a20d2018-01-19 14:34:02 +0000550 CCContext(CodeCompletionContext::CCC_Other), Opts(Opts),
551 CCAllocator(std::make_shared<GlobalCodeCompletionAllocator>()),
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000552 CCTUInfo(CCAllocator), ResultsCallback(std::move(ResultsCallback)) {
553 assert(this->ResultsCallback);
554 }
555
Sam McCall545a20d2018-01-19 14:34:02 +0000556 std::vector<CodeCompletionResult> Results;
557 CodeCompletionContext CCContext;
558 Sema *CCSema = nullptr; // Sema that created the results.
559 // FIXME: Sema is scary. Can we store ASTContext and Preprocessor, instead?
Sam McCall98775c52017-12-04 13:49:59 +0000560
Sam McCall545a20d2018-01-19 14:34:02 +0000561 void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context,
562 CodeCompletionResult *InResults,
Sam McCall98775c52017-12-04 13:49:59 +0000563 unsigned NumResults) override final {
Eric Liu42abe412018-05-24 11:20:19 +0000564 // If a callback is called without any sema result and the context does not
565 // support index-based completion, we simply skip it to give way to
566 // potential future callbacks with results.
567 if (NumResults == 0 && !contextAllowsIndex(Context.getKind()))
568 return;
Ilya Biryukov94da7bd2018-03-16 15:23:44 +0000569 if (CCSema) {
570 log(llvm::formatv(
571 "Multiple code complete callbacks (parser backtracked?). "
572 "Dropping results from context {0}, keeping results from {1}.",
Eric Liu42abe412018-05-24 11:20:19 +0000573 getCompletionKindString(Context.getKind()),
574 getCompletionKindString(this->CCContext.getKind())));
Ilya Biryukov94da7bd2018-03-16 15:23:44 +0000575 return;
576 }
Sam McCall545a20d2018-01-19 14:34:02 +0000577 // Record the completion context.
Sam McCall545a20d2018-01-19 14:34:02 +0000578 CCSema = &S;
579 CCContext = Context;
Eric Liu6f648df2017-12-19 16:50:37 +0000580
Sam McCall545a20d2018-01-19 14:34:02 +0000581 // Retain the results we might want.
Sam McCall98775c52017-12-04 13:49:59 +0000582 for (unsigned I = 0; I < NumResults; ++I) {
Sam McCall545a20d2018-01-19 14:34:02 +0000583 auto &Result = InResults[I];
584 // Drop hidden items which cannot be found by lookup after completion.
585 // Exception: some items can be named by using a qualifier.
Ilya Biryukovf60bf342018-01-10 13:51:09 +0000586 if (Result.Hidden && (!Result.Qualifier || Result.QualifierIsInformative))
587 continue;
Sam McCall545a20d2018-01-19 14:34:02 +0000588 if (!Opts.IncludeIneligibleResults &&
Sam McCall98775c52017-12-04 13:49:59 +0000589 (Result.Availability == CXAvailability_NotAvailable ||
590 Result.Availability == CXAvailability_NotAccessible))
591 continue;
Sam McCall4caa8512018-06-07 12:49:17 +0000592 if (Result.Declaration &&
593 !Context.getBaseType().isNull() // is this a member-access context?
594 && isBlacklistedMember(*Result.Declaration))
Sam McCalld2a95922018-01-22 21:05:00 +0000595 continue;
Ilya Biryukov53d6d932018-03-06 16:45:21 +0000596 // We choose to never append '::' to completion results in clangd.
597 Result.StartsNestedNameSpecifier = false;
Sam McCall545a20d2018-01-19 14:34:02 +0000598 Results.push_back(Result);
Sam McCall98775c52017-12-04 13:49:59 +0000599 }
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000600 ResultsCallback();
Sam McCall98775c52017-12-04 13:49:59 +0000601 }
602
Sam McCall545a20d2018-01-19 14:34:02 +0000603 CodeCompletionAllocator &getAllocator() override { return *CCAllocator; }
Sam McCall98775c52017-12-04 13:49:59 +0000604 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
605
Sam McCall545a20d2018-01-19 14:34:02 +0000606 // Returns the filtering/sorting name for Result, which must be from Results.
607 // Returned string is owned by this recorder (or the AST).
608 llvm::StringRef getName(const CodeCompletionResult &Result) {
Sam McCall98775c52017-12-04 13:49:59 +0000609 switch (Result.Kind) {
610 case CodeCompletionResult::RK_Declaration:
611 if (auto *ID = Result.Declaration->getIdentifier())
Sam McCall545a20d2018-01-19 14:34:02 +0000612 return ID->getName();
Sam McCall98775c52017-12-04 13:49:59 +0000613 break;
614 case CodeCompletionResult::RK_Keyword:
Sam McCall545a20d2018-01-19 14:34:02 +0000615 return Result.Keyword;
Sam McCall98775c52017-12-04 13:49:59 +0000616 case CodeCompletionResult::RK_Macro:
Sam McCall545a20d2018-01-19 14:34:02 +0000617 return Result.Macro->getName();
Sam McCall98775c52017-12-04 13:49:59 +0000618 case CodeCompletionResult::RK_Pattern:
Sam McCall545a20d2018-01-19 14:34:02 +0000619 return Result.Pattern->getTypedText();
Sam McCall98775c52017-12-04 13:49:59 +0000620 }
Ilya Biryukov43714502018-05-16 12:32:44 +0000621 auto *CCS = codeCompletionString(Result);
Sam McCall545a20d2018-01-19 14:34:02 +0000622 return CCS->getTypedText();
Sam McCall98775c52017-12-04 13:49:59 +0000623 }
624
Sam McCall545a20d2018-01-19 14:34:02 +0000625 // Build a CodeCompletion string for R, which must be from Results.
626 // The CCS will be owned by this recorder.
Ilya Biryukov43714502018-05-16 12:32:44 +0000627 CodeCompletionString *codeCompletionString(const CodeCompletionResult &R) {
Sam McCall545a20d2018-01-19 14:34:02 +0000628 // CodeCompletionResult doesn't seem to be const-correct. We own it, anyway.
629 return const_cast<CodeCompletionResult &>(R).CreateCodeCompletionString(
Ilya Biryukov43714502018-05-16 12:32:44 +0000630 *CCSema, CCContext, *CCAllocator, CCTUInfo,
631 /*IncludeBriefComments=*/false);
Sam McCall98775c52017-12-04 13:49:59 +0000632 }
633
Sam McCall545a20d2018-01-19 14:34:02 +0000634private:
635 CodeCompleteOptions Opts;
636 std::shared_ptr<GlobalCodeCompletionAllocator> CCAllocator;
Sam McCall98775c52017-12-04 13:49:59 +0000637 CodeCompletionTUInfo CCTUInfo;
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000638 UniqueFunction<void()> ResultsCallback;
Sam McCall545a20d2018-01-19 14:34:02 +0000639};
640
Sam McCall98775c52017-12-04 13:49:59 +0000641class SignatureHelpCollector final : public CodeCompleteConsumer {
642
643public:
644 SignatureHelpCollector(const clang::CodeCompleteOptions &CodeCompleteOpts,
645 SignatureHelp &SigHelp)
646 : CodeCompleteConsumer(CodeCompleteOpts, /*OutputIsBinary=*/false),
647 SigHelp(SigHelp),
648 Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
649 CCTUInfo(Allocator) {}
650
651 void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
652 OverloadCandidate *Candidates,
653 unsigned NumCandidates) override {
654 SigHelp.signatures.reserve(NumCandidates);
655 // FIXME(rwols): How can we determine the "active overload candidate"?
656 // Right now the overloaded candidates seem to be provided in a "best fit"
657 // order, so I'm not too worried about this.
658 SigHelp.activeSignature = 0;
659 assert(CurrentArg <= (unsigned)std::numeric_limits<int>::max() &&
660 "too many arguments");
661 SigHelp.activeParameter = static_cast<int>(CurrentArg);
662 for (unsigned I = 0; I < NumCandidates; ++I) {
663 const auto &Candidate = Candidates[I];
664 const auto *CCS = Candidate.CreateSignatureString(
665 CurrentArg, S, *Allocator, CCTUInfo, true);
666 assert(CCS && "Expected the CodeCompletionString to be non-null");
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +0000667 // FIXME: for headers, we need to get a comment from the index.
Ilya Biryukov43714502018-05-16 12:32:44 +0000668 SigHelp.signatures.push_back(ProcessOverloadCandidate(
669 Candidate, *CCS,
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +0000670 getParameterDocComment(S.getASTContext(), Candidate, CurrentArg,
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000671 /*CommentsFromHeaders=*/false)));
Sam McCall98775c52017-12-04 13:49:59 +0000672 }
673 }
674
675 GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
676
677 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
678
679private:
Eric Liu63696e12017-12-20 17:24:31 +0000680 // FIXME(ioeric): consider moving CodeCompletionString logic here to
681 // CompletionString.h.
Sam McCall98775c52017-12-04 13:49:59 +0000682 SignatureInformation
683 ProcessOverloadCandidate(const OverloadCandidate &Candidate,
Ilya Biryukov43714502018-05-16 12:32:44 +0000684 const CodeCompletionString &CCS,
685 llvm::StringRef DocComment) const {
Sam McCall98775c52017-12-04 13:49:59 +0000686 SignatureInformation Result;
687 const char *ReturnType = nullptr;
688
Ilya Biryukov43714502018-05-16 12:32:44 +0000689 Result.documentation = formatDocumentation(CCS, DocComment);
Sam McCall98775c52017-12-04 13:49:59 +0000690
691 for (const auto &Chunk : CCS) {
692 switch (Chunk.Kind) {
693 case CodeCompletionString::CK_ResultType:
694 // A piece of text that describes the type of an entity or,
695 // for functions and methods, the return type.
696 assert(!ReturnType && "Unexpected CK_ResultType");
697 ReturnType = Chunk.Text;
698 break;
699 case CodeCompletionString::CK_Placeholder:
700 // A string that acts as a placeholder for, e.g., a function call
701 // argument.
702 // Intentional fallthrough here.
703 case CodeCompletionString::CK_CurrentParameter: {
704 // A piece of text that describes the parameter that corresponds to
705 // the code-completion location within a function call, message send,
706 // macro invocation, etc.
707 Result.label += Chunk.Text;
708 ParameterInformation Info;
709 Info.label = Chunk.Text;
710 Result.parameters.push_back(std::move(Info));
711 break;
712 }
713 case CodeCompletionString::CK_Optional: {
714 // The rest of the parameters are defaulted/optional.
715 assert(Chunk.Optional &&
716 "Expected the optional code completion string to be non-null.");
717 Result.label +=
718 getOptionalParameters(*Chunk.Optional, Result.parameters);
719 break;
720 }
721 case CodeCompletionString::CK_VerticalSpace:
722 break;
723 default:
724 Result.label += Chunk.Text;
725 break;
726 }
727 }
728 if (ReturnType) {
729 Result.label += " -> ";
730 Result.label += ReturnType;
731 }
732 return Result;
733 }
734
735 SignatureHelp &SigHelp;
736 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
737 CodeCompletionTUInfo CCTUInfo;
738
739}; // SignatureHelpCollector
740
Sam McCall545a20d2018-01-19 14:34:02 +0000741struct SemaCompleteInput {
742 PathRef FileName;
743 const tooling::CompileCommand &Command;
744 PrecompiledPreamble const *Preamble;
Eric Liu63f419a2018-05-15 15:29:32 +0000745 const std::vector<Inclusion> &PreambleInclusions;
Sam McCall545a20d2018-01-19 14:34:02 +0000746 StringRef Contents;
747 Position Pos;
748 IntrusiveRefCntPtr<vfs::FileSystem> VFS;
749 std::shared_ptr<PCHContainerOperations> PCHs;
750};
751
752// Invokes Sema code completion on a file.
Eric Liu63f419a2018-05-15 15:29:32 +0000753// If \p Includes is set, it will be initialized after a compiler instance has
754// been set up.
Sam McCalld1a7a372018-01-31 13:40:48 +0000755bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
Sam McCall545a20d2018-01-19 14:34:02 +0000756 const clang::CodeCompleteOptions &Options,
Eric Liu63f419a2018-05-15 15:29:32 +0000757 const SemaCompleteInput &Input,
758 std::unique_ptr<IncludeInserter> *Includes = nullptr) {
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000759 trace::Span Tracer("Sema completion");
Sam McCall98775c52017-12-04 13:49:59 +0000760 std::vector<const char *> ArgStrs;
Sam McCall545a20d2018-01-19 14:34:02 +0000761 for (const auto &S : Input.Command.CommandLine)
Sam McCall98775c52017-12-04 13:49:59 +0000762 ArgStrs.push_back(S.c_str());
763
Ilya Biryukova9cf3112018-02-13 17:15:06 +0000764 if (Input.VFS->setCurrentWorkingDirectory(Input.Command.Directory)) {
765 log("Couldn't set working directory");
766 // We run parsing anyway, our lit-tests rely on results for non-existing
767 // working dirs.
768 }
Sam McCall98775c52017-12-04 13:49:59 +0000769
770 IgnoreDiagnostics DummyDiagsConsumer;
771 auto CI = createInvocationFromCommandLine(
772 ArgStrs,
773 CompilerInstance::createDiagnostics(new DiagnosticOptions,
774 &DummyDiagsConsumer, false),
Sam McCall545a20d2018-01-19 14:34:02 +0000775 Input.VFS);
Ilya Biryukovb6ad25c2018-02-09 13:51:57 +0000776 if (!CI) {
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000777 log("Couldn't create CompilerInvocation");
Ilya Biryukovb6ad25c2018-02-09 13:51:57 +0000778 return false;
779 }
Ilya Biryukov981a35d2018-05-28 12:11:37 +0000780 auto &FrontendOpts = CI->getFrontendOpts();
781 FrontendOpts.DisableFree = false;
Sam McCall98775c52017-12-04 13:49:59 +0000782 FrontendOpts.SkipFunctionBodies = true;
Ilya Biryukov981a35d2018-05-28 12:11:37 +0000783 CI->getLangOpts()->CommentOpts.ParseAllComments = true;
784 // Disable typo correction in Sema.
785 CI->getLangOpts()->SpellChecking = false;
786 // Setup code completion.
Sam McCall98775c52017-12-04 13:49:59 +0000787 FrontendOpts.CodeCompleteOpts = Options;
Sam McCall545a20d2018-01-19 14:34:02 +0000788 FrontendOpts.CodeCompletionAt.FileName = Input.FileName;
Sam McCalla4962cc2018-04-27 11:59:28 +0000789 auto Offset = positionToOffset(Input.Contents, Input.Pos);
790 if (!Offset) {
791 log("Code completion position was invalid " +
792 llvm::toString(Offset.takeError()));
793 return false;
794 }
795 std::tie(FrontendOpts.CodeCompletionAt.Line,
796 FrontendOpts.CodeCompletionAt.Column) =
797 offsetToClangLineColumn(Input.Contents, *Offset);
Sam McCall98775c52017-12-04 13:49:59 +0000798
Ilya Biryukov981a35d2018-05-28 12:11:37 +0000799 std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
800 llvm::MemoryBuffer::getMemBufferCopy(Input.Contents, Input.FileName);
801 // The diagnostic options must be set before creating a CompilerInstance.
802 CI->getDiagnosticOpts().IgnoreWarnings = true;
803 // We reuse the preamble whether it's valid or not. This is a
804 // correctness/performance tradeoff: building without a preamble is slow, and
805 // completion is latency-sensitive.
806 // NOTE: we must call BeginSourceFile after prepareCompilerInstance. Otherwise
807 // the remapped buffers do not get freed.
808 auto Clang = prepareCompilerInstance(
809 std::move(CI), Input.Preamble, std::move(ContentsBuffer),
810 std::move(Input.PCHs), std::move(Input.VFS), DummyDiagsConsumer);
Sam McCall98775c52017-12-04 13:49:59 +0000811 Clang->setCodeCompletionConsumer(Consumer.release());
812
813 SyntaxOnlyAction Action;
814 if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) {
Sam McCalld1a7a372018-01-31 13:40:48 +0000815 log("BeginSourceFile() failed when running codeComplete for " +
816 Input.FileName);
Sam McCall98775c52017-12-04 13:49:59 +0000817 return false;
818 }
Eric Liu63f419a2018-05-15 15:29:32 +0000819 if (Includes) {
820 // Initialize Includes if provided.
821
822 // FIXME(ioeric): needs more consistent style support in clangd server.
823 auto Style = format::getStyle("file", Input.FileName, "LLVM",
824 Input.Contents, Input.VFS.get());
825 if (!Style) {
826 log("Failed to get FormatStyle for file" + Input.FileName +
827 ". Fall back to use LLVM style. Error: " +
828 llvm::toString(Style.takeError()));
829 Style = format::getLLVMStyle();
830 }
831 *Includes = llvm::make_unique<IncludeInserter>(
832 Input.FileName, Input.Contents, *Style, Input.Command.Directory,
833 Clang->getPreprocessor().getHeaderSearchInfo());
834 for (const auto &Inc : Input.PreambleInclusions)
835 Includes->get()->addExisting(Inc);
836 Clang->getPreprocessor().addPPCallbacks(collectInclusionsInMainFileCallback(
837 Clang->getSourceManager(), [Includes](Inclusion Inc) {
838 Includes->get()->addExisting(std::move(Inc));
839 }));
840 }
Sam McCall98775c52017-12-04 13:49:59 +0000841 if (!Action.Execute()) {
Sam McCalld1a7a372018-01-31 13:40:48 +0000842 log("Execute() failed when running codeComplete for " + Input.FileName);
Sam McCall98775c52017-12-04 13:49:59 +0000843 return false;
844 }
Sam McCall98775c52017-12-04 13:49:59 +0000845 Action.EndSourceFile();
846
847 return true;
848}
849
Ilya Biryukova907ba42018-05-14 10:50:04 +0000850// Should we allow index completions in the specified context?
851bool allowIndex(CodeCompletionContext &CC) {
852 if (!contextAllowsIndex(CC.getKind()))
853 return false;
854 // We also avoid ClassName::bar (but allow namespace::bar).
855 auto Scope = CC.getCXXScopeSpecifier();
856 if (!Scope)
857 return true;
858 NestedNameSpecifier *NameSpec = (*Scope)->getScopeRep();
859 if (!NameSpec)
860 return true;
861 // We only query the index when qualifier is a namespace.
862 // If it's a class, we rely solely on sema completions.
863 switch (NameSpec->getKind()) {
864 case NestedNameSpecifier::Global:
865 case NestedNameSpecifier::Namespace:
866 case NestedNameSpecifier::NamespaceAlias:
867 return true;
868 case NestedNameSpecifier::Super:
869 case NestedNameSpecifier::TypeSpec:
870 case NestedNameSpecifier::TypeSpecWithTemplate:
871 // Unresolved inside a template.
872 case NestedNameSpecifier::Identifier:
873 return false;
874 }
Ilya Biryukova6556e22018-05-14 11:47:30 +0000875 llvm_unreachable("invalid NestedNameSpecifier kind");
Ilya Biryukova907ba42018-05-14 10:50:04 +0000876}
877
Sam McCall98775c52017-12-04 13:49:59 +0000878} // namespace
879
880clang::CodeCompleteOptions CodeCompleteOptions::getClangCompleteOpts() const {
881 clang::CodeCompleteOptions Result;
882 Result.IncludeCodePatterns = EnableSnippets && IncludeCodePatterns;
883 Result.IncludeMacros = IncludeMacros;
Sam McCalld8169a82018-01-18 15:31:30 +0000884 Result.IncludeGlobals = true;
Ilya Biryukov43714502018-05-16 12:32:44 +0000885 // We choose to include full comments and not do doxygen parsing in
886 // completion.
887 // FIXME: ideally, we should support doxygen in some form, e.g. do markdown
888 // formatting of the comments.
889 Result.IncludeBriefComments = false;
Sam McCall98775c52017-12-04 13:49:59 +0000890
Sam McCall3d139c52018-01-12 18:30:08 +0000891 // When an is used, Sema is responsible for completing the main file,
892 // the index can provide results from the preamble.
893 // Tell Sema not to deserialize the preamble to look for results.
894 Result.LoadExternal = !Index;
Eric Liu6f648df2017-12-19 16:50:37 +0000895
Sam McCall98775c52017-12-04 13:49:59 +0000896 return Result;
897}
898
Sam McCall545a20d2018-01-19 14:34:02 +0000899// Runs Sema-based (AST) and Index-based completion, returns merged results.
900//
901// There are a few tricky considerations:
902// - the AST provides information needed for the index query (e.g. which
903// namespaces to search in). So Sema must start first.
904// - we only want to return the top results (Opts.Limit).
905// Building CompletionItems for everything else is wasteful, so we want to
906// preserve the "native" format until we're done with scoring.
907// - the data underlying Sema completion items is owned by the AST and various
908// other arenas, which must stay alive for us to build CompletionItems.
909// - we may get duplicate results from Sema and the Index, we need to merge.
910//
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000911// So we start Sema completion first, and do all our work in its callback.
Sam McCall545a20d2018-01-19 14:34:02 +0000912// We use the Sema context information to query the index.
913// Then we merge the two result sets, producing items that are Sema/Index/Both.
914// These items are scored, and the top N are synthesized into the LSP response.
915// Finally, we can clean up the data structures created by Sema completion.
916//
917// Main collaborators are:
918// - semaCodeComplete sets up the compiler machinery to run code completion.
919// - CompletionRecorder captures Sema completion results, including context.
920// - SymbolIndex (Opts.Index) provides index completion results as Symbols
921// - CompletionCandidates are the result of merging Sema and Index results.
922// Each candidate points to an underlying CodeCompletionResult (Sema), a
923// Symbol (Index), or both. It computes the result quality score.
924// CompletionCandidate also does conversion to CompletionItem (at the end).
925// - FuzzyMatcher scores how the candidate matches the partial identifier.
926// This score is combined with the result quality score for the final score.
927// - TopN determines the results with the best score.
928class CodeCompleteFlow {
Eric Liuc5105f92018-02-16 14:15:55 +0000929 PathRef FileName;
Sam McCall545a20d2018-01-19 14:34:02 +0000930 const CodeCompleteOptions &Opts;
931 // Sema takes ownership of Recorder. Recorder is valid until Sema cleanup.
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000932 CompletionRecorder *Recorder = nullptr;
Sam McCall545a20d2018-01-19 14:34:02 +0000933 int NSema = 0, NIndex = 0, NBoth = 0; // Counters for logging.
934 bool Incomplete = false; // Would more be available with a higher limit?
Eric Liu63f419a2018-05-15 15:29:32 +0000935 llvm::Optional<FuzzyMatcher> Filter; // Initialized once Sema runs.
936 std::unique_ptr<IncludeInserter> Includes; // Initialized once compiler runs.
Eric Liu09c3c372018-06-15 08:58:12 +0000937 FileProximityMatcher FileProximityMatch;
Sam McCall545a20d2018-01-19 14:34:02 +0000938
939public:
940 // A CodeCompleteFlow object is only useful for calling run() exactly once.
Eric Liuc5105f92018-02-16 14:15:55 +0000941 CodeCompleteFlow(PathRef FileName, const CodeCompleteOptions &Opts)
Eric Liu09c3c372018-06-15 08:58:12 +0000942 : FileName(FileName), Opts(Opts),
943 // FIXME: also use path of the main header corresponding to FileName to
944 // calculate the file proximity, which would capture include/ and src/
945 // project setup where headers and implementations are not in the same
946 // directory.
Haojian Wu7943d4f2018-06-15 09:32:36 +0000947 FileProximityMatch(ArrayRef<StringRef>({FileName})) {}
Sam McCall545a20d2018-01-19 14:34:02 +0000948
949 CompletionList run(const SemaCompleteInput &SemaCCInput) && {
Sam McCalld1a7a372018-01-31 13:40:48 +0000950 trace::Span Tracer("CodeCompleteFlow");
Eric Liu63f419a2018-05-15 15:29:32 +0000951
Sam McCall545a20d2018-01-19 14:34:02 +0000952 // We run Sema code completion first. It builds an AST and calculates:
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000953 // - completion results based on the AST.
Sam McCall545a20d2018-01-19 14:34:02 +0000954 // - partial identifier and context. We need these for the index query.
955 CompletionList Output;
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000956 auto RecorderOwner = llvm::make_unique<CompletionRecorder>(Opts, [&]() {
957 assert(Recorder && "Recorder is not set");
Eric Liu63f419a2018-05-15 15:29:32 +0000958 assert(Includes && "Includes is not set");
959 // If preprocessor was run, inclusions from preprocessor callback should
960 // already be added to Inclusions.
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000961 Output = runWithSema();
Eric Liu63f419a2018-05-15 15:29:32 +0000962 Includes.reset(); // Make sure this doesn't out-live Clang.
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000963 SPAN_ATTACH(Tracer, "sema_completion_kind",
964 getCompletionKindString(Recorder->CCContext.getKind()));
965 });
966
967 Recorder = RecorderOwner.get();
Sam McCalld1a7a372018-01-31 13:40:48 +0000968 semaCodeComplete(std::move(RecorderOwner), Opts.getClangCompleteOpts(),
Eric Liu63f419a2018-05-15 15:29:32 +0000969 SemaCCInput, &Includes);
Sam McCall545a20d2018-01-19 14:34:02 +0000970
Sam McCall2b780162018-01-30 17:20:54 +0000971 SPAN_ATTACH(Tracer, "sema_results", NSema);
972 SPAN_ATTACH(Tracer, "index_results", NIndex);
973 SPAN_ATTACH(Tracer, "merged_results", NBoth);
974 SPAN_ATTACH(Tracer, "returned_results", Output.items.size());
975 SPAN_ATTACH(Tracer, "incomplete", Output.isIncomplete);
Sam McCall0f8df3e2018-06-13 11:31:20 +0000976 log(llvm::formatv("Code complete: {0} results from Sema, {1} from Index, "
977 "{2} matched, {3} returned{4}.",
978 NSema, NIndex, NBoth, Output.items.size(),
979 Output.isIncomplete ? " (incomplete)" : ""));
Sam McCall545a20d2018-01-19 14:34:02 +0000980 assert(!Opts.Limit || Output.items.size() <= Opts.Limit);
981 // We don't assert that isIncomplete means we hit a limit.
982 // Indexes may choose to impose their own limits even if we don't have one.
983 return Output;
984 }
985
986private:
987 // This is called by run() once Sema code completion is done, but before the
988 // Sema data structures are torn down. It does all the real work.
989 CompletionList runWithSema() {
990 Filter = FuzzyMatcher(
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000991 Recorder->CCSema->getPreprocessor().getCodeCompletionFilter());
Sam McCall545a20d2018-01-19 14:34:02 +0000992 // Sema provides the needed context to query the index.
993 // FIXME: in addition to querying for extra/overlapping symbols, we should
994 // explicitly request symbols corresponding to Sema results.
995 // We can use their signals even if the index can't suggest them.
996 // We must copy index results to preserve them, but there are at most Limit.
Eric Liu8f3678d2018-06-15 13:34:18 +0000997 auto IndexResults = (Opts.Index && allowIndex(Recorder->CCContext))
998 ? queryIndex()
999 : SymbolSlab();
Sam McCall545a20d2018-01-19 14:34:02 +00001000 // Merge Sema and Index results, score them, and pick the winners.
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001001 auto Top = mergeResults(Recorder->Results, IndexResults);
Sam McCall545a20d2018-01-19 14:34:02 +00001002 // Convert the results to the desired LSP structs.
1003 CompletionList Output;
1004 for (auto &C : Top)
1005 Output.items.push_back(toCompletionItem(C.first, C.second));
1006 Output.isIncomplete = Incomplete;
1007 return Output;
1008 }
1009
1010 SymbolSlab queryIndex() {
Sam McCalld1a7a372018-01-31 13:40:48 +00001011 trace::Span Tracer("Query index");
Sam McCall2b780162018-01-30 17:20:54 +00001012 SPAN_ATTACH(Tracer, "limit", Opts.Limit);
1013
Sam McCall545a20d2018-01-19 14:34:02 +00001014 SymbolSlab::Builder ResultsBuilder;
1015 // Build the query.
1016 FuzzyFindRequest Req;
Haojian Wu48b48652018-01-25 09:20:09 +00001017 if (Opts.Limit)
1018 Req.MaxCandidateCount = Opts.Limit;
Sam McCall545a20d2018-01-19 14:34:02 +00001019 Req.Query = Filter->pattern();
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +00001020 Req.RestrictForCodeCompletion = true;
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001021 Req.Scopes = getQueryScopes(Recorder->CCContext,
1022 Recorder->CCSema->getSourceManager());
Eric Liu6de95ec2018-06-12 08:48:20 +00001023 Req.ProximityPaths.push_back(FileName);
Sam McCalld1a7a372018-01-31 13:40:48 +00001024 log(llvm::formatv("Code complete: fuzzyFind(\"{0}\", scopes=[{1}])",
Sam McCall2b780162018-01-30 17:20:54 +00001025 Req.Query,
1026 llvm::join(Req.Scopes.begin(), Req.Scopes.end(), ",")));
Sam McCall545a20d2018-01-19 14:34:02 +00001027 // Run the query against the index.
Sam McCallab8e3932018-02-19 13:04:41 +00001028 if (Opts.Index->fuzzyFind(
1029 Req, [&](const Symbol &Sym) { ResultsBuilder.insert(Sym); }))
1030 Incomplete = true;
Sam McCall545a20d2018-01-19 14:34:02 +00001031 return std::move(ResultsBuilder).build();
1032 }
1033
Sam McCallc18c2802018-06-15 11:06:29 +00001034 // Merges Sema and Index results where possible, to form CompletionCandidates.
1035 // Groups overloads if desired, to form CompletionCandidate::Bundles.
1036 // The bundles are scored and top results are returned, best to worst.
1037 std::vector<ScoredBundle>
Sam McCall545a20d2018-01-19 14:34:02 +00001038 mergeResults(const std::vector<CodeCompletionResult> &SemaResults,
1039 const SymbolSlab &IndexResults) {
Sam McCalld1a7a372018-01-31 13:40:48 +00001040 trace::Span Tracer("Merge and score results");
Sam McCallc18c2802018-06-15 11:06:29 +00001041 std::vector<CompletionCandidate::Bundle> Bundles;
1042 llvm::DenseMap<size_t, size_t> BundleLookup;
1043 auto AddToBundles = [&](const CodeCompletionResult *SemaResult,
1044 const Symbol *IndexResult) {
1045 CompletionCandidate C;
1046 C.SemaResult = SemaResult;
1047 C.IndexResult = IndexResult;
1048 C.Name = IndexResult ? IndexResult->Name : Recorder->getName(*SemaResult);
1049 if (auto OverloadSet = Opts.BundleOverloads ? C.overloadSet() : 0) {
1050 auto Ret = BundleLookup.try_emplace(OverloadSet, Bundles.size());
1051 if (Ret.second)
1052 Bundles.emplace_back();
1053 Bundles[Ret.first->second].push_back(std::move(C));
1054 } else {
1055 Bundles.emplace_back();
1056 Bundles.back().push_back(std::move(C));
1057 }
1058 };
Sam McCall545a20d2018-01-19 14:34:02 +00001059 llvm::DenseSet<const Symbol *> UsedIndexResults;
1060 auto CorrespondingIndexResult =
1061 [&](const CodeCompletionResult &SemaResult) -> const Symbol * {
1062 if (auto SymID = getSymbolID(SemaResult)) {
1063 auto I = IndexResults.find(*SymID);
1064 if (I != IndexResults.end()) {
1065 UsedIndexResults.insert(&*I);
1066 return &*I;
1067 }
1068 }
1069 return nullptr;
1070 };
1071 // Emit all Sema results, merging them with Index results if possible.
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001072 for (auto &SemaResult : Recorder->Results)
Sam McCallc18c2802018-06-15 11:06:29 +00001073 AddToBundles(&SemaResult, CorrespondingIndexResult(SemaResult));
Sam McCall545a20d2018-01-19 14:34:02 +00001074 // Now emit any Index-only results.
1075 for (const auto &IndexResult : IndexResults) {
1076 if (UsedIndexResults.count(&IndexResult))
1077 continue;
Sam McCallc18c2802018-06-15 11:06:29 +00001078 AddToBundles(/*SemaResult=*/nullptr, &IndexResult);
Sam McCall545a20d2018-01-19 14:34:02 +00001079 }
Sam McCallc18c2802018-06-15 11:06:29 +00001080 // We only keep the best N results at any time, in "native" format.
1081 TopN<ScoredBundle, ScoredBundleGreater> Top(
1082 Opts.Limit == 0 ? std::numeric_limits<size_t>::max() : Opts.Limit);
1083 for (auto &Bundle : Bundles)
1084 addCandidate(Top, std::move(Bundle));
Sam McCall545a20d2018-01-19 14:34:02 +00001085 return std::move(Top).items();
1086 }
1087
Sam McCall80ad7072018-06-08 13:32:25 +00001088 Optional<float> fuzzyScore(const CompletionCandidate &C) {
1089 // Macros can be very spammy, so we only support prefix completion.
1090 // We won't end up with underfull index results, as macros are sema-only.
1091 if (C.SemaResult && C.SemaResult->Kind == CodeCompletionResult::RK_Macro &&
1092 !C.Name.startswith_lower(Filter->pattern()))
1093 return None;
1094 return Filter->match(C.Name);
1095 }
1096
Sam McCall545a20d2018-01-19 14:34:02 +00001097 // Scores a candidate and adds it to the TopN structure.
Sam McCallc18c2802018-06-15 11:06:29 +00001098 void addCandidate(TopN<ScoredBundle, ScoredBundleGreater> &Candidates,
1099 CompletionCandidate::Bundle Bundle) {
Sam McCallc5707b62018-05-15 17:43:27 +00001100 SymbolQualitySignals Quality;
1101 SymbolRelevanceSignals Relevance;
Sam McCalld9b54f02018-06-05 16:30:25 +00001102 Relevance.Query = SymbolRelevanceSignals::CodeComplete;
Eric Liu09c3c372018-06-15 08:58:12 +00001103 Relevance.FileProximityMatch = &FileProximityMatch;
Sam McCallc18c2802018-06-15 11:06:29 +00001104 auto &First = Bundle.front();
1105 if (auto FuzzyScore = fuzzyScore(First))
Sam McCallc5707b62018-05-15 17:43:27 +00001106 Relevance.NameMatch = *FuzzyScore;
Sam McCall545a20d2018-01-19 14:34:02 +00001107 else
1108 return;
Sam McCallc18c2802018-06-15 11:06:29 +00001109 unsigned SemaResult = 0, IndexResult = 0;
1110 for (const auto &Candidate : Bundle) {
1111 if (Candidate.IndexResult) {
1112 Quality.merge(*Candidate.IndexResult);
1113 Relevance.merge(*Candidate.IndexResult);
1114 ++IndexResult;
1115 }
1116 if (Candidate.SemaResult) {
1117 Quality.merge(*Candidate.SemaResult);
1118 Relevance.merge(*Candidate.SemaResult);
1119 ++SemaResult;
1120 }
Sam McCallc5707b62018-05-15 17:43:27 +00001121 }
1122
1123 float QualScore = Quality.evaluate(), RelScore = Relevance.evaluate();
1124 CompletionItemScores Scores;
1125 Scores.finalScore = evaluateSymbolAndRelevance(QualScore, RelScore);
1126 // The purpose of exporting component scores is to allow NameMatch to be
1127 // replaced on the client-side. So we export (NameMatch, final/NameMatch)
1128 // rather than (RelScore, QualScore).
1129 Scores.filterScore = Relevance.NameMatch;
1130 Scores.symbolScore =
1131 Scores.filterScore ? Scores.finalScore / Scores.filterScore : QualScore;
1132
Sam McCallc18c2802018-06-15 11:06:29 +00001133 LLVM_DEBUG(llvm::dbgs() << "CodeComplete: " << First.Name << "("
1134 << IndexResult << " index) "
1135 << "(" << SemaResult << " sema)"
1136 << " = " << Scores.finalScore << "\n"
1137 << Quality << Relevance << "\n");
Sam McCall545a20d2018-01-19 14:34:02 +00001138
1139 NSema += bool(SemaResult);
1140 NIndex += bool(IndexResult);
1141 NBoth += SemaResult && IndexResult;
Sam McCallc18c2802018-06-15 11:06:29 +00001142 if (Candidates.push({std::move(Bundle), Scores}))
Sam McCallab8e3932018-02-19 13:04:41 +00001143 Incomplete = true;
Sam McCall545a20d2018-01-19 14:34:02 +00001144 }
1145
Sam McCallc18c2802018-06-15 11:06:29 +00001146 CompletionItem toCompletionItem(const CompletionCandidate::Bundle &Bundle,
Sam McCall545a20d2018-01-19 14:34:02 +00001147 const CompletionItemScores &Scores) {
1148 CodeCompletionString *SemaCCS = nullptr;
Sam McCallc18c2802018-06-15 11:06:29 +00001149 std::string FrontDocComment;
1150 if (auto *SR = Bundle.front().SemaResult) {
Ilya Biryukov43714502018-05-16 12:32:44 +00001151 SemaCCS = Recorder->codeCompletionString(*SR);
1152 if (Opts.IncludeComments) {
1153 assert(Recorder->CCSema);
Sam McCallc18c2802018-06-15 11:06:29 +00001154 FrontDocComment = getDocComment(Recorder->CCSema->getASTContext(), *SR,
1155 /*CommentsFromHeader=*/false);
Ilya Biryukov43714502018-05-16 12:32:44 +00001156 }
1157 }
Sam McCallc18c2802018-06-15 11:06:29 +00001158 return CompletionCandidate::build(
1159 Bundle,
Eric Liu8f3678d2018-06-15 13:34:18 +00001160 Bundle.front().build(FileName, Scores, Opts, SemaCCS, *Includes,
Sam McCallc18c2802018-06-15 11:06:29 +00001161 FrontDocComment),
1162 Opts);
Sam McCall545a20d2018-01-19 14:34:02 +00001163 }
1164};
1165
Sam McCalld1a7a372018-01-31 13:40:48 +00001166CompletionList codeComplete(PathRef FileName,
Sam McCall98775c52017-12-04 13:49:59 +00001167 const tooling::CompileCommand &Command,
1168 PrecompiledPreamble const *Preamble,
Eric Liu63f419a2018-05-15 15:29:32 +00001169 const std::vector<Inclusion> &PreambleInclusions,
Sam McCall98775c52017-12-04 13:49:59 +00001170 StringRef Contents, Position Pos,
1171 IntrusiveRefCntPtr<vfs::FileSystem> VFS,
1172 std::shared_ptr<PCHContainerOperations> PCHs,
Ilya Biryukov940901e2017-12-13 12:51:22 +00001173 CodeCompleteOptions Opts) {
Eric Liuc5105f92018-02-16 14:15:55 +00001174 return CodeCompleteFlow(FileName, Opts)
Eric Liu63f419a2018-05-15 15:29:32 +00001175 .run({FileName, Command, Preamble, PreambleInclusions, Contents, Pos, VFS,
1176 PCHs});
Sam McCall98775c52017-12-04 13:49:59 +00001177}
1178
Sam McCalld1a7a372018-01-31 13:40:48 +00001179SignatureHelp signatureHelp(PathRef FileName,
Ilya Biryukov940901e2017-12-13 12:51:22 +00001180 const tooling::CompileCommand &Command,
1181 PrecompiledPreamble const *Preamble,
1182 StringRef Contents, Position Pos,
1183 IntrusiveRefCntPtr<vfs::FileSystem> VFS,
1184 std::shared_ptr<PCHContainerOperations> PCHs) {
Sam McCall98775c52017-12-04 13:49:59 +00001185 SignatureHelp Result;
1186 clang::CodeCompleteOptions Options;
1187 Options.IncludeGlobals = false;
1188 Options.IncludeMacros = false;
1189 Options.IncludeCodePatterns = false;
Ilya Biryukov43714502018-05-16 12:32:44 +00001190 Options.IncludeBriefComments = false;
Eric Liu4c0a27b2018-05-16 20:31:38 +00001191 std::vector<Inclusion> PreambleInclusions = {}; // Unused for signatureHelp
Sam McCalld1a7a372018-01-31 13:40:48 +00001192 semaCodeComplete(llvm::make_unique<SignatureHelpCollector>(Options, Result),
1193 Options,
Eric Liu4c0a27b2018-05-16 20:31:38 +00001194 {FileName, Command, Preamble, PreambleInclusions, Contents,
1195 Pos, std::move(VFS), std::move(PCHs)});
Sam McCall98775c52017-12-04 13:49:59 +00001196 return Result;
1197}
1198
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +00001199bool isIndexedForCodeCompletion(const NamedDecl &ND, ASTContext &ASTCtx) {
1200 using namespace clang::ast_matchers;
1201 auto InTopLevelScope = hasDeclContext(
1202 anyOf(namespaceDecl(), translationUnitDecl(), linkageSpecDecl()));
1203 return !match(decl(anyOf(InTopLevelScope,
1204 hasDeclContext(
1205 enumDecl(InTopLevelScope, unless(isScoped()))))),
1206 ND, ASTCtx)
1207 .empty();
1208}
1209
Sam McCall98775c52017-12-04 13:49:59 +00001210} // namespace clangd
1211} // namespace clang