clang-tools  14.0.0git
ProjectAwareIndexTests.cpp
Go to the documentation of this file.
1 //===-- ProjectAwareIndexTests.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 "Config.h"
10 #include "TestIndex.h"
11 #include "index/Index.h"
12 #include "index/MemIndex.h"
13 #include "index/ProjectAware.h"
14 #include "index/Ref.h"
15 #include "index/Relation.h"
16 #include "support/Context.h"
17 #include "support/Threading.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 #include <memory>
22 #include <utility>
23 
24 namespace clang {
25 namespace clangd {
26 using testing::ElementsAre;
27 using testing::IsEmpty;
28 
29 std::unique_ptr<SymbolIndex> createIndex() {
31  Builder.insert(symbol("1"));
32  return MemIndex::build(std::move(Builder).build(), RefSlab(), RelationSlab());
33 }
34 
35 TEST(ProjectAware, Test) {
37  return createIndex();
38  };
39 
40  auto Idx = createProjectAwareIndex(std::move(Gen), true);
41  FuzzyFindRequest Req;
42  Req.Query = "1";
43  Req.AnyScope = true;
44 
45  EXPECT_THAT(match(*Idx, Req), IsEmpty());
46 
47  Config C;
48  C.Index.External.Kind = Config::ExternalIndexSpec::File;
49  C.Index.External.Location = "test";
50  WithContextValue With(Config::Key, std::move(C));
51  EXPECT_THAT(match(*Idx, Req), ElementsAre("1"));
52  return;
53 }
54 
55 TEST(ProjectAware, CreatedOnce) {
56  unsigned InvocationCount = 0;
58  ++InvocationCount;
59  return createIndex();
60  };
61 
62  auto Idx = createProjectAwareIndex(std::move(Gen), true);
63  // No invocation at start.
64  EXPECT_EQ(InvocationCount, 0U);
65  FuzzyFindRequest Req;
66  Req.Query = "1";
67  Req.AnyScope = true;
68 
69  // Cannot invoke without proper config.
70  match(*Idx, Req);
71  EXPECT_EQ(InvocationCount, 0U);
72 
73  Config C;
74  C.Index.External.Kind = Config::ExternalIndexSpec::File;
75  C.Index.External.Location = "test";
76  WithContextValue With(Config::Key, std::move(C));
77  match(*Idx, Req);
78  // Now it should be created.
79  EXPECT_EQ(InvocationCount, 1U);
80  match(*Idx, Req);
81  // It is cached afterwards.
82  EXPECT_EQ(InvocationCount, 1U);
83  return;
84 }
85 } // namespace clangd
86 } // namespace clang
clang::clangd::TEST
TEST(BackgroundQueueTest, Priority)
Definition: BackgroundIndexTests.cpp:751
clang::clangd::createProjectAwareIndex
std::unique_ptr< SymbolIndex > createProjectAwareIndex(IndexFactory Gen, bool Sync)
Returns an index that answers queries using external indices.
Definition: ProjectAware.cpp:142
Index.h
clang::clangd::Config
Settings that express user/project preferences and control clangd behavior.
Definition: Config.h:43
clang::clangd::RefSlab
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:110
clang::clangd::FuzzyFindRequest
Definition: Index.h:27
clang::clangd::Config::ExternalIndexSpec::File
@ File
Definition: Config.h:73
ProjectAware.h
clang::clangd::FuzzyFindRequest::Query
std::string Query
A query string for the fuzzy find.
Definition: Index.h:30
clang::clangd::match
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
Definition: TestIndex.cpp:94
Relation.h
MemIndex.h
clang::clangd::WithContextValue
WithContextValue extends Context::current() with a single value.
Definition: Context.h:202
clang::clangd::RelationSlab
Definition: Relation.h:52
Builder
CodeCompletionBuilder Builder
Definition: CodeCompletionStringsTests.cpp:36
Threading.h
clang::clangd::MemIndex::build
static std::unique_ptr< SymbolIndex > build(SymbolSlab Symbols, RefSlab Refs, RelationSlab Relations)
Builds an index from slabs. The index takes ownership of the data.
Definition: MemIndex.cpp:19
Config.h
TestIndex.h
Ref.h
C
const Criteria C
Definition: FunctionCognitiveComplexityCheck.cpp:93
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::Config::ExternalIndexSpec
Describes an external index configuration.
Definition: Config.h:72
clang::clangd::AsyncTaskRunner
Runs tasks on separate (detached) threads and wait for all tasks to finish.
Definition: Threading.h:106
clang::clangd::createIndex
std::unique_ptr< SymbolIndex > createIndex()
Definition: ProjectAwareIndexTests.cpp:29
clang::clangd::FuzzyFindRequest::AnyScope
bool AnyScope
If set to true, allow symbols from any scope.
Definition: Index.h:40
clang::clangd::Config::Key
static clangd::Key< Config > Key
Context key which can be used to set the current Config.
Definition: Config.h:47
clang::clangd::symbol
Symbol symbol(llvm::StringRef QName)
Definition: TestIndex.cpp:16
clang::clangd::SymbolSlab::Builder
SymbolSlab::Builder is a mutable container that can 'freeze' to SymbolSlab.
Definition: Symbol.h:200
clang::clangd::IndexFactory
std::function< std::unique_ptr< SymbolIndex >(const Config::ExternalIndexSpec &, AsyncTaskRunner *)> IndexFactory
A functor to create an index for an external index specification.
Definition: ProjectAware.h:25
Context.h