clang-tools 19.0.0git
TestTU.cpp
Go to the documentation of this file.
1//===--- TestTU.cpp - Scratch source files for testing --------------------===//
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#include "TestTU.h"
10#include "CompileCommands.h"
11#include "Compiler.h"
12#include "Diagnostics.h"
13#include "TestFS.h"
14#include "index/FileIndex.h"
15#include "clang/AST/RecursiveASTVisitor.h"
16#include "clang/Basic/Diagnostic.h"
17#include "clang/Frontend/CompilerInvocation.h"
18#include "llvm/ADT/ScopeExit.h"
19#include "llvm/Support/ScopedPrinter.h"
20#include "llvm/Support/raw_ostream.h"
21#include <cstdlib>
22
23namespace clang {
24namespace clangd {
25
27 std::string FullFilename = testPath(Filename),
28 FullHeaderName = testPath(HeaderFilename),
29 ImportThunk = testPath("import_thunk.h");
30 // We want to implicitly include HeaderFilename without messing up offsets.
31 // -include achieves this, but sometimes we want #import (to simulate a header
32 // guard without messing up offsets). In this case, use an intermediate file.
33 std::string ThunkContents = "#import \"" + FullHeaderName + "\"\n";
34
35 FS.Files = AdditionalFiles;
36 FS.Files[FullFilename] = Code;
37 FS.Files[FullHeaderName] = HeaderCode;
38 FS.Files[ImportThunk] = ThunkContents;
39
40 ParseInputs Inputs;
41 Inputs.FeatureModules = FeatureModules;
42 auto &Argv = Inputs.CompileCommand.CommandLine;
43 Argv = {"clang"};
44 // In tests, unless explicitly specified otherwise, omit predefined macros
45 // (__GNUC__ etc) for a 25% speedup. There are hundreds, and we'd generate,
46 // parse, serialize, and re-parse them!
47 if (!PredefineMacros) {
48 Argv.push_back("-Xclang");
49 Argv.push_back("-undef");
50 }
51 // FIXME: this shouldn't need to be conditional, but it breaks a
52 // GoToDefinition test for some reason (getMacroArgExpandedLocation fails).
53 if (!HeaderCode.empty()) {
54 Argv.push_back("-include");
55 Argv.push_back(ImplicitHeaderGuard ? ImportThunk : FullHeaderName);
56 // ms-compatibility changes the meaning of #import.
57 // The default is OS-dependent (on windows), ensure it's off.
59 Inputs.CompileCommand.CommandLine.push_back("-fno-ms-compatibility");
60 }
61 Argv.insert(Argv.end(), ExtraArgs.begin(), ExtraArgs.end());
62 // Put the file name at the end -- this allows the extra arg (-xc++) to
63 // override the language setting.
64 Argv.push_back(FullFilename);
65
66 auto Mangler = CommandMangler::forTests();
67 Mangler(Inputs.CompileCommand, FullFilename);
68 Inputs.CompileCommand.Filename = FullFilename;
69 Inputs.CompileCommand.Directory = testRoot();
70 Inputs.Contents = Code;
72 FS.OverlayRealFileSystemForModules = true;
73 Inputs.TFS = &FS;
74 Inputs.Opts = ParseOptions();
76 Inputs.ClangTidyProvider = ClangTidyProvider;
77 Inputs.Index = ExternalIndex;
78 return Inputs;
79}
80
81void initializeModuleCache(CompilerInvocation &CI) {
82 llvm::SmallString<128> ModuleCachePath;
83 if (llvm::sys::fs::createUniqueDirectory("module-cache", ModuleCachePath)) {
84 llvm::errs() << "Failed to create temp directory for module-cache";
85 std::abort();
86 }
87 CI.getHeaderSearchOpts().ModuleCachePath = ModuleCachePath.c_str();
88}
89
90void deleteModuleCache(const std::string ModuleCachePath) {
91 if (!ModuleCachePath.empty()) {
92 if (llvm::sys::fs::remove_directories(ModuleCachePath)) {
93 llvm::errs() << "Failed to delete temp directory for module-cache";
94 std::abort();
95 }
96 }
97}
98
99std::shared_ptr<const PreambleData>
101 MockFS FS;
102 auto Inputs = inputs(FS);
103 IgnoreDiagnostics Diags;
104 auto CI = buildCompilerInvocation(Inputs, Diags);
105 assert(CI && "Failed to build compilation invocation.");
108 auto ModuleCacheDeleter = llvm::make_scope_exit(
109 std::bind(deleteModuleCache, CI->getHeaderSearchOpts().ModuleCachePath));
111 /*StoreInMemory=*/true, PreambleCallback);
112}
113
115 MockFS FS;
116 auto Inputs = inputs(FS);
117 Inputs.Opts = ParseOpts;
118 StoreDiags Diags;
119 auto CI = buildCompilerInvocation(Inputs, Diags);
120 assert(CI && "Failed to build compilation invocation.");
123 auto ModuleCacheDeleter = llvm::make_scope_exit(
124 std::bind(deleteModuleCache, CI->getHeaderSearchOpts().ModuleCachePath));
125
127 /*StoreInMemory=*/true,
128 /*PreambleCallback=*/nullptr);
129 auto AST = ParsedAST::build(testPath(Filename), Inputs, std::move(CI),
130 Diags.take(), Preamble);
131 if (!AST) {
132 llvm::errs() << "Failed to build code:\n" << Code;
133 std::abort();
134 }
135 // Check for error diagnostics and report gtest failures (unless expected).
136 // This guards against accidental syntax errors silently subverting tests.
137 // error-ok is awfully primitive - using clang -verify would be nicer.
138 // Ownership and layering makes it pretty hard.
139 bool ErrorOk = [&, this] {
140 llvm::StringLiteral Marker = "error-ok";
141 if (llvm::StringRef(Code).contains(Marker) ||
142 llvm::StringRef(HeaderCode).contains(Marker))
143 return true;
144 for (const auto &KV : this->AdditionalFiles)
145 if (llvm::StringRef(KV.second).contains(Marker))
146 return true;
147 return false;
148 }();
149 if (!ErrorOk) {
150 // We always build AST with a fresh preamble in TestTU.
151 for (const auto &D : AST->getDiagnostics())
152 if (D.Severity >= DiagnosticsEngine::Error) {
153 llvm::errs()
154 << "TestTU failed to build (suppress with /*error-ok*/): \n"
155 << D << "\n\nFor code:\n"
156 << Code;
157 std::abort(); // Stop after first error for simplicity.
158 }
159 }
160 return std::move(*AST);
161}
162
164 auto AST = build();
165 return std::get<0>(indexHeaderSymbols(
166 /*Version=*/"null", AST.getASTContext(), AST.getPreprocessor(),
167 AST.getPragmaIncludes()));
168}
169
171 auto AST = build();
172 return std::get<1>(indexMainDecls(AST));
173}
174
175std::unique_ptr<SymbolIndex> TestTU::index() const {
176 auto AST = build();
177 auto Idx = std::make_unique<FileIndex>();
178 Idx->updatePreamble(testPath(Filename), /*Version=*/"null",
179 AST.getASTContext(), AST.getPreprocessor(),
180 AST.getPragmaIncludes());
181 Idx->updateMain(testPath(Filename), AST);
182 return std::move(Idx);
183}
184
185const Symbol &findSymbol(const SymbolSlab &Slab, llvm::StringRef QName) {
186 const Symbol *Result = nullptr;
187 for (const Symbol &S : Slab) {
188 if (QName != (S.Scope + S.Name).str())
189 continue;
190 if (Result) {
191 llvm::errs() << "Multiple symbols named " << QName << ":\n"
192 << *Result << "\n---\n"
193 << S;
194 assert(false && "QName is not unique");
195 }
196 Result = &S;
197 }
198 if (!Result) {
199 llvm::errs() << "No symbol named " << QName << " in "
200 << llvm::to_string(Slab);
201 assert(false && "No symbol with QName");
202 }
203 return *Result;
204}
205
206// RAII scoped class to disable TraversalScope for a ParsedAST.
208 ASTContext &Ctx;
209 std::vector<Decl *> ScopeToRestore;
210
211public:
213 : Ctx(AST.getASTContext()), ScopeToRestore(Ctx.getTraversalScope()) {
214 Ctx.setTraversalScope({Ctx.getTranslationUnitDecl()});
215 }
216 ~TraverseHeadersToo() { Ctx.setTraversalScope(std::move(ScopeToRestore)); }
217};
218
219const NamedDecl &findDecl(ParsedAST &AST, llvm::StringRef QName) {
220 auto &Ctx = AST.getASTContext();
221 auto LookupDecl = [&Ctx](const DeclContext &Scope,
222 llvm::StringRef Name) -> const NamedDecl & {
223 auto LookupRes = Scope.lookup(DeclarationName(&Ctx.Idents.get(Name)));
224 assert(!LookupRes.empty() && "Lookup failed");
225 assert(LookupRes.isSingleResult() && "Lookup returned multiple results");
226 return *LookupRes.front();
227 };
228
229 const DeclContext *Scope = Ctx.getTranslationUnitDecl();
230
231 StringRef Cur, Rest;
232 for (std::tie(Cur, Rest) = QName.split("::"); !Rest.empty();
233 std::tie(Cur, Rest) = Rest.split("::")) {
234 Scope = &cast<DeclContext>(LookupDecl(*Scope, Cur));
235 }
236 return LookupDecl(*Scope, Cur);
237}
238
239const NamedDecl &findDecl(ParsedAST &AST,
240 std::function<bool(const NamedDecl &)> Filter) {
242 struct Visitor : RecursiveASTVisitor<Visitor> {
243 decltype(Filter) F;
244 llvm::SmallVector<const NamedDecl *, 1> Decls;
245 bool VisitNamedDecl(const NamedDecl *ND) {
246 if (F(*ND))
247 Decls.push_back(ND);
248 return true;
249 }
250 } Visitor;
251 Visitor.F = Filter;
252 Visitor.TraverseDecl(AST.getASTContext().getTranslationUnitDecl());
253 if (Visitor.Decls.size() != 1) {
254 llvm::errs() << Visitor.Decls.size() << " symbols matched.\n";
255 assert(Visitor.Decls.size() == 1);
256 }
257 return *Visitor.Decls.front();
258}
259
260const NamedDecl &findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name) {
261 return findDecl(AST, [Name](const NamedDecl &ND) {
262 if (auto *ID = ND.getIdentifier())
263 if (ID->getName() == Name)
264 return true;
265 return false;
266 });
267}
268
269} // namespace clangd
270} // namespace clang
llvm::SmallString< 256U > Name
std::unique_ptr< CompilerInvocation > CI
Stores and provides access to parsed AST.
Definition: ParsedAST.h:46
static std::optional< ParsedAST > build(llvm::StringRef Filename, const ParseInputs &Inputs, std::unique_ptr< clang::CompilerInvocation > CI, llvm::ArrayRef< Diag > CompilerInvocationDiags, std::shared_ptr< const PreambleData > Preamble)
Attempts to run Clang and store the parsed AST.
Definition: ParsedAST.cpp:402
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:108
StoreDiags collects the diagnostics that can later be reported by clangd.
Definition: Diagnostics.h:138
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:199
TraverseHeadersToo(ParsedAST &AST)
Definition: TestTU.cpp:212
std::function< void(CapturedASTCtx ASTCtx, std::shared_ptr< const include_cleaner::PragmaIncludes >)> PreambleParsedCallback
Definition: Preamble.h:123
const NamedDecl & findDecl(ParsedAST &AST, llvm::StringRef QName)
Definition: TestTU.cpp:219
std::unique_ptr< CompilerInvocation > buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D, std::vector< std::string > *CC1Args)
Builds compiler invocation that could be used to build AST or preamble.
Definition: Compiler.cpp:95
SlabTuple indexMainDecls(ParsedAST &AST)
Retrieves symbols and refs of local top level decls in AST (i.e.
Definition: FileIndex.cpp:223
std::string testPath(PathRef File, llvm::sys::path::Style Style)
Definition: TestFS.cpp:93
std::shared_ptr< const PreambleData > buildPreamble(PathRef FileName, CompilerInvocation CI, const ParseInputs &Inputs, bool StoreInMemory, PreambleParsedCallback PreambleCallback, PreambleBuildStats *Stats)
Build a preamble for the new inputs unless an old one can be reused.
Definition: Preamble.cpp:591
SlabTuple indexHeaderSymbols(llvm::StringRef Version, ASTContext &AST, Preprocessor &PP, const include_cleaner::PragmaIncludes &PI)
Index declarations from AST and macros from PP that are declared in included headers.
Definition: FileIndex.cpp:230
const NamedDecl & findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name)
Definition: TestTU.cpp:260
void initializeModuleCache(CompilerInvocation &CI)
Definition: TestTU.cpp:81
const Symbol & findSymbol(const SymbolSlab &Slab, llvm::StringRef QName)
Definition: TestTU.cpp:185
void deleteModuleCache(const std::string ModuleCachePath)
Definition: TestTU.cpp:90
const char * testRoot()
Definition: TestFS.cpp:85
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static CommandMangler forTests()
Information required to run clang, e.g. to parse AST or do code completion.
Definition: Compiler.h:48
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
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
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