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