| 1 |
//===-- CodeCompleteTests.cpp -----------------------------------*- C++ -*-===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "ASTSignals.h" |
| 10 |
#include "Annotations.h" |
| 11 |
#include "ClangdServer.h" |
| 12 |
#include "CodeComplete.h" |
| 13 |
#include "Compiler.h" |
| 14 |
#include "Feature.h" |
| 15 |
#include "Matchers.h" |
| 16 |
#include "Protocol.h" |
| 17 |
#include "Quality.h" |
| 18 |
#include "SourceCode.h" |
| 19 |
#include "SyncAPI.h" |
| 20 |
#include "TestFS.h" |
| 21 |
#include "TestIndex.h" |
| 22 |
#include "TestTU.h" |
| 23 |
#include "index/Index.h" |
| 24 |
#include "index/MemIndex.h" |
| 25 |
#include "index/SymbolOrigin.h" |
| 26 |
#include "support/Threading.h" |
| 27 |
#include "clang/Sema/CodeCompleteConsumer.h" |
| 28 |
#include "clang/Tooling/CompilationDatabase.h" |
| 29 |
#include "llvm/ADT/StringRef.h" |
| 30 |
#include "llvm/Support/Error.h" |
| 31 |
#include "llvm/Support/Path.h" |
| 32 |
#include "llvm/Testing/Annotations/Annotations.h" |
| 33 |
#include "llvm/Testing/Support/Error.h" |
| 34 |
#include "llvm/Testing/Support/SupportHelpers.h" |
| 35 |
#include "gmock/gmock.h" |
| 36 |
#include "gtest/gtest.h" |
| 37 |
#include <condition_variable> |
| 38 |
#include <functional> |
| 39 |
#include <mutex> |
| 40 |
#include <vector> |
| 41 |
|
| 42 |
namespace clang { |
| 43 |
namespace clangd { |
| 44 |
|
| 45 |
namespace { |
| 46 |
using ::llvm::Failed; |
| 47 |
using ::testing::AllOf; |
| 48 |
using ::testing::Contains; |
| 49 |
using ::testing::ElementsAre; |
| 50 |
using ::testing::Field; |
| 51 |
using ::testing::HasSubstr; |
| 52 |
using ::testing::IsEmpty; |
| 53 |
using ::testing::Not; |
| 54 |
using ::testing::UnorderedElementsAre; |
| 55 |
using ContextKind = CodeCompletionContext::Kind; |
| 56 |
|
| 57 |
// GMock helpers for matching completion items. |
| 58 |
MATCHER_P(named, Name, "") { return arg.Name == Name; } |
| 59 |
MATCHER_P(mainFileRefs, Refs, "") { return arg.MainFileRefs == Refs; } |
| 60 |
MATCHER_P(scopeRefs, Refs, "") { return arg.ScopeRefsInFile == Refs; } |
| 61 |
MATCHER_P(nameStartsWith, Prefix, "") { |
| 62 |
return llvm::StringRef(arg.Name).startswith(Prefix); |
| 63 |
} |
| 64 |
MATCHER_P(filterText, F, "") { return arg.FilterText == F; } |
| 65 |
MATCHER_P(scope, S, "") { return arg.Scope == S; } |
| 66 |
MATCHER_P(qualifier, Q, "") { return arg.RequiredQualifier == Q; } |
| 67 |
MATCHER_P(labeled, Label, "") { |
| 68 |
return arg.RequiredQualifier + arg.Name + arg.Signature == Label; |
| 69 |
} |
| 70 |
MATCHER_P(sigHelpLabeled, Label, "") { return arg.label == Label; } |
| 71 |
MATCHER_P(kind, K, "") { return arg.Kind == K; } |
| 72 |
MATCHER_P(doc, D, "") { |
| 73 |
return arg.Documentation && arg.Documentation->asPlainText() == D; |
| 74 |
} |
| 75 |
MATCHER_P(returnType, D, "") { return arg.ReturnType == D; } |
| 76 |
MATCHER_P(hasInclude, IncludeHeader, "") { |
| 77 |
return !arg.Includes.empty() && arg.Includes[0].Header == IncludeHeader; |
| 78 |
} |
| 79 |
MATCHER_P(insertInclude, IncludeHeader, "") { |
| 80 |
return !arg.Includes.empty() && arg.Includes[0].Header == IncludeHeader && |
| 81 |
bool(arg.Includes[0].Insertion); |
| 82 |
} |
| 83 |
MATCHER_P(insertIncludeText, InsertedText, "") { |
| 84 |
return !arg.Includes.empty() && arg.Includes[0].Insertion && |
| 85 |
arg.Includes[0].Insertion->newText == InsertedText; |
| 86 |
} |
| 87 |
MATCHER(insertInclude, "") { |
| 88 |
return !arg.Includes.empty() && bool(arg.Includes[0].Insertion); |
| 89 |
} |
| 90 |
MATCHER_P(snippetSuffix, Text, "") { return arg.SnippetSuffix == Text; } |
| 91 |
MATCHER_P(origin, OriginSet, "") { return arg.Origin == OriginSet; } |
| 92 |
MATCHER_P(signature, S, "") { return arg.Signature == S; } |
| 93 |
MATCHER_P(replacesRange, Range, "") { |
| 94 |
return arg.CompletionTokenRange == Range; |
| 95 |
} |
| 96 |
|
| 97 |
// Shorthand for Contains(named(Name)). |
| 98 |
Matcher<const std::vector<CodeCompletion> &> has(std::string Name) { |
| 99 |
return Contains(named(std::move(Name))); |
| 100 |
} |
| 101 |
Matcher<const std::vector<CodeCompletion> &> has(std::string Name, |
| 102 |
CompletionItemKind K) { |
| 103 |
return Contains(AllOf(named(std::move(Name)), kind(K))); |
| 104 |
} |
| 105 |
MATCHER(isDocumented, "") { return arg.Documentation.has_value(); } |
| 106 |
MATCHER(deprecated, "") { return arg.Deprecated; } |
| 107 |
|
| 108 |
std::unique_ptr<SymbolIndex> memIndex(std::vector<Symbol> Symbols) { |
| 109 |
SymbolSlab::Builder Slab; |
| 110 |
for (const auto &Sym : Symbols) |
| 111 |
Slab.insert(Sym); |
| 112 |
return MemIndex::build(std::move(Slab).build(), RefSlab(), RelationSlab()); |
| 113 |
} |
| 114 |
|
| 115 |
// Runs code completion. |
| 116 |
// If IndexSymbols is non-empty, an index will be built and passed to opts. |
| 117 |
CodeCompleteResult completions(const TestTU &TU, Position Point, |
| 118 |
std::vector<Symbol> IndexSymbols = {}, |
| 119 |
clangd::CodeCompleteOptions Opts = {}) { |
| 120 |
std::unique_ptr<SymbolIndex> OverrideIndex; |
| 121 |
if (!IndexSymbols.empty()) { |
| 122 |
assert(!Opts.Index && "both Index and IndexSymbols given!"); |
| 123 |
OverrideIndex = memIndex(std::move(IndexSymbols)); |
| 124 |
Opts.Index = OverrideIndex.get(); |
| 125 |
} |
| 126 |
|
| 127 |
MockFS FS; |
| 128 |
auto Inputs = TU.inputs(FS); |
| 129 |
IgnoreDiagnostics Diags; |
| 130 |
auto CI = buildCompilerInvocation(Inputs, Diags); |
| 131 |
if (!CI) { |
| 132 |
ADD_FAILURE() << "Couldn't build CompilerInvocation"; |
| 133 |
return {}; |
| 134 |
} |
| 135 |
auto Preamble = buildPreamble(testPath(TU.Filename), *CI, Inputs, |
| 136 |
/*InMemory=*/true, /*Callback=*/nullptr); |
| 137 |
return codeComplete(testPath(TU.Filename), Point, Preamble.get(), Inputs, |
| 138 |
Opts); |
| 139 |
} |
| 140 |
|
| 141 |
// Runs code completion. |
| 142 |
CodeCompleteResult completions(llvm::StringRef Text, |
| 143 |
std::vector<Symbol> IndexSymbols = {}, |
| 144 |
clangd::CodeCompleteOptions Opts = {}, |
| 145 |
PathRef FilePath = "foo.cpp") { |
| 146 |
Annotations Test(Text); |
| 147 |
auto TU = TestTU::withCode(Test.code()); |
| 148 |
// To make sure our tests for completiopns inside templates work on Windows. |
| 149 |
TU.Filename = FilePath.str(); |
| 150 |
return completions(TU, Test.point(), std::move(IndexSymbols), |
| 151 |
std::move(Opts)); |
| 152 |
} |
| 153 |
|
| 154 |
// Runs code completion without the clang parser. |
| 155 |
CodeCompleteResult completionsNoCompile(llvm::StringRef Text, |
| 156 |
std::vector<Symbol> IndexSymbols = {}, |
| 157 |
clangd::CodeCompleteOptions Opts = {}, |
| 158 |
PathRef FilePath = "foo.cpp") { |
| 159 |
std::unique_ptr<SymbolIndex> OverrideIndex; |
| 160 |
if (!IndexSymbols.empty()) { |
| 161 |
assert(!Opts.Index && "both Index and IndexSymbols given!"); |
| 162 |
OverrideIndex = memIndex(std::move(IndexSymbols)); |
| 163 |
Opts.Index = OverrideIndex.get(); |
| 164 |
} |
| 165 |
|
| 166 |
MockFS FS; |
| 167 |
Annotations Test(Text); |
| 168 |
ParseInputs ParseInput{tooling::CompileCommand(), &FS, Test.code().str()}; |
| 169 |
return codeComplete(FilePath, Test.point(), /*Preamble=*/nullptr, ParseInput, |
| 170 |
Opts); |
| 171 |
} |
| 172 |
|
| 173 |
Symbol withReferences(int N, Symbol S) { |
| 174 |
S.References = N; |
| 175 |
return S; |
| 176 |
} |
| 177 |
|
| 178 |
#if CLANGD_DECISION_FOREST |
| 179 |
TEST(DecisionForestRankingModel, NameMatchSanityTest) { |
| 180 |
clangd::CodeCompleteOptions Opts; |
| 181 |
Opts.RankingModel = CodeCompleteOptions::DecisionForest; |
| 182 |
auto Results = completions( |
| 183 |
R"cpp( |
| 184 |
struct MemberAccess { |
| 185 |
int ABG(); |
| 186 |
int AlphaBetaGamma(); |
| 187 |
}; |
| 188 |
int func() { MemberAccess().ABG^ } |
| 189 |
)cpp", |
| 190 |
/*IndexSymbols=*/{}, Opts); |
| 191 |
EXPECT_THAT(Results.Completions, |
| 192 |
ElementsAre(named("ABG"), named("AlphaBetaGamma"))); |
| 193 |
} |
| 194 |
|
| 195 |
TEST(DecisionForestRankingModel, ReferencesAffectRanking) { |
| 196 |
clangd::CodeCompleteOptions Opts; |
| 197 |
Opts.RankingModel = CodeCompleteOptions::DecisionForest; |
| 198 |
constexpr int NumReferences = 100000; |
| 199 |
EXPECT_THAT( |
| 200 |
completions("int main() { clang^ }", |
| 201 |
{ns("clangA"), withReferences(NumReferences, func("clangD"))}, |
| 202 |
Opts) |
| 203 |
.Completions, |
| 204 |
ElementsAre(named("clangD"), named("clangA"))); |
| 205 |
EXPECT_THAT( |
| 206 |
completions("int main() { clang^ }", |
| 207 |
{withReferences(NumReferences, ns("clangA")), func("clangD")}, |
| 208 |
Opts) |
| 209 |
.Completions, |
| 210 |
ElementsAre(named("clangA"), named("clangD"))); |
| 211 |
} |
| 212 |
#endif // CLANGD_DECISION_FOREST |
| 213 |
|
| 214 |
TEST(DecisionForestRankingModel, DecisionForestScorerCallbackTest) { |
| 215 |
clangd::CodeCompleteOptions Opts; |
| 216 |
constexpr float MagicNumber = 1234.5678f; |
| 217 |
Opts.RankingModel = CodeCompleteOptions::DecisionForest; |
| 218 |
Opts.DecisionForestScorer = [&](const SymbolQualitySignals &, |
| 219 |
const SymbolRelevanceSignals &, float Base) { |
| 220 |
DecisionForestScores Scores; |
| 221 |
Scores.Total = MagicNumber; |
| 222 |
Scores.ExcludingName = MagicNumber; |
| 223 |
return Scores; |
| 224 |
}; |
| 225 |
llvm::StringRef Code = "int func() { int xyz; xy^ }"; |
| 226 |
auto Results = completions(Code, |
| 227 |
/*IndexSymbols=*/{}, Opts); |
| 228 |
ASSERT_EQ(Results.Completions.size(), 1u); |
| 229 |
EXPECT_EQ(Results.Completions[0].Score.Total, MagicNumber); |
| 230 |
EXPECT_EQ(Results.Completions[0].Score.ExcludingName, MagicNumber); |
| 231 |
|
| 232 |
// Do not use DecisionForestScorer for heuristics model. |
| 233 |
Opts.RankingModel = CodeCompleteOptions::Heuristics; |
| 234 |
Results = completions(Code, |
| 235 |
/*IndexSymbols=*/{}, Opts); |
| 236 |
ASSERT_EQ(Results.Completions.size(), 1u); |
| 237 |
EXPECT_NE(Results.Completions[0].Score.Total, MagicNumber); |
| 238 |
EXPECT_NE(Results.Completions[0].Score.ExcludingName, MagicNumber); |
| 239 |
} |
| 240 |
|
| 241 |
TEST(CompletionTest, Limit) { |
| 242 |
clangd::CodeCompleteOptions Opts; |
| 243 |
Opts.Limit = 2; |
| 244 |
auto Results = completions(R"cpp( |
| 245 |
struct ClassWithMembers { |
| 246 |
int AAA(); |
| 247 |
int BBB(); |
| 248 |
int CCC(); |
| 249 |
}; |
| 250 |
|
| 251 |
int main() { ClassWithMembers().^ } |
| 252 |
)cpp", |
| 253 |
/*IndexSymbols=*/{}, Opts); |
| 254 |
|
| 255 |
EXPECT_TRUE(Results.HasMore); |
| 256 |
EXPECT_THAT(Results.Completions, ElementsAre(named("AAA"), named("BBB"))); |
| 257 |
} |
| 258 |
|
| 259 |
TEST(CompletionTest, Filter) { |
| 260 |
std::string Body = R"cpp( |
| 261 |
#define MotorCar |
| 262 |
int Car; |
| 263 |
struct S { |
| 264 |
int FooBar; |
| 265 |
int FooBaz; |
| 266 |
int Qux; |
| 267 |
}; |
| 268 |
)cpp"; |
| 269 |
|
| 270 |
// Only items matching the fuzzy query are returned. |
| 271 |
EXPECT_THAT(completions(Body + "int main() { S().Foba^ }").Completions, |
| 272 |
AllOf(has("FooBar"), has("FooBaz"), Not(has("Qux")))); |
| 273 |
|
| 274 |
// Macros require prefix match, either from index or AST. |
| 275 |
Symbol Sym = var("MotorCarIndex"); |
| 276 |
Sym.SymInfo.Kind = index::SymbolKind::Macro; |
| 277 |
EXPECT_THAT( |
| 278 |
completions(Body + "int main() { C^ }", {Sym}).Completions, |
| 279 |
AllOf(has("Car"), Not(has("MotorCar")), Not(has("MotorCarIndex")))); |
| 280 |
EXPECT_THAT(completions(Body + "int main() { M^ }", {Sym}).Completions, |
| 281 |
AllOf(has("MotorCar"), has("MotorCarIndex"))); |
| 282 |
} |
| 283 |
|
| 284 |
void testAfterDotCompletion(clangd::CodeCompleteOptions Opts) { |
| 285 |
auto Results = completions( |
| 286 |
R"cpp( |
| 287 |
int global_var; |
| 288 |
|
| 289 |
int global_func(); |
| 290 |
|
| 291 |
// Make sure this is not in preamble. |
| 292 |
#define MACRO X |
| 293 |
|
| 294 |
struct GlobalClass {}; |
| 295 |
|
| 296 |
struct ClassWithMembers { |
| 297 |
/// doc for method. |
| 298 |
int method(); |
| 299 |
|
| 300 |
int field; |
| 301 |
private: |
| 302 |
int private_field; |
| 303 |
}; |
| 304 |
|
| 305 |
int test() { |
| 306 |
struct LocalClass {}; |
| 307 |
|
| 308 |
/// doc for local_var. |
| 309 |
int local_var; |
| 310 |
|
| 311 |
ClassWithMembers().^ |
| 312 |
} |
| 313 |
)cpp", |
| 314 |
{cls("IndexClass"), var("index_var"), func("index_func")}, Opts); |
| 315 |
|
| 316 |
EXPECT_TRUE(Results.RanParser); |
| 317 |
// Class members. The only items that must be present in after-dot |
| 318 |
// completion. |
| 319 |
EXPECT_THAT(Results.Completions, |
| 320 |
AllOf(has("method"), has("field"), Not(has("ClassWithMembers")), |
| 321 |
Not(has("operator=")), Not(has("~ClassWithMembers")))); |
| 322 |
EXPECT_IFF(Opts.IncludeIneligibleResults, Results.Completions, |
| 323 |
has("private_field")); |
| 324 |
// Global items. |
| 325 |
EXPECT_THAT( |
| 326 |
Results.Completions, |
| 327 |
Not(AnyOf(has("global_var"), has("index_var"), has("global_func"), |
| 328 |
has("global_func()"), has("index_func"), has("GlobalClass"), |
| 329 |
has("IndexClass"), has("MACRO"), has("LocalClass")))); |
| 330 |
// There should be no code patterns (aka snippets) in after-dot |
| 331 |
// completion. At least there aren't any we're aware of. |
| 332 |
EXPECT_THAT(Results.Completions, |
| 333 |
Not(Contains(kind(CompletionItemKind::Snippet)))); |
| 334 |
// Check documentation. |
| 335 |
EXPECT_THAT(Results.Completions, Contains(isDocumented())); |
| 336 |
} |
| 337 |
|
| 338 |
void testGlobalScopeCompletion(clangd::CodeCompleteOptions Opts) { |
| 339 |
auto Results = completions( |
| 340 |
R"cpp( |
| 341 |
int global_var; |
| 342 |
int global_func(); |
| 343 |
|
| 344 |
// Make sure this is not in preamble. |
| 345 |
#define MACRO X |
| 346 |
|
| 347 |
struct GlobalClass {}; |
| 348 |
|
| 349 |
struct ClassWithMembers { |
| 350 |
/// doc for method. |
| 351 |
int method(); |
| 352 |
}; |
| 353 |
|
| 354 |
int test() { |
| 355 |
struct LocalClass {}; |
| 356 |
|
| 357 |
/// doc for local_var. |
| 358 |
int local_var; |
| 359 |
|
| 360 |
^ |
| 361 |
} |
| 362 |
)cpp", |
| 363 |
{cls("IndexClass"), var("index_var"), func("index_func")}, Opts); |
| 364 |
|
| 365 |
EXPECT_TRUE(Results.RanParser); |
| 366 |
// Class members. Should never be present in global completions. |
| 367 |
EXPECT_THAT(Results.Completions, |
| 368 |
Not(AnyOf(has("method"), has("method()"), has("field")))); |
| 369 |
// Global items. |
| 370 |
EXPECT_THAT(Results.Completions, |
| 371 |
AllOf(has("global_var"), has("index_var"), has("global_func"), |
| 372 |
has("index_func" /* our fake symbol doesn't include () */), |
| 373 |
has("GlobalClass"), has("IndexClass"))); |
| 374 |
// A macro. |
| 375 |
EXPECT_THAT(Results.Completions, has("MACRO")); |
| 376 |
// Local items. Must be present always. |
| 377 |
EXPECT_THAT(Results.Completions, |
| 378 |
AllOf(has("local_var"), has("LocalClass"), |
| 379 |
Contains(kind(CompletionItemKind::Snippet)))); |
| 380 |
// Check documentation. |
| 381 |
EXPECT_THAT(Results.Completions, Contains(isDocumented())); |
| 382 |
} |
| 383 |
|
| 384 |
TEST(CompletionTest, CompletionOptions) { |
| 385 |
auto Test = [&](const clangd::CodeCompleteOptions &Opts) { |
| 386 |
testAfterDotCompletion(Opts); |
| 387 |
testGlobalScopeCompletion(Opts); |
| 388 |
}; |
| 389 |
// We used to test every combination of options, but that got too slow (2^N). |
| 390 |
auto Flags = { |
| 391 |
&clangd::CodeCompleteOptions::IncludeIneligibleResults, |
| 392 |
}; |
| 393 |
// Test default options. |
| 394 |
Test({}); |
| 395 |
// Test with one flag flipped. |
| 396 |
for (auto &F : Flags) { |
| 397 |
clangd::CodeCompleteOptions O; |
| 398 |
O.*F ^= true; |
| 399 |
Test(O); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
TEST(CompletionTest, Accessible) { |
| 404 |
auto Internal = completions(R"cpp( |
| 405 |
class Foo { |
| 406 |
public: void pub(); |
| 407 |
protected: void prot(); |
| 408 |
private: void priv(); |
| 409 |
}; |
| 410 |
void Foo::pub() { this->^ } |
| 411 |
)cpp"); |
| 412 |
EXPECT_THAT(Internal.Completions, |
| 413 |
AllOf(has("priv"), has("prot"), has("pub"))); |
| 414 |
|
| 415 |
auto External = completions(R"cpp( |
| 416 |
class Foo { |
| 417 |
public: void pub(); |
| 418 |
protected: void prot(); |
| 419 |
private: void priv(); |
| 420 |
}; |
| 421 |
void test() { |
| 422 |
Foo F; |
| 423 |
F.^ |
| 424 |
} |
| 425 |
)cpp"); |
| 426 |
EXPECT_THAT(External.Completions, |
| 427 |
AllOf(has("pub"), Not(has("prot")), Not(has("priv")))); |
| 428 |
|
| 429 |
auto Results = completions(R"cpp( |
| 430 |
struct Foo { |
| 431 |
public: void pub(); |
| 432 |
protected: void prot(); |
| 433 |
private: void priv(); |
| 434 |
}; |
| 435 |
struct Bar : public Foo { |
| 436 |
private: using Foo::pub; |
| 437 |
}; |
| 438 |
void test() { |
| 439 |
Bar B; |
| 440 |
B.^ |
| 441 |
} |
| 442 |
)cpp"); |
| 443 |
EXPECT_THAT(Results.Completions, |
| 444 |
AllOf(Not(has("priv")), Not(has("prot")), Not(has("pub")))); |
| 445 |
} |
| 446 |
|
| 447 |
TEST(CompletionTest, Qualifiers) { |
| 448 |
auto Results = completions(R"cpp( |
| 449 |
class Foo { |
| 450 |
public: int foo() const; |
| 451 |
int bar() const; |
| 452 |
}; |
| 453 |
class Bar : public Foo { |
| 454 |
int foo() const; |
| 455 |
}; |
| 456 |
void test() { Bar().^ } |
| 457 |
)cpp"); |
| 458 |
EXPECT_THAT(Results.Completions, |
| 459 |
Contains(AllOf(qualifier(""), named("bar")))); |
| 460 |
// Hidden members are not shown. |
| 461 |
EXPECT_THAT(Results.Completions, |
| 462 |
Not(Contains(AllOf(qualifier("Foo::"), named("foo"))))); |
| 463 |
// Private members are not shown. |
| 464 |
EXPECT_THAT(Results.Completions, |
| 465 |
Not(Contains(AllOf(qualifier(""), named("foo"))))); |
| 466 |
} |
| 467 |
|
| 468 |
TEST(CompletionTest, InjectedTypename) { |
| 469 |
// These are suppressed when accessed as a member... |
| 470 |
EXPECT_THAT(completions("struct X{}; void foo(){ X().^ }").Completions, |
| 471 |
Not(has("X"))); |
| 472 |
EXPECT_THAT(completions("struct X{ void foo(){ this->^ } };").Completions, |
| 473 |
Not(has("X"))); |
| 474 |
// ...but accessible in other, more useful cases. |
| 475 |
EXPECT_THAT(completions("struct X{ void foo(){ ^ } };").Completions, |
| 476 |
has("X")); |
| 477 |
EXPECT_THAT( |
| 478 |
completions("struct Y{}; struct X:Y{ void foo(){ ^ } };").Completions, |
| 479 |
has("Y")); |
| 480 |
EXPECT_THAT( |
| 481 |
completions( |
| 482 |
"template<class> struct Y{}; struct X:Y<int>{ void foo(){ ^ } };") |
| 483 |
.Completions, |
| 484 |
has("Y")); |
| 485 |
// This case is marginal (`using X::X` is useful), we allow it for now. |
| 486 |
EXPECT_THAT(completions("struct X{}; void foo(){ X::^ }").Completions, |
| 487 |
has("X")); |
| 488 |
} |
| 489 |
|
| 490 |
TEST(CompletionTest, SkipInjectedWhenUnqualified) { |
| 491 |
EXPECT_THAT(completions("struct X { void f() { X^ }};").Completions, |
| 492 |
ElementsAre(named("X"), named("~X"))); |
| 493 |
} |
| 494 |
|
| 495 |
TEST(CompletionTest, Snippets) { |
| 496 |
clangd::CodeCompleteOptions Opts; |
| 497 |
auto Results = completions( |
| 498 |
R"cpp( |
| 499 |
struct fake { |
| 500 |
int a; |
| 501 |
int f(int i, const float f) const; |
| 502 |
}; |
| 503 |
int main() { |
| 504 |
fake f; |
| 505 |
f.^ |
| 506 |
} |
| 507 |
)cpp", |
| 508 |
/*IndexSymbols=*/{}, Opts); |
| 509 |
EXPECT_THAT( |
| 510 |
Results.Completions, |
| 511 |
HasSubsequence(named("a"), |
| 512 |
snippetSuffix("(${1:int i}, ${2:const float f})"))); |
| 513 |
} |
| 514 |
|
| 515 |
TEST(CompletionTest, HeuristicsForMemberFunctionCompletion) { |
| 516 |
clangd::CodeCompleteOptions Opts; |
| 517 |
Opts.EnableSnippets = true; |
| 518 |
|
| 519 |
Annotations Code(R"cpp( |
| 520 |
struct Foo { |
| 521 |
static int staticMethod(); |
| 522 |
int method() const; |
| 523 |
Foo() { |
| 524 |
this->$keepSnippet^ |
| 525 |
$keepSnippet^ |
| 526 |
Foo::$keepSnippet^ |
| 527 |
} |
| 528 |
}; |
| 529 |
|
| 530 |
struct Derived : Foo { |
| 531 |
Derived() { |
| 532 |
Foo::$keepSnippet^ |
| 533 |
} |
| 534 |
}; |
| 535 |
|
| 536 |
struct OtherClass { |
| 537 |
OtherClass() { |
| 538 |
Foo f; |
| 539 |
f.$keepSnippet^ |
| 540 |
&Foo::$noSnippet^ |
| 541 |
} |
| 542 |
}; |
| 543 |
|
| 544 |
int main() { |
| 545 |
Foo f; |
| 546 |
f.$keepSnippet^ |
| 547 |
&Foo::$noSnippet^ |
| 548 |
} |
| 549 |
)cpp"); |
| 550 |
auto TU = TestTU::withCode(Code.code()); |
| 551 |
|
| 552 |
for (const auto &P : Code.points("noSnippet")) { |
| 553 |
auto Results = completions(TU, P, /*IndexSymbols*/ {}, Opts); |
| 554 |
EXPECT_THAT(Results.Completions, |
| 555 |
Contains(AllOf(named("method"), snippetSuffix("")))); |
| 556 |
} |
| 557 |
|
| 558 |
for (const auto &P : Code.points("keepSnippet")) { |
| 559 |
auto Results = completions(TU, P, /*IndexSymbols*/ {}, Opts); |
| 560 |
EXPECT_THAT(Results.Completions, |
| 561 |
Contains(AllOf(named("method"), snippetSuffix("()")))); |
| 562 |
} |
| 563 |
|
| 564 |
// static method will always keep the snippet |
| 565 |
for (const auto &P : Code.points()) { |
| 566 |
auto Results = completions(TU, P, /*IndexSymbols*/ {}, Opts); |
| 567 |
EXPECT_THAT(Results.Completions, |
| 568 |
Contains(AllOf(named("staticMethod"), snippetSuffix("()")))); |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
TEST(CompletionTest, NoSnippetsInUsings) { |
| 573 |
clangd::CodeCompleteOptions Opts; |
| 574 |
Opts.EnableSnippets = true; |
| 575 |
auto Results = completions( |
| 576 |
R"cpp( |
| 577 |
namespace ns { |
| 578 |
int func(int a, int b); |
| 579 |
} |
| 580 |
|
| 581 |
using ns::^; |
| 582 |
)cpp", |
| 583 |
/*IndexSymbols=*/{}, Opts); |
| 584 |
EXPECT_THAT(Results.Completions, |
| 585 |
ElementsAre(AllOf(named("func"), labeled("func(int a, int b)"), |
| 586 |
snippetSuffix("")))); |
| 587 |
|
| 588 |
// Check index completions too. |
| 589 |
auto Func = func("ns::func"); |
| 590 |
Func.CompletionSnippetSuffix = "(${1:int a}, ${2: int b})"; |
| 591 |
Func.Signature = "(int a, int b)"; |
| 592 |
Func.ReturnType = "void"; |
| 593 |
|
| 594 |
Results = completions(R"cpp( |
| 595 |
namespace ns {} |
| 596 |
using ns::^; |
| 597 |
)cpp", |
| 598 |
/*IndexSymbols=*/{Func}, Opts); |
| 599 |
EXPECT_THAT(Results.Completions, |
| 600 |
ElementsAre(AllOf(named("func"), labeled("func(int a, int b)"), |
| 601 |
snippetSuffix("")))); |
| 602 |
|
| 603 |
// Check all-scopes completions too. |
| 604 |
Opts.AllScopes = true; |
| 605 |
Results = completions(R"cpp( |
| 606 |
using ^; |
| 607 |
)cpp", |
| 608 |
/*IndexSymbols=*/{Func}, Opts); |
| 609 |
EXPECT_THAT(Results.Completions, |
| 610 |
Contains(AllOf(named("func"), labeled("ns::func(int a, int b)"), |
| 611 |
snippetSuffix("")))); |
| 612 |
} |
| 613 |
|
| 614 |
TEST(CompletionTest, Kinds) { |
| 615 |
auto Results = completions( |
| 616 |
R"cpp( |
| 617 |
int variable; |
| 618 |
struct Struct {}; |
| 619 |
int function(); |
| 620 |
// make sure MACRO is not included in preamble. |
| 621 |
#define MACRO 10 |
| 622 |
int X = ^ |
| 623 |
)cpp", |
| 624 |
{func("indexFunction"), var("indexVariable"), cls("indexClass")}); |
| 625 |
EXPECT_THAT(Results.Completions, |
| 626 |
AllOf(has("function", CompletionItemKind::Function), |
| 627 |
has("variable", CompletionItemKind::Variable), |
| 628 |
has("int", CompletionItemKind::Keyword), |
| 629 |
has("Struct", CompletionItemKind::Struct), |
| 630 |
has("MACRO", CompletionItemKind::Text), |
| 631 |
has("indexFunction", CompletionItemKind::Function), |
| 632 |
has("indexVariable", CompletionItemKind::Variable), |
| 633 |
has("indexClass", CompletionItemKind::Class))); |
| 634 |
|
| 635 |
Results = completions("nam^"); |
| 636 |
EXPECT_THAT(Results.Completions, |
| 637 |
has("namespace", CompletionItemKind::Snippet)); |
| 638 |
|
| 639 |
// Members of anonymous unions are of kind 'field'. |
| 640 |
Results = completions( |
| 641 |
R"cpp( |
| 642 |
struct X{ |
| 643 |
union { |
| 644 |
void *a; |
| 645 |
}; |
| 646 |
}; |
| 647 |
auto u = X().^ |
| 648 |
)cpp"); |
| 649 |
EXPECT_THAT( |
| 650 |
Results.Completions, |
| 651 |
UnorderedElementsAre(AllOf(named("a"), kind(CompletionItemKind::Field)))); |
| 652 |
|
| 653 |
// Completion kinds for templates should not be unknown. |
| 654 |
Results = completions( |
| 655 |
R"cpp( |
| 656 |
template <class T> struct complete_class {}; |
| 657 |
template <class T> void complete_function(); |
| 658 |
template <class T> using complete_type_alias = int; |
| 659 |
template <class T> int complete_variable = 10; |
| 660 |
|
| 661 |
struct X { |
| 662 |
template <class T> static int complete_static_member = 10; |
| 663 |
|
| 664 |
static auto x = complete_^ |
| 665 |
} |
| 666 |
)cpp"); |
| 667 |
EXPECT_THAT( |
| 668 |
Results.Completions, |
| 669 |
UnorderedElementsAre( |
| 670 |
AllOf(named("complete_class"), kind(CompletionItemKind::Class)), |
| 671 |
AllOf(named("complete_function"), kind(CompletionItemKind::Function)), |
| 672 |
AllOf(named("complete_type_alias"), |
| 673 |
kind(CompletionItemKind::Interface)), |
| 674 |
AllOf(named("complete_variable"), kind(CompletionItemKind::Variable)), |
| 675 |
AllOf(named("complete_static_member"), |
| 676 |
kind(CompletionItemKind::Property)))); |
| 677 |
|
| 678 |
Results = completions( |
| 679 |
R"cpp( |
| 680 |
enum Color { |
| 681 |
Red |
| 682 |
}; |
| 683 |
Color u = ^ |
| 684 |
)cpp"); |
| 685 |
EXPECT_THAT( |
| 686 |
Results.Completions, |
| 687 |
Contains(AllOf(named("Red"), kind(CompletionItemKind::EnumMember)))); |
| 688 |
} |
| 689 |
|
| 690 |
TEST(CompletionTest, NoDuplicates) { |
| 691 |
auto Results = completions( |
| 692 |
R"cpp( |
| 693 |
class Adapter { |
| 694 |
}; |
| 695 |
|
| 696 |
void f() { |
| 697 |
Adapter^ |
| 698 |
} |
| 699 |
)cpp", |
| 700 |
{cls("Adapter")}); |
| 701 |
|
| 702 |
// Make sure there are no duplicate entries of 'Adapter'. |
| 703 |
EXPECT_THAT(Results.Completions, ElementsAre(named("Adapter"))); |
| 704 |
} |
| 705 |
|
| 706 |
TEST(CompletionTest, ScopedNoIndex) { |
| 707 |
auto Results = completions( |
| 708 |
R"cpp( |
| 709 |
namespace fake { int BigBang, Babble, Box; }; |
| 710 |
int main() { fake::ba^ } |
| 711 |
")cpp"); |
| 712 |
// Babble is a better match than BigBang. Box doesn't match at all. |
| 713 |
EXPECT_THAT(Results.Completions, |
| 714 |
ElementsAre(named("Babble"), named("BigBang"))); |
| 715 |
} |
| 716 |
|
| 717 |
TEST(CompletionTest, Scoped) { |
| 718 |
auto Results = completions( |
| 719 |
R"cpp( |
| 720 |
namespace fake { int Babble, Box; }; |
| 721 |
int main() { fake::ba^ } |
| 722 |
")cpp", |
| 723 |
{var("fake::BigBang")}); |
| 724 |
EXPECT_THAT(Results.Completions, |
| 725 |
ElementsAre(named("Babble"), named("BigBang"))); |
| 726 |
} |
| 727 |
|
| 728 |
TEST(CompletionTest, ScopedWithFilter) { |
| 729 |
auto Results = completions( |
| 730 |
R"cpp( |
| 731 |
void f() { ns::x^ } |
| 732 |
)cpp", |
| 733 |
{cls("ns::XYZ"), func("ns::foo")}); |
| 734 |
EXPECT_THAT(Results.Completions, UnorderedElementsAre(named("XYZ"))); |
| 735 |
} |
| 736 |
|
| 737 |
TEST(CompletionTest, ReferencesAffectRanking) { |
| 738 |
EXPECT_THAT(completions("int main() { abs^ }", {func("absA"), func("absB")}) |
| 739 |
.Completions, |
| 740 |
HasSubsequence(named("absA"), named("absB"))); |
| 741 |
EXPECT_THAT(completions("int main() { abs^ }", |
| 742 |
{func("absA"), withReferences(1000, func("absB"))}) |
| 743 |
.Completions, |
| 744 |
HasSubsequence(named("absB"), named("absA"))); |
| 745 |
} |
| 746 |
|
| 747 |
TEST(CompletionTest, ContextWords) { |
| 748 |
auto Results = completions(R"cpp( |
| 749 |
enum class Color { RED, YELLOW, BLUE }; |
| 750 |
|
| 751 |
// (blank lines so the definition above isn't "context") |
| 752 |
|
| 753 |
// "It was a yellow car," he said. "Big yellow car, new." |
| 754 |
auto Finish = Color::^ |
| 755 |
)cpp"); |
| 756 |
// Yellow would normally sort last (alphabetic). |
| 757 |
// But the recent mention should bump it up. |
| 758 |
ASSERT_THAT(Results.Completions, |
| 759 |
HasSubsequence(named("YELLOW"), named("BLUE"))); |
| 760 |
} |
| 761 |
|
| 762 |
TEST(CompletionTest, GlobalQualified) { |
| 763 |
auto Results = completions( |
| 764 |
R"cpp( |
| 765 |
void f() { ::^ } |
| 766 |
)cpp", |
| 767 |
{cls("XYZ")}); |
| 768 |
EXPECT_THAT(Results.Completions, |
| 769 |
AllOf(has("XYZ", CompletionItemKind::Class), |
| 770 |
has("f", CompletionItemKind::Function))); |
| 771 |
} |
| 772 |
|
| 773 |
TEST(CompletionTest, FullyQualified) { |
| 774 |
auto Results = completions( |
| 775 |
R"cpp( |
| 776 |
namespace ns { void bar(); } |
| 777 |
void f() { ::ns::^ } |
| 778 |
)cpp", |
| 779 |
{cls("ns::XYZ")}); |
| 780 |
EXPECT_THAT(Results.Completions, |
| 781 |
AllOf(has("XYZ", CompletionItemKind::Class), |
| 782 |
has("bar", CompletionItemKind::Function))); |
| 783 |
} |
| 784 |
|
| 785 |
TEST(CompletionTest, SemaIndexMerge) { |
| 786 |
auto Results = completions( |
| 787 |
R"cpp( |
| 788 |
namespace ns { int local; void both(); } |
| 789 |
void f() { ::ns::^ } |
| 790 |
)cpp", |
| 791 |
{func("ns::both"), cls("ns::Index")}); |
| 792 |
// We get results from both index and sema, with no duplicates. |
| 793 |
EXPECT_THAT(Results.Completions, |
| 794 |
UnorderedElementsAre( |
| 795 |
AllOf(named("local"), origin(SymbolOrigin::AST)), |
| 796 |
AllOf(named("Index"), origin(SymbolOrigin::Static)), |
| 797 |
AllOf(named("both"), |
| 798 |
origin(SymbolOrigin::AST | SymbolOrigin::Static)))); |
| 799 |
} |
| 800 |
|
| 801 |
TEST(CompletionTest, SemaIndexMergeWithLimit) { |
| 802 |
clangd::CodeCompleteOptions Opts; |
| 803 |
Opts.Limit = 1; |
| 804 |
auto Results = completions( |
| 805 |
R"cpp( |
| 806 |
namespace ns { int local; void both(); } |
| 807 |
void f() { ::ns::^ } |
| 808 |
)cpp", |
| 809 |
{func("ns::both"), cls("ns::Index")}, Opts); |
| 810 |
EXPECT_EQ(Results.Completions.size(), Opts.Limit); |
| 811 |
EXPECT_TRUE(Results.HasMore); |
| 812 |
} |
| 813 |
|
| 814 |
TEST(CompletionTest, IncludeInsertionPreprocessorIntegrationTests) { |
| 815 |
TestTU TU; |
| 816 |
TU.ExtraArgs.push_back("-I" + testPath("sub")); |
| 817 |
TU.AdditionalFiles["sub/bar.h"] = ""; |
| 818 |
auto BarURI = URI::create(testPath("sub/bar.h")).toString(); |
| 819 |
|
| 820 |
Symbol Sym = cls("ns::X"); |
| 821 |
Sym.CanonicalDeclaration.FileURI = BarURI.c_str(); |
| 822 |
Sym.IncludeHeaders.emplace_back(BarURI, 1, Symbol::Include); |
| 823 |
// Shorten include path based on search directory and insert. |
| 824 |
Annotations Test("int main() { ns::^ }"); |
| 825 |
TU.Code = Test.code().str(); |
| 826 |
auto Results = completions(TU, Test.point(), {Sym}); |
| 827 |
EXPECT_THAT(Results.Completions, |
| 828 |
ElementsAre(AllOf(named("X"), insertInclude("\"bar.h\"")))); |
| 829 |
// Can be disabled via option. |
| 830 |
CodeCompleteOptions NoInsertion; |
| 831 |
NoInsertion.InsertIncludes = CodeCompleteOptions::NeverInsert; |
| 832 |
Results = completions(TU, Test.point(), {Sym}, NoInsertion); |
| 833 |
EXPECT_THAT(Results.Completions, |
| 834 |
ElementsAre(AllOf(named("X"), Not(insertInclude())))); |
| 835 |
// Duplicate based on inclusions in preamble. |
| 836 |
Test = Annotations(R"cpp( |
| 837 |
#include "sub/bar.h" // not shortest, so should only match resolved. |
| 838 |
int main() { ns::^ } |
| 839 |
)cpp"); |
| 840 |
TU.Code = Test.code().str(); |
| 841 |
Results = completions(TU, Test.point(), {Sym}); |
| 842 |
EXPECT_THAT(Results.Completions, ElementsAre(AllOf(named("X"), labeled("X"), |
| 843 |
Not(insertInclude())))); |
| 844 |
} |
| 845 |
|
| 846 |
TEST(CompletionTest, NoIncludeInsertionWhenDeclFoundInFile) { |
| 847 |
Symbol SymX = cls("ns::X"); |
| 848 |
Symbol SymY = cls("ns::Y"); |
| 849 |
std::string BarHeader = testPath("bar.h"); |
| 850 |
auto BarURI = URI::create(BarHeader).toString(); |
| 851 |
SymX.CanonicalDeclaration.FileURI = BarURI.c_str(); |
| 852 |
SymY.CanonicalDeclaration.FileURI = BarURI.c_str(); |
| 853 |
SymX.IncludeHeaders.emplace_back("<bar>", 1, Symbol::Include); |
| 854 |
SymY.IncludeHeaders.emplace_back("<bar>", 1, Symbol::Include); |
| 855 |
// Shorten include path based on search directory and insert. |
| 856 |
auto Results = completions(R"cpp( |
| 857 |
namespace ns { |
| 858 |
class X; |
| 859 |
class Y {}; |
| 860 |
} |
| 861 |
int main() { ns::^ } |
| 862 |
)cpp", |
| 863 |
{SymX, SymY}); |
| 864 |
EXPECT_THAT(Results.Completions, |
| 865 |
ElementsAre(AllOf(named("X"), Not(insertInclude())), |
| 866 |
AllOf(named("Y"), Not(insertInclude())))); |
| 867 |
} |
| 868 |
|
| 869 |
TEST(CompletionTest, IndexSuppressesPreambleCompletions) { |
| 870 |
Annotations Test(R"cpp( |
| 871 |
#include "bar.h" |
| 872 |
namespace ns { int local; } |
| 873 |
void f() { ns::^; } |
| 874 |
void f2() { ns::preamble().$2^; } |
| 875 |
)cpp"); |
| 876 |
auto TU = TestTU::withCode(Test.code()); |
| 877 |
TU.AdditionalFiles["bar.h"] = |
| 878 |
R"cpp(namespace ns { struct preamble { int member; }; })cpp"; |
| 879 |
|
| 880 |
clangd::CodeCompleteOptions Opts = {}; |
| 881 |
auto I = memIndex({var("ns::index")}); |
| 882 |
Opts.Index = I.get(); |
| 883 |
auto WithIndex = completions(TU, Test.point(), {}, Opts); |
| 884 |
EXPECT_THAT(WithIndex.Completions, |
| 885 |
UnorderedElementsAre(named("local"), named("index"))); |
| 886 |
auto ClassFromPreamble = completions(TU, Test.point("2"), {}, Opts); |
| 887 |
EXPECT_THAT(ClassFromPreamble.Completions, Contains(named("member"))); |
| 888 |
|
| 889 |
Opts.Index = nullptr; |
| 890 |
auto WithoutIndex = completions(TU, Test.point(), {}, Opts); |
| 891 |
EXPECT_THAT(WithoutIndex.Completions, |
| 892 |
UnorderedElementsAre(named("local"), named("preamble"))); |
| 893 |
} |
| 894 |
|
| 895 |
// This verifies that we get normal preprocessor completions in the preamble. |
| 896 |
// This is a regression test for an old bug: if we override the preamble and |
| 897 |
// try to complete inside it, clang kicks our completion point just outside the |
| 898 |
// preamble, resulting in always getting top-level completions. |
| 899 |
TEST(CompletionTest, CompletionInPreamble) { |
| 900 |
auto Results = completions(R"cpp( |
| 901 |
#ifnd^ef FOO_H_ |
| 902 |
#define BAR_H_ |
| 903 |
#include <bar.h> |
| 904 |
int foo() {} |
| 905 |
#endif |
| 906 |
)cpp") |
| 907 |
.Completions; |
| 908 |
EXPECT_THAT(Results, ElementsAre(named("ifndef"))); |
| 909 |
} |
| 910 |
|
| 911 |
TEST(CompletionTest, CompletionRecoveryASTType) { |
| 912 |
auto Results = completions(R"cpp( |
| 913 |
struct S { int member; }; |
| 914 |
S overloaded(int); |
| 915 |
void foo() { |
| 916 |
// No overload matches, but we have recovery-expr with the correct type. |
| 917 |
overloaded().^ |
| 918 |
})cpp") |
| 919 |
.Completions; |
| 920 |
EXPECT_THAT(Results, ElementsAre(named("member"))); |
| 921 |
} |
| 922 |
|
| 923 |
TEST(CompletionTest, DynamicIndexIncludeInsertion) { |
| 924 |
MockFS FS; |
| 925 |
MockCompilationDatabase CDB; |
| 926 |
ClangdServer::Options Opts = ClangdServer::optsForTest(); |
| 927 |
Opts.BuildDynamicSymbolIndex = true; |
| 928 |
ClangdServer Server(CDB, FS, Opts); |
| 929 |
|
| 930 |
FS.Files[testPath("foo_header.h")] = R"cpp( |
| 931 |
#pragma once |
| 932 |
struct Foo { |
| 933 |
// Member doc |
| 934 |
int foo(); |
| 935 |
}; |
| 936 |
)cpp"; |
| 937 |
const std::string FileContent(R"cpp( |
| 938 |
#include "foo_header.h" |
| 939 |
int Foo::foo() { |
| 940 |
return 42; |
| 941 |
} |
| 942 |
)cpp"); |
| 943 |
Server.addDocument(testPath("foo_impl.cpp"), FileContent); |
| 944 |
// Wait for the dynamic index being built. |
| 945 |
ASSERT_TRUE(Server.blockUntilIdleForTest()); |
| 946 |
|
| 947 |
auto File = testPath("foo.cpp"); |
| 948 |
Annotations Test("Foo^ foo;"); |
| 949 |
runAddDocument(Server, File, Test.code()); |
| 950 |
auto CompletionList = |
| 951 |
llvm::cantFail(runCodeComplete(Server, File, Test.point(), {})); |
| 952 |
|
| 953 |
EXPECT_THAT(CompletionList.Completions, |
| 954 |
ElementsAre(AllOf(named("Foo"), hasInclude("\"foo_header.h\""), |
| 955 |
insertInclude()))); |
| 956 |
} |
| 957 |
|
| 958 |
TEST(CompletionTest, DynamicIndexMultiFile) { |
| 959 |
MockFS FS; |
| 960 |
MockCompilationDatabase CDB; |
| 961 |
auto Opts = ClangdServer::optsForTest(); |
| 962 |
Opts.BuildDynamicSymbolIndex = true; |
| 963 |
ClangdServer Server(CDB, FS, Opts); |
| 964 |
|
| 965 |
FS.Files[testPath("foo.h")] = R"cpp( |
| 966 |
namespace ns { class XYZ {}; void foo(int x) {} } |
| 967 |
)cpp"; |
| 968 |
runAddDocument(Server, testPath("foo.cpp"), R"cpp( |
| 969 |
#include "foo.h" |
| 970 |
)cpp"); |
| 971 |
|
| 972 |
auto File = testPath("bar.cpp"); |
| 973 |
Annotations Test(R"cpp( |
| 974 |
namespace ns { |
| 975 |
class XXX {}; |
| 976 |
/// Doooc |
| 977 |
void fooooo() {} |
| 978 |
} |
| 979 |
void f() { ns::^ } |
| 980 |
)cpp"); |
| 981 |
runAddDocument(Server, File, Test.code()); |
| 982 |
|
| 983 |
auto Results = cantFail(runCodeComplete(Server, File, Test.point(), {})); |
| 984 |
// "XYZ" and "foo" are not included in the file being completed but are still |
| 985 |
// visible through the index. |
| 986 |
EXPECT_THAT(Results.Completions, has("XYZ", CompletionItemKind::Class)); |
| 987 |
EXPECT_THAT(Results.Completions, has("foo", CompletionItemKind::Function)); |
| 988 |
EXPECT_THAT(Results.Completions, has("XXX", CompletionItemKind::Class)); |
| 989 |
EXPECT_THAT(Results.Completions, |
| 990 |
Contains((named("fooooo"), kind(CompletionItemKind::Function), |
| 991 |
doc("Doooc"), returnType("void")))); |
| 992 |
} |
| 993 |
|
| 994 |
TEST(CompletionTest, Documentation) { |
| 995 |
auto Results = completions( |
| 996 |
R"cpp( |
| 997 |
// Non-doxygen comment. |
| 998 |
__attribute__((annotate("custom_annotation"))) int foo(); |
| 999 |
/// Doxygen comment. |
| 1000 |
/// \param int a |
| 1001 |
int bar(int a); |
| 1002 |
/* Multi-line |
| 1003 |
block comment |
| 1004 |
*/ |
| 1005 |
int baz(); |
| 1006 |
|
| 1007 |
int x = ^ |
| 1008 |
)cpp"); |
| 1009 |
EXPECT_THAT(Results.Completions, |
| 1010 |
Contains(AllOf( |
| 1011 |
named("foo"), |
| 1012 |
doc("Annotation: custom_annotation\nNon-doxygen comment.")))); |
| 1013 |
EXPECT_THAT( |
| 1014 |
Results.Completions, |
| 1015 |
Contains(AllOf(named("bar"), doc("Doxygen comment.\n\\param int a")))); |
| 1016 |
EXPECT_THAT(Results.Completions, |
| 1017 |
Contains(AllOf(named("baz"), doc("Multi-line block comment")))); |
| 1018 |
} |
| 1019 |
|
| 1020 |
TEST(CompletionTest, CommentsFromSystemHeaders) { |
| 1021 |
MockFS FS; |
| 1022 |
MockCompilationDatabase CDB; |
| 1023 |
|
| 1024 |
auto Opts = ClangdServer::optsForTest(); |
| 1025 |
Opts.BuildDynamicSymbolIndex = true; |
| 1026 |
|
| 1027 |
ClangdServer Server(CDB, FS, Opts); |
| 1028 |
|
| 1029 |
FS.Files[testPath("foo.h")] = R"cpp( |
| 1030 |
#pragma GCC system_header |
| 1031 |
|
| 1032 |
// This comment should be retained! |
| 1033 |
int foo(); |
| 1034 |
)cpp"; |
| 1035 |
|
| 1036 |
auto File = testPath("foo.cpp"); |
| 1037 |
Annotations Test(R"cpp( |
| 1038 |
#include "foo.h" |
| 1039 |
int x = foo^ |
| 1040 |
)cpp"); |
| 1041 |
runAddDocument(Server, File, Test.code()); |
| 1042 |
auto CompletionList = |
| 1043 |
llvm::cantFail(runCodeComplete(Server, File, Test.point(), {})); |
| 1044 |
|
| 1045 |
EXPECT_THAT( |
| 1046 |
CompletionList.Completions, |
| 1047 |
Contains(AllOf(named("foo"), doc("This comment should be retained!")))); |
| 1048 |
} |
| 1049 |
|
| 1050 |
TEST(CompletionTest, GlobalCompletionFiltering) { |
| 1051 |
|
| 1052 |
Symbol Class = cls("XYZ"); |
| 1053 |
Class.Flags = static_cast<Symbol::SymbolFlag>( |
| 1054 |
Class.Flags & ~(Symbol::IndexedForCodeCompletion)); |
| 1055 |
Symbol Func = func("XYZ::foooo"); |
| 1056 |
Func.Flags = static_cast<Symbol::SymbolFlag>( |
| 1057 |
Func.Flags & ~(Symbol::IndexedForCodeCompletion)); |
| 1058 |
|
| 1059 |
auto Results = completions(R"(// void f() { |
| 1060 |
XYZ::foooo^ |
| 1061 |
})", |
| 1062 |
{Class, Func}); |
| 1063 |
EXPECT_THAT(Results.Completions, IsEmpty()); |
| 1064 |
} |
| 1065 |
|
| 1066 |
TEST(CodeCompleteTest, DisableTypoCorrection) { |
| 1067 |
auto Results = completions(R"cpp( |
| 1068 |
namespace clang { int v; } |
| 1069 |
void f() { clangd::^ |
| 1070 |
)cpp"); |
| 1071 |
EXPECT_TRUE(Results.Completions.empty()); |
| 1072 |
} |
| 1073 |
|
| 1074 |
TEST(CodeCompleteTest, NoColonColonAtTheEnd) { |
| 1075 |
auto Results = completions(R"cpp( |
| 1076 |
namespace clang { } |
| 1077 |
void f() { |
| 1078 |
clan^ |
| 1079 |
} |
| 1080 |
)cpp"); |
| 1081 |
|
| 1082 |
EXPECT_THAT(Results.Completions, Contains(labeled("clang"))); |
| 1083 |
EXPECT_THAT(Results.Completions, Not(Contains(labeled("clang::")))); |
| 1084 |
} |
| 1085 |
|
| 1086 |
TEST(CompletionTests, EmptySnippetDoesNotCrash) { |
| 1087 |
// See https://github.com/clangd/clangd/issues/1216 |
| 1088 |
auto Results = completions(R"cpp( |
| 1089 |
int main() { |
| 1090 |
auto w = [&](auto &&f) { return f(f); }; |
| 1091 |
auto f = w([&](auto &&f) { |
| 1092 |
return [&](auto &&n) { |
| 1093 |
if (n == 0) { |
| 1094 |
return 1; |
| 1095 |
} |
| 1096 |
return n * ^(f)(n - 1); |
| 1097 |
}; |
| 1098 |
})(10); |
| 1099 |
} |
| 1100 |
)cpp"); |
| 1101 |
} |
| 1102 |
|
| 1103 |
TEST(CompletionTest, Issue1427Crash) { |
| 1104 |
// Need to provide main file signals to ensure that the branch in |
| 1105 |
// SymbolRelevanceSignals::computeASTSignals() that tries to |
| 1106 |
// compute a symbol ID is taken. |
| 1107 |
ASTSignals MainFileSignals; |
| 1108 |
CodeCompleteOptions Opts; |
| 1109 |
Opts.MainFileSignals = &MainFileSignals; |
| 1110 |
completions(R"cpp( |
| 1111 |
auto f = []() { |
| 1112 |
1.0_^ |
| 1113 |
}; |
| 1114 |
)cpp", |
| 1115 |
{}, Opts); |
| 1116 |
} |
| 1117 |
|
| 1118 |
TEST(CompletionTest, BacktrackCrashes) { |
| 1119 |
// Sema calls code completion callbacks twice in these cases. |
| 1120 |
auto Results = completions(R"cpp( |
| 1121 |
namespace ns { |
| 1122 |
struct FooBarBaz {}; |
| 1123 |
} // namespace ns |
| 1124 |
|
| 1125 |
int foo(ns::FooBar^ |
| 1126 |
)cpp"); |
| 1127 |
|
| 1128 |
EXPECT_THAT(Results.Completions, ElementsAre(labeled("FooBarBaz"))); |
| 1129 |
|
| 1130 |
// Check we don't crash in that case too. |
| 1131 |
completions(R"cpp( |
| 1132 |
struct FooBarBaz {}; |
| 1133 |
void test() { |
| 1134 |
if (FooBarBaz * x^) {} |
| 1135 |
} |
| 1136 |
)cpp"); |
| 1137 |
} |
| 1138 |
|
| 1139 |
TEST(CompletionTest, CompleteInMacroWithStringification) { |
| 1140 |
auto Results = completions(R"cpp( |
| 1141 |
void f(const char *, int x); |
| 1142 |
#define F(x) f(#x, x) |
| 1143 |
|
| 1144 |
namespace ns { |
| 1145 |
int X; |
| 1146 |
int Y; |
| 1147 |
} // namespace ns |
| 1148 |
|
| 1149 |
int f(int input_num) { |
| 1150 |
F(ns::^) |
| 1151 |
} |
| 1152 |
)cpp"); |
| 1153 |
|
| 1154 |
EXPECT_THAT(Results.Completions, |
| 1155 |
UnorderedElementsAre(named("X"), named("Y"))); |
| 1156 |
} |
| 1157 |
|
| 1158 |
TEST(CompletionTest, CompleteInMacroAndNamespaceWithStringification) { |
| 1159 |
auto Results = completions(R"cpp( |
| 1160 |
void f(const char *, int x); |
| 1161 |
#define F(x) f(#x, x) |
| 1162 |
|
| 1163 |
namespace ns { |
| 1164 |
int X; |
| 1165 |
|
| 1166 |
int f(int input_num) { |
| 1167 |
F(^) |
| 1168 |
} |
| 1169 |
} // namespace ns |
| 1170 |
)cpp"); |
| 1171 |
|
| 1172 |
EXPECT_THAT(Results.Completions, Contains(named("X"))); |
| 1173 |
} |
| 1174 |
|
| 1175 |
TEST(CompletionTest, IgnoreCompleteInExcludedPPBranchWithRecoveryContext) { |
| 1176 |
auto Results = completions(R"cpp( |
| 1177 |
int bar(int param_in_bar) { |
| 1178 |
} |
| 1179 |
|
| 1180 |
int foo(int param_in_foo) { |
| 1181 |
#if 0 |
| 1182 |
// In recovery mode, "param_in_foo" will also be suggested among many other |
| 1183 |
// unrelated symbols; however, this is really a special case where this works. |
| 1184 |
// If the #if block is outside of the function, "param_in_foo" is still |
| 1185 |
// suggested, but "bar" and "foo" are missing. So the recovery mode doesn't |
| 1186 |
// really provide useful results in excluded branches. |
| 1187 |
par^ |
| 1188 |
#endif |
| 1189 |
} |
| 1190 |
)cpp"); |
| 1191 |
|
| 1192 |
EXPECT_TRUE(Results.Completions.empty()); |
| 1193 |
} |
| 1194 |
|
| 1195 |
TEST(CompletionTest, DefaultArgs) { |
| 1196 |
clangd::CodeCompleteOptions Opts; |
| 1197 |
std::string Context = R"cpp( |
| 1198 |
int X(int A = 0); |
| 1199 |
int Y(int A, int B = 0); |
| 1200 |
int Z(int A, int B = 0, int C = 0, int D = 0); |
| 1201 |
)cpp"; |
| 1202 |
EXPECT_THAT(completions(Context + "int y = X^", {}, Opts).Completions, |
| 1203 |
UnorderedElementsAre(labeled("X(int A = 0)"))); |
| 1204 |
EXPECT_THAT(completions(Context + "int y = Y^", {}, Opts).Completions, |
| 1205 |
UnorderedElementsAre(AllOf(labeled("Y(int A, int B = 0)"), |
| 1206 |
snippetSuffix("(${1:int A})")))); |
| 1207 |
EXPECT_THAT(completions(Context + "int y = Z^", {}, Opts).Completions, |
| 1208 |
UnorderedElementsAre( |
| 1209 |
AllOf(labeled("Z(int A, int B = 0, int C = 0, int D = 0)"), |
| 1210 |
snippetSuffix("(${1:int A})")))); |
| 1211 |
} |
| 1212 |
|
| 1213 |
TEST(CompletionTest, NoCrashWithTemplateParamsAndPreferredTypes) { |
| 1214 |
auto Completions = completions(R"cpp( |
| 1215 |
template <template <class> class TT> int foo() { |
| 1216 |
int a = ^ |
| 1217 |
} |
| 1218 |
)cpp") |
| 1219 |
.Completions; |
| 1220 |
EXPECT_THAT(Completions, Contains(named("TT"))); |
| 1221 |
} |
| 1222 |
|
| 1223 |
TEST(CompletionTest, NestedTemplateHeuristics) { |
| 1224 |
auto Completions = completions(R"cpp( |
| 1225 |
struct Plain { int xxx; }; |
| 1226 |
template <typename T> class Templ { Plain ppp; }; |
| 1227 |
template <typename T> void foo(Templ<T> &t) { |
| 1228 |
// Formally ppp has DependentTy, because Templ may be specialized. |
| 1229 |
// However we sholud be able to see into it using the primary template. |
| 1230 |
t.ppp.^ |
| 1231 |
} |
| 1232 |
)cpp") |
| 1233 |
.Completions; |
| 1234 |
EXPECT_THAT(Completions, Contains(named("xxx"))); |
| 1235 |
} |
| 1236 |
|
| 1237 |
TEST(CompletionTest, RecordCCResultCallback) { |
| 1238 |
std::vector<CodeCompletion> RecordedCompletions; |
| 1239 |
CodeCompleteOptions Opts; |
| 1240 |
Opts.RecordCCResult = [&RecordedCompletions](const CodeCompletion &CC, |
| 1241 |
const SymbolQualitySignals &, |
| 1242 |
const SymbolRelevanceSignals &, |
| 1243 |
float Score) { |
| 1244 |
RecordedCompletions.push_back(CC); |
| 1245 |
}; |
| 1246 |
|
| 1247 |
completions("int xy1, xy2; int a = xy^", /*IndexSymbols=*/{}, Opts); |
| 1248 |
EXPECT_THAT(RecordedCompletions, |
| 1249 |
UnorderedElementsAre(named("xy1"), named("xy2"))); |
| 1250 |
} |
| 1251 |
|
| 1252 |
TEST(CompletionTest, ASTSignals) { |
| 1253 |
struct Completion { |
| 1254 |
std::string Name; |
| 1255 |
unsigned MainFileRefs; |
| 1256 |
unsigned ScopeRefsInFile; |
| 1257 |
}; |
| 1258 |
CodeCompleteOptions Opts; |
| 1259 |
std::vector<Completion> RecordedCompletions; |
| 1260 |
Opts.RecordCCResult = [&RecordedCompletions](const CodeCompletion &CC, |
| 1261 |
const SymbolQualitySignals &, |
| 1262 |
const SymbolRelevanceSignals &R, |
| 1263 |
float Score) { |
| 1264 |
RecordedCompletions.push_back({CC.Name, R.MainFileRefs, R.ScopeRefsInFile}); |
| 1265 |
}; |
| 1266 |
ASTSignals MainFileSignals; |
| 1267 |
MainFileSignals.ReferencedSymbols[var("xy1").ID] = 3; |
| 1268 |
MainFileSignals.ReferencedSymbols[var("xy2").ID] = 1; |
| 1269 |
MainFileSignals.ReferencedSymbols[var("xyindex").ID] = 10; |
| 1270 |
MainFileSignals.RelatedNamespaces["tar::"] = 5; |
| 1271 |
MainFileSignals.RelatedNamespaces["bar::"] = 3; |
| 1272 |
Opts.MainFileSignals = &MainFileSignals; |
| 1273 |
Opts.AllScopes = true; |
| 1274 |
completions( |
| 1275 |
R"cpp( |
| 1276 |
int xy1; |
| 1277 |
int xy2; |
| 1278 |
namespace bar { |
| 1279 |
int xybar = 1; |
| 1280 |
int a = xy^ |
| 1281 |
} |
| 1282 |
)cpp", |
| 1283 |
/*IndexSymbols=*/{var("xyindex"), var("tar::xytar"), var("bar::xybar")}, |
| 1284 |
Opts); |
| 1285 |
EXPECT_THAT(RecordedCompletions, |
| 1286 |
UnorderedElementsAre( |
| 1287 |
AllOf(named("xy1"), mainFileRefs(3u), scopeRefs(0u)), |
| 1288 |
AllOf(named("xy2"), mainFileRefs(1u), scopeRefs(0u)), |
| 1289 |
AllOf(named("xyindex"), mainFileRefs(10u), scopeRefs(0u)), |
| 1290 |
AllOf(named("xytar"), mainFileRefs(0u), scopeRefs(5u)), |
| 1291 |
AllOf(/*both from sema and index*/ named("xybar"), |
| 1292 |
mainFileRefs(0u), scopeRefs(3u)))); |
| 1293 |
} |
| 1294 |
|
| 1295 |
SignatureHelp |
| 1296 |
signatures(llvm::StringRef Text, Position Point, |
| 1297 |
std::vector<Symbol> IndexSymbols = {}, |
| 1298 |
MarkupKind DocumentationFormat = MarkupKind::PlainText) { |
| 1299 |
std::unique_ptr<SymbolIndex> Index; |
| 1300 |
if (!IndexSymbols.empty()) |
| 1301 |
Index = memIndex(IndexSymbols); |
| 1302 |
|
| 1303 |
auto TU = TestTU::withCode(Text); |
| 1304 |
MockFS FS; |
| 1305 |
auto Inputs = TU.inputs(FS); |
| 1306 |
Inputs.Index = Index.get(); |
| 1307 |
IgnoreDiagnostics Diags; |
| 1308 |
auto CI = buildCompilerInvocation(Inputs, Diags); |
| 1309 |
if (!CI) { |
| 1310 |
ADD_FAILURE() << "Couldn't build CompilerInvocation"; |
| 1311 |
return {}; |
| 1312 |
} |
| 1313 |
auto Preamble = buildPreamble(testPath(TU.Filename), *CI, Inputs, |
| 1314 |
/*InMemory=*/true, /*Callback=*/nullptr); |
| 1315 |
if (!Preamble) { |
| 1316 |
ADD_FAILURE() << "Couldn't build Preamble"; |
| 1317 |
return {}; |
| 1318 |
} |
| 1319 |
return signatureHelp(testPath(TU.Filename), Point, *Preamble, Inputs, |
| 1320 |
DocumentationFormat); |
| 1321 |
} |
| 1322 |
|
| 1323 |
SignatureHelp |
| 1324 |
signatures(llvm::StringRef Text, std::vector<Symbol> IndexSymbols = {}, |
| 1325 |
MarkupKind DocumentationFormat = MarkupKind::PlainText) { |
| 1326 |
Annotations Test(Text); |
| 1327 |
return signatures(Test.code(), Test.point(), std::move(IndexSymbols), |
| 1328 |
DocumentationFormat); |
| 1329 |
} |
| 1330 |
|
| 1331 |
struct ExpectedParameter { |
| 1332 |
std::string Text; |
| 1333 |
std::pair<unsigned, unsigned> Offsets; |
| 1334 |
}; |
| 1335 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, |
| 1336 |
const ExpectedParameter &P) { |
| 1337 |
return OS << P.Text; |
| 1338 |
} |
| 1339 |
MATCHER_P(paramsAre, P, "") { |
| 1340 |
if (P.size() != arg.parameters.size()) |
| 1341 |
return false; |
| 1342 |
for (unsigned I = 0; I < P.size(); ++I) { |
| 1343 |
if (P[I].Text != arg.parameters[I].labelString || |
| 1344 |
P[I].Offsets != arg.parameters[I].labelOffsets) |
| 1345 |
return false; |
| 1346 |
} |
| 1347 |
return true; |
| 1348 |
} |
| 1349 |
MATCHER_P(sigDoc, doc, "") { return arg.documentation.value == doc; } |
| 1350 |
|
| 1351 |
/// \p AnnotatedLabel is a signature label with ranges marking parameters, e.g. |
| 1352 |
/// foo([[int p1]], [[double p2]]) -> void |
| 1353 |
Matcher<SignatureInformation> sig(llvm::StringRef AnnotatedLabel) { |
| 1354 |
llvm::Annotations A(AnnotatedLabel); |
| 1355 |
std::string Label = std::string(A.code()); |
| 1356 |
std::vector<ExpectedParameter> Parameters; |
| 1357 |
for (auto Range : A.ranges()) { |
| 1358 |
Parameters.emplace_back(); |
| 1359 |
|
| 1360 |
ExpectedParameter &P = Parameters.back(); |
| 1361 |
P.Text = Label.substr(Range.Begin, Range.End - Range.Begin); |
| 1362 |
P.Offsets.first = lspLength(llvm::StringRef(Label).substr(0, Range.Begin)); |
| 1363 |
P.Offsets.second = lspLength(llvm::StringRef(Label).substr(1, Range.End)); |
| 1364 |
} |
| 1365 |
return AllOf(sigHelpLabeled(Label), paramsAre(Parameters)); |
| 1366 |
} |
| 1367 |
|
| 1368 |
TEST(SignatureHelpTest, Overloads) { |
| 1369 |
auto Results = signatures(R"cpp( |
| 1370 |
void foo(int x, int y); |
| 1371 |
void foo(int x, float y); |
| 1372 |
void foo(float x, int y); |
| 1373 |
void foo(float x, float y); |
| 1374 |
void bar(int x, int y = 0); |
| 1375 |
int main() { foo(^); } |
| 1376 |
)cpp"); |
| 1377 |
EXPECT_THAT(Results.signatures, |
| 1378 |
UnorderedElementsAre(sig("foo([[float x]], [[float y]]) -> void"), |
| 1379 |
sig("foo([[float x]], [[int y]]) -> void"), |
| 1380 |
sig("foo([[int x]], [[float y]]) -> void"), |
| 1381 |
sig("foo([[int x]], [[int y]]) -> void"))); |
| 1382 |
// We always prefer the first signature. |
| 1383 |
EXPECT_EQ(0, Results.activeSignature); |
| 1384 |
EXPECT_EQ(0, Results.activeParameter); |
| 1385 |
} |
| 1386 |
|
| 1387 |
TEST(SignatureHelpTest, FunctionPointers) { |
| 1388 |
auto FunctionPointerResults = signatures(R"cpp( |
| 1389 |
void (*foo)(int x, int y); |
| 1390 |
int main() { foo(^); } |
| 1391 |
)cpp"); |
| 1392 |
EXPECT_THAT(FunctionPointerResults.signatures, |
| 1393 |
UnorderedElementsAre(sig("([[int x]], [[int y]]) -> void"))); |
| 1394 |
|
| 1395 |
auto FunctionPointerTypedefResults = signatures(R"cpp( |
| 1396 |
typedef void (*fn)(int x, int y); |
| 1397 |
fn foo; |
| 1398 |
int main() { foo(^); } |
| 1399 |
)cpp"); |
| 1400 |
EXPECT_THAT(FunctionPointerTypedefResults.signatures, |
| 1401 |
UnorderedElementsAre(sig("([[int x]], [[int y]]) -> void"))); |
| 1402 |
} |
| 1403 |
|
| 1404 |
TEST(SignatureHelpTest, Constructors) { |
| 1405 |
std::string Top = R"cpp( |
| 1406 |
struct S { |
| 1407 |
S(int); |
| 1408 |
S(const S &) = delete; |
| 1409 |
}; |
| 1410 |
)cpp"; |
| 1411 |
|
| 1412 |
auto CheckParenInit = [&](std::string Init) { |
| 1413 |
EXPECT_THAT(signatures(Top + Init).signatures, |
| 1414 |
UnorderedElementsAre(sig("S([[int]])"))) |
| 1415 |
<< Init; |
| 1416 |
}; |
| 1417 |
CheckParenInit("S s(^);"); |
| 1418 |
CheckParenInit("auto s = S(^);"); |
| 1419 |
CheckParenInit("auto s = new S(^);"); |
| 1420 |
|
| 1421 |
auto CheckBracedInit = [&](std::string Init) { |
| 1422 |
EXPECT_THAT(signatures(Top + Init).signatures, |
| 1423 |
UnorderedElementsAre(sig("S{[[int]]}"))) |
| 1424 |
<< Init; |
| 1425 |
}; |
| 1426 |
CheckBracedInit("S s{^};"); |
| 1427 |
CheckBracedInit("S s = {^};"); |
| 1428 |
CheckBracedInit("auto s = S{^};"); |
| 1429 |
// FIXME: doesn't work: no ExpectedType set in ParseCXXNewExpression. |
| 1430 |
// CheckBracedInit("auto s = new S{^};"); |
| 1431 |
CheckBracedInit("int x(S); int i = x({^});"); |
| 1432 |
} |
| 1433 |
|
| 1434 |
TEST(SignatureHelpTest, Aggregates) { |
| 1435 |
std::string Top = R"cpp( |
| 1436 |
struct S { |
| 1437 |
int a, b, c, d; |
| 1438 |
}; |
| 1439 |
)cpp"; |
| 1440 |
auto AggregateSig = sig("S{[[int a]], [[int b]], [[int c]], [[int d]]}"); |
| 1441 |
EXPECT_THAT(signatures(Top + "S s{^}").signatures, |
| 1442 |
UnorderedElementsAre(AggregateSig, sig("S{}"), |
| 1443 |
sig("S{[[const S &]]}"), |
| 1444 |
sig("S{[[S &&]]}"))); |
| 1445 |
EXPECT_THAT(signatures(Top + "S s{1,^}").signatures, |
| 1446 |
ElementsAre(AggregateSig)); |
| 1447 |
EXPECT_EQ(signatures(Top + "S s{1,^}").activeParameter, 1); |
| 1448 |
EXPECT_THAT(signatures(Top + "S s{.c=3,^}").signatures, |
| 1449 |
ElementsAre(AggregateSig)); |
| 1450 |
EXPECT_EQ(signatures(Top + "S s{.c=3,^}").activeParameter, 3); |
| 1451 |
} |
| 1452 |
|
| 1453 |
TEST(SignatureHelpTest, OverloadInitListRegression) { |
| 1454 |
auto Results = signatures(R"cpp( |
| 1455 |
struct A {int x;}; |
| 1456 |
struct B {B(A);}; |
| 1457 |
void f(); |
| 1458 |
int main() { |
| 1459 |
B b({1}); |
| 1460 |
f(^); |
| 1461 |
} |
| 1462 |
)cpp"); |
| 1463 |
EXPECT_THAT(Results.signatures, UnorderedElementsAre(sig("f() -> void"))); |
| 1464 |
} |
| 1465 |
|
| 1466 |
TEST(SignatureHelpTest, DefaultArgs) { |
| 1467 |
auto Results = signatures(R"cpp( |
| 1468 |
void bar(int x, int y = 0); |
| 1469 |
void bar(float x = 0, int y = 42); |
| 1470 |
int main() { bar(^ |
| 1471 |
)cpp"); |
| 1472 |
EXPECT_THAT(Results.signatures, |
| 1473 |
UnorderedElementsAre( |
| 1474 |
sig("bar([[int x]], [[int y = 0]]) -> void"), |
| 1475 |
sig("bar([[float x = 0]], [[int y = 42]]) -> void"))); |
| 1476 |
EXPECT_EQ(0, Results.activeSignature); |
| 1477 |
EXPECT_EQ(0, Results.activeParameter); |
| 1478 |
} |
| 1479 |
|
| 1480 |
TEST(SignatureHelpTest, ActiveArg) { |
| 1481 |
auto Results = signatures(R"cpp( |
| 1482 |
int baz(int a, int b, int c); |
| 1483 |
int main() { baz(baz(1,2,3), ^); } |
| 1484 |
)cpp"); |
| 1485 |
EXPECT_THAT(Results.signatures, |
| 1486 |
ElementsAre(sig("baz([[int a]], [[int b]], [[int c]]) -> int"))); |
| 1487 |
EXPECT_EQ(0, Results.activeSignature); |
| 1488 |
EXPECT_EQ(1, Results.activeParameter); |
| 1489 |
} |
| 1490 |
|
| 1491 |
TEST(SignatureHelpTest, OpeningParen) { |
| 1492 |
llvm::StringLiteral Tests[] = { |
| 1493 |
// Recursive function call. |
| 1494 |
R"cpp( |
| 1495 |
int foo(int a, int b, int c); |
| 1496 |
int main() { |
| 1497 |
foo(foo $p^( foo(10, 10, 10), ^ ))); |
| 1498 |
})cpp", |
| 1499 |
// Functional type cast. |
| 1500 |
R"cpp( |
| 1501 |
struct Foo { |
| 1502 |
Foo(int a, int b, int c); |
| 1503 |
}; |
| 1504 |
int main() { |
| 1505 |
Foo $p^( 10, ^ ); |
| 1506 |
})cpp", |
| 1507 |
// New expression. |
| 1508 |
R"cpp( |
| 1509 |
struct Foo { |
| 1510 |
Foo(int a, int b, int c); |
| 1511 |
}; |
| 1512 |
int main() { |
| 1513 |
new Foo $p^( 10, ^ ); |
| 1514 |
})cpp", |
| 1515 |
// Macro expansion. |
| 1516 |
R"cpp( |
| 1517 |
int foo(int a, int b, int c); |
| 1518 |
#define FOO foo( |
| 1519 |
|
| 1520 |
int main() { |
| 1521 |
// Macro expansions. |
| 1522 |
$p^FOO 10, ^ ); |
| 1523 |
})cpp", |
| 1524 |
// Macro arguments. |
| 1525 |
R"cpp( |
| 1526 |
int foo(int a, int b, int c); |
| 1527 |
int main() { |
| 1528 |
#define ID(X) X |
| 1529 |
// FIXME: figure out why ID(foo (foo(10), )) doesn't work when preserving |
| 1530 |
// the recovery expression. |
| 1531 |
ID(foo $p^( 10, ^ )) |
| 1532 |
})cpp", |
| 1533 |
// Dependent args. |
| 1534 |
R"cpp( |
| 1535 |
int foo(int a, int b); |
| 1536 |
template <typename T> void bar(T t) { |
| 1537 |
foo$p^(t, ^t); |
| 1538 |
})cpp", |
| 1539 |
// Dependent args on templated func. |
| 1540 |
R"cpp( |
| 1541 |
template <typename T> |
| 1542 |
int foo(T, T); |
| 1543 |
template <typename T> void bar(T t) { |
| 1544 |
foo$p^(t, ^t); |
| 1545 |
})cpp", |
| 1546 |
// Dependent args on member. |
| 1547 |
R"cpp( |
| 1548 |
struct Foo { int foo(int, int); }; |
| 1549 |
template <typename T> void bar(T t) { |
| 1550 |
Foo f; |
| 1551 |
f.foo$p^(t, ^t); |
| 1552 |
})cpp", |
| 1553 |
// Dependent args on templated member. |
| 1554 |
R"cpp( |
| 1555 |
struct Foo { template <typename T> int foo(T, T); }; |
| 1556 |
template <typename T> void bar(T t) { |
| 1557 |
Foo f; |
| 1558 |
f.foo$p^(t, ^t); |
| 1559 |
})cpp", |
| 1560 |
}; |
| 1561 |
|
| 1562 |
for (auto Test : Tests) { |
| 1563 |
Annotations Code(Test); |
| 1564 |
EXPECT_EQ(signatures(Code.code(), Code.point()).argListStart, |
| 1565 |
Code.point("p")) |
| 1566 |
<< "Test source:" << Test; |
| 1567 |
} |
| 1568 |
} |
| 1569 |
|
| 1570 |
TEST(SignatureHelpTest, StalePreamble) { |
| 1571 |
TestTU TU; |
| 1572 |
TU.Code = ""; |
| 1573 |
IgnoreDiagnostics Diags; |
| 1574 |
MockFS FS; |
| 1575 |
auto Inputs = TU.inputs(FS); |
| 1576 |
auto CI = buildCompilerInvocation(Inputs, Diags); |
| 1577 |
ASSERT_TRUE(CI); |
| 1578 |
auto EmptyPreamble = buildPreamble(testPath(TU.Filename), *CI, Inputs, |
| 1579 |
/*InMemory=*/true, /*Callback=*/nullptr); |
| 1580 |
ASSERT_TRUE(EmptyPreamble); |
| 1581 |
|
| 1582 |
TU.AdditionalFiles["a.h"] = "int foo(int x);"; |
| 1583 |
const Annotations Test(R"cpp( |
| 1584 |
#include "a.h" |
| 1585 |
void bar() { foo(^2); })cpp"); |
| 1586 |
TU.Code = Test.code().str(); |
| 1587 |
auto Results = |
| 1588 |
signatureHelp(testPath(TU.Filename), Test.point(), *EmptyPreamble, |
| 1589 |
TU.inputs(FS), MarkupKind::PlainText); |
| 1590 |
EXPECT_THAT(Results.signatures, ElementsAre(sig("foo([[int x]]) -> int"))); |
| 1591 |
EXPECT_EQ(0, Results.activeSignature); |
| 1592 |
EXPECT_EQ(0, Results.activeParameter); |
| 1593 |
} |
| 1594 |
|
| 1595 |
class IndexRequestCollector : public SymbolIndex { |
| 1596 |
public: |
| 1597 |
IndexRequestCollector(std::vector<Symbol> Syms = {}) : Symbols(Syms) {} |
| 1598 |
|
| 1599 |
bool |
| 1600 |
fuzzyFind(const FuzzyFindRequest &Req, |
| 1601 |
llvm::function_ref<void(const Symbol &)> Callback) const override { |
| 1602 |
std::unique_lock<std::mutex> Lock(Mut); |
| 1603 |
Requests.push_back(Req); |
| 1604 |
ReceivedRequestCV.notify_one(); |
| 1605 |
for (const auto &Sym : Symbols) |
| 1606 |
Callback(Sym); |
| 1607 |
return true; |
| 1608 |
} |
| 1609 |
|
| 1610 |
void lookup(const LookupRequest &, |
| 1611 |
llvm::function_ref<void(const Symbol &)>) const override {} |
| 1612 |
|
| 1613 |
bool refs(const RefsRequest &, |
| 1614 |
llvm::function_ref<void(const Ref &)>) const override { |
| 1615 |
return false; |
| 1616 |
} |
| 1617 |
|
| 1618 |
void relations(const RelationsRequest &, |
| 1619 |
llvm::function_ref<void(const SymbolID &, const Symbol &)>) |
| 1620 |
const override {} |
| 1621 |
|
| 1622 |
llvm::unique_function<IndexContents(llvm::StringRef) const> |
| 1623 |
indexedFiles() const override { |
| 1624 |
return [](llvm::StringRef) { return IndexContents::None; }; |
| 1625 |
} |
| 1626 |
|
| 1627 |
// This is incorrect, but IndexRequestCollector is not an actual index and it |
| 1628 |
// isn't used in production code. |
| 1629 |
size_t estimateMemoryUsage() const override { return 0; } |
| 1630 |
|
| 1631 |
const std::vector<FuzzyFindRequest> consumeRequests(size_t Num) const { |
| 1632 |
std::unique_lock<std::mutex> Lock(Mut); |
| 1633 |
EXPECT_TRUE(wait(Lock, ReceivedRequestCV, timeoutSeconds(30), |
| 1634 |
[this, Num] { return Requests.size() == Num; })); |
| 1635 |
auto Reqs = std::move(Requests); |
| 1636 |
Requests = {}; |
| 1637 |
return Reqs; |
| 1638 |
} |
| 1639 |
|
| 1640 |
private: |
| 1641 |
std::vector<Symbol> Symbols; |
| 1642 |
// We need a mutex to handle async fuzzy find requests. |
| 1643 |
mutable std::condition_variable ReceivedRequestCV; |
| 1644 |
mutable std::mutex Mut; |
| 1645 |
mutable std::vector<FuzzyFindRequest> Requests; |
| 1646 |
}; |
| 1647 |
|
| 1648 |
// Clients have to consume exactly Num requests. |
| 1649 |
std::vector<FuzzyFindRequest> captureIndexRequests(llvm::StringRef Code, |
| 1650 |
size_t Num = 1) { |
| 1651 |
clangd::CodeCompleteOptions Opts; |
| 1652 |
IndexRequestCollector Requests; |
| 1653 |
Opts.Index = &Requests; |
| 1654 |
completions(Code, {}, Opts); |
| 1655 |
const auto Reqs = Requests.consumeRequests(Num); |
| 1656 |
EXPECT_EQ(Reqs.size(), Num); |
| 1657 |
return Reqs; |
| 1658 |
} |
| 1659 |
|
| 1660 |
TEST(CompletionTest, UnqualifiedIdQuery) { |
| 1661 |
auto Requests = captureIndexRequests(R"cpp( |
| 1662 |
namespace std {} |
| 1663 |
using namespace std; |
| 1664 |
namespace ns { |
| 1665 |
void f() { |
| 1666 |
vec^ |
| 1667 |
} |
| 1668 |
} |
| 1669 |
)cpp"); |
| 1670 |
|
| 1671 |
EXPECT_THAT(Requests, |
| 1672 |
ElementsAre(Field(&FuzzyFindRequest::Scopes, |
| 1673 |
UnorderedElementsAre("", "ns::", "std::")))); |
| 1674 |
} |
| 1675 |
|
| 1676 |
TEST(CompletionTest, EnclosingScopeComesFirst) { |
| 1677 |
auto Requests = captureIndexRequests(R"cpp( |
| 1678 |
namespace std {} |
| 1679 |
using namespace std; |
| 1680 |
namespace nx { |
| 1681 |
namespace ns { |
| 1682 |
namespace { |
| 1683 |
void f() { |
| 1684 |
vec^ |
| 1685 |
} |
| 1686 |
} |
| 1687 |
} |
| 1688 |
} |
| 1689 |
)cpp"); |
| 1690 |
|
| 1691 |
EXPECT_THAT(Requests, |
| 1692 |
ElementsAre(Field( |
| 1693 |
&FuzzyFindRequest::Scopes, |
| 1694 |
UnorderedElementsAre("", "std::", "nx::ns::", "nx::")))); |
| 1695 |
EXPECT_EQ(Requests[0].Scopes[0], "nx::ns::"); |
| 1696 |
} |
| 1697 |
|
| 1698 |
TEST(CompletionTest, ResolvedQualifiedIdQuery) { |
| 1699 |
auto Requests = captureIndexRequests(R"cpp( |
| 1700 |
namespace ns1 {} |
| 1701 |
namespace ns2 {} // ignore |
| 1702 |
namespace ns3 { namespace nns3 {} } |
| 1703 |
namespace foo { |
| 1704 |
using namespace ns1; |
| 1705 |
using namespace ns3::nns3; |
| 1706 |
} |
| 1707 |
namespace ns { |
| 1708 |
void f() { |
| 1709 |
foo::^ |
| 1710 |
} |
| 1711 |
} |
| 1712 |
)cpp"); |
| 1713 |
|
| 1714 |
EXPECT_THAT(Requests, |
| 1715 |
ElementsAre(Field( |
| 1716 |
&FuzzyFindRequest::Scopes, |
| 1717 |
UnorderedElementsAre("foo::", "ns1::", "ns3::nns3::")))); |
| 1718 |
} |
| 1719 |
|
| 1720 |
TEST(CompletionTest, UnresolvedQualifierIdQuery) { |
| 1721 |
auto Requests = captureIndexRequests(R"cpp( |
| 1722 |
namespace a {} |
| 1723 |
using namespace a; |
| 1724 |
namespace ns { |
| 1725 |
void f() { |
| 1726 |
bar::^ |
| 1727 |
} |
| 1728 |
} // namespace ns |
| 1729 |
)cpp"); |
| 1730 |
|
| 1731 |
EXPECT_THAT(Requests, |
| 1732 |
ElementsAre(Field( |
| 1733 |
&FuzzyFindRequest::Scopes, |
| 1734 |
UnorderedElementsAre("a::bar::", "ns::bar::", "bar::")))); |
| 1735 |
} |
| 1736 |
|
| 1737 |
TEST(CompletionTest, UnresolvedNestedQualifierIdQuery) { |
| 1738 |
auto Requests = captureIndexRequests(R"cpp( |
| 1739 |
namespace a {} |
| 1740 |
using namespace a; |
| 1741 |
namespace ns { |
| 1742 |
void f() { |
| 1743 |
::a::bar::^ |
| 1744 |
} |
| 1745 |
} // namespace ns |
| 1746 |
)cpp"); |
| 1747 |
|
| 1748 |
EXPECT_THAT(Requests, ElementsAre(Field(&FuzzyFindRequest::Scopes, |
| 1749 |
UnorderedElementsAre("a::bar::")))); |
| 1750 |
} |
| 1751 |
|
| 1752 |
TEST(CompletionTest, EmptyQualifiedQuery) { |
| 1753 |
auto Requests = captureIndexRequests(R"cpp( |
| 1754 |
namespace ns { |
| 1755 |
void f() { |
| 1756 |
^ |
| 1757 |
} |
| 1758 |
} // namespace ns |
| 1759 |
)cpp"); |
| 1760 |
|
| 1761 |
EXPECT_THAT(Requests, ElementsAre(Field(&FuzzyFindRequest::Scopes, |
| 1762 |
UnorderedElementsAre("", "ns::")))); |
| 1763 |
} |
| 1764 |
|
| 1765 |
TEST(CompletionTest, GlobalQualifiedQuery) { |
| 1766 |
auto Requests = captureIndexRequests(R"cpp( |
| 1767 |
namespace ns { |
| 1768 |
void f() { |
| 1769 |
::^ |
| 1770 |
} |
| 1771 |
} // namespace ns |
| 1772 |
)cpp"); |
| 1773 |
|
| 1774 |
EXPECT_THAT(Requests, ElementsAre(Field(&FuzzyFindRequest::Scopes, |
| 1775 |
UnorderedElementsAre("")))); |
| 1776 |
} |
| 1777 |
|
| 1778 |
TEST(CompletionTest, NoDuplicatedQueryScopes) { |
| 1779 |
auto Requests = captureIndexRequests(R"cpp( |
| 1780 |
namespace {} |
| 1781 |
|
| 1782 |
namespace na { |
| 1783 |
namespace {} |
| 1784 |
namespace nb { |
| 1785 |
^ |
| 1786 |
} // namespace nb |
| 1787 |
} // namespace na |
| 1788 |
)cpp"); |
| 1789 |
|
| 1790 |
EXPECT_THAT(Requests, |
| 1791 |
ElementsAre(Field(&FuzzyFindRequest::Scopes, |
| 1792 |
UnorderedElementsAre("na::", "na::nb::", "")))); |
| 1793 |
} |
| 1794 |
|
| 1795 |
TEST(CompletionTest, NoIndexCompletionsInsideClasses) { |
| 1796 |
auto Completions = completions( |
| 1797 |
R"cpp( |
| 1798 |
struct Foo { |
| 1799 |
int SomeNameOfField; |
| 1800 |
typedef int SomeNameOfTypedefField; |
| 1801 |
}; |
| 1802 |
|
| 1803 |
Foo::^)cpp", |
| 1804 |
{func("::SomeNameInTheIndex"), func("::Foo::SomeNameInTheIndex")}); |
| 1805 |
|
| 1806 |
EXPECT_THAT(Completions.Completions, |
| 1807 |
AllOf(Contains(labeled("SomeNameOfField")), |
| 1808 |
Contains(labeled("SomeNameOfTypedefField")), |
| 1809 |
Not(Contains(labeled("SomeNameInTheIndex"))))); |
| 1810 |
} |
| 1811 |
|
| 1812 |
TEST(CompletionTest, NoIndexCompletionsInsideDependentCode) { |
| 1813 |
{ |
| 1814 |
auto Completions = completions( |
| 1815 |
R"cpp( |
| 1816 |
template <class T> |
| 1817 |
void foo() { |
| 1818 |
T::^ |
| 1819 |
} |
| 1820 |
)cpp", |
| 1821 |
{func("::SomeNameInTheIndex")}); |
| 1822 |
|
| 1823 |
EXPECT_THAT(Completions.Completions, |
| 1824 |
Not(Contains(labeled("SomeNameInTheIndex")))); |
| 1825 |
} |
| 1826 |
|
| 1827 |
{ |
| 1828 |
auto Completions = completions( |
| 1829 |
R"cpp( |
| 1830 |
template <class T> |
| 1831 |
void foo() { |
| 1832 |
T::template Y<int>::^ |
| 1833 |
} |
| 1834 |
)cpp", |
| 1835 |
{func("::SomeNameInTheIndex")}); |
| 1836 |
|
| 1837 |
EXPECT_THAT(Completions.Completions, |
| 1838 |
Not(Contains(labeled("SomeNameInTheIndex")))); |
| 1839 |
} |
| 1840 |
|
| 1841 |
{ |
| 1842 |
auto Completions = completions( |
| 1843 |
R"cpp( |
| 1844 |
template <class T> |
| 1845 |
void foo() { |
| 1846 |
T::foo::^ |
| 1847 |
} |
| 1848 |
)cpp", |
| 1849 |
{func("::SomeNameInTheIndex")}); |
| 1850 |
|
| 1851 |
EXPECT_THAT(Completions.Completions, |
| 1852 |
Not(Contains(labeled("SomeNameInTheIndex")))); |
| 1853 |
} |
| 1854 |
} |
| 1855 |
|
| 1856 |
TEST(CompletionTest, OverloadBundling) { |
| 1857 |
clangd::CodeCompleteOptions Opts; |
| 1858 |
Opts.BundleOverloads = true; |
| 1859 |
|
| 1860 |
std::string Context = R"cpp( |
| 1861 |
struct X { |
| 1862 |
// Overload with int |
| 1863 |
int a(int) __attribute__((deprecated("", ""))); |
| 1864 |
// Overload with bool |
| 1865 |
int a(bool); |
| 1866 |
int b(float); |
| 1867 |
|
| 1868 |
X(int); |
| 1869 |
X(float); |
| 1870 |
}; |
| 1871 |
int GFuncC(int); |
| 1872 |
int GFuncD(int); |
| 1873 |
)cpp"; |
| 1874 |
|
| 1875 |
// Member completions are bundled. |
| 1876 |
EXPECT_THAT(completions(Context + "int y = X().^", {}, Opts).Completions, |
| 1877 |
UnorderedElementsAre(labeled("a(…)"), labeled("b(float)"))); |
| 1878 |
|
| 1879 |
// Constructor completions are bundled. |
| 1880 |
EXPECT_THAT(completions(Context + "X z = X^", {}, Opts).Completions, |
| 1881 |
UnorderedElementsAre(labeled("X"), labeled("X(…)"))); |
| 1882 |
|
| 1883 |
// Non-member completions are bundled, including index+sema. |
| 1884 |
Symbol NoArgsGFunc = func("GFuncC"); |
| 1885 |
EXPECT_THAT( |
| 1886 |
completions(Context + "int y = GFunc^", {NoArgsGFunc}, Opts).Completions, |
| 1887 |
UnorderedElementsAre(labeled("GFuncC(…)"), labeled("GFuncD(int)"))); |
| 1888 |
|
| 1889 |
// Differences in header-to-insert suppress bundling. |
| 1890 |
std::string DeclFile = URI::create(testPath("foo")).toString(); |
| 1891 |
NoArgsGFunc.CanonicalDeclaration.FileURI = DeclFile.c_str(); |
| 1892 |
NoArgsGFunc.IncludeHeaders.emplace_back("<foo>", 1, Symbol::Include); |
| 1893 |
EXPECT_THAT( |
| 1894 |
completions(Context + "int y = GFunc^", {NoArgsGFunc}, Opts).Completions, |
| 1895 |
UnorderedElementsAre(AllOf(named("GFuncC"), insertInclude("<foo>")), |
| 1896 |
labeled("GFuncC(int)"), labeled("GFuncD(int)"))); |
| 1897 |
|
| 1898 |
// Examine a bundled completion in detail. |
| 1899 |
auto A = |
| 1900 |
completions(Context + "int y = X().a^", {}, Opts).Completions.front(); |
| 1901 |
EXPECT_EQ(A.Name, "a"); |
| 1902 |
EXPECT_EQ(A.Signature, "(…)"); |
| 1903 |
EXPECT_EQ(A.BundleSize, 2u); |
| 1904 |
EXPECT_EQ(A.Kind, CompletionItemKind::Method); |
| 1905 |
EXPECT_EQ(A.ReturnType, "int"); // All overloads return int. |
| 1906 |
// For now we just return one of the doc strings arbitrarily. |
| 1907 |
ASSERT_TRUE(A.Documentation); |
| 1908 |
ASSERT_FALSE(A.Deprecated); // Not all overloads deprecated. |
| 1909 |
EXPECT_THAT( |
| 1910 |
A.Documentation->asPlainText(), |
| 1911 |
AnyOf(HasSubstr("Overload with int"), HasSubstr("Overload with bool"))); |
| 1912 |
EXPECT_EQ(A.SnippetSuffix, "($0)"); |
| 1913 |
} |
| 1914 |
|
| 1915 |
TEST(CompletionTest, OverloadBundlingSameFileDifferentURI) { |
| 1916 |
clangd::CodeCompleteOptions Opts; |
| 1917 |
Opts.BundleOverloads = true; |
| 1918 |
|
| 1919 |
Symbol SymX = sym("ns::X", index::SymbolKind::Function, "@F@\\0#"); |
| 1920 |
Symbol SymY = sym("ns::X", index::SymbolKind::Function, "@F@\\0#I#"); |
| 1921 |
std::string BarHeader = testPath("bar.h"); |
| 1922 |
auto BarURI = URI::create(BarHeader).toString(); |
| 1923 |
SymX.CanonicalDeclaration.FileURI = BarURI.c_str(); |
| 1924 |
SymY.CanonicalDeclaration.FileURI = BarURI.c_str(); |
| 1925 |
// The include header is different, but really it's the same file. |
| 1926 |
SymX.IncludeHeaders.emplace_back("\"bar.h\"", 1, Symbol::Include); |
| 1927 |
SymY.IncludeHeaders.emplace_back(BarURI.c_str(), 1, Symbol::Include); |
| 1928 |
|
| 1929 |
auto Results = completions("void f() { ::ns::^ }", {SymX, SymY}, Opts); |
| 1930 |
// Expect both results are bundled, despite the different-but-same |
| 1931 |
// IncludeHeader. |
| 1932 |
ASSERT_EQ(1u, Results.Completions.size()); |
| 1933 |
const auto &R = Results.Completions.front(); |
| 1934 |
EXPECT_EQ("X", R.Name); |
| 1935 |
EXPECT_EQ(2u, R.BundleSize); |
| 1936 |
} |
| 1937 |
|
| 1938 |
TEST(CompletionTest, DocumentationFromChangedFileCrash) { |
| 1939 |
MockFS FS; |
| 1940 |
auto FooH = testPath("foo.h"); |
| 1941 |
auto FooCpp = testPath("foo.cpp"); |
| 1942 |
FS.Files[FooH] = R"cpp( |
| 1943 |
// this is my documentation comment. |
| 1944 |
int func(); |
| 1945 |
)cpp"; |
| 1946 |
FS.Files[FooCpp] = ""; |
| 1947 |
|
| 1948 |
MockCompilationDatabase CDB; |
| 1949 |
ClangdServer Server(CDB, FS, ClangdServer::optsForTest()); |
| 1950 |
|
| 1951 |
Annotations Source(R"cpp( |
| 1952 |
#include "foo.h" |
| 1953 |
int func() { |
| 1954 |
// This makes sure we have func from header in the AST. |
| 1955 |
} |
| 1956 |
int a = fun^ |
| 1957 |
)cpp"); |
| 1958 |
Server.addDocument(FooCpp, Source.code(), "null", WantDiagnostics::Yes); |
| 1959 |
// We need to wait for preamble to build. |
| 1960 |
ASSERT_TRUE(Server.blockUntilIdleForTest()); |
| 1961 |
|
| 1962 |
// Change the header file. Completion will reuse the old preamble! |
| 1963 |
FS.Files[FooH] = R"cpp( |
| 1964 |
int func(); |
| 1965 |
)cpp"; |
| 1966 |
|
| 1967 |
clangd::CodeCompleteOptions Opts; |
| 1968 |
CodeCompleteResult Completions = |
| 1969 |
cantFail(runCodeComplete(Server, FooCpp, Source.point(), Opts)); |
| 1970 |
// We shouldn't crash. Unfortunately, current workaround is to not produce |
| 1971 |
// comments for symbols from headers. |
| 1972 |
EXPECT_THAT(Completions.Completions, |
| 1973 |
Contains(AllOf(Not(isDocumented()), named("func")))); |
| 1974 |
} |
| 1975 |
|
| 1976 |
TEST(CompletionTest, NonDocComments) { |
| 1977 |
const char *Text = R"cpp( |
| 1978 |
// We ignore namespace comments, for rationale see CodeCompletionStrings.h. |
| 1979 |
namespace comments_ns { |
| 1980 |
} |
| 1981 |
|
| 1982 |
// ------------------ |
| 1983 |
int comments_foo(); |
| 1984 |
|
| 1985 |
// A comment and a decl are separated by newlines. |
| 1986 |
// Therefore, the comment shouldn't show up as doc comment. |
| 1987 |
|
| 1988 |
int comments_bar(); |
| 1989 |
|
| 1990 |
// this comment should be in the results. |
| 1991 |
int comments_baz(); |
| 1992 |
|
| 1993 |
|
| 1994 |
template <class T> |
| 1995 |
struct Struct { |
| 1996 |
int comments_qux(); |
| 1997 |
int comments_quux(); |
| 1998 |
}; |
| 1999 |
|
| 2000 |
|
| 2001 |
// This comment should not be there. |
| 2002 |
|
| 2003 |
template <class T> |
| 2004 |
int Struct<T>::comments_qux() { |
| 2005 |
} |
| 2006 |
|
| 2007 |
// This comment **should** be in results. |
| 2008 |
template <class T> |
| 2009 |
int Struct<T>::comments_quux() { |
| 2010 |
int a = comments^; |
| 2011 |
} |
| 2012 |
)cpp"; |
| 2013 |
|
| 2014 |
// We should not get any of those comments in completion. |
| 2015 |
EXPECT_THAT( |
| 2016 |
completions(Text).Completions, |
| 2017 |
UnorderedElementsAre(AllOf(Not(isDocumented()), named("comments_foo")), |
| 2018 |
AllOf(isDocumented(), named("comments_baz")), |
| 2019 |
AllOf(isDocumented(), named("comments_quux")), |
| 2020 |
AllOf(Not(isDocumented()), named("comments_ns")), |
| 2021 |
// FIXME(ibiryukov): the following items should have |
| 2022 |
// empty documentation, since they are separated from |
| 2023 |
// a comment with an empty line. Unfortunately, I |
| 2024 |
// couldn't make Sema tests pass if we ignore those. |
| 2025 |
AllOf(isDocumented(), named("comments_bar")), |
| 2026 |
AllOf(isDocumented(), named("comments_qux")))); |
| 2027 |
} |
| 2028 |
|
| 2029 |
TEST(CompletionTest, CompleteOnInvalidLine) { |
| 2030 |
auto FooCpp = testPath("foo.cpp"); |
| 2031 |
|
| 2032 |
MockCompilationDatabase CDB; |
| 2033 |
MockFS FS; |
| 2034 |
FS.Files[FooCpp] = "// empty file"; |
| 2035 |
|
| 2036 |
ClangdServer Server(CDB, FS, ClangdServer::optsForTest()); |
| 2037 |
// Run completion outside the file range. |
| 2038 |
Position Pos; |
| 2039 |
Pos.line = 100; |
| 2040 |
Pos.character = 0; |
| 2041 |
EXPECT_THAT_EXPECTED( |
| 2042 |
runCodeComplete(Server, FooCpp, Pos, clangd::CodeCompleteOptions()), |
| 2043 |
Failed()); |
| 2044 |
} |
| 2045 |
|
| 2046 |
TEST(CompletionTest, QualifiedNames) { |
| 2047 |
auto Results = completions( |
| 2048 |
R"cpp( |
| 2049 |
namespace ns { int local; void both(); } |
| 2050 |
void f() { ::ns::^ } |
| 2051 |
)cpp", |
| 2052 |
{func("ns::both"), cls("ns::Index")}); |
| 2053 |
// We get results from both index and sema, with no duplicates. |
| 2054 |
EXPECT_THAT( |
| 2055 |
Results.Completions, |
| 2056 |
UnorderedElementsAre(scope("ns::"), scope("ns::"), scope("ns::"))); |
| 2057 |
} |
| 2058 |
|
| 2059 |
TEST(CompletionTest, Render) { |
| 2060 |
CodeCompletion C; |
| 2061 |
C.Name = "x"; |
| 2062 |
C.FilterText = "x"; |
| 2063 |
C.Signature = "(bool) const"; |
| 2064 |
C.SnippetSuffix = "(${0:bool})"; |
| 2065 |
C.ReturnType = "int"; |
| 2066 |
C.RequiredQualifier = "Foo::"; |
| 2067 |
C.Scope = "ns::Foo::"; |
| 2068 |
C.Documentation.emplace(); |
| 2069 |
C.Documentation->addParagraph().appendText("This is ").appendCode("x()"); |
| 2070 |
C.Includes.emplace_back(); |
| 2071 |
auto &Include = C.Includes.back(); |
| 2072 |
Include.Header = "\"foo.h\""; |
| 2073 |
C.Kind = CompletionItemKind::Method; |
| 2074 |
C.Score.Total = 1.0; |
| 2075 |
C.Score.ExcludingName = .5; |
| 2076 |
C.Origin = SymbolOrigin::AST | SymbolOrigin::Static; |
| 2077 |
|
| 2078 |
CodeCompleteOptions Opts; |
| 2079 |
Opts.IncludeIndicator.Insert = "^"; |
| 2080 |
Opts.IncludeIndicator.NoInsert = ""; |
| 2081 |
Opts.EnableSnippets = false; |
| 2082 |
|
| 2083 |
auto R = C.render(Opts); |
| 2084 |
EXPECT_EQ(R.label, "Foo::x(bool) const"); |
| 2085 |
EXPECT_EQ(R.insertText, "Foo::x"); |
| 2086 |
EXPECT_EQ(R.insertTextFormat, InsertTextFormat::PlainText); |
| 2087 |
EXPECT_EQ(R.filterText, "x"); |
| 2088 |
EXPECT_EQ(R.detail, "int"); |
| 2089 |
EXPECT_EQ(R.documentation->value, "From \"foo.h\"\nThis is x()"); |
| 2090 |
EXPECT_THAT(R.additionalTextEdits, IsEmpty()); |
| 2091 |
EXPECT_EQ(R.sortText, sortText(1.0, "x")); |
| 2092 |
EXPECT_FALSE(R.deprecated); |
| 2093 |
EXPECT_EQ(R.score, .5f); |
| 2094 |
|
| 2095 |
C.FilterText = "xtra"; |
| 2096 |
R = C.render(Opts); |
| 2097 |
EXPECT_EQ(R.filterText, "xtra"); |
| 2098 |
EXPECT_EQ(R.sortText, sortText(1.0, "xtra")); |
| 2099 |
|
| 2100 |
Opts.EnableSnippets = true; |
| 2101 |
R = C.render(Opts); |
| 2102 |
EXPECT_EQ(R.insertText, "Foo::x(${0:bool})"); |
| 2103 |
EXPECT_EQ(R.insertTextFormat, InsertTextFormat::Snippet); |
| 2104 |
|
| 2105 |
C.SnippetSuffix = ""; |
| 2106 |
R = C.render(Opts); |
| 2107 |
EXPECT_EQ(R.insertText, "Foo::x"); |
| 2108 |
EXPECT_EQ(R.insertTextFormat, InsertTextFormat::PlainText); |
| 2109 |
|
| 2110 |
Include.Insertion.emplace(); |
| 2111 |
R = C.render(Opts); |
| 2112 |
EXPECT_EQ(R.label, "^Foo::x(bool) const"); |
| 2113 |
EXPECT_THAT(R.additionalTextEdits, Not(IsEmpty())); |
| 2114 |
|
| 2115 |
Opts.ShowOrigins = true; |
| 2116 |
R = C.render(Opts); |
| 2117 |
EXPECT_EQ(R.label, "^[AS]Foo::x(bool) const"); |
| 2118 |
|
| 2119 |
C.BundleSize = 2; |
| 2120 |
R = C.render(Opts); |
| 2121 |
EXPECT_EQ(R.detail, "[2 overloads]"); |
| 2122 |
EXPECT_EQ(R.documentation->value, "From \"foo.h\"\nThis is x()"); |
| 2123 |
|
| 2124 |
C.Deprecated = true; |
| 2125 |
R = C.render(Opts); |
| 2126 |
EXPECT_TRUE(R.deprecated); |
| 2127 |
|
| 2128 |
Opts.DocumentationFormat = MarkupKind::Markdown; |
| 2129 |
R = C.render(Opts); |
| 2130 |
EXPECT_EQ(R.documentation->value, "From `\"foo.h\"` \nThis is `x()`"); |
| 2131 |
} |
| 2132 |
|
| 2133 |
TEST(CompletionTest, IgnoreRecoveryResults) { |
| 2134 |
auto Results = completions( |
| 2135 |
R"cpp( |
| 2136 |
namespace ns { int NotRecovered() { return 0; } } |
| 2137 |
void f() { |
| 2138 |
// Sema enters recovery mode first and then normal mode. |
| 2139 |
if (auto x = ns::NotRecover^) |
| 2140 |
} |
| 2141 |
)cpp"); |
| 2142 |
EXPECT_THAT(Results.Completions, UnorderedElementsAre(named("NotRecovered"))); |
| 2143 |
} |
| 2144 |
|
| 2145 |
TEST(CompletionTest, ScopeOfClassFieldInConstructorInitializer) { |
| 2146 |
auto Results = completions( |
| 2147 |
R"cpp( |
| 2148 |
namespace ns { |
| 2149 |
class X { public: X(); int x_; }; |
| 2150 |
X::X() : x_^(0) {} |
| 2151 |
} |
| 2152 |
)cpp"); |
| 2153 |
EXPECT_THAT(Results.Completions, |
| 2154 |
UnorderedElementsAre(AllOf(scope("ns::X::"), named("x_")))); |
| 2155 |
} |
| 2156 |
|
| 2157 |
// Like other class members, constructor init lists have to parse what's below, |
| 2158 |
// after the completion point. |
| 2159 |
// But recovering from an incomplete constructor init list is particularly |
| 2160 |
// tricky because the bulk of the list is not surrounded by brackets. |
| 2161 |
TEST(CompletionTest, ConstructorInitListIncomplete) { |
| 2162 |
auto Results = completions( |
| 2163 |
R"cpp( |
| 2164 |
namespace ns { |
| 2165 |
struct X { |
| 2166 |
X() : x^ |
| 2167 |
int xyz_; |
| 2168 |
}; |
| 2169 |
} |
| 2170 |
)cpp"); |
| 2171 |
EXPECT_THAT(Results.Completions, ElementsAre(named("xyz_"))); |
| 2172 |
|
| 2173 |
Results = completions( |
| 2174 |
R"cpp( |
| 2175 |
int foo(); |
| 2176 |
|
| 2177 |
namespace ns { |
| 2178 |
struct X { |
| 2179 |
X() : xyz_(fo^ |
| 2180 |
int xyz_; |
| 2181 |
}; |
| 2182 |
} |
| 2183 |
)cpp"); |
| 2184 |
EXPECT_THAT(Results.Completions, ElementsAre(named("foo"))); |
| 2185 |
} |
| 2186 |
|
| 2187 |
TEST(CompletionTest, CodeCompletionContext) { |
| 2188 |
auto Results = completions( |
| 2189 |
R"cpp( |
| 2190 |
namespace ns { |
| 2191 |
class X { public: X(); int x_; }; |
| 2192 |
void f() { |
| 2193 |
X x; |
| 2194 |
x.^; |
| 2195 |
} |
| 2196 |
} |
| 2197 |
)cpp"); |
| 2198 |
|
| 2199 |
EXPECT_THAT(Results.Context, CodeCompletionContext::CCC_DotMemberAccess); |
| 2200 |
} |
| 2201 |
|
| 2202 |
TEST(CompletionTest, FixItForArrowToDot) { |
| 2203 |
MockFS FS; |
| 2204 |
MockCompilationDatabase CDB; |
| 2205 |
|
| 2206 |
CodeCompleteOptions Opts; |
| 2207 |
Opts.IncludeFixIts = true; |
| 2208 |
const char *Code = |
| 2209 |
R"cpp( |
| 2210 |
class Auxilary { |
| 2211 |
public: |
| 2212 |
void AuxFunction(); |
| 2213 |
}; |
| 2214 |
class ClassWithPtr { |
| 2215 |
public: |
| 2216 |
void MemberFunction(); |
| 2217 |
Auxilary* operator->() const; |
| 2218 |
Auxilary* Aux; |
| 2219 |
}; |
| 2220 |
void f() { |
| 2221 |
ClassWithPtr x; |
| 2222 |
x[[->]]^; |
| 2223 |
} |
| 2224 |
)cpp"; |
| 2225 |
auto Results = completions(Code, {}, Opts); |
| 2226 |
EXPECT_EQ(Results.Completions.size(), 3u); |
| 2227 |
|
| 2228 |
TextEdit ReplacementEdit; |
| 2229 |
ReplacementEdit.range = Annotations(Code).range(); |
| 2230 |
ReplacementEdit.newText = "."; |
| 2231 |
for (const auto &C : Results.Completions) { |
| 2232 |
EXPECT_TRUE(C.FixIts.size() == 1u || C.Name == "AuxFunction"); |
| 2233 |
if (!C.FixIts.empty()) { |
| 2234 |
EXPECT_THAT(C.FixIts, ElementsAre(ReplacementEdit)); |
| 2235 |
} |
| 2236 |
} |
| 2237 |
} |
| 2238 |
|
| 2239 |
TEST(CompletionTest, FixItForDotToArrow) { |
| 2240 |
CodeCompleteOptions Opts; |
| 2241 |
Opts.IncludeFixIts = true; |
| 2242 |
const char *Code = |
| 2243 |
R"cpp( |
| 2244 |
class Auxilary { |
| 2245 |
public: |
| 2246 |
void AuxFunction(); |
| 2247 |
}; |
| 2248 |
class ClassWithPtr { |
| 2249 |
public: |
| 2250 |
void MemberFunction(); |
| 2251 |
Auxilary* operator->() const; |
| 2252 |
Auxilary* Aux; |
| 2253 |
}; |
| 2254 |
void f() { |
| 2255 |
ClassWithPtr x; |
| 2256 |
x[[.]]^; |
| 2257 |
} |
| 2258 |
)cpp"; |
| 2259 |
auto Results = completions(Code, {}, Opts); |
| 2260 |
EXPECT_EQ(Results.Completions.size(), 3u); |
| 2261 |
|
| 2262 |
TextEdit ReplacementEdit; |
| 2263 |
ReplacementEdit.range = Annotations(Code).range(); |
| 2264 |
ReplacementEdit.newText = "->"; |
| 2265 |
for (const auto &C : Results.Completions) { |
| 2266 |
EXPECT_TRUE(C.FixIts.empty() || C.Name == "AuxFunction"); |
| 2267 |
if (!C.FixIts.empty()) { |
| 2268 |
EXPECT_THAT(C.FixIts, ElementsAre(ReplacementEdit)); |
| 2269 |
} |
| 2270 |
} |
| 2271 |
} |
| 2272 |
|
| 2273 |
TEST(CompletionTest, RenderWithFixItMerged) { |
| 2274 |
TextEdit FixIt; |
| 2275 |
FixIt.range.end.character = 5; |
| 2276 |
FixIt.newText = "->"; |
| 2277 |
|
| 2278 |
CodeCompletion C; |
| 2279 |
C.Name = "x"; |
| 2280 |
C.RequiredQualifier = "Foo::"; |
| 2281 |
C.FixIts = {FixIt}; |
| 2282 |
C.CompletionTokenRange.start.character = 5; |
| 2283 |
|
| 2284 |
CodeCompleteOptions Opts; |
| 2285 |
Opts.IncludeFixIts = true; |
| 2286 |
|
| 2287 |
auto R = C.render(Opts); |
| 2288 |
EXPECT_TRUE(R.textEdit); |
| 2289 |
EXPECT_EQ(R.textEdit->newText, "->Foo::x"); |
| 2290 |
EXPECT_TRUE(R.additionalTextEdits.empty()); |
| 2291 |
} |
| 2292 |
|
| 2293 |
TEST(CompletionTest, RenderWithFixItNonMerged) { |
| 2294 |
TextEdit FixIt; |
| 2295 |
FixIt.range.end.character = 4; |
| 2296 |
FixIt.newText = "->"; |
| 2297 |
|
| 2298 |
CodeCompletion C; |
| 2299 |
C.Name = "x"; |
| 2300 |
C.RequiredQualifier = "Foo::"; |
| 2301 |
C.FixIts = {FixIt}; |
| 2302 |
C.CompletionTokenRange.start.character = 5; |
| 2303 |
|
| 2304 |
CodeCompleteOptions Opts; |
| 2305 |
Opts.IncludeFixIts = true; |
| 2306 |
|
| 2307 |
auto R = C.render(Opts); |
| 2308 |
EXPECT_TRUE(R.textEdit); |
| 2309 |
EXPECT_EQ(R.textEdit->newText, "Foo::x"); |
| 2310 |
EXPECT_THAT(R.additionalTextEdits, UnorderedElementsAre(FixIt)); |
| 2311 |
} |
| 2312 |
|
| 2313 |
TEST(CompletionTest, CompletionTokenRange) { |
| 2314 |
MockFS FS; |
| 2315 |
MockCompilationDatabase CDB; |
| 2316 |
TestTU TU; |
| 2317 |
TU.AdditionalFiles["foo/abc/foo.h"] = ""; |
| 2318 |
|
| 2319 |
constexpr const char *TestCodes[] = { |
| 2320 |
R"cpp( |
| 2321 |
class Auxilary { |
| 2322 |
public: |
| 2323 |
void AuxFunction(); |
| 2324 |
}; |
| 2325 |
void f() { |
| 2326 |
Auxilary x; |
| 2327 |
x.[[Aux]]^; |
| 2328 |
} |
| 2329 |
)cpp", |
| 2330 |
R"cpp( |
| 2331 |
class Auxilary { |
| 2332 |
public: |
| 2333 |
void AuxFunction(); |
| 2334 |
}; |
| 2335 |
void f() { |
| 2336 |
Auxilary x; |
| 2337 |
x.[[]]^; |
| 2338 |
} |
| 2339 |
)cpp", |
| 2340 |
R"cpp( |
| 2341 |
#include "foo/[[a^/]]foo.h" |
| 2342 |
)cpp", |
| 2343 |
R"cpp( |
| 2344 |
#include "foo/abc/[[fo^o.h"]] |
| 2345 |
)cpp", |
| 2346 |
}; |
| 2347 |
for (const auto &Text : TestCodes) { |
| 2348 |
Annotations TestCode(Text); |
| 2349 |
TU.Code = TestCode.code().str(); |
| 2350 |
auto Results = completions(TU, TestCode.point()); |
| 2351 |
if (Results.Completions.size() != 1) { |
| 2352 |
ADD_FAILURE() << "Results.Completions.size() != 1" << Text; |
| 2353 |
continue; |
| 2354 |
} |
| 2355 |
EXPECT_THAT(Results.Completions.front().CompletionTokenRange, |
| 2356 |
TestCode.range()); |
| 2357 |
} |
| 2358 |
} |
| 2359 |
|
| 2360 |
TEST(SignatureHelpTest, OverloadsOrdering) { |
| 2361 |
const auto Results = signatures(R"cpp( |
| 2362 |
void foo(int x); |
| 2363 |
void foo(int x, float y); |
| 2364 |
void foo(float x, int y); |
| 2365 |
void foo(float x, float y); |
| 2366 |
void foo(int x, int y = 0); |
| 2367 |
int main() { foo(^); } |
| 2368 |
)cpp"); |
| 2369 |
EXPECT_THAT(Results.signatures, |
| 2370 |
ElementsAre(sig("foo([[int x]]) -> void"), |
| 2371 |
sig("foo([[int x]], [[int y = 0]]) -> void"), |
| 2372 |
sig("foo([[float x]], [[int y]]) -> void"), |
| 2373 |
sig("foo([[int x]], [[float y]]) -> void"), |
| 2374 |
sig("foo([[float x]], [[float y]]) -> void"))); |
| 2375 |
// We always prefer the first signature. |
| 2376 |
EXPECT_EQ(0, Results.activeSignature); |
| 2377 |
EXPECT_EQ(0, Results.activeParameter); |
| 2378 |
} |
| 2379 |
|
| 2380 |
TEST(SignatureHelpTest, InstantiatedSignatures) { |
| 2381 |
StringRef Sig0 = R"cpp( |
| 2382 |
template <class T> |
| 2383 |
void foo(T, T, T); |
| 2384 |
|
| 2385 |
int main() { |
| 2386 |
foo<int>(^); |
| 2387 |
} |
| 2388 |
)cpp"; |
| 2389 |
|
| 2390 |
EXPECT_THAT(signatures(Sig0).signatures, |
| 2391 |
ElementsAre(sig("foo([[T]], [[T]], [[T]]) -> void"))); |
| 2392 |
|
| 2393 |
StringRef Sig1 = R"cpp( |
| 2394 |
template <class T> |
| 2395 |
void foo(T, T, T); |
| 2396 |
|
| 2397 |
int main() { |
| 2398 |
foo(10, ^); |
| 2399 |
})cpp"; |
| 2400 |
|
| 2401 |
EXPECT_THAT(signatures(Sig1).signatures, |
| 2402 |
ElementsAre(sig("foo([[T]], [[T]], [[T]]) -> void"))); |
| 2403 |
|
| 2404 |
StringRef Sig2 = R"cpp( |
| 2405 |
template <class ...T> |
| 2406 |
void foo(T...); |
| 2407 |
|
| 2408 |
int main() { |
| 2409 |
foo<int>(^); |
| 2410 |
} |
| 2411 |
)cpp"; |
| 2412 |
|
| 2413 |
EXPECT_THAT(signatures(Sig2).signatures, |
| 2414 |
ElementsAre(sig("foo([[T...]]) -> void"))); |
| 2415 |
|
| 2416 |
// It is debatable whether we should substitute the outer template parameter |
| 2417 |
// ('T') in that case. Currently we don't substitute it in signature help, but |
| 2418 |
// do substitute in code complete. |
| 2419 |
// FIXME: make code complete and signature help consistent, figure out which |
| 2420 |
// way is better. |
| 2421 |
StringRef Sig3 = R"cpp( |
| 2422 |
template <class T> |
| 2423 |
struct X { |
| 2424 |
template <class U> |
| 2425 |
void foo(T, U); |
| 2426 |
}; |
| 2427 |
|
| 2428 |
int main() { |
| 2429 |
X<int>().foo<double>(^) |
| 2430 |
} |
| 2431 |
)cpp"; |
| 2432 |
|
| 2433 |
EXPECT_THAT(signatures(Sig3).signatures, |
| 2434 |
ElementsAre(sig("foo([[T]], [[U]]) -> void"))); |
| 2435 |
} |
| 2436 |
|
| 2437 |
TEST(SignatureHelpTest, IndexDocumentation) { |
| 2438 |
Symbol Foo0 = sym("foo", index::SymbolKind::Function, "@F@\\0#"); |
| 2439 |
Foo0.Documentation = "doc from the index"; |
| 2440 |
Symbol Foo1 = sym("foo", index::SymbolKind::Function, "@F@\\0#I#"); |
| 2441 |
Foo1.Documentation = "doc from the index"; |
| 2442 |
Symbol Foo2 = sym("foo", index::SymbolKind::Function, "@F@\\0#I#I#"); |
| 2443 |
|
| 2444 |
StringRef Sig0 = R"cpp( |
| 2445 |
int foo(); |
| 2446 |
int foo(double); |
| 2447 |
|
| 2448 |
void test() { |
| 2449 |
foo(^); |
| 2450 |
} |
| 2451 |
)cpp"; |
| 2452 |
|
| 2453 |
EXPECT_THAT( |
| 2454 |
signatures(Sig0, {Foo0}).signatures, |
| 2455 |
ElementsAre(AllOf(sig("foo() -> int"), sigDoc("doc from the index")), |
| 2456 |
AllOf(sig("foo([[double]]) -> int"), sigDoc("")))); |
| 2457 |
|
| 2458 |
StringRef Sig1 = R"cpp( |
| 2459 |
int foo(); |
| 2460 |
// Overriden doc from sema |
| 2461 |
int foo(int); |
| 2462 |
// doc from sema |
| 2463 |
int foo(int, int); |
| 2464 |
|
| 2465 |
void test() { |
| 2466 |
foo(^); |
| 2467 |
} |
| 2468 |
)cpp"; |
| 2469 |
|
| 2470 |
EXPECT_THAT( |
| 2471 |
signatures(Sig1, {Foo0, Foo1, Foo2}).signatures, |
| 2472 |
ElementsAre( |
| 2473 |
AllOf(sig("foo() -> int"), sigDoc("doc from the index")), |
| 2474 |
AllOf(sig("foo([[int]]) -> int"), sigDoc("Overriden doc from sema")), |
| 2475 |
AllOf(sig("foo([[int]], [[int]]) -> int"), sigDoc("doc from sema")))); |
| 2476 |
} |
| 2477 |
|
| 2478 |
TEST(SignatureHelpTest, DynamicIndexDocumentation) { |
| 2479 |
MockFS FS; |
| 2480 |
MockCompilationDatabase CDB; |
| 2481 |
ClangdServer::Options Opts = ClangdServer::optsForTest(); |
| 2482 |
Opts.BuildDynamicSymbolIndex = true; |
| 2483 |
ClangdServer Server(CDB, FS, Opts); |
| 2484 |
|
| 2485 |
FS.Files[testPath("foo.h")] = R"cpp( |
| 2486 |
struct Foo { |
| 2487 |
// Member doc |
| 2488 |
int foo(); |
| 2489 |
}; |
| 2490 |
)cpp"; |
| 2491 |
Annotations FileContent(R"cpp( |
| 2492 |
#include "foo.h" |
| 2493 |
void test() { |
| 2494 |
Foo f; |
| 2495 |
f.foo(^); |
| 2496 |
} |
| 2497 |
)cpp"); |
| 2498 |
auto File = testPath("test.cpp"); |
| 2499 |
Server.addDocument(File, FileContent.code()); |
| 2500 |
// Wait for the dynamic index being built. |
| 2501 |
ASSERT_TRUE(Server.blockUntilIdleForTest()); |
| 2502 |
EXPECT_THAT(llvm::cantFail(runSignatureHelp(Server, File, FileContent.point(), |
| 2503 |
MarkupKind::PlainText)) |
| 2504 |
.signatures, |
| 2505 |
ElementsAre(AllOf(sig("foo() -> int"), sigDoc("Member doc")))); |
| 2506 |
} |
| 2507 |
|
| 2508 |
TEST(CompletionTest, CompletionFunctionArgsDisabled) { |
| 2509 |
CodeCompleteOptions Opts; |
| 2510 |
Opts.EnableSnippets = true; |
| 2511 |
Opts.EnableFunctionArgSnippets = false; |
| 2512 |
|
| 2513 |
{ |
| 2514 |
auto Results = completions( |
| 2515 |
R"cpp( |
| 2516 |
void xfoo(); |
| 2517 |
void xfoo(int x, int y); |
| 2518 |
void f() { xfo^ })cpp", |
| 2519 |
{}, Opts); |
| 2520 |
EXPECT_THAT( |
| 2521 |
Results.Completions, |
| 2522 |
UnorderedElementsAre(AllOf(named("xfoo"), snippetSuffix("()")), |
| 2523 |
AllOf(named("xfoo"), snippetSuffix("($0)")))); |
| 2524 |
} |
| 2525 |
{ |
| 2526 |
auto Results = completions( |
| 2527 |
R"cpp( |
| 2528 |
void xbar(); |
| 2529 |
void f() { xba^ })cpp", |
| 2530 |
{}, Opts); |
| 2531 |
EXPECT_THAT(Results.Completions, UnorderedElementsAre(AllOf( |
| 2532 |
named("xbar"), snippetSuffix("()")))); |
| 2533 |
} |
| 2534 |
{ |
| 2535 |
Opts.BundleOverloads = true; |
| 2536 |
auto Results = completions( |
| 2537 |
R"cpp( |
| 2538 |
void xfoo(); |
| 2539 |
void xfoo(int x, int y); |
| 2540 |
void f() { xfo^ })cpp", |
| 2541 |
{}, Opts); |
| 2542 |
EXPECT_THAT( |
| 2543 |
Results.Completions, |
| 2544 |
UnorderedElementsAre(AllOf(named("xfoo"), snippetSuffix("($0)")))); |
| 2545 |
} |
| 2546 |
{ |
| 2547 |
auto Results = completions( |
| 2548 |
R"cpp( |
| 2549 |
template <class T, class U> |
| 2550 |
void xfoo(int a, U b); |
| 2551 |
void f() { xfo^ })cpp", |
| 2552 |
{}, Opts); |
| 2553 |
EXPECT_THAT( |
| 2554 |
Results.Completions, |
| 2555 |
UnorderedElementsAre(AllOf(named("xfoo"), snippetSuffix("<$1>($0)")))); |
| 2556 |
} |
| 2557 |
{ |
| 2558 |
auto Results = completions( |
| 2559 |
R"cpp( |
| 2560 |
template <class T> |
| 2561 |
class foo_class{}; |
| 2562 |
template <class T> |
| 2563 |
using foo_alias = T**; |
| 2564 |
void f() { foo_^ })cpp", |
| 2565 |
{}, Opts); |
| 2566 |
EXPECT_THAT( |
| 2567 |
Results.Completions, |
| 2568 |
UnorderedElementsAre(AllOf(named("foo_class"), snippetSuffix("<$0>")), |
| 2569 |
AllOf(named("foo_alias"), snippetSuffix("<$0>")))); |
| 2570 |
} |
| 2571 |
{ |
| 2572 |
auto Results = completions( |
| 2573 |
R"cpp( |
| 2574 |
#define FOO(x, y) x##f |
| 2575 |
FO^ )cpp", |
| 2576 |
{}, Opts); |
| 2577 |
EXPECT_THAT(Results.Completions, UnorderedElementsAre(AllOf( |
| 2578 |
named("FOO"), snippetSuffix("($0)")))); |
| 2579 |
} |
| 2580 |
} |
| 2581 |
|
| 2582 |
TEST(CompletionTest, SuggestOverrides) { |
| 2583 |
constexpr const char *const Text(R"cpp( |
| 2584 |
class A { |
| 2585 |
public: |
| 2586 |
virtual void vfunc(bool param); |
| 2587 |
virtual void vfunc(bool param, int p); |
| 2588 |
void func(bool param); |
| 2589 |
}; |
| 2590 |
class B : public A { |
| 2591 |
virtual void ttt(bool param) const; |
| 2592 |
void vfunc(bool param, int p) override; |
| 2593 |
}; |
| 2594 |
class C : public B { |
| 2595 |
public: |
| 2596 |
void vfunc(bool param) override; |
| 2597 |
^ |
| 2598 |
}; |
| 2599 |
)cpp"); |
| 2600 |
const auto Results = completions(Text); |
| 2601 |
EXPECT_THAT( |
| 2602 |
Results.Completions, |
| 2603 |
AllOf(Contains(AllOf(labeled("void vfunc(bool param, int p) override"), |
| 2604 |
nameStartsWith("vfunc"))), |
| 2605 |
Contains(AllOf(labeled("void ttt(bool param) const override"), |
| 2606 |
nameStartsWith("ttt"))), |
| 2607 |
Not(Contains(labeled("void vfunc(bool param) override"))))); |
| 2608 |
} |
| 2609 |
|
| 2610 |
TEST(CompletionTest, OverridesNonIdentName) { |
| 2611 |
// Check the completions call does not crash. |
| 2612 |
completions(R"cpp( |
| 2613 |
struct Base { |
| 2614 |
virtual ~Base() = 0; |
| 2615 |
virtual operator int() = 0; |
| 2616 |
virtual Base& operator+(Base&) = 0; |
| 2617 |
}; |
| 2618 |
|
| 2619 |
struct Derived : Base { |
| 2620 |
^ |
| 2621 |
}; |
| 2622 |
)cpp"); |
| 2623 |
} |
| 2624 |
|
| 2625 |
TEST(CompletionTest, NoCrashOnMissingNewLineAtEOF) { |
| 2626 |
auto FooCpp = testPath("foo.cpp"); |
| 2627 |
|
| 2628 |
MockCompilationDatabase CDB; |
| 2629 |
MockFS FS; |
| 2630 |
Annotations F("#pragma ^ // no new line"); |
| 2631 |
FS.Files[FooCpp] = F.code().str(); |
| 2632 |
ClangdServer Server(CDB, FS, ClangdServer::optsForTest()); |
| 2633 |
runAddDocument(Server, FooCpp, F.code()); |
| 2634 |
// Run completion outside the file range. |
| 2635 |
EXPECT_THAT(cantFail(runCodeComplete(Server, FooCpp, F.point(), |
| 2636 |
clangd::CodeCompleteOptions())) |
| 2637 |
.Completions, |
| 2638 |
IsEmpty()); |
| 2639 |
EXPECT_THAT(cantFail(runSignatureHelp(Server, FooCpp, F.point(), |
| 2640 |
MarkupKind::PlainText)) |
| 2641 |
.signatures, |
| 2642 |
IsEmpty()); |
| 2643 |
} |
| 2644 |
|
| 2645 |
TEST(GuessCompletionPrefix, Filters) { |
| 2646 |
for (llvm::StringRef Case : { |
| 2647 |
"[[scope::]][[ident]]^", |
| 2648 |
"[[]][[]]^", |
| 2649 |
"\n[[]][[]]^", |
| 2650 |
"[[]][[ab]]^", |
| 2651 |
"x.[[]][[ab]]^", |
| 2652 |
"x.[[]][[]]^", |
| 2653 |
"[[x::]][[ab]]^", |
| 2654 |
"[[x::]][[]]^", |
| 2655 |
"[[::x::]][[ab]]^", |
| 2656 |
"some text [[scope::more::]][[identif]]^ier", |
| 2657 |
"some text [[scope::]][[mor]]^e::identifier", |
| 2658 |
"weird case foo::[[::bar::]][[baz]]^", |
| 2659 |
"/* [[]][[]]^ */", |
| 2660 |
}) { |
| 2661 |
Annotations F(Case); |
| 2662 |
auto Offset = cantFail(positionToOffset(F.code(), F.point())); |
| 2663 |
auto ToStringRef = [&](Range R) { |
| 2664 |
return F.code().slice(cantFail(positionToOffset(F.code(), R.start)), |
| 2665 |
cantFail(positionToOffset(F.code(), R.end))); |
| 2666 |
}; |
| 2667 |
auto WantQualifier = ToStringRef(F.ranges()[0]), |
| 2668 |
WantName = ToStringRef(F.ranges()[1]); |
| 2669 |
|
| 2670 |
auto Prefix = guessCompletionPrefix(F.code(), Offset); |
| 2671 |
// Even when components are empty, check their offsets are correct. |
| 2672 |
EXPECT_EQ(WantQualifier, Prefix.Qualifier) << Case; |
| 2673 |
EXPECT_EQ(WantQualifier.begin(), Prefix.Qualifier.begin()) << Case; |
| 2674 |
EXPECT_EQ(WantName, Prefix.Name) << Case; |
| 2675 |
EXPECT_EQ(WantName.begin(), Prefix.Name.begin()) << Case; |
| 2676 |
} |
| 2677 |
} |
| 2678 |
|
| 2679 |
TEST(CompletionTest, EnableSpeculativeIndexRequest) { |
| 2680 |
MockFS FS; |
| 2681 |
MockCompilationDatabase CDB; |
| 2682 |
ClangdServer Server(CDB, FS, ClangdServer::optsForTest()); |
| 2683 |
|
| 2684 |
auto File = testPath("foo.cpp"); |
| 2685 |
Annotations Test(R"cpp( |
| 2686 |
namespace ns1 { int abc; } |
| 2687 |
namespace ns2 { int abc; } |
| 2688 |
void f() { ns1::ab$1^; ns1::ab$2^; } |
| 2689 |
void f2() { ns2::ab$3^; } |
| 2690 |
)cpp"); |
| 2691 |
runAddDocument(Server, File, Test.code()); |
| 2692 |
clangd::CodeCompleteOptions Opts = {}; |
| 2693 |
|
| 2694 |
IndexRequestCollector Requests; |
| 2695 |
Opts.Index = &Requests; |
| 2696 |
|
| 2697 |
auto CompleteAtPoint = [&](StringRef P) { |
| 2698 |
auto CCR = cantFail(runCodeComplete(Server, File, Test.point(P), Opts)); |
| 2699 |
EXPECT_TRUE(CCR.HasMore); |
| 2700 |
}; |
| 2701 |
|
| 2702 |
CompleteAtPoint("1"); |
| 2703 |
auto Reqs1 = Requests.consumeRequests(1); |
| 2704 |
ASSERT_EQ(Reqs1.size(), 1u); |
| 2705 |
EXPECT_THAT(Reqs1[0].Scopes, UnorderedElementsAre("ns1::")); |
| 2706 |
|
| 2707 |
CompleteAtPoint("2"); |
| 2708 |
auto Reqs2 = Requests.consumeRequests(1); |
| 2709 |
// Speculation succeeded. Used speculative index result. |
| 2710 |
ASSERT_EQ(Reqs2.size(), 1u); |
| 2711 |
EXPECT_EQ(Reqs2[0], Reqs1[0]); |
| 2712 |
|
| 2713 |
CompleteAtPoint("3"); |
| 2714 |
// Speculation failed. Sent speculative index request and the new index |
| 2715 |
// request after sema. |
| 2716 |
auto Reqs3 = Requests.consumeRequests(2); |
| 2717 |
ASSERT_EQ(Reqs3.size(), 2u); |
| 2718 |
} |
| 2719 |
|
| 2720 |
TEST(CompletionTest, InsertTheMostPopularHeader) { |
| 2721 |
std::string DeclFile = URI::create(testPath("foo")).toString(); |
| 2722 |
Symbol Sym = func("Func"); |
| 2723 |
Sym.CanonicalDeclaration.FileURI = DeclFile.c_str(); |
| 2724 |
Sym.IncludeHeaders.emplace_back("\"foo.h\"", 2, Symbol::Include); |
| 2725 |
Sym.IncludeHeaders.emplace_back("\"bar.h\"", 1000, Symbol::Include); |
| 2726 |
|
| 2727 |
auto Results = completions("Fun^", {Sym}).Completions; |
| 2728 |
assert(!Results.empty()); |
| 2729 |
EXPECT_THAT(Results[0], AllOf(named("Func"), insertInclude("\"bar.h\""))); |
| 2730 |
EXPECT_EQ(Results[0].Includes.size(), 2u); |
| 2731 |
} |
| 2732 |
|
| 2733 |
TEST(CompletionTest, InsertIncludeOrImport) { |
| 2734 |
std::string DeclFile = URI::create(testPath("foo")).toString(); |
| 2735 |
Symbol Sym = func("Func"); |
| 2736 |
Sym.CanonicalDeclaration.FileURI = DeclFile.c_str(); |
| 2737 |
Sym.IncludeHeaders.emplace_back("\"bar.h\"", 1000, |
| 2738 |
Symbol::Include | Symbol::Import); |
| 2739 |
CodeCompleteOptions Opts; |
| 2740 |
// Should only take effect in import contexts. |
| 2741 |
Opts.ImportInsertions = true; |
| 2742 |
auto Results = completions("Fun^", {Sym}, Opts).Completions; |
| 2743 |
assert(!Results.empty()); |
| 2744 |
EXPECT_THAT(Results[0], |
| 2745 |
AllOf(named("Func"), insertIncludeText("#include \"bar.h\"\n"))); |
| 2746 |
|
| 2747 |
ASTSignals Signals; |
| 2748 |
Signals.InsertionDirective = Symbol::IncludeDirective::Import; |
| 2749 |
Opts.MainFileSignals = &Signals; |
| 2750 |
Results = completions("Fun^", {Sym}, Opts, "Foo.m").Completions; |
| 2751 |
assert(!Results.empty()); |
| 2752 |
EXPECT_THAT(Results[0], |
| 2753 |
AllOf(named("Func"), insertIncludeText("#import \"bar.h\"\n"))); |
| 2754 |
|
| 2755 |
Sym.IncludeHeaders[0].SupportedDirectives = Symbol::Import; |
| 2756 |
Results = completions("Fun^", {Sym}).Completions; |
| 2757 |
assert(!Results.empty()); |
| 2758 |
EXPECT_THAT(Results[0], AllOf(named("Func"), Not(insertInclude()))); |
| 2759 |
} |
| 2760 |
|
| 2761 |
TEST(CompletionTest, NoInsertIncludeIfOnePresent) { |
| 2762 |
Annotations Test(R"cpp( |
| 2763 |
#include "foo.h" |
| 2764 |
Fun^ |
| 2765 |
)cpp"); |
| 2766 |
auto TU = TestTU::withCode(Test.code()); |
| 2767 |
TU.AdditionalFiles["foo.h"] = ""; |
| 2768 |
|
| 2769 |
std::string DeclFile = URI::create(testPath("foo")).toString(); |
| 2770 |
Symbol Sym = func("Func"); |
| 2771 |
Sym.CanonicalDeclaration.FileURI = DeclFile.c_str(); |
| 2772 |
Sym.IncludeHeaders.emplace_back("\"foo.h\"", 2, Symbol::Include); |
| 2773 |
Sym.IncludeHeaders.emplace_back("\"bar.h\"", 1000, Symbol::Include); |
| 2774 |
|
| 2775 |
EXPECT_THAT(completions(TU, Test.point(), {Sym}).Completions, |
| 2776 |
UnorderedElementsAre(AllOf(named("Func"), hasInclude("\"foo.h\""), |
| 2777 |
Not(insertInclude())))); |
| 2778 |
} |
| 2779 |
|
| 2780 |
TEST(CompletionTest, MergeMacrosFromIndexAndSema) { |
| 2781 |
Symbol Sym; |
| 2782 |
Sym.Name = "Clangd_Macro_Test"; |
| 2783 |
Sym.ID = SymbolID("c:foo.cpp@8@macro@Clangd_Macro_Test"); |
| 2784 |
Sym.SymInfo.Kind = index::SymbolKind::Macro; |
| 2785 |
Sym.Flags |= Symbol::IndexedForCodeCompletion; |
| 2786 |
EXPECT_THAT(completions("#define Clangd_Macro_Test\nClangd_Macro_T^", {Sym}) |
| 2787 |
.Completions, |
| 2788 |
UnorderedElementsAre(named("Clangd_Macro_Test"))); |
| 2789 |
} |
| 2790 |
|
| 2791 |
TEST(CompletionTest, MacroFromPreamble) { |
| 2792 |
Annotations Test(R"cpp(#define CLANGD_PREAMBLE_MAIN x |
| 2793 |
|
| 2794 |
int x = 0; |
| 2795 |
#define CLANGD_MAIN x |
| 2796 |
void f() { CLANGD_^ } |
| 2797 |
)cpp"); |
| 2798 |
auto TU = TestTU::withCode(Test.code()); |
| 2799 |
TU.HeaderCode = "#define CLANGD_PREAMBLE_HEADER x"; |
| 2800 |
auto Results = completions(TU, Test.point(), {func("CLANGD_INDEX")}); |
| 2801 |
// We should get results from the main file, including the preamble section. |
| 2802 |
// However no results from included files (the index should cover them). |
| 2803 |
EXPECT_THAT(Results.Completions, |
| 2804 |
UnorderedElementsAre(named("CLANGD_PREAMBLE_MAIN"), |
| 2805 |
named("CLANGD_MAIN"), |
| 2806 |
named("CLANGD_INDEX"))); |
| 2807 |
} |
| 2808 |
|
| 2809 |
TEST(CompletionTest, DeprecatedResults) { |
| 2810 |
std::string Body = R"cpp( |
| 2811 |
void TestClangd(); |
| 2812 |
void TestClangc() __attribute__((deprecated("", ""))); |
| 2813 |
)cpp"; |
| 2814 |
|
| 2815 |
EXPECT_THAT( |
| 2816 |
completions(Body + "int main() { TestClang^ }").Completions, |
| 2817 |
UnorderedElementsAre(AllOf(named("TestClangd"), Not(deprecated())), |
| 2818 |
AllOf(named("TestClangc"), deprecated()))); |
| 2819 |
} |
| 2820 |
|
| 2821 |
TEST(SignatureHelpTest, PartialSpec) { |
| 2822 |
const auto Results = signatures(R"cpp( |
| 2823 |
template <typename T> struct Foo {}; |
| 2824 |
template <typename T> struct Foo<T*> { Foo(T); }; |
| 2825 |
Foo<int*> F(^);)cpp"); |
| 2826 |
EXPECT_THAT(Results.signatures, Contains(sig("Foo([[T]])"))); |
| 2827 |
EXPECT_EQ(0, Results.activeParameter); |
| 2828 |
} |
| 2829 |
|
| 2830 |
TEST(SignatureHelpTest, InsideArgument) { |
| 2831 |
{ |
| 2832 |
const auto Results = signatures(R"cpp( |
| 2833 |
void foo(int x); |
| 2834 |
void foo(int x, int y); |
| 2835 |
int main() { foo(1+^); } |
| 2836 |
)cpp"); |
| 2837 |
EXPECT_THAT(Results.signatures, |
| 2838 |
ElementsAre(sig("foo([[int x]]) -> void"), |
| 2839 |
sig("foo([[int x]], [[int y]]) -> void"))); |
| 2840 |
EXPECT_EQ(0, Results.activeParameter); |
| 2841 |
} |
| 2842 |
{ |
| 2843 |
const auto Results = signatures(R"cpp( |
| 2844 |
void foo(int x); |
| 2845 |
void foo(int x, int y); |
| 2846 |
int main() { foo(1^); } |
| 2847 |
)cpp"); |
| 2848 |
EXPECT_THAT(Results.signatures, |
| 2849 |
ElementsAre(sig("foo([[int x]]) -> void"), |
| 2850 |
sig("foo([[int x]], [[int y]]) -> void"))); |
| 2851 |
EXPECT_EQ(0, Results.activeParameter); |
| 2852 |
} |
| 2853 |
{ |
| 2854 |
const auto Results = signatures(R"cpp( |
| 2855 |
void foo(int x); |
| 2856 |
void foo(int x, int y); |
| 2857 |
int main() { foo(1^0); } |
| 2858 |
)cpp"); |
| 2859 |
EXPECT_THAT(Results.signatures, |
| 2860 |
ElementsAre(sig("foo([[int x]]) -> void"), |
| 2861 |
sig("foo([[int x]], [[int y]]) -> void"))); |
| 2862 |
EXPECT_EQ(0, Results.activeParameter); |
| 2863 |
} |
| 2864 |
{ |
| 2865 |
const auto Results = signatures(R"cpp( |
| 2866 |
void foo(int x); |
| 2867 |
void foo(int x, int y); |
| 2868 |
int bar(int x, int y); |
| 2869 |
int main() { bar(foo(2, 3^)); } |
| 2870 |
)cpp"); |
| 2871 |
EXPECT_THAT(Results.signatures, |
| 2872 |
ElementsAre(sig("foo([[int x]], [[int y]]) -> void"))); |
| 2873 |
EXPECT_EQ(1, Results.activeParameter); |
| 2874 |
} |
| 2875 |
} |
| 2876 |
|
| 2877 |
TEST(SignatureHelpTest, ConstructorInitializeFields) { |
| 2878 |
{ |
| 2879 |
const auto Results = signatures(R"cpp( |
| 2880 |
struct A { A(int); }; |
| 2881 |
struct B { |
| 2882 |
B() : a_elem(^) {} |
| 2883 |
A a_elem; |
| 2884 |
}; |
| 2885 |
)cpp"); |
| 2886 |
EXPECT_THAT(Results.signatures, |
| 2887 |
UnorderedElementsAre(sig("A([[int]])"), sig("A([[A &&]])"), |
| 2888 |
sig("A([[const A &]])"))); |
| 2889 |
} |
| 2890 |
{ |
| 2891 |
const auto Results = signatures(R"cpp( |
| 2892 |
struct A { A(int); }; |
| 2893 |
struct B { |
| 2894 |
B() : a_elem(^ |
| 2895 |
A a_elem; |
| 2896 |
}; |
| 2897 |
)cpp"); |
| 2898 |
// FIXME: currently the parser skips over the decl of a_elem as part of the |
| 2899 |
// (broken) init list, so we don't get signatures for the first member. |
| 2900 |
EXPECT_THAT(Results.signatures, IsEmpty()); |
| 2901 |
} |
| 2902 |
{ |
| 2903 |
const auto Results = signatures(R"cpp( |
| 2904 |
struct A { A(int); }; |
| 2905 |
struct B { |
| 2906 |
B() : a_elem(^ |
| 2907 |
int dummy_elem; |
| 2908 |
A a_elem; |
| 2909 |
}; |
| 2910 |
)cpp"); |
| 2911 |
EXPECT_THAT(Results.signatures, |
| 2912 |
UnorderedElementsAre(sig("A([[int]])"), sig("A([[A &&]])"), |
| 2913 |
sig("A([[const A &]])"))); |
| 2914 |
} |
| 2915 |
{ |
| 2916 |
const auto Results = signatures(R"cpp( |
| 2917 |
struct A { |
| 2918 |
A(int); |
| 2919 |
}; |
| 2920 |
struct C { |
| 2921 |
C(int); |
| 2922 |
C(A); |
| 2923 |
}; |
| 2924 |
struct B { |
| 2925 |
B() : c_elem(A(1^)) {} |
| 2926 |
C c_elem; |
| 2927 |
}; |
| 2928 |
)cpp"); |
| 2929 |
EXPECT_THAT(Results.signatures, |
| 2930 |
UnorderedElementsAre(sig("A([[int]])"), sig("A([[A &&]])"), |
| 2931 |
sig("A([[const A &]])"))); |
| 2932 |
} |
| 2933 |
} |
| 2934 |
|
| 2935 |
TEST(SignatureHelpTest, Variadic) { |
| 2936 |
const std::string Header = R"cpp( |
| 2937 |
void fun(int x, ...) {} |
| 2938 |
void test() {)cpp"; |
| 2939 |
const std::string ExpectedSig = "fun([[int x]], [[...]]) -> void"; |
| 2940 |
|
| 2941 |
{ |
| 2942 |
const auto Result = signatures(Header + "fun(^);}"); |
| 2943 |
EXPECT_EQ(0, Result.activeParameter); |
| 2944 |
EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig))); |
| 2945 |
} |
| 2946 |
{ |
| 2947 |
const auto Result = signatures(Header + "fun(1, ^);}"); |
| 2948 |
EXPECT_EQ(1, Result.activeParameter); |
| 2949 |
EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig))); |
| 2950 |
} |
| 2951 |
{ |
| 2952 |
const auto Result = signatures(Header + "fun(1, 2, ^);}"); |
| 2953 |
EXPECT_EQ(1, Result.activeParameter); |
| 2954 |
EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig))); |
| 2955 |
} |
| 2956 |
} |
| 2957 |
|
| 2958 |
TEST(SignatureHelpTest, VariadicTemplate) { |
| 2959 |
const std::string Header = R"cpp( |
| 2960 |
template<typename T, typename ...Args> |
| 2961 |
void fun(T t, Args ...args) {} |
| 2962 |
void test() {)cpp"; |
| 2963 |
const std::string ExpectedSig = "fun([[T t]], [[Args args...]]) -> void"; |
| 2964 |
|
| 2965 |
{ |
| 2966 |
const auto Result = signatures(Header + "fun(^);}"); |
| 2967 |
EXPECT_EQ(0, Result.activeParameter); |
| 2968 |
EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig))); |
| 2969 |
} |
| 2970 |
{ |
| 2971 |
const auto Result = signatures(Header + "fun(1, ^);}"); |
| 2972 |
EXPECT_EQ(1, Result.activeParameter); |
| 2973 |
EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig))); |
| 2974 |
} |
| 2975 |
{ |
| 2976 |
const auto Result = signatures(Header + "fun(1, 2, ^);}"); |
| 2977 |
EXPECT_EQ(1, Result.activeParameter); |
| 2978 |
EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig))); |
| 2979 |
} |
| 2980 |
} |
| 2981 |
|
| 2982 |
TEST(SignatureHelpTest, VariadicMethod) { |
| 2983 |
const std::string Header = R"cpp( |
| 2984 |
class C { |
| 2985 |
template<typename T, typename ...Args> |
| 2986 |
void fun(T t, Args ...args) {} |
| 2987 |
}; |
| 2988 |
void test() {C c; )cpp"; |
| 2989 |
const std::string ExpectedSig = "fun([[T t]], [[Args args...]]) -> void"; |
| 2990 |
|
| 2991 |
{ |
| 2992 |
const auto Result = signatures(Header + "c.fun(^);}"); |
| 2993 |
EXPECT_EQ(0, Result.activeParameter); |
| 2994 |
EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig))); |
| 2995 |
} |
| 2996 |
{ |
| 2997 |
const auto Result = signatures(Header + "c.fun(1, ^);}"); |
| 2998 |
EXPECT_EQ(1, Result.activeParameter); |
| 2999 |
EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig))); |
| 3000 |
} |
| 3001 |
{ |
| 3002 |
const auto Result = signatures(Header + "c.fun(1, 2, ^);}"); |
| 3003 |
EXPECT_EQ(1, Result.activeParameter); |
| 3004 |
EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig))); |
| 3005 |
} |
| 3006 |
} |
| 3007 |
|
| 3008 |
TEST(SignatureHelpTest, VariadicType) { |
| 3009 |
const std::string Header = R"cpp( |
| 3010 |
void fun(int x, ...) {} |
| 3011 |
auto get_fun() { return fun; } |
| 3012 |
void test() { |
| 3013 |
)cpp"; |
| 3014 |
const std::string ExpectedSig = "([[int]], [[...]]) -> void"; |
| 3015 |
|
| 3016 |
{ |
| 3017 |
const auto Result = signatures(Header + "get_fun()(^);}"); |
| 3018 |
EXPECT_EQ(0, Result.activeParameter); |
| 3019 |
EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig))); |
| 3020 |
} |
| 3021 |
{ |
| 3022 |
const auto Result = signatures(Header + "get_fun()(1, ^);}"); |
| 3023 |
EXPECT_EQ(1, Result.activeParameter); |
| 3024 |
EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig))); |
| 3025 |
} |
| 3026 |
{ |
| 3027 |
const auto Result = signatures(Header + "get_fun()(1, 2, ^);}"); |
| 3028 |
EXPECT_EQ(1, Result.activeParameter); |
| 3029 |
EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig))); |
| 3030 |
} |
| 3031 |
} |
| 3032 |
|
| 3033 |
TEST(CompletionTest, IncludedCompletionKinds) { |
| 3034 |
Annotations Test(R"cpp(#include "^)cpp"); |
| 3035 |
auto TU = TestTU::withCode(Test.code()); |
| 3036 |
TU.AdditionalFiles["sub/bar.h"] = ""; |
| 3037 |
TU.ExtraArgs.push_back("-I" + testPath("sub")); |
| 3038 |
|
| 3039 |
auto Results = completions(TU, Test.point()); |
| 3040 |
EXPECT_THAT(Results.Completions, |
| 3041 |
AllOf(has("sub/", CompletionItemKind::Folder), |
| 3042 |
has("bar.h\"", CompletionItemKind::File))); |
| 3043 |
} |
| 3044 |
|
| 3045 |
TEST(CompletionTest, NoCrashAtNonAlphaIncludeHeader) { |
| 3046 |
completions( |
| 3047 |
R"cpp( |
| 3048 |
#include "./^" |
| 3049 |
)cpp"); |
| 3050 |
} |
| 3051 |
|
| 3052 |
TEST(CompletionTest, NoAllScopesCompletionWhenQualified) { |
| 3053 |
clangd::CodeCompleteOptions Opts = {}; |
| 3054 |
Opts.AllScopes = true; |
| 3055 |
|
| 3056 |
auto Results = completions( |
| 3057 |
R"cpp( |
| 3058 |
void f() { na::Clangd^ } |
| 3059 |
)cpp", |
| 3060 |
{cls("na::ClangdA"), cls("nx::ClangdX"), cls("Clangd3")}, Opts); |
| 3061 |
EXPECT_THAT(Results.Completions, |
| 3062 |
UnorderedElementsAre( |
| 3063 |
AllOf(qualifier(""), scope("na::"), named("ClangdA")))); |
| 3064 |
} |
| 3065 |
|
| 3066 |
TEST(CompletionTest, AllScopesCompletion) { |
| 3067 |
clangd::CodeCompleteOptions Opts = {}; |
| 3068 |
Opts.AllScopes = true; |
| 3069 |
|
| 3070 |
auto Results = completions( |
| 3071 |
R"cpp( |
| 3072 |
namespace na { |
| 3073 |
void f() { Clangd^ } |
| 3074 |
} |
| 3075 |
)cpp", |
| 3076 |
{cls("nx::Clangd1"), cls("ny::Clangd2"), cls("Clangd3"), |
| 3077 |
cls("na::nb::Clangd4"), enmConstant("na::C::Clangd5")}, |
| 3078 |
Opts); |
| 3079 |
EXPECT_THAT( |
| 3080 |
Results.Completions, |
| 3081 |
UnorderedElementsAre(AllOf(qualifier("nx::"), named("Clangd1"), |
| 3082 |
kind(CompletionItemKind::Class)), |
| 3083 |
AllOf(qualifier("ny::"), named("Clangd2"), |
| 3084 |
kind(CompletionItemKind::Class)), |
| 3085 |
AllOf(qualifier(""), scope(""), named("Clangd3"), |
| 3086 |
kind(CompletionItemKind::Class)), |
| 3087 |
AllOf(qualifier("nb::"), named("Clangd4"), |
| 3088 |
kind(CompletionItemKind::Class)), |
| 3089 |
AllOf(qualifier("C::"), named("Clangd5"), |
| 3090 |
kind(CompletionItemKind::EnumMember)))); |
| 3091 |
} |
| 3092 |
|
| 3093 |
TEST(CompletionTest, NoQualifierIfShadowed) { |
| 3094 |
clangd::CodeCompleteOptions Opts = {}; |
| 3095 |
Opts.AllScopes = true; |
| 3096 |
|
| 3097 |
auto Results = completions(R"cpp( |
| 3098 |
namespace nx { class Clangd1 {}; } |
| 3099 |
using nx::Clangd1; |
| 3100 |
void f() { Clangd^ } |
| 3101 |
)cpp", |
| 3102 |
{cls("nx::Clangd1"), cls("nx::Clangd2")}, Opts); |
| 3103 |
// Although Clangd1 is from another namespace, Sema tells us it's in-scope and |
| 3104 |
// needs no qualifier. |
| 3105 |
EXPECT_THAT(Results.Completions, |
| 3106 |
UnorderedElementsAre(AllOf(qualifier(""), named("Clangd1")), |
| 3107 |
AllOf(qualifier("nx::"), named("Clangd2")))); |
| 3108 |
} |
| 3109 |
|
| 3110 |
TEST(CompletionTest, NoCompletionsForNewNames) { |
| 3111 |
clangd::CodeCompleteOptions Opts; |
| 3112 |
Opts.AllScopes = true; |
| 3113 |
auto Results = completions(R"cpp( |
| 3114 |
void f() { int n^ } |
| 3115 |
)cpp", |
| 3116 |
{cls("naber"), cls("nx::naber")}, Opts); |
| 3117 |
EXPECT_THAT(Results.Completions, UnorderedElementsAre()); |
| 3118 |
} |
| 3119 |
|
| 3120 |
TEST(CompletionTest, Lambda) { |
| 3121 |
clangd::CodeCompleteOptions Opts = {}; |
| 3122 |
|
| 3123 |
auto Results = completions(R"cpp( |
| 3124 |
void function() { |
| 3125 |
auto Lambda = [](int a, const double &b) {return 1.f;}; |
| 3126 |
Lam^ |
| 3127 |
} |
| 3128 |
)cpp", |
| 3129 |
{}, Opts); |
| 3130 |
|
| 3131 |
ASSERT_EQ(Results.Completions.size(), 1u); |
| 3132 |
const auto &A = Results.Completions.front(); |
| 3133 |
EXPECT_EQ(A.Name, "Lambda"); |
| 3134 |
EXPECT_EQ(A.Signature, "(int a, const double &b) const"); |
| 3135 |
EXPECT_EQ(A.Kind, CompletionItemKind::Variable); |
| 3136 |
EXPECT_EQ(A.ReturnType, "float"); |
| 3137 |
EXPECT_EQ(A.SnippetSuffix, "(${1:int a}, ${2:const double &b})"); |
| 3138 |
} |
| 3139 |
|
| 3140 |
TEST(CompletionTest, StructuredBinding) { |
| 3141 |
clangd::CodeCompleteOptions Opts = {}; |
| 3142 |
|
| 3143 |
auto Results = completions(R"cpp( |
| 3144 |
struct S { |
| 3145 |
using Float = float; |
| 3146 |
int x; |
| 3147 |
Float y; |
| 3148 |
}; |
| 3149 |
void function() { |
| 3150 |
const auto &[xxx, yyy] = S{}; |
| 3151 |
yyy^ |
| 3152 |
} |
| 3153 |
)cpp", |
| 3154 |
{}, Opts); |
| 3155 |
|
| 3156 |
ASSERT_EQ(Results.Completions.size(), 1u); |
| 3157 |
const auto &A = Results.Completions.front(); |
| 3158 |
EXPECT_EQ(A.Name, "yyy"); |
| 3159 |
EXPECT_EQ(A.Kind, CompletionItemKind::Variable); |
| 3160 |
EXPECT_EQ(A.ReturnType, "const Float"); |
| 3161 |
} |
| 3162 |
|
| 3163 |
TEST(CompletionTest, ObjectiveCMethodNoArguments) { |
| 3164 |
auto Results = completions(R"objc( |
| 3165 |
@interface Foo |
| 3166 |
@property(nonatomic, setter=setXToIgnoreComplete:) int value; |
| 3167 |
@end |
| 3168 |
Foo *foo = [Foo new]; int y = [foo v^] |
| 3169 |
)objc", |
| 3170 |
/*IndexSymbols=*/{}, |
| 3171 |
/*Opts=*/{}, "Foo.m"); |
| 3172 |
|
| 3173 |
auto C = Results.Completions; |
| 3174 |
EXPECT_THAT(C, ElementsAre(named("value"))); |
| 3175 |
EXPECT_THAT(C, ElementsAre(kind(CompletionItemKind::Method))); |
| 3176 |
EXPECT_THAT(C, ElementsAre(returnType("int"))); |
| 3177 |
EXPECT_THAT(C, ElementsAre(signature(""))); |
| 3178 |
EXPECT_THAT(C, ElementsAre(snippetSuffix(""))); |
| 3179 |
} |
| 3180 |
|
| 3181 |
TEST(CompletionTest, ObjectiveCMethodOneArgument) { |
| 3182 |
auto Results = completions(R"objc( |
| 3183 |
@interface Foo |
| 3184 |
- (int)valueForCharacter:(char)c; |
| 3185 |
@end |
| 3186 |
Foo *foo = [Foo new]; int y = [foo v^] |
| 3187 |
)objc", |
| 3188 |
/*IndexSymbols=*/{}, |
| 3189 |
/*Opts=*/{}, "Foo.m"); |
| 3190 |
|
| 3191 |
auto C = Results.Completions; |
| 3192 |
EXPECT_THAT(C, ElementsAre(named("valueForCharacter:"))); |
| 3193 |
EXPECT_THAT(C, ElementsAre(kind(CompletionItemKind::Method))); |
| 3194 |
EXPECT_THAT(C, ElementsAre(returnType("int"))); |
| 3195 |
EXPECT_THAT(C, ElementsAre(signature("(char)"))); |
| 3196 |
EXPECT_THAT(C, ElementsAre(snippetSuffix("${1:(char)}"))); |
| 3197 |
} |
| 3198 |
|
| 3199 |
TEST(CompletionTest, ObjectiveCMethodTwoArgumentsFromBeginning) { |
| 3200 |
auto Results = completions(R"objc( |
| 3201 |
@interface Foo |
| 3202 |
+ (id)fooWithValue:(int)value fooey:(unsigned int)fooey; |
| 3203 |
@end |
| 3204 |
id val = [Foo foo^] |
| 3205 |
)objc", |
| 3206 |
/*IndexSymbols=*/{}, |
| 3207 |
/*Opts=*/{}, "Foo.m"); |
| 3208 |
|
| 3209 |
auto C = Results.Completions; |
| 3210 |
EXPECT_THAT(C, ElementsAre(named("fooWithValue:"))); |
| 3211 |
EXPECT_THAT(C, ElementsAre(kind(CompletionItemKind::Method))); |
| 3212 |
EXPECT_THAT(C, ElementsAre(returnType("id"))); |
| 3213 |
EXPECT_THAT(C, ElementsAre(signature("(int) fooey:(unsigned int)"))); |
| 3214 |
EXPECT_THAT( |
| 3215 |
C, ElementsAre(snippetSuffix("${1:(int)} fooey:${2:(unsigned int)}"))); |
| 3216 |
} |
| 3217 |
|
| 3218 |
TEST(CompletionTest, ObjectiveCMethodTwoArgumentsFromMiddle) { |
| 3219 |
auto Results = completions(R"objc( |
| 3220 |
@interface Foo |
| 3221 |
+ (id)fooWithValue:(int)value fooey:(unsigned int)fooey; |
| 3222 |
@end |
| 3223 |
id val = [Foo fooWithValue:10 f^] |
| 3224 |
)objc", |
| 3225 |
/*IndexSymbols=*/{}, |
| 3226 |
/*Opts=*/{}, "Foo.m"); |
| 3227 |
|
| 3228 |
auto C = Results.Completions; |
| 3229 |
EXPECT_THAT(C, ElementsAre(named("fooey:"))); |
| 3230 |
EXPECT_THAT(C, ElementsAre(kind(CompletionItemKind::Method))); |
| 3231 |
EXPECT_THAT(C, ElementsAre(returnType("id"))); |
| 3232 |
EXPECT_THAT(C, ElementsAre(signature("(unsigned int)"))); |
| 3233 |
EXPECT_THAT(C, ElementsAre(snippetSuffix("${1:(unsigned int)}"))); |
| 3234 |
} |
| 3235 |
|
| 3236 |
TEST(CompletionTest, ObjectiveCMethodFilterOnEntireSelector) { |
| 3237 |
auto Results = completions(R"objc( |
| 3238 |
@interface Foo |
| 3239 |
+ (id)player:(id)player willRun:(id)run; |
| 3240 |
@end |
| 3241 |
id val = [Foo wi^] |
| 3242 |
)objc", |
| 3243 |
/*IndexSymbols=*/{}, |
| 3244 |
/*Opts=*/{}, "Foo.m"); |
| 3245 |
|
| 3246 |
auto C = Results.Completions; |
| 3247 |
EXPECT_THAT(C, ElementsAre(named("player:"))); |
| 3248 |
EXPECT_THAT(C, ElementsAre(filterText("player:willRun:"))); |
| 3249 |
EXPECT_THAT(C, ElementsAre(kind(CompletionItemKind::Method))); |
| 3250 |
EXPECT_THAT(C, ElementsAre(returnType("id"))); |
| 3251 |
EXPECT_THAT(C, ElementsAre(signature("(id) willRun:(id)"))); |
| 3252 |
EXPECT_THAT(C, ElementsAre(snippetSuffix("${1:(id)} willRun:${2:(id)}"))); |
| 3253 |
} |
| 3254 |
|
| 3255 |
TEST(CompletionTest, ObjectiveCSimpleMethodDeclaration) { |
| 3256 |
auto Results = completions(R"objc( |
| 3257 |
@interface Foo |
| 3258 |
- (void)foo; |
| 3259 |
@end |
| 3260 |
@implementation Foo |
| 3261 |
fo^ |
| 3262 |
@end |
| 3263 |
)objc", |
| 3264 |
/*IndexSymbols=*/{}, |
| 3265 |
/*Opts=*/{}, "Foo.m"); |
| 3266 |
|
| 3267 |
auto C = Results.Completions; |
| 3268 |
EXPECT_THAT(C, ElementsAre(named("foo"))); |
| 3269 |
EXPECT_THAT(C, ElementsAre(kind(CompletionItemKind::Method))); |
| 3270 |
EXPECT_THAT(C, ElementsAre(qualifier("- (void)"))); |
| 3271 |
} |
| 3272 |
|
| 3273 |
TEST(CompletionTest, ObjectiveCMethodDeclaration) { |
| 3274 |
auto Results = completions(R"objc( |
| 3275 |
@interface Foo |
| 3276 |
- (int)valueForCharacter:(char)c secondArgument:(id)object; |
| 3277 |
@end |
| 3278 |
@implementation Foo |
| 3279 |
valueFor^ |
| 3280 |
@end |
| 3281 |
)objc", |
| 3282 |
/*IndexSymbols=*/{}, |
| 3283 |
/*Opts=*/{}, "Foo.m"); |
| 3284 |
|
| 3285 |
auto C = Results.Completions; |
| 3286 |
EXPECT_THAT(C, ElementsAre(named("valueForCharacter:"))); |
| 3287 |
EXPECT_THAT(C, ElementsAre(kind(CompletionItemKind::Method))); |
| 3288 |
EXPECT_THAT(C, ElementsAre(qualifier("- (int)"))); |
| 3289 |
EXPECT_THAT(C, ElementsAre(signature("(char)c secondArgument:(id)object"))); |
| 3290 |
} |
| 3291 |
|
| 3292 |
TEST(CompletionTest, ObjectiveCMethodDeclarationFilterOnEntireSelector) { |
| 3293 |
auto Results = completions(R"objc( |
| 3294 |
@interface Foo |
| 3295 |
- (int)valueForCharacter:(char)c secondArgument:(id)object; |
| 3296 |
@end |
| 3297 |
@implementation Foo |
| 3298 |
secondArg^ |
| 3299 |
@end |
| 3300 |
)objc", |
| 3301 |
/*IndexSymbols=*/{}, |
| 3302 |
/*Opts=*/{}, "Foo.m"); |
| 3303 |
|
| 3304 |
auto C = Results.Completions; |
| 3305 |
EXPECT_THAT(C, ElementsAre(named("valueForCharacter:"))); |
| 3306 |
EXPECT_THAT(C, ElementsAre(filterText("valueForCharacter:secondArgument:"))); |
| 3307 |
EXPECT_THAT(C, ElementsAre(kind(CompletionItemKind::Method))); |
| 3308 |
EXPECT_THAT(C, ElementsAre(qualifier("- (int)"))); |
| 3309 |
EXPECT_THAT(C, ElementsAre(signature("(char)c secondArgument:(id)object"))); |
| 3310 |
} |
| 3311 |
|
| 3312 |
TEST(CompletionTest, ObjectiveCMethodDeclarationPrefixTyped) { |
| 3313 |
auto Results = completions(R"objc( |
| 3314 |
@interface Foo |
| 3315 |
- (int)valueForCharacter:(char)c; |
| 3316 |
@end |
| 3317 |
@implementation Foo |
| 3318 |
- (int)valueFor^ |
| 3319 |
@end |
| 3320 |
)objc", |
| 3321 |
/*IndexSymbols=*/{}, |
| 3322 |
/*Opts=*/{}, "Foo.m"); |
| 3323 |
|
| 3324 |
auto C = Results.Completions; |
| 3325 |
EXPECT_THAT(C, ElementsAre(named("valueForCharacter:"))); |
| 3326 |
EXPECT_THAT(C, ElementsAre(kind(CompletionItemKind::Method))); |
| 3327 |
EXPECT_THAT(C, ElementsAre(signature("(char)c"))); |
| 3328 |
} |
| 3329 |
|
| 3330 |
TEST(CompletionTest, ObjectiveCMethodDeclarationFromMiddle) { |
| 3331 |
auto Results = completions(R"objc( |
| 3332 |
@interface Foo |
| 3333 |
- (int)valueForCharacter:(char)c secondArgument:(id)object; |
| 3334 |
@end |
| 3335 |
@implementation Foo |
| 3336 |
- (int)valueForCharacter:(char)c second^ |
| 3337 |
@end |
| 3338 |
)objc", |
| 3339 |
/*IndexSymbols=*/{}, |
| 3340 |
/*Opts=*/{}, "Foo.m"); |
| 3341 |
|
| 3342 |
auto C = Results.Completions; |
| 3343 |
EXPECT_THAT(C, ElementsAre(named("secondArgument:"))); |
| 3344 |
EXPECT_THAT(C, ElementsAre(kind(CompletionItemKind::Method))); |
| 3345 |
EXPECT_THAT(C, ElementsAre(signature("(id)object"))); |
| 3346 |
} |
| 3347 |
|
| 3348 |
TEST(CompletionTest, ObjectiveCProtocolFromIndex) { |
| 3349 |
Symbol FoodClass = objcClass("FoodClass"); |
| 3350 |
Symbol SymFood = objcProtocol("Food"); |
| 3351 |
Symbol SymFooey = objcProtocol("Fooey"); |
| 3352 |
auto Results = completions("id<Foo^>", {SymFood, FoodClass, SymFooey}, |
| 3353 |
/*Opts=*/{}, "Foo.m"); |
| 3354 |
|
| 3355 |
// Should only give protocols for ObjC protocol completions. |
| 3356 |
EXPECT_THAT(Results.Completions, |
| 3357 |
UnorderedElementsAre( |
| 3358 |
AllOf(named("Food"), kind(CompletionItemKind::Interface)), |
| 3359 |
AllOf(named("Fooey"), kind(CompletionItemKind::Interface)))); |
| 3360 |
|
| 3361 |
Results = completions("Fo^", {SymFood, FoodClass, SymFooey}, |
| 3362 |
/*Opts=*/{}, "Foo.m"); |
| 3363 |
// Shouldn't give protocols for non protocol completions. |
| 3364 |
EXPECT_THAT( |
| 3365 |
Results.Completions, |
| 3366 |
ElementsAre(AllOf(named("FoodClass"), kind(CompletionItemKind::Class)))); |
| 3367 |
} |
| 3368 |
|
| 3369 |
TEST(CompletionTest, ObjectiveCProtocolFromIndexSpeculation) { |
| 3370 |
MockFS FS; |
| 3371 |
MockCompilationDatabase CDB; |
| 3372 |
ClangdServer Server(CDB, FS, ClangdServer::optsForTest()); |
| 3373 |
|
| 3374 |
auto File = testPath("Foo.m"); |
| 3375 |
Annotations Test(R"cpp( |
| 3376 |
@protocol Food |
| 3377 |
@end |
| 3378 |
id<Foo$1^> foo; |
| 3379 |
Foo$2^ bar; |
| 3380 |
)cpp"); |
| 3381 |
runAddDocument(Server, File, Test.code()); |
| 3382 |
clangd::CodeCompleteOptions Opts = {}; |
| 3383 |
|
| 3384 |
Symbol FoodClass = objcClass("FoodClass"); |
| 3385 |
IndexRequestCollector Requests({FoodClass}); |
| 3386 |
Opts.Index = &Requests; |
| 3387 |
|
| 3388 |
auto CompleteAtPoint = [&](StringRef P) { |
| 3389 |
return cantFail(runCodeComplete(Server, File, Test.point(P), Opts)) |
| 3390 |
.Completions; |
| 3391 |
}; |
| 3392 |
|
| 3393 |
auto C = CompleteAtPoint("1"); |
| 3394 |
auto Reqs1 = Requests.consumeRequests(1); |
| 3395 |
ASSERT_EQ(Reqs1.size(), 1u); |
| 3396 |
EXPECT_THAT(C, ElementsAre(AllOf(named("Food"), |
| 3397 |
kind(CompletionItemKind::Interface)))); |
| 3398 |
|
| 3399 |
C = CompleteAtPoint("2"); |
| 3400 |
auto Reqs2 = Requests.consumeRequests(1); |
| 3401 |
// Speculation succeeded. Used speculative index result, but filtering now to |
| 3402 |
// now include FoodClass. |
| 3403 |
ASSERT_EQ(Reqs2.size(), 1u); |
| 3404 |
EXPECT_EQ(Reqs2[0], Reqs1[0]); |
| 3405 |
EXPECT_THAT(C, ElementsAre(AllOf(named("FoodClass"), |
| 3406 |
kind(CompletionItemKind::Class)))); |
| 3407 |
} |
| 3408 |
|
| 3409 |
TEST(CompletionTest, ObjectiveCCategoryFromIndexIgnored) { |
| 3410 |
Symbol FoodCategory = objcCategory("FoodClass", "Extension"); |
| 3411 |
auto Results = completions(R"objc( |
| 3412 |
@interface Foo |
| 3413 |
@end |
| 3414 |
@interface Foo (^) |
| 3415 |
@end |
| 3416 |
)objc", |
| 3417 |
{FoodCategory}, |
| 3418 |
/*Opts=*/{}, "Foo.m"); |
| 3419 |
EXPECT_THAT(Results.Completions, IsEmpty()); |
| 3420 |
} |
| 3421 |
|
| 3422 |
TEST(CompletionTest, CursorInSnippets) { |
| 3423 |
clangd::CodeCompleteOptions Options; |
| 3424 |
Options.EnableSnippets = true; |
| 3425 |
auto Results = completions( |
| 3426 |
R"cpp( |
| 3427 |
void while_foo(int a, int b); |
| 3428 |
void test() { |
| 3429 |
whil^ |
| 3430 |
})cpp", |
| 3431 |
/*IndexSymbols=*/{}, Options); |
| 3432 |
|
| 3433 |
// Last placeholder in code patterns should be $0 to put the cursor there. |
| 3434 |
EXPECT_THAT(Results.Completions, |
| 3435 |
Contains(AllOf(named("while"), |
| 3436 |
snippetSuffix(" (${1:condition}) {\n$0\n}")))); |
| 3437 |
// However, snippets for functions must *not* end with $0. |
| 3438 |
EXPECT_THAT(Results.Completions, |
| 3439 |
Contains(AllOf(named("while_foo"), |
| 3440 |
snippetSuffix("(${1:int a}, ${2:int b})")))); |
| 3441 |
} |
| 3442 |
|
| 3443 |
TEST(CompletionTest, WorksWithNullType) { |
| 3444 |
auto R = completions(R"cpp( |
| 3445 |
int main() { |
| 3446 |
for (auto [loopVar] : y ) { // y has to be unresolved. |
| 3447 |
int z = loopV^; |
| 3448 |
} |
| 3449 |
} |
| 3450 |
)cpp"); |
| 3451 |
EXPECT_THAT(R.Completions, ElementsAre(named("loopVar"))); |
| 3452 |
} |
| 3453 |
|
| 3454 |
TEST(CompletionTest, UsingDecl) { |
| 3455 |
const char *Header(R"cpp( |
| 3456 |
void foo(int); |
| 3457 |
namespace std { |
| 3458 |
using ::foo; |
| 3459 |
})cpp"); |
| 3460 |
const char *Source(R"cpp( |
| 3461 |
void bar() { |
| 3462 |
std::^; |
| 3463 |
})cpp"); |
| 3464 |
auto Index = TestTU::withHeaderCode(Header).index(); |
| 3465 |
clangd::CodeCompleteOptions Opts; |
| 3466 |
Opts.Index = Index.get(); |
| 3467 |
Opts.AllScopes = true; |
| 3468 |
auto R = completions(Source, {}, Opts); |
| 3469 |
EXPECT_THAT(R.Completions, |
| 3470 |
ElementsAre(AllOf(scope("std::"), named("foo"), |
| 3471 |
kind(CompletionItemKind::Reference)))); |
| 3472 |
} |
| 3473 |
|
| 3474 |
TEST(CompletionTest, Enums) { |
| 3475 |
const char *Header(R"cpp( |
| 3476 |
namespace ns { |
| 3477 |
enum Unscoped { Clangd1 }; |
| 3478 |
class C { |
| 3479 |
enum Unscoped { Clangd2 }; |
| 3480 |
}; |
| 3481 |
enum class Scoped { Clangd3 }; |
| 3482 |
})cpp"); |
| 3483 |
const char *Source(R"cpp( |
| 3484 |
void bar() { |
| 3485 |
Clangd^ |
| 3486 |
})cpp"); |
| 3487 |
auto Index = TestTU::withHeaderCode(Header).index(); |
| 3488 |
clangd::CodeCompleteOptions Opts; |
| 3489 |
Opts.Index = Index.get(); |
| 3490 |
Opts.AllScopes = true; |
| 3491 |
auto R = completions(Source, {}, Opts); |
| 3492 |
EXPECT_THAT(R.Completions, UnorderedElementsAre( |
| 3493 |
AllOf(scope("ns::"), named("Clangd1"), |
| 3494 |
kind(CompletionItemKind::EnumMember)), |
| 3495 |
AllOf(scope("ns::C::"), named("Clangd2"), |
| 3496 |
kind(CompletionItemKind::EnumMember)), |
| 3497 |
AllOf(scope("ns::Scoped::"), named("Clangd3"), |
| 3498 |
kind(CompletionItemKind::EnumMember)))); |
| 3499 |
} |
| 3500 |
|
| 3501 |
TEST(CompletionTest, ScopeIsUnresolved) { |
| 3502 |
clangd::CodeCompleteOptions Opts = {}; |
| 3503 |
Opts.AllScopes = true; |
| 3504 |
|
| 3505 |
auto Results = completions(R"cpp( |
| 3506 |
namespace a { |
| 3507 |
void f() { b::X^ } |
| 3508 |
} |
| 3509 |
)cpp", |
| 3510 |
{cls("a::b::XYZ")}, Opts); |
| 3511 |
EXPECT_THAT(Results.Completions, |
| 3512 |
UnorderedElementsAre(AllOf(qualifier(""), named("XYZ")))); |
| 3513 |
} |
| 3514 |
|
| 3515 |
TEST(CompletionTest, NestedScopeIsUnresolved) { |
| 3516 |
clangd::CodeCompleteOptions Opts = {}; |
| 3517 |
Opts.AllScopes = true; |
| 3518 |
|
| 3519 |
auto Results = completions(R"cpp( |
| 3520 |
namespace a { |
| 3521 |
namespace b {} |
| 3522 |
void f() { b::c::X^ } |
| 3523 |
} |
| 3524 |
)cpp", |
| 3525 |
{cls("a::b::c::XYZ")}, Opts); |
| 3526 |
EXPECT_THAT(Results.Completions, |
| 3527 |
UnorderedElementsAre(AllOf(qualifier(""), named("XYZ")))); |
| 3528 |
} |
| 3529 |
|
| 3530 |
// Clang parser gets confused here and doesn't report the ns:: prefix. |
| 3531 |
// Naive behavior is to insert it again. We examine the source and recover. |
| 3532 |
TEST(CompletionTest, NamespaceDoubleInsertion) { |
| 3533 |
clangd::CodeCompleteOptions Opts = {}; |
| 3534 |
|
| 3535 |
auto Results = completions(R"cpp( |
| 3536 |
namespace foo { |
| 3537 |
namespace ns {} |
| 3538 |
#define M(X) < X |
| 3539 |
M(ns::ABC^ |
| 3540 |
} |
| 3541 |
)cpp", |
| 3542 |
{cls("foo::ns::ABCDE")}, Opts); |
| 3543 |
EXPECT_THAT(Results.Completions, |
| 3544 |
UnorderedElementsAre(AllOf(qualifier(""), named("ABCDE")))); |
| 3545 |
} |
| 3546 |
|
| 3547 |
TEST(CompletionTest, DerivedMethodsAreAlwaysVisible) { |
| 3548 |
// Despite the fact that base method matches the ref-qualifier better, |
| 3549 |
// completion results should only include the derived method. |
| 3550 |
auto Completions = completions(R"cpp( |
| 3551 |
struct deque_base { |
| 3552 |
float size(); |
| 3553 |
double size() const; |
| 3554 |
}; |
| 3555 |
struct deque : deque_base { |
| 3556 |
int size() const; |
| 3557 |
}; |
| 3558 |
|
| 3559 |
auto x = deque().^ |
| 3560 |
)cpp") |
| 3561 |
.Completions; |
| 3562 |
EXPECT_THAT(Completions, |
| 3563 |
ElementsAre(AllOf(returnType("int"), named("size")))); |
| 3564 |
} |
| 3565 |
|
| 3566 |
TEST(CompletionTest, NoCrashWithIncompleteLambda) { |
| 3567 |
auto Completions = completions("auto&& x = []{^").Completions; |
| 3568 |
// The completion of x itself can cause a problem: in the code completion |
| 3569 |
// callback, its type is not known, which affects the linkage calculation. |
| 3570 |
// A bad linkage value gets cached, and subsequently updated. |
| 3571 |
EXPECT_THAT(Completions, Contains(named("x"))); |
| 3572 |
|
| 3573 |
auto Signatures = signatures("auto x() { x(^").signatures; |
| 3574 |
EXPECT_THAT(Signatures, Contains(sig("x() -> auto"))); |
| 3575 |
} |
| 3576 |
|
| 3577 |
TEST(CompletionTest, DelayedTemplateParsing) { |
| 3578 |
Annotations Test(R"cpp( |
| 3579 |
int xxx; |
| 3580 |
template <typename T> int foo() { return xx^; } |
| 3581 |
)cpp"); |
| 3582 |
auto TU = TestTU::withCode(Test.code()); |
| 3583 |
// Even though delayed-template-parsing is on, we will disable it to provide |
| 3584 |
// completion in templates. |
| 3585 |
TU.ExtraArgs.push_back("-fdelayed-template-parsing"); |
| 3586 |
|
| 3587 |
EXPECT_THAT(completions(TU, Test.point()).Completions, |
| 3588 |
Contains(named("xxx"))); |
| 3589 |
} |
| 3590 |
|
| 3591 |
TEST(CompletionTest, CompletionRange) { |
| 3592 |
const char *WithRange = "auto x = [[abc]]^"; |
| 3593 |
auto Completions = completions(WithRange); |
| 3594 |
EXPECT_EQ(Completions.CompletionRange, Annotations(WithRange).range()); |
| 3595 |
Completions = completionsNoCompile(WithRange); |
| 3596 |
EXPECT_EQ(Completions.CompletionRange, Annotations(WithRange).range()); |
| 3597 |
|
| 3598 |
const char *EmptyRange = "auto x = [[]]^"; |
| 3599 |
Completions = completions(EmptyRange); |
| 3600 |
EXPECT_EQ(Completions.CompletionRange, Annotations(EmptyRange).range()); |
| 3601 |
Completions = completionsNoCompile(EmptyRange); |
| 3602 |
EXPECT_EQ(Completions.CompletionRange, Annotations(EmptyRange).range()); |
| 3603 |
|
| 3604 |
// Sema doesn't trigger at all here, while the no-sema completion runs |
| 3605 |
// heuristics as normal and reports a range. It'd be nice to be consistent. |
| 3606 |
const char *NoCompletion = "/* foo [[]]^ */"; |
| 3607 |
Completions = completions(NoCompletion); |
| 3608 |
EXPECT_EQ(Completions.CompletionRange, std::nullopt); |
| 3609 |
Completions = completionsNoCompile(NoCompletion); |
| 3610 |
EXPECT_EQ(Completions.CompletionRange, Annotations(NoCompletion).range()); |
| 3611 |
} |
| 3612 |
|
| 3613 |
TEST(NoCompileCompletionTest, Basic) { |
| 3614 |
auto Results = completionsNoCompile(R"cpp( |
| 3615 |
void func() { |
| 3616 |
int xyz; |
| 3617 |
int abc; |
| 3618 |
^ |
| 3619 |
} |
| 3620 |
)cpp"); |
| 3621 |
EXPECT_FALSE(Results.RanParser); |
| 3622 |
EXPECT_THAT(Results.Completions, |
| 3623 |
UnorderedElementsAre(named("void"), named("func"), named("int"), |
| 3624 |
named("xyz"), named("abc"))); |
| 3625 |
} |
| 3626 |
|
| 3627 |
TEST(NoCompileCompletionTest, WithFilter) { |
| 3628 |
auto Results = completionsNoCompile(R"cpp( |
| 3629 |
void func() { |
| 3630 |
int sym1; |
| 3631 |
int sym2; |
| 3632 |
int xyz1; |
| 3633 |
int xyz2; |
| 3634 |
sy^ |
| 3635 |
} |
| 3636 |
)cpp"); |
| 3637 |
EXPECT_THAT(Results.Completions, |
| 3638 |
UnorderedElementsAre(named("sym1"), named("sym2"))); |
| 3639 |
} |
| 3640 |
|
| 3641 |
TEST(NoCompileCompletionTest, WithIndex) { |
| 3642 |
std::vector<Symbol> Syms = {func("xxx"), func("a::xxx"), func("ns::b::xxx"), |
| 3643 |
func("c::xxx"), func("ns::d::xxx")}; |
| 3644 |
auto Results = completionsNoCompile( |
| 3645 |
R"cpp( |
| 3646 |
// Current-scopes, unqualified completion. |
| 3647 |
using namespace a; |
| 3648 |
namespace ns { |
| 3649 |
using namespace b; |
| 3650 |
void foo() { |
| 3651 |
xx^ |
| 3652 |
} |
| 3653 |
} |
| 3654 |
)cpp", |
| 3655 |
Syms); |
| 3656 |
EXPECT_THAT(Results.Completions, |
| 3657 |
UnorderedElementsAre(AllOf(qualifier(""), scope("")), |
| 3658 |
AllOf(qualifier(""), scope("a::")), |
| 3659 |
AllOf(qualifier(""), scope("ns::b::")))); |
| 3660 |
CodeCompleteOptions Opts; |
| 3661 |
Opts.AllScopes = true; |
| 3662 |
Results = completionsNoCompile( |
| 3663 |
R"cpp( |
| 3664 |
// All-scopes unqualified completion. |
| 3665 |
using namespace a; |
| 3666 |
namespace ns { |
| 3667 |
using namespace b; |
| 3668 |
void foo() { |
| 3669 |
xx^ |
| 3670 |
} |
| 3671 |
} |
| 3672 |
)cpp", |
| 3673 |
Syms, Opts); |
| 3674 |
EXPECT_THAT(Results.Completions, |
| 3675 |
UnorderedElementsAre(AllOf(qualifier(""), scope("")), |
| 3676 |
AllOf(qualifier(""), scope("a::")), |
| 3677 |
AllOf(qualifier(""), scope("ns::b::")), |
| 3678 |
AllOf(qualifier("c::"), scope("c::")), |
| 3679 |
AllOf(qualifier("d::"), scope("ns::d::")))); |
| 3680 |
Results = completionsNoCompile( |
| 3681 |
R"cpp( |
| 3682 |
// Qualified completion. |
| 3683 |
using namespace a; |
| 3684 |
namespace ns { |
| 3685 |
using namespace b; |
| 3686 |
void foo() { |
| 3687 |
b::xx^ |
| 3688 |
} |
| 3689 |
} |
| 3690 |
)cpp", |
| 3691 |
Syms, Opts); |
| 3692 |
EXPECT_THAT(Results.Completions, |
| 3693 |
ElementsAre(AllOf(qualifier(""), scope("ns::b::")))); |
| 3694 |
Results = completionsNoCompile( |
| 3695 |
R"cpp( |
| 3696 |
// Absolutely qualified completion. |
| 3697 |
using namespace a; |
| 3698 |
namespace ns { |
| 3699 |
using namespace b; |
| 3700 |
void foo() { |
| 3701 |
::a::xx^ |
| 3702 |
} |
| 3703 |
} |
| 3704 |
)cpp", |
| 3705 |
Syms, Opts); |
| 3706 |
EXPECT_THAT(Results.Completions, |
| 3707 |
ElementsAre(AllOf(qualifier(""), scope("a::")))); |
| 3708 |
} |
| 3709 |
|
| 3710 |
TEST(AllowImplicitCompletion, All) { |
| 3711 |
const char *Yes[] = { |
| 3712 |
"foo.^bar", |
| 3713 |
"foo->^bar", |
| 3714 |
"foo::^bar", |
| 3715 |
" # include <^foo.h>", |
| 3716 |
"#import <foo/^bar.h>", |
| 3717 |
"#include_next \"^", |
| 3718 |
}; |
| 3719 |
const char *No[] = { |
| 3720 |
"foo>^bar", |
| 3721 |
"foo:^bar", |
| 3722 |
"foo\n^bar", |
| 3723 |
"#include <foo.h> //^", |
| 3724 |
"#include \"foo.h\"^", |
| 3725 |
"#error <^", |
| 3726 |
"#<^", |
| 3727 |
}; |
| 3728 |
for (const char *Test : Yes) { |
| 3729 |
llvm::Annotations A(Test); |
| 3730 |
EXPECT_TRUE(allowImplicitCompletion(A.code(), A.point())) << Test; |
| 3731 |
} |
| 3732 |
for (const char *Test : No) { |
| 3733 |
llvm::Annotations A(Test); |
| 3734 |
EXPECT_FALSE(allowImplicitCompletion(A.code(), A.point())) << Test; |
| 3735 |
} |
| 3736 |
} |
| 3737 |
|
| 3738 |
TEST(CompletionTest, FunctionArgsExist) { |
| 3739 |
clangd::CodeCompleteOptions Opts; |
| 3740 |
Opts.EnableSnippets = true; |
| 3741 |
std::string Context = R"cpp( |
| 3742 |
#define MACRO(x) |
| 3743 |
int foo(int A); |
| 3744 |
int bar(); |
| 3745 |
struct Object { |
| 3746 |
Object(int B) {} |
| 3747 |
}; |
| 3748 |
template <typename T> |
| 3749 |
struct Container { |
| 3750 |
Container(int Size) {} |
| 3751 |
}; |
| 3752 |
)cpp"; |
| 3753 |
EXPECT_THAT(completions(Context + "int y = fo^", {}, Opts).Completions, |
| 3754 |
UnorderedElementsAre( |
| 3755 |
AllOf(labeled("foo(int A)"), snippetSuffix("(${1:int A})")))); |
| 3756 |
EXPECT_THAT( |
| 3757 |
completions(Context + "int y = fo^(42)", {}, Opts).Completions, |
| 3758 |
UnorderedElementsAre(AllOf(labeled("foo(int A)"), snippetSuffix("")))); |
| 3759 |
// FIXME(kirillbobyrev): No snippet should be produced here. |
| 3760 |
EXPECT_THAT(completions(Context + "int y = fo^o(42)", {}, Opts).Completions, |
| 3761 |
UnorderedElementsAre( |
| 3762 |
AllOf(labeled("foo(int A)"), snippetSuffix("(${1:int A})")))); |
| 3763 |
EXPECT_THAT( |
| 3764 |
completions(Context + "int y = ba^", {}, Opts).Completions, |
| 3765 |
UnorderedElementsAre(AllOf(labeled("bar()"), snippetSuffix("()")))); |
| 3766 |
EXPECT_THAT(completions(Context + "int y = ba^()", {}, Opts).Completions, |
| 3767 |
UnorderedElementsAre(AllOf(labeled("bar()"), snippetSuffix("")))); |
| 3768 |
EXPECT_THAT( |
| 3769 |
completions(Context + "Object o = Obj^", {}, Opts).Completions, |
| 3770 |
Contains(AllOf(labeled("Object(int B)"), snippetSuffix("(${1:int B})"), |
| 3771 |
kind(CompletionItemKind::Constructor)))); |
| 3772 |
EXPECT_THAT(completions(Context + "Object o = Obj^()", {}, Opts).Completions, |
| 3773 |
Contains(AllOf(labeled("Object(int B)"), snippetSuffix(""), |
| 3774 |
kind(CompletionItemKind::Constructor)))); |
| 3775 |
EXPECT_THAT( |
| 3776 |
completions(Context + "Container c = Cont^", {}, Opts).Completions, |
| 3777 |
Contains(AllOf(labeled("Container<typename T>(int Size)"), |
| 3778 |
snippetSuffix("<${1:typename T}>(${2:int Size})"), |
| 3779 |
kind(CompletionItemKind::Constructor)))); |
| 3780 |
EXPECT_THAT( |
| 3781 |
completions(Context + "Container c = Cont^()", {}, Opts).Completions, |
| 3782 |
Contains(AllOf(labeled("Container<typename T>(int Size)"), |
| 3783 |
snippetSuffix("<${1:typename T}>"), |
| 3784 |
kind(CompletionItemKind::Constructor)))); |
| 3785 |
EXPECT_THAT( |
| 3786 |
completions(Context + "Container c = Cont^<int>()", {}, Opts).Completions, |
| 3787 |
Contains(AllOf(labeled("Container<typename T>(int Size)"), |
| 3788 |
snippetSuffix(""), |
| 3789 |
kind(CompletionItemKind::Constructor)))); |
| 3790 |
EXPECT_THAT(completions(Context + "MAC^(2)", {}, Opts).Completions, |
| 3791 |
Contains(AllOf(labeled("MACRO(x)"), snippetSuffix(""), |
| 3792 |
kind(CompletionItemKind::Text)))); |
| 3793 |
} |
| 3794 |
|
| 3795 |
TEST(CompletionTest, NoCrashDueToMacroOrdering) { |
| 3796 |
EXPECT_THAT(completions(R"cpp( |
| 3797 |
#define ECHO(X) X |
| 3798 |
#define ECHO2(X) ECHO(X) |
| 3799 |
int finish_preamble = EC^HO(2);)cpp") |
| 3800 |
.Completions, |
| 3801 |
UnorderedElementsAre(labeled("ECHO(X)"), labeled("ECHO2(X)"))); |
| 3802 |
} |
| 3803 |
|
| 3804 |
TEST(CompletionTest, ObjCCategoryDecls) { |
| 3805 |
TestTU TU; |
| 3806 |
TU.ExtraArgs.push_back("-xobjective-c"); |
| 3807 |
TU.HeaderCode = R"objc( |
| 3808 |
@interface Foo |
| 3809 |
@end |
| 3810 |
|
| 3811 |
@interface Foo (FooExt1) |
| 3812 |
@end |
| 3813 |
|
| 3814 |
@interface Foo (FooExt2) |
| 3815 |
@end |
| 3816 |
|
| 3817 |
@interface Bar |
| 3818 |
@end |
| 3819 |
|
| 3820 |
@interface Bar (BarExt) |
| 3821 |
@end)objc"; |
| 3822 |
|
| 3823 |
{ |
| 3824 |
Annotations Test(R"objc( |
| 3825 |
@implementation Foo (^) |
| 3826 |
@end |
| 3827 |
)objc"); |
| 3828 |
TU.Code = Test.code().str(); |
| 3829 |
auto Results = completions(TU, Test.point()); |
| 3830 |
EXPECT_THAT(Results.Completions, |
| 3831 |
UnorderedElementsAre(labeled("FooExt1"), labeled("FooExt2"))); |
| 3832 |
} |
| 3833 |
{ |
| 3834 |
Annotations Test(R"objc( |
| 3835 |
@interface Foo (^) |
| 3836 |
@end |
| 3837 |
)objc"); |
| 3838 |
TU.Code = Test.code().str(); |
| 3839 |
auto Results = completions(TU, Test.point()); |
| 3840 |
EXPECT_THAT(Results.Completions, UnorderedElementsAre(labeled("BarExt"))); |
| 3841 |
} |
| 3842 |
} |
| 3843 |
|
| 3844 |
TEST(CompletionTest, PreambleCodeComplete) { |
| 3845 |
llvm::StringLiteral Baseline = "\n#define MACRO 12\nint num = MACRO;"; |
| 3846 |
llvm::StringLiteral ModifiedCC = |
| 3847 |
"#include \"header.h\"\n#define MACRO 12\nint num = MACRO; int num2 = M^"; |
| 3848 |
|
| 3849 |
Annotations Test(ModifiedCC); |
| 3850 |
auto BaselineTU = TestTU::withCode(Baseline); |
| 3851 |
auto ModifiedTU = TestTU::withCode(Test.code()); |
| 3852 |
|
| 3853 |
MockFS FS; |
| 3854 |
auto Inputs = ModifiedTU.inputs(FS); |
| 3855 |
auto Result = codeComplete(testPath(ModifiedTU.Filename), Test.point(), |
| 3856 |
BaselineTU.preamble().get(), Inputs, {}); |
| 3857 |
EXPECT_THAT(Result.Completions, Not(testing::IsEmpty())); |
| 3858 |
} |
| 3859 |
|
| 3860 |
TEST(CompletionTest, CommentParamName) { |
| 3861 |
const std::string Code = R"cpp( |
| 3862 |
void fun(int foo, int bar); |
| 3863 |
void overloaded(int param_int); |
| 3864 |
void overloaded(int param_int, int param_other); |
| 3865 |
void overloaded(char param_char); |
| 3866 |
int main() { |
| 3867 |
)cpp"; |
| 3868 |
|
| 3869 |
EXPECT_THAT(completions(Code + "fun(/*^").Completions, |
| 3870 |
UnorderedElementsAre(labeled("foo=*/"))); |
| 3871 |
EXPECT_THAT(completions(Code + "fun(1, /*^").Completions, |
| 3872 |
UnorderedElementsAre(labeled("bar=*/"))); |
| 3873 |
EXPECT_THAT(completions(Code + "/*^").Completions, IsEmpty()); |
| 3874 |
// Test de-duplication. |
| 3875 |
EXPECT_THAT( |
| 3876 |
completions(Code + "overloaded(/*^").Completions, |
| 3877 |
UnorderedElementsAre(labeled("param_int=*/"), labeled("param_char=*/"))); |
| 3878 |
// Comment already has some text in it. |
| 3879 |
EXPECT_THAT(completions(Code + "fun(/* ^").Completions, |
| 3880 |
UnorderedElementsAre(labeled("foo=*/"))); |
| 3881 |
EXPECT_THAT(completions(Code + "fun(/* f^").Completions, |
| 3882 |
UnorderedElementsAre(labeled("foo=*/"))); |
| 3883 |
EXPECT_THAT(completions(Code + "fun(/* x^").Completions, IsEmpty()); |
| 3884 |
EXPECT_THAT(completions(Code + "fun(/* f ^").Completions, IsEmpty()); |
| 3885 |
|
| 3886 |
// Test ranges |
| 3887 |
{ |
| 3888 |
std::string CompletionRangeTest(Code + "fun(/*[[^]]"); |
| 3889 |
auto Results = completions(CompletionRangeTest); |
| 3890 |
EXPECT_THAT(Results.CompletionRange, |
| 3891 |
llvm::ValueIs(Annotations(CompletionRangeTest).range())); |
| 3892 |
EXPECT_THAT( |
| 3893 |
Results.Completions, |
| 3894 |
testing::Each( |
| 3895 |
AllOf(replacesRange(Annotations(CompletionRangeTest).range()), |
| 3896 |
origin(SymbolOrigin::AST), kind(CompletionItemKind::Text)))); |
| 3897 |
} |
| 3898 |
{ |
| 3899 |
std::string CompletionRangeTest(Code + "fun(/*[[fo^]]"); |
| 3900 |
auto Results = completions(CompletionRangeTest); |
| 3901 |
EXPECT_THAT(Results.CompletionRange, |
| 3902 |
llvm::ValueIs(Annotations(CompletionRangeTest).range())); |
| 3903 |
EXPECT_THAT( |
| 3904 |
Results.Completions, |
| 3905 |
testing::Each( |
| 3906 |
AllOf(replacesRange(Annotations(CompletionRangeTest).range()), |
| 3907 |
origin(SymbolOrigin::AST), kind(CompletionItemKind::Text)))); |
| 3908 |
} |
| 3909 |
} |
| 3910 |
|
| 3911 |
TEST(CompletionTest, Concepts) { |
| 3912 |
Annotations Code(R"cpp( |
| 3913 |
template<class T> |
| 3914 |
concept A = sizeof(T) <= 8; |
| 3915 |
|
| 3916 |
template<$tparam^A U> |
| 3917 |
int foo(); |
| 3918 |
|
| 3919 |
template<class T> |
| 3920 |
concept b = $other^A<T> && $other^sizeof(T) % 2 == 0 || $other^A<T> && sizeof(T) == 1; |
| 3921 |
|
| 3922 |
$other^A<T> auto i = 19; |
| 3923 |
)cpp"); |
| 3924 |
TestTU TU; |
| 3925 |
TU.Code = Code.code().str(); |
| 3926 |
TU.ExtraArgs = {"-std=c++20"}; |
| 3927 |
|
| 3928 |
std::vector<Symbol> Syms = {conceptSym("same_as")}; |
| 3929 |
for (auto P : Code.points("tparam")) { |
| 3930 |
ASSERT_THAT(completions(TU, P, Syms).Completions, |
| 3931 |
AllOf(Contains(named("A")), Contains(named("same_as")), |
| 3932 |
Contains(named("class")), Contains(named("typename")))) |
| 3933 |
<< "Completing template parameter at position " << P; |
| 3934 |
} |
| 3935 |
|
| 3936 |
for (auto P : Code.points("other")) { |
| 3937 |
EXPECT_THAT(completions(TU, P, Syms).Completions, |
| 3938 |
AllOf(Contains(named("A")), Contains(named("same_as")))) |
| 3939 |
<< "Completing 'requires' expression at position " << P; |
| 3940 |
} |
| 3941 |
} |
| 3942 |
|
| 3943 |
TEST(SignatureHelp, DocFormat) { |
| 3944 |
Annotations Code(R"cpp( |
| 3945 |
// Comment `with` markup. |
| 3946 |
void foo(int); |
| 3947 |
void bar() { foo(^); } |
| 3948 |
)cpp"); |
| 3949 |
for (auto DocumentationFormat : |
| 3950 |
{MarkupKind::PlainText, MarkupKind::Markdown}) { |
| 3951 |
auto Sigs = signatures(Code.code(), Code.point(), /*IndexSymbols=*/{}, |
| 3952 |
DocumentationFormat); |
| 3953 |
ASSERT_EQ(Sigs.signatures.size(), 1U); |
| 3954 |
EXPECT_EQ(Sigs.signatures[0].documentation.kind, DocumentationFormat); |
| 3955 |
} |
| 3956 |
} |
| 3957 |
|
| 3958 |
TEST(SignatureHelp, TemplateArguments) { |
| 3959 |
std::string Top = R"cpp( |
| 3960 |
template <typename T, int> bool foo(char); |
| 3961 |
template <int I, int> bool foo(float); |
| 3962 |
)cpp"; |
| 3963 |
|
| 3964 |
auto First = signatures(Top + "bool x = foo<^"); |
| 3965 |
EXPECT_THAT( |
| 3966 |
First.signatures, |
| 3967 |
UnorderedElementsAre(sig("foo<[[typename T]], [[int]]>() -> bool"), |
| 3968 |
sig("foo<[[int I]], [[int]]>() -> bool"))); |
| 3969 |
EXPECT_EQ(First.activeParameter, 0); |
| 3970 |
|
| 3971 |
auto Second = signatures(Top + "bool x = foo<1, ^"); |
| 3972 |
EXPECT_THAT(Second.signatures, |
| 3973 |
ElementsAre(sig("foo<[[int I]], [[int]]>() -> bool"))); |
| 3974 |
EXPECT_EQ(Second.activeParameter, 1); |
| 3975 |
} |
| 3976 |
|
| 3977 |
} // namespace |
| 3978 |
} // namespace clangd |
| 3979 |
} // namespace clang |