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 {
withCodeTestTU33   static TestTU withCode(llvm::StringRef Code) {
34     TestTU TU;
35     TU.Code = Code;
36     return TU;
37   }
38 
withHeaderCodeTestTU39   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
86