blob: 12bed7a01ba4b80a14a3a732380b335bf5e27525 [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 McCalla68951e2018-06-22 16:11:35 +0000252 llvm::StringRef Name(SemaCCS->getTypedText());
253 std::string Signature, SnippetSuffix, Qualifiers;
254 getSignature(*SemaCCS, &Signature, &SnippetSuffix, &Qualifiers);
255 I.label = (Qualifiers + Name + Signature).str();
256 I.filterText = Name;
257 I.insertText = (Qualifiers + Name).str();
258 if (Opts.EnableSnippets)
259 I.insertText += SnippetSuffix;
Ilya Biryukov43714502018-05-16 12:32:44 +0000260 I.documentation = formatDocumentation(*SemaCCS, SemaDocComment);
Sam McCalla68951e2018-06-22 16:11:35 +0000261 I.detail = getReturnType(*SemaCCS);
Eric Liu7ad16962018-06-22 10:46:59 +0000262 if (SemaResult->Kind == CodeCompletionResult::RK_Declaration)
263 if (const auto *D = SemaResult->getDeclaration())
264 if (const auto *ND = llvm::dyn_cast<NamedDecl>(D))
265 I.SymbolScope = splitQualifiedName(printQualifiedName(*ND)).first;
Sam McCalla68951e2018-06-22 16:11:35 +0000266 I.kind = toCompletionItemKind(SemaResult->Kind, SemaResult->Declaration);
Sam McCall545a20d2018-01-19 14:34:02 +0000267 }
268 if (IndexResult) {
Eric Liu7ad16962018-06-22 10:46:59 +0000269 if (I.SymbolScope.empty())
270 I.SymbolScope = IndexResult->Scope;
Sam McCall545a20d2018-01-19 14:34:02 +0000271 if (I.kind == CompletionItemKind::Missing)
272 I.kind = toCompletionItemKind(IndexResult->SymInfo.Kind);
273 // FIXME: reintroduce a way to show the index source for debugging.
274 if (I.label.empty())
Sam McCalla68951e2018-06-22 16:11:35 +0000275 I.label = (IndexResult->Name + IndexResult->Signature).str();
Sam McCall545a20d2018-01-19 14:34:02 +0000276 if (I.filterText.empty())
277 I.filterText = IndexResult->Name;
Sam McCall98775c52017-12-04 13:49:59 +0000278
Sam McCall545a20d2018-01-19 14:34:02 +0000279 // FIXME(ioeric): support inserting/replacing scope qualifiers.
Sam McCalla68951e2018-06-22 16:11:35 +0000280 if (I.insertText.empty()) {
281 I.insertText = IndexResult->Name;
282 if (Opts.EnableSnippets)
283 I.insertText += IndexResult->CompletionSnippetSuffix;
284 }
Sam McCall545a20d2018-01-19 14:34:02 +0000285
286 if (auto *D = IndexResult->Detail) {
287 if (I.documentation.empty())
288 I.documentation = D->Documentation;
289 if (I.detail.empty())
Sam McCalla68951e2018-06-22 16:11:35 +0000290 I.detail = D->ReturnType;
Sam McCallc18c2802018-06-15 11:06:29 +0000291 if (auto Inserted = headerToInsertIfNotPresent()) {
Eric Liu8f3678d2018-06-15 13:34:18 +0000292 auto IncludePath = [&]() -> Expected<std::string> {
Eric Liu63f419a2018-05-15 15:29:32 +0000293 auto ResolvedDeclaring = toHeaderFile(
294 IndexResult->CanonicalDeclaration.FileURI, FileName);
295 if (!ResolvedDeclaring)
296 return ResolvedDeclaring.takeError();
Sam McCallc18c2802018-06-15 11:06:29 +0000297 auto ResolvedInserted = toHeaderFile(*Inserted, FileName);
Eric Liu63f419a2018-05-15 15:29:32 +0000298 if (!ResolvedInserted)
299 return ResolvedInserted.takeError();
Eric Liu8f3678d2018-06-15 13:34:18 +0000300 if (!Includes.shouldInsertInclude(*ResolvedDeclaring,
301 *ResolvedInserted))
302 return "";
303 return Includes.calculateIncludePath(*ResolvedDeclaring,
304 *ResolvedInserted);
Eric Liu63f419a2018-05-15 15:29:32 +0000305 }();
Eric Liu8f3678d2018-06-15 13:34:18 +0000306 if (!IncludePath) {
Eric Liu63f419a2018-05-15 15:29:32 +0000307 std::string ErrMsg =
308 ("Failed to generate include insertion edits for adding header "
309 "(FileURI=\"" +
310 IndexResult->CanonicalDeclaration.FileURI +
311 "\", IncludeHeader=\"" + D->IncludeHeader + "\") into " +
312 FileName)
313 .str();
Eric Liu8f3678d2018-06-15 13:34:18 +0000314 log(ErrMsg + ":" + llvm::toString(IncludePath.takeError()));
315 } else if (!IncludePath->empty()) {
316 // FIXME: consider what to show when there is no #include insertion,
317 // and for sema results, for consistency.
318 if (auto Edit = Includes.insert(*IncludePath)) {
319 I.detail += ("\n" + StringRef(*IncludePath).trim('"')).str();
320 I.additionalTextEdits = {std::move(*Edit)};
321 InsertingInclude = true;
322 }
Eric Liu63f419a2018-05-15 15:29:32 +0000323 }
324 }
Sam McCall545a20d2018-01-19 14:34:02 +0000325 }
326 }
Eric Liu8f3678d2018-06-15 13:34:18 +0000327 I.label = (InsertingInclude ? Opts.IncludeIndicator.Insert
328 : Opts.IncludeIndicator.NoInsert) +
329 I.label;
Sam McCall545a20d2018-01-19 14:34:02 +0000330 I.scoreInfo = Scores;
331 I.sortText = sortText(Scores.finalScore, Name);
332 I.insertTextFormat = Opts.EnableSnippets ? InsertTextFormat::Snippet
333 : InsertTextFormat::PlainText;
334 return I;
Sam McCall98775c52017-12-04 13:49:59 +0000335 }
Sam McCallc18c2802018-06-15 11:06:29 +0000336
337 using Bundle = llvm::SmallVector<CompletionCandidate, 4>;
338
339 static CompletionItem build(const Bundle &Bundle, CompletionItem First,
340 const clangd::CodeCompleteOptions &Opts) {
341 if (Bundle.size() == 1)
342 return First;
343 // Patch up the completion item to make it look like a bundle.
344 // This is a bit of a hack but most things are the same.
345
346 // Need to erase the signature. All bundles are function calls.
347 llvm::StringRef Name = Bundle.front().Name;
348 First.insertText =
349 Opts.EnableSnippets ? (Name + "(${0})").str() : Name.str();
Eric Liu8f3678d2018-06-15 13:34:18 +0000350 // Keep the visual indicator of the original label.
351 bool InsertingInclude =
352 StringRef(First.label).startswith(Opts.IncludeIndicator.Insert);
353 First.label = (Twine(InsertingInclude ? Opts.IncludeIndicator.Insert
354 : Opts.IncludeIndicator.NoInsert) +
355 Name + "(…)")
356 .str();
Sam McCallc18c2802018-06-15 11:06:29 +0000357 First.detail = llvm::formatv("[{0} overloads]", Bundle.size());
358 return First;
359 }
Sam McCall98775c52017-12-04 13:49:59 +0000360};
Sam McCallc18c2802018-06-15 11:06:29 +0000361using ScoredBundle =
362 std::pair<CompletionCandidate::Bundle, CompletionItemScores>;
363struct ScoredBundleGreater {
364 bool operator()(const ScoredBundle &L, const ScoredBundle &R) {
365 if (L.second.finalScore != R.second.finalScore)
366 return L.second.finalScore > R.second.finalScore;
367 return L.first.front().Name <
368 R.first.front().Name; // Earlier name is better.
369 }
370};
Sam McCall98775c52017-12-04 13:49:59 +0000371
Sam McCall545a20d2018-01-19 14:34:02 +0000372// Determine the symbol ID for a Sema code completion result, if possible.
373llvm::Optional<SymbolID> getSymbolID(const CodeCompletionResult &R) {
374 switch (R.Kind) {
375 case CodeCompletionResult::RK_Declaration:
376 case CodeCompletionResult::RK_Pattern: {
377 llvm::SmallString<128> USR;
378 if (/*Ignore=*/clang::index::generateUSRForDecl(R.Declaration, USR))
379 return None;
380 return SymbolID(USR);
381 }
382 case CodeCompletionResult::RK_Macro:
383 // FIXME: Macros do have USRs, but the CCR doesn't contain enough info.
384 case CodeCompletionResult::RK_Keyword:
385 return None;
386 }
387 llvm_unreachable("unknown CodeCompletionResult kind");
388}
389
Haojian Wu061c73e2018-01-23 11:37:26 +0000390// Scopes of the paritial identifier we're trying to complete.
391// It is used when we query the index for more completion results.
Eric Liu6f648df2017-12-19 16:50:37 +0000392struct SpecifiedScope {
Haojian Wu061c73e2018-01-23 11:37:26 +0000393 // The scopes we should look in, determined by Sema.
394 //
395 // If the qualifier was fully resolved, we look for completions in these
396 // scopes; if there is an unresolved part of the qualifier, it should be
397 // resolved within these scopes.
398 //
399 // Examples of qualified completion:
400 //
401 // "::vec" => {""}
402 // "using namespace std; ::vec^" => {"", "std::"}
403 // "namespace ns {using namespace std;} ns::^" => {"ns::", "std::"}
404 // "std::vec^" => {""} // "std" unresolved
405 //
406 // Examples of unqualified completion:
407 //
408 // "vec^" => {""}
409 // "using namespace std; vec^" => {"", "std::"}
410 // "using namespace std; namespace ns { vec^ }" => {"ns::", "std::", ""}
411 //
412 // "" for global namespace, "ns::" for normal namespace.
413 std::vector<std::string> AccessibleScopes;
414 // The full scope qualifier as typed by the user (without the leading "::").
415 // Set if the qualifier is not fully resolved by Sema.
416 llvm::Optional<std::string> UnresolvedQualifier;
Sam McCall545a20d2018-01-19 14:34:02 +0000417
Haojian Wu061c73e2018-01-23 11:37:26 +0000418 // Construct scopes being queried in indexes.
419 // This method format the scopes to match the index request representation.
420 std::vector<std::string> scopesForIndexQuery() {
421 std::vector<std::string> Results;
422 for (llvm::StringRef AS : AccessibleScopes) {
423 Results.push_back(AS);
424 if (UnresolvedQualifier)
425 Results.back() += *UnresolvedQualifier;
426 }
427 return Results;
Sam McCall545a20d2018-01-19 14:34:02 +0000428 }
Eric Liu6f648df2017-12-19 16:50:37 +0000429};
430
Haojian Wu061c73e2018-01-23 11:37:26 +0000431// Get all scopes that will be queried in indexes.
432std::vector<std::string> getQueryScopes(CodeCompletionContext &CCContext,
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000433 const SourceManager &SM) {
434 auto GetAllAccessibleScopes = [](CodeCompletionContext &CCContext) {
Haojian Wu061c73e2018-01-23 11:37:26 +0000435 SpecifiedScope Info;
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000436 for (auto *Context : CCContext.getVisitedContexts()) {
Haojian Wu061c73e2018-01-23 11:37:26 +0000437 if (isa<TranslationUnitDecl>(Context))
438 Info.AccessibleScopes.push_back(""); // global namespace
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000439 else if (const auto *NS = dyn_cast<NamespaceDecl>(Context))
Haojian Wu061c73e2018-01-23 11:37:26 +0000440 Info.AccessibleScopes.push_back(NS->getQualifiedNameAsString() + "::");
441 }
442 return Info;
443 };
444
445 auto SS = CCContext.getCXXScopeSpecifier();
446
447 // Unqualified completion (e.g. "vec^").
448 if (!SS) {
449 // FIXME: Once we can insert namespace qualifiers and use the in-scope
450 // namespaces for scoring, search in all namespaces.
451 // FIXME: Capture scopes and use for scoring, for example,
452 // "using namespace std; namespace foo {v^}" =>
453 // foo::value > std::vector > boost::variant
454 return GetAllAccessibleScopes(CCContext).scopesForIndexQuery();
455 }
456
457 // Qualified completion ("std::vec^"), we have two cases depending on whether
458 // the qualifier can be resolved by Sema.
459 if ((*SS)->isValid()) { // Resolved qualifier.
Haojian Wu061c73e2018-01-23 11:37:26 +0000460 return GetAllAccessibleScopes(CCContext).scopesForIndexQuery();
461 }
462
463 // Unresolved qualifier.
464 // FIXME: When Sema can resolve part of a scope chain (e.g.
465 // "known::unknown::id"), we should expand the known part ("known::") rather
466 // than treating the whole thing as unknown.
467 SpecifiedScope Info;
468 Info.AccessibleScopes.push_back(""); // global namespace
469
470 Info.UnresolvedQualifier =
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000471 Lexer::getSourceText(CharSourceRange::getCharRange((*SS)->getRange()), SM,
472 clang::LangOptions())
473 .ltrim("::");
Haojian Wu061c73e2018-01-23 11:37:26 +0000474 // Sema excludes the trailing "::".
475 if (!Info.UnresolvedQualifier->empty())
476 *Info.UnresolvedQualifier += "::";
477
478 return Info.scopesForIndexQuery();
479}
480
Eric Liu42abe412018-05-24 11:20:19 +0000481// Should we perform index-based completion in a context of the specified kind?
482// FIXME: consider allowing completion, but restricting the result types.
483bool contextAllowsIndex(enum CodeCompletionContext::Kind K) {
484 switch (K) {
485 case CodeCompletionContext::CCC_TopLevel:
486 case CodeCompletionContext::CCC_ObjCInterface:
487 case CodeCompletionContext::CCC_ObjCImplementation:
488 case CodeCompletionContext::CCC_ObjCIvarList:
489 case CodeCompletionContext::CCC_ClassStructUnion:
490 case CodeCompletionContext::CCC_Statement:
491 case CodeCompletionContext::CCC_Expression:
492 case CodeCompletionContext::CCC_ObjCMessageReceiver:
493 case CodeCompletionContext::CCC_EnumTag:
494 case CodeCompletionContext::CCC_UnionTag:
495 case CodeCompletionContext::CCC_ClassOrStructTag:
496 case CodeCompletionContext::CCC_ObjCProtocolName:
497 case CodeCompletionContext::CCC_Namespace:
498 case CodeCompletionContext::CCC_Type:
499 case CodeCompletionContext::CCC_Name: // FIXME: why does ns::^ give this?
500 case CodeCompletionContext::CCC_PotentiallyQualifiedName:
501 case CodeCompletionContext::CCC_ParenthesizedExpression:
502 case CodeCompletionContext::CCC_ObjCInterfaceName:
503 case CodeCompletionContext::CCC_ObjCCategoryName:
504 return true;
505 case CodeCompletionContext::CCC_Other: // Be conservative.
506 case CodeCompletionContext::CCC_OtherWithMacros:
507 case CodeCompletionContext::CCC_DotMemberAccess:
508 case CodeCompletionContext::CCC_ArrowMemberAccess:
509 case CodeCompletionContext::CCC_ObjCPropertyAccess:
510 case CodeCompletionContext::CCC_MacroName:
511 case CodeCompletionContext::CCC_MacroNameUse:
512 case CodeCompletionContext::CCC_PreprocessorExpression:
513 case CodeCompletionContext::CCC_PreprocessorDirective:
514 case CodeCompletionContext::CCC_NaturalLanguage:
515 case CodeCompletionContext::CCC_SelectorName:
516 case CodeCompletionContext::CCC_TypeQualifiers:
517 case CodeCompletionContext::CCC_ObjCInstanceMessage:
518 case CodeCompletionContext::CCC_ObjCClassMessage:
519 case CodeCompletionContext::CCC_Recovery:
520 return false;
521 }
522 llvm_unreachable("unknown code completion context");
523}
524
Sam McCall4caa8512018-06-07 12:49:17 +0000525// Some member calls are blacklisted because they're so rarely useful.
526static bool isBlacklistedMember(const NamedDecl &D) {
527 // Destructor completion is rarely useful, and works inconsistently.
528 // (s.^ completes ~string, but s.~st^ is an error).
529 if (D.getKind() == Decl::CXXDestructor)
530 return true;
531 // Injected name may be useful for A::foo(), but who writes A::A::foo()?
532 if (auto *R = dyn_cast_or_null<RecordDecl>(&D))
533 if (R->isInjectedClassName())
534 return true;
535 // Explicit calls to operators are also rare.
536 auto NameKind = D.getDeclName().getNameKind();
537 if (NameKind == DeclarationName::CXXOperatorName ||
538 NameKind == DeclarationName::CXXLiteralOperatorName ||
539 NameKind == DeclarationName::CXXConversionFunctionName)
540 return true;
541 return false;
542}
543
Sam McCall545a20d2018-01-19 14:34:02 +0000544// The CompletionRecorder captures Sema code-complete output, including context.
545// It filters out ignored results (but doesn't apply fuzzy-filtering yet).
546// It doesn't do scoring or conversion to CompletionItem yet, as we want to
547// merge with index results first.
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000548// Generally the fields and methods of this object should only be used from
549// within the callback.
Sam McCall545a20d2018-01-19 14:34:02 +0000550struct CompletionRecorder : public CodeCompleteConsumer {
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000551 CompletionRecorder(const CodeCompleteOptions &Opts,
552 UniqueFunction<void()> ResultsCallback)
Sam McCall545a20d2018-01-19 14:34:02 +0000553 : CodeCompleteConsumer(Opts.getClangCompleteOpts(),
Sam McCall98775c52017-12-04 13:49:59 +0000554 /*OutputIsBinary=*/false),
Sam McCall545a20d2018-01-19 14:34:02 +0000555 CCContext(CodeCompletionContext::CCC_Other), Opts(Opts),
556 CCAllocator(std::make_shared<GlobalCodeCompletionAllocator>()),
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000557 CCTUInfo(CCAllocator), ResultsCallback(std::move(ResultsCallback)) {
558 assert(this->ResultsCallback);
559 }
560
Sam McCall545a20d2018-01-19 14:34:02 +0000561 std::vector<CodeCompletionResult> Results;
562 CodeCompletionContext CCContext;
563 Sema *CCSema = nullptr; // Sema that created the results.
564 // FIXME: Sema is scary. Can we store ASTContext and Preprocessor, instead?
Sam McCall98775c52017-12-04 13:49:59 +0000565
Sam McCall545a20d2018-01-19 14:34:02 +0000566 void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context,
567 CodeCompletionResult *InResults,
Sam McCall98775c52017-12-04 13:49:59 +0000568 unsigned NumResults) override final {
Eric Liu42abe412018-05-24 11:20:19 +0000569 // If a callback is called without any sema result and the context does not
570 // support index-based completion, we simply skip it to give way to
571 // potential future callbacks with results.
572 if (NumResults == 0 && !contextAllowsIndex(Context.getKind()))
573 return;
Ilya Biryukov94da7bd2018-03-16 15:23:44 +0000574 if (CCSema) {
575 log(llvm::formatv(
576 "Multiple code complete callbacks (parser backtracked?). "
577 "Dropping results from context {0}, keeping results from {1}.",
Eric Liu42abe412018-05-24 11:20:19 +0000578 getCompletionKindString(Context.getKind()),
579 getCompletionKindString(this->CCContext.getKind())));
Ilya Biryukov94da7bd2018-03-16 15:23:44 +0000580 return;
581 }
Sam McCall545a20d2018-01-19 14:34:02 +0000582 // Record the completion context.
Sam McCall545a20d2018-01-19 14:34:02 +0000583 CCSema = &S;
584 CCContext = Context;
Eric Liu6f648df2017-12-19 16:50:37 +0000585
Sam McCall545a20d2018-01-19 14:34:02 +0000586 // Retain the results we might want.
Sam McCall98775c52017-12-04 13:49:59 +0000587 for (unsigned I = 0; I < NumResults; ++I) {
Sam McCall545a20d2018-01-19 14:34:02 +0000588 auto &Result = InResults[I];
589 // Drop hidden items which cannot be found by lookup after completion.
590 // Exception: some items can be named by using a qualifier.
Ilya Biryukovf60bf342018-01-10 13:51:09 +0000591 if (Result.Hidden && (!Result.Qualifier || Result.QualifierIsInformative))
592 continue;
Sam McCall545a20d2018-01-19 14:34:02 +0000593 if (!Opts.IncludeIneligibleResults &&
Sam McCall98775c52017-12-04 13:49:59 +0000594 (Result.Availability == CXAvailability_NotAvailable ||
595 Result.Availability == CXAvailability_NotAccessible))
596 continue;
Sam McCall4caa8512018-06-07 12:49:17 +0000597 if (Result.Declaration &&
598 !Context.getBaseType().isNull() // is this a member-access context?
599 && isBlacklistedMember(*Result.Declaration))
Sam McCalld2a95922018-01-22 21:05:00 +0000600 continue;
Ilya Biryukov53d6d932018-03-06 16:45:21 +0000601 // We choose to never append '::' to completion results in clangd.
602 Result.StartsNestedNameSpecifier = false;
Sam McCall545a20d2018-01-19 14:34:02 +0000603 Results.push_back(Result);
Sam McCall98775c52017-12-04 13:49:59 +0000604 }
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000605 ResultsCallback();
Sam McCall98775c52017-12-04 13:49:59 +0000606 }
607
Sam McCall545a20d2018-01-19 14:34:02 +0000608 CodeCompletionAllocator &getAllocator() override { return *CCAllocator; }
Sam McCall98775c52017-12-04 13:49:59 +0000609 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
610
Sam McCall545a20d2018-01-19 14:34:02 +0000611 // Returns the filtering/sorting name for Result, which must be from Results.
612 // Returned string is owned by this recorder (or the AST).
613 llvm::StringRef getName(const CodeCompletionResult &Result) {
Sam McCall98775c52017-12-04 13:49:59 +0000614 switch (Result.Kind) {
615 case CodeCompletionResult::RK_Declaration:
616 if (auto *ID = Result.Declaration->getIdentifier())
Sam McCall545a20d2018-01-19 14:34:02 +0000617 return ID->getName();
Sam McCall98775c52017-12-04 13:49:59 +0000618 break;
619 case CodeCompletionResult::RK_Keyword:
Sam McCall545a20d2018-01-19 14:34:02 +0000620 return Result.Keyword;
Sam McCall98775c52017-12-04 13:49:59 +0000621 case CodeCompletionResult::RK_Macro:
Sam McCall545a20d2018-01-19 14:34:02 +0000622 return Result.Macro->getName();
Sam McCall98775c52017-12-04 13:49:59 +0000623 case CodeCompletionResult::RK_Pattern:
Sam McCall545a20d2018-01-19 14:34:02 +0000624 return Result.Pattern->getTypedText();
Sam McCall98775c52017-12-04 13:49:59 +0000625 }
Ilya Biryukov43714502018-05-16 12:32:44 +0000626 auto *CCS = codeCompletionString(Result);
Sam McCall545a20d2018-01-19 14:34:02 +0000627 return CCS->getTypedText();
Sam McCall98775c52017-12-04 13:49:59 +0000628 }
629
Sam McCall545a20d2018-01-19 14:34:02 +0000630 // Build a CodeCompletion string for R, which must be from Results.
631 // The CCS will be owned by this recorder.
Ilya Biryukov43714502018-05-16 12:32:44 +0000632 CodeCompletionString *codeCompletionString(const CodeCompletionResult &R) {
Sam McCall545a20d2018-01-19 14:34:02 +0000633 // CodeCompletionResult doesn't seem to be const-correct. We own it, anyway.
634 return const_cast<CodeCompletionResult &>(R).CreateCodeCompletionString(
Ilya Biryukov43714502018-05-16 12:32:44 +0000635 *CCSema, CCContext, *CCAllocator, CCTUInfo,
636 /*IncludeBriefComments=*/false);
Sam McCall98775c52017-12-04 13:49:59 +0000637 }
638
Sam McCall545a20d2018-01-19 14:34:02 +0000639private:
640 CodeCompleteOptions Opts;
641 std::shared_ptr<GlobalCodeCompletionAllocator> CCAllocator;
Sam McCall98775c52017-12-04 13:49:59 +0000642 CodeCompletionTUInfo CCTUInfo;
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000643 UniqueFunction<void()> ResultsCallback;
Sam McCall545a20d2018-01-19 14:34:02 +0000644};
645
Sam McCall98775c52017-12-04 13:49:59 +0000646class SignatureHelpCollector final : public CodeCompleteConsumer {
647
648public:
649 SignatureHelpCollector(const clang::CodeCompleteOptions &CodeCompleteOpts,
650 SignatureHelp &SigHelp)
651 : CodeCompleteConsumer(CodeCompleteOpts, /*OutputIsBinary=*/false),
652 SigHelp(SigHelp),
653 Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
654 CCTUInfo(Allocator) {}
655
656 void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
657 OverloadCandidate *Candidates,
658 unsigned NumCandidates) override {
659 SigHelp.signatures.reserve(NumCandidates);
660 // FIXME(rwols): How can we determine the "active overload candidate"?
661 // Right now the overloaded candidates seem to be provided in a "best fit"
662 // order, so I'm not too worried about this.
663 SigHelp.activeSignature = 0;
664 assert(CurrentArg <= (unsigned)std::numeric_limits<int>::max() &&
665 "too many arguments");
666 SigHelp.activeParameter = static_cast<int>(CurrentArg);
667 for (unsigned I = 0; I < NumCandidates; ++I) {
668 const auto &Candidate = Candidates[I];
669 const auto *CCS = Candidate.CreateSignatureString(
670 CurrentArg, S, *Allocator, CCTUInfo, true);
671 assert(CCS && "Expected the CodeCompletionString to be non-null");
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +0000672 // FIXME: for headers, we need to get a comment from the index.
Ilya Biryukov43714502018-05-16 12:32:44 +0000673 SigHelp.signatures.push_back(ProcessOverloadCandidate(
674 Candidate, *CCS,
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +0000675 getParameterDocComment(S.getASTContext(), Candidate, CurrentArg,
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000676 /*CommentsFromHeaders=*/false)));
Sam McCall98775c52017-12-04 13:49:59 +0000677 }
678 }
679
680 GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
681
682 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
683
684private:
Eric Liu63696e12017-12-20 17:24:31 +0000685 // FIXME(ioeric): consider moving CodeCompletionString logic here to
686 // CompletionString.h.
Sam McCall98775c52017-12-04 13:49:59 +0000687 SignatureInformation
688 ProcessOverloadCandidate(const OverloadCandidate &Candidate,
Ilya Biryukov43714502018-05-16 12:32:44 +0000689 const CodeCompletionString &CCS,
690 llvm::StringRef DocComment) const {
Sam McCall98775c52017-12-04 13:49:59 +0000691 SignatureInformation Result;
692 const char *ReturnType = nullptr;
693
Ilya Biryukov43714502018-05-16 12:32:44 +0000694 Result.documentation = formatDocumentation(CCS, DocComment);
Sam McCall98775c52017-12-04 13:49:59 +0000695
696 for (const auto &Chunk : CCS) {
697 switch (Chunk.Kind) {
698 case CodeCompletionString::CK_ResultType:
699 // A piece of text that describes the type of an entity or,
700 // for functions and methods, the return type.
701 assert(!ReturnType && "Unexpected CK_ResultType");
702 ReturnType = Chunk.Text;
703 break;
704 case CodeCompletionString::CK_Placeholder:
705 // A string that acts as a placeholder for, e.g., a function call
706 // argument.
707 // Intentional fallthrough here.
708 case CodeCompletionString::CK_CurrentParameter: {
709 // A piece of text that describes the parameter that corresponds to
710 // the code-completion location within a function call, message send,
711 // macro invocation, etc.
712 Result.label += Chunk.Text;
713 ParameterInformation Info;
714 Info.label = Chunk.Text;
715 Result.parameters.push_back(std::move(Info));
716 break;
717 }
718 case CodeCompletionString::CK_Optional: {
719 // The rest of the parameters are defaulted/optional.
720 assert(Chunk.Optional &&
721 "Expected the optional code completion string to be non-null.");
722 Result.label +=
723 getOptionalParameters(*Chunk.Optional, Result.parameters);
724 break;
725 }
726 case CodeCompletionString::CK_VerticalSpace:
727 break;
728 default:
729 Result.label += Chunk.Text;
730 break;
731 }
732 }
733 if (ReturnType) {
734 Result.label += " -> ";
735 Result.label += ReturnType;
736 }
737 return Result;
738 }
739
740 SignatureHelp &SigHelp;
741 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
742 CodeCompletionTUInfo CCTUInfo;
743
744}; // SignatureHelpCollector
745
Sam McCall545a20d2018-01-19 14:34:02 +0000746struct SemaCompleteInput {
747 PathRef FileName;
748 const tooling::CompileCommand &Command;
749 PrecompiledPreamble const *Preamble;
Eric Liu63f419a2018-05-15 15:29:32 +0000750 const std::vector<Inclusion> &PreambleInclusions;
Sam McCall545a20d2018-01-19 14:34:02 +0000751 StringRef Contents;
752 Position Pos;
753 IntrusiveRefCntPtr<vfs::FileSystem> VFS;
754 std::shared_ptr<PCHContainerOperations> PCHs;
755};
756
757// Invokes Sema code completion on a file.
Eric Liu63f419a2018-05-15 15:29:32 +0000758// If \p Includes is set, it will be initialized after a compiler instance has
759// been set up.
Sam McCalld1a7a372018-01-31 13:40:48 +0000760bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
Sam McCall545a20d2018-01-19 14:34:02 +0000761 const clang::CodeCompleteOptions &Options,
Eric Liu63f419a2018-05-15 15:29:32 +0000762 const SemaCompleteInput &Input,
763 std::unique_ptr<IncludeInserter> *Includes = nullptr) {
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000764 trace::Span Tracer("Sema completion");
Sam McCall98775c52017-12-04 13:49:59 +0000765 std::vector<const char *> ArgStrs;
Sam McCall545a20d2018-01-19 14:34:02 +0000766 for (const auto &S : Input.Command.CommandLine)
Sam McCall98775c52017-12-04 13:49:59 +0000767 ArgStrs.push_back(S.c_str());
768
Ilya Biryukova9cf3112018-02-13 17:15:06 +0000769 if (Input.VFS->setCurrentWorkingDirectory(Input.Command.Directory)) {
770 log("Couldn't set working directory");
771 // We run parsing anyway, our lit-tests rely on results for non-existing
772 // working dirs.
773 }
Sam McCall98775c52017-12-04 13:49:59 +0000774
775 IgnoreDiagnostics DummyDiagsConsumer;
776 auto CI = createInvocationFromCommandLine(
777 ArgStrs,
778 CompilerInstance::createDiagnostics(new DiagnosticOptions,
779 &DummyDiagsConsumer, false),
Sam McCall545a20d2018-01-19 14:34:02 +0000780 Input.VFS);
Ilya Biryukovb6ad25c2018-02-09 13:51:57 +0000781 if (!CI) {
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000782 log("Couldn't create CompilerInvocation");
Ilya Biryukovb6ad25c2018-02-09 13:51:57 +0000783 return false;
784 }
Ilya Biryukov981a35d2018-05-28 12:11:37 +0000785 auto &FrontendOpts = CI->getFrontendOpts();
786 FrontendOpts.DisableFree = false;
Sam McCall98775c52017-12-04 13:49:59 +0000787 FrontendOpts.SkipFunctionBodies = true;
Ilya Biryukov981a35d2018-05-28 12:11:37 +0000788 CI->getLangOpts()->CommentOpts.ParseAllComments = true;
789 // Disable typo correction in Sema.
790 CI->getLangOpts()->SpellChecking = false;
791 // Setup code completion.
Sam McCall98775c52017-12-04 13:49:59 +0000792 FrontendOpts.CodeCompleteOpts = Options;
Sam McCall545a20d2018-01-19 14:34:02 +0000793 FrontendOpts.CodeCompletionAt.FileName = Input.FileName;
Sam McCalla4962cc2018-04-27 11:59:28 +0000794 auto Offset = positionToOffset(Input.Contents, Input.Pos);
795 if (!Offset) {
796 log("Code completion position was invalid " +
797 llvm::toString(Offset.takeError()));
798 return false;
799 }
800 std::tie(FrontendOpts.CodeCompletionAt.Line,
801 FrontendOpts.CodeCompletionAt.Column) =
802 offsetToClangLineColumn(Input.Contents, *Offset);
Sam McCall98775c52017-12-04 13:49:59 +0000803
Ilya Biryukov981a35d2018-05-28 12:11:37 +0000804 std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
805 llvm::MemoryBuffer::getMemBufferCopy(Input.Contents, Input.FileName);
806 // The diagnostic options must be set before creating a CompilerInstance.
807 CI->getDiagnosticOpts().IgnoreWarnings = true;
808 // We reuse the preamble whether it's valid or not. This is a
809 // correctness/performance tradeoff: building without a preamble is slow, and
810 // completion is latency-sensitive.
811 // NOTE: we must call BeginSourceFile after prepareCompilerInstance. Otherwise
812 // the remapped buffers do not get freed.
813 auto Clang = prepareCompilerInstance(
814 std::move(CI), Input.Preamble, std::move(ContentsBuffer),
815 std::move(Input.PCHs), std::move(Input.VFS), DummyDiagsConsumer);
Sam McCall98775c52017-12-04 13:49:59 +0000816 Clang->setCodeCompletionConsumer(Consumer.release());
817
818 SyntaxOnlyAction Action;
819 if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) {
Sam McCalld1a7a372018-01-31 13:40:48 +0000820 log("BeginSourceFile() failed when running codeComplete for " +
821 Input.FileName);
Sam McCall98775c52017-12-04 13:49:59 +0000822 return false;
823 }
Eric Liu63f419a2018-05-15 15:29:32 +0000824 if (Includes) {
825 // Initialize Includes if provided.
826
827 // FIXME(ioeric): needs more consistent style support in clangd server.
Eric Liubbadbe02018-06-26 12:49:09 +0000828 auto Style = format::getStyle(format::DefaultFormatStyle, Input.FileName,
829 format::DefaultFallbackStyle, Input.Contents,
830 Input.VFS.get());
Eric Liu63f419a2018-05-15 15:29:32 +0000831 if (!Style) {
Eric Liubbadbe02018-06-26 12:49:09 +0000832 log("ERROR: failed to get FormatStyle for file " + Input.FileName +
Eric Liu63f419a2018-05-15 15:29:32 +0000833 ". Fall back to use LLVM style. Error: " +
834 llvm::toString(Style.takeError()));
835 Style = format::getLLVMStyle();
836 }
837 *Includes = llvm::make_unique<IncludeInserter>(
838 Input.FileName, Input.Contents, *Style, Input.Command.Directory,
839 Clang->getPreprocessor().getHeaderSearchInfo());
840 for (const auto &Inc : Input.PreambleInclusions)
841 Includes->get()->addExisting(Inc);
842 Clang->getPreprocessor().addPPCallbacks(collectInclusionsInMainFileCallback(
843 Clang->getSourceManager(), [Includes](Inclusion Inc) {
844 Includes->get()->addExisting(std::move(Inc));
845 }));
846 }
Sam McCall98775c52017-12-04 13:49:59 +0000847 if (!Action.Execute()) {
Sam McCalld1a7a372018-01-31 13:40:48 +0000848 log("Execute() failed when running codeComplete for " + Input.FileName);
Sam McCall98775c52017-12-04 13:49:59 +0000849 return false;
850 }
Sam McCall98775c52017-12-04 13:49:59 +0000851 Action.EndSourceFile();
852
853 return true;
854}
855
Ilya Biryukova907ba42018-05-14 10:50:04 +0000856// Should we allow index completions in the specified context?
857bool allowIndex(CodeCompletionContext &CC) {
858 if (!contextAllowsIndex(CC.getKind()))
859 return false;
860 // We also avoid ClassName::bar (but allow namespace::bar).
861 auto Scope = CC.getCXXScopeSpecifier();
862 if (!Scope)
863 return true;
864 NestedNameSpecifier *NameSpec = (*Scope)->getScopeRep();
865 if (!NameSpec)
866 return true;
867 // We only query the index when qualifier is a namespace.
868 // If it's a class, we rely solely on sema completions.
869 switch (NameSpec->getKind()) {
870 case NestedNameSpecifier::Global:
871 case NestedNameSpecifier::Namespace:
872 case NestedNameSpecifier::NamespaceAlias:
873 return true;
874 case NestedNameSpecifier::Super:
875 case NestedNameSpecifier::TypeSpec:
876 case NestedNameSpecifier::TypeSpecWithTemplate:
877 // Unresolved inside a template.
878 case NestedNameSpecifier::Identifier:
879 return false;
880 }
Ilya Biryukova6556e22018-05-14 11:47:30 +0000881 llvm_unreachable("invalid NestedNameSpecifier kind");
Ilya Biryukova907ba42018-05-14 10:50:04 +0000882}
883
Sam McCall98775c52017-12-04 13:49:59 +0000884} // namespace
885
886clang::CodeCompleteOptions CodeCompleteOptions::getClangCompleteOpts() const {
887 clang::CodeCompleteOptions Result;
888 Result.IncludeCodePatterns = EnableSnippets && IncludeCodePatterns;
889 Result.IncludeMacros = IncludeMacros;
Sam McCalld8169a82018-01-18 15:31:30 +0000890 Result.IncludeGlobals = true;
Ilya Biryukov43714502018-05-16 12:32:44 +0000891 // We choose to include full comments and not do doxygen parsing in
892 // completion.
893 // FIXME: ideally, we should support doxygen in some form, e.g. do markdown
894 // formatting of the comments.
895 Result.IncludeBriefComments = false;
Sam McCall98775c52017-12-04 13:49:59 +0000896
Sam McCall3d139c52018-01-12 18:30:08 +0000897 // When an is used, Sema is responsible for completing the main file,
898 // the index can provide results from the preamble.
899 // Tell Sema not to deserialize the preamble to look for results.
900 Result.LoadExternal = !Index;
Eric Liu6f648df2017-12-19 16:50:37 +0000901
Sam McCall98775c52017-12-04 13:49:59 +0000902 return Result;
903}
904
Sam McCall545a20d2018-01-19 14:34:02 +0000905// Runs Sema-based (AST) and Index-based completion, returns merged results.
906//
907// There are a few tricky considerations:
908// - the AST provides information needed for the index query (e.g. which
909// namespaces to search in). So Sema must start first.
910// - we only want to return the top results (Opts.Limit).
911// Building CompletionItems for everything else is wasteful, so we want to
912// preserve the "native" format until we're done with scoring.
913// - the data underlying Sema completion items is owned by the AST and various
914// other arenas, which must stay alive for us to build CompletionItems.
915// - we may get duplicate results from Sema and the Index, we need to merge.
916//
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000917// So we start Sema completion first, and do all our work in its callback.
Sam McCall545a20d2018-01-19 14:34:02 +0000918// We use the Sema context information to query the index.
919// Then we merge the two result sets, producing items that are Sema/Index/Both.
920// These items are scored, and the top N are synthesized into the LSP response.
921// Finally, we can clean up the data structures created by Sema completion.
922//
923// Main collaborators are:
924// - semaCodeComplete sets up the compiler machinery to run code completion.
925// - CompletionRecorder captures Sema completion results, including context.
926// - SymbolIndex (Opts.Index) provides index completion results as Symbols
927// - CompletionCandidates are the result of merging Sema and Index results.
928// Each candidate points to an underlying CodeCompletionResult (Sema), a
929// Symbol (Index), or both. It computes the result quality score.
930// CompletionCandidate also does conversion to CompletionItem (at the end).
931// - FuzzyMatcher scores how the candidate matches the partial identifier.
932// This score is combined with the result quality score for the final score.
933// - TopN determines the results with the best score.
934class CodeCompleteFlow {
Eric Liuc5105f92018-02-16 14:15:55 +0000935 PathRef FileName;
Sam McCall545a20d2018-01-19 14:34:02 +0000936 const CodeCompleteOptions &Opts;
937 // Sema takes ownership of Recorder. Recorder is valid until Sema cleanup.
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000938 CompletionRecorder *Recorder = nullptr;
Sam McCall545a20d2018-01-19 14:34:02 +0000939 int NSema = 0, NIndex = 0, NBoth = 0; // Counters for logging.
940 bool Incomplete = false; // Would more be available with a higher limit?
Eric Liu63f419a2018-05-15 15:29:32 +0000941 llvm::Optional<FuzzyMatcher> Filter; // Initialized once Sema runs.
942 std::unique_ptr<IncludeInserter> Includes; // Initialized once compiler runs.
Eric Liu09c3c372018-06-15 08:58:12 +0000943 FileProximityMatcher FileProximityMatch;
Sam McCall545a20d2018-01-19 14:34:02 +0000944
945public:
946 // A CodeCompleteFlow object is only useful for calling run() exactly once.
Eric Liuc5105f92018-02-16 14:15:55 +0000947 CodeCompleteFlow(PathRef FileName, const CodeCompleteOptions &Opts)
Eric Liu09c3c372018-06-15 08:58:12 +0000948 : FileName(FileName), Opts(Opts),
949 // FIXME: also use path of the main header corresponding to FileName to
950 // calculate the file proximity, which would capture include/ and src/
951 // project setup where headers and implementations are not in the same
952 // directory.
Haojian Wu7943d4f2018-06-15 09:32:36 +0000953 FileProximityMatch(ArrayRef<StringRef>({FileName})) {}
Sam McCall545a20d2018-01-19 14:34:02 +0000954
955 CompletionList run(const SemaCompleteInput &SemaCCInput) && {
Sam McCalld1a7a372018-01-31 13:40:48 +0000956 trace::Span Tracer("CodeCompleteFlow");
Eric Liu63f419a2018-05-15 15:29:32 +0000957
Sam McCall545a20d2018-01-19 14:34:02 +0000958 // We run Sema code completion first. It builds an AST and calculates:
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000959 // - completion results based on the AST.
Sam McCall545a20d2018-01-19 14:34:02 +0000960 // - partial identifier and context. We need these for the index query.
961 CompletionList Output;
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000962 auto RecorderOwner = llvm::make_unique<CompletionRecorder>(Opts, [&]() {
963 assert(Recorder && "Recorder is not set");
Eric Liu63f419a2018-05-15 15:29:32 +0000964 assert(Includes && "Includes is not set");
965 // If preprocessor was run, inclusions from preprocessor callback should
966 // already be added to Inclusions.
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000967 Output = runWithSema();
Eric Liu63f419a2018-05-15 15:29:32 +0000968 Includes.reset(); // Make sure this doesn't out-live Clang.
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000969 SPAN_ATTACH(Tracer, "sema_completion_kind",
970 getCompletionKindString(Recorder->CCContext.getKind()));
971 });
972
973 Recorder = RecorderOwner.get();
Sam McCalld1a7a372018-01-31 13:40:48 +0000974 semaCodeComplete(std::move(RecorderOwner), Opts.getClangCompleteOpts(),
Eric Liu63f419a2018-05-15 15:29:32 +0000975 SemaCCInput, &Includes);
Sam McCall545a20d2018-01-19 14:34:02 +0000976
Sam McCall2b780162018-01-30 17:20:54 +0000977 SPAN_ATTACH(Tracer, "sema_results", NSema);
978 SPAN_ATTACH(Tracer, "index_results", NIndex);
979 SPAN_ATTACH(Tracer, "merged_results", NBoth);
980 SPAN_ATTACH(Tracer, "returned_results", Output.items.size());
981 SPAN_ATTACH(Tracer, "incomplete", Output.isIncomplete);
Sam McCall0f8df3e2018-06-13 11:31:20 +0000982 log(llvm::formatv("Code complete: {0} results from Sema, {1} from Index, "
983 "{2} matched, {3} returned{4}.",
984 NSema, NIndex, NBoth, Output.items.size(),
985 Output.isIncomplete ? " (incomplete)" : ""));
Sam McCall545a20d2018-01-19 14:34:02 +0000986 assert(!Opts.Limit || Output.items.size() <= Opts.Limit);
987 // We don't assert that isIncomplete means we hit a limit.
988 // Indexes may choose to impose their own limits even if we don't have one.
989 return Output;
990 }
991
992private:
993 // This is called by run() once Sema code completion is done, but before the
994 // Sema data structures are torn down. It does all the real work.
995 CompletionList runWithSema() {
996 Filter = FuzzyMatcher(
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000997 Recorder->CCSema->getPreprocessor().getCodeCompletionFilter());
Sam McCall545a20d2018-01-19 14:34:02 +0000998 // Sema provides the needed context to query the index.
999 // FIXME: in addition to querying for extra/overlapping symbols, we should
1000 // explicitly request symbols corresponding to Sema results.
1001 // We can use their signals even if the index can't suggest them.
1002 // We must copy index results to preserve them, but there are at most Limit.
Eric Liu8f3678d2018-06-15 13:34:18 +00001003 auto IndexResults = (Opts.Index && allowIndex(Recorder->CCContext))
1004 ? queryIndex()
1005 : SymbolSlab();
Sam McCall545a20d2018-01-19 14:34:02 +00001006 // Merge Sema and Index results, score them, and pick the winners.
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001007 auto Top = mergeResults(Recorder->Results, IndexResults);
Sam McCall545a20d2018-01-19 14:34:02 +00001008 // Convert the results to the desired LSP structs.
1009 CompletionList Output;
1010 for (auto &C : Top)
1011 Output.items.push_back(toCompletionItem(C.first, C.second));
1012 Output.isIncomplete = Incomplete;
1013 return Output;
1014 }
1015
1016 SymbolSlab queryIndex() {
Sam McCalld1a7a372018-01-31 13:40:48 +00001017 trace::Span Tracer("Query index");
Sam McCall2b780162018-01-30 17:20:54 +00001018 SPAN_ATTACH(Tracer, "limit", Opts.Limit);
1019
Sam McCall545a20d2018-01-19 14:34:02 +00001020 SymbolSlab::Builder ResultsBuilder;
1021 // Build the query.
1022 FuzzyFindRequest Req;
Haojian Wu48b48652018-01-25 09:20:09 +00001023 if (Opts.Limit)
1024 Req.MaxCandidateCount = Opts.Limit;
Sam McCall545a20d2018-01-19 14:34:02 +00001025 Req.Query = Filter->pattern();
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +00001026 Req.RestrictForCodeCompletion = true;
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001027 Req.Scopes = getQueryScopes(Recorder->CCContext,
1028 Recorder->CCSema->getSourceManager());
Eric Liu6de95ec2018-06-12 08:48:20 +00001029 Req.ProximityPaths.push_back(FileName);
Sam McCalld1a7a372018-01-31 13:40:48 +00001030 log(llvm::formatv("Code complete: fuzzyFind(\"{0}\", scopes=[{1}])",
Sam McCall2b780162018-01-30 17:20:54 +00001031 Req.Query,
1032 llvm::join(Req.Scopes.begin(), Req.Scopes.end(), ",")));
Sam McCall545a20d2018-01-19 14:34:02 +00001033 // Run the query against the index.
Sam McCallab8e3932018-02-19 13:04:41 +00001034 if (Opts.Index->fuzzyFind(
1035 Req, [&](const Symbol &Sym) { ResultsBuilder.insert(Sym); }))
1036 Incomplete = true;
Sam McCall545a20d2018-01-19 14:34:02 +00001037 return std::move(ResultsBuilder).build();
1038 }
1039
Sam McCallc18c2802018-06-15 11:06:29 +00001040 // Merges Sema and Index results where possible, to form CompletionCandidates.
1041 // Groups overloads if desired, to form CompletionCandidate::Bundles.
1042 // The bundles are scored and top results are returned, best to worst.
1043 std::vector<ScoredBundle>
Sam McCall545a20d2018-01-19 14:34:02 +00001044 mergeResults(const std::vector<CodeCompletionResult> &SemaResults,
1045 const SymbolSlab &IndexResults) {
Sam McCalld1a7a372018-01-31 13:40:48 +00001046 trace::Span Tracer("Merge and score results");
Sam McCallc18c2802018-06-15 11:06:29 +00001047 std::vector<CompletionCandidate::Bundle> Bundles;
1048 llvm::DenseMap<size_t, size_t> BundleLookup;
1049 auto AddToBundles = [&](const CodeCompletionResult *SemaResult,
1050 const Symbol *IndexResult) {
1051 CompletionCandidate C;
1052 C.SemaResult = SemaResult;
1053 C.IndexResult = IndexResult;
1054 C.Name = IndexResult ? IndexResult->Name : Recorder->getName(*SemaResult);
1055 if (auto OverloadSet = Opts.BundleOverloads ? C.overloadSet() : 0) {
1056 auto Ret = BundleLookup.try_emplace(OverloadSet, Bundles.size());
1057 if (Ret.second)
1058 Bundles.emplace_back();
1059 Bundles[Ret.first->second].push_back(std::move(C));
1060 } else {
1061 Bundles.emplace_back();
1062 Bundles.back().push_back(std::move(C));
1063 }
1064 };
Sam McCall545a20d2018-01-19 14:34:02 +00001065 llvm::DenseSet<const Symbol *> UsedIndexResults;
1066 auto CorrespondingIndexResult =
1067 [&](const CodeCompletionResult &SemaResult) -> const Symbol * {
1068 if (auto SymID = getSymbolID(SemaResult)) {
1069 auto I = IndexResults.find(*SymID);
1070 if (I != IndexResults.end()) {
1071 UsedIndexResults.insert(&*I);
1072 return &*I;
1073 }
1074 }
1075 return nullptr;
1076 };
1077 // Emit all Sema results, merging them with Index results if possible.
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001078 for (auto &SemaResult : Recorder->Results)
Sam McCallc18c2802018-06-15 11:06:29 +00001079 AddToBundles(&SemaResult, CorrespondingIndexResult(SemaResult));
Sam McCall545a20d2018-01-19 14:34:02 +00001080 // Now emit any Index-only results.
1081 for (const auto &IndexResult : IndexResults) {
1082 if (UsedIndexResults.count(&IndexResult))
1083 continue;
Sam McCallc18c2802018-06-15 11:06:29 +00001084 AddToBundles(/*SemaResult=*/nullptr, &IndexResult);
Sam McCall545a20d2018-01-19 14:34:02 +00001085 }
Sam McCallc18c2802018-06-15 11:06:29 +00001086 // We only keep the best N results at any time, in "native" format.
1087 TopN<ScoredBundle, ScoredBundleGreater> Top(
1088 Opts.Limit == 0 ? std::numeric_limits<size_t>::max() : Opts.Limit);
1089 for (auto &Bundle : Bundles)
1090 addCandidate(Top, std::move(Bundle));
Sam McCall545a20d2018-01-19 14:34:02 +00001091 return std::move(Top).items();
1092 }
1093
Sam McCall80ad7072018-06-08 13:32:25 +00001094 Optional<float> fuzzyScore(const CompletionCandidate &C) {
1095 // Macros can be very spammy, so we only support prefix completion.
1096 // We won't end up with underfull index results, as macros are sema-only.
1097 if (C.SemaResult && C.SemaResult->Kind == CodeCompletionResult::RK_Macro &&
1098 !C.Name.startswith_lower(Filter->pattern()))
1099 return None;
1100 return Filter->match(C.Name);
1101 }
1102
Sam McCall545a20d2018-01-19 14:34:02 +00001103 // Scores a candidate and adds it to the TopN structure.
Sam McCallc18c2802018-06-15 11:06:29 +00001104 void addCandidate(TopN<ScoredBundle, ScoredBundleGreater> &Candidates,
1105 CompletionCandidate::Bundle Bundle) {
Sam McCallc5707b62018-05-15 17:43:27 +00001106 SymbolQualitySignals Quality;
1107 SymbolRelevanceSignals Relevance;
Sam McCalld9b54f02018-06-05 16:30:25 +00001108 Relevance.Query = SymbolRelevanceSignals::CodeComplete;
Eric Liu09c3c372018-06-15 08:58:12 +00001109 Relevance.FileProximityMatch = &FileProximityMatch;
Sam McCallc18c2802018-06-15 11:06:29 +00001110 auto &First = Bundle.front();
1111 if (auto FuzzyScore = fuzzyScore(First))
Sam McCallc5707b62018-05-15 17:43:27 +00001112 Relevance.NameMatch = *FuzzyScore;
Sam McCall545a20d2018-01-19 14:34:02 +00001113 else
1114 return;
Sam McCallc18c2802018-06-15 11:06:29 +00001115 unsigned SemaResult = 0, IndexResult = 0;
1116 for (const auto &Candidate : Bundle) {
1117 if (Candidate.IndexResult) {
1118 Quality.merge(*Candidate.IndexResult);
1119 Relevance.merge(*Candidate.IndexResult);
1120 ++IndexResult;
1121 }
1122 if (Candidate.SemaResult) {
1123 Quality.merge(*Candidate.SemaResult);
1124 Relevance.merge(*Candidate.SemaResult);
1125 ++SemaResult;
1126 }
Sam McCallc5707b62018-05-15 17:43:27 +00001127 }
1128
1129 float QualScore = Quality.evaluate(), RelScore = Relevance.evaluate();
1130 CompletionItemScores Scores;
1131 Scores.finalScore = evaluateSymbolAndRelevance(QualScore, RelScore);
1132 // The purpose of exporting component scores is to allow NameMatch to be
1133 // replaced on the client-side. So we export (NameMatch, final/NameMatch)
1134 // rather than (RelScore, QualScore).
1135 Scores.filterScore = Relevance.NameMatch;
1136 Scores.symbolScore =
1137 Scores.filterScore ? Scores.finalScore / Scores.filterScore : QualScore;
1138
Sam McCallc18c2802018-06-15 11:06:29 +00001139 LLVM_DEBUG(llvm::dbgs() << "CodeComplete: " << First.Name << "("
1140 << IndexResult << " index) "
1141 << "(" << SemaResult << " sema)"
1142 << " = " << Scores.finalScore << "\n"
1143 << Quality << Relevance << "\n");
Sam McCall545a20d2018-01-19 14:34:02 +00001144
1145 NSema += bool(SemaResult);
1146 NIndex += bool(IndexResult);
1147 NBoth += SemaResult && IndexResult;
Sam McCallc18c2802018-06-15 11:06:29 +00001148 if (Candidates.push({std::move(Bundle), Scores}))
Sam McCallab8e3932018-02-19 13:04:41 +00001149 Incomplete = true;
Sam McCall545a20d2018-01-19 14:34:02 +00001150 }
1151
Sam McCallc18c2802018-06-15 11:06:29 +00001152 CompletionItem toCompletionItem(const CompletionCandidate::Bundle &Bundle,
Sam McCall545a20d2018-01-19 14:34:02 +00001153 const CompletionItemScores &Scores) {
1154 CodeCompletionString *SemaCCS = nullptr;
Sam McCallc18c2802018-06-15 11:06:29 +00001155 std::string FrontDocComment;
1156 if (auto *SR = Bundle.front().SemaResult) {
Ilya Biryukov43714502018-05-16 12:32:44 +00001157 SemaCCS = Recorder->codeCompletionString(*SR);
1158 if (Opts.IncludeComments) {
1159 assert(Recorder->CCSema);
Sam McCallc18c2802018-06-15 11:06:29 +00001160 FrontDocComment = getDocComment(Recorder->CCSema->getASTContext(), *SR,
1161 /*CommentsFromHeader=*/false);
Ilya Biryukov43714502018-05-16 12:32:44 +00001162 }
1163 }
Sam McCallc18c2802018-06-15 11:06:29 +00001164 return CompletionCandidate::build(
1165 Bundle,
Eric Liu8f3678d2018-06-15 13:34:18 +00001166 Bundle.front().build(FileName, Scores, Opts, SemaCCS, *Includes,
Sam McCallc18c2802018-06-15 11:06:29 +00001167 FrontDocComment),
1168 Opts);
Sam McCall545a20d2018-01-19 14:34:02 +00001169 }
1170};
1171
Sam McCalld1a7a372018-01-31 13:40:48 +00001172CompletionList codeComplete(PathRef FileName,
Sam McCall98775c52017-12-04 13:49:59 +00001173 const tooling::CompileCommand &Command,
1174 PrecompiledPreamble const *Preamble,
Eric Liu63f419a2018-05-15 15:29:32 +00001175 const std::vector<Inclusion> &PreambleInclusions,
Sam McCall98775c52017-12-04 13:49:59 +00001176 StringRef Contents, Position Pos,
1177 IntrusiveRefCntPtr<vfs::FileSystem> VFS,
1178 std::shared_ptr<PCHContainerOperations> PCHs,
Ilya Biryukov940901e2017-12-13 12:51:22 +00001179 CodeCompleteOptions Opts) {
Eric Liuc5105f92018-02-16 14:15:55 +00001180 return CodeCompleteFlow(FileName, Opts)
Eric Liu63f419a2018-05-15 15:29:32 +00001181 .run({FileName, Command, Preamble, PreambleInclusions, Contents, Pos, VFS,
1182 PCHs});
Sam McCall98775c52017-12-04 13:49:59 +00001183}
1184
Sam McCalld1a7a372018-01-31 13:40:48 +00001185SignatureHelp signatureHelp(PathRef FileName,
Ilya Biryukov940901e2017-12-13 12:51:22 +00001186 const tooling::CompileCommand &Command,
1187 PrecompiledPreamble const *Preamble,
1188 StringRef Contents, Position Pos,
1189 IntrusiveRefCntPtr<vfs::FileSystem> VFS,
1190 std::shared_ptr<PCHContainerOperations> PCHs) {
Sam McCall98775c52017-12-04 13:49:59 +00001191 SignatureHelp Result;
1192 clang::CodeCompleteOptions Options;
1193 Options.IncludeGlobals = false;
1194 Options.IncludeMacros = false;
1195 Options.IncludeCodePatterns = false;
Ilya Biryukov43714502018-05-16 12:32:44 +00001196 Options.IncludeBriefComments = false;
Eric Liu4c0a27b2018-05-16 20:31:38 +00001197 std::vector<Inclusion> PreambleInclusions = {}; // Unused for signatureHelp
Sam McCalld1a7a372018-01-31 13:40:48 +00001198 semaCodeComplete(llvm::make_unique<SignatureHelpCollector>(Options, Result),
1199 Options,
Eric Liu4c0a27b2018-05-16 20:31:38 +00001200 {FileName, Command, Preamble, PreambleInclusions, Contents,
1201 Pos, std::move(VFS), std::move(PCHs)});
Sam McCall98775c52017-12-04 13:49:59 +00001202 return Result;
1203}
1204
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +00001205bool isIndexedForCodeCompletion(const NamedDecl &ND, ASTContext &ASTCtx) {
1206 using namespace clang::ast_matchers;
1207 auto InTopLevelScope = hasDeclContext(
1208 anyOf(namespaceDecl(), translationUnitDecl(), linkageSpecDecl()));
1209 return !match(decl(anyOf(InTopLevelScope,
1210 hasDeclContext(
1211 enumDecl(InTopLevelScope, unless(isScoped()))))),
1212 ND, ASTCtx)
1213 .empty();
1214}
1215
Sam McCall98775c52017-12-04 13:49:59 +00001216} // namespace clangd
1217} // namespace clang