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
32namespace clang {
33namespace clangd {
34
35struct 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
62 // Predefine macros such as __UINTPTR_TYPE__.
63 bool PredefineMacros = false;
64
65 TidyProvider ClangTidyProvider = {};
66 // Index to use when building AST.
67 const SymbolIndex *ExternalIndex = nullptr;
68
69 // Simulate a header guard of the header (using an #import directive).
70 bool ImplicitHeaderGuard = true;
71
72 // Parse options pass on to the ParseInputs
73 ParseOptions ParseOpts = {};
74
75 // Whether to use overlay the TestFS over the real filesystem. This is
76 // required for use of implicit modules.where the module file is written to
77 // disk and later read back.
78 // FIXME: Change the way reading/writing modules work to allow us to keep them
79 // in memory across multiple clang invocations, at least in tests, to
80 // eliminate the need for real file system here.
81 // Please avoid using this for things other than implicit modules. The plan is
82 // to eliminate this option some day.
83 bool OverlayRealFileSystemForModules = false;
84
85 FeatureModuleSet *FeatureModules = nullptr;
86
87 // By default, build() will report Error diagnostics as GTest errors.
88 // Suppress this behavior by adding an 'error-ok' comment to the code.
89 // The result will always have getDiagnostics() populated.
90 ParsedAST build() const;
91 std::shared_ptr<const PreambleData>
92 preamble(PreambleParsedCallback PreambleCallback = nullptr) const;
93 ParseInputs inputs(MockFS &FS) const;
94 SymbolSlab headerSymbols() const;
95 RefSlab headerRefs() const;
96 std::unique_ptr<SymbolIndex> index() const;
97};
98
99// Look up an index symbol by qualified name, which must be unique.
100const Symbol &findSymbol(const SymbolSlab &, llvm::StringRef QName);
101// Look up an AST symbol by qualified name, which must be unique and top-level.
102const NamedDecl &findDecl(ParsedAST &AST, llvm::StringRef QName);
103// Look up an AST symbol that satisfies \p Filter.
104const NamedDecl &findDecl(ParsedAST &AST,
105 std::function<bool(const NamedDecl &)> Filter);
106// Look up an AST symbol by unqualified name, which must be unique.
107const NamedDecl &findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name);
108
109} // namespace clangd
110} // namespace clang
111
112#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_UNITTESTS_TESTTU_H
113

source code of clang-tools-extra/clangd/unittests/TestTU.h