clang-tools  14.0.0git
TestTU.h
Go to the documentation of this file.
1 //===--- TestTU.h - Scratch source files for testing -------------*- 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 // Many tests for indexing, code completion etc are most naturally expressed
10 // using code examples.
11 // TestTU lets test define these examples in a common way without dealing with
12 // the mechanics of VFS and compiler interactions, and then easily grab the
13 // AST, particular symbols, etc.
14 //
15 //===---------------------------------------------------------------------===//
16 
17 #ifndef LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANGD_TESTTU_H
18 #define LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANGD_TESTTU_H
19 
20 #include "../TidyProvider.h"
21 #include "Compiler.h"
22 #include "FeatureModule.h"
23 #include "ParsedAST.h"
24 #include "TestFS.h"
25 #include "index/Index.h"
26 #include "support/Path.h"
27 #include "llvm/ADT/StringMap.h"
28 #include <memory>
29 #include <string>
30 #include <utility>
31 #include <vector>
32 
33 namespace clang {
34 namespace clangd {
35 
36 struct TestTU {
37  static TestTU withCode(llvm::StringRef Code) {
38  TestTU TU;
39  TU.Code = std::string(Code);
40  return TU;
41  }
42 
43  static TestTU withHeaderCode(llvm::StringRef HeaderCode) {
44  TestTU TU;
45  TU.HeaderCode = std::string(HeaderCode);
46  return TU;
47  }
48 
49  // The code to be compiled.
50  std::string Code;
51  std::string Filename = "TestTU.cpp";
52 
53  // Define contents of a header which will be implicitly included by Code.
54  std::string HeaderCode;
55  std::string HeaderFilename = "TestTU.h";
56 
57  // Name and contents of each file.
58  llvm::StringMap<std::string> AdditionalFiles;
59 
60  // Extra arguments for the compiler invocation.
61  std::vector<std::string> ExtraArgs;
62 
64  // Index to use when building AST.
65  const SymbolIndex *ExternalIndex = nullptr;
66 
67  // Simulate a header guard of the header (using an #import directive).
68  bool ImplicitHeaderGuard = true;
69 
70  // Whether to use overlay the TestFS over the real filesystem. This is
71  // required for use of implicit modules.where the module file is written to
72  // disk and later read back.
73  // FIXME: Change the way reading/writing modules work to allow us to keep them
74  // in memory across multiple clang invocations, at least in tests, to
75  // eliminate the need for real file system here.
76  // Please avoid using this for things other than implicit modules. The plan is
77  // to eliminate this option some day.
79 
81 
82  // By default, build() will report Error diagnostics as GTest errors.
83  // Suppress this behavior by adding an 'error-ok' comment to the code.
84  // The result will always have getDiagnostics() populated.
85  ParsedAST build() const;
86  std::shared_ptr<const PreambleData>
87  preamble(PreambleParsedCallback PreambleCallback = nullptr) const;
88  ParseInputs inputs(MockFS &FS) const;
89  SymbolSlab headerSymbols() const;
90  RefSlab headerRefs() const;
91  std::unique_ptr<SymbolIndex> index() const;
92 };
93 
94 // Look up an index symbol by qualified name, which must be unique.
95 const Symbol &findSymbol(const SymbolSlab &, llvm::StringRef QName);
96 // Look up an AST symbol by qualified name, which must be unique and top-level.
97 const NamedDecl &findDecl(ParsedAST &AST, llvm::StringRef QName);
98 // Look up an AST symbol that satisfies \p Filter.
99 const NamedDecl &findDecl(ParsedAST &AST,
100  std::function<bool(const NamedDecl &)> Filter);
101 // Look up an AST symbol by unqualified name, which must be unique.
102 const NamedDecl &findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name);
103 
104 } // namespace clangd
105 } // namespace clang
106 
107 #endif // LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANGD_TESTTU_H
clang::clangd::TestTU::ExtraArgs
std::vector< std::string > ExtraArgs
Definition: TestTU.h:61
clang::clangd::TestTU::HeaderFilename
std::string HeaderFilename
Definition: TestTU.h:55
clang::clangd::findDecl
const NamedDecl & findDecl(ParsedAST &AST, llvm::StringRef QName)
Definition: TestTU.cpp:215
clang::clangd::FeatureModuleSet
A FeatureModuleSet is a collection of feature modules installed in clangd.
Definition: FeatureModule.h:145
clang::clangd::PreambleParsedCallback
std::function< void(ASTContext &, std::shared_ptr< clang::Preprocessor >, const CanonicalIncludes &)> PreambleParsedCallback
Definition: Preamble.h:75
clang::clangd::TestTU::build
ParsedAST build() const
Definition: TestTU.cpp:109
Path.h
clang::clangd::TestTU::headerSymbols
SymbolSlab headerSymbols() const
Definition: TestTU.cpp:159
clang::clangd::TestTU::headerRefs
RefSlab headerRefs() const
Definition: TestTU.cpp:166
Index.h
clang::clangd::RefSlab
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:110
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::findUnqualifiedDecl
const NamedDecl & findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name)
Definition: TestTU.cpp:256
clang::clangd::TestTU::ExternalIndex
const SymbolIndex * ExternalIndex
Definition: TestTU.h:65
clang::clangd::TestTU::inputs
ParseInputs inputs(MockFS &FS) const
Definition: TestTU.cpp:28
clang::clangd::TestTU::withHeaderCode
static TestTU withHeaderCode(llvm::StringRef HeaderCode)
Definition: TestTU.h:43
clang::clangd::TestTU::ClangTidyProvider
TidyProvider ClangTidyProvider
Definition: TestTU.h:63
clang::clangd::ParseInputs
Information required to run clang, e.g. to parse AST or do code completion.
Definition: Compiler.h:47
clang::clangd::MockFS
Definition: TestFS.h:34
clang::clangd::TestTU::Filename
std::string Filename
Definition: TestTU.h:51
clang::clangd::TidyProvider
llvm::unique_function< void(tidy::ClangTidyOptions &, llvm::StringRef) const > TidyProvider
A factory to modify a tidy::ClangTidyOptions.
Definition: TidyProvider.h:24
TestFS.h
clang::clangd::Symbol
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::clangd::TestTU::OverlayRealFileSystemForModules
bool OverlayRealFileSystemForModules
Definition: TestTU.h:78
clang::clangd::TestTU
Definition: TestTU.h:36
clang::clangd::TestTU::withCode
static TestTU withCode(llvm::StringRef Code)
Definition: TestTU.h:37
clang::clangd::SymbolIndex
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:111
clang::clangd::TestTU::ImplicitHeaderGuard
bool ImplicitHeaderGuard
Definition: TestTU.h:68
Compiler.h
FeatureModule.h
clang::clangd::TestTU::index
std::unique_ptr< SymbolIndex > index() const
Definition: TestTU.cpp:171
clang::clangd::TestTU::FeatureModules
FeatureModuleSet * FeatureModules
Definition: TestTU.h:80
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::SymbolSlab
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:177
clang::clangd::TestTU::AdditionalFiles
llvm::StringMap< std::string > AdditionalFiles
Definition: TestTU.h:58
clang::clangd::findSymbol
const Symbol & findSymbol(const SymbolSlab &Slab, llvm::StringRef QName)
Definition: TestTU.cpp:181
clang::clangd::TestTU::HeaderCode
std::string HeaderCode
Definition: TestTU.h:54
ParsedAST.h