clang-tools  15.0.0git
TestWorkspace.h
Go to the documentation of this file.
1 //===--- TestWorkspace.h - 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 // TestWorkspace builds on TestTU to provide a way to write tests involving
10 // several related files with inclusion relationships between them.
11 //
12 // The tests can exercise both index and AST based operations.
13 //
14 //===---------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_UNITTESTS_TESTWORKSPACE_H
17 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_UNITTESTS_TESTWORKSPACE_H
18 
19 #include "TestTU.h"
20 #include "index/Index.h"
21 #include "llvm/ADT/StringRef.h"
22 #include <string>
23 #include <vector>
24 
25 namespace clang {
26 namespace clangd {
27 
29 public:
30  // The difference between addSource() and addMainFile() is that only main
31  // files will be indexed.
32  void addSource(llvm::StringRef Filename, llvm::StringRef Code) {
33  addInput(Filename.str(), {Code.str(), /*IsMainFile=*/false});
34  }
35  void addMainFile(llvm::StringRef Filename, llvm::StringRef Code) {
36  addInput(Filename.str(), {Code.str(), /*IsMainFile=*/true});
37  }
38 
39  std::unique_ptr<SymbolIndex> index();
40 
41  Optional<ParsedAST> openFile(llvm::StringRef Filename);
42 
43 private:
44  struct SourceFile {
45  std::string Code;
46  bool IsMainFile = false;
47  };
48  llvm::StringMap<SourceFile> Inputs;
49  TestTU TU;
50 
51  void addInput(llvm::StringRef Filename, const SourceFile &Input);
52 };
53 
54 } // namespace clangd
55 } // namespace clang
56 
57 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_UNITTESTS_TESTWORKSPACE_H
Index.h
clang::clangd::TestWorkspace::addSource
void addSource(llvm::StringRef Filename, llvm::StringRef Code)
Definition: TestWorkspace.h:32
TestTU.h
Filename
std::string Filename
Filename as a string.
Definition: IncludeOrderCheck.cpp:39
clang::clangd::TestWorkspace::openFile
Optional< ParsedAST > openFile(llvm::StringRef Filename)
Definition: TestWorkspace.cpp:34
Code
std::string Code
Definition: FindTargetTests.cpp:67
clang::clangd::TestWorkspace::index
std::unique_ptr< SymbolIndex > index()
Definition: TestWorkspace.cpp:16
clang::clangd::TestWorkspace
Definition: TestWorkspace.h:28
clang::clangd::TestWorkspace::addMainFile
void addMainFile(llvm::StringRef Filename, llvm::StringRef Code)
Definition: TestWorkspace.h:35
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27