clang-tools  10.0.0svn
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;
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
ParsedAST build() const
Definition: TestTU.cpp:22
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:177
llvm::Optional< std::string > ClangTidyChecks
Definition: TestTU.h:59
std::string HeaderCode
Definition: TestTU.h:50
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:85
std::string Code
Definition: TestTU.h:46
const NamedDecl & findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name)
Definition: TestTU.cpp:159
std::vector< const char * > ExtraArgs
Definition: TestTU.h:57
const SymbolIndex * ExternalIndex
Definition: TestTU.h:62
std::string QName
static TestTU withHeaderCode(llvm::StringRef HeaderCode)
Definition: TestTU.h:39
bool ImplicitHeaderGuard
Definition: TestTU.h:65
llvm::Optional< std::string > ClangTidyWarningsAsErrors
Definition: TestTU.h:60
static constexpr llvm::StringLiteral Name
Stores and provides access to parsed AST.
Definition: ParsedAST.h:46
static TestTU withCode(llvm::StringRef Code)
Definition: TestTU.h:33
SymbolSlab headerSymbols() const
Definition: TestTU.cpp:81
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
const Symbol & findSymbol(const SymbolSlab &Slab, llvm::StringRef QName)
Definition: TestTU.cpp:97
std::string Filename
Definition: TestTU.h:47
std::unique_ptr< SymbolIndex > index() const
Definition: TestTU.cpp:88
std::string HeaderFilename
Definition: TestTU.h:51
llvm::StringMap< std::string > AdditionalFiles
Definition: TestTU.h:54
const NamedDecl & findDecl(ParsedAST &AST, llvm::StringRef QName)
Definition: TestTU.cpp:118