clang-tools 22.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
24namespace clang {
25namespace clangd {
26using testing::ElementsAre;
27using testing::IsEmpty;
28
29std::unique_ptr<SymbolIndex> createIndex() {
30 SymbolSlab::Builder Builder;
31 Builder.insert(symbol("1"));
32 return MemIndex::build(std::move(Builder).build(), RefSlab(), RelationSlab());
33}
34
35TEST(ProjectAware, Test) {
37 return createIndex();
38 };
39
40 auto Idx = createProjectAwareIndex(std::move(Gen), true);
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}
53
54TEST(ProjectAware, CreatedOnce) {
55 unsigned InvocationCount = 0;
57 ++InvocationCount;
58 return createIndex();
59 };
60
61 auto Idx = createProjectAwareIndex(std::move(Gen), true);
62 // No invocation at start.
63 EXPECT_EQ(InvocationCount, 0U);
65 Req.Query = "1";
66 Req.AnyScope = true;
67
68 // Cannot invoke without proper config.
69 match(*Idx, Req);
70 EXPECT_EQ(InvocationCount, 0U);
71
72 Config C;
73 C.Index.External.Kind = Config::ExternalIndexSpec::File;
74 C.Index.External.Location = "test";
75 WithContextValue With(Config::Key, std::move(C));
76 match(*Idx, Req);
77 // Now it should be created.
78 EXPECT_EQ(InvocationCount, 1U);
79 match(*Idx, Req);
80 // It is cached afterwards.
81 EXPECT_EQ(InvocationCount, 1U);
82}
83} // namespace clangd
84} // namespace clang
Runs tasks on separate (detached) threads and wait for all tasks to finish.
Definition Threading.h:108
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:18
An efficient structure of storing large set of symbol references in memory.
Definition Ref.h:111
SymbolSlab::Builder is a mutable container that can 'freeze' to SymbolSlab.
Definition Symbol.h:224
void insert(const Symbol &S)
Adds a symbol, overwriting any existing one with the same ID.
Definition Symbol.cpp:52
WithContextValue extends Context::current() with a single value.
Definition Context.h:200
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
Definition AST.cpp:45
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
TEST(BackgroundQueueTest, Priority)
Symbol symbol(llvm::StringRef QName)
Definition TestIndex.cpp:17
std::unique_ptr< SymbolIndex > createProjectAwareIndex(IndexFactory Gen, bool Sync)
Returns an index that answers queries using external indices.
std::unique_ptr< SymbolIndex > createIndex()
std::function< std::unique_ptr< SymbolIndex >( const Config::ExternalIndexSpec &, AsyncTaskRunner *)> IndexFactory
A functor to create an index for an external index specification.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Describes an external index configuration.
Definition Config.h:78
Settings that express user/project preferences and control clangd behavior.
Definition Config.h:44
static clangd::Key< Config > Key
Context key which can be used to set the current Config.
Definition Config.h:48
std::string Query
A query string for the fuzzy find.
Definition Index.h:29
bool AnyScope
If set to true, allow symbols from any scope.
Definition Index.h:39