• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "../TidyProvider.h"
21 #include "Compiler.h"
22 #include "ParsedAST.h"
23 #include "TestFS.h"
24 #include "index/Index.h"
25 #include "support/Path.h"
26 #include "llvm/ADT/StringMap.h"
27 #include "gtest/gtest.h"
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   // Whether to use overlay the TestFS over the real filesystem. This is
70   // required for use of implicit modules.where the module file is written to
71   // disk and later read back.
72   // FIXME: Change the way reading/writing modules work to allow us to keep them
73   // in memory across multiple clang invocations, at least in tests, to
74   // eliminate the need for real file system here.
75   // Please avoid using this for things other than implicit modules. The plan is
76   // to eliminate this option some day.
77   bool OverlayRealFileSystemForModules = false;
78 
79   // By default, build() will report Error diagnostics as GTest errors.
80   // Suppress this behavior by adding an 'error-ok' comment to the code.
81   ParsedAST build() const;
82   std::shared_ptr<const PreambleData>
83   preamble(PreambleParsedCallback PreambleCallback = nullptr) const;
84   ParseInputs inputs(MockFS &FS) const;
85   SymbolSlab headerSymbols() const;
86   RefSlab headerRefs() const;
87   std::unique_ptr<SymbolIndex> index() const;
88 };
89 
90 // Look up an index symbol by qualified name, which must be unique.
91 const Symbol &findSymbol(const SymbolSlab &, llvm::StringRef QName);
92 // Look up an AST symbol by qualified name, which must be unique and top-level.
93 const NamedDecl &findDecl(ParsedAST &AST, llvm::StringRef QName);
94 // Look up an AST symbol that satisfies \p Filter.
95 const NamedDecl &findDecl(ParsedAST &AST,
96                           std::function<bool(const NamedDecl &)> Filter);
97 // Look up an AST symbol by unqualified name, which must be unique.
98 const NamedDecl &findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name);
99 
100 } // namespace clangd
101 } // namespace clang
102 
103 #endif // LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANGD_TESTTU_H
104