clang-tools 10.0.0

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 "ParsedAST.h"
21 #include "Path.h"
22 #include "index/Index.h"
23 #include "llvm/ADT/StringMap.h"
24 #include "gtest/gtest.h"
25 #include <string>
26 #include <utility>
27 #include <vector>
28
29 namespace clang {
30 namespace clangd {
31
32 struct TestTU {
33 static TestTU withCode(llvm::StringRef Code) {
34 TestTU TU;
35 TU.Code = Code;
36 return TU;
37 }
38
39 static TestTU withHeaderCode(llvm::StringRef HeaderCode) {
40 TestTU TU;
41 TU.HeaderCode = HeaderCode;
42 return TU;
43 }
44
45 // The code to be compiled.
46 std::string Code;
47 std::string Filename = "TestTU.cpp";
48
49 // Define contents of a header which will be implicitly included by Code.
50 std::string HeaderCode;
51 std::string HeaderFilename = "TestTU.h";
52
53 // Name and contents of each file.
54 llvm::StringMap<std::string> AdditionalFiles;
55
56 // Extra arguments for the compiler invocation.
57 std::vector<const char *> ExtraArgs;
58
59 llvm::Optional<std::string> ClangTidyChecks;
60 llvm::Optional<std::string> ClangTidyWarningsAsErrors;
61 // Index to use when building AST.
62 const SymbolIndex *ExternalIndex = nullptr;
63
64 // Simulate a header guard of the header (using an #import directive).
65 bool ImplicitHeaderGuard = true;
66
67 ParsedAST build() const;
68 SymbolSlab headerSymbols() const;
69 std::unique_ptr<SymbolIndex> index() const;
70 };
71
72 // Look up an index symbol by qualified name, which must be unique.
73 const Symbol &findSymbol(const SymbolSlab &, llvm::StringRef QName);
74 // Look up an AST symbol by qualified name, which must be unique and top-level.
75 const NamedDecl &findDecl(ParsedAST &AST, llvm::StringRef QName);
76 // Look up an AST symbol that satisfies \p Filter.
77 const NamedDecl &findDecl(ParsedAST &AST,
78 std::function<bool(const NamedDecl &)> Filter);
79 // Look up an AST symbol by unqualified name, which must be unique.
80 const NamedDecl &findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name);
81
82 } // namespace clangd
83 } // namespace clang
84
85 #endif // LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANGD_TESTTU_H
clang::clangd::TestTU::build
ParsedAST build() const
Definition: TestTU.cpp:22
clang::clangd::TestTU
Definition: TestTU.h:32
clang::clangd::SymbolSlab
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:177
clang::clangd::TestTU::ClangTidyChecks
llvm::Optional< std::string > ClangTidyChecks
Definition: TestTU.h:59
clang::clangd::TestTU::HeaderCode
std::string HeaderCode
Definition: TestTU.h:50
Index.h
clang::clangd::SymbolIndex
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:85
clang::clangd::TestTU::Code
std::string Code
Definition: TestTU.h:46
clang::clangd::findUnqualifiedDecl
const NamedDecl & findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name)
Definition: TestTU.cpp:159
clang::clangd::TestTU::ExtraArgs
std::vector< const char * > ExtraArgs
Definition: TestTU.h:57
clang::clangd::TestTU::ExternalIndex
const SymbolIndex * ExternalIndex
Definition: TestTU.h:62
QName
std::string QName
Definition: DiagnosticsTests.cpp:523
clang::clangd::SymbolOrigin::AST
clang::clangd::TestTU::withHeaderCode
static TestTU withHeaderCode(llvm::StringRef HeaderCode)
Definition: TestTU.h:39
clang::clangd::TestTU::ImplicitHeaderGuard
bool ImplicitHeaderGuard
Definition: TestTU.h:65
clang::clangd::TestTU::ClangTidyWarningsAsErrors
llvm::Optional< std::string > ClangTidyWarningsAsErrors
Definition: TestTU.h:60
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::clangd::ParsedAST
Stores and provides access to parsed AST.
Definition: ParsedAST.h:46
clang::clangd::TestTU::withCode
static TestTU withCode(llvm::StringRef Code)
Definition: TestTU.h:33
clang::clangd::TestTU::headerSymbols
SymbolSlab headerSymbols() const
Definition: TestTU.cpp:81
clang::clangd::Symbol
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
ParsedAST.h
clang::clangd::findSymbol
const Symbol & findSymbol(const SymbolSlab &Slab, llvm::StringRef QName)
Definition: TestTU.cpp:97
Path.h
clang::clangd::TestTU::Filename
std::string Filename
Definition: TestTU.h:47
clang::clangd::TestTU::index
std::unique_ptr< SymbolIndex > index() const
Definition: TestTU.cpp:88
clang::clangd::TestTU::HeaderFilename
std::string HeaderFilename
Definition: TestTU.h:51
clang::clangd::TestTU::AdditionalFiles
llvm::StringMap< std::string > AdditionalFiles
Definition: TestTU.h:54
clang::clangd::findDecl
const NamedDecl & findDecl(ParsedAST &AST, llvm::StringRef QName)
Definition: TestTU.cpp:118