blob: 819cd6b3ce25cd9382c0af6b583a28baa4545d71 [file] [log] [blame]
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +00001//===--- CodeComplete.cpp ----------------------------------------*- C++-*-===//
Sam McCall98775c52017-12-04 13:49:59 +00002//
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//
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +00008//===----------------------------------------------------------------------===//
Sam McCall98775c52017-12-04 13:49:59 +00009//
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//
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +000019//===----------------------------------------------------------------------===//
Sam McCall98775c52017-12-04 13:49:59 +000020
21#include "CodeComplete.h"
Eric Liu7ad16962018-06-22 10:46:59 +000022#include "AST.h"
Eric Liub1d75422018-10-02 10:43:55 +000023#include "ClangdUnit.h"
Eric Liu63696e12017-12-20 17:24:31 +000024#include "CodeCompletionStrings.h"
Sam McCall98775c52017-12-04 13:49:59 +000025#include "Compiler.h"
Kadir Cetinkaya2f84d912018-08-08 08:59:29 +000026#include "Diagnostics.h"
Sam McCall3f0243f2018-07-03 08:09:29 +000027#include "FileDistance.h"
Sam McCall84652cc2018-01-12 16:16:09 +000028#include "FuzzyMatch.h"
Eric Liu63f419a2018-05-15 15:29:32 +000029#include "Headers.h"
Eric Liu6f648df2017-12-19 16:50:37 +000030#include "Logger.h"
Sam McCallc5707b62018-05-15 17:43:27 +000031#include "Quality.h"
Eric Liuc5105f92018-02-16 14:15:55 +000032#include "SourceCode.h"
Eric Liu25d74e92018-08-24 11:23:56 +000033#include "TUScheduler.h"
Sam McCall2b780162018-01-30 17:20:54 +000034#include "Trace.h"
Eric Liu63f419a2018-05-15 15:29:32 +000035#include "URI.h"
Eric Liu6f648df2017-12-19 16:50:37 +000036#include "index/Index.h"
Eric Liu3fac4ef2018-10-17 11:19:02 +000037#include "clang/AST/Decl.h"
38#include "clang/AST/DeclBase.h"
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +000039#include "clang/ASTMatchers/ASTMatchFinder.h"
Ilya Biryukovc22d3442018-05-16 12:32:49 +000040#include "clang/Basic/LangOptions.h"
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +000041#include "clang/Basic/SourceLocation.h"
Eric Liuc5105f92018-02-16 14:15:55 +000042#include "clang/Format/Format.h"
Sam McCall98775c52017-12-04 13:49:59 +000043#include "clang/Frontend/CompilerInstance.h"
44#include "clang/Frontend/FrontendActions.h"
Sam McCallebef8122018-09-14 12:36:06 +000045#include "clang/Lex/PreprocessorOptions.h"
Sam McCall98775c52017-12-04 13:49:59 +000046#include "clang/Sema/CodeCompleteConsumer.h"
47#include "clang/Sema/Sema.h"
Eric Liu670c1472018-09-27 18:46:00 +000048#include "llvm/ADT/ArrayRef.h"
Eric Liu3fac4ef2018-10-17 11:19:02 +000049#include "llvm/ADT/None.h"
Eric Liu25d74e92018-08-24 11:23:56 +000050#include "llvm/ADT/Optional.h"
Eric Liu83f63e42018-09-03 10:18:21 +000051#include "llvm/ADT/SmallVector.h"
Eric Liu25d74e92018-08-24 11:23:56 +000052#include "llvm/Support/Error.h"
Haojian Wuba28e9a2018-01-10 14:44:34 +000053#include "llvm/Support/Format.h"
Eric Liubc25ef72018-07-05 08:29:33 +000054#include "llvm/Support/FormatVariadic.h"
Sam McCall2161ec72018-07-05 06:20:41 +000055#include "llvm/Support/ScopedPrinter.h"
Eric Liu83f63e42018-09-03 10:18:21 +000056#include <algorithm>
57#include <iterator>
Sam McCall98775c52017-12-04 13:49:59 +000058
Sam McCallc5707b62018-05-15 17:43:27 +000059// We log detailed candidate here if you run with -debug-only=codecomplete.
Sam McCall27c979a2018-06-29 14:47:57 +000060#define DEBUG_TYPE "CodeComplete"
Sam McCallc5707b62018-05-15 17:43:27 +000061
Sam McCallc008af62018-10-20 15:30:37 +000062using namespace llvm;
Sam McCall98775c52017-12-04 13:49:59 +000063namespace clang {
64namespace clangd {
65namespace {
66
Eric Liu6f648df2017-12-19 16:50:37 +000067CompletionItemKind toCompletionItemKind(index::SymbolKind Kind) {
68 using SK = index::SymbolKind;
69 switch (Kind) {
70 case SK::Unknown:
71 return CompletionItemKind::Missing;
72 case SK::Module:
73 case SK::Namespace:
74 case SK::NamespaceAlias:
75 return CompletionItemKind::Module;
76 case SK::Macro:
77 return CompletionItemKind::Text;
78 case SK::Enum:
79 return CompletionItemKind::Enum;
80 // FIXME(ioeric): use LSP struct instead of class when it is suppoted in the
81 // protocol.
82 case SK::Struct:
83 case SK::Class:
84 case SK::Protocol:
85 case SK::Extension:
86 case SK::Union:
87 return CompletionItemKind::Class;
88 // FIXME(ioeric): figure out whether reference is the right type for aliases.
89 case SK::TypeAlias:
90 case SK::Using:
91 return CompletionItemKind::Reference;
92 case SK::Function:
93 // FIXME(ioeric): this should probably be an operator. This should be fixed
94 // when `Operator` is support type in the protocol.
95 case SK::ConversionFunction:
96 return CompletionItemKind::Function;
97 case SK::Variable:
98 case SK::Parameter:
99 return CompletionItemKind::Variable;
100 case SK::Field:
101 return CompletionItemKind::Field;
102 // FIXME(ioeric): use LSP enum constant when it is supported in the protocol.
103 case SK::EnumConstant:
104 return CompletionItemKind::Value;
105 case SK::InstanceMethod:
106 case SK::ClassMethod:
107 case SK::StaticMethod:
108 case SK::Destructor:
109 return CompletionItemKind::Method;
110 case SK::InstanceProperty:
111 case SK::ClassProperty:
112 case SK::StaticProperty:
113 return CompletionItemKind::Property;
114 case SK::Constructor:
115 return CompletionItemKind::Constructor;
116 }
117 llvm_unreachable("Unhandled clang::index::SymbolKind.");
118}
119
Sam McCall83305892018-06-08 21:17:19 +0000120CompletionItemKind
121toCompletionItemKind(CodeCompletionResult::ResultKind ResKind,
Sam McCall4c077f92018-09-18 09:08:28 +0000122 const NamedDecl *Decl,
123 CodeCompletionContext::Kind CtxKind) {
Sam McCall83305892018-06-08 21:17:19 +0000124 if (Decl)
125 return toCompletionItemKind(index::getSymbolInfo(Decl).Kind);
Sam McCall4c077f92018-09-18 09:08:28 +0000126 if (CtxKind == CodeCompletionContext::CCC_IncludedFile)
127 return CompletionItemKind::File;
Sam McCall83305892018-06-08 21:17:19 +0000128 switch (ResKind) {
129 case CodeCompletionResult::RK_Declaration:
130 llvm_unreachable("RK_Declaration without Decl");
131 case CodeCompletionResult::RK_Keyword:
132 return CompletionItemKind::Keyword;
133 case CodeCompletionResult::RK_Macro:
134 return CompletionItemKind::Text; // unfortunately, there's no 'Macro'
135 // completion items in LSP.
136 case CodeCompletionResult::RK_Pattern:
137 return CompletionItemKind::Snippet;
138 }
139 llvm_unreachable("Unhandled CodeCompletionResult::ResultKind.");
140}
141
Sam McCall98775c52017-12-04 13:49:59 +0000142/// Get the optional chunk as a string. This function is possibly recursive.
143///
144/// The parameter info for each parameter is appended to the Parameters.
Kadir Cetinkayae486e372018-08-13 08:40:05 +0000145std::string getOptionalParameters(const CodeCompletionString &CCS,
146 std::vector<ParameterInformation> &Parameters,
147 SignatureQualitySignals &Signal) {
Sam McCall98775c52017-12-04 13:49:59 +0000148 std::string Result;
149 for (const auto &Chunk : CCS) {
150 switch (Chunk.Kind) {
151 case CodeCompletionString::CK_Optional:
152 assert(Chunk.Optional &&
153 "Expected the optional code completion string to be non-null.");
Kadir Cetinkayae486e372018-08-13 08:40:05 +0000154 Result += getOptionalParameters(*Chunk.Optional, Parameters, Signal);
Sam McCall98775c52017-12-04 13:49:59 +0000155 break;
156 case CodeCompletionString::CK_VerticalSpace:
157 break;
158 case CodeCompletionString::CK_Placeholder:
159 // A string that acts as a placeholder for, e.g., a function call
160 // argument.
161 // Intentional fallthrough here.
162 case CodeCompletionString::CK_CurrentParameter: {
163 // A piece of text that describes the parameter that corresponds to
164 // the code-completion location within a function call, message send,
165 // macro invocation, etc.
166 Result += Chunk.Text;
167 ParameterInformation Info;
168 Info.label = Chunk.Text;
169 Parameters.push_back(std::move(Info));
Kadir Cetinkayae486e372018-08-13 08:40:05 +0000170 Signal.ContainsActiveParameter = true;
171 Signal.NumberOfOptionalParameters++;
Sam McCall98775c52017-12-04 13:49:59 +0000172 break;
173 }
174 default:
175 Result += Chunk.Text;
176 break;
177 }
178 }
179 return Result;
180}
181
Eric Liu63f419a2018-05-15 15:29:32 +0000182/// Creates a `HeaderFile` from \p Header which can be either a URI or a literal
183/// include.
Sam McCallc008af62018-10-20 15:30:37 +0000184static Expected<HeaderFile> toHeaderFile(StringRef Header, StringRef HintPath) {
Eric Liu63f419a2018-05-15 15:29:32 +0000185 if (isLiteralInclude(Header))
186 return HeaderFile{Header.str(), /*Verbatim=*/true};
187 auto U = URI::parse(Header);
188 if (!U)
189 return U.takeError();
190
191 auto IncludePath = URI::includeSpelling(*U);
192 if (!IncludePath)
193 return IncludePath.takeError();
194 if (!IncludePath->empty())
195 return HeaderFile{std::move(*IncludePath), /*Verbatim=*/true};
196
197 auto Resolved = URI::resolve(*U, HintPath);
198 if (!Resolved)
199 return Resolved.takeError();
200 return HeaderFile{std::move(*Resolved), /*Verbatim=*/false};
201}
202
Sam McCall545a20d2018-01-19 14:34:02 +0000203/// A code completion result, in clang-native form.
Sam McCall98775c52017-12-04 13:49:59 +0000204/// It may be promoted to a CompletionItem if it's among the top-ranked results.
205struct CompletionCandidate {
Sam McCallc008af62018-10-20 15:30:37 +0000206 StringRef Name; // Used for filtering and sorting.
Sam McCall545a20d2018-01-19 14:34:02 +0000207 // We may have a result from Sema, from the index, or both.
208 const CodeCompletionResult *SemaResult = nullptr;
209 const Symbol *IndexResult = nullptr;
Sam McCallc008af62018-10-20 15:30:37 +0000210 SmallVector<StringRef, 1> RankedIncludeHeaders;
Sam McCall98775c52017-12-04 13:49:59 +0000211
Sam McCallc18c2802018-06-15 11:06:29 +0000212 // Returns a token identifying the overload set this is part of.
213 // 0 indicates it's not part of any overload set.
214 size_t overloadSet() const {
215 SmallString<256> Scratch;
216 if (IndexResult) {
217 switch (IndexResult->SymInfo.Kind) {
218 case index::SymbolKind::ClassMethod:
219 case index::SymbolKind::InstanceMethod:
220 case index::SymbolKind::StaticMethod:
221 assert(false && "Don't expect members from index in code completion");
222 // fall through
223 case index::SymbolKind::Function:
224 // We can't group overloads together that need different #includes.
225 // This could break #include insertion.
226 return hash_combine(
227 (IndexResult->Scope + IndexResult->Name).toStringRef(Scratch),
Eric Liu83f63e42018-09-03 10:18:21 +0000228 headerToInsertIfAllowed().getValueOr(""));
Sam McCallc18c2802018-06-15 11:06:29 +0000229 default:
230 return 0;
231 }
232 }
233 assert(SemaResult);
234 // We need to make sure we're consistent with the IndexResult case!
235 const NamedDecl *D = SemaResult->Declaration;
236 if (!D || !D->isFunctionOrFunctionTemplate())
237 return 0;
238 {
Sam McCallc008af62018-10-20 15:30:37 +0000239 raw_svector_ostream OS(Scratch);
Sam McCallc18c2802018-06-15 11:06:29 +0000240 D->printQualifiedName(OS);
241 }
Eric Liu83f63e42018-09-03 10:18:21 +0000242 return hash_combine(Scratch, headerToInsertIfAllowed().getValueOr(""));
Sam McCallc18c2802018-06-15 11:06:29 +0000243 }
244
Eric Liu83f63e42018-09-03 10:18:21 +0000245 // The best header to include if include insertion is allowed.
Sam McCallc008af62018-10-20 15:30:37 +0000246 Optional<StringRef> headerToInsertIfAllowed() const {
Eric Liu83f63e42018-09-03 10:18:21 +0000247 if (RankedIncludeHeaders.empty())
Sam McCallc008af62018-10-20 15:30:37 +0000248 return None;
Sam McCallc18c2802018-06-15 11:06:29 +0000249 if (SemaResult && SemaResult->Declaration) {
250 // Avoid inserting new #include if the declaration is found in the current
251 // file e.g. the symbol is forward declared.
252 auto &SM = SemaResult->Declaration->getASTContext().getSourceManager();
253 for (const Decl *RD : SemaResult->Declaration->redecls())
Stephen Kelly43465bf2018-08-09 22:42:26 +0000254 if (SM.isInMainFile(SM.getExpansionLoc(RD->getBeginLoc())))
Sam McCallc008af62018-10-20 15:30:37 +0000255 return None;
Sam McCallc18c2802018-06-15 11:06:29 +0000256 }
Eric Liu83f63e42018-09-03 10:18:21 +0000257 return RankedIncludeHeaders[0];
Sam McCallc18c2802018-06-15 11:06:29 +0000258 }
259
Sam McCallc008af62018-10-20 15:30:37 +0000260 using Bundle = SmallVector<CompletionCandidate, 4>;
Sam McCall98775c52017-12-04 13:49:59 +0000261};
Sam McCallc18c2802018-06-15 11:06:29 +0000262using ScoredBundle =
Sam McCall27c979a2018-06-29 14:47:57 +0000263 std::pair<CompletionCandidate::Bundle, CodeCompletion::Scores>;
Sam McCallc18c2802018-06-15 11:06:29 +0000264struct ScoredBundleGreater {
265 bool operator()(const ScoredBundle &L, const ScoredBundle &R) {
Sam McCall27c979a2018-06-29 14:47:57 +0000266 if (L.second.Total != R.second.Total)
267 return L.second.Total > R.second.Total;
Sam McCallc18c2802018-06-15 11:06:29 +0000268 return L.first.front().Name <
269 R.first.front().Name; // Earlier name is better.
270 }
271};
Sam McCall98775c52017-12-04 13:49:59 +0000272
Sam McCall27c979a2018-06-29 14:47:57 +0000273// Assembles a code completion out of a bundle of >=1 completion candidates.
274// Many of the expensive strings are only computed at this point, once we know
275// the candidate bundle is going to be returned.
276//
277// Many fields are the same for all candidates in a bundle (e.g. name), and are
278// computed from the first candidate, in the constructor.
279// Others vary per candidate, so add() must be called for remaining candidates.
280struct CodeCompletionBuilder {
281 CodeCompletionBuilder(ASTContext &ASTCtx, const CompletionCandidate &C,
282 CodeCompletionString *SemaCCS,
Sam McCallc008af62018-10-20 15:30:37 +0000283 ArrayRef<std::string> QueryScopes,
Sam McCall27c979a2018-06-29 14:47:57 +0000284 const IncludeInserter &Includes, StringRef FileName,
Sam McCall4c077f92018-09-18 09:08:28 +0000285 CodeCompletionContext::Kind ContextKind,
Sam McCall27c979a2018-06-29 14:47:57 +0000286 const CodeCompleteOptions &Opts)
Kadir Cetinkaya516fcda2018-08-23 12:19:39 +0000287 : ASTCtx(ASTCtx), ExtractDocumentation(Opts.IncludeComments),
288 EnableFunctionArgSnippets(Opts.EnableFunctionArgSnippets) {
Sam McCall27c979a2018-06-29 14:47:57 +0000289 add(C, SemaCCS);
290 if (C.SemaResult) {
Sam McCall4e5742a2018-07-06 11:50:49 +0000291 Completion.Origin |= SymbolOrigin::AST;
Sam McCallc008af62018-10-20 15:30:37 +0000292 Completion.Name = StringRef(SemaCCS->getTypedText());
Eric Liuf433c2d2018-07-18 15:31:14 +0000293 if (Completion.Scope.empty()) {
294 if ((C.SemaResult->Kind == CodeCompletionResult::RK_Declaration) ||
295 (C.SemaResult->Kind == CodeCompletionResult::RK_Pattern))
Sam McCall27c979a2018-06-29 14:47:57 +0000296 if (const auto *D = C.SemaResult->getDeclaration())
Sam McCallc008af62018-10-20 15:30:37 +0000297 if (const auto *ND = dyn_cast<NamedDecl>(D))
Sam McCall27c979a2018-06-29 14:47:57 +0000298 Completion.Scope =
299 splitQualifiedName(printQualifiedName(*ND)).first;
Eric Liuf433c2d2018-07-18 15:31:14 +0000300 }
Sam McCall4c077f92018-09-18 09:08:28 +0000301 Completion.Kind = toCompletionItemKind(
302 C.SemaResult->Kind, C.SemaResult->Declaration, ContextKind);
Kadir Cetinkaya0ed5d292018-09-27 14:21:07 +0000303 // Sema could provide more info on whether the completion was a file or
304 // folder.
305 if (Completion.Kind == CompletionItemKind::File &&
306 Completion.Name.back() == '/')
307 Completion.Kind = CompletionItemKind::Folder;
Kadir Cetinkaya2f84d912018-08-08 08:59:29 +0000308 for (const auto &FixIt : C.SemaResult->FixIts) {
309 Completion.FixIts.push_back(
310 toTextEdit(FixIt, ASTCtx.getSourceManager(), ASTCtx.getLangOpts()));
311 }
Kirill Bobyrev4a5ff882018-10-07 14:49:41 +0000312 llvm::sort(Completion.FixIts, [](const TextEdit &X, const TextEdit &Y) {
313 return std::tie(X.range.start.line, X.range.start.character) <
314 std::tie(Y.range.start.line, Y.range.start.character);
315 });
Eric Liu6df66002018-09-06 18:52:26 +0000316 Completion.Deprecated |=
317 (C.SemaResult->Availability == CXAvailability_Deprecated);
Sam McCall27c979a2018-06-29 14:47:57 +0000318 }
319 if (C.IndexResult) {
Sam McCall4e5742a2018-07-06 11:50:49 +0000320 Completion.Origin |= C.IndexResult->Origin;
Sam McCall27c979a2018-06-29 14:47:57 +0000321 if (Completion.Scope.empty())
322 Completion.Scope = C.IndexResult->Scope;
323 if (Completion.Kind == CompletionItemKind::Missing)
324 Completion.Kind = toCompletionItemKind(C.IndexResult->SymInfo.Kind);
325 if (Completion.Name.empty())
326 Completion.Name = C.IndexResult->Name;
Eric Liu670c1472018-09-27 18:46:00 +0000327 // If the completion was visible to Sema, no qualifier is needed. This
328 // avoids unneeded qualifiers in cases like with `using ns::X`.
329 if (Completion.RequiredQualifier.empty() && !C.SemaResult) {
330 StringRef ShortestQualifier = C.IndexResult->Scope;
331 for (StringRef Scope : QueryScopes) {
332 StringRef Qualifier = C.IndexResult->Scope;
333 if (Qualifier.consume_front(Scope) &&
334 Qualifier.size() < ShortestQualifier.size())
335 ShortestQualifier = Qualifier;
336 }
337 Completion.RequiredQualifier = ShortestQualifier;
338 }
Eric Liu6df66002018-09-06 18:52:26 +0000339 Completion.Deprecated |= (C.IndexResult->Flags & Symbol::Deprecated);
Sam McCall27c979a2018-06-29 14:47:57 +0000340 }
Eric Liu83f63e42018-09-03 10:18:21 +0000341
342 // Turn absolute path into a literal string that can be #included.
343 auto Inserted =
344 [&](StringRef Header) -> Expected<std::pair<std::string, bool>> {
345 auto ResolvedDeclaring =
346 toHeaderFile(C.IndexResult->CanonicalDeclaration.FileURI, FileName);
347 if (!ResolvedDeclaring)
348 return ResolvedDeclaring.takeError();
349 auto ResolvedInserted = toHeaderFile(Header, FileName);
350 if (!ResolvedInserted)
351 return ResolvedInserted.takeError();
352 return std::make_pair(
353 Includes.calculateIncludePath(*ResolvedDeclaring, *ResolvedInserted),
354 Includes.shouldInsertInclude(*ResolvedDeclaring, *ResolvedInserted));
355 };
356 bool ShouldInsert = C.headerToInsertIfAllowed().hasValue();
357 // Calculate include paths and edits for all possible headers.
358 for (const auto &Inc : C.RankedIncludeHeaders) {
359 if (auto ToInclude = Inserted(Inc)) {
360 CodeCompletion::IncludeCandidate Include;
361 Include.Header = ToInclude->first;
362 if (ToInclude->second && ShouldInsert)
363 Include.Insertion = Includes.insert(ToInclude->first);
364 Completion.Includes.push_back(std::move(Include));
Sam McCall27c979a2018-06-29 14:47:57 +0000365 } else
Sam McCallbed58852018-07-11 10:35:11 +0000366 log("Failed to generate include insertion edits for adding header "
Sam McCall27c979a2018-06-29 14:47:57 +0000367 "(FileURI='{0}', IncludeHeader='{1}') into {2}",
Eric Liu83f63e42018-09-03 10:18:21 +0000368 C.IndexResult->CanonicalDeclaration.FileURI, Inc, FileName);
Sam McCall27c979a2018-06-29 14:47:57 +0000369 }
Eric Liu83f63e42018-09-03 10:18:21 +0000370 // Prefer includes that do not need edits (i.e. already exist).
371 std::stable_partition(Completion.Includes.begin(),
372 Completion.Includes.end(),
373 [](const CodeCompletion::IncludeCandidate &I) {
374 return !I.Insertion.hasValue();
375 });
Sam McCall27c979a2018-06-29 14:47:57 +0000376 }
377
378 void add(const CompletionCandidate &C, CodeCompletionString *SemaCCS) {
379 assert(bool(C.SemaResult) == bool(SemaCCS));
380 Bundled.emplace_back();
381 BundledEntry &S = Bundled.back();
382 if (C.SemaResult) {
383 getSignature(*SemaCCS, &S.Signature, &S.SnippetSuffix,
384 &Completion.RequiredQualifier);
385 S.ReturnType = getReturnType(*SemaCCS);
386 } else if (C.IndexResult) {
387 S.Signature = C.IndexResult->Signature;
388 S.SnippetSuffix = C.IndexResult->CompletionSnippetSuffix;
Sam McCall2e5700f2018-08-31 13:55:01 +0000389 S.ReturnType = C.IndexResult->ReturnType;
Sam McCall27c979a2018-06-29 14:47:57 +0000390 }
391 if (ExtractDocumentation && Completion.Documentation.empty()) {
Sam McCall2e5700f2018-08-31 13:55:01 +0000392 if (C.IndexResult)
393 Completion.Documentation = C.IndexResult->Documentation;
Sam McCall27c979a2018-06-29 14:47:57 +0000394 else if (C.SemaResult)
395 Completion.Documentation = getDocComment(ASTCtx, *C.SemaResult,
396 /*CommentsFromHeader=*/false);
397 }
398 }
399
400 CodeCompletion build() {
401 Completion.ReturnType = summarizeReturnType();
402 Completion.Signature = summarizeSignature();
403 Completion.SnippetSuffix = summarizeSnippet();
404 Completion.BundleSize = Bundled.size();
405 return std::move(Completion);
406 }
407
408private:
409 struct BundledEntry {
410 std::string SnippetSuffix;
411 std::string Signature;
412 std::string ReturnType;
413 };
414
415 // If all BundledEntrys have the same value for a property, return it.
416 template <std::string BundledEntry::*Member>
417 const std::string *onlyValue() const {
418 auto B = Bundled.begin(), E = Bundled.end();
419 for (auto I = B + 1; I != E; ++I)
420 if (I->*Member != B->*Member)
421 return nullptr;
422 return &(B->*Member);
423 }
424
Kadir Cetinkayaf8b85a32018-08-23 13:14:50 +0000425 template <bool BundledEntry::*Member> const bool *onlyValue() const {
426 auto B = Bundled.begin(), E = Bundled.end();
427 for (auto I = B + 1; I != E; ++I)
428 if (I->*Member != B->*Member)
429 return nullptr;
430 return &(B->*Member);
431 }
432
Sam McCall27c979a2018-06-29 14:47:57 +0000433 std::string summarizeReturnType() const {
434 if (auto *RT = onlyValue<&BundledEntry::ReturnType>())
435 return *RT;
436 return "";
437 }
438
439 std::string summarizeSnippet() const {
Kadir Cetinkaya516fcda2018-08-23 12:19:39 +0000440 auto *Snippet = onlyValue<&BundledEntry::SnippetSuffix>();
441 if (!Snippet)
442 // All bundles are function calls.
Ilya Biryukov4f984702018-09-26 05:45:31 +0000443 // FIXME(ibiryukov): sometimes add template arguments to a snippet, e.g.
444 // we need to complete 'forward<$1>($0)'.
Kadir Cetinkaya516fcda2018-08-23 12:19:39 +0000445 return "($0)";
Ilya Biryukov4f984702018-09-26 05:45:31 +0000446 if (EnableFunctionArgSnippets)
447 return *Snippet;
448
449 // Replace argument snippets with a simplified pattern.
450 if (Snippet->empty())
451 return "";
452 if (Completion.Kind == CompletionItemKind::Function ||
453 Completion.Kind == CompletionItemKind::Method) {
454 // Functions snippets can be of 2 types:
455 // - containing only function arguments, e.g.
456 // foo(${1:int p1}, ${2:int p2});
457 // We transform this pattern to '($0)' or '()'.
458 // - template arguments and function arguments, e.g.
459 // foo<${1:class}>(${2:int p1}).
460 // We transform this pattern to '<$1>()$0' or '<$0>()'.
461
Sam McCallc008af62018-10-20 15:30:37 +0000462 bool EmptyArgs = StringRef(*Snippet).endswith("()");
Ilya Biryukov4f984702018-09-26 05:45:31 +0000463 if (Snippet->front() == '<')
464 return EmptyArgs ? "<$1>()$0" : "<$1>($0)";
465 if (Snippet->front() == '(')
466 return EmptyArgs ? "()" : "($0)";
467 return *Snippet; // Not an arg snippet?
468 }
469 if (Completion.Kind == CompletionItemKind::Reference ||
470 Completion.Kind == CompletionItemKind::Class) {
471 if (Snippet->front() != '<')
472 return *Snippet; // Not an arg snippet?
473
474 // Classes and template using aliases can only have template arguments,
475 // e.g. Foo<${1:class}>.
Sam McCallc008af62018-10-20 15:30:37 +0000476 if (StringRef(*Snippet).endswith("<>"))
Ilya Biryukov4f984702018-09-26 05:45:31 +0000477 return "<>"; // can happen with defaulted template arguments.
478 return "<$0>";
479 }
Kadir Cetinkaya516fcda2018-08-23 12:19:39 +0000480 return *Snippet;
Sam McCall27c979a2018-06-29 14:47:57 +0000481 }
482
483 std::string summarizeSignature() const {
484 if (auto *Signature = onlyValue<&BundledEntry::Signature>())
485 return *Signature;
486 // All bundles are function calls.
487 return "(…)";
488 }
489
490 ASTContext &ASTCtx;
491 CodeCompletion Completion;
492 SmallVector<BundledEntry, 1> Bundled;
493 bool ExtractDocumentation;
Kadir Cetinkaya516fcda2018-08-23 12:19:39 +0000494 bool EnableFunctionArgSnippets;
Sam McCall27c979a2018-06-29 14:47:57 +0000495};
496
Sam McCall545a20d2018-01-19 14:34:02 +0000497// Determine the symbol ID for a Sema code completion result, if possible.
Sam McCallc008af62018-10-20 15:30:37 +0000498Optional<SymbolID> getSymbolID(const CodeCompletionResult &R,
499 const SourceManager &SM) {
Sam McCall545a20d2018-01-19 14:34:02 +0000500 switch (R.Kind) {
501 case CodeCompletionResult::RK_Declaration:
502 case CodeCompletionResult::RK_Pattern: {
Haojian Wuc6ddb462018-08-07 08:57:52 +0000503 return clang::clangd::getSymbolID(R.Declaration);
Sam McCall545a20d2018-01-19 14:34:02 +0000504 }
505 case CodeCompletionResult::RK_Macro:
Eric Liud25f1212018-09-06 09:59:37 +0000506 return clang::clangd::getSymbolID(*R.Macro, R.MacroDefInfo, SM);
Sam McCall545a20d2018-01-19 14:34:02 +0000507 case CodeCompletionResult::RK_Keyword:
508 return None;
509 }
510 llvm_unreachable("unknown CodeCompletionResult kind");
511}
512
Haojian Wu061c73e2018-01-23 11:37:26 +0000513// Scopes of the paritial identifier we're trying to complete.
514// It is used when we query the index for more completion results.
Eric Liu6f648df2017-12-19 16:50:37 +0000515struct SpecifiedScope {
Haojian Wu061c73e2018-01-23 11:37:26 +0000516 // The scopes we should look in, determined by Sema.
517 //
518 // If the qualifier was fully resolved, we look for completions in these
519 // scopes; if there is an unresolved part of the qualifier, it should be
520 // resolved within these scopes.
521 //
522 // Examples of qualified completion:
523 //
524 // "::vec" => {""}
525 // "using namespace std; ::vec^" => {"", "std::"}
526 // "namespace ns {using namespace std;} ns::^" => {"ns::", "std::"}
527 // "std::vec^" => {""} // "std" unresolved
528 //
529 // Examples of unqualified completion:
530 //
531 // "vec^" => {""}
532 // "using namespace std; vec^" => {"", "std::"}
533 // "using namespace std; namespace ns { vec^ }" => {"ns::", "std::", ""}
534 //
535 // "" for global namespace, "ns::" for normal namespace.
536 std::vector<std::string> AccessibleScopes;
537 // The full scope qualifier as typed by the user (without the leading "::").
538 // Set if the qualifier is not fully resolved by Sema.
Sam McCallc008af62018-10-20 15:30:37 +0000539 Optional<std::string> UnresolvedQualifier;
Sam McCall545a20d2018-01-19 14:34:02 +0000540
Haojian Wu061c73e2018-01-23 11:37:26 +0000541 // Construct scopes being queried in indexes.
542 // This method format the scopes to match the index request representation.
543 std::vector<std::string> scopesForIndexQuery() {
544 std::vector<std::string> Results;
Sam McCallc008af62018-10-20 15:30:37 +0000545 for (StringRef AS : AccessibleScopes) {
Haojian Wu061c73e2018-01-23 11:37:26 +0000546 Results.push_back(AS);
547 if (UnresolvedQualifier)
548 Results.back() += *UnresolvedQualifier;
549 }
550 return Results;
Sam McCall545a20d2018-01-19 14:34:02 +0000551 }
Eric Liu6f648df2017-12-19 16:50:37 +0000552};
553
Eric Liu670c1472018-09-27 18:46:00 +0000554// Get all scopes that will be queried in indexes and whether symbols from
Eric Liu3fac4ef2018-10-17 11:19:02 +0000555// any scope is allowed. The first scope in the list is the preferred scope
556// (e.g. enclosing namespace).
Eric Liu670c1472018-09-27 18:46:00 +0000557std::pair<std::vector<std::string>, bool>
Eric Liu3fac4ef2018-10-17 11:19:02 +0000558getQueryScopes(CodeCompletionContext &CCContext, const Sema &CCSema,
Eric Liu670c1472018-09-27 18:46:00 +0000559 const CodeCompleteOptions &Opts) {
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000560 auto GetAllAccessibleScopes = [](CodeCompletionContext &CCContext) {
Haojian Wu061c73e2018-01-23 11:37:26 +0000561 SpecifiedScope Info;
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000562 for (auto *Context : CCContext.getVisitedContexts()) {
Haojian Wu061c73e2018-01-23 11:37:26 +0000563 if (isa<TranslationUnitDecl>(Context))
564 Info.AccessibleScopes.push_back(""); // global namespace
Mikael Holmenfa41add2018-10-18 06:00:39 +0000565 else if (isa<NamespaceDecl>(Context))
Eric Liu3fac4ef2018-10-17 11:19:02 +0000566 Info.AccessibleScopes.push_back(printNamespaceScope(*Context));
Haojian Wu061c73e2018-01-23 11:37:26 +0000567 }
568 return Info;
569 };
570
571 auto SS = CCContext.getCXXScopeSpecifier();
572
573 // Unqualified completion (e.g. "vec^").
574 if (!SS) {
Eric Liu3fac4ef2018-10-17 11:19:02 +0000575 std::vector<std::string> Scopes;
576 std::string EnclosingScope = printNamespaceScope(*CCSema.CurContext);
577 Scopes.push_back(EnclosingScope);
578 for (auto &S : GetAllAccessibleScopes(CCContext).scopesForIndexQuery()) {
579 if (EnclosingScope != S)
580 Scopes.push_back(std::move(S));
581 }
Eric Liu670c1472018-09-27 18:46:00 +0000582 // Allow AllScopes completion only for there is no explicit scope qualifier.
583 return {Scopes, Opts.AllScopes};
Haojian Wu061c73e2018-01-23 11:37:26 +0000584 }
585
586 // Qualified completion ("std::vec^"), we have two cases depending on whether
587 // the qualifier can be resolved by Sema.
588 if ((*SS)->isValid()) { // Resolved qualifier.
Eric Liu670c1472018-09-27 18:46:00 +0000589 return {GetAllAccessibleScopes(CCContext).scopesForIndexQuery(), false};
Haojian Wu061c73e2018-01-23 11:37:26 +0000590 }
591
592 // Unresolved qualifier.
593 // FIXME: When Sema can resolve part of a scope chain (e.g.
594 // "known::unknown::id"), we should expand the known part ("known::") rather
595 // than treating the whole thing as unknown.
596 SpecifiedScope Info;
597 Info.AccessibleScopes.push_back(""); // global namespace
598
599 Info.UnresolvedQualifier =
Eric Liu3fac4ef2018-10-17 11:19:02 +0000600 Lexer::getSourceText(CharSourceRange::getCharRange((*SS)->getRange()),
601 CCSema.SourceMgr, clang::LangOptions())
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000602 .ltrim("::");
Haojian Wu061c73e2018-01-23 11:37:26 +0000603 // Sema excludes the trailing "::".
604 if (!Info.UnresolvedQualifier->empty())
605 *Info.UnresolvedQualifier += "::";
606
Eric Liu670c1472018-09-27 18:46:00 +0000607 return {Info.scopesForIndexQuery(), false};
Haojian Wu061c73e2018-01-23 11:37:26 +0000608}
609
Eric Liu42abe412018-05-24 11:20:19 +0000610// Should we perform index-based completion in a context of the specified kind?
611// FIXME: consider allowing completion, but restricting the result types.
612bool contextAllowsIndex(enum CodeCompletionContext::Kind K) {
613 switch (K) {
614 case CodeCompletionContext::CCC_TopLevel:
615 case CodeCompletionContext::CCC_ObjCInterface:
616 case CodeCompletionContext::CCC_ObjCImplementation:
617 case CodeCompletionContext::CCC_ObjCIvarList:
618 case CodeCompletionContext::CCC_ClassStructUnion:
619 case CodeCompletionContext::CCC_Statement:
620 case CodeCompletionContext::CCC_Expression:
621 case CodeCompletionContext::CCC_ObjCMessageReceiver:
622 case CodeCompletionContext::CCC_EnumTag:
623 case CodeCompletionContext::CCC_UnionTag:
624 case CodeCompletionContext::CCC_ClassOrStructTag:
625 case CodeCompletionContext::CCC_ObjCProtocolName:
626 case CodeCompletionContext::CCC_Namespace:
627 case CodeCompletionContext::CCC_Type:
628 case CodeCompletionContext::CCC_Name: // FIXME: why does ns::^ give this?
629 case CodeCompletionContext::CCC_PotentiallyQualifiedName:
630 case CodeCompletionContext::CCC_ParenthesizedExpression:
631 case CodeCompletionContext::CCC_ObjCInterfaceName:
632 case CodeCompletionContext::CCC_ObjCCategoryName:
633 return true;
634 case CodeCompletionContext::CCC_Other: // Be conservative.
635 case CodeCompletionContext::CCC_OtherWithMacros:
636 case CodeCompletionContext::CCC_DotMemberAccess:
637 case CodeCompletionContext::CCC_ArrowMemberAccess:
638 case CodeCompletionContext::CCC_ObjCPropertyAccess:
639 case CodeCompletionContext::CCC_MacroName:
640 case CodeCompletionContext::CCC_MacroNameUse:
641 case CodeCompletionContext::CCC_PreprocessorExpression:
642 case CodeCompletionContext::CCC_PreprocessorDirective:
643 case CodeCompletionContext::CCC_NaturalLanguage:
644 case CodeCompletionContext::CCC_SelectorName:
645 case CodeCompletionContext::CCC_TypeQualifiers:
646 case CodeCompletionContext::CCC_ObjCInstanceMessage:
647 case CodeCompletionContext::CCC_ObjCClassMessage:
Sam McCall4c077f92018-09-18 09:08:28 +0000648 case CodeCompletionContext::CCC_IncludedFile:
Eric Liu42abe412018-05-24 11:20:19 +0000649 case CodeCompletionContext::CCC_Recovery:
650 return false;
651 }
652 llvm_unreachable("unknown code completion context");
653}
654
Sam McCall4caa8512018-06-07 12:49:17 +0000655// Some member calls are blacklisted because they're so rarely useful.
656static bool isBlacklistedMember(const NamedDecl &D) {
657 // Destructor completion is rarely useful, and works inconsistently.
658 // (s.^ completes ~string, but s.~st^ is an error).
659 if (D.getKind() == Decl::CXXDestructor)
660 return true;
661 // Injected name may be useful for A::foo(), but who writes A::A::foo()?
662 if (auto *R = dyn_cast_or_null<RecordDecl>(&D))
663 if (R->isInjectedClassName())
664 return true;
665 // Explicit calls to operators are also rare.
666 auto NameKind = D.getDeclName().getNameKind();
667 if (NameKind == DeclarationName::CXXOperatorName ||
668 NameKind == DeclarationName::CXXLiteralOperatorName ||
669 NameKind == DeclarationName::CXXConversionFunctionName)
670 return true;
671 return false;
672}
673
Sam McCall545a20d2018-01-19 14:34:02 +0000674// The CompletionRecorder captures Sema code-complete output, including context.
675// It filters out ignored results (but doesn't apply fuzzy-filtering yet).
676// It doesn't do scoring or conversion to CompletionItem yet, as we want to
677// merge with index results first.
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000678// Generally the fields and methods of this object should only be used from
679// within the callback.
Sam McCall545a20d2018-01-19 14:34:02 +0000680struct CompletionRecorder : public CodeCompleteConsumer {
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000681 CompletionRecorder(const CodeCompleteOptions &Opts,
Sam McCallc008af62018-10-20 15:30:37 +0000682 unique_function<void()> ResultsCallback)
Sam McCall545a20d2018-01-19 14:34:02 +0000683 : CodeCompleteConsumer(Opts.getClangCompleteOpts(),
Sam McCall98775c52017-12-04 13:49:59 +0000684 /*OutputIsBinary=*/false),
Sam McCall545a20d2018-01-19 14:34:02 +0000685 CCContext(CodeCompletionContext::CCC_Other), Opts(Opts),
686 CCAllocator(std::make_shared<GlobalCodeCompletionAllocator>()),
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000687 CCTUInfo(CCAllocator), ResultsCallback(std::move(ResultsCallback)) {
688 assert(this->ResultsCallback);
689 }
690
Sam McCall545a20d2018-01-19 14:34:02 +0000691 std::vector<CodeCompletionResult> Results;
692 CodeCompletionContext CCContext;
693 Sema *CCSema = nullptr; // Sema that created the results.
694 // FIXME: Sema is scary. Can we store ASTContext and Preprocessor, instead?
Sam McCall98775c52017-12-04 13:49:59 +0000695
Sam McCall545a20d2018-01-19 14:34:02 +0000696 void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context,
697 CodeCompletionResult *InResults,
Sam McCall98775c52017-12-04 13:49:59 +0000698 unsigned NumResults) override final {
Eric Liu485074f2018-07-11 13:15:31 +0000699 // Results from recovery mode are generally useless, and the callback after
700 // recovery (if any) is usually more interesting. To make sure we handle the
701 // future callback from sema, we just ignore all callbacks in recovery mode,
702 // as taking only results from recovery mode results in poor completion
703 // results.
704 // FIXME: in case there is no future sema completion callback after the
705 // recovery mode, we might still want to provide some results (e.g. trivial
706 // identifier-based completion).
707 if (Context.getKind() == CodeCompletionContext::CCC_Recovery) {
708 log("Code complete: Ignoring sema code complete callback with Recovery "
709 "context.");
710 return;
711 }
Eric Liu42abe412018-05-24 11:20:19 +0000712 // If a callback is called without any sema result and the context does not
713 // support index-based completion, we simply skip it to give way to
714 // potential future callbacks with results.
715 if (NumResults == 0 && !contextAllowsIndex(Context.getKind()))
716 return;
Ilya Biryukov94da7bd2018-03-16 15:23:44 +0000717 if (CCSema) {
Sam McCallbed58852018-07-11 10:35:11 +0000718 log("Multiple code complete callbacks (parser backtracked?). "
Ilya Biryukov94da7bd2018-03-16 15:23:44 +0000719 "Dropping results from context {0}, keeping results from {1}.",
Eric Liu42abe412018-05-24 11:20:19 +0000720 getCompletionKindString(Context.getKind()),
Sam McCallbed58852018-07-11 10:35:11 +0000721 getCompletionKindString(this->CCContext.getKind()));
Ilya Biryukov94da7bd2018-03-16 15:23:44 +0000722 return;
723 }
Sam McCall545a20d2018-01-19 14:34:02 +0000724 // Record the completion context.
Sam McCall545a20d2018-01-19 14:34:02 +0000725 CCSema = &S;
726 CCContext = Context;
Eric Liu6f648df2017-12-19 16:50:37 +0000727
Sam McCall545a20d2018-01-19 14:34:02 +0000728 // Retain the results we might want.
Sam McCall98775c52017-12-04 13:49:59 +0000729 for (unsigned I = 0; I < NumResults; ++I) {
Sam McCall545a20d2018-01-19 14:34:02 +0000730 auto &Result = InResults[I];
Sam McCalle8437cb2018-10-24 13:51:44 +0000731 // Class members that are shadowed by subclasses are usually noise.
732 if (Result.Hidden && Result.Declaration &&
733 Result.Declaration->isCXXClassMember())
Ilya Biryukovf60bf342018-01-10 13:51:09 +0000734 continue;
Sam McCall545a20d2018-01-19 14:34:02 +0000735 if (!Opts.IncludeIneligibleResults &&
Sam McCall98775c52017-12-04 13:49:59 +0000736 (Result.Availability == CXAvailability_NotAvailable ||
737 Result.Availability == CXAvailability_NotAccessible))
738 continue;
Sam McCall4caa8512018-06-07 12:49:17 +0000739 if (Result.Declaration &&
740 !Context.getBaseType().isNull() // is this a member-access context?
741 && isBlacklistedMember(*Result.Declaration))
Sam McCalld2a95922018-01-22 21:05:00 +0000742 continue;
Ilya Biryukov53d6d932018-03-06 16:45:21 +0000743 // We choose to never append '::' to completion results in clangd.
744 Result.StartsNestedNameSpecifier = false;
Sam McCall545a20d2018-01-19 14:34:02 +0000745 Results.push_back(Result);
Sam McCall98775c52017-12-04 13:49:59 +0000746 }
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000747 ResultsCallback();
Sam McCall98775c52017-12-04 13:49:59 +0000748 }
749
Sam McCall545a20d2018-01-19 14:34:02 +0000750 CodeCompletionAllocator &getAllocator() override { return *CCAllocator; }
Sam McCall98775c52017-12-04 13:49:59 +0000751 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
752
Sam McCall545a20d2018-01-19 14:34:02 +0000753 // Returns the filtering/sorting name for Result, which must be from Results.
754 // Returned string is owned by this recorder (or the AST).
Sam McCallc008af62018-10-20 15:30:37 +0000755 StringRef getName(const CodeCompletionResult &Result) {
Sam McCall98775c52017-12-04 13:49:59 +0000756 switch (Result.Kind) {
757 case CodeCompletionResult::RK_Declaration:
758 if (auto *ID = Result.Declaration->getIdentifier())
Sam McCall545a20d2018-01-19 14:34:02 +0000759 return ID->getName();
Sam McCall98775c52017-12-04 13:49:59 +0000760 break;
761 case CodeCompletionResult::RK_Keyword:
Sam McCall545a20d2018-01-19 14:34:02 +0000762 return Result.Keyword;
Sam McCall98775c52017-12-04 13:49:59 +0000763 case CodeCompletionResult::RK_Macro:
Sam McCall545a20d2018-01-19 14:34:02 +0000764 return Result.Macro->getName();
Sam McCall98775c52017-12-04 13:49:59 +0000765 case CodeCompletionResult::RK_Pattern:
Sam McCall545a20d2018-01-19 14:34:02 +0000766 return Result.Pattern->getTypedText();
Sam McCall98775c52017-12-04 13:49:59 +0000767 }
Ilya Biryukov43714502018-05-16 12:32:44 +0000768 auto *CCS = codeCompletionString(Result);
Sam McCall545a20d2018-01-19 14:34:02 +0000769 return CCS->getTypedText();
Sam McCall98775c52017-12-04 13:49:59 +0000770 }
771
Sam McCall545a20d2018-01-19 14:34:02 +0000772 // Build a CodeCompletion string for R, which must be from Results.
773 // The CCS will be owned by this recorder.
Ilya Biryukov43714502018-05-16 12:32:44 +0000774 CodeCompletionString *codeCompletionString(const CodeCompletionResult &R) {
Sam McCall545a20d2018-01-19 14:34:02 +0000775 // CodeCompletionResult doesn't seem to be const-correct. We own it, anyway.
776 return const_cast<CodeCompletionResult &>(R).CreateCodeCompletionString(
Ilya Biryukov43714502018-05-16 12:32:44 +0000777 *CCSema, CCContext, *CCAllocator, CCTUInfo,
778 /*IncludeBriefComments=*/false);
Sam McCall98775c52017-12-04 13:49:59 +0000779 }
780
Sam McCall545a20d2018-01-19 14:34:02 +0000781private:
782 CodeCompleteOptions Opts;
783 std::shared_ptr<GlobalCodeCompletionAllocator> CCAllocator;
Sam McCall98775c52017-12-04 13:49:59 +0000784 CodeCompletionTUInfo CCTUInfo;
Sam McCallc008af62018-10-20 15:30:37 +0000785 unique_function<void()> ResultsCallback;
Sam McCall545a20d2018-01-19 14:34:02 +0000786};
787
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000788struct ScoredSignature {
789 // When set, requires documentation to be requested from the index with this
790 // ID.
Sam McCallc008af62018-10-20 15:30:37 +0000791 Optional<SymbolID> IDForDoc;
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000792 SignatureInformation Signature;
793 SignatureQualitySignals Quality;
794};
Kadir Cetinkayae486e372018-08-13 08:40:05 +0000795
Sam McCall98775c52017-12-04 13:49:59 +0000796class SignatureHelpCollector final : public CodeCompleteConsumer {
Sam McCall98775c52017-12-04 13:49:59 +0000797public:
798 SignatureHelpCollector(const clang::CodeCompleteOptions &CodeCompleteOpts,
Sam McCall046557b2018-09-03 16:37:59 +0000799 const SymbolIndex *Index, SignatureHelp &SigHelp)
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000800 : CodeCompleteConsumer(CodeCompleteOpts,
801 /*OutputIsBinary=*/false),
Sam McCall98775c52017-12-04 13:49:59 +0000802 SigHelp(SigHelp),
803 Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000804 CCTUInfo(Allocator), Index(Index) {}
Sam McCall98775c52017-12-04 13:49:59 +0000805
806 void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
807 OverloadCandidate *Candidates,
Ilya Biryukov43c292c2018-08-30 13:14:31 +0000808 unsigned NumCandidates,
809 SourceLocation OpenParLoc) override {
810 assert(!OpenParLoc.isInvalid());
811 SourceManager &SrcMgr = S.getSourceManager();
812 OpenParLoc = SrcMgr.getFileLoc(OpenParLoc);
813 if (SrcMgr.isInMainFile(OpenParLoc))
814 SigHelp.argListStart = sourceLocToPosition(SrcMgr, OpenParLoc);
815 else
816 elog("Location oustide main file in signature help: {0}",
817 OpenParLoc.printToString(SrcMgr));
818
Kadir Cetinkayae486e372018-08-13 08:40:05 +0000819 std::vector<ScoredSignature> ScoredSignatures;
Sam McCall98775c52017-12-04 13:49:59 +0000820 SigHelp.signatures.reserve(NumCandidates);
Kadir Cetinkayae486e372018-08-13 08:40:05 +0000821 ScoredSignatures.reserve(NumCandidates);
Sam McCall98775c52017-12-04 13:49:59 +0000822 // FIXME(rwols): How can we determine the "active overload candidate"?
823 // Right now the overloaded candidates seem to be provided in a "best fit"
824 // order, so I'm not too worried about this.
825 SigHelp.activeSignature = 0;
826 assert(CurrentArg <= (unsigned)std::numeric_limits<int>::max() &&
827 "too many arguments");
828 SigHelp.activeParameter = static_cast<int>(CurrentArg);
829 for (unsigned I = 0; I < NumCandidates; ++I) {
Ilya Biryukov8fd44bb2018-08-14 09:36:32 +0000830 OverloadCandidate Candidate = Candidates[I];
831 // We want to avoid showing instantiated signatures, because they may be
832 // long in some cases (e.g. when 'T' is substituted with 'std::string', we
833 // would get 'std::basic_string<char>').
834 if (auto *Func = Candidate.getFunction()) {
835 if (auto *Pattern = Func->getTemplateInstantiationPattern())
836 Candidate = OverloadCandidate(Pattern);
837 }
838
Sam McCall98775c52017-12-04 13:49:59 +0000839 const auto *CCS = Candidate.CreateSignatureString(
840 CurrentArg, S, *Allocator, CCTUInfo, true);
841 assert(CCS && "Expected the CodeCompletionString to be non-null");
Kadir Cetinkayae486e372018-08-13 08:40:05 +0000842 ScoredSignatures.push_back(processOverloadCandidate(
Ilya Biryukov43714502018-05-16 12:32:44 +0000843 Candidate, *CCS,
Ilya Biryukov5f4a3512018-08-17 09:29:38 +0000844 Candidate.getFunction()
845 ? getDeclComment(S.getASTContext(), *Candidate.getFunction())
846 : ""));
Sam McCall98775c52017-12-04 13:49:59 +0000847 }
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000848
849 // Sema does not load the docs from the preamble, so we need to fetch extra
850 // docs from the index instead.
Sam McCallc008af62018-10-20 15:30:37 +0000851 DenseMap<SymbolID, std::string> FetchedDocs;
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000852 if (Index) {
853 LookupRequest IndexRequest;
854 for (const auto &S : ScoredSignatures) {
855 if (!S.IDForDoc)
856 continue;
857 IndexRequest.IDs.insert(*S.IDForDoc);
858 }
859 Index->lookup(IndexRequest, [&](const Symbol &S) {
Sam McCall2e5700f2018-08-31 13:55:01 +0000860 if (!S.Documentation.empty())
861 FetchedDocs[S.ID] = S.Documentation;
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000862 });
863 log("SigHelp: requested docs for {0} symbols from the index, got {1} "
864 "symbols with non-empty docs in the response",
865 IndexRequest.IDs.size(), FetchedDocs.size());
866 }
867
Kirill Bobyrev4a5ff882018-10-07 14:49:41 +0000868 llvm::sort(
869 ScoredSignatures,
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000870 [](const ScoredSignature &L, const ScoredSignature &R) {
871 // Ordering follows:
872 // - Less number of parameters is better.
873 // - Function is better than FunctionType which is better than
874 // Function Template.
875 // - High score is better.
876 // - Shorter signature is better.
877 // - Alphebatically smaller is better.
878 if (L.Quality.NumberOfParameters != R.Quality.NumberOfParameters)
879 return L.Quality.NumberOfParameters < R.Quality.NumberOfParameters;
880 if (L.Quality.NumberOfOptionalParameters !=
881 R.Quality.NumberOfOptionalParameters)
882 return L.Quality.NumberOfOptionalParameters <
883 R.Quality.NumberOfOptionalParameters;
884 if (L.Quality.Kind != R.Quality.Kind) {
885 using OC = CodeCompleteConsumer::OverloadCandidate;
886 switch (L.Quality.Kind) {
887 case OC::CK_Function:
888 return true;
889 case OC::CK_FunctionType:
890 return R.Quality.Kind != OC::CK_Function;
891 case OC::CK_FunctionTemplate:
892 return false;
893 }
894 llvm_unreachable("Unknown overload candidate type.");
895 }
896 if (L.Signature.label.size() != R.Signature.label.size())
897 return L.Signature.label.size() < R.Signature.label.size();
898 return L.Signature.label < R.Signature.label;
899 });
900
901 for (auto &SS : ScoredSignatures) {
902 auto IndexDocIt =
903 SS.IDForDoc ? FetchedDocs.find(*SS.IDForDoc) : FetchedDocs.end();
904 if (IndexDocIt != FetchedDocs.end())
905 SS.Signature.documentation = IndexDocIt->second;
906
907 SigHelp.signatures.push_back(std::move(SS.Signature));
908 }
Sam McCall98775c52017-12-04 13:49:59 +0000909 }
910
911 GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
912
913 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
914
915private:
Eric Liu63696e12017-12-20 17:24:31 +0000916 // FIXME(ioeric): consider moving CodeCompletionString logic here to
917 // CompletionString.h.
Kadir Cetinkayae486e372018-08-13 08:40:05 +0000918 ScoredSignature processOverloadCandidate(const OverloadCandidate &Candidate,
919 const CodeCompletionString &CCS,
Sam McCallc008af62018-10-20 15:30:37 +0000920 StringRef DocComment) const {
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000921 SignatureInformation Signature;
Kadir Cetinkayae486e372018-08-13 08:40:05 +0000922 SignatureQualitySignals Signal;
Sam McCall98775c52017-12-04 13:49:59 +0000923 const char *ReturnType = nullptr;
924
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000925 Signature.documentation = formatDocumentation(CCS, DocComment);
Kadir Cetinkayae486e372018-08-13 08:40:05 +0000926 Signal.Kind = Candidate.getKind();
Sam McCall98775c52017-12-04 13:49:59 +0000927
928 for (const auto &Chunk : CCS) {
929 switch (Chunk.Kind) {
930 case CodeCompletionString::CK_ResultType:
931 // A piece of text that describes the type of an entity or,
932 // for functions and methods, the return type.
933 assert(!ReturnType && "Unexpected CK_ResultType");
934 ReturnType = Chunk.Text;
935 break;
936 case CodeCompletionString::CK_Placeholder:
937 // A string that acts as a placeholder for, e.g., a function call
938 // argument.
939 // Intentional fallthrough here.
940 case CodeCompletionString::CK_CurrentParameter: {
941 // A piece of text that describes the parameter that corresponds to
942 // the code-completion location within a function call, message send,
943 // macro invocation, etc.
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000944 Signature.label += Chunk.Text;
Sam McCall98775c52017-12-04 13:49:59 +0000945 ParameterInformation Info;
946 Info.label = Chunk.Text;
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000947 Signature.parameters.push_back(std::move(Info));
Kadir Cetinkayae486e372018-08-13 08:40:05 +0000948 Signal.NumberOfParameters++;
949 Signal.ContainsActiveParameter = true;
Sam McCall98775c52017-12-04 13:49:59 +0000950 break;
951 }
952 case CodeCompletionString::CK_Optional: {
953 // The rest of the parameters are defaulted/optional.
954 assert(Chunk.Optional &&
955 "Expected the optional code completion string to be non-null.");
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000956 Signature.label += getOptionalParameters(*Chunk.Optional,
957 Signature.parameters, Signal);
Sam McCall98775c52017-12-04 13:49:59 +0000958 break;
959 }
960 case CodeCompletionString::CK_VerticalSpace:
961 break;
962 default:
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000963 Signature.label += Chunk.Text;
Sam McCall98775c52017-12-04 13:49:59 +0000964 break;
965 }
966 }
967 if (ReturnType) {
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000968 Signature.label += " -> ";
969 Signature.label += ReturnType;
Sam McCall98775c52017-12-04 13:49:59 +0000970 }
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000971 dlog("Signal for {0}: {1}", Signature, Signal);
972 ScoredSignature Result;
973 Result.Signature = std::move(Signature);
974 Result.Quality = Signal;
975 Result.IDForDoc =
976 Result.Signature.documentation.empty() && Candidate.getFunction()
977 ? clangd::getSymbolID(Candidate.getFunction())
Sam McCallc008af62018-10-20 15:30:37 +0000978 : None;
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000979 return Result;
Sam McCall98775c52017-12-04 13:49:59 +0000980 }
981
982 SignatureHelp &SigHelp;
983 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
984 CodeCompletionTUInfo CCTUInfo;
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000985 const SymbolIndex *Index;
Sam McCall98775c52017-12-04 13:49:59 +0000986}; // SignatureHelpCollector
987
Sam McCall545a20d2018-01-19 14:34:02 +0000988struct SemaCompleteInput {
989 PathRef FileName;
990 const tooling::CompileCommand &Command;
Eric Liub1d75422018-10-02 10:43:55 +0000991 const PreambleData *Preamble;
Sam McCall545a20d2018-01-19 14:34:02 +0000992 StringRef Contents;
993 Position Pos;
Sam McCallc008af62018-10-20 15:30:37 +0000994 IntrusiveRefCntPtr<vfs::FileSystem> VFS;
Sam McCall545a20d2018-01-19 14:34:02 +0000995 std::shared_ptr<PCHContainerOperations> PCHs;
996};
997
998// Invokes Sema code completion on a file.
Sam McCall3f0243f2018-07-03 08:09:29 +0000999// If \p Includes is set, it will be updated based on the compiler invocation.
Sam McCalld1a7a372018-01-31 13:40:48 +00001000bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
Sam McCall545a20d2018-01-19 14:34:02 +00001001 const clang::CodeCompleteOptions &Options,
Eric Liu63f419a2018-05-15 15:29:32 +00001002 const SemaCompleteInput &Input,
Sam McCall3f0243f2018-07-03 08:09:29 +00001003 IncludeStructure *Includes = nullptr) {
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001004 trace::Span Tracer("Sema completion");
Sam McCall98775c52017-12-04 13:49:59 +00001005 std::vector<const char *> ArgStrs;
Sam McCall545a20d2018-01-19 14:34:02 +00001006 for (const auto &S : Input.Command.CommandLine)
Sam McCall98775c52017-12-04 13:49:59 +00001007 ArgStrs.push_back(S.c_str());
1008
Ilya Biryukova9cf3112018-02-13 17:15:06 +00001009 if (Input.VFS->setCurrentWorkingDirectory(Input.Command.Directory)) {
1010 log("Couldn't set working directory");
1011 // We run parsing anyway, our lit-tests rely on results for non-existing
1012 // working dirs.
1013 }
Sam McCall98775c52017-12-04 13:49:59 +00001014
Sam McCallc008af62018-10-20 15:30:37 +00001015 IntrusiveRefCntPtr<vfs::FileSystem> VFS = Input.VFS;
Eric Liub1d75422018-10-02 10:43:55 +00001016 if (Input.Preamble && Input.Preamble->StatCache)
1017 VFS = Input.Preamble->StatCache->getConsumingFS(std::move(VFS));
Sam McCall98775c52017-12-04 13:49:59 +00001018 IgnoreDiagnostics DummyDiagsConsumer;
1019 auto CI = createInvocationFromCommandLine(
1020 ArgStrs,
1021 CompilerInstance::createDiagnostics(new DiagnosticOptions,
1022 &DummyDiagsConsumer, false),
Eric Liub1d75422018-10-02 10:43:55 +00001023 VFS);
Ilya Biryukovb6ad25c2018-02-09 13:51:57 +00001024 if (!CI) {
Sam McCallbed58852018-07-11 10:35:11 +00001025 elog("Couldn't create CompilerInvocation");
Ilya Biryukovb6ad25c2018-02-09 13:51:57 +00001026 return false;
1027 }
Ilya Biryukov981a35d2018-05-28 12:11:37 +00001028 auto &FrontendOpts = CI->getFrontendOpts();
1029 FrontendOpts.DisableFree = false;
Sam McCall98775c52017-12-04 13:49:59 +00001030 FrontendOpts.SkipFunctionBodies = true;
Ilya Biryukov981a35d2018-05-28 12:11:37 +00001031 CI->getLangOpts()->CommentOpts.ParseAllComments = true;
1032 // Disable typo correction in Sema.
1033 CI->getLangOpts()->SpellChecking = false;
1034 // Setup code completion.
Sam McCall98775c52017-12-04 13:49:59 +00001035 FrontendOpts.CodeCompleteOpts = Options;
Sam McCall545a20d2018-01-19 14:34:02 +00001036 FrontendOpts.CodeCompletionAt.FileName = Input.FileName;
Sam McCalla4962cc2018-04-27 11:59:28 +00001037 auto Offset = positionToOffset(Input.Contents, Input.Pos);
1038 if (!Offset) {
Sam McCallbed58852018-07-11 10:35:11 +00001039 elog("Code completion position was invalid {0}", Offset.takeError());
Sam McCalla4962cc2018-04-27 11:59:28 +00001040 return false;
1041 }
1042 std::tie(FrontendOpts.CodeCompletionAt.Line,
1043 FrontendOpts.CodeCompletionAt.Column) =
1044 offsetToClangLineColumn(Input.Contents, *Offset);
Sam McCall98775c52017-12-04 13:49:59 +00001045
Sam McCallc008af62018-10-20 15:30:37 +00001046 std::unique_ptr<MemoryBuffer> ContentsBuffer =
1047 MemoryBuffer::getMemBufferCopy(Input.Contents, Input.FileName);
Ilya Biryukov981a35d2018-05-28 12:11:37 +00001048 // The diagnostic options must be set before creating a CompilerInstance.
1049 CI->getDiagnosticOpts().IgnoreWarnings = true;
1050 // We reuse the preamble whether it's valid or not. This is a
1051 // correctness/performance tradeoff: building without a preamble is slow, and
1052 // completion is latency-sensitive.
Sam McCallebef8122018-09-14 12:36:06 +00001053 // However, if we're completing *inside* the preamble section of the draft,
1054 // overriding the preamble will break sema completion. Fortunately we can just
1055 // skip all includes in this case; these completions are really simple.
1056 bool CompletingInPreamble =
1057 ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0).Size >
1058 *Offset;
Ilya Biryukov981a35d2018-05-28 12:11:37 +00001059 // NOTE: we must call BeginSourceFile after prepareCompilerInstance. Otherwise
1060 // the remapped buffers do not get freed.
1061 auto Clang = prepareCompilerInstance(
Eric Liub1d75422018-10-02 10:43:55 +00001062 std::move(CI),
1063 (Input.Preamble && !CompletingInPreamble) ? &Input.Preamble->Preamble
1064 : nullptr,
1065 std::move(ContentsBuffer), std::move(Input.PCHs), std::move(VFS),
Sam McCallebef8122018-09-14 12:36:06 +00001066 DummyDiagsConsumer);
1067 Clang->getPreprocessorOpts().SingleFileParseMode = CompletingInPreamble;
Sam McCall98775c52017-12-04 13:49:59 +00001068 Clang->setCodeCompletionConsumer(Consumer.release());
1069
1070 SyntaxOnlyAction Action;
1071 if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) {
Sam McCallbed58852018-07-11 10:35:11 +00001072 log("BeginSourceFile() failed when running codeComplete for {0}",
Sam McCalld1a7a372018-01-31 13:40:48 +00001073 Input.FileName);
Sam McCall98775c52017-12-04 13:49:59 +00001074 return false;
1075 }
Sam McCall3f0243f2018-07-03 08:09:29 +00001076 if (Includes)
1077 Clang->getPreprocessor().addPPCallbacks(
1078 collectIncludeStructureCallback(Clang->getSourceManager(), Includes));
Sam McCall98775c52017-12-04 13:49:59 +00001079 if (!Action.Execute()) {
Sam McCallbed58852018-07-11 10:35:11 +00001080 log("Execute() failed when running codeComplete for {0}", Input.FileName);
Sam McCall98775c52017-12-04 13:49:59 +00001081 return false;
1082 }
Sam McCall98775c52017-12-04 13:49:59 +00001083 Action.EndSourceFile();
1084
1085 return true;
1086}
1087
Ilya Biryukova907ba42018-05-14 10:50:04 +00001088// Should we allow index completions in the specified context?
1089bool allowIndex(CodeCompletionContext &CC) {
1090 if (!contextAllowsIndex(CC.getKind()))
1091 return false;
1092 // We also avoid ClassName::bar (but allow namespace::bar).
1093 auto Scope = CC.getCXXScopeSpecifier();
1094 if (!Scope)
1095 return true;
1096 NestedNameSpecifier *NameSpec = (*Scope)->getScopeRep();
1097 if (!NameSpec)
1098 return true;
1099 // We only query the index when qualifier is a namespace.
1100 // If it's a class, we rely solely on sema completions.
1101 switch (NameSpec->getKind()) {
1102 case NestedNameSpecifier::Global:
1103 case NestedNameSpecifier::Namespace:
1104 case NestedNameSpecifier::NamespaceAlias:
1105 return true;
1106 case NestedNameSpecifier::Super:
1107 case NestedNameSpecifier::TypeSpec:
1108 case NestedNameSpecifier::TypeSpecWithTemplate:
1109 // Unresolved inside a template.
1110 case NestedNameSpecifier::Identifier:
1111 return false;
1112 }
Ilya Biryukova6556e22018-05-14 11:47:30 +00001113 llvm_unreachable("invalid NestedNameSpecifier kind");
Ilya Biryukova907ba42018-05-14 10:50:04 +00001114}
1115
Eric Liu25d74e92018-08-24 11:23:56 +00001116std::future<SymbolSlab> startAsyncFuzzyFind(const SymbolIndex &Index,
1117 const FuzzyFindRequest &Req) {
1118 return runAsync<SymbolSlab>([&Index, Req]() {
1119 trace::Span Tracer("Async fuzzyFind");
1120 SymbolSlab::Builder Syms;
1121 Index.fuzzyFind(Req, [&Syms](const Symbol &Sym) { Syms.insert(Sym); });
1122 return std::move(Syms).build();
1123 });
1124}
1125
1126// Creates a `FuzzyFindRequest` based on the cached index request from the
1127// last completion, if any, and the speculated completion filter text in the
1128// source code.
Sam McCallc008af62018-10-20 15:30:37 +00001129Optional<FuzzyFindRequest> speculativeFuzzyFindRequestForCompletion(
Eric Liu25d74e92018-08-24 11:23:56 +00001130 FuzzyFindRequest CachedReq, PathRef File, StringRef Content, Position Pos) {
1131 auto Filter = speculateCompletionFilter(Content, Pos);
1132 if (!Filter) {
1133 elog("Failed to speculate filter text for code completion at Pos "
1134 "{0}:{1}: {2}",
1135 Pos.line, Pos.character, Filter.takeError());
Sam McCallc008af62018-10-20 15:30:37 +00001136 return None;
Eric Liu25d74e92018-08-24 11:23:56 +00001137 }
1138 CachedReq.Query = *Filter;
1139 return CachedReq;
1140}
1141
Sam McCall98775c52017-12-04 13:49:59 +00001142} // namespace
1143
1144clang::CodeCompleteOptions CodeCompleteOptions::getClangCompleteOpts() const {
1145 clang::CodeCompleteOptions Result;
1146 Result.IncludeCodePatterns = EnableSnippets && IncludeCodePatterns;
1147 Result.IncludeMacros = IncludeMacros;
Sam McCalld8169a82018-01-18 15:31:30 +00001148 Result.IncludeGlobals = true;
Ilya Biryukov43714502018-05-16 12:32:44 +00001149 // We choose to include full comments and not do doxygen parsing in
1150 // completion.
1151 // FIXME: ideally, we should support doxygen in some form, e.g. do markdown
1152 // formatting of the comments.
1153 Result.IncludeBriefComments = false;
Sam McCall98775c52017-12-04 13:49:59 +00001154
Sam McCall3d139c52018-01-12 18:30:08 +00001155 // When an is used, Sema is responsible for completing the main file,
1156 // the index can provide results from the preamble.
1157 // Tell Sema not to deserialize the preamble to look for results.
1158 Result.LoadExternal = !Index;
Kadir Cetinkaya2f84d912018-08-08 08:59:29 +00001159 Result.IncludeFixIts = IncludeFixIts;
Eric Liu6f648df2017-12-19 16:50:37 +00001160
Sam McCall98775c52017-12-04 13:49:59 +00001161 return Result;
1162}
1163
Eric Liu83f63e42018-09-03 10:18:21 +00001164// Returns the most popular include header for \p Sym. If two headers are
1165// equally popular, prefer the shorter one. Returns empty string if \p Sym has
1166// no include header.
Sam McCallc008af62018-10-20 15:30:37 +00001167SmallVector<StringRef, 1> getRankedIncludes(const Symbol &Sym) {
Eric Liu83f63e42018-09-03 10:18:21 +00001168 auto Includes = Sym.IncludeHeaders;
1169 // Sort in descending order by reference count and header length.
Kirill Bobyrev4a5ff882018-10-07 14:49:41 +00001170 llvm::sort(Includes, [](const Symbol::IncludeHeaderWithReferences &LHS,
1171 const Symbol::IncludeHeaderWithReferences &RHS) {
1172 if (LHS.References == RHS.References)
1173 return LHS.IncludeHeader.size() < RHS.IncludeHeader.size();
1174 return LHS.References > RHS.References;
1175 });
Sam McCallc008af62018-10-20 15:30:37 +00001176 SmallVector<StringRef, 1> Headers;
Eric Liu83f63e42018-09-03 10:18:21 +00001177 for (const auto &Include : Includes)
1178 Headers.push_back(Include.IncludeHeader);
1179 return Headers;
1180}
1181
Sam McCall545a20d2018-01-19 14:34:02 +00001182// Runs Sema-based (AST) and Index-based completion, returns merged results.
1183//
1184// There are a few tricky considerations:
1185// - the AST provides information needed for the index query (e.g. which
1186// namespaces to search in). So Sema must start first.
1187// - we only want to return the top results (Opts.Limit).
1188// Building CompletionItems for everything else is wasteful, so we want to
1189// preserve the "native" format until we're done with scoring.
1190// - the data underlying Sema completion items is owned by the AST and various
1191// other arenas, which must stay alive for us to build CompletionItems.
1192// - we may get duplicate results from Sema and the Index, we need to merge.
1193//
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001194// So we start Sema completion first, and do all our work in its callback.
Sam McCall545a20d2018-01-19 14:34:02 +00001195// We use the Sema context information to query the index.
1196// Then we merge the two result sets, producing items that are Sema/Index/Both.
1197// These items are scored, and the top N are synthesized into the LSP response.
1198// Finally, we can clean up the data structures created by Sema completion.
1199//
1200// Main collaborators are:
1201// - semaCodeComplete sets up the compiler machinery to run code completion.
1202// - CompletionRecorder captures Sema completion results, including context.
1203// - SymbolIndex (Opts.Index) provides index completion results as Symbols
1204// - CompletionCandidates are the result of merging Sema and Index results.
1205// Each candidate points to an underlying CodeCompletionResult (Sema), a
1206// Symbol (Index), or both. It computes the result quality score.
1207// CompletionCandidate also does conversion to CompletionItem (at the end).
1208// - FuzzyMatcher scores how the candidate matches the partial identifier.
1209// This score is combined with the result quality score for the final score.
1210// - TopN determines the results with the best score.
1211class CodeCompleteFlow {
Eric Liuc5105f92018-02-16 14:15:55 +00001212 PathRef FileName;
Sam McCall3f0243f2018-07-03 08:09:29 +00001213 IncludeStructure Includes; // Complete once the compiler runs.
Eric Liu25d74e92018-08-24 11:23:56 +00001214 SpeculativeFuzzyFind *SpecFuzzyFind; // Can be nullptr.
Sam McCall545a20d2018-01-19 14:34:02 +00001215 const CodeCompleteOptions &Opts;
Eric Liu25d74e92018-08-24 11:23:56 +00001216
Sam McCall545a20d2018-01-19 14:34:02 +00001217 // Sema takes ownership of Recorder. Recorder is valid until Sema cleanup.
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001218 CompletionRecorder *Recorder = nullptr;
Sam McCall545a20d2018-01-19 14:34:02 +00001219 int NSema = 0, NIndex = 0, NBoth = 0; // Counters for logging.
1220 bool Incomplete = false; // Would more be available with a higher limit?
Sam McCallc008af62018-10-20 15:30:37 +00001221 Optional<FuzzyMatcher> Filter; // Initialized once Sema runs.
Eric Liu670c1472018-09-27 18:46:00 +00001222 std::vector<std::string> QueryScopes; // Initialized once Sema runs.
Eric Liu3fac4ef2018-10-17 11:19:02 +00001223 // Initialized once QueryScopes is initialized, if there are scopes.
Sam McCallc008af62018-10-20 15:30:37 +00001224 Optional<ScopeDistance> ScopeProximity;
Eric Liu670c1472018-09-27 18:46:00 +00001225 // Whether to query symbols from any scope. Initialized once Sema runs.
1226 bool AllScopes = false;
Sam McCall3f0243f2018-07-03 08:09:29 +00001227 // Include-insertion and proximity scoring rely on the include structure.
1228 // This is available after Sema has run.
Sam McCallc008af62018-10-20 15:30:37 +00001229 Optional<IncludeInserter> Inserter; // Available during runWithSema.
1230 Optional<URIDistance> FileProximity; // Initialized once Sema runs.
Eric Liu25d74e92018-08-24 11:23:56 +00001231 /// Speculative request based on the cached request and the filter text before
1232 /// the cursor.
1233 /// Initialized right before sema run. This is only set if `SpecFuzzyFind` is
1234 /// set and contains a cached request.
Sam McCallc008af62018-10-20 15:30:37 +00001235 Optional<FuzzyFindRequest> SpecReq;
Sam McCall545a20d2018-01-19 14:34:02 +00001236
1237public:
1238 // A CodeCompleteFlow object is only useful for calling run() exactly once.
Sam McCall3f0243f2018-07-03 08:09:29 +00001239 CodeCompleteFlow(PathRef FileName, const IncludeStructure &Includes,
Eric Liu25d74e92018-08-24 11:23:56 +00001240 SpeculativeFuzzyFind *SpecFuzzyFind,
Sam McCall3f0243f2018-07-03 08:09:29 +00001241 const CodeCompleteOptions &Opts)
Eric Liu25d74e92018-08-24 11:23:56 +00001242 : FileName(FileName), Includes(Includes), SpecFuzzyFind(SpecFuzzyFind),
1243 Opts(Opts) {}
Sam McCall545a20d2018-01-19 14:34:02 +00001244
Sam McCall27c979a2018-06-29 14:47:57 +00001245 CodeCompleteResult run(const SemaCompleteInput &SemaCCInput) && {
Sam McCalld1a7a372018-01-31 13:40:48 +00001246 trace::Span Tracer("CodeCompleteFlow");
Eric Liu25d74e92018-08-24 11:23:56 +00001247 if (Opts.Index && SpecFuzzyFind && SpecFuzzyFind->CachedReq.hasValue()) {
1248 assert(!SpecFuzzyFind->Result.valid());
1249 if ((SpecReq = speculativeFuzzyFindRequestForCompletion(
1250 *SpecFuzzyFind->CachedReq, SemaCCInput.FileName,
1251 SemaCCInput.Contents, SemaCCInput.Pos)))
1252 SpecFuzzyFind->Result = startAsyncFuzzyFind(*Opts.Index, *SpecReq);
1253 }
Eric Liu63f419a2018-05-15 15:29:32 +00001254
Sam McCall545a20d2018-01-19 14:34:02 +00001255 // We run Sema code completion first. It builds an AST and calculates:
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001256 // - completion results based on the AST.
Sam McCall545a20d2018-01-19 14:34:02 +00001257 // - partial identifier and context. We need these for the index query.
Sam McCall27c979a2018-06-29 14:47:57 +00001258 CodeCompleteResult Output;
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001259 auto RecorderOwner = llvm::make_unique<CompletionRecorder>(Opts, [&]() {
1260 assert(Recorder && "Recorder is not set");
Sam McCall3f0243f2018-07-03 08:09:29 +00001261 auto Style =
Eric Liu9338a882018-07-03 14:51:23 +00001262 format::getStyle(format::DefaultFormatStyle, SemaCCInput.FileName,
1263 format::DefaultFallbackStyle, SemaCCInput.Contents,
1264 SemaCCInput.VFS.get());
Sam McCall3f0243f2018-07-03 08:09:29 +00001265 if (!Style) {
Sam McCallbed58852018-07-11 10:35:11 +00001266 log("getStyle() failed for file {0}: {1}. Fallback is LLVM style.",
1267 SemaCCInput.FileName, Style.takeError());
Sam McCall3f0243f2018-07-03 08:09:29 +00001268 Style = format::getLLVMStyle();
1269 }
Eric Liu63f419a2018-05-15 15:29:32 +00001270 // If preprocessor was run, inclusions from preprocessor callback should
Sam McCall3f0243f2018-07-03 08:09:29 +00001271 // already be added to Includes.
1272 Inserter.emplace(
1273 SemaCCInput.FileName, SemaCCInput.Contents, *Style,
1274 SemaCCInput.Command.Directory,
1275 Recorder->CCSema->getPreprocessor().getHeaderSearchInfo());
1276 for (const auto &Inc : Includes.MainFileIncludes)
1277 Inserter->addExisting(Inc);
1278
1279 // Most of the cost of file proximity is in initializing the FileDistance
1280 // structures based on the observed includes, once per query. Conceptually
1281 // that happens here (though the per-URI-scheme initialization is lazy).
1282 // The per-result proximity scoring is (amortized) very cheap.
1283 FileDistanceOptions ProxOpts{}; // Use defaults.
1284 const auto &SM = Recorder->CCSema->getSourceManager();
Sam McCallc008af62018-10-20 15:30:37 +00001285 StringMap<SourceParams> ProxSources;
Sam McCall3f0243f2018-07-03 08:09:29 +00001286 for (auto &Entry : Includes.includeDepth(
1287 SM.getFileEntryForID(SM.getMainFileID())->getName())) {
1288 auto &Source = ProxSources[Entry.getKey()];
1289 Source.Cost = Entry.getValue() * ProxOpts.IncludeCost;
1290 // Symbols near our transitive includes are good, but only consider
1291 // things in the same directory or below it. Otherwise there can be
1292 // many false positives.
1293 if (Entry.getValue() > 0)
1294 Source.MaxUpTraversals = 1;
1295 }
1296 FileProximity.emplace(ProxSources, ProxOpts);
1297
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001298 Output = runWithSema();
Sam McCall3f0243f2018-07-03 08:09:29 +00001299 Inserter.reset(); // Make sure this doesn't out-live Clang.
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001300 SPAN_ATTACH(Tracer, "sema_completion_kind",
1301 getCompletionKindString(Recorder->CCContext.getKind()));
Eric Liu670c1472018-09-27 18:46:00 +00001302 log("Code complete: sema context {0}, query scopes [{1}] (AnyScope={2})",
Eric Liubc25ef72018-07-05 08:29:33 +00001303 getCompletionKindString(Recorder->CCContext.getKind()),
Sam McCallc008af62018-10-20 15:30:37 +00001304 join(QueryScopes.begin(), QueryScopes.end(), ","), AllScopes);
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001305 });
1306
1307 Recorder = RecorderOwner.get();
Eric Liu25d74e92018-08-24 11:23:56 +00001308
Sam McCalld1a7a372018-01-31 13:40:48 +00001309 semaCodeComplete(std::move(RecorderOwner), Opts.getClangCompleteOpts(),
Eric Liu63f419a2018-05-15 15:29:32 +00001310 SemaCCInput, &Includes);
Sam McCall545a20d2018-01-19 14:34:02 +00001311
Sam McCall2b780162018-01-30 17:20:54 +00001312 SPAN_ATTACH(Tracer, "sema_results", NSema);
1313 SPAN_ATTACH(Tracer, "index_results", NIndex);
1314 SPAN_ATTACH(Tracer, "merged_results", NBoth);
Sam McCalld20d7982018-07-09 14:25:59 +00001315 SPAN_ATTACH(Tracer, "returned_results", int64_t(Output.Completions.size()));
Sam McCall27c979a2018-06-29 14:47:57 +00001316 SPAN_ATTACH(Tracer, "incomplete", Output.HasMore);
Sam McCallbed58852018-07-11 10:35:11 +00001317 log("Code complete: {0} results from Sema, {1} from Index, "
1318 "{2} matched, {3} returned{4}.",
1319 NSema, NIndex, NBoth, Output.Completions.size(),
1320 Output.HasMore ? " (incomplete)" : "");
Sam McCall27c979a2018-06-29 14:47:57 +00001321 assert(!Opts.Limit || Output.Completions.size() <= Opts.Limit);
Sam McCall545a20d2018-01-19 14:34:02 +00001322 // We don't assert that isIncomplete means we hit a limit.
1323 // Indexes may choose to impose their own limits even if we don't have one.
1324 return Output;
1325 }
1326
1327private:
1328 // This is called by run() once Sema code completion is done, but before the
1329 // Sema data structures are torn down. It does all the real work.
Sam McCall27c979a2018-06-29 14:47:57 +00001330 CodeCompleteResult runWithSema() {
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +00001331 const auto &CodeCompletionRange = CharSourceRange::getCharRange(
1332 Recorder->CCSema->getPreprocessor().getCodeCompletionTokenRange());
1333 Range TextEditRange;
1334 // When we are getting completions with an empty identifier, for example
1335 // std::vector<int> asdf;
1336 // asdf.^;
1337 // Then the range will be invalid and we will be doing insertion, use
1338 // current cursor position in such cases as range.
1339 if (CodeCompletionRange.isValid()) {
1340 TextEditRange = halfOpenToRange(Recorder->CCSema->getSourceManager(),
1341 CodeCompletionRange);
1342 } else {
1343 const auto &Pos = sourceLocToPosition(
1344 Recorder->CCSema->getSourceManager(),
1345 Recorder->CCSema->getPreprocessor().getCodeCompletionLoc());
1346 TextEditRange.start = TextEditRange.end = Pos;
1347 }
Sam McCall545a20d2018-01-19 14:34:02 +00001348 Filter = FuzzyMatcher(
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001349 Recorder->CCSema->getPreprocessor().getCodeCompletionFilter());
Eric Liu3fac4ef2018-10-17 11:19:02 +00001350 std::tie(QueryScopes, AllScopes) =
1351 getQueryScopes(Recorder->CCContext, *Recorder->CCSema, Opts);
1352 if (!QueryScopes.empty())
1353 ScopeProximity.emplace(QueryScopes);
Sam McCall545a20d2018-01-19 14:34:02 +00001354 // Sema provides the needed context to query the index.
1355 // FIXME: in addition to querying for extra/overlapping symbols, we should
1356 // explicitly request symbols corresponding to Sema results.
1357 // We can use their signals even if the index can't suggest them.
1358 // We must copy index results to preserve them, but there are at most Limit.
Eric Liu8f3678d2018-06-15 13:34:18 +00001359 auto IndexResults = (Opts.Index && allowIndex(Recorder->CCContext))
1360 ? queryIndex()
1361 : SymbolSlab();
Eric Liu25d74e92018-08-24 11:23:56 +00001362 trace::Span Tracer("Populate CodeCompleteResult");
Kadir Cetinkayab15b8dc2018-10-02 09:42:17 +00001363 // Merge Sema and Index results, score them, and pick the winners.
1364 auto Top = mergeResults(Recorder->Results, IndexResults);
Sam McCall27c979a2018-06-29 14:47:57 +00001365 CodeCompleteResult Output;
Kadir Cetinkayaf8b85a32018-08-23 13:14:50 +00001366
1367 // Convert the results to final form, assembling the expensive strings.
Sam McCall27c979a2018-06-29 14:47:57 +00001368 for (auto &C : Top) {
1369 Output.Completions.push_back(toCodeCompletion(C.first));
1370 Output.Completions.back().Score = C.second;
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +00001371 Output.Completions.back().CompletionTokenRange = TextEditRange;
Sam McCall27c979a2018-06-29 14:47:57 +00001372 }
1373 Output.HasMore = Incomplete;
Eric Liu5d2a8072018-07-23 10:56:37 +00001374 Output.Context = Recorder->CCContext.getKind();
Kadir Cetinkayaf8b85a32018-08-23 13:14:50 +00001375
Sam McCall545a20d2018-01-19 14:34:02 +00001376 return Output;
1377 }
1378
1379 SymbolSlab queryIndex() {
Sam McCalld1a7a372018-01-31 13:40:48 +00001380 trace::Span Tracer("Query index");
Sam McCalld20d7982018-07-09 14:25:59 +00001381 SPAN_ATTACH(Tracer, "limit", int64_t(Opts.Limit));
Sam McCall2b780162018-01-30 17:20:54 +00001382
Sam McCall545a20d2018-01-19 14:34:02 +00001383 // Build the query.
1384 FuzzyFindRequest Req;
Haojian Wu48b48652018-01-25 09:20:09 +00001385 if (Opts.Limit)
Kirill Bobyreve6dd0802018-09-13 14:27:03 +00001386 Req.Limit = Opts.Limit;
Sam McCall545a20d2018-01-19 14:34:02 +00001387 Req.Query = Filter->pattern();
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +00001388 Req.RestrictForCodeCompletion = true;
Eric Liubc25ef72018-07-05 08:29:33 +00001389 Req.Scopes = QueryScopes;
Eric Liu670c1472018-09-27 18:46:00 +00001390 Req.AnyScope = AllScopes;
Sam McCall3f0243f2018-07-03 08:09:29 +00001391 // FIXME: we should send multiple weighted paths here.
Eric Liu6de95ec2018-06-12 08:48:20 +00001392 Req.ProximityPaths.push_back(FileName);
Kirill Bobyrev09f00dc2018-09-10 11:51:05 +00001393 vlog("Code complete: fuzzyFind({0:2})", toJSON(Req));
Eric Liu25d74e92018-08-24 11:23:56 +00001394
1395 if (SpecFuzzyFind)
1396 SpecFuzzyFind->NewReq = Req;
1397 if (SpecFuzzyFind && SpecFuzzyFind->Result.valid() && (*SpecReq == Req)) {
1398 vlog("Code complete: speculative fuzzy request matches the actual index "
1399 "request. Waiting for the speculative index results.");
1400 SPAN_ATTACH(Tracer, "Speculative results", true);
1401
1402 trace::Span WaitSpec("Wait speculative results");
1403 return SpecFuzzyFind->Result.get();
1404 }
1405
1406 SPAN_ATTACH(Tracer, "Speculative results", false);
1407
Sam McCall545a20d2018-01-19 14:34:02 +00001408 // Run the query against the index.
Eric Liu25d74e92018-08-24 11:23:56 +00001409 SymbolSlab::Builder ResultsBuilder;
Sam McCallab8e3932018-02-19 13:04:41 +00001410 if (Opts.Index->fuzzyFind(
1411 Req, [&](const Symbol &Sym) { ResultsBuilder.insert(Sym); }))
1412 Incomplete = true;
Sam McCall545a20d2018-01-19 14:34:02 +00001413 return std::move(ResultsBuilder).build();
1414 }
1415
Kadir Cetinkayab15b8dc2018-10-02 09:42:17 +00001416 // Merges Sema and Index results where possible, to form CompletionCandidates.
1417 // Groups overloads if desired, to form CompletionCandidate::Bundles. The
1418 // bundles are scored and top results are returned, best to worst.
Sam McCallc18c2802018-06-15 11:06:29 +00001419 std::vector<ScoredBundle>
Kadir Cetinkayaf8b85a32018-08-23 13:14:50 +00001420 mergeResults(const std::vector<CodeCompletionResult> &SemaResults,
Kadir Cetinkayab15b8dc2018-10-02 09:42:17 +00001421 const SymbolSlab &IndexResults) {
Sam McCalld1a7a372018-01-31 13:40:48 +00001422 trace::Span Tracer("Merge and score results");
Sam McCallc18c2802018-06-15 11:06:29 +00001423 std::vector<CompletionCandidate::Bundle> Bundles;
Sam McCallc008af62018-10-20 15:30:37 +00001424 DenseMap<size_t, size_t> BundleLookup;
Sam McCallc18c2802018-06-15 11:06:29 +00001425 auto AddToBundles = [&](const CodeCompletionResult *SemaResult,
Kadir Cetinkayab15b8dc2018-10-02 09:42:17 +00001426 const Symbol *IndexResult) {
Sam McCallc18c2802018-06-15 11:06:29 +00001427 CompletionCandidate C;
1428 C.SemaResult = SemaResult;
1429 C.IndexResult = IndexResult;
Eric Liu83f63e42018-09-03 10:18:21 +00001430 if (C.IndexResult)
1431 C.RankedIncludeHeaders = getRankedIncludes(*C.IndexResult);
Sam McCallc18c2802018-06-15 11:06:29 +00001432 C.Name = IndexResult ? IndexResult->Name : Recorder->getName(*SemaResult);
1433 if (auto OverloadSet = Opts.BundleOverloads ? C.overloadSet() : 0) {
1434 auto Ret = BundleLookup.try_emplace(OverloadSet, Bundles.size());
1435 if (Ret.second)
1436 Bundles.emplace_back();
1437 Bundles[Ret.first->second].push_back(std::move(C));
1438 } else {
1439 Bundles.emplace_back();
1440 Bundles.back().push_back(std::move(C));
1441 }
1442 };
Sam McCallc008af62018-10-20 15:30:37 +00001443 DenseSet<const Symbol *> UsedIndexResults;
Sam McCall545a20d2018-01-19 14:34:02 +00001444 auto CorrespondingIndexResult =
1445 [&](const CodeCompletionResult &SemaResult) -> const Symbol * {
Eric Liud25f1212018-09-06 09:59:37 +00001446 if (auto SymID =
1447 getSymbolID(SemaResult, Recorder->CCSema->getSourceManager())) {
Sam McCall545a20d2018-01-19 14:34:02 +00001448 auto I = IndexResults.find(*SymID);
1449 if (I != IndexResults.end()) {
1450 UsedIndexResults.insert(&*I);
1451 return &*I;
1452 }
1453 }
1454 return nullptr;
1455 };
1456 // Emit all Sema results, merging them with Index results if possible.
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001457 for (auto &SemaResult : Recorder->Results)
Kadir Cetinkayab15b8dc2018-10-02 09:42:17 +00001458 AddToBundles(&SemaResult, CorrespondingIndexResult(SemaResult));
Sam McCall545a20d2018-01-19 14:34:02 +00001459 // Now emit any Index-only results.
1460 for (const auto &IndexResult : IndexResults) {
1461 if (UsedIndexResults.count(&IndexResult))
1462 continue;
Kadir Cetinkayab15b8dc2018-10-02 09:42:17 +00001463 AddToBundles(/*SemaResult=*/nullptr, &IndexResult);
Sam McCall545a20d2018-01-19 14:34:02 +00001464 }
Sam McCallc18c2802018-06-15 11:06:29 +00001465 // We only keep the best N results at any time, in "native" format.
1466 TopN<ScoredBundle, ScoredBundleGreater> Top(
1467 Opts.Limit == 0 ? std::numeric_limits<size_t>::max() : Opts.Limit);
1468 for (auto &Bundle : Bundles)
1469 addCandidate(Top, std::move(Bundle));
Sam McCall545a20d2018-01-19 14:34:02 +00001470 return std::move(Top).items();
1471 }
1472
Sam McCall80ad7072018-06-08 13:32:25 +00001473 Optional<float> fuzzyScore(const CompletionCandidate &C) {
1474 // Macros can be very spammy, so we only support prefix completion.
1475 // We won't end up with underfull index results, as macros are sema-only.
1476 if (C.SemaResult && C.SemaResult->Kind == CodeCompletionResult::RK_Macro &&
1477 !C.Name.startswith_lower(Filter->pattern()))
1478 return None;
1479 return Filter->match(C.Name);
1480 }
1481
Sam McCall545a20d2018-01-19 14:34:02 +00001482 // Scores a candidate and adds it to the TopN structure.
Sam McCallc18c2802018-06-15 11:06:29 +00001483 void addCandidate(TopN<ScoredBundle, ScoredBundleGreater> &Candidates,
1484 CompletionCandidate::Bundle Bundle) {
Sam McCallc5707b62018-05-15 17:43:27 +00001485 SymbolQualitySignals Quality;
1486 SymbolRelevanceSignals Relevance;
Eric Liu5d2a8072018-07-23 10:56:37 +00001487 Relevance.Context = Recorder->CCContext.getKind();
Sam McCalld9b54f02018-06-05 16:30:25 +00001488 Relevance.Query = SymbolRelevanceSignals::CodeComplete;
Sam McCallf84dd022018-07-05 08:26:53 +00001489 Relevance.FileProximityMatch = FileProximity.getPointer();
Eric Liu3fac4ef2018-10-17 11:19:02 +00001490 if (ScopeProximity)
1491 Relevance.ScopeProximityMatch = ScopeProximity.getPointer();
Eric Liu670c1472018-09-27 18:46:00 +00001492
Sam McCallc18c2802018-06-15 11:06:29 +00001493 auto &First = Bundle.front();
1494 if (auto FuzzyScore = fuzzyScore(First))
Sam McCallc5707b62018-05-15 17:43:27 +00001495 Relevance.NameMatch = *FuzzyScore;
Sam McCall545a20d2018-01-19 14:34:02 +00001496 else
1497 return;
Sam McCall2161ec72018-07-05 06:20:41 +00001498 SymbolOrigin Origin = SymbolOrigin::Unknown;
Sam McCall4e5742a2018-07-06 11:50:49 +00001499 bool FromIndex = false;
Sam McCallc18c2802018-06-15 11:06:29 +00001500 for (const auto &Candidate : Bundle) {
1501 if (Candidate.IndexResult) {
1502 Quality.merge(*Candidate.IndexResult);
1503 Relevance.merge(*Candidate.IndexResult);
Sam McCall4e5742a2018-07-06 11:50:49 +00001504 Origin |= Candidate.IndexResult->Origin;
1505 FromIndex = true;
Sam McCallc18c2802018-06-15 11:06:29 +00001506 }
1507 if (Candidate.SemaResult) {
1508 Quality.merge(*Candidate.SemaResult);
1509 Relevance.merge(*Candidate.SemaResult);
Sam McCall4e5742a2018-07-06 11:50:49 +00001510 Origin |= SymbolOrigin::AST;
Sam McCallc18c2802018-06-15 11:06:29 +00001511 }
Sam McCallc5707b62018-05-15 17:43:27 +00001512 }
1513
Sam McCall27c979a2018-06-29 14:47:57 +00001514 CodeCompletion::Scores Scores;
1515 Scores.Quality = Quality.evaluate();
1516 Scores.Relevance = Relevance.evaluate();
1517 Scores.Total = evaluateSymbolAndRelevance(Scores.Quality, Scores.Relevance);
1518 // NameMatch is in fact a multiplier on total score, so rescoring is sound.
1519 Scores.ExcludingName = Relevance.NameMatch
1520 ? Scores.Total / Relevance.NameMatch
1521 : Scores.Quality;
Sam McCallc5707b62018-05-15 17:43:27 +00001522
Sam McCallbed58852018-07-11 10:35:11 +00001523 dlog("CodeComplete: {0} ({1}) = {2}\n{3}{4}\n", First.Name,
Sam McCallc008af62018-10-20 15:30:37 +00001524 to_string(Origin), Scores.Total, to_string(Quality),
1525 to_string(Relevance));
Sam McCall545a20d2018-01-19 14:34:02 +00001526
Sam McCall2161ec72018-07-05 06:20:41 +00001527 NSema += bool(Origin & SymbolOrigin::AST);
Sam McCall4e5742a2018-07-06 11:50:49 +00001528 NIndex += FromIndex;
1529 NBoth += bool(Origin & SymbolOrigin::AST) && FromIndex;
Sam McCallc18c2802018-06-15 11:06:29 +00001530 if (Candidates.push({std::move(Bundle), Scores}))
Sam McCallab8e3932018-02-19 13:04:41 +00001531 Incomplete = true;
Sam McCall545a20d2018-01-19 14:34:02 +00001532 }
1533
Sam McCall27c979a2018-06-29 14:47:57 +00001534 CodeCompletion toCodeCompletion(const CompletionCandidate::Bundle &Bundle) {
Sam McCallc008af62018-10-20 15:30:37 +00001535 Optional<CodeCompletionBuilder> Builder;
Sam McCall27c979a2018-06-29 14:47:57 +00001536 for (const auto &Item : Bundle) {
1537 CodeCompletionString *SemaCCS =
1538 Item.SemaResult ? Recorder->codeCompletionString(*Item.SemaResult)
1539 : nullptr;
1540 if (!Builder)
1541 Builder.emplace(Recorder->CCSema->getASTContext(), Item, SemaCCS,
Eric Liu670c1472018-09-27 18:46:00 +00001542 QueryScopes, *Inserter, FileName,
1543 Recorder->CCContext.getKind(), Opts);
Sam McCall27c979a2018-06-29 14:47:57 +00001544 else
1545 Builder->add(Item, SemaCCS);
Ilya Biryukov43714502018-05-16 12:32:44 +00001546 }
Sam McCall27c979a2018-06-29 14:47:57 +00001547 return Builder->build();
Sam McCall545a20d2018-01-19 14:34:02 +00001548 }
1549};
1550
Sam McCallc008af62018-10-20 15:30:37 +00001551Expected<StringRef> speculateCompletionFilter(StringRef Content, Position Pos) {
Eric Liu25d74e92018-08-24 11:23:56 +00001552 auto Offset = positionToOffset(Content, Pos);
1553 if (!Offset)
Sam McCallc008af62018-10-20 15:30:37 +00001554 return make_error<StringError>(
Eric Liu25d74e92018-08-24 11:23:56 +00001555 "Failed to convert position to offset in content.",
Sam McCallc008af62018-10-20 15:30:37 +00001556 inconvertibleErrorCode());
Eric Liu25d74e92018-08-24 11:23:56 +00001557 if (*Offset == 0)
1558 return "";
1559
1560 // Start from the character before the cursor.
1561 int St = *Offset - 1;
1562 // FIXME(ioeric): consider UTF characters?
1563 auto IsValidIdentifierChar = [](char c) {
1564 return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
1565 (c >= '0' && c <= '9') || (c == '_'));
1566 };
1567 size_t Len = 0;
1568 for (; (St >= 0) && IsValidIdentifierChar(Content[St]); --St, ++Len) {
1569 }
1570 if (Len > 0)
1571 St++; // Shift to the first valid character.
1572 return Content.substr(St, Len);
1573}
1574
1575CodeCompleteResult
1576codeComplete(PathRef FileName, const tooling::CompileCommand &Command,
Eric Liub1d75422018-10-02 10:43:55 +00001577 const PreambleData *Preamble, StringRef Contents, Position Pos,
Sam McCallc008af62018-10-20 15:30:37 +00001578 IntrusiveRefCntPtr<vfs::FileSystem> VFS,
Eric Liu25d74e92018-08-24 11:23:56 +00001579 std::shared_ptr<PCHContainerOperations> PCHs,
1580 CodeCompleteOptions Opts, SpeculativeFuzzyFind *SpecFuzzyFind) {
Eric Liub1d75422018-10-02 10:43:55 +00001581 return CodeCompleteFlow(FileName,
1582 Preamble ? Preamble->Includes : IncludeStructure(),
1583 SpecFuzzyFind, Opts)
Sam McCall3f0243f2018-07-03 08:09:29 +00001584 .run({FileName, Command, Preamble, Contents, Pos, VFS, PCHs});
Sam McCall98775c52017-12-04 13:49:59 +00001585}
1586
Sam McCalld1a7a372018-01-31 13:40:48 +00001587SignatureHelp signatureHelp(PathRef FileName,
Ilya Biryukov940901e2017-12-13 12:51:22 +00001588 const tooling::CompileCommand &Command,
Eric Liub1d75422018-10-02 10:43:55 +00001589 const PreambleData *Preamble, StringRef Contents,
1590 Position Pos,
Sam McCallc008af62018-10-20 15:30:37 +00001591 IntrusiveRefCntPtr<vfs::FileSystem> VFS,
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +00001592 std::shared_ptr<PCHContainerOperations> PCHs,
Sam McCall046557b2018-09-03 16:37:59 +00001593 const SymbolIndex *Index) {
Sam McCall98775c52017-12-04 13:49:59 +00001594 SignatureHelp Result;
1595 clang::CodeCompleteOptions Options;
1596 Options.IncludeGlobals = false;
1597 Options.IncludeMacros = false;
1598 Options.IncludeCodePatterns = false;
Ilya Biryukov43714502018-05-16 12:32:44 +00001599 Options.IncludeBriefComments = false;
Sam McCall3f0243f2018-07-03 08:09:29 +00001600 IncludeStructure PreambleInclusions; // Unused for signatureHelp
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +00001601 semaCodeComplete(
1602 llvm::make_unique<SignatureHelpCollector>(Options, Index, Result),
1603 Options,
1604 {FileName, Command, Preamble, Contents, Pos, std::move(VFS),
1605 std::move(PCHs)});
Sam McCall98775c52017-12-04 13:49:59 +00001606 return Result;
1607}
1608
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +00001609bool isIndexedForCodeCompletion(const NamedDecl &ND, ASTContext &ASTCtx) {
1610 using namespace clang::ast_matchers;
1611 auto InTopLevelScope = hasDeclContext(
1612 anyOf(namespaceDecl(), translationUnitDecl(), linkageSpecDecl()));
1613 return !match(decl(anyOf(InTopLevelScope,
1614 hasDeclContext(
1615 enumDecl(InTopLevelScope, unless(isScoped()))))),
1616 ND, ASTCtx)
1617 .empty();
1618}
1619
Sam McCall27c979a2018-06-29 14:47:57 +00001620CompletionItem CodeCompletion::render(const CodeCompleteOptions &Opts) const {
1621 CompletionItem LSP;
Eric Liu83f63e42018-09-03 10:18:21 +00001622 const auto *InsertInclude = Includes.empty() ? nullptr : &Includes[0];
1623 LSP.label = ((InsertInclude && InsertInclude->Insertion)
1624 ? Opts.IncludeIndicator.Insert
1625 : Opts.IncludeIndicator.NoInsert) +
Sam McCallc008af62018-10-20 15:30:37 +00001626 (Opts.ShowOrigins ? "[" + to_string(Origin) + "]" : "") +
Sam McCall27c979a2018-06-29 14:47:57 +00001627 RequiredQualifier + Name + Signature;
Sam McCall2161ec72018-07-05 06:20:41 +00001628
Sam McCall27c979a2018-06-29 14:47:57 +00001629 LSP.kind = Kind;
Sam McCallc008af62018-10-20 15:30:37 +00001630 LSP.detail =
1631 BundleSize > 1 ? formatv("[{0} overloads]", BundleSize) : ReturnType;
Eric Liu6df66002018-09-06 18:52:26 +00001632 LSP.deprecated = Deprecated;
Eric Liu83f63e42018-09-03 10:18:21 +00001633 if (InsertInclude)
1634 LSP.detail += "\n" + InsertInclude->Header;
Sam McCall27c979a2018-06-29 14:47:57 +00001635 LSP.documentation = Documentation;
1636 LSP.sortText = sortText(Score.Total, Name);
1637 LSP.filterText = Name;
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +00001638 LSP.textEdit = {CompletionTokenRange, RequiredQualifier + Name};
Fangrui Song445bdd12018-09-05 08:01:37 +00001639 // Merge continuous additionalTextEdits into main edit. The main motivation
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +00001640 // behind this is to help LSP clients, it seems most of them are confused when
1641 // they are provided with additionalTextEdits that are consecutive to main
1642 // edit.
1643 // Note that we store additional text edits from back to front in a line. That
1644 // is mainly to help LSP clients again, so that changes do not effect each
1645 // other.
1646 for (const auto &FixIt : FixIts) {
1647 if (IsRangeConsecutive(FixIt.range, LSP.textEdit->range)) {
1648 LSP.textEdit->newText = FixIt.newText + LSP.textEdit->newText;
1649 LSP.textEdit->range.start = FixIt.range.start;
1650 } else {
1651 LSP.additionalTextEdits.push_back(FixIt);
1652 }
1653 }
Kadir Cetinkaya516fcda2018-08-23 12:19:39 +00001654 if (Opts.EnableSnippets)
1655 LSP.textEdit->newText += SnippetSuffix;
Kadir Cetinkaya6c9f15c2018-08-17 15:42:54 +00001656
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +00001657 // FIXME(kadircet): Do not even fill insertText after making sure textEdit is
1658 // compatible with most of the editors.
1659 LSP.insertText = LSP.textEdit->newText;
Sam McCall27c979a2018-06-29 14:47:57 +00001660 LSP.insertTextFormat = Opts.EnableSnippets ? InsertTextFormat::Snippet
1661 : InsertTextFormat::PlainText;
Eric Liu83f63e42018-09-03 10:18:21 +00001662 if (InsertInclude && InsertInclude->Insertion)
1663 LSP.additionalTextEdits.push_back(*InsertInclude->Insertion);
Eric Liu6df66002018-09-06 18:52:26 +00001664
Sam McCall27c979a2018-06-29 14:47:57 +00001665 return LSP;
1666}
1667
Sam McCalle746a2b2018-07-02 11:13:16 +00001668raw_ostream &operator<<(raw_ostream &OS, const CodeCompletion &C) {
1669 // For now just lean on CompletionItem.
1670 return OS << C.render(CodeCompleteOptions());
1671}
1672
1673raw_ostream &operator<<(raw_ostream &OS, const CodeCompleteResult &R) {
1674 OS << "CodeCompleteResult: " << R.Completions.size() << (R.HasMore ? "+" : "")
Eric Liu5d2a8072018-07-23 10:56:37 +00001675 << " (" << getCompletionKindString(R.Context) << ")"
Sam McCalle746a2b2018-07-02 11:13:16 +00001676 << " items:\n";
1677 for (const auto &C : R.Completions)
1678 OS << C << "\n";
1679 return OS;
1680}
1681
Sam McCall98775c52017-12-04 13:49:59 +00001682} // namespace clangd
1683} // namespace clang