Debian Codegraph @

llvm-project/clang-tools-extra/clangd/unittests/TestWorkspace.cpp

raw
1 //===--- TestWorkspace.cpp - Utility for writing multi-file tests -*- 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 "TestWorkspace.h"
10 #include "index/FileIndex.h"
11 #include "gtest/gtest.h"
12 #include <optional>
13
14 namespace clang {
15 namespace clangd {
16
17 std::unique_ptr<SymbolIndex> TestWorkspace::index() {
18 auto Index = std::make_unique<FileIndex>();
19 for (const auto &Input : Inputs) {
20 if (!Input.second.IsMainFile)
21 continue;
22 TU.Code = Input.second.Code;
23 TU.Filename = Input.first().str();
24 TU.preamble([&](ASTContext &Ctx, Preprocessor &PP,
25 const CanonicalIncludes &CanonIncludes) {
26 Index->updatePreamble(testPath(Input.first()), "null", Ctx, PP,
27 CanonIncludes);
28 });
29 ParsedAST MainAST = TU.build();
30 Index->updateMain(testPath(Input.first()), MainAST);
31 }
32 return Index;
33 }
34
35 std::optional<ParsedAST> TestWorkspace::openFile(llvm::StringRef Filename) {
36 auto It = Inputs.find(Filename);
37 if (It == Inputs.end()) {
38 ADD_FAILURE() << "Accessing non-existing file: " << Filename;
39 return std::nullopt;
40 }
41 TU.Code = It->second.Code;
42 TU.Filename = It->first().str();
43 return TU.build();
44 }
45
46 void TestWorkspace::addInput(llvm::StringRef Filename,
47 const SourceFile &Input) {
48 Inputs.insert(std::make_pair(Filename, Input));
49 TU.AdditionalFiles.insert(std::make_pair(Filename, Input.Code));
50 }
51 } // namespace clangd
52 } // namespace clang