clang-tools 19.0.0git
Go to the documentation of this file.
1//===--- TestTU.h - Scratch source files for testing -------------*- C++-*-===//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
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.
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>
32namespace clang {
33namespace clangd {
35struct TestTU {
36 static TestTU withCode(llvm::StringRef Code) {
37 TestTU TU;
38 TU.Code = std::string(Code);
39 return TU;
40 }
42 static TestTU withHeaderCode(llvm::StringRef HeaderCode) {
43 TestTU TU;
44 TU.HeaderCode = std::string(HeaderCode);
45 return TU;
46 }
48 // The code to be compiled.
49 std::string Code;
50 std::string Filename = "TestTU.cpp";
52 // Define contents of a header which will be implicitly included by Code.
53 std::string HeaderCode;
54 std::string HeaderFilename = "TestTU.h";
56 // Name and contents of each file.
57 llvm::StringMap<std::string> AdditionalFiles;
59 // Extra arguments for the compiler invocation.
60 std::vector<std::string> ExtraArgs;
62 // Predefine macros such as __UINTPTR_TYPE__.
63 bool PredefineMacros = false;
66 // Index to use when building AST.
67 const SymbolIndex *ExternalIndex = nullptr;
69 // Simulate a header guard of the header (using an #import directive).
72 // Parse options pass on to the ParseInputs
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.
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;
95 RefSlab headerRefs() const;
96 std::unique_ptr<SymbolIndex> index() const;
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);
109} // namespace clangd
110} // namespace clang
llvm::SmallString< 256U > Name
A FeatureModuleSet is a collection of feature modules installed in clangd.
Stores and provides access to parsed AST.
Definition: ParsedAST.h:46
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:108
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:113
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:199
std::function< void(CapturedASTCtx ASTCtx, std::shared_ptr< const include_cleaner::PragmaIncludes >)> PreambleParsedCallback
Definition: Preamble.h:132
const NamedDecl & findDecl(ParsedAST &AST, llvm::StringRef QName)
Definition: TestTU.cpp:219
const NamedDecl & findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name)
Definition: TestTU.cpp:260
const Symbol & findSymbol(const SymbolSlab &Slab, llvm::StringRef QName)
Definition: TestTU.cpp:185
llvm::unique_function< void(tidy::ClangTidyOptions &, llvm::StringRef) const > TidyProvider
A factory to modify a tidy::ClangTidyOptions.
Definition: TidyProvider.h:23
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Information required to run clang, e.g. to parse AST or do code completion.
Definition: Compiler.h:49
The class presents a C++ symbol, e.g.
Definition: Symbol.h:39
std::vector< std::string > ExtraArgs
Definition: TestTU.h:60
TidyProvider ClangTidyProvider
Definition: TestTU.h:65
std::string Code
Definition: TestTU.h:49
ParsedAST build() const
Definition: TestTU.cpp:114
static TestTU withHeaderCode(llvm::StringRef HeaderCode)
Definition: TestTU.h:42
std::string Filename
Definition: TestTU.h:50
ParseInputs inputs(MockFS &FS) const
Definition: TestTU.cpp:26
bool ImplicitHeaderGuard
Definition: TestTU.h:70
RefSlab headerRefs() const
Definition: TestTU.cpp:170
ParseOptions ParseOpts
Definition: TestTU.h:73
std::string HeaderFilename
Definition: TestTU.h:54
SymbolSlab headerSymbols() const
Definition: TestTU.cpp:163
static TestTU withCode(llvm::StringRef Code)
Definition: TestTU.h:36
std::shared_ptr< const PreambleData > preamble(PreambleParsedCallback PreambleCallback=nullptr) const
Definition: TestTU.cpp:100
bool OverlayRealFileSystemForModules
Definition: TestTU.h:83
const SymbolIndex * ExternalIndex
Definition: TestTU.h:67
llvm::StringMap< std::string > AdditionalFiles
Definition: TestTU.h:57
FeatureModuleSet * FeatureModules
Definition: TestTU.h:85
std::string HeaderCode
Definition: TestTU.h:53
std::unique_ptr< SymbolIndex > index() const
Definition: TestTU.cpp:175