clang-tools  15.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 "index/FileIndex.h"
11 #include "gtest/gtest.h"
12 
13 namespace clang {
14 namespace clangd {
15 
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 
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 
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
clang::clangd::testPath
std::string testPath(PathRef File, llvm::sys::path::Style Style)
Definition: TestFS.cpp:94
clang::clangd::TestTU::build
ParsedAST build() const
Definition: TestTU.cpp:109
Filename
std::string Filename
Filename as a string.
Definition: IncludeOrderCheck.cpp:39
Ctx
Context Ctx
Definition: TUScheduler.cpp:547
TestWorkspace.h
clang::clangd::TestTU::preamble
std::shared_ptr< const PreambleData > preamble(PreambleParsedCallback PreambleCallback=nullptr) const
Definition: TestTU.cpp:95
clang::clangd::TestTU::Code
std::string Code
Definition: TestTU.h:49
clang::clangd::TestWorkspace::openFile
Optional< ParsedAST > openFile(llvm::StringRef Filename)
Definition: TestWorkspace.cpp:34
clang::clangd::CanonicalIncludes
Maps a definition location onto an #include file, based on a set of filename rules.
Definition: CanonicalIncludes.h:37
clang::clangd::TestTU::Filename
std::string Filename
Definition: TestTU.h:50
clang::clangd::TestWorkspace::index
std::unique_ptr< SymbolIndex > index()
Definition: TestWorkspace.cpp:16
FileIndex.h
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:29
Index
const SymbolIndex * Index
Definition: Dexp.cpp:98
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::ParsedAST
Stores and provides access to parsed AST.
Definition: ParsedAST.h:45
clang::clangd::TestTU::AdditionalFiles
llvm::StringMap< std::string > AdditionalFiles
Definition: TestTU.h:57