clang-tools  12.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 "Compiler.h"
21 #include "ParsedAST.h"
22 #include "TestFS.h"
23 #include "index/Index.h"
24 #include "support/Path.h"
25 #include "llvm/ADT/StringMap.h"
26 #include "gtest/gtest.h"
27 #include <string>
28 #include <utility>
29 #include <vector>
30 
31 namespace clang {
32 namespace clangd {
33 
34 struct TestTU {
35  static TestTU withCode(llvm::StringRef Code) {
36  TestTU TU;
37  TU.Code = std::string(Code);
38  return TU;
39  }
40 
41  static TestTU withHeaderCode(llvm::StringRef HeaderCode) {
42  TestTU TU;
43  TU.HeaderCode = std::string(HeaderCode);
44  return TU;
45  }
46 
47  // The code to be compiled.
48  std::string Code;
49  std::string Filename = "TestTU.cpp";
50 
51  // Define contents of a header which will be implicitly included by Code.
52  std::string HeaderCode;
53  std::string HeaderFilename = "TestTU.h";
54 
55  // Name and contents of each file.
56  llvm::StringMap<std::string> AdditionalFiles;
57 
58  // Extra arguments for the compiler invocation.
59  std::vector<std::string> ExtraArgs;
60 
61  llvm::Optional<std::string> ClangTidyChecks;
62  llvm::Optional<std::string> ClangTidyWarningsAsErrors;
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  // By default, build() will report Error diagnostics as GTest errors.
70  // Suppress this behavior by adding an 'error-ok' comment to the code.
71  ParsedAST build() const;
72  std::shared_ptr<const PreambleData> preamble() const;
73  ParseInputs inputs(MockFS &FS) const;
74  SymbolSlab headerSymbols() const;
75  RefSlab headerRefs() const;
76  std::unique_ptr<SymbolIndex> index() const;
77 };
78 
79 // Look up an index symbol by qualified name, which must be unique.
80 const Symbol &findSymbol(const SymbolSlab &, llvm::StringRef QName);
81 // Look up an AST symbol by qualified name, which must be unique and top-level.
82 const NamedDecl &findDecl(ParsedAST &AST, llvm::StringRef QName);
83 // Look up an AST symbol that satisfies \p Filter.
84 const NamedDecl &findDecl(ParsedAST &AST,
85  std::function<bool(const NamedDecl &)> Filter);
86 // Look up an AST symbol by unqualified name, which must be unique.
87 const NamedDecl &findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name);
88 
89 } // namespace clangd
90 } // namespace clang
91 
92 #endif // LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANGD_TESTTU_H
ParsedAST build() const
Definition: TestTU.cpp:80
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:177
llvm::Optional< std::string > ClangTidyChecks
Definition: TestTU.h:61
std::string HeaderCode
Definition: TestTU.h:52
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:104
std::shared_ptr< const PreambleData > preamble() const
Definition: TestTU.cpp:69
MockFS FS
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:48
const NamedDecl & findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name)
Definition: TestTU.cpp:206
const SymbolIndex * ExternalIndex
Definition: TestTU.h:64
std::string QName
static TestTU withHeaderCode(llvm::StringRef HeaderCode)
Definition: TestTU.h:41
bool ImplicitHeaderGuard
Definition: TestTU.h:67
llvm::Optional< std::string > ClangTidyWarningsAsErrors
Definition: TestTU.h:62
static constexpr llvm::StringLiteral Name
Stores and provides access to parsed AST.
Definition: ParsedAST.h:48
static TestTU withCode(llvm::StringRef Code)
Definition: TestTU.h:35
Information required to run clang, e.g. to parse AST or do code completion.
Definition: Compiler.h:47
SymbolSlab headerSymbols() const
Definition: TestTU.cpp:122
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
RefSlab headerRefs() const
Definition: TestTU.cpp:129
const Symbol & findSymbol(const SymbolSlab &Slab, llvm::StringRef QName)
Definition: TestTU.cpp:144
std::string Filename
Definition: TestTU.h:49
std::unique_ptr< SymbolIndex > index() const
Definition: TestTU.cpp:134
std::string HeaderFilename
Definition: TestTU.h:53
llvm::StringMap< std::string > AdditionalFiles
Definition: TestTU.h:56
ParseInputs inputs(MockFS &FS) const
Definition: TestTU.cpp:23
std::vector< std::string > ExtraArgs
Definition: TestTU.h:59
const NamedDecl & findDecl(ParsedAST &AST, llvm::StringRef QName)
Definition: TestTU.cpp:165