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