1 //===-- FileIndexTests.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 "AST.h" 10 #include "Annotations.h" 11 #include "Compiler.h" 12 #include "Headers.h" 13 #include "ParsedAST.h" 14 #include "SyncAPI.h" 15 #include "TestFS.h" 16 #include "TestTU.h" 17 #include "URI.h" 18 #include "index/CanonicalIncludes.h" 19 #include "index/FileIndex.h" 20 #include "index/Index.h" 21 #include "index/Ref.h" 22 #include "index/Relation.h" 23 #include "index/Serialization.h" 24 #include "index/Symbol.h" 25 #include "clang/Frontend/CompilerInvocation.h" 26 #include "clang/Frontend/Utils.h" 27 #include "clang/Index/IndexSymbol.h" 28 #include "clang/Lex/Preprocessor.h" 29 #include "clang/Tooling/CompilationDatabase.h" 30 #include "gmock/gmock.h" 31 #include "gtest/gtest.h" 32 #include <utility> 33 34 using ::testing::_; 35 using ::testing::AllOf; 36 using ::testing::Contains; 37 using ::testing::ElementsAre; 38 using ::testing::IsEmpty; 39 using ::testing::Pair; 40 using ::testing::UnorderedElementsAre; 41 42 MATCHER_P(RefRange, Range, "") { 43 return std::make_tuple(arg.Location.Start.line(), arg.Location.Start.column(), 44 arg.Location.End.line(), arg.Location.End.column()) == 45 std::make_tuple(Range.start.line, Range.start.character, 46 Range.end.line, Range.end.character); 47 } 48 MATCHER_P(FileURI, F, "") { return llvm::StringRef(arg.Location.FileURI) == F; } 49 MATCHER_P(DeclURI, U, "") { 50 return llvm::StringRef(arg.CanonicalDeclaration.FileURI) == U; 51 } 52 MATCHER_P(DefURI, U, "") { 53 return llvm::StringRef(arg.Definition.FileURI) == U; 54 } 55 MATCHER_P(QName, N, "") { return (arg.Scope + arg.Name).str() == N; } 56 MATCHER_P(NumReferences, N, "") { return arg.References == N; } 57 MATCHER_P(hasOrign, O, "") { return bool(arg.Origin & O); } 58 59 namespace clang { 60 namespace clangd { 61 namespace { 62 ::testing::Matcher<const RefSlab &> 63 RefsAre(std::vector<::testing::Matcher<Ref>> Matchers) { 64 return ElementsAre(::testing::Pair(_, UnorderedElementsAreArray(Matchers))); 65 } 66 67 Symbol symbol(llvm::StringRef ID) { 68 Symbol Sym; 69 Sym.ID = SymbolID(ID); 70 Sym.Name = ID; 71 return Sym; 72 } 73 74 std::unique_ptr<SymbolSlab> numSlab(int Begin, int End) { 75 SymbolSlab::Builder Slab; 76 for (int i = Begin; i <= End; i++) 77 Slab.insert(symbol(std::to_string(i))); 78 return std::make_unique<SymbolSlab>(std::move(Slab).build()); 79 } 80 81 std::unique_ptr<RefSlab> refSlab(const SymbolID &ID, const char *Path) { 82 RefSlab::Builder Slab; 83 Ref R; 84 R.Location.FileURI = Path; 85 R.Kind = RefKind::Reference; 86 Slab.insert(ID, R); 87 return std::make_unique<RefSlab>(std::move(Slab).build()); 88 } 89 90 TEST(FileSymbolsTest, UpdateAndGet) { 91 FileSymbols FS; 92 EXPECT_THAT(runFuzzyFind(*FS.buildIndex(IndexType::Light), ""), IsEmpty()); 93 94 FS.update("f1", numSlab(1, 3), refSlab(SymbolID("1"), "f1.cc"), nullptr, 95 false); 96 EXPECT_THAT(runFuzzyFind(*FS.buildIndex(IndexType::Light), ""), 97 UnorderedElementsAre(QName("1"), QName("2"), QName("3"))); 98 EXPECT_THAT(getRefs(*FS.buildIndex(IndexType::Light), SymbolID("1")), 99 RefsAre({FileURI("f1.cc")})); 100 } 101 102 TEST(FileSymbolsTest, Overlap) { 103 FileSymbols FS; 104 FS.update("f1", numSlab(1, 3), nullptr, nullptr, false); 105 FS.update("f2", numSlab(3, 5), nullptr, nullptr, false); 106 for (auto Type : {IndexType::Light, IndexType::Heavy}) 107 EXPECT_THAT(runFuzzyFind(*FS.buildIndex(Type), ""), 108 UnorderedElementsAre(QName("1"), QName("2"), QName("3"), 109 QName("4"), QName("5"))); 110 } 111 112 TEST(FileSymbolsTest, MergeOverlap) { 113 FileSymbols FS; 114 auto OneSymboSlab = [](Symbol Sym) { 115 SymbolSlab::Builder S; 116 S.insert(Sym); 117 return std::make_unique<SymbolSlab>(std::move(S).build()); 118 }; 119 auto X1 = symbol("x"); 120 X1.CanonicalDeclaration.FileURI = "file:///x1"; 121 auto X2 = symbol("x"); 122 X2.Definition.FileURI = "file:///x2"; 123 124 FS.update("f1", OneSymboSlab(X1), nullptr, nullptr, false); 125 FS.update("f2", OneSymboSlab(X2), nullptr, nullptr, false); 126 for (auto Type : {IndexType::Light, IndexType::Heavy}) 127 EXPECT_THAT( 128 runFuzzyFind(*FS.buildIndex(Type, DuplicateHandling::Merge), "x"), 129 UnorderedElementsAre( 130 AllOf(QName("x"), DeclURI("file:///x1"), DefURI("file:///x2")))); 131 } 132 133 TEST(FileSymbolsTest, SnapshotAliveAfterRemove) { 134 FileSymbols FS; 135 136 SymbolID ID("1"); 137 FS.update("f1", numSlab(1, 3), refSlab(ID, "f1.cc"), nullptr, false); 138 139 auto Symbols = FS.buildIndex(IndexType::Light); 140 EXPECT_THAT(runFuzzyFind(*Symbols, ""), 141 UnorderedElementsAre(QName("1"), QName("2"), QName("3"))); 142 EXPECT_THAT(getRefs(*Symbols, ID), RefsAre({FileURI("f1.cc")})); 143 144 FS.update("f1", nullptr, nullptr, nullptr, false); 145 auto Empty = FS.buildIndex(IndexType::Light); 146 EXPECT_THAT(runFuzzyFind(*Empty, ""), IsEmpty()); 147 EXPECT_THAT(getRefs(*Empty, ID), ElementsAre()); 148 149 EXPECT_THAT(runFuzzyFind(*Symbols, ""), 150 UnorderedElementsAre(QName("1"), QName("2"), QName("3"))); 151 EXPECT_THAT(getRefs(*Symbols, ID), RefsAre({FileURI("f1.cc")})); 152 } 153 154 // Adds Basename.cpp, which includes Basename.h, which contains Code. 155 void update(FileIndex &M, llvm::StringRef Basename, llvm::StringRef Code) { 156 TestTU File; 157 File.Filename = (Basename + ".cpp").str(); 158 File.HeaderFilename = (Basename + ".h").str(); 159 File.HeaderCode = std::string(Code); 160 auto AST = File.build(); 161 M.updatePreamble(testPath(File.Filename), /*Version=*/"null", 162 AST.getASTContext(), AST.getPreprocessorPtr(), 163 AST.getCanonicalIncludes()); 164 } 165 166 TEST(FileIndexTest, CustomizedURIScheme) { 167 FileIndex M; 168 update(M, "f", "class string {};"); 169 170 EXPECT_THAT(runFuzzyFind(M, ""), ElementsAre(DeclURI("unittest:///f.h"))); 171 } 172 173 TEST(FileIndexTest, IndexAST) { 174 FileIndex M; 175 update(M, "f1", "namespace ns { void f() {} class X {}; }"); 176 177 FuzzyFindRequest Req; 178 Req.Query = ""; 179 Req.Scopes = {"ns::"}; 180 EXPECT_THAT(runFuzzyFind(M, Req), 181 UnorderedElementsAre(QName("ns::f"), QName("ns::X"))); 182 } 183 184 TEST(FileIndexTest, NoLocal) { 185 FileIndex M; 186 update(M, "f1", "namespace ns { void f() { int local = 0; } class X {}; }"); 187 188 EXPECT_THAT( 189 runFuzzyFind(M, ""), 190 UnorderedElementsAre(QName("ns"), QName("ns::f"), QName("ns::X"))); 191 } 192 193 TEST(FileIndexTest, IndexMultiASTAndDeduplicate) { 194 FileIndex M; 195 update(M, "f1", "namespace ns { void f() {} class X {}; }"); 196 update(M, "f2", "namespace ns { void ff() {} class X {}; }"); 197 198 FuzzyFindRequest Req; 199 Req.Scopes = {"ns::"}; 200 EXPECT_THAT( 201 runFuzzyFind(M, Req), 202 UnorderedElementsAre(QName("ns::f"), QName("ns::X"), QName("ns::ff"))); 203 } 204 205 TEST(FileIndexTest, ClassMembers) { 206 FileIndex M; 207 update(M, "f1", "class X { static int m1; int m2; static void f(); };"); 208 209 EXPECT_THAT(runFuzzyFind(M, ""), 210 UnorderedElementsAre(QName("X"), QName("X::m1"), QName("X::m2"), 211 QName("X::f"))); 212 } 213 214 TEST(FileIndexTest, IncludeCollected) { 215 FileIndex M; 216 update( 217 M, "f", 218 "// IWYU pragma: private, include <the/good/header.h>\nclass string {};"); 219 220 auto Symbols = runFuzzyFind(M, ""); 221 EXPECT_THAT(Symbols, ElementsAre(_)); 222 EXPECT_THAT(Symbols.begin()->IncludeHeaders.front().IncludeHeader, 223 "<the/good/header.h>"); 224 } 225 226 TEST(FileIndexTest, HasSystemHeaderMappingsInPreamble) { 227 TestTU TU; 228 TU.HeaderCode = "class Foo{};"; 229 TU.HeaderFilename = "algorithm"; 230 231 auto Symbols = runFuzzyFind(*TU.index(), ""); 232 EXPECT_THAT(Symbols, ElementsAre(_)); 233 EXPECT_THAT(Symbols.begin()->IncludeHeaders.front().IncludeHeader, 234 "<algorithm>"); 235 } 236 237 TEST(FileIndexTest, TemplateParamsInLabel) { 238 auto Source = R"cpp( 239 template <class Ty> 240 class vector { 241 }; 242 243 template <class Ty, class Arg> 244 vector<Ty> make_vector(Arg A) {} 245 )cpp"; 246 247 FileIndex M; 248 update(M, "f", Source); 249 250 auto Symbols = runFuzzyFind(M, ""); 251 EXPECT_THAT(Symbols, 252 UnorderedElementsAre(QName("vector"), QName("make_vector"))); 253 auto It = Symbols.begin(); 254 Symbol Vector = *It++; 255 Symbol MakeVector = *It++; 256 if (MakeVector.Name == "vector") 257 std::swap(MakeVector, Vector); 258 259 EXPECT_EQ(Vector.Signature, "<class Ty>"); 260 EXPECT_EQ(Vector.CompletionSnippetSuffix, "<${1:class Ty}>"); 261 262 EXPECT_EQ(MakeVector.Signature, "<class Ty>(Arg A)"); 263 EXPECT_EQ(MakeVector.CompletionSnippetSuffix, "<${1:class Ty}>(${2:Arg A})"); 264 } 265 266 TEST(FileIndexTest, RebuildWithPreamble) { 267 auto FooCpp = testPath("foo.cpp"); 268 auto FooH = testPath("foo.h"); 269 // Preparse ParseInputs. 270 ParseInputs PI; 271 PI.CompileCommand.Directory = testRoot(); 272 PI.CompileCommand.Filename = FooCpp; 273 PI.CompileCommand.CommandLine = {"clang", "-xc++", FooCpp}; 274 275 llvm::StringMap<std::string> Files; 276 Files[FooCpp] = ""; 277 Files[FooH] = R"cpp( 278 namespace ns_in_header { 279 int func_in_header(); 280 } 281 )cpp"; 282 PI.FS = buildTestFS(std::move(Files)); 283 284 PI.Contents = R"cpp( 285 #include "foo.h" 286 namespace ns_in_source { 287 int func_in_source(); 288 } 289 )cpp"; 290 291 // Rebuild the file. 292 IgnoreDiagnostics IgnoreDiags; 293 auto CI = buildCompilerInvocation(PI, IgnoreDiags); 294 295 FileIndex Index; 296 bool IndexUpdated = false; 297 buildPreamble(FooCpp, *CI, PI, 298 /*StoreInMemory=*/true, 299 [&](ASTContext &Ctx, std::shared_ptr<Preprocessor> PP, 300 const CanonicalIncludes &CanonIncludes) { 301 EXPECT_FALSE(IndexUpdated) 302 << "Expected only a single index update"; 303 IndexUpdated = true; 304 Index.updatePreamble(FooCpp, /*Version=*/"null", Ctx, 305 std::move(PP), CanonIncludes); 306 }); 307 ASSERT_TRUE(IndexUpdated); 308 309 // Check the index contains symbols from the preamble, but not from the main 310 // file. 311 FuzzyFindRequest Req; 312 Req.Query = ""; 313 Req.Scopes = {"", "ns_in_header::"}; 314 315 EXPECT_THAT(runFuzzyFind(Index, Req), 316 UnorderedElementsAre(QName("ns_in_header"), 317 QName("ns_in_header::func_in_header"))); 318 } 319 320 TEST(FileIndexTest, Refs) { 321 const char *HeaderCode = "class Foo {};"; 322 Annotations MainCode(R"cpp( 323 void f() { 324 $foo[[Foo]] foo; 325 } 326 )cpp"); 327 328 auto Foo = 329 findSymbol(TestTU::withHeaderCode(HeaderCode).headerSymbols(), "Foo"); 330 331 RefsRequest Request; 332 Request.IDs = {Foo.ID}; 333 334 FileIndex Index; 335 // Add test.cc 336 TestTU Test; 337 Test.HeaderCode = HeaderCode; 338 Test.Code = std::string(MainCode.code()); 339 Test.Filename = "test.cc"; 340 auto AST = Test.build(); 341 Index.updateMain(Test.Filename, AST); 342 // Add test2.cc 343 TestTU Test2; 344 Test2.HeaderCode = HeaderCode; 345 Test2.Code = std::string(MainCode.code()); 346 Test2.Filename = "test2.cc"; 347 AST = Test2.build(); 348 Index.updateMain(Test2.Filename, AST); 349 350 EXPECT_THAT(getRefs(Index, Foo.ID), 351 RefsAre({AllOf(RefRange(MainCode.range("foo")), 352 FileURI("unittest:///test.cc")), 353 AllOf(RefRange(MainCode.range("foo")), 354 FileURI("unittest:///test2.cc"))})); 355 } 356 357 TEST(FileIndexTest, MacroRefs) { 358 Annotations HeaderCode(R"cpp( 359 #define $def1[[HEADER_MACRO]](X) (X+1) 360 )cpp"); 361 Annotations MainCode(R"cpp( 362 #define $def2[[MAINFILE_MACRO]](X) (X+1) 363 void f() { 364 int a = $ref1[[HEADER_MACRO]](2); 365 int b = $ref2[[MAINFILE_MACRO]](1); 366 } 367 )cpp"); 368 369 FileIndex Index; 370 // Add test.cc 371 TestTU Test; 372 Test.HeaderCode = std::string(HeaderCode.code()); 373 Test.Code = std::string(MainCode.code()); 374 Test.Filename = "test.cc"; 375 auto AST = Test.build(); 376 Index.updateMain(Test.Filename, AST); 377 378 auto HeaderMacro = findSymbol(Test.headerSymbols(), "HEADER_MACRO"); 379 EXPECT_THAT(getRefs(Index, HeaderMacro.ID), 380 RefsAre({AllOf(RefRange(MainCode.range("ref1")), 381 FileURI("unittest:///test.cc"))})); 382 383 auto MainFileMacro = findSymbol(Test.headerSymbols(), "MAINFILE_MACRO"); 384 EXPECT_THAT(getRefs(Index, MainFileMacro.ID), 385 RefsAre({AllOf(RefRange(MainCode.range("def2")), 386 FileURI("unittest:///test.cc")), 387 AllOf(RefRange(MainCode.range("ref2")), 388 FileURI("unittest:///test.cc"))})); 389 } 390 391 TEST(FileIndexTest, CollectMacros) { 392 FileIndex M; 393 update(M, "f", "#define CLANGD 1"); 394 EXPECT_THAT(runFuzzyFind(M, ""), Contains(QName("CLANGD"))); 395 } 396 397 TEST(FileIndexTest, Relations) { 398 TestTU TU; 399 TU.Filename = "f.cpp"; 400 TU.HeaderFilename = "f.h"; 401 TU.HeaderCode = "class A {}; class B : public A {};"; 402 auto AST = TU.build(); 403 FileIndex Index; 404 Index.updatePreamble(testPath(TU.Filename), /*Version=*/"null", 405 AST.getASTContext(), AST.getPreprocessorPtr(), 406 AST.getCanonicalIncludes()); 407 SymbolID A = findSymbol(TU.headerSymbols(), "A").ID; 408 uint32_t Results = 0; 409 RelationsRequest Req; 410 Req.Subjects.insert(A); 411 Req.Predicate = RelationKind::BaseOf; 412 Index.relations(Req, [&](const SymbolID &, const Symbol &) { ++Results; }); 413 EXPECT_EQ(Results, 1u); 414 } 415 416 TEST(FileIndexTest, ReferencesInMainFileWithPreamble) { 417 TestTU TU; 418 TU.HeaderCode = "class Foo{};"; 419 Annotations Main(R"cpp( 420 void f() { 421 [[Foo]] foo; 422 } 423 )cpp"); 424 TU.Code = std::string(Main.code()); 425 auto AST = TU.build(); 426 FileIndex Index; 427 Index.updateMain(testPath(TU.Filename), AST); 428 429 // Expect to see references in main file, references in headers are excluded 430 // because we only index main AST. 431 EXPECT_THAT(getRefs(Index, findSymbol(TU.headerSymbols(), "Foo").ID), 432 RefsAre({RefRange(Main.range())})); 433 } 434 435 TEST(FileIndexTest, MergeMainFileSymbols) { 436 const char *CommonHeader = "void foo();"; 437 TestTU Header = TestTU::withCode(CommonHeader); 438 TestTU Cpp = TestTU::withCode("void foo() {}"); 439 Cpp.Filename = "foo.cpp"; 440 Cpp.HeaderFilename = "foo.h"; 441 Cpp.HeaderCode = CommonHeader; 442 443 FileIndex Index; 444 auto HeaderAST = Header.build(); 445 auto CppAST = Cpp.build(); 446 Index.updateMain(testPath("foo.h"), HeaderAST); 447 Index.updateMain(testPath("foo.cpp"), CppAST); 448 449 auto Symbols = runFuzzyFind(Index, ""); 450 // Check foo is merged, foo in Cpp wins (as we see the definition there). 451 EXPECT_THAT(Symbols, ElementsAre(AllOf(DeclURI("unittest:///foo.h"), 452 DefURI("unittest:///foo.cpp"), 453 hasOrign(SymbolOrigin::Merge)))); 454 } 455 456 TEST(FileSymbolsTest, CountReferencesNoRefSlabs) { 457 FileSymbols FS; 458 FS.update("f1", numSlab(1, 3), nullptr, nullptr, true); 459 FS.update("f2", numSlab(1, 3), nullptr, nullptr, false); 460 EXPECT_THAT( 461 runFuzzyFind(*FS.buildIndex(IndexType::Light, DuplicateHandling::Merge), 462 ""), 463 UnorderedElementsAre(AllOf(QName("1"), NumReferences(0u)), 464 AllOf(QName("2"), NumReferences(0u)), 465 AllOf(QName("3"), NumReferences(0u)))); 466 } 467 468 TEST(FileSymbolsTest, CountReferencesWithRefSlabs) { 469 FileSymbols FS; 470 FS.update("f1cpp", numSlab(1, 3), refSlab(SymbolID("1"), "f1.cpp"), nullptr, 471 true); 472 FS.update("f1h", numSlab(1, 3), refSlab(SymbolID("1"), "f1.h"), nullptr, 473 false); 474 FS.update("f2cpp", numSlab(1, 3), refSlab(SymbolID("2"), "f2.cpp"), nullptr, 475 true); 476 FS.update("f2h", numSlab(1, 3), refSlab(SymbolID("2"), "f2.h"), nullptr, 477 false); 478 FS.update("f3cpp", numSlab(1, 3), refSlab(SymbolID("3"), "f3.cpp"), nullptr, 479 true); 480 FS.update("f3h", numSlab(1, 3), refSlab(SymbolID("3"), "f3.h"), nullptr, 481 false); 482 EXPECT_THAT( 483 runFuzzyFind(*FS.buildIndex(IndexType::Light, DuplicateHandling::Merge), 484 ""), 485 UnorderedElementsAre(AllOf(QName("1"), NumReferences(1u)), 486 AllOf(QName("2"), NumReferences(1u)), 487 AllOf(QName("3"), NumReferences(1u)))); 488 } 489 490 TEST(FileIndexTest, StalePreambleSymbolsDeleted) { 491 FileIndex M; 492 TestTU File; 493 File.HeaderFilename = "a.h"; 494 495 File.Filename = "f1.cpp"; 496 File.HeaderCode = "int a;"; 497 auto AST = File.build(); 498 M.updatePreamble(testPath(File.Filename), /*Version=*/"null", 499 AST.getASTContext(), AST.getPreprocessorPtr(), 500 AST.getCanonicalIncludes()); 501 EXPECT_THAT(runFuzzyFind(M, ""), UnorderedElementsAre(QName("a"))); 502 503 File.Filename = "f2.cpp"; 504 File.HeaderCode = "int b;"; 505 AST = File.build(); 506 M.updatePreamble(testPath(File.Filename), /*Version=*/"null", 507 AST.getASTContext(), AST.getPreprocessorPtr(), 508 AST.getCanonicalIncludes()); 509 EXPECT_THAT(runFuzzyFind(M, ""), UnorderedElementsAre(QName("b"))); 510 } 511 512 TEST(FileShardedIndexTest, Sharding) { 513 auto AHeaderUri = URI::create(testPath("a.h")).toString(); 514 auto BHeaderUri = URI::create(testPath("b.h")).toString(); 515 auto BSourceUri = URI::create(testPath("b.cc")).toString(); 516 517 auto Sym1 = symbol("1"); 518 Sym1.CanonicalDeclaration.FileURI = AHeaderUri.c_str(); 519 520 auto Sym2 = symbol("2"); 521 Sym2.CanonicalDeclaration.FileURI = BHeaderUri.c_str(); 522 Sym2.Definition.FileURI = BSourceUri.c_str(); 523 524 IndexFileIn IF; 525 { 526 SymbolSlab::Builder B; 527 // Should be stored in only a.h 528 B.insert(Sym1); 529 // Should be stored in both b.h and b.cc 530 B.insert(Sym2); 531 IF.Symbols = std::move(B).build(); 532 } 533 { 534 // Should be stored in b.cc 535 IF.Refs = std::move(*refSlab(Sym1.ID, BSourceUri.c_str())); 536 } 537 { 538 RelationSlab::Builder B; 539 // Should be stored in a.h 540 B.insert(Relation{Sym1.ID, RelationKind::BaseOf, Sym2.ID}); 541 // Should be stored in b.h 542 B.insert(Relation{Sym2.ID, RelationKind::BaseOf, Sym1.ID}); 543 // Dangling relation should be dropped. 544 B.insert(Relation{symbol("3").ID, RelationKind::BaseOf, Sym1.ID}); 545 IF.Relations = std::move(B).build(); 546 } 547 548 IF.Sources.emplace(); 549 IncludeGraph &IG = *IF.Sources; 550 { 551 // b.cc includes b.h 552 auto &Node = IG[BSourceUri]; 553 Node.DirectIncludes = {BHeaderUri}; 554 Node.URI = BSourceUri; 555 } 556 { 557 // b.h includes a.h 558 auto &Node = IG[BHeaderUri]; 559 Node.DirectIncludes = {AHeaderUri}; 560 Node.URI = BHeaderUri; 561 } 562 { 563 // a.h includes nothing. 564 auto &Node = IG[AHeaderUri]; 565 Node.DirectIncludes = {}; 566 Node.URI = AHeaderUri; 567 } 568 569 IF.Cmd = tooling::CompileCommand(testRoot(), "b.cc", {"clang"}, "out"); 570 571 FileShardedIndex ShardedIndex(std::move(IF), testPath("b.cc")); 572 ASSERT_THAT( 573 ShardedIndex.getAllFiles(), 574 UnorderedElementsAre(testPath("a.h"), testPath("b.h"), testPath("b.cc"))); 575 576 { 577 auto Shard = ShardedIndex.getShard(testPath("a.h")); 578 EXPECT_THAT(Shard.Symbols.getValue(), UnorderedElementsAre(QName("1"))); 579 EXPECT_THAT(Shard.Refs.getValue(), IsEmpty()); 580 EXPECT_THAT( 581 Shard.Relations.getValue(), 582 UnorderedElementsAre(Relation{Sym1.ID, RelationKind::BaseOf, Sym2.ID})); 583 ASSERT_THAT(Shard.Sources.getValue().keys(), 584 UnorderedElementsAre(AHeaderUri)); 585 EXPECT_THAT(Shard.Sources->lookup(AHeaderUri).DirectIncludes, IsEmpty()); 586 EXPECT_TRUE(Shard.Cmd.hasValue()); 587 } 588 { 589 auto Shard = ShardedIndex.getShard(testPath("b.h")); 590 EXPECT_THAT(Shard.Symbols.getValue(), UnorderedElementsAre(QName("2"))); 591 EXPECT_THAT(Shard.Refs.getValue(), IsEmpty()); 592 EXPECT_THAT( 593 Shard.Relations.getValue(), 594 UnorderedElementsAre(Relation{Sym2.ID, RelationKind::BaseOf, Sym1.ID})); 595 ASSERT_THAT(Shard.Sources.getValue().keys(), 596 UnorderedElementsAre(BHeaderUri, AHeaderUri)); 597 EXPECT_THAT(Shard.Sources->lookup(BHeaderUri).DirectIncludes, 598 UnorderedElementsAre(AHeaderUri)); 599 EXPECT_TRUE(Shard.Cmd.hasValue()); 600 } 601 { 602 auto Shard = ShardedIndex.getShard(testPath("b.cc")); 603 EXPECT_THAT(Shard.Symbols.getValue(), UnorderedElementsAre(QName("2"))); 604 EXPECT_THAT(Shard.Refs.getValue(), UnorderedElementsAre(Pair(Sym1.ID, _))); 605 EXPECT_THAT(Shard.Relations.getValue(), IsEmpty()); 606 ASSERT_THAT(Shard.Sources.getValue().keys(), 607 UnorderedElementsAre(BSourceUri, BHeaderUri)); 608 EXPECT_THAT(Shard.Sources->lookup(BSourceUri).DirectIncludes, 609 UnorderedElementsAre(BHeaderUri)); 610 EXPECT_TRUE(Shard.Cmd.hasValue()); 611 } 612 } 613 } // namespace 614 } // namespace clangd 615 } // namespace clang 616