TUSchedulerTests.cpp
39.8 KB
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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
//===-- TUSchedulerTests.cpp ------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "Annotations.h"
#include "ClangdServer.h"
#include "Diagnostics.h"
#include "Matchers.h"
#include "ParsedAST.h"
#include "Preamble.h"
#include "TUScheduler.h"
#include "TestFS.h"
#include "support/Cancellation.h"
#include "support/Context.h"
#include "support/Path.h"
#include "support/TestTracer.h"
#include "support/Threading.h"
#include "support/ThreadsafeFS.h"
#include "clang/Basic/DiagnosticDriver.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/FunctionExtras.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
namespace clang {
namespace clangd {
namespace {
using ::testing::AnyOf;
using ::testing::Each;
using ::testing::ElementsAre;
using ::testing::Eq;
using ::testing::Field;
using ::testing::IsEmpty;
using ::testing::Pointee;
using ::testing::SizeIs;
using ::testing::UnorderedElementsAre;
MATCHER_P2(TUState, PreambleActivity, ASTActivity, "") {
if (arg.PreambleActivity != PreambleActivity) {
*result_listener << "preamblestate is "
<< static_cast<uint8_t>(arg.PreambleActivity);
return false;
}
if (arg.ASTActivity.K != ASTActivity) {
*result_listener << "aststate is " << arg.ASTActivity.K;
return false;
}
return true;
}
// Dummy ContextProvider to verify the provider is invoked & contexts are used.
static Key<std::string> BoundPath;
Context bindPath(PathRef F) {
return Context::current().derive(BoundPath, F.str());
}
llvm::StringRef boundPath() {
const std::string *V = Context::current().get(BoundPath);
return V ? *V : llvm::StringRef("");
}
TUScheduler::Options optsForTest() {
TUScheduler::Options Opts(ClangdServer::optsForTest());
Opts.ContextProvider = bindPath;
return Opts;
}
class TUSchedulerTests : public ::testing::Test {
protected:
ParseInputs getInputs(PathRef File, std::string Contents) {
ParseInputs Inputs;
Inputs.CompileCommand = *CDB.getCompileCommand(File);
Inputs.TFS = &FS;
Inputs.Contents = std::move(Contents);
Inputs.Opts = ParseOptions();
return Inputs;
}
void updateWithCallback(TUScheduler &S, PathRef File,
llvm::StringRef Contents, WantDiagnostics WD,
llvm::unique_function<void()> CB) {
updateWithCallback(S, File, getInputs(File, std::string(Contents)), WD,
std::move(CB));
}
void updateWithCallback(TUScheduler &S, PathRef File, ParseInputs Inputs,
WantDiagnostics WD,
llvm::unique_function<void()> CB) {
WithContextValue Ctx(llvm::make_scope_exit(std::move(CB)));
S.update(File, Inputs, WD);
}
static Key<llvm::unique_function<void(PathRef File, std::vector<Diag>)>>
DiagsCallbackKey;
/// A diagnostics callback that should be passed to TUScheduler when it's used
/// in updateWithDiags.
static std::unique_ptr<ParsingCallbacks> captureDiags() {
class CaptureDiags : public ParsingCallbacks {
public:
void onMainAST(PathRef File, ParsedAST &AST, PublishFn Publish) override {
reportDiagnostics(File, AST.getDiagnostics(), Publish);
}
void onFailedAST(PathRef File, llvm::StringRef Version,
std::vector<Diag> Diags, PublishFn Publish) override {
reportDiagnostics(File, Diags, Publish);
}
private:
void reportDiagnostics(PathRef File, llvm::ArrayRef<Diag> Diags,
PublishFn Publish) {
auto D = Context::current().get(DiagsCallbackKey);
if (!D)
return;
Publish([&]() {
const_cast<
llvm::unique_function<void(PathRef, std::vector<Diag>)> &> (*D)(
File, std::move(Diags));
});
}
};
return std::make_unique<CaptureDiags>();
}
/// Schedule an update and call \p CB with the diagnostics it produces, if
/// any. The TUScheduler should be created with captureDiags as a
/// DiagsCallback for this to work.
void updateWithDiags(TUScheduler &S, PathRef File, ParseInputs Inputs,
WantDiagnostics WD,
llvm::unique_function<void(std::vector<Diag>)> CB) {
Path OrigFile = File.str();
WithContextValue Ctx(DiagsCallbackKey,
[OrigFile, CB = std::move(CB)](
PathRef File, std::vector<Diag> Diags) mutable {
assert(File == OrigFile);
CB(std::move(Diags));
});
S.update(File, std::move(Inputs), WD);
}
void updateWithDiags(TUScheduler &S, PathRef File, llvm::StringRef Contents,
WantDiagnostics WD,
llvm::unique_function<void(std::vector<Diag>)> CB) {
return updateWithDiags(S, File, getInputs(File, std::string(Contents)), WD,
std::move(CB));
}
MockFS FS;
MockCompilationDatabase CDB;
};
Key<llvm::unique_function<void(PathRef File, std::vector<Diag>)>>
TUSchedulerTests::DiagsCallbackKey;
TEST_F(TUSchedulerTests, MissingFiles) {
TUScheduler S(CDB, optsForTest());
auto Added = testPath("added.cpp");
FS.Files[Added] = "x";
auto Missing = testPath("missing.cpp");
FS.Files[Missing] = "";
S.update(Added, getInputs(Added, "x"), WantDiagnostics::No);
// Assert each operation for missing file is an error (even if it's
// available in VFS).
S.runWithAST("", Missing,
[&](Expected<InputsAndAST> AST) { EXPECT_ERROR(AST); });
S.runWithPreamble(
"", Missing, TUScheduler::Stale,
[&](Expected<InputsAndPreamble> Preamble) { EXPECT_ERROR(Preamble); });
// remove() shouldn't crash on missing files.
S.remove(Missing);
// Assert there aren't any errors for added file.
S.runWithAST("", Added,
[&](Expected<InputsAndAST> AST) { EXPECT_TRUE(bool(AST)); });
S.runWithPreamble("", Added, TUScheduler::Stale,
[&](Expected<InputsAndPreamble> Preamble) {
EXPECT_TRUE(bool(Preamble));
});
S.remove(Added);
// Assert that all operations fail after removing the file.
S.runWithAST("", Added,
[&](Expected<InputsAndAST> AST) { EXPECT_ERROR(AST); });
S.runWithPreamble("", Added, TUScheduler::Stale,
[&](Expected<InputsAndPreamble> Preamble) {
ASSERT_FALSE(bool(Preamble));
llvm::consumeError(Preamble.takeError());
});
// remove() shouldn't crash on missing files.
S.remove(Added);
}
TEST_F(TUSchedulerTests, WantDiagnostics) {
std::atomic<int> CallbackCount(0);
{
// To avoid a racy test, don't allow tasks to actually run on the worker
// thread until we've scheduled them all.
Notification Ready;
TUScheduler S(CDB, optsForTest(), captureDiags());
auto Path = testPath("foo.cpp");
updateWithDiags(S, Path, "", WantDiagnostics::Yes,
[&](std::vector<Diag>) { Ready.wait(); });
updateWithDiags(S, Path, "request diags", WantDiagnostics::Yes,
[&](std::vector<Diag>) { ++CallbackCount; });
updateWithDiags(S, Path, "auto (clobbered)", WantDiagnostics::Auto,
[&](std::vector<Diag>) {
ADD_FAILURE()
<< "auto should have been cancelled by auto";
});
updateWithDiags(S, Path, "request no diags", WantDiagnostics::No,
[&](std::vector<Diag>) {
ADD_FAILURE() << "no diags should not be called back";
});
updateWithDiags(S, Path, "auto (produces)", WantDiagnostics::Auto,
[&](std::vector<Diag>) { ++CallbackCount; });
Ready.notify();
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
}
EXPECT_EQ(2, CallbackCount);
}
TEST_F(TUSchedulerTests, Debounce) {
std::atomic<int> CallbackCount(0);
{
auto Opts = optsForTest();
Opts.UpdateDebounce = DebouncePolicy::fixed(std::chrono::seconds(1));
TUScheduler S(CDB, Opts, captureDiags());
// FIXME: we could probably use timeouts lower than 1 second here.
auto Path = testPath("foo.cpp");
updateWithDiags(S, Path, "auto (debounced)", WantDiagnostics::Auto,
[&](std::vector<Diag>) {
ADD_FAILURE()
<< "auto should have been debounced and canceled";
});
std::this_thread::sleep_for(std::chrono::milliseconds(200));
updateWithDiags(S, Path, "auto (timed out)", WantDiagnostics::Auto,
[&](std::vector<Diag>) { ++CallbackCount; });
std::this_thread::sleep_for(std::chrono::seconds(2));
updateWithDiags(S, Path, "auto (shut down)", WantDiagnostics::Auto,
[&](std::vector<Diag>) { ++CallbackCount; });
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
}
EXPECT_EQ(2, CallbackCount);
}
TEST_F(TUSchedulerTests, Cancellation) {
// We have the following update/read sequence
// U0
// U1(WantDiags=Yes) <-- cancelled
// R1 <-- cancelled
// U2(WantDiags=Yes) <-- cancelled
// R2A <-- cancelled
// R2B
// U3(WantDiags=Yes)
// R3 <-- cancelled
std::vector<std::string> DiagsSeen, ReadsSeen, ReadsCanceled;
{
Notification Proceed; // Ensure we schedule everything.
TUScheduler S(CDB, optsForTest(), captureDiags());
auto Path = testPath("foo.cpp");
// Helper to schedule a named update and return a function to cancel it.
auto Update = [&](std::string ID) -> Canceler {
auto T = cancelableTask();
WithContext C(std::move(T.first));
updateWithDiags(
S, Path, "//" + ID, WantDiagnostics::Yes,
[&, ID](std::vector<Diag> Diags) { DiagsSeen.push_back(ID); });
return std::move(T.second);
};
// Helper to schedule a named read and return a function to cancel it.
auto Read = [&](std::string ID) -> Canceler {
auto T = cancelableTask();
WithContext C(std::move(T.first));
S.runWithAST(ID, Path, [&, ID](llvm::Expected<InputsAndAST> E) {
if (auto Err = E.takeError()) {
if (Err.isA<CancelledError>()) {
ReadsCanceled.push_back(ID);
consumeError(std::move(Err));
} else {
ADD_FAILURE() << "Non-cancelled error for " << ID << ": "
<< llvm::toString(std::move(Err));
}
} else {
ReadsSeen.push_back(ID);
}
});
return std::move(T.second);
};
updateWithCallback(S, Path, "", WantDiagnostics::Yes,
[&]() { Proceed.wait(); });
// The second parens indicate cancellation, where present.
Update("U1")();
Read("R1")();
Update("U2")();
Read("R2A")();
Read("R2B");
Update("U3");
Read("R3")();
Proceed.notify();
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
}
EXPECT_THAT(DiagsSeen, ElementsAre("U2", "U3"))
<< "U1 and all dependent reads were cancelled. "
"U2 has a dependent read R2A. "
"U3 was not cancelled.";
EXPECT_THAT(ReadsSeen, ElementsAre("R2B"))
<< "All reads other than R2B were cancelled";
EXPECT_THAT(ReadsCanceled, ElementsAre("R1", "R2A", "R3"))
<< "All reads other than R2B were cancelled";
}
TEST_F(TUSchedulerTests, InvalidationNoCrash) {
auto Path = testPath("foo.cpp");
TUScheduler S(CDB, optsForTest(), captureDiags());
Notification StartedRunning;
Notification ScheduledChange;
// We expect invalidation logic to not crash by trying to invalidate a running
// request.
S.update(Path, getInputs(Path, ""), WantDiagnostics::Auto);
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
S.runWithAST(
"invalidatable-but-running", Path,
[&](llvm::Expected<InputsAndAST> AST) {
StartedRunning.notify();
ScheduledChange.wait();
ASSERT_TRUE(bool(AST));
},
TUScheduler::InvalidateOnUpdate);
StartedRunning.wait();
S.update(Path, getInputs(Path, ""), WantDiagnostics::Auto);
ScheduledChange.notify();
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
}
TEST_F(TUSchedulerTests, Invalidation) {
auto Path = testPath("foo.cpp");
TUScheduler S(CDB, optsForTest(), captureDiags());
std::atomic<int> Builds(0), Actions(0);
Notification Start;
updateWithDiags(S, Path, "a", WantDiagnostics::Yes, [&](std::vector<Diag>) {
++Builds;
Start.wait();
});
S.runWithAST(
"invalidatable", Path,
[&](llvm::Expected<InputsAndAST> AST) {
++Actions;
EXPECT_FALSE(bool(AST));
llvm::Error E = AST.takeError();
EXPECT_TRUE(E.isA<CancelledError>());
handleAllErrors(std::move(E), [&](const CancelledError &E) {
EXPECT_EQ(E.Reason, static_cast<int>(ErrorCode::ContentModified));
});
},
TUScheduler::InvalidateOnUpdate);
S.runWithAST(
"not-invalidatable", Path,
[&](llvm::Expected<InputsAndAST> AST) {
++Actions;
EXPECT_TRUE(bool(AST));
},
TUScheduler::NoInvalidation);
updateWithDiags(S, Path, "b", WantDiagnostics::Auto, [&](std::vector<Diag>) {
++Builds;
ADD_FAILURE() << "Shouldn't build, all dependents invalidated";
});
S.runWithAST(
"invalidatable", Path,
[&](llvm::Expected<InputsAndAST> AST) {
++Actions;
EXPECT_FALSE(bool(AST));
llvm::Error E = AST.takeError();
EXPECT_TRUE(E.isA<CancelledError>());
consumeError(std::move(E));
},
TUScheduler::InvalidateOnUpdate);
updateWithDiags(S, Path, "c", WantDiagnostics::Auto,
[&](std::vector<Diag>) { ++Builds; });
S.runWithAST(
"invalidatable", Path,
[&](llvm::Expected<InputsAndAST> AST) {
++Actions;
EXPECT_TRUE(bool(AST)) << "Shouldn't be invalidated, no update follows";
},
TUScheduler::InvalidateOnUpdate);
Start.notify();
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
EXPECT_EQ(2, Builds.load()) << "Middle build should be skipped";
EXPECT_EQ(4, Actions.load()) << "All actions should run (some with error)";
}
TEST_F(TUSchedulerTests, ManyUpdates) {
const int FilesCount = 3;
const int UpdatesPerFile = 10;
std::mutex Mut;
int TotalASTReads = 0;
int TotalPreambleReads = 0;
int TotalUpdates = 0;
llvm::StringMap<int> LatestDiagVersion;
// Run TUScheduler and collect some stats.
{
auto Opts = optsForTest();
Opts.UpdateDebounce = DebouncePolicy::fixed(std::chrono::milliseconds(50));
TUScheduler S(CDB, Opts, captureDiags());
std::vector<std::string> Files;
for (int I = 0; I < FilesCount; ++I) {
std::string Name = "foo" + std::to_string(I) + ".cpp";
Files.push_back(testPath(Name));
this->FS.Files[Files.back()] = "";
}
StringRef Contents1 = R"cpp(int a;)cpp";
StringRef Contents2 = R"cpp(int main() { return 1; })cpp";
StringRef Contents3 = R"cpp(int a; int b; int sum() { return a + b; })cpp";
StringRef AllContents[] = {Contents1, Contents2, Contents3};
const int AllContentsSize = 3;
// Scheduler may run tasks asynchronously, but should propagate the
// context. We stash a nonce in the context, and verify it in the task.
static Key<int> NonceKey;
int Nonce = 0;
for (int FileI = 0; FileI < FilesCount; ++FileI) {
for (int UpdateI = 0; UpdateI < UpdatesPerFile; ++UpdateI) {
auto Contents = AllContents[(FileI + UpdateI) % AllContentsSize];
auto File = Files[FileI];
auto Inputs = getInputs(File, Contents.str());
{
WithContextValue WithNonce(NonceKey, ++Nonce);
Inputs.Version = std::to_string(UpdateI);
updateWithDiags(
S, File, Inputs, WantDiagnostics::Auto,
[File, Nonce, Version(Inputs.Version), &Mut, &TotalUpdates,
&LatestDiagVersion](std::vector<Diag>) {
EXPECT_THAT(Context::current().get(NonceKey), Pointee(Nonce));
EXPECT_EQ(File, boundPath());
std::lock_guard<std::mutex> Lock(Mut);
++TotalUpdates;
EXPECT_EQ(File, *TUScheduler::getFileBeingProcessedInContext());
// Make sure Diags are for a newer version.
auto It = LatestDiagVersion.try_emplace(File, -1);
const int PrevVersion = It.first->second;
int CurVersion;
ASSERT_TRUE(llvm::to_integer(Version, CurVersion, 10));
EXPECT_LT(PrevVersion, CurVersion);
It.first->getValue() = CurVersion;
});
}
{
WithContextValue WithNonce(NonceKey, ++Nonce);
S.runWithAST(
"CheckAST", File,
[File, Inputs, Nonce, &Mut,
&TotalASTReads](Expected<InputsAndAST> AST) {
EXPECT_THAT(Context::current().get(NonceKey), Pointee(Nonce));
EXPECT_EQ(File, boundPath());
ASSERT_TRUE((bool)AST);
EXPECT_EQ(AST->Inputs.Contents, Inputs.Contents);
EXPECT_EQ(AST->Inputs.Version, Inputs.Version);
EXPECT_EQ(AST->AST.version(), Inputs.Version);
std::lock_guard<std::mutex> Lock(Mut);
++TotalASTReads;
EXPECT_EQ(File, *TUScheduler::getFileBeingProcessedInContext());
});
}
{
WithContextValue WithNonce(NonceKey, ++Nonce);
S.runWithPreamble(
"CheckPreamble", File, TUScheduler::Stale,
[File, Inputs, Nonce, &Mut,
&TotalPreambleReads](Expected<InputsAndPreamble> Preamble) {
EXPECT_THAT(Context::current().get(NonceKey), Pointee(Nonce));
EXPECT_EQ(File, boundPath());
ASSERT_TRUE((bool)Preamble);
EXPECT_EQ(Preamble->Contents, Inputs.Contents);
std::lock_guard<std::mutex> Lock(Mut);
++TotalPreambleReads;
EXPECT_EQ(File, *TUScheduler::getFileBeingProcessedInContext());
});
}
}
}
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
} // TUScheduler destructor waits for all operations to finish.
std::lock_guard<std::mutex> Lock(Mut);
// Updates might get coalesced in preamble thread and result in dropping
// diagnostics for intermediate snapshots.
EXPECT_GE(TotalUpdates, FilesCount);
EXPECT_LE(TotalUpdates, FilesCount * UpdatesPerFile);
// We should receive diags for last update.
for (const auto &Entry : LatestDiagVersion)
EXPECT_EQ(Entry.second, UpdatesPerFile - 1);
EXPECT_EQ(TotalASTReads, FilesCount * UpdatesPerFile);
EXPECT_EQ(TotalPreambleReads, FilesCount * UpdatesPerFile);
}
TEST_F(TUSchedulerTests, EvictedAST) {
std::atomic<int> BuiltASTCounter(0);
auto Opts = optsForTest();
Opts.AsyncThreadsCount = 1;
Opts.RetentionPolicy.MaxRetainedASTs = 2;
trace::TestTracer Tracer;
TUScheduler S(CDB, Opts);
llvm::StringLiteral SourceContents = R"cpp(
int* a;
double* b = a;
)cpp";
llvm::StringLiteral OtherSourceContents = R"cpp(
int* a;
double* b = a + 0;
)cpp";
auto Foo = testPath("foo.cpp");
auto Bar = testPath("bar.cpp");
auto Baz = testPath("baz.cpp");
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "hit"), SizeIs(0));
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "miss"), SizeIs(0));
// Build one file in advance. We will not access it later, so it will be the
// one that the cache will evict.
updateWithCallback(S, Foo, SourceContents, WantDiagnostics::Yes,
[&BuiltASTCounter]() { ++BuiltASTCounter; });
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
ASSERT_EQ(BuiltASTCounter.load(), 1);
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "hit"), SizeIs(0));
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "miss"), SizeIs(1));
// Build two more files. Since we can retain only 2 ASTs, these should be
// the ones we see in the cache later.
updateWithCallback(S, Bar, SourceContents, WantDiagnostics::Yes,
[&BuiltASTCounter]() { ++BuiltASTCounter; });
updateWithCallback(S, Baz, SourceContents, WantDiagnostics::Yes,
[&BuiltASTCounter]() { ++BuiltASTCounter; });
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
ASSERT_EQ(BuiltASTCounter.load(), 3);
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "hit"), SizeIs(0));
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "miss"), SizeIs(2));
// Check only the last two ASTs are retained.
ASSERT_THAT(S.getFilesWithCachedAST(), UnorderedElementsAre(Bar, Baz));
// Access the old file again.
updateWithCallback(S, Foo, OtherSourceContents, WantDiagnostics::Yes,
[&BuiltASTCounter]() { ++BuiltASTCounter; });
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
ASSERT_EQ(BuiltASTCounter.load(), 4);
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "hit"), SizeIs(0));
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "miss"), SizeIs(1));
// Check the AST for foo.cpp is retained now and one of the others got
// evicted.
EXPECT_THAT(S.getFilesWithCachedAST(),
UnorderedElementsAre(Foo, AnyOf(Bar, Baz)));
}
// We send "empty" changes to TUScheduler when we think some external event
// *might* have invalidated current state (e.g. a header was edited).
// Verify that this doesn't evict our cache entries.
TEST_F(TUSchedulerTests, NoopChangesDontThrashCache) {
auto Opts = optsForTest();
Opts.RetentionPolicy.MaxRetainedASTs = 1;
TUScheduler S(CDB, Opts);
auto Foo = testPath("foo.cpp");
auto FooInputs = getInputs(Foo, "int x=1;");
auto Bar = testPath("bar.cpp");
auto BarInputs = getInputs(Bar, "int x=2;");
// After opening Foo then Bar, AST cache contains Bar.
S.update(Foo, FooInputs, WantDiagnostics::Auto);
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
S.update(Bar, BarInputs, WantDiagnostics::Auto);
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
ASSERT_THAT(S.getFilesWithCachedAST(), ElementsAre(Bar));
// Any number of no-op updates to Foo don't dislodge Bar from the cache.
S.update(Foo, FooInputs, WantDiagnostics::Auto);
S.update(Foo, FooInputs, WantDiagnostics::Auto);
S.update(Foo, FooInputs, WantDiagnostics::Auto);
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
ASSERT_THAT(S.getFilesWithCachedAST(), ElementsAre(Bar));
// In fact each file has been built only once.
ASSERT_EQ(S.fileStats().lookup(Foo).ASTBuilds, 1u);
ASSERT_EQ(S.fileStats().lookup(Bar).ASTBuilds, 1u);
}
TEST_F(TUSchedulerTests, EmptyPreamble) {
TUScheduler S(CDB, optsForTest());
auto Foo = testPath("foo.cpp");
auto Header = testPath("foo.h");
FS.Files[Header] = "void foo()";
FS.Timestamps[Header] = time_t(0);
auto WithPreamble = R"cpp(
#include "foo.h"
int main() {}
)cpp";
auto WithEmptyPreamble = R"cpp(int main() {})cpp";
S.update(Foo, getInputs(Foo, WithPreamble), WantDiagnostics::Auto);
S.runWithPreamble(
"getNonEmptyPreamble", Foo, TUScheduler::Stale,
[&](Expected<InputsAndPreamble> Preamble) {
// We expect to get a non-empty preamble.
EXPECT_GT(
cantFail(std::move(Preamble)).Preamble->Preamble.getBounds().Size,
0u);
});
// Wait for the preamble is being built.
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
// Update the file which results in an empty preamble.
S.update(Foo, getInputs(Foo, WithEmptyPreamble), WantDiagnostics::Auto);
// Wait for the preamble is being built.
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
S.runWithPreamble(
"getEmptyPreamble", Foo, TUScheduler::Stale,
[&](Expected<InputsAndPreamble> Preamble) {
// We expect to get an empty preamble.
EXPECT_EQ(
cantFail(std::move(Preamble)).Preamble->Preamble.getBounds().Size,
0u);
});
}
TEST_F(TUSchedulerTests, RunWaitsForPreamble) {
// Testing strategy: we update the file and schedule a few preamble reads at
// the same time. All reads should get the same non-null preamble.
TUScheduler S(CDB, optsForTest());
auto Foo = testPath("foo.cpp");
auto NonEmptyPreamble = R"cpp(
#define FOO 1
#define BAR 2
int main() {}
)cpp";
constexpr int ReadsToSchedule = 10;
std::mutex PreamblesMut;
std::vector<const void *> Preambles(ReadsToSchedule, nullptr);
S.update(Foo, getInputs(Foo, NonEmptyPreamble), WantDiagnostics::Auto);
for (int I = 0; I < ReadsToSchedule; ++I) {
S.runWithPreamble(
"test", Foo, TUScheduler::Stale,
[I, &PreamblesMut, &Preambles](Expected<InputsAndPreamble> IP) {
std::lock_guard<std::mutex> Lock(PreamblesMut);
Preambles[I] = cantFail(std::move(IP)).Preamble;
});
}
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
// Check all actions got the same non-null preamble.
std::lock_guard<std::mutex> Lock(PreamblesMut);
ASSERT_NE(Preambles[0], nullptr);
ASSERT_THAT(Preambles, Each(Preambles[0]));
}
TEST_F(TUSchedulerTests, NoopOnEmptyChanges) {
TUScheduler S(CDB, optsForTest(), captureDiags());
auto Source = testPath("foo.cpp");
auto Header = testPath("foo.h");
FS.Files[Header] = "int a;";
FS.Timestamps[Header] = time_t(0);
std::string SourceContents = R"cpp(
#include "foo.h"
int b = a;
)cpp";
// Return value indicates if the updated callback was received.
auto DoUpdate = [&](std::string Contents) -> bool {
std::atomic<bool> Updated(false);
Updated = false;
updateWithDiags(S, Source, Contents, WantDiagnostics::Yes,
[&Updated](std::vector<Diag>) { Updated = true; });
bool UpdateFinished = S.blockUntilIdle(timeoutSeconds(10));
if (!UpdateFinished)
ADD_FAILURE() << "Updated has not finished in one second. Threading bug?";
return Updated;
};
// Test that subsequent updates with the same inputs do not cause rebuilds.
ASSERT_TRUE(DoUpdate(SourceContents));
ASSERT_EQ(S.fileStats().lookup(Source).ASTBuilds, 1u);
ASSERT_EQ(S.fileStats().lookup(Source).PreambleBuilds, 1u);
ASSERT_FALSE(DoUpdate(SourceContents));
ASSERT_EQ(S.fileStats().lookup(Source).ASTBuilds, 1u);
ASSERT_EQ(S.fileStats().lookup(Source).PreambleBuilds, 1u);
// Update to a header should cause a rebuild, though.
FS.Timestamps[Header] = time_t(1);
ASSERT_TRUE(DoUpdate(SourceContents));
ASSERT_FALSE(DoUpdate(SourceContents));
ASSERT_EQ(S.fileStats().lookup(Source).ASTBuilds, 2u);
ASSERT_EQ(S.fileStats().lookup(Source).PreambleBuilds, 2u);
// Update to the contents should cause a rebuild.
SourceContents += "\nint c = b;";
ASSERT_TRUE(DoUpdate(SourceContents));
ASSERT_FALSE(DoUpdate(SourceContents));
ASSERT_EQ(S.fileStats().lookup(Source).ASTBuilds, 3u);
ASSERT_EQ(S.fileStats().lookup(Source).PreambleBuilds, 2u);
// Update to the compile commands should also cause a rebuild.
CDB.ExtraClangFlags.push_back("-DSOMETHING");
ASSERT_TRUE(DoUpdate(SourceContents));
ASSERT_FALSE(DoUpdate(SourceContents));
ASSERT_EQ(S.fileStats().lookup(Source).ASTBuilds, 4u);
ASSERT_EQ(S.fileStats().lookup(Source).PreambleBuilds, 3u);
}
// We rebuild if a completely missing header exists, but not if one is added
// on a higher-priority include path entry (for performance).
// (Previously we wouldn't automatically rebuild when files were added).
TEST_F(TUSchedulerTests, MissingHeader) {
CDB.ExtraClangFlags.push_back("-I" + testPath("a"));
CDB.ExtraClangFlags.push_back("-I" + testPath("b"));
// Force both directories to exist so they don't get pruned.
FS.Files.try_emplace("a/__unused__");
FS.Files.try_emplace("b/__unused__");
TUScheduler S(CDB, optsForTest(), captureDiags());
auto Source = testPath("foo.cpp");
auto HeaderA = testPath("a/foo.h");
auto HeaderB = testPath("b/foo.h");
auto SourceContents = R"cpp(
#include "foo.h"
int c = b;
)cpp";
ParseInputs Inputs = getInputs(Source, SourceContents);
std::atomic<size_t> DiagCount(0);
// Update the source contents, which should trigger an initial build with
// the header file missing.
updateWithDiags(
S, Source, Inputs, WantDiagnostics::Yes,
[&DiagCount](std::vector<Diag> Diags) {
++DiagCount;
EXPECT_THAT(Diags,
ElementsAre(Field(&Diag::Message, "'foo.h' file not found"),
Field(&Diag::Message,
"use of undeclared identifier 'b'")));
});
S.blockUntilIdle(timeoutSeconds(10));
FS.Files[HeaderB] = "int b;";
FS.Timestamps[HeaderB] = time_t(1);
// The addition of the missing header file triggers a rebuild, no errors.
updateWithDiags(S, Source, Inputs, WantDiagnostics::Yes,
[&DiagCount](std::vector<Diag> Diags) {
++DiagCount;
EXPECT_THAT(Diags, IsEmpty());
});
// Ensure previous assertions are done before we touch the FS again.
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
// Add the high-priority header file, which should reintroduce the error.
FS.Files[HeaderA] = "int a;";
FS.Timestamps[HeaderA] = time_t(1);
// This isn't detected: we don't stat a/foo.h to validate the preamble.
updateWithDiags(S, Source, Inputs, WantDiagnostics::Yes,
[&DiagCount](std::vector<Diag> Diags) {
++DiagCount;
ADD_FAILURE()
<< "Didn't expect new diagnostics when adding a/foo.h";
});
// Forcing the reload should should cause a rebuild.
Inputs.ForceRebuild = true;
updateWithDiags(
S, Source, Inputs, WantDiagnostics::Yes,
[&DiagCount](std::vector<Diag> Diags) {
++DiagCount;
ElementsAre(Field(&Diag::Message, "use of undeclared identifier 'b'"));
});
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
EXPECT_EQ(DiagCount, 3U);
}
TEST_F(TUSchedulerTests, NoChangeDiags) {
trace::TestTracer Tracer;
TUScheduler S(CDB, optsForTest(), captureDiags());
auto FooCpp = testPath("foo.cpp");
const auto *Contents = "int a; int b;";
EXPECT_THAT(Tracer.takeMetric("ast_access_read", "hit"), SizeIs(0));
EXPECT_THAT(Tracer.takeMetric("ast_access_read", "miss"), SizeIs(0));
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "hit"), SizeIs(0));
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "miss"), SizeIs(0));
updateWithDiags(
S, FooCpp, Contents, WantDiagnostics::No,
[](std::vector<Diag>) { ADD_FAILURE() << "Should not be called."; });
S.runWithAST("touchAST", FooCpp, [](Expected<InputsAndAST> IA) {
// Make sure the AST was actually built.
cantFail(std::move(IA));
});
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
EXPECT_THAT(Tracer.takeMetric("ast_access_read", "hit"), SizeIs(0));
EXPECT_THAT(Tracer.takeMetric("ast_access_read", "miss"), SizeIs(1));
// Even though the inputs didn't change and AST can be reused, we need to
// report the diagnostics, as they were not reported previously.
std::atomic<bool> SeenDiags(false);
updateWithDiags(S, FooCpp, Contents, WantDiagnostics::Auto,
[&](std::vector<Diag>) { SeenDiags = true; });
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
ASSERT_TRUE(SeenDiags);
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "hit"), SizeIs(1));
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "miss"), SizeIs(0));
// Subsequent request does not get any diagnostics callback because the same
// diags have previously been reported and the inputs didn't change.
updateWithDiags(
S, FooCpp, Contents, WantDiagnostics::Auto,
[&](std::vector<Diag>) { ADD_FAILURE() << "Should not be called."; });
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
}
TEST_F(TUSchedulerTests, Run) {
for (bool Sync : {false, true}) {
auto Opts = optsForTest();
if (Sync)
Opts.AsyncThreadsCount = 0;
TUScheduler S(CDB, Opts);
std::atomic<int> Counter(0);
S.run("add 1", /*Path=*/"", [&] { ++Counter; });
S.run("add 2", /*Path=*/"", [&] { Counter += 2; });
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
EXPECT_EQ(Counter.load(), 3);
Notification TaskRun;
Key<int> TestKey;
WithContextValue CtxWithKey(TestKey, 10);
const char *Path = "somepath";
S.run("props context", Path, [&] {
EXPECT_EQ(Context::current().getExisting(TestKey), 10);
EXPECT_EQ(Path, boundPath());
TaskRun.notify();
});
TaskRun.wait();
}
}
TEST_F(TUSchedulerTests, TUStatus) {
class CaptureTUStatus : public ClangdServer::Callbacks {
public:
void onFileUpdated(PathRef File, const TUStatus &Status) override {
auto ASTAction = Status.ASTActivity.K;
auto PreambleAction = Status.PreambleActivity;
std::lock_guard<std::mutex> Lock(Mutex);
// Only push the action if it has changed. Since TUStatus can be published
// from either Preamble or AST thread and when one changes the other stays
// the same.
// Note that this can result in missing some updates when something other
// than action kind changes, e.g. when AST is built/reused the action kind
// stays as Building.
if (ASTActions.empty() || ASTActions.back() != ASTAction)
ASTActions.push_back(ASTAction);
if (PreambleActions.empty() || PreambleActions.back() != PreambleAction)
PreambleActions.push_back(PreambleAction);
}
std::vector<PreambleAction> preambleStatuses() {
std::lock_guard<std::mutex> Lock(Mutex);
return PreambleActions;
}
std::vector<ASTAction::Kind> astStatuses() {
std::lock_guard<std::mutex> Lock(Mutex);
return ASTActions;
}
private:
std::mutex Mutex;
std::vector<ASTAction::Kind> ASTActions;
std::vector<PreambleAction> PreambleActions;
} CaptureTUStatus;
MockFS FS;
MockCompilationDatabase CDB;
ClangdServer Server(CDB, FS, ClangdServer::optsForTest(), &CaptureTUStatus);
Annotations Code("int m^ain () {}");
// We schedule the following tasks in the queue:
// [Update] [GoToDefinition]
Server.addDocument(testPath("foo.cpp"), Code.code(), "1",
WantDiagnostics::Auto);
ASSERT_TRUE(Server.blockUntilIdleForTest());
Server.locateSymbolAt(testPath("foo.cpp"), Code.point(),
[](Expected<std::vector<LocatedSymbol>> Result) {
ASSERT_TRUE((bool)Result);
});
ASSERT_TRUE(Server.blockUntilIdleForTest());
EXPECT_THAT(CaptureTUStatus.preambleStatuses(),
ElementsAre(
// PreambleThread starts idle, as the update is first handled
// by ASTWorker.
PreambleAction::Idle,
// Then it starts building first preamble and releases that to
// ASTWorker.
PreambleAction::Building,
// Then goes idle and stays that way as we don't receive any
// more update requests.
PreambleAction::Idle));
EXPECT_THAT(CaptureTUStatus.astStatuses(),
ElementsAre(
// Starts handling the update action and blocks until the
// first preamble is built.
ASTAction::RunningAction,
// Afterwqards it builds an AST for that preamble to publish
// diagnostics.
ASTAction::Building,
// Then goes idle.
ASTAction::Idle,
// Afterwards we start executing go-to-def.
ASTAction::RunningAction,
// Then go idle.
ASTAction::Idle));
}
TEST_F(TUSchedulerTests, CommandLineErrors) {
// We should see errors from command-line parsing inside the main file.
CDB.ExtraClangFlags = {"-fsome-unknown-flag"};
// (!) 'Ready' must live longer than TUScheduler.
Notification Ready;
TUScheduler S(CDB, optsForTest(), captureDiags());
std::vector<Diag> Diagnostics;
updateWithDiags(S, testPath("foo.cpp"), "void test() {}",
WantDiagnostics::Yes, [&](std::vector<Diag> D) {
Diagnostics = std::move(D);
Ready.notify();
});
Ready.wait();
EXPECT_THAT(
Diagnostics,
ElementsAre(AllOf(
Field(&Diag::ID, Eq(diag::err_drv_unknown_argument)),
Field(&Diag::Name, Eq("drv_unknown_argument")),
Field(&Diag::Message, "unknown argument: '-fsome-unknown-flag'"))));
}
TEST_F(TUSchedulerTests, CommandLineWarnings) {
// We should not see warnings from command-line parsing.
CDB.ExtraClangFlags = {"-Wsome-unknown-warning"};
// (!) 'Ready' must live longer than TUScheduler.
Notification Ready;
TUScheduler S(CDB, optsForTest(), captureDiags());
std::vector<Diag> Diagnostics;
updateWithDiags(S, testPath("foo.cpp"), "void test() {}",
WantDiagnostics::Yes, [&](std::vector<Diag> D) {
Diagnostics = std::move(D);
Ready.notify();
});
Ready.wait();
EXPECT_THAT(Diagnostics, IsEmpty());
}
TEST(DebouncePolicy, Compute) {
namespace c = std::chrono;
std::vector<DebouncePolicy::clock::duration> History = {
c::seconds(0),
c::seconds(5),
c::seconds(10),
c::seconds(20),
};
DebouncePolicy Policy;
Policy.Min = c::seconds(3);
Policy.Max = c::seconds(25);
// Call Policy.compute(History) and return seconds as a float.
auto Compute = [&](llvm::ArrayRef<DebouncePolicy::clock::duration> History) {
using FloatingSeconds = c::duration<float, c::seconds::period>;
return static_cast<float>(Policy.compute(History) / FloatingSeconds(1));
};
EXPECT_NEAR(10, Compute(History), 0.01) << "(upper) median = 10";
Policy.RebuildRatio = 1.5;
EXPECT_NEAR(15, Compute(History), 0.01) << "median = 10, ratio = 1.5";
Policy.RebuildRatio = 3;
EXPECT_NEAR(25, Compute(History), 0.01) << "constrained by max";
Policy.RebuildRatio = 0;
EXPECT_NEAR(3, Compute(History), 0.01) << "constrained by min";
EXPECT_NEAR(25, Compute({}), 0.01) << "no history -> max";
}
TEST_F(TUSchedulerTests, AsyncPreambleThread) {
// Blocks preamble thread while building preamble with \p BlockVersion until
// \p N is notified.
class BlockPreambleThread : public ParsingCallbacks {
public:
BlockPreambleThread(llvm::StringRef BlockVersion, Notification &N)
: BlockVersion(BlockVersion), N(N) {}
void onPreambleAST(PathRef Path, llvm::StringRef Version, ASTContext &Ctx,
std::shared_ptr<clang::Preprocessor> PP,
const CanonicalIncludes &) override {
if (Version == BlockVersion)
N.wait();
}
private:
llvm::StringRef BlockVersion;
Notification &N;
};
static constexpr llvm::StringLiteral InputsV0 = "v0";
static constexpr llvm::StringLiteral InputsV1 = "v1";
Notification Ready;
TUScheduler S(CDB, optsForTest(),
std::make_unique<BlockPreambleThread>(InputsV1, Ready));
Path File = testPath("foo.cpp");
auto PI = getInputs(File, "");
PI.Version = InputsV0.str();
S.update(File, PI, WantDiagnostics::Auto);
S.blockUntilIdle(timeoutSeconds(10));
// Block preamble builds.
PI.Version = InputsV1.str();
// Issue second update which will block preamble thread.
S.update(File, PI, WantDiagnostics::Auto);
Notification RunASTAction;
// Issue an AST read, which shouldn't be blocked and see latest version of the
// file.
S.runWithAST("test", File, [&](Expected<InputsAndAST> AST) {
ASSERT_TRUE(bool(AST));
// Make sure preamble is built with stale inputs, but AST was built using
// new ones.
EXPECT_THAT(AST->AST.preambleVersion(), InputsV0);
EXPECT_THAT(AST->Inputs.Version, InputsV1.str());
RunASTAction.notify();
});
RunASTAction.wait();
Ready.notify();
}
} // namespace
} // namespace clangd
} // namespace clang