1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
|
//===-- ClangdTests.cpp - Clangd unit tests ---------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "ClangdLSPServer.h"
#include "ClangdServer.h"
#include "Context.h"
#include "TestFS.h"
#include "clang/Config/config.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Regex.h"
#include "gtest/gtest.h"
#include <algorithm>
#include <chrono>
#include <iostream>
#include <random>
#include <string>
#include <thread>
#include <vector>
namespace clang {
namespace clangd {
namespace {
// Don't wait for async ops in clangd test more than that to avoid blocking
// indefinitely in case of bugs.
static const std::chrono::seconds DefaultFutureTimeout =
std::chrono::seconds(10);
static bool diagsContainErrors(ArrayRef<DiagWithFixIts> Diagnostics) {
for (const auto &DiagAndFixIts : Diagnostics) {
// FIXME: severities returned by clangd should have a descriptive
// diagnostic severity enum
const int ErrorSeverity = 1;
if (DiagAndFixIts.Diag.severity == ErrorSeverity)
return true;
}
return false;
}
class ErrorCheckingDiagConsumer : public DiagnosticsConsumer {
public:
void
onDiagnosticsReady(PathRef File,
Tagged<std::vector<DiagWithFixIts>> Diagnostics) override {
bool HadError = diagsContainErrors(Diagnostics.Value);
std::lock_guard<std::mutex> Lock(Mutex);
HadErrorInLastDiags = HadError;
LastVFSTag = Diagnostics.Tag;
}
bool hadErrorInLastDiags() {
std::lock_guard<std::mutex> Lock(Mutex);
return HadErrorInLastDiags;
}
VFSTag lastVFSTag() { return LastVFSTag; }
private:
std::mutex Mutex;
bool HadErrorInLastDiags = false;
VFSTag LastVFSTag = VFSTag();
};
class ConstantFSProvider : public FileSystemProvider {
public:
ConstantFSProvider(IntrusiveRefCntPtr<vfs::FileSystem> FS,
VFSTag Tag = VFSTag())
: FS(std::move(FS)), Tag(std::move(Tag)) {}
Tagged<IntrusiveRefCntPtr<vfs::FileSystem>>
getTaggedFileSystem(PathRef File) override {
return make_tagged(FS, Tag);
}
private:
IntrusiveRefCntPtr<vfs::FileSystem> FS;
VFSTag Tag;
};
/// Replaces all patterns of the form 0x123abc with spaces
std::string replacePtrsInDump(std::string const &Dump) {
llvm::Regex RE("0x[0-9a-fA-F]+");
llvm::SmallVector<StringRef, 1> Matches;
llvm::StringRef Pending = Dump;
std::string Result;
while (RE.match(Pending, &Matches)) {
assert(Matches.size() == 1 && "Exactly one match expected");
auto MatchPos = Matches[0].data() - Pending.data();
Result += Pending.take_front(MatchPos);
Pending = Pending.drop_front(MatchPos + Matches[0].size());
}
Result += Pending;
return Result;
}
std::string dumpASTWithoutMemoryLocs(ClangdServer &Server, PathRef File) {
auto DumpWithMemLocs = Server.dumpAST(File);
return replacePtrsInDump(DumpWithMemLocs);
}
} // namespace
class ClangdVFSTest : public ::testing::Test {
protected:
std::string parseSourceAndDumpAST(
PathRef SourceFileRelPath, StringRef SourceContents,
std::vector<std::pair<PathRef, StringRef>> ExtraFiles = {},
bool ExpectErrors = false) {
MockFSProvider FS;
ErrorCheckingDiagConsumer DiagConsumer;
MockCompilationDatabase CDB;
ClangdServer Server(CDB, DiagConsumer, FS, getDefaultAsyncThreadsCount(),
/*StorePreamblesInMemory=*/true);
for (const auto &FileWithContents : ExtraFiles)
FS.Files[getVirtualTestFilePath(FileWithContents.first)] =
FileWithContents.second;
auto SourceFilename = getVirtualTestFilePath(SourceFileRelPath);
FS.ExpectedFile = SourceFilename;
// Have to sync reparses because requests are processed on the calling
// thread.
auto AddDocFuture =
Server.addDocument(Context::empty(), SourceFilename, SourceContents);
auto Result = dumpASTWithoutMemoryLocs(Server, SourceFilename);
// Wait for reparse to finish before checking for errors.
EXPECT_EQ(AddDocFuture.wait_for(DefaultFutureTimeout),
std::future_status::ready);
EXPECT_EQ(ExpectErrors, DiagConsumer.hadErrorInLastDiags());
return Result;
}
};
TEST_F(ClangdVFSTest, Parse) {
// FIXME: figure out a stable format for AST dumps, so that we can check the
// output of the dump itself is equal to the expected one, not just that it's
// different.
auto Empty = parseSourceAndDumpAST("foo.cpp", "", {});
auto OneDecl = parseSourceAndDumpAST("foo.cpp", "int a;", {});
auto SomeDecls = parseSourceAndDumpAST("foo.cpp", "int a; int b; int c;", {});
EXPECT_NE(Empty, OneDecl);
EXPECT_NE(Empty, SomeDecls);
EXPECT_NE(SomeDecls, OneDecl);
auto Empty2 = parseSourceAndDumpAST("foo.cpp", "");
auto OneDecl2 = parseSourceAndDumpAST("foo.cpp", "int a;");
auto SomeDecls2 = parseSourceAndDumpAST("foo.cpp", "int a; int b; int c;");
EXPECT_EQ(Empty, Empty2);
EXPECT_EQ(OneDecl, OneDecl2);
EXPECT_EQ(SomeDecls, SomeDecls2);
}
TEST_F(ClangdVFSTest, ParseWithHeader) {
parseSourceAndDumpAST("foo.cpp", "#include \"foo.h\"", {},
/*ExpectErrors=*/true);
parseSourceAndDumpAST("foo.cpp", "#include \"foo.h\"", {{"foo.h", ""}},
/*ExpectErrors=*/false);
const auto SourceContents = R"cpp(
#include "foo.h"
int b = a;
)cpp";
parseSourceAndDumpAST("foo.cpp", SourceContents, {{"foo.h", ""}},
/*ExpectErrors=*/true);
parseSourceAndDumpAST("foo.cpp", SourceContents, {{"foo.h", "int a;"}},
/*ExpectErrors=*/false);
}
TEST_F(ClangdVFSTest, Reparse) {
MockFSProvider FS;
ErrorCheckingDiagConsumer DiagConsumer;
MockCompilationDatabase CDB;
ClangdServer Server(CDB, DiagConsumer, FS, getDefaultAsyncThreadsCount(),
/*StorePreamblesInMemory=*/true);
const auto SourceContents = R"cpp(
#include "foo.h"
int b = a;
)cpp";
auto FooCpp = getVirtualTestFilePath("foo.cpp");
auto FooH = getVirtualTestFilePath("foo.h");
FS.Files[FooH] = "int a;";
FS.Files[FooCpp] = SourceContents;
FS.ExpectedFile = FooCpp;
// To sync reparses before checking for errors.
std::future<Context> ParseFuture;
ParseFuture = Server.addDocument(Context::empty(), FooCpp, SourceContents);
auto DumpParse1 = dumpASTWithoutMemoryLocs(Server, FooCpp);
ASSERT_EQ(ParseFuture.wait_for(DefaultFutureTimeout),
std::future_status::ready);
EXPECT_FALSE(DiagConsumer.hadErrorInLastDiags());
ParseFuture = Server.addDocument(Context::empty(), FooCpp, "");
auto DumpParseEmpty = dumpASTWithoutMemoryLocs(Server, FooCpp);
ASSERT_EQ(ParseFuture.wait_for(DefaultFutureTimeout),
std::future_status::ready);
EXPECT_FALSE(DiagConsumer.hadErrorInLastDiags());
ParseFuture = Server.addDocument(Context::empty(), FooCpp, SourceContents);
auto DumpParse2 = dumpASTWithoutMemoryLocs(Server, FooCpp);
ASSERT_EQ(ParseFuture.wait_for(DefaultFutureTimeout),
std::future_status::ready);
EXPECT_FALSE(DiagConsumer.hadErrorInLastDiags());
EXPECT_EQ(DumpParse1, DumpParse2);
EXPECT_NE(DumpParse1, DumpParseEmpty);
}
TEST_F(ClangdVFSTest, ReparseOnHeaderChange) {
MockFSProvider FS;
ErrorCheckingDiagConsumer DiagConsumer;
MockCompilationDatabase CDB;
ClangdServer Server(CDB, DiagConsumer, FS, getDefaultAsyncThreadsCount(),
/*StorePreamblesInMemory=*/true);
const auto SourceContents = R"cpp(
#include "foo.h"
int b = a;
)cpp";
auto FooCpp = getVirtualTestFilePath("foo.cpp");
auto FooH = getVirtualTestFilePath("foo.h");
FS.Files[FooH] = "int a;";
FS.Files[FooCpp] = SourceContents;
FS.ExpectedFile = FooCpp;
// To sync reparses before checking for errors.
std::future<Context> ParseFuture;
ParseFuture = Server.addDocument(Context::empty(), FooCpp, SourceContents);
auto DumpParse1 = dumpASTWithoutMemoryLocs(Server, FooCpp);
ASSERT_EQ(ParseFuture.wait_for(DefaultFutureTimeout),
std::future_status::ready);
EXPECT_FALSE(DiagConsumer.hadErrorInLastDiags());
FS.Files[FooH] = "";
ParseFuture = Server.forceReparse(Context::empty(), FooCpp);
auto DumpParseDifferent = dumpASTWithoutMemoryLocs(Server, FooCpp);
ASSERT_EQ(ParseFuture.wait_for(DefaultFutureTimeout),
std::future_status::ready);
EXPECT_TRUE(DiagConsumer.hadErrorInLastDiags());
FS.Files[FooH] = "int a;";
ParseFuture = Server.forceReparse(Context::empty(), FooCpp);
auto DumpParse2 = dumpASTWithoutMemoryLocs(Server, FooCpp);
EXPECT_EQ(ParseFuture.wait_for(DefaultFutureTimeout),
std::future_status::ready);
EXPECT_FALSE(DiagConsumer.hadErrorInLastDiags());
EXPECT_EQ(DumpParse1, DumpParse2);
EXPECT_NE(DumpParse1, DumpParseDifferent);
}
TEST_F(ClangdVFSTest, CheckVersions) {
MockFSProvider FS;
ErrorCheckingDiagConsumer DiagConsumer;
MockCompilationDatabase CDB;
// Run ClangdServer synchronously.
ClangdServer Server(CDB, DiagConsumer, FS,
/*AsyncThreadsCount=*/0,
/*StorePreamblesInMemory=*/true);
auto FooCpp = getVirtualTestFilePath("foo.cpp");
const auto SourceContents = "int a;";
FS.Files[FooCpp] = SourceContents;
FS.ExpectedFile = FooCpp;
// Use default completion options.
clangd::CodeCompleteOptions CCOpts;
// No need to sync reparses, because requests are processed on the calling
// thread.
FS.Tag = "123";
Server.addDocument(Context::empty(), FooCpp, SourceContents);
EXPECT_EQ(
Server.codeComplete(Context::empty(), FooCpp, Position{0, 0}, CCOpts)
.get()
.second.Tag,
FS.Tag);
EXPECT_EQ(DiagConsumer.lastVFSTag(), FS.Tag);
FS.Tag = "321";
Server.addDocument(Context::empty(), FooCpp, SourceContents);
EXPECT_EQ(DiagConsumer.lastVFSTag(), FS.Tag);
EXPECT_EQ(
Server.codeComplete(Context::empty(), FooCpp, Position{0, 0}, CCOpts)
.get()
.second.Tag,
FS.Tag);
}
// Only enable this test on Unix
#ifdef LLVM_ON_UNIX
TEST_F(ClangdVFSTest, SearchLibDir) {
// Checks that searches for GCC installation is done through vfs.
MockFSProvider FS;
ErrorCheckingDiagConsumer DiagConsumer;
MockCompilationDatabase CDB;
CDB.ExtraClangFlags.insert(CDB.ExtraClangFlags.end(),
{"-xc++", "-target", "x86_64-linux-unknown",
"-m64", "--gcc-toolchain=/randomusr",
"-stdlib=libstdc++"});
// Run ClangdServer synchronously.
ClangdServer Server(CDB, DiagConsumer, FS,
/*AsyncThreadsCount=*/0,
/*StorePreamblesInMemory=*/true);
// Just a random gcc version string
SmallString<8> Version("4.9.3");
// A lib dir for gcc installation
SmallString<64> LibDir("/randomusr/lib/gcc/x86_64-linux-gnu");
llvm::sys::path::append(LibDir, Version);
// Put crtbegin.o into LibDir/64 to trick clang into thinking there's a gcc
// installation there.
SmallString<64> DummyLibFile;
llvm::sys::path::append(DummyLibFile, LibDir, "64", "crtbegin.o");
FS.Files[DummyLibFile] = "";
SmallString<64> IncludeDir("/randomusr/include/c++");
llvm::sys::path::append(IncludeDir, Version);
SmallString<64> StringPath;
llvm::sys::path::append(StringPath, IncludeDir, "string");
FS.Files[StringPath] = "class mock_string {};";
auto FooCpp = getVirtualTestFilePath("foo.cpp");
const auto SourceContents = R"cpp(
#include <string>
mock_string x;
)cpp";
FS.Files[FooCpp] = SourceContents;
// No need to sync reparses, because requests are processed on the calling
// thread.
Server.addDocument(Context::empty(), FooCpp, SourceContents);
EXPECT_FALSE(DiagConsumer.hadErrorInLastDiags());
const auto SourceContentsWithError = R"cpp(
#include <string>
std::string x;
)cpp";
Server.addDocument(Context::empty(), FooCpp, SourceContentsWithError);
EXPECT_TRUE(DiagConsumer.hadErrorInLastDiags());
}
#endif // LLVM_ON_UNIX
TEST_F(ClangdVFSTest, ForceReparseCompileCommand) {
MockFSProvider FS;
ErrorCheckingDiagConsumer DiagConsumer;
MockCompilationDatabase CDB;
ClangdServer Server(CDB, DiagConsumer, FS,
/*AsyncThreadsCount=*/0,
/*StorePreamblesInMemory=*/true);
// No need to sync reparses, because reparses are performed on the calling
// thread.
auto FooCpp = getVirtualTestFilePath("foo.cpp");
const auto SourceContents1 = R"cpp(
template <class T>
struct foo { T x; };
)cpp";
const auto SourceContents2 = R"cpp(
template <class T>
struct bar { T x; };
)cpp";
FS.Files[FooCpp] = "";
FS.ExpectedFile = FooCpp;
// First parse files in C mode and check they produce errors.
CDB.ExtraClangFlags = {"-xc"};
Server.addDocument(Context::empty(), FooCpp, SourceContents1);
EXPECT_TRUE(DiagConsumer.hadErrorInLastDiags());
Server.addDocument(Context::empty(), FooCpp, SourceContents2);
EXPECT_TRUE(DiagConsumer.hadErrorInLastDiags());
// Now switch to C++ mode.
CDB.ExtraClangFlags = {"-xc++"};
// Currently, addDocument never checks if CompileCommand has changed, so we
// expect to see the errors.
Server.addDocument(Context::empty(), FooCpp, SourceContents1);
EXPECT_TRUE(DiagConsumer.hadErrorInLastDiags());
Server.addDocument(Context::empty(), FooCpp, SourceContents2);
EXPECT_TRUE(DiagConsumer.hadErrorInLastDiags());
// But forceReparse should reparse the file with proper flags.
Server.forceReparse(Context::empty(), FooCpp);
EXPECT_FALSE(DiagConsumer.hadErrorInLastDiags());
// Subsequent addDocument calls should finish without errors too.
Server.addDocument(Context::empty(), FooCpp, SourceContents1);
EXPECT_FALSE(DiagConsumer.hadErrorInLastDiags());
Server.addDocument(Context::empty(), FooCpp, SourceContents2);
EXPECT_FALSE(DiagConsumer.hadErrorInLastDiags());
}
class ClangdThreadingTest : public ClangdVFSTest {};
TEST_F(ClangdThreadingTest, StressTest) {
// Without 'static' clang gives an error for a usage inside TestDiagConsumer.
static const unsigned FilesCount = 5;
const unsigned RequestsCount = 500;
// Blocking requests wait for the parsing to complete, they slow down the test
// dramatically, so they are issued rarely. Each
// BlockingRequestInterval-request will be a blocking one.
const unsigned BlockingRequestInterval = 40;
const auto SourceContentsWithoutErrors = R"cpp(
int a;
int b;
int c;
int d;
)cpp";
const auto SourceContentsWithErrors = R"cpp(
int a = x;
int b;
int c;
int d;
)cpp";
// Giving invalid line and column number should not crash ClangdServer, but
// just to make sure we're sometimes hitting the bounds inside the file we
// limit the intervals of line and column number that are generated.
unsigned MaxLineForFileRequests = 7;
unsigned MaxColumnForFileRequests = 10;
std::vector<SmallString<32>> FilePaths;
FilePaths.reserve(FilesCount);
for (unsigned I = 0; I < FilesCount; ++I)
FilePaths.push_back(getVirtualTestFilePath(std::string("Foo") +
std::to_string(I) + ".cpp"));
// Mark all of those files as existing.
llvm::StringMap<std::string> FileContents;
for (auto &&FilePath : FilePaths)
FileContents[FilePath] = "";
ConstantFSProvider FS(buildTestFS(FileContents));
struct FileStat {
unsigned HitsWithoutErrors = 0;
unsigned HitsWithErrors = 0;
bool HadErrorsInLastDiags = false;
};
class TestDiagConsumer : public DiagnosticsConsumer {
public:
TestDiagConsumer() : Stats(FilesCount, FileStat()) {}
void onDiagnosticsReady(
PathRef File,
Tagged<std::vector<DiagWithFixIts>> Diagnostics) override {
StringRef FileIndexStr = llvm::sys::path::stem(File);
ASSERT_TRUE(FileIndexStr.consume_front("Foo"));
unsigned long FileIndex = std::stoul(FileIndexStr.str());
bool HadError = diagsContainErrors(Diagnostics.Value);
std::lock_guard<std::mutex> Lock(Mutex);
if (HadError)
Stats[FileIndex].HitsWithErrors++;
else
Stats[FileIndex].HitsWithoutErrors++;
Stats[FileIndex].HadErrorsInLastDiags = HadError;
}
std::vector<FileStat> takeFileStats() {
std::lock_guard<std::mutex> Lock(Mutex);
return std::move(Stats);
}
private:
std::mutex Mutex;
std::vector<FileStat> Stats;
};
struct RequestStats {
unsigned RequestsWithoutErrors = 0;
unsigned RequestsWithErrors = 0;
bool LastContentsHadErrors = false;
bool FileIsRemoved = true;
std::future<Context> LastRequestFuture;
};
std::vector<RequestStats> ReqStats;
ReqStats.reserve(FilesCount);
for (unsigned FileIndex = 0; FileIndex < FilesCount; ++FileIndex)
ReqStats.emplace_back();
TestDiagConsumer DiagConsumer;
{
MockCompilationDatabase CDB;
ClangdServer Server(CDB, DiagConsumer, FS, getDefaultAsyncThreadsCount(),
/*StorePreamblesInMemory=*/true);
// Prepare some random distributions for the test.
std::random_device RandGen;
std::uniform_int_distribution<unsigned> FileIndexDist(0, FilesCount - 1);
// Pass a text that contains compiler errors to addDocument in about 20% of
// all requests.
std::bernoulli_distribution ShouldHaveErrorsDist(0.2);
// Line and Column numbers for requests that need them.
std::uniform_int_distribution<int> LineDist(0, MaxLineForFileRequests);
std::uniform_int_distribution<int> ColumnDist(0, MaxColumnForFileRequests);
// Some helpers.
auto UpdateStatsOnAddDocument = [&](unsigned FileIndex, bool HadErrors,
std::future<Context> Future) {
auto &Stats = ReqStats[FileIndex];
if (HadErrors)
++Stats.RequestsWithErrors;
else
++Stats.RequestsWithoutErrors;
Stats.LastContentsHadErrors = HadErrors;
Stats.FileIsRemoved = false;
Stats.LastRequestFuture = std::move(Future);
};
auto UpdateStatsOnRemoveDocument = [&](unsigned FileIndex,
std::future<Context> Future) {
auto &Stats = ReqStats[FileIndex];
Stats.FileIsRemoved = true;
Stats.LastRequestFuture = std::move(Future);
};
auto UpdateStatsOnForceReparse = [&](unsigned FileIndex,
std::future<Context> Future) {
auto &Stats = ReqStats[FileIndex];
Stats.LastRequestFuture = std::move(Future);
if (Stats.LastContentsHadErrors)
++Stats.RequestsWithErrors;
else
++Stats.RequestsWithoutErrors;
};
auto AddDocument = [&](unsigned FileIndex) {
bool ShouldHaveErrors = ShouldHaveErrorsDist(RandGen);
auto Future =
Server.addDocument(Context::empty(), FilePaths[FileIndex],
ShouldHaveErrors ? SourceContentsWithErrors
: SourceContentsWithoutErrors);
UpdateStatsOnAddDocument(FileIndex, ShouldHaveErrors, std::move(Future));
};
// Various requests that we would randomly run.
auto AddDocumentRequest = [&]() {
unsigned FileIndex = FileIndexDist(RandGen);
AddDocument(FileIndex);
};
auto ForceReparseRequest = [&]() {
unsigned FileIndex = FileIndexDist(RandGen);
// Make sure we don't violate the ClangdServer's contract.
if (ReqStats[FileIndex].FileIsRemoved)
AddDocument(FileIndex);
auto Future = Server.forceReparse(Context::empty(), FilePaths[FileIndex]);
UpdateStatsOnForceReparse(FileIndex, std::move(Future));
};
auto RemoveDocumentRequest = [&]() {
unsigned FileIndex = FileIndexDist(RandGen);
// Make sure we don't violate the ClangdServer's contract.
if (ReqStats[FileIndex].FileIsRemoved)
AddDocument(FileIndex);
auto Future =
Server.removeDocument(Context::empty(), FilePaths[FileIndex]);
UpdateStatsOnRemoveDocument(FileIndex, std::move(Future));
};
auto CodeCompletionRequest = [&]() {
unsigned FileIndex = FileIndexDist(RandGen);
// Make sure we don't violate the ClangdServer's contract.
if (ReqStats[FileIndex].FileIsRemoved)
AddDocument(FileIndex);
Position Pos{LineDist(RandGen), ColumnDist(RandGen)};
// FIXME(ibiryukov): Also test async completion requests.
// Simply putting CodeCompletion into async requests now would make
// tests slow, since there's no way to cancel previous completion
// requests as opposed to AddDocument/RemoveDocument, which are implicitly
// cancelled by any subsequent AddDocument/RemoveDocument request to the
// same file.
Server
.codeComplete(Context::empty(), FilePaths[FileIndex], Pos,
clangd::CodeCompleteOptions())
.wait();
};
auto FindDefinitionsRequest = [&]() {
unsigned FileIndex = FileIndexDist(RandGen);
// Make sure we don't violate the ClangdServer's contract.
if (ReqStats[FileIndex].FileIsRemoved)
AddDocument(FileIndex);
Position Pos{LineDist(RandGen), ColumnDist(RandGen)};
ASSERT_TRUE(!!Server.findDefinitions(Context::empty(),
FilePaths[FileIndex], Pos));
};
std::vector<std::function<void()>> AsyncRequests = {
AddDocumentRequest, ForceReparseRequest, RemoveDocumentRequest};
std::vector<std::function<void()>> BlockingRequests = {
CodeCompletionRequest, FindDefinitionsRequest};
// Bash requests to ClangdServer in a loop.
std::uniform_int_distribution<int> AsyncRequestIndexDist(
0, AsyncRequests.size() - 1);
std::uniform_int_distribution<int> BlockingRequestIndexDist(
0, BlockingRequests.size() - 1);
for (unsigned I = 1; I <= RequestsCount; ++I) {
if (I % BlockingRequestInterval != 0) {
// Issue an async request most of the time. It should be fast.
unsigned RequestIndex = AsyncRequestIndexDist(RandGen);
AsyncRequests[RequestIndex]();
} else {
// Issue a blocking request once in a while.
auto RequestIndex = BlockingRequestIndexDist(RandGen);
BlockingRequests[RequestIndex]();
}
}
// Wait for last requests to finish.
for (auto &ReqStat : ReqStats) {
if (!ReqStat.LastRequestFuture.valid())
continue; // We never ran any requests for this file.
// Future should be ready much earlier than in 5 seconds, the timeout is
// there to check we won't wait indefinitely.
ASSERT_EQ(ReqStat.LastRequestFuture.wait_for(std::chrono::seconds(5)),
std::future_status::ready);
}
} // Wait for ClangdServer to shutdown before proceeding.
// Check some invariants about the state of the program.
std::vector<FileStat> Stats = DiagConsumer.takeFileStats();
for (unsigned I = 0; I < FilesCount; ++I) {
if (!ReqStats[I].FileIsRemoved) {
ASSERT_EQ(Stats[I].HadErrorsInLastDiags,
ReqStats[I].LastContentsHadErrors);
}
ASSERT_LE(Stats[I].HitsWithErrors, ReqStats[I].RequestsWithErrors);
ASSERT_LE(Stats[I].HitsWithoutErrors, ReqStats[I].RequestsWithoutErrors);
}
}
TEST_F(ClangdVFSTest, CheckSourceHeaderSwitch) {
MockFSProvider FS;
ErrorCheckingDiagConsumer DiagConsumer;
MockCompilationDatabase CDB;
ClangdServer Server(CDB, DiagConsumer, FS, getDefaultAsyncThreadsCount(),
/*StorePreamblesInMemory=*/true);
auto SourceContents = R"cpp(
#include "foo.h"
int b = a;
)cpp";
auto FooCpp = getVirtualTestFilePath("foo.cpp");
auto FooH = getVirtualTestFilePath("foo.h");
auto Invalid = getVirtualTestFilePath("main.cpp");
FS.Files[FooCpp] = SourceContents;
FS.Files[FooH] = "int a;";
FS.Files[Invalid] = "int main() { \n return 0; \n }";
llvm::Optional<Path> PathResult = Server.switchSourceHeader(FooCpp);
EXPECT_TRUE(PathResult.hasValue());
ASSERT_EQ(PathResult.getValue(), FooH);
PathResult = Server.switchSourceHeader(FooH);
EXPECT_TRUE(PathResult.hasValue());
ASSERT_EQ(PathResult.getValue(), FooCpp);
SourceContents = R"c(
#include "foo.HH"
int b = a;
)c";
// Test with header file in capital letters and different extension, source
// file with different extension
auto FooC = getVirtualTestFilePath("bar.c");
auto FooHH = getVirtualTestFilePath("bar.HH");
FS.Files[FooC] = SourceContents;
FS.Files[FooHH] = "int a;";
PathResult = Server.switchSourceHeader(FooC);
EXPECT_TRUE(PathResult.hasValue());
ASSERT_EQ(PathResult.getValue(), FooHH);
// Test with both capital letters
auto Foo2C = getVirtualTestFilePath("foo2.C");
auto Foo2HH = getVirtualTestFilePath("foo2.HH");
FS.Files[Foo2C] = SourceContents;
FS.Files[Foo2HH] = "int a;";
PathResult = Server.switchSourceHeader(Foo2C);
EXPECT_TRUE(PathResult.hasValue());
ASSERT_EQ(PathResult.getValue(), Foo2HH);
// Test with source file as capital letter and .hxx header file
auto Foo3C = getVirtualTestFilePath("foo3.C");
auto Foo3HXX = getVirtualTestFilePath("foo3.hxx");
SourceContents = R"c(
#include "foo3.hxx"
int b = a;
)c";
FS.Files[Foo3C] = SourceContents;
FS.Files[Foo3HXX] = "int a;";
PathResult = Server.switchSourceHeader(Foo3C);
EXPECT_TRUE(PathResult.hasValue());
ASSERT_EQ(PathResult.getValue(), Foo3HXX);
// Test if asking for a corresponding file that doesn't exist returns an empty
// string.
PathResult = Server.switchSourceHeader(Invalid);
EXPECT_FALSE(PathResult.hasValue());
}
TEST_F(ClangdThreadingTest, NoConcurrentDiagnostics) {
class NoConcurrentAccessDiagConsumer : public DiagnosticsConsumer {
public:
NoConcurrentAccessDiagConsumer(std::promise<void> StartSecondReparse)
: StartSecondReparse(std::move(StartSecondReparse)) {}
void onDiagnosticsReady(
PathRef File,
Tagged<std::vector<DiagWithFixIts>> Diagnostics) override {
std::unique_lock<std::mutex> Lock(Mutex, std::try_to_lock_t());
ASSERT_TRUE(Lock.owns_lock())
<< "Detected concurrent onDiagnosticsReady calls for the same file.";
if (FirstRequest) {
FirstRequest = false;
StartSecondReparse.set_value();
// Sleep long enough for the second request to be processed.
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
}
private:
std::mutex Mutex;
bool FirstRequest = true;
std::promise<void> StartSecondReparse;
};
const auto SourceContentsWithoutErrors = R"cpp(
int a;
int b;
int c;
int d;
)cpp";
const auto SourceContentsWithErrors = R"cpp(
int a = x;
int b;
int c;
int d;
)cpp";
auto FooCpp = getVirtualTestFilePath("foo.cpp");
llvm::StringMap<std::string> FileContents;
FileContents[FooCpp] = "";
ConstantFSProvider FS(buildTestFS(FileContents));
std::promise<void> StartSecondReparsePromise;
std::future<void> StartSecondReparse = StartSecondReparsePromise.get_future();
NoConcurrentAccessDiagConsumer DiagConsumer(
std::move(StartSecondReparsePromise));
MockCompilationDatabase CDB;
ClangdServer Server(CDB, DiagConsumer, FS, 4,
/*StorePreamblesInMemory=*/true);
Server.addDocument(Context::empty(), FooCpp, SourceContentsWithErrors);
StartSecondReparse.wait();
auto Future =
Server.addDocument(Context::empty(), FooCpp, SourceContentsWithoutErrors);
Future.wait();
}
} // namespace clangd
} // namespace clang
|