1 //===--- TestWorkspace.cpp - Utility for writing multi-file tests -*- C++-*===//
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
7 //===----------------------------------------------------------------------===//
9 #include "TestWorkspace.h"
10 #include "index/FileIndex.h"
11 #include "gtest/gtest.h"
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
)
22 TU
.Code
= Input
.second
.Code
;
23 TU
.Filename
= Input
.first().str();
25 [&](CapturedASTCtx ASTCtx
,
26 const std::shared_ptr
<const CanonicalIncludes
> CanonIncludes
) {
27 auto &Ctx
= ASTCtx
.getASTContext();
28 auto &PP
= ASTCtx
.getPreprocessor();
29 Index
->updatePreamble(testPath(Input
.first()), "null", Ctx
, PP
,
32 ParsedAST MainAST
= TU
.build();
33 Index
->updateMain(testPath(Input
.first()), MainAST
);
38 std::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
;
44 TU
.Code
= It
->second
.Code
;
45 TU
.Filename
= It
->first().str();
49 void 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
));