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