blob: d2067219ce0c9819ef99e3dbd9fa42b95640abcc [file] [log] [blame]
Sam McCall9aad25f2017-12-05 07:20:26 +00001//===-- CodeCompleteTests.cpp -----------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Eric Liu6f648df2017-12-19 16:50:37 +00009
Sam McCall328cbdb2017-12-20 16:06:05 +000010#include "Annotations.h"
Sam McCall9aad25f2017-12-05 07:20:26 +000011#include "ClangdServer.h"
Sam McCall328cbdb2017-12-20 16:06:05 +000012#include "CodeComplete.h"
Sam McCall9aad25f2017-12-05 07:20:26 +000013#include "Compiler.h"
Ilya Biryukov5a85b8e2017-12-13 12:53:16 +000014#include "Matchers.h"
Sam McCall9aad25f2017-12-05 07:20:26 +000015#include "Protocol.h"
Sam McCalle746a2b2018-07-02 11:13:16 +000016#include "Quality.h"
Sam McCallb536a2a2017-12-19 12:23:48 +000017#include "SourceCode.h"
Ilya Biryukovcd5eb002018-02-12 11:37:28 +000018#include "SyncAPI.h"
Sam McCall9aad25f2017-12-05 07:20:26 +000019#include "TestFS.h"
Eric Liu6f648df2017-12-19 16:50:37 +000020#include "index/MemIndex.h"
Eric Liu5d2a8072018-07-23 10:56:37 +000021#include "clang/Sema/CodeCompleteConsumer.h"
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +000022#include "llvm/Support/Error.h"
Ilya Biryukov981a35d2018-05-28 12:11:37 +000023#include "llvm/Testing/Support/Error.h"
Sam McCallf6ae3232017-12-05 20:11:29 +000024#include "gmock/gmock.h"
Sam McCall9aad25f2017-12-05 07:20:26 +000025#include "gtest/gtest.h"
26
27namespace clang {
28namespace clangd {
Sam McCallf6ae3232017-12-05 20:11:29 +000029
Sam McCall9aad25f2017-12-05 07:20:26 +000030namespace {
31using namespace llvm;
Sam McCallf6ae3232017-12-05 20:11:29 +000032using ::testing::AllOf;
33using ::testing::Contains;
Ilya Biryukov5a5e1ca2017-12-29 14:59:22 +000034using ::testing::Each;
Sam McCallf6ae3232017-12-05 20:11:29 +000035using ::testing::ElementsAre;
Ilya Biryukov71028b82018-03-12 15:28:22 +000036using ::testing::Field;
Sam McCallc18c2802018-06-15 11:06:29 +000037using ::testing::HasSubstr;
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +000038using ::testing::IsEmpty;
Sam McCallf6ae3232017-12-05 20:11:29 +000039using ::testing::Not;
Sam McCall3d139c52018-01-12 18:30:08 +000040using ::testing::UnorderedElementsAre;
Sam McCall9aad25f2017-12-05 07:20:26 +000041
42class IgnoreDiagnostics : public DiagnosticsConsumer {
Ilya Biryukov71028b82018-03-12 15:28:22 +000043 void onDiagnosticsReady(PathRef File,
Sam McCalla7bb0cc2018-03-12 23:22:35 +000044 std::vector<Diag> Diagnostics) override {}
Sam McCall9aad25f2017-12-05 07:20:26 +000045};
46
Sam McCallf6ae3232017-12-05 20:11:29 +000047// GMock helpers for matching completion items.
Sam McCalle746a2b2018-07-02 11:13:16 +000048MATCHER_P(Named, Name, "") { return arg.Name == Name; }
49MATCHER_P(Scope, S, "") { return arg.Scope == S; }
50MATCHER_P(Qualifier, Q, "") { return arg.RequiredQualifier == Q; }
Eric Liu8f3678d2018-06-15 13:34:18 +000051MATCHER_P(Labeled, Label, "") {
Sam McCalle746a2b2018-07-02 11:13:16 +000052 return arg.RequiredQualifier + arg.Name + arg.Signature == Label;
Eric Liu8f3678d2018-06-15 13:34:18 +000053}
54MATCHER_P(SigHelpLabeled, Label, "") { return arg.label == Label; }
Sam McCalle746a2b2018-07-02 11:13:16 +000055MATCHER_P(Kind, K, "") { return arg.Kind == K; }
56MATCHER_P(Doc, D, "") { return arg.Documentation == D; }
57MATCHER_P(ReturnType, D, "") { return arg.ReturnType == D; }
Eric Liu83f63e42018-09-03 10:18:21 +000058MATCHER_P(HasInclude, IncludeHeader, "") {
59 return !arg.Includes.empty() && arg.Includes[0].Header == IncludeHeader;
Eric Liu63f419a2018-05-15 15:29:32 +000060}
Eric Liu83f63e42018-09-03 10:18:21 +000061MATCHER_P(InsertInclude, IncludeHeader, "") {
62 return !arg.Includes.empty() && arg.Includes[0].Header == IncludeHeader &&
63 bool(arg.Includes[0].Insertion);
64}
65MATCHER(InsertInclude, "") {
66 return !arg.Includes.empty() && bool(arg.Includes[0].Insertion);
67}
Sam McCalle746a2b2018-07-02 11:13:16 +000068MATCHER_P(SnippetSuffix, Text, "") { return arg.SnippetSuffix == Text; }
Sam McCall2161ec72018-07-05 06:20:41 +000069MATCHER_P(Origin, OriginSet, "") { return arg.Origin == OriginSet; }
Eric Liu63f419a2018-05-15 15:29:32 +000070
Sam McCallf6ae3232017-12-05 20:11:29 +000071// Shorthand for Contains(Named(Name)).
Sam McCalle746a2b2018-07-02 11:13:16 +000072Matcher<const std::vector<CodeCompletion> &> Has(std::string Name) {
Sam McCallf6ae3232017-12-05 20:11:29 +000073 return Contains(Named(std::move(Name)));
74}
Sam McCalle746a2b2018-07-02 11:13:16 +000075Matcher<const std::vector<CodeCompletion> &> Has(std::string Name,
Sam McCall44fdcec22017-12-08 15:00:59 +000076 CompletionItemKind K) {
77 return Contains(AllOf(Named(std::move(Name)), Kind(K)));
Sam McCallf6ae3232017-12-05 20:11:29 +000078}
Sam McCalle746a2b2018-07-02 11:13:16 +000079MATCHER(IsDocumented, "") { return !arg.Documentation.empty(); }
Sam McCall9aad25f2017-12-05 07:20:26 +000080
Sam McCalla15c2d62018-01-18 09:27:56 +000081std::unique_ptr<SymbolIndex> memIndex(std::vector<Symbol> Symbols) {
82 SymbolSlab::Builder Slab;
83 for (const auto &Sym : Symbols)
84 Slab.insert(Sym);
Sam McCallb0138312018-09-04 14:39:56 +000085 return MemIndex::build(std::move(Slab).build(), RefSlab());
Sam McCalla15c2d62018-01-18 09:27:56 +000086}
87
Kadir Cetinkaya2f84d912018-08-08 08:59:29 +000088CodeCompleteResult completions(ClangdServer &Server, StringRef TestCode,
89 Position point,
90 std::vector<Symbol> IndexSymbols = {},
91 clangd::CodeCompleteOptions Opts = {}) {
92 std::unique_ptr<SymbolIndex> OverrideIndex;
93 if (!IndexSymbols.empty()) {
94 assert(!Opts.Index && "both Index and IndexSymbols given!");
95 OverrideIndex = memIndex(std::move(IndexSymbols));
96 Opts.Index = OverrideIndex.get();
97 }
98
99 auto File = testPath("foo.cpp");
100 runAddDocument(Server, File, TestCode);
101 auto CompletionList = cantFail(runCodeComplete(Server, File, point, Opts));
102 return CompletionList;
103}
104
Sam McCalle746a2b2018-07-02 11:13:16 +0000105CodeCompleteResult completions(ClangdServer &Server, StringRef Text,
106 std::vector<Symbol> IndexSymbols = {},
107 clangd::CodeCompleteOptions Opts = {}) {
Sam McCalla15c2d62018-01-18 09:27:56 +0000108 std::unique_ptr<SymbolIndex> OverrideIndex;
109 if (!IndexSymbols.empty()) {
110 assert(!Opts.Index && "both Index and IndexSymbols given!");
111 OverrideIndex = memIndex(std::move(IndexSymbols));
112 Opts.Index = OverrideIndex.get();
113 }
114
Sam McCallc1568062018-02-16 09:41:43 +0000115 auto File = testPath("foo.cpp");
Sam McCall328cbdb2017-12-20 16:06:05 +0000116 Annotations Test(Text);
Sam McCall7363a2f2018-03-05 17:28:54 +0000117 runAddDocument(Server, File, Test.code());
Sam McCalla7bb0cc2018-03-12 23:22:35 +0000118 auto CompletionList =
119 cantFail(runCodeComplete(Server, File, Test.point(), Opts));
Ilya Biryukov5a5e1ca2017-12-29 14:59:22 +0000120 return CompletionList;
Sam McCall9aad25f2017-12-05 07:20:26 +0000121}
122
Eric Liu63f419a2018-05-15 15:29:32 +0000123// Builds a server and runs code completion.
124// If IndexSymbols is non-empty, an index will be built and passed to opts.
Sam McCalle746a2b2018-07-02 11:13:16 +0000125CodeCompleteResult completions(StringRef Text,
126 std::vector<Symbol> IndexSymbols = {},
127 clangd::CodeCompleteOptions Opts = {}) {
Eric Liu63f419a2018-05-15 15:29:32 +0000128 MockFSProvider FS;
129 MockCompilationDatabase CDB;
130 IgnoreDiagnostics DiagConsumer;
131 ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
132 return completions(Server, Text, std::move(IndexSymbols), std::move(Opts));
133}
134
Sam McCall545a20d2018-01-19 14:34:02 +0000135std::string replace(StringRef Haystack, StringRef Needle, StringRef Repl) {
136 std::string Result;
137 raw_string_ostream OS(Result);
138 std::pair<StringRef, StringRef> Split;
139 for (Split = Haystack.split(Needle); !Split.second.empty();
140 Split = Split.first.split(Needle))
141 OS << Split.first << Repl;
142 Result += Split.first;
143 OS.flush();
144 return Result;
145}
146
Sam McCalla15c2d62018-01-18 09:27:56 +0000147// Helpers to produce fake index symbols for memIndex() or completions().
Sam McCall545a20d2018-01-19 14:34:02 +0000148// USRFormat is a regex replacement string for the unqualified part of the USR.
149Symbol sym(StringRef QName, index::SymbolKind Kind, StringRef USRFormat) {
Sam McCalla15c2d62018-01-18 09:27:56 +0000150 Symbol Sym;
Sam McCall545a20d2018-01-19 14:34:02 +0000151 std::string USR = "c:"; // We synthesize a few simple cases of USRs by hand!
Sam McCalla15c2d62018-01-18 09:27:56 +0000152 size_t Pos = QName.rfind("::");
153 if (Pos == llvm::StringRef::npos) {
154 Sym.Name = QName;
155 Sym.Scope = "";
156 } else {
157 Sym.Name = QName.substr(Pos + 2);
Sam McCall8b2faee2018-01-19 22:18:21 +0000158 Sym.Scope = QName.substr(0, Pos + 2);
159 USR += "@N@" + replace(QName.substr(0, Pos), "::", "@N@"); // ns:: -> @N@ns
Sam McCalla15c2d62018-01-18 09:27:56 +0000160 }
Sam McCall545a20d2018-01-19 14:34:02 +0000161 USR += Regex("^.*$").sub(USRFormat, Sym.Name); // e.g. func -> @F@func#
162 Sym.ID = SymbolID(USR);
Sam McCalla15c2d62018-01-18 09:27:56 +0000163 Sym.SymInfo.Kind = Kind;
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +0000164 Sym.IsIndexedForCodeCompletion = true;
Sam McCall2161ec72018-07-05 06:20:41 +0000165 Sym.Origin = SymbolOrigin::Static;
Sam McCalla15c2d62018-01-18 09:27:56 +0000166 return Sym;
167}
Sam McCall545a20d2018-01-19 14:34:02 +0000168Symbol func(StringRef Name) { // Assumes the function has no args.
169 return sym(Name, index::SymbolKind::Function, "@F@\\0#"); // no args
170}
171Symbol cls(StringRef Name) {
Eric Liu9b3cba72018-05-30 09:03:39 +0000172 return sym(Name, index::SymbolKind::Class, "@S@\\0");
Sam McCall545a20d2018-01-19 14:34:02 +0000173}
174Symbol var(StringRef Name) {
175 return sym(Name, index::SymbolKind::Variable, "@\\0");
176}
Sam McCalldc8abc42018-05-03 14:53:02 +0000177Symbol ns(StringRef Name) {
178 return sym(Name, index::SymbolKind::Namespace, "@N@\\0");
179}
180Symbol withReferences(int N, Symbol S) {
181 S.References = N;
182 return S;
183}
Sam McCalla15c2d62018-01-18 09:27:56 +0000184
Sam McCallf6ae3232017-12-05 20:11:29 +0000185TEST(CompletionTest, Limit) {
186 clangd::CodeCompleteOptions Opts;
187 Opts.Limit = 2;
188 auto Results = completions(R"cpp(
Sam McCall9aad25f2017-12-05 07:20:26 +0000189struct ClassWithMembers {
190 int AAA();
191 int BBB();
192 int CCC();
193}
Sam McCallf6ae3232017-12-05 20:11:29 +0000194int main() { ClassWithMembers().^ }
Sam McCall9aad25f2017-12-05 07:20:26 +0000195 )cpp",
Sam McCalla15c2d62018-01-18 09:27:56 +0000196 /*IndexSymbols=*/{}, Opts);
Sam McCall9aad25f2017-12-05 07:20:26 +0000197
Sam McCalle746a2b2018-07-02 11:13:16 +0000198 EXPECT_TRUE(Results.HasMore);
199 EXPECT_THAT(Results.Completions, ElementsAre(Named("AAA"), Named("BBB")));
Sam McCall9aad25f2017-12-05 07:20:26 +0000200}
201
Sam McCallf6ae3232017-12-05 20:11:29 +0000202TEST(CompletionTest, Filter) {
203 std::string Body = R"cpp(
Sam McCall8b2dcc12018-06-14 13:50:30 +0000204 #define MotorCar
205 int Car;
Sam McCall9aad25f2017-12-05 07:20:26 +0000206 struct S {
207 int FooBar;
208 int FooBaz;
209 int Qux;
210 };
211 )cpp";
Sam McCall8b2dcc12018-06-14 13:50:30 +0000212
213 // Only items matching the fuzzy query are returned.
Sam McCalle746a2b2018-07-02 11:13:16 +0000214 EXPECT_THAT(completions(Body + "int main() { S().Foba^ }").Completions,
Sam McCall8b2dcc12018-06-14 13:50:30 +0000215 AllOf(Has("FooBar"), Has("FooBaz"), Not(Has("Qux"))));
Sam McCall9aad25f2017-12-05 07:20:26 +0000216
Sam McCall8b2dcc12018-06-14 13:50:30 +0000217 // Macros require prefix match.
Sam McCalle746a2b2018-07-02 11:13:16 +0000218 EXPECT_THAT(completions(Body + "int main() { C^ }").Completions,
Sam McCall8b2dcc12018-06-14 13:50:30 +0000219 AllOf(Has("Car"), Not(Has("MotorCar"))));
Sam McCall9aad25f2017-12-05 07:20:26 +0000220}
221
Sam McCallf6ae3232017-12-05 20:11:29 +0000222void TestAfterDotCompletion(clangd::CodeCompleteOptions Opts) {
Sam McCall44fdcec22017-12-08 15:00:59 +0000223 auto Results = completions(
224 R"cpp(
225 #define MACRO X
Sam McCall9aad25f2017-12-05 07:20:26 +0000226
Sam McCall44fdcec22017-12-08 15:00:59 +0000227 int global_var;
Sam McCall9aad25f2017-12-05 07:20:26 +0000228
Sam McCall44fdcec22017-12-08 15:00:59 +0000229 int global_func();
Sam McCall9aad25f2017-12-05 07:20:26 +0000230
Sam McCall44fdcec22017-12-08 15:00:59 +0000231 struct GlobalClass {};
Sam McCall9aad25f2017-12-05 07:20:26 +0000232
Sam McCall44fdcec22017-12-08 15:00:59 +0000233 struct ClassWithMembers {
234 /// Doc for method.
235 int method();
Sam McCall9aad25f2017-12-05 07:20:26 +0000236
Sam McCall44fdcec22017-12-08 15:00:59 +0000237 int field;
238 private:
239 int private_field;
240 };
Sam McCall9aad25f2017-12-05 07:20:26 +0000241
Sam McCall44fdcec22017-12-08 15:00:59 +0000242 int test() {
243 struct LocalClass {};
Sam McCall9aad25f2017-12-05 07:20:26 +0000244
Sam McCall44fdcec22017-12-08 15:00:59 +0000245 /// Doc for local_var.
246 int local_var;
Sam McCall9aad25f2017-12-05 07:20:26 +0000247
Sam McCall44fdcec22017-12-08 15:00:59 +0000248 ClassWithMembers().^
249 }
250 )cpp",
Sam McCall545a20d2018-01-19 14:34:02 +0000251 {cls("IndexClass"), var("index_var"), func("index_func")}, Opts);
Sam McCall9aad25f2017-12-05 07:20:26 +0000252
Sam McCallf6ae3232017-12-05 20:11:29 +0000253 // Class members. The only items that must be present in after-dot
254 // completion.
Sam McCalle746a2b2018-07-02 11:13:16 +0000255 EXPECT_THAT(Results.Completions,
256 AllOf(Has("method"), Has("field"), Not(Has("ClassWithMembers")),
Sam McCall4caa8512018-06-07 12:49:17 +0000257 Not(Has("operator=")), Not(Has("~ClassWithMembers"))));
Sam McCalle746a2b2018-07-02 11:13:16 +0000258 EXPECT_IFF(Opts.IncludeIneligibleResults, Results.Completions,
Sam McCall44fdcec22017-12-08 15:00:59 +0000259 Has("private_field"));
Sam McCallf6ae3232017-12-05 20:11:29 +0000260 // Global items.
Sam McCall545a20d2018-01-19 14:34:02 +0000261 EXPECT_THAT(
Sam McCalle746a2b2018-07-02 11:13:16 +0000262 Results.Completions,
Sam McCall545a20d2018-01-19 14:34:02 +0000263 Not(AnyOf(Has("global_var"), Has("index_var"), Has("global_func"),
264 Has("global_func()"), Has("index_func"), Has("GlobalClass"),
265 Has("IndexClass"), Has("MACRO"), Has("LocalClass"))));
Sam McCallf6ae3232017-12-05 20:11:29 +0000266 // There should be no code patterns (aka snippets) in after-dot
267 // completion. At least there aren't any we're aware of.
Sam McCalle746a2b2018-07-02 11:13:16 +0000268 EXPECT_THAT(Results.Completions,
269 Not(Contains(Kind(CompletionItemKind::Snippet))));
Sam McCallf6ae3232017-12-05 20:11:29 +0000270 // Check documentation.
Sam McCalle746a2b2018-07-02 11:13:16 +0000271 EXPECT_IFF(Opts.IncludeComments, Results.Completions,
272 Contains(IsDocumented()));
Sam McCallf6ae3232017-12-05 20:11:29 +0000273}
Sam McCall9aad25f2017-12-05 07:20:26 +0000274
Sam McCallf6ae3232017-12-05 20:11:29 +0000275void TestGlobalScopeCompletion(clangd::CodeCompleteOptions Opts) {
Sam McCall44fdcec22017-12-08 15:00:59 +0000276 auto Results = completions(
277 R"cpp(
278 #define MACRO X
Sam McCall9aad25f2017-12-05 07:20:26 +0000279
Sam McCall44fdcec22017-12-08 15:00:59 +0000280 int global_var;
281 int global_func();
Sam McCall9aad25f2017-12-05 07:20:26 +0000282
Sam McCall44fdcec22017-12-08 15:00:59 +0000283 struct GlobalClass {};
Sam McCall9aad25f2017-12-05 07:20:26 +0000284
Sam McCall44fdcec22017-12-08 15:00:59 +0000285 struct ClassWithMembers {
286 /// Doc for method.
287 int method();
288 };
Sam McCall9aad25f2017-12-05 07:20:26 +0000289
Sam McCall44fdcec22017-12-08 15:00:59 +0000290 int test() {
291 struct LocalClass {};
Sam McCall9aad25f2017-12-05 07:20:26 +0000292
Sam McCall44fdcec22017-12-08 15:00:59 +0000293 /// Doc for local_var.
294 int local_var;
Sam McCall9aad25f2017-12-05 07:20:26 +0000295
Sam McCall44fdcec22017-12-08 15:00:59 +0000296 ^
297 }
298 )cpp",
Sam McCall545a20d2018-01-19 14:34:02 +0000299 {cls("IndexClass"), var("index_var"), func("index_func")}, Opts);
Sam McCallf6ae3232017-12-05 20:11:29 +0000300
301 // Class members. Should never be present in global completions.
Sam McCalle746a2b2018-07-02 11:13:16 +0000302 EXPECT_THAT(Results.Completions,
Sam McCallf6ae3232017-12-05 20:11:29 +0000303 Not(AnyOf(Has("method"), Has("method()"), Has("field"))));
304 // Global items.
Sam McCalle746a2b2018-07-02 11:13:16 +0000305 EXPECT_THAT(Results.Completions,
306 AllOf(Has("global_var"), Has("index_var"), Has("global_func"),
Sam McCall545a20d2018-01-19 14:34:02 +0000307 Has("index_func" /* our fake symbol doesn't include () */),
308 Has("GlobalClass"), Has("IndexClass")));
Sam McCallf6ae3232017-12-05 20:11:29 +0000309 // A macro.
Sam McCalle746a2b2018-07-02 11:13:16 +0000310 EXPECT_IFF(Opts.IncludeMacros, Results.Completions, Has("MACRO"));
Sam McCallf6ae3232017-12-05 20:11:29 +0000311 // Local items. Must be present always.
Sam McCalle746a2b2018-07-02 11:13:16 +0000312 EXPECT_THAT(Results.Completions,
Ilya Biryukov9b5ffc22017-12-12 12:56:46 +0000313 AllOf(Has("local_var"), Has("LocalClass"),
314 Contains(Kind(CompletionItemKind::Snippet))));
Sam McCallf6ae3232017-12-05 20:11:29 +0000315 // Check documentation.
Sam McCalle746a2b2018-07-02 11:13:16 +0000316 EXPECT_IFF(Opts.IncludeComments, Results.Completions,
317 Contains(IsDocumented()));
Sam McCallf6ae3232017-12-05 20:11:29 +0000318}
319
320TEST(CompletionTest, CompletionOptions) {
Sam McCall2c3849a2018-01-16 12:21:24 +0000321 auto Test = [&](const clangd::CodeCompleteOptions &Opts) {
322 TestAfterDotCompletion(Opts);
323 TestGlobalScopeCompletion(Opts);
324 };
325 // We used to test every combination of options, but that got too slow (2^N).
326 auto Flags = {
Ilya Biryukov71028b82018-03-12 15:28:22 +0000327 &clangd::CodeCompleteOptions::IncludeMacros,
Ilya Biryukov43714502018-05-16 12:32:44 +0000328 &clangd::CodeCompleteOptions::IncludeComments,
Ilya Biryukov71028b82018-03-12 15:28:22 +0000329 &clangd::CodeCompleteOptions::IncludeCodePatterns,
330 &clangd::CodeCompleteOptions::IncludeIneligibleResults,
Sam McCall2c3849a2018-01-16 12:21:24 +0000331 };
332 // Test default options.
333 Test({});
334 // Test with one flag flipped.
335 for (auto &F : Flags) {
336 clangd::CodeCompleteOptions O;
337 O.*F ^= true;
338 Test(O);
Sam McCall9aad25f2017-12-05 07:20:26 +0000339 }
340}
341
Sam McCall44fdcec22017-12-08 15:00:59 +0000342TEST(CompletionTest, Priorities) {
343 auto Internal = completions(R"cpp(
344 class Foo {
345 public: void pub();
346 protected: void prot();
347 private: void priv();
348 };
349 void Foo::pub() { this->^ }
350 )cpp");
Sam McCalle746a2b2018-07-02 11:13:16 +0000351 EXPECT_THAT(Internal.Completions,
Sam McCall44fdcec22017-12-08 15:00:59 +0000352 HasSubsequence(Named("priv"), Named("prot"), Named("pub")));
353
354 auto External = completions(R"cpp(
355 class Foo {
356 public: void pub();
357 protected: void prot();
358 private: void priv();
359 };
360 void test() {
361 Foo F;
362 F.^
363 }
364 )cpp");
Sam McCalle746a2b2018-07-02 11:13:16 +0000365 EXPECT_THAT(External.Completions,
Sam McCall44fdcec22017-12-08 15:00:59 +0000366 AllOf(Has("pub"), Not(Has("prot")), Not(Has("priv"))));
367}
368
369TEST(CompletionTest, Qualifiers) {
370 auto Results = completions(R"cpp(
371 class Foo {
372 public: int foo() const;
373 int bar() const;
374 };
375 class Bar : public Foo {
376 int foo() const;
377 };
378 void test() { Bar().^ }
379 )cpp");
Sam McCalle746a2b2018-07-02 11:13:16 +0000380 EXPECT_THAT(Results.Completions,
381 HasSubsequence(AllOf(Qualifier(""), Named("bar")),
382 AllOf(Qualifier("Foo::"), Named("foo"))));
383 EXPECT_THAT(Results.Completions,
384 Not(Contains(AllOf(Qualifier(""), Named("foo"))))); // private
Sam McCall44fdcec22017-12-08 15:00:59 +0000385}
386
Sam McCall4caa8512018-06-07 12:49:17 +0000387TEST(CompletionTest, InjectedTypename) {
388 // These are suppressed when accessed as a member...
Sam McCalle746a2b2018-07-02 11:13:16 +0000389 EXPECT_THAT(completions("struct X{}; void foo(){ X().^ }").Completions,
Sam McCall4caa8512018-06-07 12:49:17 +0000390 Not(Has("X")));
Sam McCalle746a2b2018-07-02 11:13:16 +0000391 EXPECT_THAT(completions("struct X{ void foo(){ this->^ } };").Completions,
Sam McCall4caa8512018-06-07 12:49:17 +0000392 Not(Has("X")));
393 // ...but accessible in other, more useful cases.
Sam McCalle746a2b2018-07-02 11:13:16 +0000394 EXPECT_THAT(completions("struct X{ void foo(){ ^ } };").Completions,
395 Has("X"));
396 EXPECT_THAT(
397 completions("struct Y{}; struct X:Y{ void foo(){ ^ } };").Completions,
398 Has("Y"));
Sam McCall4caa8512018-06-07 12:49:17 +0000399 EXPECT_THAT(
400 completions(
401 "template<class> struct Y{}; struct X:Y<int>{ void foo(){ ^ } };")
Sam McCalle746a2b2018-07-02 11:13:16 +0000402 .Completions,
Sam McCall4caa8512018-06-07 12:49:17 +0000403 Has("Y"));
404 // This case is marginal (`using X::X` is useful), we allow it for now.
Sam McCalle746a2b2018-07-02 11:13:16 +0000405 EXPECT_THAT(completions("struct X{}; void foo(){ X::^ }").Completions,
406 Has("X"));
Sam McCall4caa8512018-06-07 12:49:17 +0000407}
408
Sam McCall44fdcec22017-12-08 15:00:59 +0000409TEST(CompletionTest, Snippets) {
410 clangd::CodeCompleteOptions Opts;
Sam McCall44fdcec22017-12-08 15:00:59 +0000411 auto Results = completions(
412 R"cpp(
413 struct fake {
414 int a;
415 int f(int i, const float f) const;
416 };
417 int main() {
418 fake f;
419 f.^
420 }
421 )cpp",
Sam McCalla15c2d62018-01-18 09:27:56 +0000422 /*IndexSymbols=*/{}, Opts);
Sam McCalle746a2b2018-07-02 11:13:16 +0000423 EXPECT_THAT(
424 Results.Completions,
425 HasSubsequence(Named("a"),
426 SnippetSuffix("(${1:int i}, ${2:const float f})")));
Sam McCall44fdcec22017-12-08 15:00:59 +0000427}
428
429TEST(CompletionTest, Kinds) {
Sam McCall545a20d2018-01-19 14:34:02 +0000430 auto Results = completions(
431 R"cpp(
432 #define MACRO X
433 int variable;
434 struct Struct {};
435 int function();
436 int X = ^
437 )cpp",
438 {func("indexFunction"), var("indexVariable"), cls("indexClass")});
Sam McCalle746a2b2018-07-02 11:13:16 +0000439 EXPECT_THAT(Results.Completions,
Sam McCall545a20d2018-01-19 14:34:02 +0000440 AllOf(Has("function", CompletionItemKind::Function),
441 Has("variable", CompletionItemKind::Variable),
442 Has("int", CompletionItemKind::Keyword),
443 Has("Struct", CompletionItemKind::Class),
444 Has("MACRO", CompletionItemKind::Text),
445 Has("indexFunction", CompletionItemKind::Function),
446 Has("indexVariable", CompletionItemKind::Variable),
447 Has("indexClass", CompletionItemKind::Class)));
Sam McCall44fdcec22017-12-08 15:00:59 +0000448
Sam McCall44fdcec22017-12-08 15:00:59 +0000449 Results = completions("nam^");
Sam McCalle746a2b2018-07-02 11:13:16 +0000450 EXPECT_THAT(Results.Completions,
451 Has("namespace", CompletionItemKind::Snippet));
Sam McCall44fdcec22017-12-08 15:00:59 +0000452}
453
Sam McCall84652cc2018-01-12 16:16:09 +0000454TEST(CompletionTest, NoDuplicates) {
Sam McCall545a20d2018-01-19 14:34:02 +0000455 auto Results = completions(
456 R"cpp(
457 class Adapter {
Sam McCall545a20d2018-01-19 14:34:02 +0000458 };
Sam McCall84652cc2018-01-12 16:16:09 +0000459
Eric Liu9b3cba72018-05-30 09:03:39 +0000460 void f() {
Sam McCall545a20d2018-01-19 14:34:02 +0000461 Adapter^
462 }
463 )cpp",
464 {cls("Adapter")});
Sam McCall84652cc2018-01-12 16:16:09 +0000465
466 // Make sure there are no duplicate entries of 'Adapter'.
Sam McCalle746a2b2018-07-02 11:13:16 +0000467 EXPECT_THAT(Results.Completions, ElementsAre(Named("Adapter")));
Sam McCall84652cc2018-01-12 16:16:09 +0000468}
469
Sam McCall545a20d2018-01-19 14:34:02 +0000470TEST(CompletionTest, ScopedNoIndex) {
471 auto Results = completions(
472 R"cpp(
Sam McCall8b2dcc12018-06-14 13:50:30 +0000473 namespace fake { int BigBang, Babble, Box; };
474 int main() { fake::ba^ }
Sam McCall545a20d2018-01-19 14:34:02 +0000475 ")cpp");
Sam McCall8b2dcc12018-06-14 13:50:30 +0000476 // Babble is a better match than BigBang. Box doesn't match at all.
Sam McCalle746a2b2018-07-02 11:13:16 +0000477 EXPECT_THAT(Results.Completions,
478 ElementsAre(Named("Babble"), Named("BigBang")));
Sam McCall84652cc2018-01-12 16:16:09 +0000479}
480
Sam McCall545a20d2018-01-19 14:34:02 +0000481TEST(CompletionTest, Scoped) {
Sam McCalla15c2d62018-01-18 09:27:56 +0000482 auto Results = completions(
483 R"cpp(
Sam McCall8b2dcc12018-06-14 13:50:30 +0000484 namespace fake { int Babble, Box; };
485 int main() { fake::ba^ }
Sam McCall545a20d2018-01-19 14:34:02 +0000486 ")cpp",
487 {var("fake::BigBang")});
Sam McCalle746a2b2018-07-02 11:13:16 +0000488 EXPECT_THAT(Results.Completions,
489 ElementsAre(Named("Babble"), Named("BigBang")));
Sam McCalla15c2d62018-01-18 09:27:56 +0000490}
491
Sam McCall545a20d2018-01-19 14:34:02 +0000492TEST(CompletionTest, ScopedWithFilter) {
Sam McCalla15c2d62018-01-18 09:27:56 +0000493 auto Results = completions(
494 R"cpp(
495 void f() { ns::x^ }
496 )cpp",
497 {cls("ns::XYZ"), func("ns::foo")});
Sam McCalle746a2b2018-07-02 11:13:16 +0000498 EXPECT_THAT(Results.Completions, UnorderedElementsAre(Named("XYZ")));
Sam McCalla15c2d62018-01-18 09:27:56 +0000499}
500
Sam McCalldc8abc42018-05-03 14:53:02 +0000501TEST(CompletionTest, ReferencesAffectRanking) {
Eric Liu84bd5db2018-07-25 11:26:35 +0000502 auto Results = completions("int main() { abs^ }", {ns("absl"), func("absb")});
503 EXPECT_THAT(Results.Completions, HasSubsequence(Named("absb"), Named("absl")));
Sam McCalldc8abc42018-05-03 14:53:02 +0000504 Results = completions("int main() { abs^ }",
Eric Liu84bd5db2018-07-25 11:26:35 +0000505 {withReferences(10000, ns("absl")), func("absb")});
506 EXPECT_THAT(Results.Completions,
507 HasSubsequence(Named("absl"), Named("absb")));
Sam McCalldc8abc42018-05-03 14:53:02 +0000508}
509
Sam McCall545a20d2018-01-19 14:34:02 +0000510TEST(CompletionTest, GlobalQualified) {
Sam McCalla15c2d62018-01-18 09:27:56 +0000511 auto Results = completions(
512 R"cpp(
513 void f() { ::^ }
514 )cpp",
515 {cls("XYZ")});
Sam McCalle746a2b2018-07-02 11:13:16 +0000516 EXPECT_THAT(Results.Completions,
517 AllOf(Has("XYZ", CompletionItemKind::Class),
518 Has("f", CompletionItemKind::Function)));
Sam McCalla15c2d62018-01-18 09:27:56 +0000519}
520
Sam McCall545a20d2018-01-19 14:34:02 +0000521TEST(CompletionTest, FullyQualified) {
Sam McCalla15c2d62018-01-18 09:27:56 +0000522 auto Results = completions(
523 R"cpp(
Sam McCall545a20d2018-01-19 14:34:02 +0000524 namespace ns { void bar(); }
Sam McCalla15c2d62018-01-18 09:27:56 +0000525 void f() { ::ns::^ }
526 )cpp",
527 {cls("ns::XYZ")});
Sam McCalle746a2b2018-07-02 11:13:16 +0000528 EXPECT_THAT(Results.Completions,
529 AllOf(Has("XYZ", CompletionItemKind::Class),
530 Has("bar", CompletionItemKind::Function)));
Sam McCall545a20d2018-01-19 14:34:02 +0000531}
532
533TEST(CompletionTest, SemaIndexMerge) {
534 auto Results = completions(
535 R"cpp(
536 namespace ns { int local; void both(); }
537 void f() { ::ns::^ }
538 )cpp",
539 {func("ns::both"), cls("ns::Index")});
540 // We get results from both index and sema, with no duplicates.
Sam McCall2161ec72018-07-05 06:20:41 +0000541 EXPECT_THAT(Results.Completions,
542 UnorderedElementsAre(
543 AllOf(Named("local"), Origin(SymbolOrigin::AST)),
544 AllOf(Named("Index"), Origin(SymbolOrigin::Static)),
545 AllOf(Named("both"),
546 Origin(SymbolOrigin::AST | SymbolOrigin::Static))));
Sam McCalla15c2d62018-01-18 09:27:56 +0000547}
548
Haojian Wu48b48652018-01-25 09:20:09 +0000549TEST(CompletionTest, SemaIndexMergeWithLimit) {
550 clangd::CodeCompleteOptions Opts;
551 Opts.Limit = 1;
552 auto Results = completions(
553 R"cpp(
554 namespace ns { int local; void both(); }
555 void f() { ::ns::^ }
556 )cpp",
557 {func("ns::both"), cls("ns::Index")}, Opts);
Sam McCalle746a2b2018-07-02 11:13:16 +0000558 EXPECT_EQ(Results.Completions.size(), Opts.Limit);
559 EXPECT_TRUE(Results.HasMore);
Haojian Wu48b48652018-01-25 09:20:09 +0000560}
561
Eric Liu63f419a2018-05-15 15:29:32 +0000562TEST(CompletionTest, IncludeInsertionPreprocessorIntegrationTests) {
563 MockFSProvider FS;
564 MockCompilationDatabase CDB;
565 std::string Subdir = testPath("sub");
566 std::string SearchDirArg = (llvm::Twine("-I") + Subdir).str();
567 CDB.ExtraClangFlags = {SearchDirArg.c_str()};
568 std::string BarHeader = testPath("sub/bar.h");
569 FS.Files[BarHeader] = "";
570
571 IgnoreDiagnostics DiagConsumer;
572 ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
Eric Liu63f419a2018-05-15 15:29:32 +0000573 auto BarURI = URI::createFile(BarHeader).toString();
574 Symbol Sym = cls("ns::X");
575 Sym.CanonicalDeclaration.FileURI = BarURI;
Eric Liu83f63e42018-09-03 10:18:21 +0000576 Sym.IncludeHeaders.emplace_back(BarURI, 1);
Eric Liu63f419a2018-05-15 15:29:32 +0000577 // Shoten include path based on search dirctory and insert.
578 auto Results = completions(Server,
579 R"cpp(
580 int main() { ns::^ }
581 )cpp",
582 {Sym});
Sam McCalle746a2b2018-07-02 11:13:16 +0000583 EXPECT_THAT(Results.Completions,
584 ElementsAre(AllOf(Named("X"), InsertInclude("\"bar.h\""))));
Eric Liu63f419a2018-05-15 15:29:32 +0000585 // Duplicate based on inclusions in preamble.
586 Results = completions(Server,
587 R"cpp(
588 #include "sub/bar.h" // not shortest, so should only match resolved.
589 int main() { ns::^ }
590 )cpp",
591 {Sym});
Sam McCalle746a2b2018-07-02 11:13:16 +0000592 EXPECT_THAT(Results.Completions, ElementsAre(AllOf(Named("X"), Labeled("X"),
593 Not(InsertInclude()))));
Eric Liu63f419a2018-05-15 15:29:32 +0000594}
595
Eric Liu9b3cba72018-05-30 09:03:39 +0000596TEST(CompletionTest, NoIncludeInsertionWhenDeclFoundInFile) {
597 MockFSProvider FS;
598 MockCompilationDatabase CDB;
599
600 IgnoreDiagnostics DiagConsumer;
601 ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
Eric Liu9b3cba72018-05-30 09:03:39 +0000602 Symbol SymX = cls("ns::X");
603 Symbol SymY = cls("ns::Y");
604 std::string BarHeader = testPath("bar.h");
605 auto BarURI = URI::createFile(BarHeader).toString();
606 SymX.CanonicalDeclaration.FileURI = BarURI;
607 SymY.CanonicalDeclaration.FileURI = BarURI;
Eric Liu83f63e42018-09-03 10:18:21 +0000608 SymX.IncludeHeaders.emplace_back("<bar>", 1);
609 SymY.IncludeHeaders.emplace_back("<bar>", 1);
Eric Liu9b3cba72018-05-30 09:03:39 +0000610 // Shoten include path based on search dirctory and insert.
611 auto Results = completions(Server,
612 R"cpp(
613 namespace ns {
614 class X;
615 class Y {}
616 }
617 int main() { ns::^ }
618 )cpp",
619 {SymX, SymY});
Sam McCalle746a2b2018-07-02 11:13:16 +0000620 EXPECT_THAT(Results.Completions,
621 ElementsAre(AllOf(Named("X"), Not(InsertInclude())),
622 AllOf(Named("Y"), Not(InsertInclude()))));
Eric Liu9b3cba72018-05-30 09:03:39 +0000623}
624
Sam McCalla15c2d62018-01-18 09:27:56 +0000625TEST(CompletionTest, IndexSuppressesPreambleCompletions) {
626 MockFSProvider FS;
627 MockCompilationDatabase CDB;
628 IgnoreDiagnostics DiagConsumer;
Sam McCall7363a2f2018-03-05 17:28:54 +0000629 ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
Sam McCalla15c2d62018-01-18 09:27:56 +0000630
Sam McCallc1568062018-02-16 09:41:43 +0000631 FS.Files[testPath("bar.h")] =
Sam McCalld5ea3e32018-01-24 17:53:32 +0000632 R"cpp(namespace ns { struct preamble { int member; }; })cpp";
Sam McCallc1568062018-02-16 09:41:43 +0000633 auto File = testPath("foo.cpp");
Sam McCalla15c2d62018-01-18 09:27:56 +0000634 Annotations Test(R"cpp(
635 #include "bar.h"
636 namespace ns { int local; }
Sam McCalld5ea3e32018-01-24 17:53:32 +0000637 void f() { ns::^; }
638 void f() { ns::preamble().$2^; }
Sam McCalla15c2d62018-01-18 09:27:56 +0000639 )cpp");
Sam McCall7363a2f2018-03-05 17:28:54 +0000640 runAddDocument(Server, File, Test.code());
Sam McCalla15c2d62018-01-18 09:27:56 +0000641 clangd::CodeCompleteOptions Opts = {};
642
Sam McCalla15c2d62018-01-18 09:27:56 +0000643 auto I = memIndex({var("ns::index")});
644 Opts.Index = I.get();
Sam McCalla7bb0cc2018-03-12 23:22:35 +0000645 auto WithIndex = cantFail(runCodeComplete(Server, File, Test.point(), Opts));
Sam McCalle746a2b2018-07-02 11:13:16 +0000646 EXPECT_THAT(WithIndex.Completions,
Sam McCalla15c2d62018-01-18 09:27:56 +0000647 UnorderedElementsAre(Named("local"), Named("index")));
Sam McCalld5ea3e32018-01-24 17:53:32 +0000648 auto ClassFromPreamble =
Sam McCalla7bb0cc2018-03-12 23:22:35 +0000649 cantFail(runCodeComplete(Server, File, Test.point("2"), Opts));
Sam McCalle746a2b2018-07-02 11:13:16 +0000650 EXPECT_THAT(ClassFromPreamble.Completions, Contains(Named("member")));
Sam McCall0bb24cd2018-02-13 08:59:23 +0000651
652 Opts.Index = nullptr;
Sam McCalla7bb0cc2018-03-12 23:22:35 +0000653 auto WithoutIndex =
654 cantFail(runCodeComplete(Server, File, Test.point(), Opts));
Sam McCalle746a2b2018-07-02 11:13:16 +0000655 EXPECT_THAT(WithoutIndex.Completions,
Sam McCall0bb24cd2018-02-13 08:59:23 +0000656 UnorderedElementsAre(Named("local"), Named("preamble")));
Sam McCalla15c2d62018-01-18 09:27:56 +0000657}
658
659TEST(CompletionTest, DynamicIndexMultiFile) {
660 MockFSProvider FS;
661 MockCompilationDatabase CDB;
662 IgnoreDiagnostics DiagConsumer;
Sam McCall7363a2f2018-03-05 17:28:54 +0000663 auto Opts = ClangdServer::optsForTest();
664 Opts.BuildDynamicSymbolIndex = true;
665 ClangdServer Server(CDB, FS, DiagConsumer, Opts);
Sam McCalla15c2d62018-01-18 09:27:56 +0000666
Eric Liu709bde82018-02-19 18:48:44 +0000667 FS.Files[testPath("foo.h")] = R"cpp(
Sam McCalla15c2d62018-01-18 09:27:56 +0000668 namespace ns { class XYZ {}; void foo(int x) {} }
Eric Liu709bde82018-02-19 18:48:44 +0000669 )cpp";
Sam McCall7363a2f2018-03-05 17:28:54 +0000670 runAddDocument(Server, testPath("foo.cpp"), R"cpp(
Eric Liu709bde82018-02-19 18:48:44 +0000671 #include "foo.h"
Sam McCall0bb24cd2018-02-13 08:59:23 +0000672 )cpp");
Sam McCalla15c2d62018-01-18 09:27:56 +0000673
Sam McCallc1568062018-02-16 09:41:43 +0000674 auto File = testPath("bar.cpp");
Sam McCalla15c2d62018-01-18 09:27:56 +0000675 Annotations Test(R"cpp(
676 namespace ns {
677 class XXX {};
678 /// Doooc
679 void fooooo() {}
680 }
681 void f() { ns::^ }
682 )cpp");
Sam McCall7363a2f2018-03-05 17:28:54 +0000683 runAddDocument(Server, File, Test.code());
Sam McCalla15c2d62018-01-18 09:27:56 +0000684
Sam McCalla7bb0cc2018-03-12 23:22:35 +0000685 auto Results = cantFail(runCodeComplete(Server, File, Test.point(), {}));
Sam McCalla15c2d62018-01-18 09:27:56 +0000686 // "XYZ" and "foo" are not included in the file being completed but are still
687 // visible through the index.
Sam McCalle746a2b2018-07-02 11:13:16 +0000688 EXPECT_THAT(Results.Completions, Has("XYZ", CompletionItemKind::Class));
689 EXPECT_THAT(Results.Completions, Has("foo", CompletionItemKind::Function));
690 EXPECT_THAT(Results.Completions, Has("XXX", CompletionItemKind::Class));
691 EXPECT_THAT(Results.Completions,
692 Contains((Named("fooooo"), Kind(CompletionItemKind::Function),
693 Doc("Doooc"), ReturnType("void"))));
Sam McCalla15c2d62018-01-18 09:27:56 +0000694}
695
Ilya Biryukovc22d3442018-05-16 12:32:49 +0000696TEST(CompletionTest, Documentation) {
697 auto Results = completions(
698 R"cpp(
699 // Non-doxygen comment.
700 int foo();
701 /// Doxygen comment.
702 /// \param int a
703 int bar(int a);
704 /* Multi-line
705 block comment
706 */
707 int baz();
708
709 int x = ^
710 )cpp");
Sam McCalle746a2b2018-07-02 11:13:16 +0000711 EXPECT_THAT(Results.Completions,
Ilya Biryukovc22d3442018-05-16 12:32:49 +0000712 Contains(AllOf(Named("foo"), Doc("Non-doxygen comment."))));
713 EXPECT_THAT(
Sam McCalle746a2b2018-07-02 11:13:16 +0000714 Results.Completions,
Ilya Biryukovc22d3442018-05-16 12:32:49 +0000715 Contains(AllOf(Named("bar"), Doc("Doxygen comment.\n\\param int a"))));
Sam McCalle746a2b2018-07-02 11:13:16 +0000716 EXPECT_THAT(Results.Completions,
Ilya Biryukovc22d3442018-05-16 12:32:49 +0000717 Contains(AllOf(Named("baz"), Doc("Multi-line\nblock comment"))));
718}
719
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +0000720TEST(CompletionTest, GlobalCompletionFiltering) {
721
722 Symbol Class = cls("XYZ");
723 Class.IsIndexedForCodeCompletion = false;
724 Symbol Func = func("XYZ::foooo");
725 Func.IsIndexedForCodeCompletion = false;
726
727 auto Results = completions(R"(// void f() {
728 XYZ::foooo^
729 })",
730 {Class, Func});
Sam McCalle746a2b2018-07-02 11:13:16 +0000731 EXPECT_THAT(Results.Completions, IsEmpty());
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +0000732}
733
Haojian Wu58d208d2018-01-25 09:44:06 +0000734TEST(CodeCompleteTest, DisableTypoCorrection) {
735 auto Results = completions(R"cpp(
736 namespace clang { int v; }
737 void f() { clangd::^
738 )cpp");
Sam McCalle746a2b2018-07-02 11:13:16 +0000739 EXPECT_TRUE(Results.Completions.empty());
Haojian Wu58d208d2018-01-25 09:44:06 +0000740}
741
Ilya Biryukov53d6d932018-03-06 16:45:21 +0000742TEST(CodeCompleteTest, NoColonColonAtTheEnd) {
743 auto Results = completions(R"cpp(
744 namespace clang { }
745 void f() {
746 clan^
747 }
748 )cpp");
749
Sam McCalle746a2b2018-07-02 11:13:16 +0000750 EXPECT_THAT(Results.Completions, Contains(Labeled("clang")));
751 EXPECT_THAT(Results.Completions, Not(Contains(Labeled("clang::"))));
Ilya Biryukov53d6d932018-03-06 16:45:21 +0000752}
753
Ilya Biryukov94da7bd2018-03-16 15:23:44 +0000754TEST(CompletionTest, BacktrackCrashes) {
755 // Sema calls code completion callbacks twice in these cases.
756 auto Results = completions(R"cpp(
757 namespace ns {
758 struct FooBarBaz {};
759 } // namespace ns
760
761 int foo(ns::FooBar^
762 )cpp");
763
Sam McCalle746a2b2018-07-02 11:13:16 +0000764 EXPECT_THAT(Results.Completions, ElementsAre(Labeled("FooBarBaz")));
Ilya Biryukov94da7bd2018-03-16 15:23:44 +0000765
766 // Check we don't crash in that case too.
767 completions(R"cpp(
768 struct FooBarBaz {};
769 void test() {
770 if (FooBarBaz * x^) {}
771 }
772)cpp");
773}
774
Eric Liu42abe412018-05-24 11:20:19 +0000775TEST(CompletionTest, CompleteInMacroWithStringification) {
776 auto Results = completions(R"cpp(
777void f(const char *, int x);
778#define F(x) f(#x, x)
779
780namespace ns {
781int X;
782int Y;
783} // namespace ns
784
785int f(int input_num) {
786 F(ns::^)
787}
788)cpp");
789
Sam McCalle746a2b2018-07-02 11:13:16 +0000790 EXPECT_THAT(Results.Completions,
Eric Liu42abe412018-05-24 11:20:19 +0000791 UnorderedElementsAre(Named("X"), Named("Y")));
792}
793
794TEST(CompletionTest, CompleteInMacroAndNamespaceWithStringification) {
795 auto Results = completions(R"cpp(
796void f(const char *, int x);
797#define F(x) f(#x, x)
798
799namespace ns {
800int X;
801
802int f(int input_num) {
803 F(^)
804}
805} // namespace ns
806)cpp");
807
Sam McCalle746a2b2018-07-02 11:13:16 +0000808 EXPECT_THAT(Results.Completions, Contains(Named("X")));
Eric Liu42abe412018-05-24 11:20:19 +0000809}
810
Eric Liu485074f2018-07-11 13:15:31 +0000811TEST(CompletionTest, IgnoreCompleteInExcludedPPBranchWithRecoveryContext) {
Ilya Biryukov94da7bd2018-03-16 15:23:44 +0000812 auto Results = completions(R"cpp(
813 int bar(int param_in_bar) {
814 }
815
816 int foo(int param_in_foo) {
817#if 0
Eric Liu485074f2018-07-11 13:15:31 +0000818 // In recorvery mode, "param_in_foo" will also be suggested among many other
819 // unrelated symbols; however, this is really a special case where this works.
820 // If the #if block is outside of the function, "param_in_foo" is still
821 // suggested, but "bar" and "foo" are missing. So the recovery mode doesn't
822 // really provide useful results in excluded branches.
Ilya Biryukov94da7bd2018-03-16 15:23:44 +0000823 par^
824#endif
825 }
826)cpp");
827
Eric Liu485074f2018-07-11 13:15:31 +0000828 EXPECT_TRUE(Results.Completions.empty());
Ilya Biryukov94da7bd2018-03-16 15:23:44 +0000829}
Ilya Biryukov43c292c2018-08-30 13:14:31 +0000830SignatureHelp signatures(StringRef Text, Position Point,
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000831 std::vector<Symbol> IndexSymbols = {}) {
832 std::unique_ptr<SymbolIndex> Index;
833 if (!IndexSymbols.empty())
834 Index = memIndex(IndexSymbols);
835
Sam McCall800d4372017-12-19 10:29:27 +0000836 MockFSProvider FS;
837 MockCompilationDatabase CDB;
838 IgnoreDiagnostics DiagConsumer;
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000839 ClangdServer::Options Opts = ClangdServer::optsForTest();
840 Opts.StaticIndex = Index.get();
841
842 ClangdServer Server(CDB, FS, DiagConsumer, Opts);
Sam McCallc1568062018-02-16 09:41:43 +0000843 auto File = testPath("foo.cpp");
Ilya Biryukov43c292c2018-08-30 13:14:31 +0000844 runAddDocument(Server, File, Text);
845 return cantFail(runSignatureHelp(Server, File, Point));
846}
847
848SignatureHelp signatures(StringRef Text,
849 std::vector<Symbol> IndexSymbols = {}) {
Sam McCall328cbdb2017-12-20 16:06:05 +0000850 Annotations Test(Text);
Ilya Biryukov43c292c2018-08-30 13:14:31 +0000851 return signatures(Test.code(), Test.point(), std::move(IndexSymbols));
Sam McCall800d4372017-12-19 10:29:27 +0000852}
853
854MATCHER_P(ParamsAre, P, "") {
855 if (P.size() != arg.parameters.size())
856 return false;
857 for (unsigned I = 0; I < P.size(); ++I)
858 if (P[I] != arg.parameters[I].label)
859 return false;
860 return true;
861}
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000862MATCHER_P(SigDoc, Doc, "") { return arg.documentation == Doc; }
Sam McCall800d4372017-12-19 10:29:27 +0000863
864Matcher<SignatureInformation> Sig(std::string Label,
865 std::vector<std::string> Params) {
Eric Liu8f3678d2018-06-15 13:34:18 +0000866 return AllOf(SigHelpLabeled(Label), ParamsAre(Params));
Sam McCall800d4372017-12-19 10:29:27 +0000867}
868
869TEST(SignatureHelpTest, Overloads) {
870 auto Results = signatures(R"cpp(
871 void foo(int x, int y);
872 void foo(int x, float y);
873 void foo(float x, int y);
874 void foo(float x, float y);
875 void bar(int x, int y = 0);
876 int main() { foo(^); }
877 )cpp");
878 EXPECT_THAT(Results.signatures,
879 UnorderedElementsAre(
880 Sig("foo(float x, float y) -> void", {"float x", "float y"}),
881 Sig("foo(float x, int y) -> void", {"float x", "int y"}),
882 Sig("foo(int x, float y) -> void", {"int x", "float y"}),
883 Sig("foo(int x, int y) -> void", {"int x", "int y"})));
884 // We always prefer the first signature.
885 EXPECT_EQ(0, Results.activeSignature);
886 EXPECT_EQ(0, Results.activeParameter);
887}
888
889TEST(SignatureHelpTest, DefaultArgs) {
890 auto Results = signatures(R"cpp(
891 void bar(int x, int y = 0);
892 void bar(float x = 0, int y = 42);
893 int main() { bar(^
894 )cpp");
895 EXPECT_THAT(Results.signatures,
896 UnorderedElementsAre(
897 Sig("bar(int x, int y = 0) -> void", {"int x", "int y = 0"}),
898 Sig("bar(float x = 0, int y = 42) -> void",
899 {"float x = 0", "int y = 42"})));
900 EXPECT_EQ(0, Results.activeSignature);
901 EXPECT_EQ(0, Results.activeParameter);
902}
903
904TEST(SignatureHelpTest, ActiveArg) {
905 auto Results = signatures(R"cpp(
906 int baz(int a, int b, int c);
907 int main() { baz(baz(1,2,3), ^); }
908 )cpp");
909 EXPECT_THAT(Results.signatures,
910 ElementsAre(Sig("baz(int a, int b, int c) -> int",
911 {"int a", "int b", "int c"})));
912 EXPECT_EQ(0, Results.activeSignature);
913 EXPECT_EQ(1, Results.activeParameter);
914}
915
Ilya Biryukov43c292c2018-08-30 13:14:31 +0000916TEST(SignatureHelpTest, OpeningParen) {
917 llvm::StringLiteral Tests[] = {// Recursive function call.
918 R"cpp(
919 int foo(int a, int b, int c);
920 int main() {
921 foo(foo $p^( foo(10, 10, 10), ^ )));
922 })cpp",
923 // Functional type cast.
924 R"cpp(
925 struct Foo {
926 Foo(int a, int b, int c);
927 };
928 int main() {
929 Foo $p^( 10, ^ );
930 })cpp",
931 // New expression.
932 R"cpp(
933 struct Foo {
934 Foo(int a, int b, int c);
935 };
936 int main() {
937 new Foo $p^( 10, ^ );
938 })cpp",
939 // Macro expansion.
940 R"cpp(
941 int foo(int a, int b, int c);
942 #define FOO foo(
943
944 int main() {
945 // Macro expansions.
946 $p^FOO 10, ^ );
947 })cpp",
948 // Macro arguments.
949 R"cpp(
950 int foo(int a, int b, int c);
951 int main() {
952 #define ID(X) X
953 ID(foo $p^( foo(10), ^ ))
954 })cpp"};
955
956 for (auto Test : Tests) {
957 Annotations Code(Test);
958 EXPECT_EQ(signatures(Code.code(), Code.point()).argListStart,
959 Code.point("p"))
960 << "Test source:" << Test;
961 }
962}
963
Haojian Wu061c73e2018-01-23 11:37:26 +0000964class IndexRequestCollector : public SymbolIndex {
965public:
966 bool
Sam McCalld1a7a372018-01-31 13:40:48 +0000967 fuzzyFind(const FuzzyFindRequest &Req,
Haojian Wu061c73e2018-01-23 11:37:26 +0000968 llvm::function_ref<void(const Symbol &)> Callback) const override {
969 Requests.push_back(Req);
Sam McCallab8e3932018-02-19 13:04:41 +0000970 return true;
Haojian Wu061c73e2018-01-23 11:37:26 +0000971 }
972
Eric Liu9ec459f2018-03-14 09:48:05 +0000973 void lookup(const LookupRequest &,
974 llvm::function_ref<void(const Symbol &)>) const override {}
975
Sam McCallb0138312018-09-04 14:39:56 +0000976 void refs(const RefsRequest &,
977 llvm::function_ref<void(const Ref &)>) const override {}
Haojian Wu65ac3212018-08-06 13:14:32 +0000978
Kirill Bobyrevfc890012018-08-24 09:12:54 +0000979 // This is incorrect, but IndexRequestCollector is not an actual index and it
980 // isn't used in production code.
981 size_t estimateMemoryUsage() const override { return 0; }
982
Eric Liu25d74e92018-08-24 11:23:56 +0000983 const std::vector<FuzzyFindRequest> consumeRequests() const {
984 auto Reqs = std::move(Requests);
985 Requests = {};
986 return Reqs;
987 }
Haojian Wu061c73e2018-01-23 11:37:26 +0000988
989private:
990 mutable std::vector<FuzzyFindRequest> Requests;
991};
992
993std::vector<FuzzyFindRequest> captureIndexRequests(llvm::StringRef Code) {
994 clangd::CodeCompleteOptions Opts;
995 IndexRequestCollector Requests;
996 Opts.Index = &Requests;
997 completions(Code, {}, Opts);
Eric Liu25d74e92018-08-24 11:23:56 +0000998 return Requests.consumeRequests();
Haojian Wu061c73e2018-01-23 11:37:26 +0000999}
1000
1001TEST(CompletionTest, UnqualifiedIdQuery) {
1002 auto Requests = captureIndexRequests(R"cpp(
1003 namespace std {}
1004 using namespace std;
1005 namespace ns {
1006 void f() {
1007 vec^
1008 }
1009 }
1010 )cpp");
1011
1012 EXPECT_THAT(Requests,
1013 ElementsAre(Field(&FuzzyFindRequest::Scopes,
1014 UnorderedElementsAre("", "ns::", "std::"))));
1015}
1016
1017TEST(CompletionTest, ResolvedQualifiedIdQuery) {
1018 auto Requests = captureIndexRequests(R"cpp(
1019 namespace ns1 {}
1020 namespace ns2 {} // ignore
1021 namespace ns3 { namespace nns3 {} }
1022 namespace foo {
1023 using namespace ns1;
1024 using namespace ns3::nns3;
1025 }
1026 namespace ns {
1027 void f() {
1028 foo::^
1029 }
1030 }
1031 )cpp");
1032
1033 EXPECT_THAT(Requests,
1034 ElementsAre(Field(
1035 &FuzzyFindRequest::Scopes,
1036 UnorderedElementsAre("foo::", "ns1::", "ns3::nns3::"))));
1037}
1038
1039TEST(CompletionTest, UnresolvedQualifierIdQuery) {
1040 auto Requests = captureIndexRequests(R"cpp(
1041 namespace a {}
1042 using namespace a;
1043 namespace ns {
1044 void f() {
1045 bar::^
1046 }
1047 } // namespace ns
1048 )cpp");
1049
1050 EXPECT_THAT(Requests, ElementsAre(Field(&FuzzyFindRequest::Scopes,
1051 UnorderedElementsAre("bar::"))));
1052}
1053
1054TEST(CompletionTest, UnresolvedNestedQualifierIdQuery) {
1055 auto Requests = captureIndexRequests(R"cpp(
1056 namespace a {}
1057 using namespace a;
1058 namespace ns {
1059 void f() {
1060 ::a::bar::^
1061 }
1062 } // namespace ns
1063 )cpp");
1064
1065 EXPECT_THAT(Requests, ElementsAre(Field(&FuzzyFindRequest::Scopes,
1066 UnorderedElementsAre("a::bar::"))));
1067}
1068
1069TEST(CompletionTest, EmptyQualifiedQuery) {
1070 auto Requests = captureIndexRequests(R"cpp(
1071 namespace ns {
1072 void f() {
1073 ^
1074 }
1075 } // namespace ns
1076 )cpp");
1077
1078 EXPECT_THAT(Requests, ElementsAre(Field(&FuzzyFindRequest::Scopes,
1079 UnorderedElementsAre("", "ns::"))));
1080}
1081
1082TEST(CompletionTest, GlobalQualifiedQuery) {
1083 auto Requests = captureIndexRequests(R"cpp(
1084 namespace ns {
1085 void f() {
1086 ::^
1087 }
1088 } // namespace ns
1089 )cpp");
1090
1091 EXPECT_THAT(Requests, ElementsAre(Field(&FuzzyFindRequest::Scopes,
1092 UnorderedElementsAre(""))));
1093}
1094
Ilya Biryukova907ba42018-05-14 10:50:04 +00001095TEST(CompletionTest, NoIndexCompletionsInsideClasses) {
1096 auto Completions = completions(
1097 R"cpp(
1098 struct Foo {
1099 int SomeNameOfField;
1100 typedef int SomeNameOfTypedefField;
1101 };
1102
1103 Foo::^)cpp",
1104 {func("::SomeNameInTheIndex"), func("::Foo::SomeNameInTheIndex")});
1105
Sam McCalle746a2b2018-07-02 11:13:16 +00001106 EXPECT_THAT(Completions.Completions,
Ilya Biryukova907ba42018-05-14 10:50:04 +00001107 AllOf(Contains(Labeled("SomeNameOfField")),
1108 Contains(Labeled("SomeNameOfTypedefField")),
1109 Not(Contains(Labeled("SomeNameInTheIndex")))));
1110}
1111
1112TEST(CompletionTest, NoIndexCompletionsInsideDependentCode) {
1113 {
1114 auto Completions = completions(
1115 R"cpp(
1116 template <class T>
1117 void foo() {
1118 T::^
1119 }
1120 )cpp",
1121 {func("::SomeNameInTheIndex")});
1122
Sam McCalle746a2b2018-07-02 11:13:16 +00001123 EXPECT_THAT(Completions.Completions,
Ilya Biryukova907ba42018-05-14 10:50:04 +00001124 Not(Contains(Labeled("SomeNameInTheIndex"))));
1125 }
1126
1127 {
1128 auto Completions = completions(
1129 R"cpp(
1130 template <class T>
1131 void foo() {
1132 T::template Y<int>::^
1133 }
1134 )cpp",
1135 {func("::SomeNameInTheIndex")});
1136
Sam McCalle746a2b2018-07-02 11:13:16 +00001137 EXPECT_THAT(Completions.Completions,
Ilya Biryukova907ba42018-05-14 10:50:04 +00001138 Not(Contains(Labeled("SomeNameInTheIndex"))));
1139 }
1140
1141 {
1142 auto Completions = completions(
1143 R"cpp(
1144 template <class T>
1145 void foo() {
1146 T::foo::^
1147 }
1148 )cpp",
1149 {func("::SomeNameInTheIndex")});
1150
Sam McCalle746a2b2018-07-02 11:13:16 +00001151 EXPECT_THAT(Completions.Completions,
Ilya Biryukova907ba42018-05-14 10:50:04 +00001152 Not(Contains(Labeled("SomeNameInTheIndex"))));
1153 }
1154}
1155
Sam McCallc18c2802018-06-15 11:06:29 +00001156TEST(CompletionTest, OverloadBundling) {
1157 clangd::CodeCompleteOptions Opts;
1158 Opts.BundleOverloads = true;
1159
1160 std::string Context = R"cpp(
1161 struct X {
1162 // Overload with int
1163 int a(int);
1164 // Overload with bool
1165 int a(bool);
1166 int b(float);
1167 };
1168 int GFuncC(int);
1169 int GFuncD(int);
1170 )cpp";
1171
1172 // Member completions are bundled.
Sam McCalle746a2b2018-07-02 11:13:16 +00001173 EXPECT_THAT(completions(Context + "int y = X().^", {}, Opts).Completions,
Sam McCallc18c2802018-06-15 11:06:29 +00001174 UnorderedElementsAre(Labeled("a(…)"), Labeled("b(float)")));
1175
1176 // Non-member completions are bundled, including index+sema.
1177 Symbol NoArgsGFunc = func("GFuncC");
1178 EXPECT_THAT(
Sam McCalle746a2b2018-07-02 11:13:16 +00001179 completions(Context + "int y = GFunc^", {NoArgsGFunc}, Opts).Completions,
Sam McCallc18c2802018-06-15 11:06:29 +00001180 UnorderedElementsAre(Labeled("GFuncC(…)"), Labeled("GFuncD(int)")));
1181
1182 // Differences in header-to-insert suppress bundling.
Sam McCallc18c2802018-06-15 11:06:29 +00001183 std::string DeclFile = URI::createFile(testPath("foo")).toString();
1184 NoArgsGFunc.CanonicalDeclaration.FileURI = DeclFile;
Eric Liu83f63e42018-09-03 10:18:21 +00001185 NoArgsGFunc.IncludeHeaders.emplace_back("<foo>", 1);
Sam McCallc18c2802018-06-15 11:06:29 +00001186 EXPECT_THAT(
Sam McCalle746a2b2018-07-02 11:13:16 +00001187 completions(Context + "int y = GFunc^", {NoArgsGFunc}, Opts).Completions,
1188 UnorderedElementsAre(AllOf(Named("GFuncC"), InsertInclude("<foo>")),
1189 Labeled("GFuncC(int)"), Labeled("GFuncD(int)")));
Sam McCallc18c2802018-06-15 11:06:29 +00001190
1191 // Examine a bundled completion in detail.
Sam McCalle746a2b2018-07-02 11:13:16 +00001192 auto A =
1193 completions(Context + "int y = X().a^", {}, Opts).Completions.front();
1194 EXPECT_EQ(A.Name, "a");
1195 EXPECT_EQ(A.Signature, "(…)");
1196 EXPECT_EQ(A.BundleSize, 2u);
1197 EXPECT_EQ(A.Kind, CompletionItemKind::Method);
1198 EXPECT_EQ(A.ReturnType, "int"); // All overloads return int.
Sam McCallc18c2802018-06-15 11:06:29 +00001199 // For now we just return one of the doc strings arbitrarily.
Sam McCalle746a2b2018-07-02 11:13:16 +00001200 EXPECT_THAT(A.Documentation, AnyOf(HasSubstr("Overload with int"),
Sam McCallc18c2802018-06-15 11:06:29 +00001201 HasSubstr("Overload with bool")));
Kadir Cetinkaya516fcda2018-08-23 12:19:39 +00001202 EXPECT_EQ(A.SnippetSuffix, "($0)");
Sam McCallc18c2802018-06-15 11:06:29 +00001203}
1204
Ilya Biryukov30b04b12018-05-28 09:54:51 +00001205TEST(CompletionTest, DocumentationFromChangedFileCrash) {
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +00001206 MockFSProvider FS;
1207 auto FooH = testPath("foo.h");
1208 auto FooCpp = testPath("foo.cpp");
1209 FS.Files[FooH] = R"cpp(
1210 // this is my documentation comment.
1211 int func();
1212 )cpp";
1213 FS.Files[FooCpp] = "";
1214
1215 MockCompilationDatabase CDB;
1216 IgnoreDiagnostics DiagConsumer;
1217 ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
1218
1219 Annotations Source(R"cpp(
1220 #include "foo.h"
1221 int func() {
1222 // This makes sure we have func from header in the AST.
1223 }
1224 int a = fun^
1225 )cpp");
1226 Server.addDocument(FooCpp, Source.code(), WantDiagnostics::Yes);
1227 // We need to wait for preamble to build.
1228 ASSERT_TRUE(Server.blockUntilIdleForTest());
1229
1230 // Change the header file. Completion will reuse the old preamble!
1231 FS.Files[FooH] = R"cpp(
1232 int func();
1233 )cpp";
1234
1235 clangd::CodeCompleteOptions Opts;
1236 Opts.IncludeComments = true;
Sam McCalle746a2b2018-07-02 11:13:16 +00001237 CodeCompleteResult Completions =
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +00001238 cantFail(runCodeComplete(Server, FooCpp, Source.point(), Opts));
1239 // We shouldn't crash. Unfortunately, current workaround is to not produce
1240 // comments for symbols from headers.
Sam McCalle746a2b2018-07-02 11:13:16 +00001241 EXPECT_THAT(Completions.Completions,
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +00001242 Contains(AllOf(Not(IsDocumented()), Named("func"))));
1243}
1244
Ilya Biryukov89fcf6b2018-06-15 08:31:17 +00001245TEST(CompletionTest, NonDocComments) {
1246 MockFSProvider FS;
1247 auto FooCpp = testPath("foo.cpp");
1248 FS.Files[FooCpp] = "";
1249
1250 MockCompilationDatabase CDB;
1251 IgnoreDiagnostics DiagConsumer;
1252 ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
1253
1254 Annotations Source(R"cpp(
Ilya Biryukovda8dd8b2018-06-27 09:47:20 +00001255 // We ignore namespace comments, for rationale see CodeCompletionStrings.h.
1256 namespace comments_ns {
1257 }
1258
Ilya Biryukov89fcf6b2018-06-15 08:31:17 +00001259 // ------------------
1260 int comments_foo();
1261
1262 // A comment and a decl are separated by newlines.
1263 // Therefore, the comment shouldn't show up as doc comment.
1264
1265 int comments_bar();
1266
1267 // this comment should be in the results.
1268 int comments_baz();
1269
1270
1271 template <class T>
1272 struct Struct {
1273 int comments_qux();
1274 int comments_quux();
1275 };
1276
1277
1278 // This comment should not be there.
1279
1280 template <class T>
1281 int Struct<T>::comments_qux() {
1282 }
1283
1284 // This comment **should** be in results.
1285 template <class T>
1286 int Struct<T>::comments_quux() {
1287 int a = comments^;
1288 }
1289 )cpp");
Reid Kleckner80274b12018-06-18 18:55:10 +00001290 // FIXME: Auto-completion in a template requires disabling delayed template
1291 // parsing.
1292 CDB.ExtraClangFlags.push_back("-fno-delayed-template-parsing");
Ilya Biryukov89fcf6b2018-06-15 08:31:17 +00001293 Server.addDocument(FooCpp, Source.code(), WantDiagnostics::Yes);
Sam McCalle746a2b2018-07-02 11:13:16 +00001294 CodeCompleteResult Completions = cantFail(runCodeComplete(
Ilya Biryukov89fcf6b2018-06-15 08:31:17 +00001295 Server, FooCpp, Source.point(), clangd::CodeCompleteOptions()));
1296
1297 // We should not get any of those comments in completion.
1298 EXPECT_THAT(
Sam McCalle746a2b2018-07-02 11:13:16 +00001299 Completions.Completions,
Ilya Biryukov89fcf6b2018-06-15 08:31:17 +00001300 UnorderedElementsAre(AllOf(Not(IsDocumented()), Named("comments_foo")),
1301 AllOf(IsDocumented(), Named("comments_baz")),
1302 AllOf(IsDocumented(), Named("comments_quux")),
Ilya Biryukovda8dd8b2018-06-27 09:47:20 +00001303 AllOf(Not(IsDocumented()), Named("comments_ns")),
Ilya Biryukov89fcf6b2018-06-15 08:31:17 +00001304 // FIXME(ibiryukov): the following items should have
1305 // empty documentation, since they are separated from
1306 // a comment with an empty line. Unfortunately, I
1307 // couldn't make Sema tests pass if we ignore those.
1308 AllOf(IsDocumented(), Named("comments_bar")),
1309 AllOf(IsDocumented(), Named("comments_qux"))));
1310}
1311
Ilya Biryukov981a35d2018-05-28 12:11:37 +00001312TEST(CompletionTest, CompleteOnInvalidLine) {
1313 auto FooCpp = testPath("foo.cpp");
1314
1315 MockCompilationDatabase CDB;
1316 IgnoreDiagnostics DiagConsumer;
1317 MockFSProvider FS;
1318 FS.Files[FooCpp] = "// empty file";
1319
1320 ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
1321 // Run completion outside the file range.
1322 Position Pos;
1323 Pos.line = 100;
1324 Pos.character = 0;
1325 EXPECT_THAT_EXPECTED(
1326 runCodeComplete(Server, FooCpp, Pos, clangd::CodeCompleteOptions()),
1327 Failed());
1328}
1329
Eric Liu7ad16962018-06-22 10:46:59 +00001330TEST(CompletionTest, QualifiedNames) {
1331 auto Results = completions(
1332 R"cpp(
1333 namespace ns { int local; void both(); }
1334 void f() { ::ns::^ }
1335 )cpp",
1336 {func("ns::both"), cls("ns::Index")});
1337 // We get results from both index and sema, with no duplicates.
Sam McCalle746a2b2018-07-02 11:13:16 +00001338 EXPECT_THAT(
1339 Results.Completions,
1340 UnorderedElementsAre(Scope("ns::"), Scope("ns::"), Scope("ns::")));
1341}
1342
1343TEST(CompletionTest, Render) {
1344 CodeCompletion C;
1345 C.Name = "x";
1346 C.Signature = "(bool) const";
1347 C.SnippetSuffix = "(${0:bool})";
1348 C.ReturnType = "int";
1349 C.RequiredQualifier = "Foo::";
1350 C.Scope = "ns::Foo::";
1351 C.Documentation = "This is x().";
Eric Liu83f63e42018-09-03 10:18:21 +00001352 C.Includes.emplace_back();
1353 auto &Include = C.Includes.back();
1354 Include.Header = "\"foo.h\"";
Sam McCalle746a2b2018-07-02 11:13:16 +00001355 C.Kind = CompletionItemKind::Method;
1356 C.Score.Total = 1.0;
Sam McCall4e5742a2018-07-06 11:50:49 +00001357 C.Origin = SymbolOrigin::AST | SymbolOrigin::Static;
Sam McCalle746a2b2018-07-02 11:13:16 +00001358
1359 CodeCompleteOptions Opts;
1360 Opts.IncludeIndicator.Insert = "^";
1361 Opts.IncludeIndicator.NoInsert = "";
1362 Opts.EnableSnippets = false;
1363
1364 auto R = C.render(Opts);
1365 EXPECT_EQ(R.label, "Foo::x(bool) const");
1366 EXPECT_EQ(R.insertText, "Foo::x");
1367 EXPECT_EQ(R.insertTextFormat, InsertTextFormat::PlainText);
1368 EXPECT_EQ(R.filterText, "x");
1369 EXPECT_EQ(R.detail, "int\n\"foo.h\"");
1370 EXPECT_EQ(R.documentation, "This is x().");
1371 EXPECT_THAT(R.additionalTextEdits, IsEmpty());
Sam McCalle746a2b2018-07-02 11:13:16 +00001372 EXPECT_EQ(R.sortText, sortText(1.0, "x"));
1373
1374 Opts.EnableSnippets = true;
1375 R = C.render(Opts);
1376 EXPECT_EQ(R.insertText, "Foo::x(${0:bool})");
1377 EXPECT_EQ(R.insertTextFormat, InsertTextFormat::Snippet);
1378
Eric Liu83f63e42018-09-03 10:18:21 +00001379 Include.Insertion.emplace();
Sam McCalle746a2b2018-07-02 11:13:16 +00001380 R = C.render(Opts);
1381 EXPECT_EQ(R.label, "^Foo::x(bool) const");
1382 EXPECT_THAT(R.additionalTextEdits, Not(IsEmpty()));
1383
Sam McCall2161ec72018-07-05 06:20:41 +00001384 Opts.ShowOrigins = true;
1385 R = C.render(Opts);
1386 EXPECT_EQ(R.label, "^[AS]Foo::x(bool) const");
1387
Sam McCalle746a2b2018-07-02 11:13:16 +00001388 C.BundleSize = 2;
1389 R = C.render(Opts);
1390 EXPECT_EQ(R.detail, "[2 overloads]\n\"foo.h\"");
Eric Liu7ad16962018-06-22 10:46:59 +00001391}
Ilya Biryukov981a35d2018-05-28 12:11:37 +00001392
Eric Liu485074f2018-07-11 13:15:31 +00001393TEST(CompletionTest, IgnoreRecoveryResults) {
1394 auto Results = completions(
1395 R"cpp(
1396 namespace ns { int NotRecovered() { return 0; } }
1397 void f() {
1398 // Sema enters recovery mode first and then normal mode.
1399 if (auto x = ns::NotRecover^)
1400 }
1401 )cpp");
1402 EXPECT_THAT(Results.Completions, UnorderedElementsAre(Named("NotRecovered")));
1403}
1404
Eric Liuf433c2d2018-07-18 15:31:14 +00001405TEST(CompletionTest, ScopeOfClassFieldInConstructorInitializer) {
1406 auto Results = completions(
1407 R"cpp(
1408 namespace ns {
1409 class X { public: X(); int x_; };
1410 X::X() : x_^(0) {}
1411 }
1412 )cpp");
1413 EXPECT_THAT(Results.Completions,
1414 UnorderedElementsAre(AllOf(Scope("ns::X::"), Named("x_"))));
1415}
1416
Eric Liu5d2a8072018-07-23 10:56:37 +00001417TEST(CompletionTest, CodeCompletionContext) {
1418 auto Results = completions(
1419 R"cpp(
1420 namespace ns {
1421 class X { public: X(); int x_; };
1422 void f() {
1423 X x;
1424 x.^;
1425 }
1426 }
1427 )cpp");
1428
1429 EXPECT_THAT(Results.Context, CodeCompletionContext::CCC_DotMemberAccess);
1430}
1431
Kadir Cetinkaya2f84d912018-08-08 08:59:29 +00001432TEST(CompletionTest, FixItForArrowToDot) {
1433 MockFSProvider FS;
1434 MockCompilationDatabase CDB;
1435 IgnoreDiagnostics DiagConsumer;
1436 ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
1437
1438 CodeCompleteOptions Opts;
1439 Opts.IncludeFixIts = true;
1440 Annotations TestCode(
1441 R"cpp(
1442 class Auxilary {
1443 public:
1444 void AuxFunction();
1445 };
1446 class ClassWithPtr {
1447 public:
1448 void MemberFunction();
1449 Auxilary* operator->() const;
1450 Auxilary* Aux;
1451 };
1452 void f() {
1453 ClassWithPtr x;
1454 x[[->]]^;
1455 }
1456 )cpp");
1457 auto Results =
1458 completions(Server, TestCode.code(), TestCode.point(), {}, Opts);
1459 EXPECT_EQ(Results.Completions.size(), 3u);
1460
1461 TextEdit ReplacementEdit;
1462 ReplacementEdit.range = TestCode.range();
1463 ReplacementEdit.newText = ".";
1464 for (const auto &C : Results.Completions) {
1465 EXPECT_TRUE(C.FixIts.size() == 1u || C.Name == "AuxFunction");
Haojian Wu1793bc92018-08-10 08:34:16 +00001466 if (!C.FixIts.empty()) {
Kadir Cetinkaya2f84d912018-08-08 08:59:29 +00001467 EXPECT_THAT(C.FixIts, ElementsAre(ReplacementEdit));
Haojian Wu1793bc92018-08-10 08:34:16 +00001468 }
Kadir Cetinkaya2f84d912018-08-08 08:59:29 +00001469 }
1470}
1471
1472TEST(CompletionTest, FixItForDotToArrow) {
1473 MockFSProvider FS;
1474 MockCompilationDatabase CDB;
1475 IgnoreDiagnostics DiagConsumer;
1476 ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
1477
1478 CodeCompleteOptions Opts;
1479 Opts.IncludeFixIts = true;
1480 Annotations TestCode(
1481 R"cpp(
1482 class Auxilary {
1483 public:
1484 void AuxFunction();
1485 };
1486 class ClassWithPtr {
1487 public:
1488 void MemberFunction();
1489 Auxilary* operator->() const;
1490 Auxilary* Aux;
1491 };
1492 void f() {
1493 ClassWithPtr x;
1494 x[[.]]^;
1495 }
1496 )cpp");
1497 auto Results =
1498 completions(Server, TestCode.code(), TestCode.point(), {}, Opts);
1499 EXPECT_EQ(Results.Completions.size(), 3u);
1500
1501 TextEdit ReplacementEdit;
1502 ReplacementEdit.range = TestCode.range();
1503 ReplacementEdit.newText = "->";
1504 for (const auto &C : Results.Completions) {
1505 EXPECT_TRUE(C.FixIts.empty() || C.Name == "AuxFunction");
1506 if (!C.FixIts.empty()) {
1507 EXPECT_THAT(C.FixIts, ElementsAre(ReplacementEdit));
1508 }
1509 }
1510}
1511
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +00001512TEST(CompletionTest, RenderWithFixItMerged) {
1513 TextEdit FixIt;
1514 FixIt.range.end.character = 5;
1515 FixIt.newText = "->";
1516
1517 CodeCompletion C;
1518 C.Name = "x";
1519 C.RequiredQualifier = "Foo::";
1520 C.FixIts = {FixIt};
1521 C.CompletionTokenRange.start.character = 5;
1522
1523 CodeCompleteOptions Opts;
1524 Opts.IncludeFixIts = true;
1525
1526 auto R = C.render(Opts);
1527 EXPECT_TRUE(R.textEdit);
1528 EXPECT_EQ(R.textEdit->newText, "->Foo::x");
1529 EXPECT_TRUE(R.additionalTextEdits.empty());
1530}
1531
1532TEST(CompletionTest, RenderWithFixItNonMerged) {
1533 TextEdit FixIt;
1534 FixIt.range.end.character = 4;
1535 FixIt.newText = "->";
1536
1537 CodeCompletion C;
1538 C.Name = "x";
1539 C.RequiredQualifier = "Foo::";
1540 C.FixIts = {FixIt};
1541 C.CompletionTokenRange.start.character = 5;
1542
1543 CodeCompleteOptions Opts;
1544 Opts.IncludeFixIts = true;
1545
1546 auto R = C.render(Opts);
1547 EXPECT_TRUE(R.textEdit);
1548 EXPECT_EQ(R.textEdit->newText, "Foo::x");
1549 EXPECT_THAT(R.additionalTextEdits, UnorderedElementsAre(FixIt));
1550}
1551
1552TEST(CompletionTest, CompletionTokenRange) {
1553 MockFSProvider FS;
1554 MockCompilationDatabase CDB;
1555 IgnoreDiagnostics DiagConsumer;
1556 ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
1557
1558 constexpr const char *TestCodes[] = {
1559 R"cpp(
1560 class Auxilary {
1561 public:
1562 void AuxFunction();
1563 };
1564 void f() {
1565 Auxilary x;
1566 x.[[Aux]]^;
1567 }
1568 )cpp",
1569 R"cpp(
1570 class Auxilary {
1571 public:
1572 void AuxFunction();
1573 };
1574 void f() {
1575 Auxilary x;
1576 x.[[]]^;
1577 }
1578 )cpp"};
1579 for (const auto &Text : TestCodes) {
1580 Annotations TestCode(Text);
1581 auto Results = completions(Server, TestCode.code(), TestCode.point());
1582
1583 EXPECT_EQ(Results.Completions.size(), 1u);
1584 EXPECT_THAT(Results.Completions.front().CompletionTokenRange, TestCode.range());
1585 }
1586}
1587
Kadir Cetinkayae486e372018-08-13 08:40:05 +00001588TEST(SignatureHelpTest, OverloadsOrdering) {
1589 const auto Results = signatures(R"cpp(
1590 void foo(int x);
1591 void foo(int x, float y);
1592 void foo(float x, int y);
1593 void foo(float x, float y);
1594 void foo(int x, int y = 0);
1595 int main() { foo(^); }
1596 )cpp");
1597 EXPECT_THAT(
1598 Results.signatures,
1599 ElementsAre(
1600 Sig("foo(int x) -> void", {"int x"}),
1601 Sig("foo(int x, int y = 0) -> void", {"int x", "int y = 0"}),
1602 Sig("foo(float x, int y) -> void", {"float x", "int y"}),
1603 Sig("foo(int x, float y) -> void", {"int x", "float y"}),
1604 Sig("foo(float x, float y) -> void", {"float x", "float y"})));
1605 // We always prefer the first signature.
1606 EXPECT_EQ(0, Results.activeSignature);
1607 EXPECT_EQ(0, Results.activeParameter);
1608}
1609
Ilya Biryukov8fd44bb2018-08-14 09:36:32 +00001610TEST(SignatureHelpTest, InstantiatedSignatures) {
Simon Pilgrim83552ee2018-08-16 11:41:19 +00001611 StringRef Sig0 = R"cpp(
Ilya Biryukov8fd44bb2018-08-14 09:36:32 +00001612 template <class T>
1613 void foo(T, T, T);
1614
1615 int main() {
1616 foo<int>(^);
1617 }
Simon Pilgrim83552ee2018-08-16 11:41:19 +00001618 )cpp";
1619
1620 EXPECT_THAT(signatures(Sig0).signatures,
Ilya Biryukov8fd44bb2018-08-14 09:36:32 +00001621 ElementsAre(Sig("foo(T, T, T) -> void", {"T", "T", "T"})));
1622
Simon Pilgrim83552ee2018-08-16 11:41:19 +00001623 StringRef Sig1 = R"cpp(
Ilya Biryukov8fd44bb2018-08-14 09:36:32 +00001624 template <class T>
1625 void foo(T, T, T);
1626
1627 int main() {
1628 foo(10, ^);
Simon Pilgrim83552ee2018-08-16 11:41:19 +00001629 })cpp";
1630
1631 EXPECT_THAT(signatures(Sig1).signatures,
Ilya Biryukov8fd44bb2018-08-14 09:36:32 +00001632 ElementsAre(Sig("foo(T, T, T) -> void", {"T", "T", "T"})));
1633
Simon Pilgrim83552ee2018-08-16 11:41:19 +00001634 StringRef Sig2 = R"cpp(
Ilya Biryukov8fd44bb2018-08-14 09:36:32 +00001635 template <class ...T>
1636 void foo(T...);
1637
1638 int main() {
1639 foo<int>(^);
1640 }
Simon Pilgrim83552ee2018-08-16 11:41:19 +00001641 )cpp";
1642
1643 EXPECT_THAT(signatures(Sig2).signatures,
Ilya Biryukov8fd44bb2018-08-14 09:36:32 +00001644 ElementsAre(Sig("foo(T...) -> void", {"T..."})));
1645
1646 // It is debatable whether we should substitute the outer template parameter
1647 // ('T') in that case. Currently we don't substitute it in signature help, but
1648 // do substitute in code complete.
1649 // FIXME: make code complete and signature help consistent, figure out which
1650 // way is better.
Simon Pilgrim83552ee2018-08-16 11:41:19 +00001651 StringRef Sig3 = R"cpp(
Ilya Biryukov8fd44bb2018-08-14 09:36:32 +00001652 template <class T>
1653 struct X {
1654 template <class U>
1655 void foo(T, U);
1656 };
1657
1658 int main() {
1659 X<int>().foo<double>(^)
1660 }
Simon Pilgrim83552ee2018-08-16 11:41:19 +00001661 )cpp";
1662
1663 EXPECT_THAT(signatures(Sig3).signatures,
Ilya Biryukov8fd44bb2018-08-14 09:36:32 +00001664 ElementsAre(Sig("foo(T, U) -> void", {"T", "U"})));
1665}
1666
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +00001667TEST(SignatureHelpTest, IndexDocumentation) {
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +00001668 Symbol Foo0 = sym("foo", index::SymbolKind::Function, "@F@\\0#");
Sam McCall2e5700f2018-08-31 13:55:01 +00001669 Foo0.Documentation = "Doc from the index";
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +00001670 Symbol Foo1 = sym("foo", index::SymbolKind::Function, "@F@\\0#I#");
Sam McCall2e5700f2018-08-31 13:55:01 +00001671 Foo1.Documentation = "Doc from the index";
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +00001672 Symbol Foo2 = sym("foo", index::SymbolKind::Function, "@F@\\0#I#I#");
1673
Simon Pilgrim24d34922018-08-17 10:40:05 +00001674 StringRef Sig0 = R"cpp(
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +00001675 int foo();
1676 int foo(double);
1677
1678 void test() {
1679 foo(^);
1680 }
Simon Pilgrim24d34922018-08-17 10:40:05 +00001681 )cpp";
1682
1683 EXPECT_THAT(
1684 signatures(Sig0, {Foo0}).signatures,
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +00001685 ElementsAre(AllOf(Sig("foo() -> int", {}), SigDoc("Doc from the index")),
1686 AllOf(Sig("foo(double) -> int", {"double"}), SigDoc(""))));
1687
Simon Pilgrim24d34922018-08-17 10:40:05 +00001688 StringRef Sig1 = R"cpp(
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +00001689 int foo();
1690 // Overriden doc from sema
1691 int foo(int);
1692 // Doc from sema
1693 int foo(int, int);
1694
1695 void test() {
1696 foo(^);
1697 }
Simon Pilgrim24d34922018-08-17 10:40:05 +00001698 )cpp";
1699
1700 EXPECT_THAT(
1701 signatures(Sig1, {Foo0, Foo1, Foo2}).signatures,
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +00001702 ElementsAre(AllOf(Sig("foo() -> int", {}), SigDoc("Doc from the index")),
1703 AllOf(Sig("foo(int) -> int", {"int"}),
1704 SigDoc("Overriden doc from sema")),
1705 AllOf(Sig("foo(int, int) -> int", {"int", "int"}),
1706 SigDoc("Doc from sema"))));
1707}
1708
Kadir Cetinkaya516fcda2018-08-23 12:19:39 +00001709TEST(CompletionTest, CompletionFunctionArgsDisabled) {
Kadir Cetinkaya6c9f15c2018-08-17 15:42:54 +00001710 CodeCompleteOptions Opts;
Kadir Cetinkaya6c9f15c2018-08-17 15:42:54 +00001711 Opts.EnableSnippets = true;
1712 Opts.EnableFunctionArgSnippets = false;
Kadir Cetinkaya516fcda2018-08-23 12:19:39 +00001713 const std::string Header =
1714 R"cpp(
1715 void xfoo();
1716 void xfoo(int x, int y);
1717 void xbar();
1718 void f() {
1719 )cpp";
Kadir Cetinkaya6c9f15c2018-08-17 15:42:54 +00001720 {
Kadir Cetinkaya516fcda2018-08-23 12:19:39 +00001721 auto Results = completions(Header + "\nxfo^", {}, Opts);
1722 EXPECT_THAT(
1723 Results.Completions,
1724 UnorderedElementsAre(AllOf(Named("xfoo"), SnippetSuffix("()")),
1725 AllOf(Named("xfoo"), SnippetSuffix("($0)"))));
Kadir Cetinkaya6c9f15c2018-08-17 15:42:54 +00001726 }
Kadir Cetinkaya6c9f15c2018-08-17 15:42:54 +00001727 {
Kadir Cetinkaya516fcda2018-08-23 12:19:39 +00001728 auto Results = completions(Header + "\nxba^", {}, Opts);
1729 EXPECT_THAT(Results.Completions, UnorderedElementsAre(AllOf(
1730 Named("xbar"), SnippetSuffix("()"))));
Kadir Cetinkaya6c9f15c2018-08-17 15:42:54 +00001731 }
Kadir Cetinkaya6c9f15c2018-08-17 15:42:54 +00001732 {
Kadir Cetinkaya516fcda2018-08-23 12:19:39 +00001733 Opts.BundleOverloads = true;
1734 auto Results = completions(Header + "\nxfo^", {}, Opts);
1735 EXPECT_THAT(
1736 Results.Completions,
1737 UnorderedElementsAre(AllOf(Named("xfoo"), SnippetSuffix("($0)"))));
Kadir Cetinkaya6c9f15c2018-08-17 15:42:54 +00001738 }
1739}
1740
Kadir Cetinkayaf8b85a32018-08-23 13:14:50 +00001741TEST(CompletionTest, SuggestOverrides) {
1742 constexpr const char *const Text(R"cpp(
1743 class A {
1744 public:
1745 virtual void vfunc(bool param);
1746 virtual void vfunc(bool param, int p);
1747 void func(bool param);
1748 };
1749 class B : public A {
1750 virtual void ttt(bool param) const;
1751 void vfunc(bool param, int p) override;
1752 };
1753 class C : public B {
1754 public:
1755 void vfunc(bool param) override;
1756 ^
1757 };
1758 )cpp");
1759 const auto Results = completions(Text);
1760 EXPECT_THAT(Results.Completions,
1761 AllOf(Contains(Labeled("void vfunc(bool param, int p) override")),
1762 Contains(Labeled("void ttt(bool param) const override")),
1763 Not(Contains(Labeled("void vfunc(bool param) override")))));
1764}
1765
Ilya Biryukov5a79d1e2018-09-03 15:25:27 +00001766TEST(CompletionTest, OverridesNonIdentName) {
1767 // Check the completions call does not crash.
1768 completions(R"cpp(
1769 struct Base {
1770 virtual ~Base() = 0;
1771 virtual operator int() = 0;
1772 virtual Base& operator+(Base&) = 0;
1773 };
1774
1775 struct Derived : Base {
1776 ^
1777 };
1778 )cpp");
1779}
1780
Eric Liu25d74e92018-08-24 11:23:56 +00001781TEST(SpeculateCompletionFilter, Filters) {
1782 Annotations F(R"cpp($bof^
1783 $bol^
1784 ab$ab^
1785 x.ab$dot^
1786 x.$dotempty^
1787 x::ab$scoped^
1788 x::$scopedempty^
1789
1790 )cpp");
1791 auto speculate = [&](StringRef PointName) {
1792 auto Filter = speculateCompletionFilter(F.code(), F.point(PointName));
1793 assert(Filter);
1794 return *Filter;
1795 };
1796 EXPECT_EQ(speculate("bof"), "");
1797 EXPECT_EQ(speculate("bol"), "");
1798 EXPECT_EQ(speculate("ab"), "ab");
1799 EXPECT_EQ(speculate("dot"), "ab");
1800 EXPECT_EQ(speculate("dotempty"), "");
1801 EXPECT_EQ(speculate("scoped"), "ab");
1802 EXPECT_EQ(speculate("scopedempty"), "");
1803}
1804
1805TEST(CompletionTest, EnableSpeculativeIndexRequest) {
1806 MockFSProvider FS;
1807 MockCompilationDatabase CDB;
1808 IgnoreDiagnostics DiagConsumer;
1809 ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
1810
1811 auto File = testPath("foo.cpp");
1812 Annotations Test(R"cpp(
1813 namespace ns1 { int abc; }
1814 namespace ns2 { int abc; }
1815 void f() { ns1::ab$1^; ns1::ab$2^; }
1816 void f() { ns2::ab$3^; }
1817 )cpp");
1818 runAddDocument(Server, File, Test.code());
1819 clangd::CodeCompleteOptions Opts = {};
1820
1821 IndexRequestCollector Requests;
1822 Opts.Index = &Requests;
1823 Opts.SpeculativeIndexRequest = true;
1824
1825 auto CompleteAtPoint = [&](StringRef P) {
1826 cantFail(runCodeComplete(Server, File, Test.point(P), Opts));
1827 // Sleep for a while to make sure asynchronous call (if applicable) is also
1828 // triggered before callback is invoked.
1829 std::this_thread::sleep_for(std::chrono::milliseconds(100));
1830 };
1831
1832 CompleteAtPoint("1");
1833 auto Reqs1 = Requests.consumeRequests();
1834 ASSERT_EQ(Reqs1.size(), 1u);
1835 EXPECT_THAT(Reqs1[0].Scopes, UnorderedElementsAre("ns1::"));
1836
1837 CompleteAtPoint("2");
1838 auto Reqs2 = Requests.consumeRequests();
1839 // Speculation succeeded. Used speculative index result.
1840 ASSERT_EQ(Reqs2.size(), 1u);
1841 EXPECT_EQ(Reqs2[0], Reqs1[0]);
1842
1843 CompleteAtPoint("3");
1844 // Speculation failed. Sent speculative index request and the new index
1845 // request after sema.
1846 auto Reqs3 = Requests.consumeRequests();
1847 ASSERT_EQ(Reqs3.size(), 2u);
1848}
1849
Eric Liu83f63e42018-09-03 10:18:21 +00001850TEST(CompletionTest, InsertTheMostPopularHeader) {
1851 std::string DeclFile = URI::createFile(testPath("foo")).toString();
1852 Symbol sym = func("Func");
1853 sym.CanonicalDeclaration.FileURI = DeclFile;
1854 sym.IncludeHeaders.emplace_back("\"foo.h\"", 2);
1855 sym.IncludeHeaders.emplace_back("\"bar.h\"", 1000);
1856
1857 auto Results = completions("Fun^", {sym}).Completions;
1858 assert(!Results.empty());
1859 EXPECT_THAT(Results[0], AllOf(Named("Func"), InsertInclude("\"bar.h\"")));
1860 EXPECT_EQ(Results[0].Includes.size(), 2u);
1861}
1862
1863TEST(CompletionTest, NoInsertIncludeIfOnePresent) {
1864 MockFSProvider FS;
1865 MockCompilationDatabase CDB;
1866
1867 std::string FooHeader = testPath("foo.h");
1868 FS.Files[FooHeader] = "";
1869
1870 IgnoreDiagnostics DiagConsumer;
1871 ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
1872
1873 std::string DeclFile = URI::createFile(testPath("foo")).toString();
1874 Symbol sym = func("Func");
1875 sym.CanonicalDeclaration.FileURI = DeclFile;
1876 sym.IncludeHeaders.emplace_back("\"foo.h\"", 2);
1877 sym.IncludeHeaders.emplace_back("\"bar.h\"", 1000);
1878
1879 EXPECT_THAT(
1880 completions(Server, "#include \"foo.h\"\nFun^", {sym}).Completions,
1881 UnorderedElementsAre(
1882 AllOf(Named("Func"), HasInclude("\"foo.h\""), Not(InsertInclude()))));
1883}
1884
Eric Liud25f1212018-09-06 09:59:37 +00001885TEST(CompletionTest, MergeMacrosFromIndexAndSema) {
1886 Symbol Sym;
1887 Sym.Name = "Clangd_Macro_Test";
1888 Sym.ID = SymbolID("c:foo.cpp@8@macro@Clangd_Macro_Test");
1889 Sym.SymInfo.Kind = index::SymbolKind::Macro;
1890 Sym.IsIndexedForCodeCompletion = true;
1891 EXPECT_THAT(completions("#define Clangd_Macro_Test\nClangd_Macro_T^", {Sym})
1892 .Completions,
1893 UnorderedElementsAre(Named("Clangd_Macro_Test")));
1894}
1895
Sam McCall9aad25f2017-12-05 07:20:26 +00001896} // namespace
1897} // namespace clangd
1898} // namespace clang