blob: 31619398d172c4c1002abf29ced06733ee50f096 [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 Liu63696e12017-12-20 17:24:31 +000022#include "CodeCompletionStrings.h"
Sam McCall98775c52017-12-04 13:49:59 +000023#include "Compiler.h"
Sam McCall84652cc2018-01-12 16:16:09 +000024#include "FuzzyMatch.h"
Eric Liu63f419a2018-05-15 15:29:32 +000025#include "Headers.h"
Eric Liu6f648df2017-12-19 16:50:37 +000026#include "Logger.h"
Sam McCallc5707b62018-05-15 17:43:27 +000027#include "Quality.h"
Eric Liuc5105f92018-02-16 14:15:55 +000028#include "SourceCode.h"
Sam McCall2b780162018-01-30 17:20:54 +000029#include "Trace.h"
Eric Liu63f419a2018-05-15 15:29:32 +000030#include "URI.h"
Eric Liu6f648df2017-12-19 16:50:37 +000031#include "index/Index.h"
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +000032#include "clang/ASTMatchers/ASTMatchFinder.h"
Ilya Biryukovc22d3442018-05-16 12:32:49 +000033#include "clang/Basic/LangOptions.h"
Eric Liuc5105f92018-02-16 14:15:55 +000034#include "clang/Format/Format.h"
Sam McCall98775c52017-12-04 13:49:59 +000035#include "clang/Frontend/CompilerInstance.h"
36#include "clang/Frontend/FrontendActions.h"
Sam McCall545a20d2018-01-19 14:34:02 +000037#include "clang/Index/USRGeneration.h"
Sam McCall98775c52017-12-04 13:49:59 +000038#include "clang/Sema/CodeCompleteConsumer.h"
39#include "clang/Sema/Sema.h"
Eric Liuc5105f92018-02-16 14:15:55 +000040#include "clang/Tooling/Core/Replacement.h"
Haojian Wuba28e9a2018-01-10 14:44:34 +000041#include "llvm/Support/Format.h"
Sam McCall98775c52017-12-04 13:49:59 +000042#include <queue>
43
Sam McCallc5707b62018-05-15 17:43:27 +000044// We log detailed candidate here if you run with -debug-only=codecomplete.
45#define DEBUG_TYPE "codecomplete"
46
Sam McCall98775c52017-12-04 13:49:59 +000047namespace clang {
48namespace clangd {
49namespace {
50
Eric Liu6f648df2017-12-19 16:50:37 +000051CompletionItemKind toCompletionItemKind(index::SymbolKind Kind) {
52 using SK = index::SymbolKind;
53 switch (Kind) {
54 case SK::Unknown:
55 return CompletionItemKind::Missing;
56 case SK::Module:
57 case SK::Namespace:
58 case SK::NamespaceAlias:
59 return CompletionItemKind::Module;
60 case SK::Macro:
61 return CompletionItemKind::Text;
62 case SK::Enum:
63 return CompletionItemKind::Enum;
64 // FIXME(ioeric): use LSP struct instead of class when it is suppoted in the
65 // protocol.
66 case SK::Struct:
67 case SK::Class:
68 case SK::Protocol:
69 case SK::Extension:
70 case SK::Union:
71 return CompletionItemKind::Class;
72 // FIXME(ioeric): figure out whether reference is the right type for aliases.
73 case SK::TypeAlias:
74 case SK::Using:
75 return CompletionItemKind::Reference;
76 case SK::Function:
77 // FIXME(ioeric): this should probably be an operator. This should be fixed
78 // when `Operator` is support type in the protocol.
79 case SK::ConversionFunction:
80 return CompletionItemKind::Function;
81 case SK::Variable:
82 case SK::Parameter:
83 return CompletionItemKind::Variable;
84 case SK::Field:
85 return CompletionItemKind::Field;
86 // FIXME(ioeric): use LSP enum constant when it is supported in the protocol.
87 case SK::EnumConstant:
88 return CompletionItemKind::Value;
89 case SK::InstanceMethod:
90 case SK::ClassMethod:
91 case SK::StaticMethod:
92 case SK::Destructor:
93 return CompletionItemKind::Method;
94 case SK::InstanceProperty:
95 case SK::ClassProperty:
96 case SK::StaticProperty:
97 return CompletionItemKind::Property;
98 case SK::Constructor:
99 return CompletionItemKind::Constructor;
100 }
101 llvm_unreachable("Unhandled clang::index::SymbolKind.");
102}
103
Sam McCall83305892018-06-08 21:17:19 +0000104CompletionItemKind
105toCompletionItemKind(CodeCompletionResult::ResultKind ResKind,
106 const NamedDecl *Decl) {
107 if (Decl)
108 return toCompletionItemKind(index::getSymbolInfo(Decl).Kind);
109 switch (ResKind) {
110 case CodeCompletionResult::RK_Declaration:
111 llvm_unreachable("RK_Declaration without Decl");
112 case CodeCompletionResult::RK_Keyword:
113 return CompletionItemKind::Keyword;
114 case CodeCompletionResult::RK_Macro:
115 return CompletionItemKind::Text; // unfortunately, there's no 'Macro'
116 // completion items in LSP.
117 case CodeCompletionResult::RK_Pattern:
118 return CompletionItemKind::Snippet;
119 }
120 llvm_unreachable("Unhandled CodeCompletionResult::ResultKind.");
121}
122
Sam McCall98775c52017-12-04 13:49:59 +0000123/// Get the optional chunk as a string. This function is possibly recursive.
124///
125/// The parameter info for each parameter is appended to the Parameters.
126std::string
127getOptionalParameters(const CodeCompletionString &CCS,
128 std::vector<ParameterInformation> &Parameters) {
129 std::string Result;
130 for (const auto &Chunk : CCS) {
131 switch (Chunk.Kind) {
132 case CodeCompletionString::CK_Optional:
133 assert(Chunk.Optional &&
134 "Expected the optional code completion string to be non-null.");
135 Result += getOptionalParameters(*Chunk.Optional, Parameters);
136 break;
137 case CodeCompletionString::CK_VerticalSpace:
138 break;
139 case CodeCompletionString::CK_Placeholder:
140 // A string that acts as a placeholder for, e.g., a function call
141 // argument.
142 // Intentional fallthrough here.
143 case CodeCompletionString::CK_CurrentParameter: {
144 // A piece of text that describes the parameter that corresponds to
145 // the code-completion location within a function call, message send,
146 // macro invocation, etc.
147 Result += Chunk.Text;
148 ParameterInformation Info;
149 Info.label = Chunk.Text;
150 Parameters.push_back(std::move(Info));
151 break;
152 }
153 default:
154 Result += Chunk.Text;
155 break;
156 }
157 }
158 return Result;
159}
160
Eric Liu63f419a2018-05-15 15:29:32 +0000161/// Creates a `HeaderFile` from \p Header which can be either a URI or a literal
162/// include.
163static llvm::Expected<HeaderFile> toHeaderFile(StringRef Header,
164 llvm::StringRef HintPath) {
165 if (isLiteralInclude(Header))
166 return HeaderFile{Header.str(), /*Verbatim=*/true};
167 auto U = URI::parse(Header);
168 if (!U)
169 return U.takeError();
170
171 auto IncludePath = URI::includeSpelling(*U);
172 if (!IncludePath)
173 return IncludePath.takeError();
174 if (!IncludePath->empty())
175 return HeaderFile{std::move(*IncludePath), /*Verbatim=*/true};
176
177 auto Resolved = URI::resolve(*U, HintPath);
178 if (!Resolved)
179 return Resolved.takeError();
180 return HeaderFile{std::move(*Resolved), /*Verbatim=*/false};
181}
182
Sam McCall545a20d2018-01-19 14:34:02 +0000183/// A code completion result, in clang-native form.
Sam McCall98775c52017-12-04 13:49:59 +0000184/// It may be promoted to a CompletionItem if it's among the top-ranked results.
185struct CompletionCandidate {
Sam McCall545a20d2018-01-19 14:34:02 +0000186 llvm::StringRef Name; // Used for filtering and sorting.
187 // We may have a result from Sema, from the index, or both.
188 const CodeCompletionResult *SemaResult = nullptr;
189 const Symbol *IndexResult = nullptr;
Sam McCall98775c52017-12-04 13:49:59 +0000190
Sam McCallc18c2802018-06-15 11:06:29 +0000191 // Returns a token identifying the overload set this is part of.
192 // 0 indicates it's not part of any overload set.
193 size_t overloadSet() const {
194 SmallString<256> Scratch;
195 if (IndexResult) {
196 switch (IndexResult->SymInfo.Kind) {
197 case index::SymbolKind::ClassMethod:
198 case index::SymbolKind::InstanceMethod:
199 case index::SymbolKind::StaticMethod:
200 assert(false && "Don't expect members from index in code completion");
201 // fall through
202 case index::SymbolKind::Function:
203 // We can't group overloads together that need different #includes.
204 // This could break #include insertion.
205 return hash_combine(
206 (IndexResult->Scope + IndexResult->Name).toStringRef(Scratch),
207 headerToInsertIfNotPresent().getValueOr(""));
208 default:
209 return 0;
210 }
211 }
212 assert(SemaResult);
213 // We need to make sure we're consistent with the IndexResult case!
214 const NamedDecl *D = SemaResult->Declaration;
215 if (!D || !D->isFunctionOrFunctionTemplate())
216 return 0;
217 {
218 llvm::raw_svector_ostream OS(Scratch);
219 D->printQualifiedName(OS);
220 }
221 return hash_combine(Scratch, headerToInsertIfNotPresent().getValueOr(""));
222 }
223
224 llvm::Optional<llvm::StringRef> headerToInsertIfNotPresent() const {
225 if (!IndexResult || !IndexResult->Detail ||
226 IndexResult->Detail->IncludeHeader.empty())
227 return llvm::None;
228 if (SemaResult && SemaResult->Declaration) {
229 // Avoid inserting new #include if the declaration is found in the current
230 // file e.g. the symbol is forward declared.
231 auto &SM = SemaResult->Declaration->getASTContext().getSourceManager();
232 for (const Decl *RD : SemaResult->Declaration->redecls())
233 if (SM.isInMainFile(SM.getExpansionLoc(RD->getLocStart())))
234 return llvm::None;
235 }
236 return IndexResult->Detail->IncludeHeader;
237 }
238
Sam McCall545a20d2018-01-19 14:34:02 +0000239 // Builds an LSP completion item.
Eric Liu63f419a2018-05-15 15:29:32 +0000240 CompletionItem build(StringRef FileName, const CompletionItemScores &Scores,
Sam McCall545a20d2018-01-19 14:34:02 +0000241 const CodeCompleteOptions &Opts,
Eric Liu63f419a2018-05-15 15:29:32 +0000242 CodeCompletionString *SemaCCS,
Eric Liu8f3678d2018-06-15 13:34:18 +0000243 const IncludeInserter &Includes,
Ilya Biryukov43714502018-05-16 12:32:44 +0000244 llvm::StringRef SemaDocComment) const {
Sam McCall545a20d2018-01-19 14:34:02 +0000245 assert(bool(SemaResult) == bool(SemaCCS));
246 CompletionItem I;
Eric Liu8f3678d2018-06-15 13:34:18 +0000247 bool InsertingInclude = false; // Whether a new #include will be added.
Sam McCall545a20d2018-01-19 14:34:02 +0000248 if (SemaResult) {
Sam McCall83305892018-06-08 21:17:19 +0000249 I.kind = toCompletionItemKind(SemaResult->Kind, SemaResult->Declaration);
Sam McCall545a20d2018-01-19 14:34:02 +0000250 getLabelAndInsertText(*SemaCCS, &I.label, &I.insertText,
251 Opts.EnableSnippets);
Sam McCall032db942018-06-22 06:41:43 +0000252 if (const char* Text = SemaCCS->getTypedText())
253 I.filterText = Text;
Ilya Biryukov43714502018-05-16 12:32:44 +0000254 I.documentation = formatDocumentation(*SemaCCS, SemaDocComment);
Sam McCall545a20d2018-01-19 14:34:02 +0000255 I.detail = getDetail(*SemaCCS);
256 }
257 if (IndexResult) {
258 if (I.kind == CompletionItemKind::Missing)
259 I.kind = toCompletionItemKind(IndexResult->SymInfo.Kind);
260 // FIXME: reintroduce a way to show the index source for debugging.
261 if (I.label.empty())
262 I.label = IndexResult->CompletionLabel;
263 if (I.filterText.empty())
264 I.filterText = IndexResult->Name;
Sam McCall98775c52017-12-04 13:49:59 +0000265
Sam McCall545a20d2018-01-19 14:34:02 +0000266 // FIXME(ioeric): support inserting/replacing scope qualifiers.
267 if (I.insertText.empty())
268 I.insertText = Opts.EnableSnippets
269 ? IndexResult->CompletionSnippetInsertText
270 : IndexResult->CompletionPlainInsertText;
271
272 if (auto *D = IndexResult->Detail) {
273 if (I.documentation.empty())
274 I.documentation = D->Documentation;
275 if (I.detail.empty())
276 I.detail = D->CompletionDetail;
Sam McCallc18c2802018-06-15 11:06:29 +0000277 if (auto Inserted = headerToInsertIfNotPresent()) {
Eric Liu8f3678d2018-06-15 13:34:18 +0000278 auto IncludePath = [&]() -> Expected<std::string> {
Eric Liu63f419a2018-05-15 15:29:32 +0000279 auto ResolvedDeclaring = toHeaderFile(
280 IndexResult->CanonicalDeclaration.FileURI, FileName);
281 if (!ResolvedDeclaring)
282 return ResolvedDeclaring.takeError();
Sam McCallc18c2802018-06-15 11:06:29 +0000283 auto ResolvedInserted = toHeaderFile(*Inserted, FileName);
Eric Liu63f419a2018-05-15 15:29:32 +0000284 if (!ResolvedInserted)
285 return ResolvedInserted.takeError();
Eric Liu8f3678d2018-06-15 13:34:18 +0000286 if (!Includes.shouldInsertInclude(*ResolvedDeclaring,
287 *ResolvedInserted))
288 return "";
289 return Includes.calculateIncludePath(*ResolvedDeclaring,
290 *ResolvedInserted);
Eric Liu63f419a2018-05-15 15:29:32 +0000291 }();
Eric Liu8f3678d2018-06-15 13:34:18 +0000292 if (!IncludePath) {
Eric Liu63f419a2018-05-15 15:29:32 +0000293 std::string ErrMsg =
294 ("Failed to generate include insertion edits for adding header "
295 "(FileURI=\"" +
296 IndexResult->CanonicalDeclaration.FileURI +
297 "\", IncludeHeader=\"" + D->IncludeHeader + "\") into " +
298 FileName)
299 .str();
Eric Liu8f3678d2018-06-15 13:34:18 +0000300 log(ErrMsg + ":" + llvm::toString(IncludePath.takeError()));
301 } else if (!IncludePath->empty()) {
302 // FIXME: consider what to show when there is no #include insertion,
303 // and for sema results, for consistency.
304 if (auto Edit = Includes.insert(*IncludePath)) {
305 I.detail += ("\n" + StringRef(*IncludePath).trim('"')).str();
306 I.additionalTextEdits = {std::move(*Edit)};
307 InsertingInclude = true;
308 }
Eric Liu63f419a2018-05-15 15:29:32 +0000309 }
310 }
Sam McCall545a20d2018-01-19 14:34:02 +0000311 }
312 }
Eric Liu8f3678d2018-06-15 13:34:18 +0000313 I.label = (InsertingInclude ? Opts.IncludeIndicator.Insert
314 : Opts.IncludeIndicator.NoInsert) +
315 I.label;
Sam McCall545a20d2018-01-19 14:34:02 +0000316 I.scoreInfo = Scores;
317 I.sortText = sortText(Scores.finalScore, Name);
318 I.insertTextFormat = Opts.EnableSnippets ? InsertTextFormat::Snippet
319 : InsertTextFormat::PlainText;
320 return I;
Sam McCall98775c52017-12-04 13:49:59 +0000321 }
Sam McCallc18c2802018-06-15 11:06:29 +0000322
323 using Bundle = llvm::SmallVector<CompletionCandidate, 4>;
324
325 static CompletionItem build(const Bundle &Bundle, CompletionItem First,
326 const clangd::CodeCompleteOptions &Opts) {
327 if (Bundle.size() == 1)
328 return First;
329 // Patch up the completion item to make it look like a bundle.
330 // This is a bit of a hack but most things are the same.
331
332 // Need to erase the signature. All bundles are function calls.
333 llvm::StringRef Name = Bundle.front().Name;
334 First.insertText =
335 Opts.EnableSnippets ? (Name + "(${0})").str() : Name.str();
Eric Liu8f3678d2018-06-15 13:34:18 +0000336 // Keep the visual indicator of the original label.
337 bool InsertingInclude =
338 StringRef(First.label).startswith(Opts.IncludeIndicator.Insert);
339 First.label = (Twine(InsertingInclude ? Opts.IncludeIndicator.Insert
340 : Opts.IncludeIndicator.NoInsert) +
341 Name + "(…)")
342 .str();
Sam McCallc18c2802018-06-15 11:06:29 +0000343 First.detail = llvm::formatv("[{0} overloads]", Bundle.size());
344 return First;
345 }
Sam McCall98775c52017-12-04 13:49:59 +0000346};
Sam McCallc18c2802018-06-15 11:06:29 +0000347using ScoredBundle =
348 std::pair<CompletionCandidate::Bundle, CompletionItemScores>;
349struct ScoredBundleGreater {
350 bool operator()(const ScoredBundle &L, const ScoredBundle &R) {
351 if (L.second.finalScore != R.second.finalScore)
352 return L.second.finalScore > R.second.finalScore;
353 return L.first.front().Name <
354 R.first.front().Name; // Earlier name is better.
355 }
356};
Sam McCall98775c52017-12-04 13:49:59 +0000357
Sam McCall545a20d2018-01-19 14:34:02 +0000358// Determine the symbol ID for a Sema code completion result, if possible.
359llvm::Optional<SymbolID> getSymbolID(const CodeCompletionResult &R) {
360 switch (R.Kind) {
361 case CodeCompletionResult::RK_Declaration:
362 case CodeCompletionResult::RK_Pattern: {
363 llvm::SmallString<128> USR;
364 if (/*Ignore=*/clang::index::generateUSRForDecl(R.Declaration, USR))
365 return None;
366 return SymbolID(USR);
367 }
368 case CodeCompletionResult::RK_Macro:
369 // FIXME: Macros do have USRs, but the CCR doesn't contain enough info.
370 case CodeCompletionResult::RK_Keyword:
371 return None;
372 }
373 llvm_unreachable("unknown CodeCompletionResult kind");
374}
375
Haojian Wu061c73e2018-01-23 11:37:26 +0000376// Scopes of the paritial identifier we're trying to complete.
377// It is used when we query the index for more completion results.
Eric Liu6f648df2017-12-19 16:50:37 +0000378struct SpecifiedScope {
Haojian Wu061c73e2018-01-23 11:37:26 +0000379 // The scopes we should look in, determined by Sema.
380 //
381 // If the qualifier was fully resolved, we look for completions in these
382 // scopes; if there is an unresolved part of the qualifier, it should be
383 // resolved within these scopes.
384 //
385 // Examples of qualified completion:
386 //
387 // "::vec" => {""}
388 // "using namespace std; ::vec^" => {"", "std::"}
389 // "namespace ns {using namespace std;} ns::^" => {"ns::", "std::"}
390 // "std::vec^" => {""} // "std" unresolved
391 //
392 // Examples of unqualified completion:
393 //
394 // "vec^" => {""}
395 // "using namespace std; vec^" => {"", "std::"}
396 // "using namespace std; namespace ns { vec^ }" => {"ns::", "std::", ""}
397 //
398 // "" for global namespace, "ns::" for normal namespace.
399 std::vector<std::string> AccessibleScopes;
400 // The full scope qualifier as typed by the user (without the leading "::").
401 // Set if the qualifier is not fully resolved by Sema.
402 llvm::Optional<std::string> UnresolvedQualifier;
Sam McCall545a20d2018-01-19 14:34:02 +0000403
Haojian Wu061c73e2018-01-23 11:37:26 +0000404 // Construct scopes being queried in indexes.
405 // This method format the scopes to match the index request representation.
406 std::vector<std::string> scopesForIndexQuery() {
407 std::vector<std::string> Results;
408 for (llvm::StringRef AS : AccessibleScopes) {
409 Results.push_back(AS);
410 if (UnresolvedQualifier)
411 Results.back() += *UnresolvedQualifier;
412 }
413 return Results;
Sam McCall545a20d2018-01-19 14:34:02 +0000414 }
Eric Liu6f648df2017-12-19 16:50:37 +0000415};
416
Haojian Wu061c73e2018-01-23 11:37:26 +0000417// Get all scopes that will be queried in indexes.
418std::vector<std::string> getQueryScopes(CodeCompletionContext &CCContext,
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000419 const SourceManager &SM) {
420 auto GetAllAccessibleScopes = [](CodeCompletionContext &CCContext) {
Haojian Wu061c73e2018-01-23 11:37:26 +0000421 SpecifiedScope Info;
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000422 for (auto *Context : CCContext.getVisitedContexts()) {
Haojian Wu061c73e2018-01-23 11:37:26 +0000423 if (isa<TranslationUnitDecl>(Context))
424 Info.AccessibleScopes.push_back(""); // global namespace
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000425 else if (const auto *NS = dyn_cast<NamespaceDecl>(Context))
Haojian Wu061c73e2018-01-23 11:37:26 +0000426 Info.AccessibleScopes.push_back(NS->getQualifiedNameAsString() + "::");
427 }
428 return Info;
429 };
430
431 auto SS = CCContext.getCXXScopeSpecifier();
432
433 // Unqualified completion (e.g. "vec^").
434 if (!SS) {
435 // FIXME: Once we can insert namespace qualifiers and use the in-scope
436 // namespaces for scoring, search in all namespaces.
437 // FIXME: Capture scopes and use for scoring, for example,
438 // "using namespace std; namespace foo {v^}" =>
439 // foo::value > std::vector > boost::variant
440 return GetAllAccessibleScopes(CCContext).scopesForIndexQuery();
441 }
442
443 // Qualified completion ("std::vec^"), we have two cases depending on whether
444 // the qualifier can be resolved by Sema.
445 if ((*SS)->isValid()) { // Resolved qualifier.
Haojian Wu061c73e2018-01-23 11:37:26 +0000446 return GetAllAccessibleScopes(CCContext).scopesForIndexQuery();
447 }
448
449 // Unresolved qualifier.
450 // FIXME: When Sema can resolve part of a scope chain (e.g.
451 // "known::unknown::id"), we should expand the known part ("known::") rather
452 // than treating the whole thing as unknown.
453 SpecifiedScope Info;
454 Info.AccessibleScopes.push_back(""); // global namespace
455
456 Info.UnresolvedQualifier =
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000457 Lexer::getSourceText(CharSourceRange::getCharRange((*SS)->getRange()), SM,
458 clang::LangOptions())
459 .ltrim("::");
Haojian Wu061c73e2018-01-23 11:37:26 +0000460 // Sema excludes the trailing "::".
461 if (!Info.UnresolvedQualifier->empty())
462 *Info.UnresolvedQualifier += "::";
463
464 return Info.scopesForIndexQuery();
465}
466
Eric Liu42abe412018-05-24 11:20:19 +0000467// Should we perform index-based completion in a context of the specified kind?
468// FIXME: consider allowing completion, but restricting the result types.
469bool contextAllowsIndex(enum CodeCompletionContext::Kind K) {
470 switch (K) {
471 case CodeCompletionContext::CCC_TopLevel:
472 case CodeCompletionContext::CCC_ObjCInterface:
473 case CodeCompletionContext::CCC_ObjCImplementation:
474 case CodeCompletionContext::CCC_ObjCIvarList:
475 case CodeCompletionContext::CCC_ClassStructUnion:
476 case CodeCompletionContext::CCC_Statement:
477 case CodeCompletionContext::CCC_Expression:
478 case CodeCompletionContext::CCC_ObjCMessageReceiver:
479 case CodeCompletionContext::CCC_EnumTag:
480 case CodeCompletionContext::CCC_UnionTag:
481 case CodeCompletionContext::CCC_ClassOrStructTag:
482 case CodeCompletionContext::CCC_ObjCProtocolName:
483 case CodeCompletionContext::CCC_Namespace:
484 case CodeCompletionContext::CCC_Type:
485 case CodeCompletionContext::CCC_Name: // FIXME: why does ns::^ give this?
486 case CodeCompletionContext::CCC_PotentiallyQualifiedName:
487 case CodeCompletionContext::CCC_ParenthesizedExpression:
488 case CodeCompletionContext::CCC_ObjCInterfaceName:
489 case CodeCompletionContext::CCC_ObjCCategoryName:
490 return true;
491 case CodeCompletionContext::CCC_Other: // Be conservative.
492 case CodeCompletionContext::CCC_OtherWithMacros:
493 case CodeCompletionContext::CCC_DotMemberAccess:
494 case CodeCompletionContext::CCC_ArrowMemberAccess:
495 case CodeCompletionContext::CCC_ObjCPropertyAccess:
496 case CodeCompletionContext::CCC_MacroName:
497 case CodeCompletionContext::CCC_MacroNameUse:
498 case CodeCompletionContext::CCC_PreprocessorExpression:
499 case CodeCompletionContext::CCC_PreprocessorDirective:
500 case CodeCompletionContext::CCC_NaturalLanguage:
501 case CodeCompletionContext::CCC_SelectorName:
502 case CodeCompletionContext::CCC_TypeQualifiers:
503 case CodeCompletionContext::CCC_ObjCInstanceMessage:
504 case CodeCompletionContext::CCC_ObjCClassMessage:
505 case CodeCompletionContext::CCC_Recovery:
506 return false;
507 }
508 llvm_unreachable("unknown code completion context");
509}
510
Sam McCall4caa8512018-06-07 12:49:17 +0000511// Some member calls are blacklisted because they're so rarely useful.
512static bool isBlacklistedMember(const NamedDecl &D) {
513 // Destructor completion is rarely useful, and works inconsistently.
514 // (s.^ completes ~string, but s.~st^ is an error).
515 if (D.getKind() == Decl::CXXDestructor)
516 return true;
517 // Injected name may be useful for A::foo(), but who writes A::A::foo()?
518 if (auto *R = dyn_cast_or_null<RecordDecl>(&D))
519 if (R->isInjectedClassName())
520 return true;
521 // Explicit calls to operators are also rare.
522 auto NameKind = D.getDeclName().getNameKind();
523 if (NameKind == DeclarationName::CXXOperatorName ||
524 NameKind == DeclarationName::CXXLiteralOperatorName ||
525 NameKind == DeclarationName::CXXConversionFunctionName)
526 return true;
527 return false;
528}
529
Sam McCall545a20d2018-01-19 14:34:02 +0000530// The CompletionRecorder captures Sema code-complete output, including context.
531// It filters out ignored results (but doesn't apply fuzzy-filtering yet).
532// It doesn't do scoring or conversion to CompletionItem yet, as we want to
533// merge with index results first.
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000534// Generally the fields and methods of this object should only be used from
535// within the callback.
Sam McCall545a20d2018-01-19 14:34:02 +0000536struct CompletionRecorder : public CodeCompleteConsumer {
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000537 CompletionRecorder(const CodeCompleteOptions &Opts,
538 UniqueFunction<void()> ResultsCallback)
Sam McCall545a20d2018-01-19 14:34:02 +0000539 : CodeCompleteConsumer(Opts.getClangCompleteOpts(),
Sam McCall98775c52017-12-04 13:49:59 +0000540 /*OutputIsBinary=*/false),
Sam McCall545a20d2018-01-19 14:34:02 +0000541 CCContext(CodeCompletionContext::CCC_Other), Opts(Opts),
542 CCAllocator(std::make_shared<GlobalCodeCompletionAllocator>()),
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000543 CCTUInfo(CCAllocator), ResultsCallback(std::move(ResultsCallback)) {
544 assert(this->ResultsCallback);
545 }
546
Sam McCall545a20d2018-01-19 14:34:02 +0000547 std::vector<CodeCompletionResult> Results;
548 CodeCompletionContext CCContext;
549 Sema *CCSema = nullptr; // Sema that created the results.
550 // FIXME: Sema is scary. Can we store ASTContext and Preprocessor, instead?
Sam McCall98775c52017-12-04 13:49:59 +0000551
Sam McCall545a20d2018-01-19 14:34:02 +0000552 void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context,
553 CodeCompletionResult *InResults,
Sam McCall98775c52017-12-04 13:49:59 +0000554 unsigned NumResults) override final {
Eric Liu42abe412018-05-24 11:20:19 +0000555 // If a callback is called without any sema result and the context does not
556 // support index-based completion, we simply skip it to give way to
557 // potential future callbacks with results.
558 if (NumResults == 0 && !contextAllowsIndex(Context.getKind()))
559 return;
Ilya Biryukov94da7bd2018-03-16 15:23:44 +0000560 if (CCSema) {
561 log(llvm::formatv(
562 "Multiple code complete callbacks (parser backtracked?). "
563 "Dropping results from context {0}, keeping results from {1}.",
Eric Liu42abe412018-05-24 11:20:19 +0000564 getCompletionKindString(Context.getKind()),
565 getCompletionKindString(this->CCContext.getKind())));
Ilya Biryukov94da7bd2018-03-16 15:23:44 +0000566 return;
567 }
Sam McCall545a20d2018-01-19 14:34:02 +0000568 // Record the completion context.
Sam McCall545a20d2018-01-19 14:34:02 +0000569 CCSema = &S;
570 CCContext = Context;
Eric Liu6f648df2017-12-19 16:50:37 +0000571
Sam McCall545a20d2018-01-19 14:34:02 +0000572 // Retain the results we might want.
Sam McCall98775c52017-12-04 13:49:59 +0000573 for (unsigned I = 0; I < NumResults; ++I) {
Sam McCall545a20d2018-01-19 14:34:02 +0000574 auto &Result = InResults[I];
575 // Drop hidden items which cannot be found by lookup after completion.
576 // Exception: some items can be named by using a qualifier.
Ilya Biryukovf60bf342018-01-10 13:51:09 +0000577 if (Result.Hidden && (!Result.Qualifier || Result.QualifierIsInformative))
578 continue;
Sam McCall545a20d2018-01-19 14:34:02 +0000579 if (!Opts.IncludeIneligibleResults &&
Sam McCall98775c52017-12-04 13:49:59 +0000580 (Result.Availability == CXAvailability_NotAvailable ||
581 Result.Availability == CXAvailability_NotAccessible))
582 continue;
Sam McCall4caa8512018-06-07 12:49:17 +0000583 if (Result.Declaration &&
584 !Context.getBaseType().isNull() // is this a member-access context?
585 && isBlacklistedMember(*Result.Declaration))
Sam McCalld2a95922018-01-22 21:05:00 +0000586 continue;
Ilya Biryukov53d6d932018-03-06 16:45:21 +0000587 // We choose to never append '::' to completion results in clangd.
588 Result.StartsNestedNameSpecifier = false;
Sam McCall545a20d2018-01-19 14:34:02 +0000589 Results.push_back(Result);
Sam McCall98775c52017-12-04 13:49:59 +0000590 }
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000591 ResultsCallback();
Sam McCall98775c52017-12-04 13:49:59 +0000592 }
593
Sam McCall545a20d2018-01-19 14:34:02 +0000594 CodeCompletionAllocator &getAllocator() override { return *CCAllocator; }
Sam McCall98775c52017-12-04 13:49:59 +0000595 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
596
Sam McCall545a20d2018-01-19 14:34:02 +0000597 // Returns the filtering/sorting name for Result, which must be from Results.
598 // Returned string is owned by this recorder (or the AST).
599 llvm::StringRef getName(const CodeCompletionResult &Result) {
Sam McCall98775c52017-12-04 13:49:59 +0000600 switch (Result.Kind) {
601 case CodeCompletionResult::RK_Declaration:
602 if (auto *ID = Result.Declaration->getIdentifier())
Sam McCall545a20d2018-01-19 14:34:02 +0000603 return ID->getName();
Sam McCall98775c52017-12-04 13:49:59 +0000604 break;
605 case CodeCompletionResult::RK_Keyword:
Sam McCall545a20d2018-01-19 14:34:02 +0000606 return Result.Keyword;
Sam McCall98775c52017-12-04 13:49:59 +0000607 case CodeCompletionResult::RK_Macro:
Sam McCall545a20d2018-01-19 14:34:02 +0000608 return Result.Macro->getName();
Sam McCall98775c52017-12-04 13:49:59 +0000609 case CodeCompletionResult::RK_Pattern:
Sam McCall545a20d2018-01-19 14:34:02 +0000610 return Result.Pattern->getTypedText();
Sam McCall98775c52017-12-04 13:49:59 +0000611 }
Ilya Biryukov43714502018-05-16 12:32:44 +0000612 auto *CCS = codeCompletionString(Result);
Sam McCall545a20d2018-01-19 14:34:02 +0000613 return CCS->getTypedText();
Sam McCall98775c52017-12-04 13:49:59 +0000614 }
615
Sam McCall545a20d2018-01-19 14:34:02 +0000616 // Build a CodeCompletion string for R, which must be from Results.
617 // The CCS will be owned by this recorder.
Ilya Biryukov43714502018-05-16 12:32:44 +0000618 CodeCompletionString *codeCompletionString(const CodeCompletionResult &R) {
Sam McCall545a20d2018-01-19 14:34:02 +0000619 // CodeCompletionResult doesn't seem to be const-correct. We own it, anyway.
620 return const_cast<CodeCompletionResult &>(R).CreateCodeCompletionString(
Ilya Biryukov43714502018-05-16 12:32:44 +0000621 *CCSema, CCContext, *CCAllocator, CCTUInfo,
622 /*IncludeBriefComments=*/false);
Sam McCall98775c52017-12-04 13:49:59 +0000623 }
624
Sam McCall545a20d2018-01-19 14:34:02 +0000625private:
626 CodeCompleteOptions Opts;
627 std::shared_ptr<GlobalCodeCompletionAllocator> CCAllocator;
Sam McCall98775c52017-12-04 13:49:59 +0000628 CodeCompletionTUInfo CCTUInfo;
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000629 UniqueFunction<void()> ResultsCallback;
Sam McCall545a20d2018-01-19 14:34:02 +0000630};
631
Sam McCall98775c52017-12-04 13:49:59 +0000632class SignatureHelpCollector final : public CodeCompleteConsumer {
633
634public:
635 SignatureHelpCollector(const clang::CodeCompleteOptions &CodeCompleteOpts,
636 SignatureHelp &SigHelp)
637 : CodeCompleteConsumer(CodeCompleteOpts, /*OutputIsBinary=*/false),
638 SigHelp(SigHelp),
639 Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
640 CCTUInfo(Allocator) {}
641
642 void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
643 OverloadCandidate *Candidates,
644 unsigned NumCandidates) override {
645 SigHelp.signatures.reserve(NumCandidates);
646 // FIXME(rwols): How can we determine the "active overload candidate"?
647 // Right now the overloaded candidates seem to be provided in a "best fit"
648 // order, so I'm not too worried about this.
649 SigHelp.activeSignature = 0;
650 assert(CurrentArg <= (unsigned)std::numeric_limits<int>::max() &&
651 "too many arguments");
652 SigHelp.activeParameter = static_cast<int>(CurrentArg);
653 for (unsigned I = 0; I < NumCandidates; ++I) {
654 const auto &Candidate = Candidates[I];
655 const auto *CCS = Candidate.CreateSignatureString(
656 CurrentArg, S, *Allocator, CCTUInfo, true);
657 assert(CCS && "Expected the CodeCompletionString to be non-null");
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +0000658 // FIXME: for headers, we need to get a comment from the index.
Ilya Biryukov43714502018-05-16 12:32:44 +0000659 SigHelp.signatures.push_back(ProcessOverloadCandidate(
660 Candidate, *CCS,
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +0000661 getParameterDocComment(S.getASTContext(), Candidate, CurrentArg,
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000662 /*CommentsFromHeaders=*/false)));
Sam McCall98775c52017-12-04 13:49:59 +0000663 }
664 }
665
666 GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
667
668 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
669
670private:
Eric Liu63696e12017-12-20 17:24:31 +0000671 // FIXME(ioeric): consider moving CodeCompletionString logic here to
672 // CompletionString.h.
Sam McCall98775c52017-12-04 13:49:59 +0000673 SignatureInformation
674 ProcessOverloadCandidate(const OverloadCandidate &Candidate,
Ilya Biryukov43714502018-05-16 12:32:44 +0000675 const CodeCompletionString &CCS,
676 llvm::StringRef DocComment) const {
Sam McCall98775c52017-12-04 13:49:59 +0000677 SignatureInformation Result;
678 const char *ReturnType = nullptr;
679
Ilya Biryukov43714502018-05-16 12:32:44 +0000680 Result.documentation = formatDocumentation(CCS, DocComment);
Sam McCall98775c52017-12-04 13:49:59 +0000681
682 for (const auto &Chunk : CCS) {
683 switch (Chunk.Kind) {
684 case CodeCompletionString::CK_ResultType:
685 // A piece of text that describes the type of an entity or,
686 // for functions and methods, the return type.
687 assert(!ReturnType && "Unexpected CK_ResultType");
688 ReturnType = Chunk.Text;
689 break;
690 case CodeCompletionString::CK_Placeholder:
691 // A string that acts as a placeholder for, e.g., a function call
692 // argument.
693 // Intentional fallthrough here.
694 case CodeCompletionString::CK_CurrentParameter: {
695 // A piece of text that describes the parameter that corresponds to
696 // the code-completion location within a function call, message send,
697 // macro invocation, etc.
698 Result.label += Chunk.Text;
699 ParameterInformation Info;
700 Info.label = Chunk.Text;
701 Result.parameters.push_back(std::move(Info));
702 break;
703 }
704 case CodeCompletionString::CK_Optional: {
705 // The rest of the parameters are defaulted/optional.
706 assert(Chunk.Optional &&
707 "Expected the optional code completion string to be non-null.");
708 Result.label +=
709 getOptionalParameters(*Chunk.Optional, Result.parameters);
710 break;
711 }
712 case CodeCompletionString::CK_VerticalSpace:
713 break;
714 default:
715 Result.label += Chunk.Text;
716 break;
717 }
718 }
719 if (ReturnType) {
720 Result.label += " -> ";
721 Result.label += ReturnType;
722 }
723 return Result;
724 }
725
726 SignatureHelp &SigHelp;
727 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
728 CodeCompletionTUInfo CCTUInfo;
729
730}; // SignatureHelpCollector
731
Sam McCall545a20d2018-01-19 14:34:02 +0000732struct SemaCompleteInput {
733 PathRef FileName;
734 const tooling::CompileCommand &Command;
735 PrecompiledPreamble const *Preamble;
Eric Liu63f419a2018-05-15 15:29:32 +0000736 const std::vector<Inclusion> &PreambleInclusions;
Sam McCall545a20d2018-01-19 14:34:02 +0000737 StringRef Contents;
738 Position Pos;
739 IntrusiveRefCntPtr<vfs::FileSystem> VFS;
740 std::shared_ptr<PCHContainerOperations> PCHs;
741};
742
743// Invokes Sema code completion on a file.
Eric Liu63f419a2018-05-15 15:29:32 +0000744// If \p Includes is set, it will be initialized after a compiler instance has
745// been set up.
Sam McCalld1a7a372018-01-31 13:40:48 +0000746bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
Sam McCall545a20d2018-01-19 14:34:02 +0000747 const clang::CodeCompleteOptions &Options,
Eric Liu63f419a2018-05-15 15:29:32 +0000748 const SemaCompleteInput &Input,
749 std::unique_ptr<IncludeInserter> *Includes = nullptr) {
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000750 trace::Span Tracer("Sema completion");
Sam McCall98775c52017-12-04 13:49:59 +0000751 std::vector<const char *> ArgStrs;
Sam McCall545a20d2018-01-19 14:34:02 +0000752 for (const auto &S : Input.Command.CommandLine)
Sam McCall98775c52017-12-04 13:49:59 +0000753 ArgStrs.push_back(S.c_str());
754
Ilya Biryukova9cf3112018-02-13 17:15:06 +0000755 if (Input.VFS->setCurrentWorkingDirectory(Input.Command.Directory)) {
756 log("Couldn't set working directory");
757 // We run parsing anyway, our lit-tests rely on results for non-existing
758 // working dirs.
759 }
Sam McCall98775c52017-12-04 13:49:59 +0000760
761 IgnoreDiagnostics DummyDiagsConsumer;
762 auto CI = createInvocationFromCommandLine(
763 ArgStrs,
764 CompilerInstance::createDiagnostics(new DiagnosticOptions,
765 &DummyDiagsConsumer, false),
Sam McCall545a20d2018-01-19 14:34:02 +0000766 Input.VFS);
Ilya Biryukovb6ad25c2018-02-09 13:51:57 +0000767 if (!CI) {
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000768 log("Couldn't create CompilerInvocation");
Ilya Biryukovb6ad25c2018-02-09 13:51:57 +0000769 return false;
770 }
Ilya Biryukov981a35d2018-05-28 12:11:37 +0000771 auto &FrontendOpts = CI->getFrontendOpts();
772 FrontendOpts.DisableFree = false;
Sam McCall98775c52017-12-04 13:49:59 +0000773 FrontendOpts.SkipFunctionBodies = true;
Ilya Biryukov981a35d2018-05-28 12:11:37 +0000774 CI->getLangOpts()->CommentOpts.ParseAllComments = true;
775 // Disable typo correction in Sema.
776 CI->getLangOpts()->SpellChecking = false;
777 // Setup code completion.
Sam McCall98775c52017-12-04 13:49:59 +0000778 FrontendOpts.CodeCompleteOpts = Options;
Sam McCall545a20d2018-01-19 14:34:02 +0000779 FrontendOpts.CodeCompletionAt.FileName = Input.FileName;
Sam McCalla4962cc2018-04-27 11:59:28 +0000780 auto Offset = positionToOffset(Input.Contents, Input.Pos);
781 if (!Offset) {
782 log("Code completion position was invalid " +
783 llvm::toString(Offset.takeError()));
784 return false;
785 }
786 std::tie(FrontendOpts.CodeCompletionAt.Line,
787 FrontendOpts.CodeCompletionAt.Column) =
788 offsetToClangLineColumn(Input.Contents, *Offset);
Sam McCall98775c52017-12-04 13:49:59 +0000789
Ilya Biryukov981a35d2018-05-28 12:11:37 +0000790 std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
791 llvm::MemoryBuffer::getMemBufferCopy(Input.Contents, Input.FileName);
792 // The diagnostic options must be set before creating a CompilerInstance.
793 CI->getDiagnosticOpts().IgnoreWarnings = true;
794 // We reuse the preamble whether it's valid or not. This is a
795 // correctness/performance tradeoff: building without a preamble is slow, and
796 // completion is latency-sensitive.
797 // NOTE: we must call BeginSourceFile after prepareCompilerInstance. Otherwise
798 // the remapped buffers do not get freed.
799 auto Clang = prepareCompilerInstance(
800 std::move(CI), Input.Preamble, std::move(ContentsBuffer),
801 std::move(Input.PCHs), std::move(Input.VFS), DummyDiagsConsumer);
Sam McCall98775c52017-12-04 13:49:59 +0000802 Clang->setCodeCompletionConsumer(Consumer.release());
803
804 SyntaxOnlyAction Action;
805 if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) {
Sam McCalld1a7a372018-01-31 13:40:48 +0000806 log("BeginSourceFile() failed when running codeComplete for " +
807 Input.FileName);
Sam McCall98775c52017-12-04 13:49:59 +0000808 return false;
809 }
Eric Liu63f419a2018-05-15 15:29:32 +0000810 if (Includes) {
811 // Initialize Includes if provided.
812
813 // FIXME(ioeric): needs more consistent style support in clangd server.
814 auto Style = format::getStyle("file", Input.FileName, "LLVM",
815 Input.Contents, Input.VFS.get());
816 if (!Style) {
817 log("Failed to get FormatStyle for file" + Input.FileName +
818 ". Fall back to use LLVM style. Error: " +
819 llvm::toString(Style.takeError()));
820 Style = format::getLLVMStyle();
821 }
822 *Includes = llvm::make_unique<IncludeInserter>(
823 Input.FileName, Input.Contents, *Style, Input.Command.Directory,
824 Clang->getPreprocessor().getHeaderSearchInfo());
825 for (const auto &Inc : Input.PreambleInclusions)
826 Includes->get()->addExisting(Inc);
827 Clang->getPreprocessor().addPPCallbacks(collectInclusionsInMainFileCallback(
828 Clang->getSourceManager(), [Includes](Inclusion Inc) {
829 Includes->get()->addExisting(std::move(Inc));
830 }));
831 }
Sam McCall98775c52017-12-04 13:49:59 +0000832 if (!Action.Execute()) {
Sam McCalld1a7a372018-01-31 13:40:48 +0000833 log("Execute() failed when running codeComplete for " + Input.FileName);
Sam McCall98775c52017-12-04 13:49:59 +0000834 return false;
835 }
Sam McCall98775c52017-12-04 13:49:59 +0000836 Action.EndSourceFile();
837
838 return true;
839}
840
Ilya Biryukova907ba42018-05-14 10:50:04 +0000841// Should we allow index completions in the specified context?
842bool allowIndex(CodeCompletionContext &CC) {
843 if (!contextAllowsIndex(CC.getKind()))
844 return false;
845 // We also avoid ClassName::bar (but allow namespace::bar).
846 auto Scope = CC.getCXXScopeSpecifier();
847 if (!Scope)
848 return true;
849 NestedNameSpecifier *NameSpec = (*Scope)->getScopeRep();
850 if (!NameSpec)
851 return true;
852 // We only query the index when qualifier is a namespace.
853 // If it's a class, we rely solely on sema completions.
854 switch (NameSpec->getKind()) {
855 case NestedNameSpecifier::Global:
856 case NestedNameSpecifier::Namespace:
857 case NestedNameSpecifier::NamespaceAlias:
858 return true;
859 case NestedNameSpecifier::Super:
860 case NestedNameSpecifier::TypeSpec:
861 case NestedNameSpecifier::TypeSpecWithTemplate:
862 // Unresolved inside a template.
863 case NestedNameSpecifier::Identifier:
864 return false;
865 }
Ilya Biryukova6556e22018-05-14 11:47:30 +0000866 llvm_unreachable("invalid NestedNameSpecifier kind");
Ilya Biryukova907ba42018-05-14 10:50:04 +0000867}
868
Sam McCall98775c52017-12-04 13:49:59 +0000869} // namespace
870
871clang::CodeCompleteOptions CodeCompleteOptions::getClangCompleteOpts() const {
872 clang::CodeCompleteOptions Result;
873 Result.IncludeCodePatterns = EnableSnippets && IncludeCodePatterns;
874 Result.IncludeMacros = IncludeMacros;
Sam McCalld8169a82018-01-18 15:31:30 +0000875 Result.IncludeGlobals = true;
Ilya Biryukov43714502018-05-16 12:32:44 +0000876 // We choose to include full comments and not do doxygen parsing in
877 // completion.
878 // FIXME: ideally, we should support doxygen in some form, e.g. do markdown
879 // formatting of the comments.
880 Result.IncludeBriefComments = false;
Sam McCall98775c52017-12-04 13:49:59 +0000881
Sam McCall3d139c52018-01-12 18:30:08 +0000882 // When an is used, Sema is responsible for completing the main file,
883 // the index can provide results from the preamble.
884 // Tell Sema not to deserialize the preamble to look for results.
885 Result.LoadExternal = !Index;
Eric Liu6f648df2017-12-19 16:50:37 +0000886
Sam McCall98775c52017-12-04 13:49:59 +0000887 return Result;
888}
889
Sam McCall545a20d2018-01-19 14:34:02 +0000890// Runs Sema-based (AST) and Index-based completion, returns merged results.
891//
892// There are a few tricky considerations:
893// - the AST provides information needed for the index query (e.g. which
894// namespaces to search in). So Sema must start first.
895// - we only want to return the top results (Opts.Limit).
896// Building CompletionItems for everything else is wasteful, so we want to
897// preserve the "native" format until we're done with scoring.
898// - the data underlying Sema completion items is owned by the AST and various
899// other arenas, which must stay alive for us to build CompletionItems.
900// - we may get duplicate results from Sema and the Index, we need to merge.
901//
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000902// So we start Sema completion first, and do all our work in its callback.
Sam McCall545a20d2018-01-19 14:34:02 +0000903// We use the Sema context information to query the index.
904// Then we merge the two result sets, producing items that are Sema/Index/Both.
905// These items are scored, and the top N are synthesized into the LSP response.
906// Finally, we can clean up the data structures created by Sema completion.
907//
908// Main collaborators are:
909// - semaCodeComplete sets up the compiler machinery to run code completion.
910// - CompletionRecorder captures Sema completion results, including context.
911// - SymbolIndex (Opts.Index) provides index completion results as Symbols
912// - CompletionCandidates are the result of merging Sema and Index results.
913// Each candidate points to an underlying CodeCompletionResult (Sema), a
914// Symbol (Index), or both. It computes the result quality score.
915// CompletionCandidate also does conversion to CompletionItem (at the end).
916// - FuzzyMatcher scores how the candidate matches the partial identifier.
917// This score is combined with the result quality score for the final score.
918// - TopN determines the results with the best score.
919class CodeCompleteFlow {
Eric Liuc5105f92018-02-16 14:15:55 +0000920 PathRef FileName;
Sam McCall545a20d2018-01-19 14:34:02 +0000921 const CodeCompleteOptions &Opts;
922 // Sema takes ownership of Recorder. Recorder is valid until Sema cleanup.
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000923 CompletionRecorder *Recorder = nullptr;
Sam McCall545a20d2018-01-19 14:34:02 +0000924 int NSema = 0, NIndex = 0, NBoth = 0; // Counters for logging.
925 bool Incomplete = false; // Would more be available with a higher limit?
Eric Liu63f419a2018-05-15 15:29:32 +0000926 llvm::Optional<FuzzyMatcher> Filter; // Initialized once Sema runs.
927 std::unique_ptr<IncludeInserter> Includes; // Initialized once compiler runs.
Eric Liu09c3c372018-06-15 08:58:12 +0000928 FileProximityMatcher FileProximityMatch;
Sam McCall545a20d2018-01-19 14:34:02 +0000929
930public:
931 // A CodeCompleteFlow object is only useful for calling run() exactly once.
Eric Liuc5105f92018-02-16 14:15:55 +0000932 CodeCompleteFlow(PathRef FileName, const CodeCompleteOptions &Opts)
Eric Liu09c3c372018-06-15 08:58:12 +0000933 : FileName(FileName), Opts(Opts),
934 // FIXME: also use path of the main header corresponding to FileName to
935 // calculate the file proximity, which would capture include/ and src/
936 // project setup where headers and implementations are not in the same
937 // directory.
Haojian Wu7943d4f2018-06-15 09:32:36 +0000938 FileProximityMatch(ArrayRef<StringRef>({FileName})) {}
Sam McCall545a20d2018-01-19 14:34:02 +0000939
940 CompletionList run(const SemaCompleteInput &SemaCCInput) && {
Sam McCalld1a7a372018-01-31 13:40:48 +0000941 trace::Span Tracer("CodeCompleteFlow");
Eric Liu63f419a2018-05-15 15:29:32 +0000942
Sam McCall545a20d2018-01-19 14:34:02 +0000943 // We run Sema code completion first. It builds an AST and calculates:
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000944 // - completion results based on the AST.
Sam McCall545a20d2018-01-19 14:34:02 +0000945 // - partial identifier and context. We need these for the index query.
946 CompletionList Output;
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000947 auto RecorderOwner = llvm::make_unique<CompletionRecorder>(Opts, [&]() {
948 assert(Recorder && "Recorder is not set");
Eric Liu63f419a2018-05-15 15:29:32 +0000949 assert(Includes && "Includes is not set");
950 // If preprocessor was run, inclusions from preprocessor callback should
951 // already be added to Inclusions.
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000952 Output = runWithSema();
Eric Liu63f419a2018-05-15 15:29:32 +0000953 Includes.reset(); // Make sure this doesn't out-live Clang.
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000954 SPAN_ATTACH(Tracer, "sema_completion_kind",
955 getCompletionKindString(Recorder->CCContext.getKind()));
956 });
957
958 Recorder = RecorderOwner.get();
Sam McCalld1a7a372018-01-31 13:40:48 +0000959 semaCodeComplete(std::move(RecorderOwner), Opts.getClangCompleteOpts(),
Eric Liu63f419a2018-05-15 15:29:32 +0000960 SemaCCInput, &Includes);
Sam McCall545a20d2018-01-19 14:34:02 +0000961
Sam McCall2b780162018-01-30 17:20:54 +0000962 SPAN_ATTACH(Tracer, "sema_results", NSema);
963 SPAN_ATTACH(Tracer, "index_results", NIndex);
964 SPAN_ATTACH(Tracer, "merged_results", NBoth);
965 SPAN_ATTACH(Tracer, "returned_results", Output.items.size());
966 SPAN_ATTACH(Tracer, "incomplete", Output.isIncomplete);
Sam McCall0f8df3e2018-06-13 11:31:20 +0000967 log(llvm::formatv("Code complete: {0} results from Sema, {1} from Index, "
968 "{2} matched, {3} returned{4}.",
969 NSema, NIndex, NBoth, Output.items.size(),
970 Output.isIncomplete ? " (incomplete)" : ""));
Sam McCall545a20d2018-01-19 14:34:02 +0000971 assert(!Opts.Limit || Output.items.size() <= Opts.Limit);
972 // We don't assert that isIncomplete means we hit a limit.
973 // Indexes may choose to impose their own limits even if we don't have one.
974 return Output;
975 }
976
977private:
978 // This is called by run() once Sema code completion is done, but before the
979 // Sema data structures are torn down. It does all the real work.
980 CompletionList runWithSema() {
981 Filter = FuzzyMatcher(
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000982 Recorder->CCSema->getPreprocessor().getCodeCompletionFilter());
Sam McCall545a20d2018-01-19 14:34:02 +0000983 // Sema provides the needed context to query the index.
984 // FIXME: in addition to querying for extra/overlapping symbols, we should
985 // explicitly request symbols corresponding to Sema results.
986 // We can use their signals even if the index can't suggest them.
987 // We must copy index results to preserve them, but there are at most Limit.
Eric Liu8f3678d2018-06-15 13:34:18 +0000988 auto IndexResults = (Opts.Index && allowIndex(Recorder->CCContext))
989 ? queryIndex()
990 : SymbolSlab();
Sam McCall545a20d2018-01-19 14:34:02 +0000991 // Merge Sema and Index results, score them, and pick the winners.
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000992 auto Top = mergeResults(Recorder->Results, IndexResults);
Sam McCall545a20d2018-01-19 14:34:02 +0000993 // Convert the results to the desired LSP structs.
994 CompletionList Output;
995 for (auto &C : Top)
996 Output.items.push_back(toCompletionItem(C.first, C.second));
997 Output.isIncomplete = Incomplete;
998 return Output;
999 }
1000
1001 SymbolSlab queryIndex() {
Sam McCalld1a7a372018-01-31 13:40:48 +00001002 trace::Span Tracer("Query index");
Sam McCall2b780162018-01-30 17:20:54 +00001003 SPAN_ATTACH(Tracer, "limit", Opts.Limit);
1004
Sam McCall545a20d2018-01-19 14:34:02 +00001005 SymbolSlab::Builder ResultsBuilder;
1006 // Build the query.
1007 FuzzyFindRequest Req;
Haojian Wu48b48652018-01-25 09:20:09 +00001008 if (Opts.Limit)
1009 Req.MaxCandidateCount = Opts.Limit;
Sam McCall545a20d2018-01-19 14:34:02 +00001010 Req.Query = Filter->pattern();
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +00001011 Req.RestrictForCodeCompletion = true;
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001012 Req.Scopes = getQueryScopes(Recorder->CCContext,
1013 Recorder->CCSema->getSourceManager());
Eric Liu6de95ec2018-06-12 08:48:20 +00001014 Req.ProximityPaths.push_back(FileName);
Sam McCalld1a7a372018-01-31 13:40:48 +00001015 log(llvm::formatv("Code complete: fuzzyFind(\"{0}\", scopes=[{1}])",
Sam McCall2b780162018-01-30 17:20:54 +00001016 Req.Query,
1017 llvm::join(Req.Scopes.begin(), Req.Scopes.end(), ",")));
Sam McCall545a20d2018-01-19 14:34:02 +00001018 // Run the query against the index.
Sam McCallab8e3932018-02-19 13:04:41 +00001019 if (Opts.Index->fuzzyFind(
1020 Req, [&](const Symbol &Sym) { ResultsBuilder.insert(Sym); }))
1021 Incomplete = true;
Sam McCall545a20d2018-01-19 14:34:02 +00001022 return std::move(ResultsBuilder).build();
1023 }
1024
Sam McCallc18c2802018-06-15 11:06:29 +00001025 // Merges Sema and Index results where possible, to form CompletionCandidates.
1026 // Groups overloads if desired, to form CompletionCandidate::Bundles.
1027 // The bundles are scored and top results are returned, best to worst.
1028 std::vector<ScoredBundle>
Sam McCall545a20d2018-01-19 14:34:02 +00001029 mergeResults(const std::vector<CodeCompletionResult> &SemaResults,
1030 const SymbolSlab &IndexResults) {
Sam McCalld1a7a372018-01-31 13:40:48 +00001031 trace::Span Tracer("Merge and score results");
Sam McCallc18c2802018-06-15 11:06:29 +00001032 std::vector<CompletionCandidate::Bundle> Bundles;
1033 llvm::DenseMap<size_t, size_t> BundleLookup;
1034 auto AddToBundles = [&](const CodeCompletionResult *SemaResult,
1035 const Symbol *IndexResult) {
1036 CompletionCandidate C;
1037 C.SemaResult = SemaResult;
1038 C.IndexResult = IndexResult;
1039 C.Name = IndexResult ? IndexResult->Name : Recorder->getName(*SemaResult);
1040 if (auto OverloadSet = Opts.BundleOverloads ? C.overloadSet() : 0) {
1041 auto Ret = BundleLookup.try_emplace(OverloadSet, Bundles.size());
1042 if (Ret.second)
1043 Bundles.emplace_back();
1044 Bundles[Ret.first->second].push_back(std::move(C));
1045 } else {
1046 Bundles.emplace_back();
1047 Bundles.back().push_back(std::move(C));
1048 }
1049 };
Sam McCall545a20d2018-01-19 14:34:02 +00001050 llvm::DenseSet<const Symbol *> UsedIndexResults;
1051 auto CorrespondingIndexResult =
1052 [&](const CodeCompletionResult &SemaResult) -> const Symbol * {
1053 if (auto SymID = getSymbolID(SemaResult)) {
1054 auto I = IndexResults.find(*SymID);
1055 if (I != IndexResults.end()) {
1056 UsedIndexResults.insert(&*I);
1057 return &*I;
1058 }
1059 }
1060 return nullptr;
1061 };
1062 // Emit all Sema results, merging them with Index results if possible.
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001063 for (auto &SemaResult : Recorder->Results)
Sam McCallc18c2802018-06-15 11:06:29 +00001064 AddToBundles(&SemaResult, CorrespondingIndexResult(SemaResult));
Sam McCall545a20d2018-01-19 14:34:02 +00001065 // Now emit any Index-only results.
1066 for (const auto &IndexResult : IndexResults) {
1067 if (UsedIndexResults.count(&IndexResult))
1068 continue;
Sam McCallc18c2802018-06-15 11:06:29 +00001069 AddToBundles(/*SemaResult=*/nullptr, &IndexResult);
Sam McCall545a20d2018-01-19 14:34:02 +00001070 }
Sam McCallc18c2802018-06-15 11:06:29 +00001071 // We only keep the best N results at any time, in "native" format.
1072 TopN<ScoredBundle, ScoredBundleGreater> Top(
1073 Opts.Limit == 0 ? std::numeric_limits<size_t>::max() : Opts.Limit);
1074 for (auto &Bundle : Bundles)
1075 addCandidate(Top, std::move(Bundle));
Sam McCall545a20d2018-01-19 14:34:02 +00001076 return std::move(Top).items();
1077 }
1078
Sam McCall80ad7072018-06-08 13:32:25 +00001079 Optional<float> fuzzyScore(const CompletionCandidate &C) {
1080 // Macros can be very spammy, so we only support prefix completion.
1081 // We won't end up with underfull index results, as macros are sema-only.
1082 if (C.SemaResult && C.SemaResult->Kind == CodeCompletionResult::RK_Macro &&
1083 !C.Name.startswith_lower(Filter->pattern()))
1084 return None;
1085 return Filter->match(C.Name);
1086 }
1087
Sam McCall545a20d2018-01-19 14:34:02 +00001088 // Scores a candidate and adds it to the TopN structure.
Sam McCallc18c2802018-06-15 11:06:29 +00001089 void addCandidate(TopN<ScoredBundle, ScoredBundleGreater> &Candidates,
1090 CompletionCandidate::Bundle Bundle) {
Sam McCallc5707b62018-05-15 17:43:27 +00001091 SymbolQualitySignals Quality;
1092 SymbolRelevanceSignals Relevance;
Sam McCalld9b54f02018-06-05 16:30:25 +00001093 Relevance.Query = SymbolRelevanceSignals::CodeComplete;
Eric Liu09c3c372018-06-15 08:58:12 +00001094 Relevance.FileProximityMatch = &FileProximityMatch;
Sam McCallc18c2802018-06-15 11:06:29 +00001095 auto &First = Bundle.front();
1096 if (auto FuzzyScore = fuzzyScore(First))
Sam McCallc5707b62018-05-15 17:43:27 +00001097 Relevance.NameMatch = *FuzzyScore;
Sam McCall545a20d2018-01-19 14:34:02 +00001098 else
1099 return;
Sam McCallc18c2802018-06-15 11:06:29 +00001100 unsigned SemaResult = 0, IndexResult = 0;
1101 for (const auto &Candidate : Bundle) {
1102 if (Candidate.IndexResult) {
1103 Quality.merge(*Candidate.IndexResult);
1104 Relevance.merge(*Candidate.IndexResult);
1105 ++IndexResult;
1106 }
1107 if (Candidate.SemaResult) {
1108 Quality.merge(*Candidate.SemaResult);
1109 Relevance.merge(*Candidate.SemaResult);
1110 ++SemaResult;
1111 }
Sam McCallc5707b62018-05-15 17:43:27 +00001112 }
1113
1114 float QualScore = Quality.evaluate(), RelScore = Relevance.evaluate();
1115 CompletionItemScores Scores;
1116 Scores.finalScore = evaluateSymbolAndRelevance(QualScore, RelScore);
1117 // The purpose of exporting component scores is to allow NameMatch to be
1118 // replaced on the client-side. So we export (NameMatch, final/NameMatch)
1119 // rather than (RelScore, QualScore).
1120 Scores.filterScore = Relevance.NameMatch;
1121 Scores.symbolScore =
1122 Scores.filterScore ? Scores.finalScore / Scores.filterScore : QualScore;
1123
Sam McCallc18c2802018-06-15 11:06:29 +00001124 LLVM_DEBUG(llvm::dbgs() << "CodeComplete: " << First.Name << "("
1125 << IndexResult << " index) "
1126 << "(" << SemaResult << " sema)"
1127 << " = " << Scores.finalScore << "\n"
1128 << Quality << Relevance << "\n");
Sam McCall545a20d2018-01-19 14:34:02 +00001129
1130 NSema += bool(SemaResult);
1131 NIndex += bool(IndexResult);
1132 NBoth += SemaResult && IndexResult;
Sam McCallc18c2802018-06-15 11:06:29 +00001133 if (Candidates.push({std::move(Bundle), Scores}))
Sam McCallab8e3932018-02-19 13:04:41 +00001134 Incomplete = true;
Sam McCall545a20d2018-01-19 14:34:02 +00001135 }
1136
Sam McCallc18c2802018-06-15 11:06:29 +00001137 CompletionItem toCompletionItem(const CompletionCandidate::Bundle &Bundle,
Sam McCall545a20d2018-01-19 14:34:02 +00001138 const CompletionItemScores &Scores) {
1139 CodeCompletionString *SemaCCS = nullptr;
Sam McCallc18c2802018-06-15 11:06:29 +00001140 std::string FrontDocComment;
1141 if (auto *SR = Bundle.front().SemaResult) {
Ilya Biryukov43714502018-05-16 12:32:44 +00001142 SemaCCS = Recorder->codeCompletionString(*SR);
1143 if (Opts.IncludeComments) {
1144 assert(Recorder->CCSema);
Sam McCallc18c2802018-06-15 11:06:29 +00001145 FrontDocComment = getDocComment(Recorder->CCSema->getASTContext(), *SR,
1146 /*CommentsFromHeader=*/false);
Ilya Biryukov43714502018-05-16 12:32:44 +00001147 }
1148 }
Sam McCallc18c2802018-06-15 11:06:29 +00001149 return CompletionCandidate::build(
1150 Bundle,
Eric Liu8f3678d2018-06-15 13:34:18 +00001151 Bundle.front().build(FileName, Scores, Opts, SemaCCS, *Includes,
Sam McCallc18c2802018-06-15 11:06:29 +00001152 FrontDocComment),
1153 Opts);
Sam McCall545a20d2018-01-19 14:34:02 +00001154 }
1155};
1156
Sam McCalld1a7a372018-01-31 13:40:48 +00001157CompletionList codeComplete(PathRef FileName,
Sam McCall98775c52017-12-04 13:49:59 +00001158 const tooling::CompileCommand &Command,
1159 PrecompiledPreamble const *Preamble,
Eric Liu63f419a2018-05-15 15:29:32 +00001160 const std::vector<Inclusion> &PreambleInclusions,
Sam McCall98775c52017-12-04 13:49:59 +00001161 StringRef Contents, Position Pos,
1162 IntrusiveRefCntPtr<vfs::FileSystem> VFS,
1163 std::shared_ptr<PCHContainerOperations> PCHs,
Ilya Biryukov940901e2017-12-13 12:51:22 +00001164 CodeCompleteOptions Opts) {
Eric Liuc5105f92018-02-16 14:15:55 +00001165 return CodeCompleteFlow(FileName, Opts)
Eric Liu63f419a2018-05-15 15:29:32 +00001166 .run({FileName, Command, Preamble, PreambleInclusions, Contents, Pos, VFS,
1167 PCHs});
Sam McCall98775c52017-12-04 13:49:59 +00001168}
1169
Sam McCalld1a7a372018-01-31 13:40:48 +00001170SignatureHelp signatureHelp(PathRef FileName,
Ilya Biryukov940901e2017-12-13 12:51:22 +00001171 const tooling::CompileCommand &Command,
1172 PrecompiledPreamble const *Preamble,
1173 StringRef Contents, Position Pos,
1174 IntrusiveRefCntPtr<vfs::FileSystem> VFS,
1175 std::shared_ptr<PCHContainerOperations> PCHs) {
Sam McCall98775c52017-12-04 13:49:59 +00001176 SignatureHelp Result;
1177 clang::CodeCompleteOptions Options;
1178 Options.IncludeGlobals = false;
1179 Options.IncludeMacros = false;
1180 Options.IncludeCodePatterns = false;
Ilya Biryukov43714502018-05-16 12:32:44 +00001181 Options.IncludeBriefComments = false;
Eric Liu4c0a27b2018-05-16 20:31:38 +00001182 std::vector<Inclusion> PreambleInclusions = {}; // Unused for signatureHelp
Sam McCalld1a7a372018-01-31 13:40:48 +00001183 semaCodeComplete(llvm::make_unique<SignatureHelpCollector>(Options, Result),
1184 Options,
Eric Liu4c0a27b2018-05-16 20:31:38 +00001185 {FileName, Command, Preamble, PreambleInclusions, Contents,
1186 Pos, std::move(VFS), std::move(PCHs)});
Sam McCall98775c52017-12-04 13:49:59 +00001187 return Result;
1188}
1189
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +00001190bool isIndexedForCodeCompletion(const NamedDecl &ND, ASTContext &ASTCtx) {
1191 using namespace clang::ast_matchers;
1192 auto InTopLevelScope = hasDeclContext(
1193 anyOf(namespaceDecl(), translationUnitDecl(), linkageSpecDecl()));
1194 return !match(decl(anyOf(InTopLevelScope,
1195 hasDeclContext(
1196 enumDecl(InTopLevelScope, unless(isScoped()))))),
1197 ND, ASTCtx)
1198 .empty();
1199}
1200
Sam McCall98775c52017-12-04 13:49:59 +00001201} // namespace clangd
1202} // namespace clang