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