clang-tools  14.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 "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, std::shared_ptr<clang::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
clang::clangd::testPath
std::string testPath(PathRef File, llvm::sys::path::Style Style)
Definition: TestFS.cpp:82
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:454
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:50
clang::clangd::TestWorkspace::openFile
Optional< ParsedAST > openFile(llvm::StringRef Filename)
Definition: TestWorkspace.cpp:33
clang::clangd::CanonicalIncludes
Maps a definition location onto an #include file, based on a set of filename rules.
Definition: CanonicalIncludes.h:36
clang::clangd::TestTU::Filename
std::string Filename
Definition: TestTU.h:51
clang::clangd::TestWorkspace::index
std::unique_ptr< SymbolIndex > index()
Definition: TestWorkspace.cpp:15
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:29
Index
const SymbolIndex * Index
Definition: Dexp.cpp:99
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::ParsedAST
Stores and provides access to parsed AST.
Definition: ParsedAST.h:49
clang::clangd::TestTU::AdditionalFiles
llvm::StringMap< std::string > AdditionalFiles
Definition: TestTU.h:58