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