clang-tools 20.0.0git
TestWorkspace.cpp
Go to the documentation of this file.
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 "clang-include-cleaner/Record.h"
11#include "index/FileIndex.h"
12#include "gtest/gtest.h"
13#include <memory>
14#include <optional>
15
16namespace clang {
17namespace clangd {
18
19std::unique_ptr<SymbolIndex> TestWorkspace::index() {
20 auto Index = std::make_unique<FileIndex>();
21 for (const auto &Input : Inputs) {
22 if (!Input.second.IsMainFile)
23 continue;
24 TU.Code = Input.second.Code;
25 TU.Filename = Input.first().str();
26 TU.preamble([&](CapturedASTCtx ASTCtx,
27 std::shared_ptr<const include_cleaner::PragmaIncludes> PI) {
28 auto &Ctx = ASTCtx.getASTContext();
29 auto &PP = ASTCtx.getPreprocessor();
30 Index->updatePreamble(testPath(Input.first()), "null", Ctx, PP, *PI);
31 });
32 ParsedAST MainAST = TU.build();
33 Index->updateMain(testPath(Input.first()), MainAST);
34 }
35 return Index;
36}
37
38std::optional<ParsedAST> TestWorkspace::openFile(llvm::StringRef Filename) {
39 auto It = Inputs.find(Filename);
40 if (It == Inputs.end()) {
41 ADD_FAILURE() << "Accessing non-existing file: " << Filename;
42 return std::nullopt;
43 }
44 TU.Code = It->second.Code;
45 TU.Filename = It->first().str();
46 return TU.build();
47}
48
49void TestWorkspace::addInput(llvm::StringRef Filename,
50 const SourceFile &Input) {
51 Inputs.insert(std::make_pair(Filename, Input));
52 TU.AdditionalFiles.insert(std::make_pair(Filename, Input.Code));
53}
54} // namespace clangd
55} // namespace clang
std::string Filename
Filename as a string.
Stores and provides access to parsed AST.
Definition: ParsedAST.h:46
std::optional< ParsedAST > openFile(llvm::StringRef Filename)
std::unique_ptr< SymbolIndex > index()
std::string testPath(PathRef File, llvm::sys::path::Style Style)
Definition: TestFS.cpp:93
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
The captured AST context.
Definition: Preamble.h:59
std::string Code
Definition: TestTU.h:49
ParsedAST build() const
Definition: TestTU.cpp:114
std::string Filename
Definition: TestTU.h:50
std::shared_ptr< const PreambleData > preamble(PreambleParsedCallback PreambleCallback=nullptr) const
Definition: TestTU.cpp:100
llvm::StringMap< std::string > AdditionalFiles
Definition: TestTU.h:57