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