clang-tools 17.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 assert(AST->getDiagnostics() &&
136 "TestTU should always build an AST with a fresh Preamble");
137 // Check for error diagnostics and report gtest failures (unless expected).
138 // This guards against accidental syntax errors silently subverting tests.
139 // error-ok is awfully primitive - using clang -verify would be nicer.
140 // Ownership and layering makes it pretty hard.
141 bool ErrorOk = [&, this] {
142 llvm::StringLiteral Marker = "error-ok";
143 if (llvm::StringRef(Code).contains(Marker) ||
144 llvm::StringRef(HeaderCode).contains(Marker))
145 return true;
146 for (const auto &KV : this->AdditionalFiles)
147 if (llvm::StringRef(KV.second).contains(Marker))
148 return true;
149 return false;
150 }();
151 if (!ErrorOk) {
152 // We always build AST with a fresh preamble in TestTU.
153 for (const auto &D : *AST->getDiagnostics())
154 if (D.Severity >= DiagnosticsEngine::Error) {
155 llvm::errs()
156 << "TestTU failed to build (suppress with /*error-ok*/): \n"
157 << D << "\n\nFor code:\n"
158 << Code;
159 std::abort(); // Stop after first error for simplicity.
160 }
161 }
162 return std::move(*AST);
163}
164
166 auto AST = build();
167 return std::get<0>(indexHeaderSymbols(/*Version=*/"null", AST.getASTContext(),
168 AST.getPreprocessor(),
169 AST.getCanonicalIncludes()));
170}
171
173 auto AST = build();
174 return std::get<1>(indexMainDecls(AST));
175}
176
177std::unique_ptr<SymbolIndex> TestTU::index() const {
178 auto AST = build();
179 auto Idx = std::make_unique<FileIndex>();
180 Idx->updatePreamble(testPath(Filename), /*Version=*/"null",
181 AST.getASTContext(), AST.getPreprocessor(),
182 AST.getCanonicalIncludes());
183 Idx->updateMain(testPath(Filename), AST);
184 return std::move(Idx);
185}
186
187const Symbol &findSymbol(const SymbolSlab &Slab, llvm::StringRef QName) {
188 const Symbol *Result = nullptr;
189 for (const Symbol &S : Slab) {
190 if (QName != (S.Scope + S.Name).str())
191 continue;
192 if (Result) {
193 llvm::errs() << "Multiple symbols named " << QName << ":\n"
194 << *Result << "\n---\n"
195 << S;
196 assert(false && "QName is not unique");
197 }
198 Result = &S;
199 }
200 if (!Result) {
201 llvm::errs() << "No symbol named " << QName << " in "
202 << llvm::to_string(Slab);
203 assert(false && "No symbol with QName");
204 }
205 return *Result;
206}
207
208// RAII scoped class to disable TraversalScope for a ParsedAST.
210 ASTContext &Ctx;
211 std::vector<Decl *> ScopeToRestore;
212
213public:
215 : Ctx(AST.getASTContext()), ScopeToRestore(Ctx.getTraversalScope()) {
216 Ctx.setTraversalScope({Ctx.getTranslationUnitDecl()});
217 }
218 ~TraverseHeadersToo() { Ctx.setTraversalScope(std::move(ScopeToRestore)); }
219};
220
221const NamedDecl &findDecl(ParsedAST &AST, llvm::StringRef QName) {
222 auto &Ctx = AST.getASTContext();
223 auto LookupDecl = [&Ctx](const DeclContext &Scope,
224 llvm::StringRef Name) -> const NamedDecl & {
225 auto LookupRes = Scope.lookup(DeclarationName(&Ctx.Idents.get(Name)));
226 assert(!LookupRes.empty() && "Lookup failed");
227 assert(LookupRes.isSingleResult() && "Lookup returned multiple results");
228 return *LookupRes.front();
229 };
230
231 const DeclContext *Scope = Ctx.getTranslationUnitDecl();
232
233 StringRef Cur, Rest;
234 for (std::tie(Cur, Rest) = QName.split("::"); !Rest.empty();
235 std::tie(Cur, Rest) = Rest.split("::")) {
236 Scope = &cast<DeclContext>(LookupDecl(*Scope, Cur));
237 }
238 return LookupDecl(*Scope, Cur);
239}
240
241const NamedDecl &findDecl(ParsedAST &AST,
242 std::function<bool(const NamedDecl &)> Filter) {
244 struct Visitor : RecursiveASTVisitor<Visitor> {
245 decltype(Filter) F;
246 llvm::SmallVector<const NamedDecl *, 1> Decls;
247 bool VisitNamedDecl(const NamedDecl *ND) {
248 if (F(*ND))
249 Decls.push_back(ND);
250 return true;
251 }
252 } Visitor;
253 Visitor.F = Filter;
254 Visitor.TraverseDecl(AST.getASTContext().getTranslationUnitDecl());
255 if (Visitor.Decls.size() != 1) {
256 llvm::errs() << Visitor.Decls.size() << " symbols matched.\n";
257 assert(Visitor.Decls.size() == 1);
258 }
259 return *Visitor.Decls.front();
260}
261
262const NamedDecl &findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name) {
263 return findDecl(AST, [Name](const NamedDecl &ND) {
264 if (auto *ID = ND.getIdentifier())
265 if (ID->getName() == Name)
266 return true;
267 return false;
268 });
269}
270
271} // namespace clangd
272} // namespace clang
Token Name
std::unique_ptr< CompilerInvocation > CI
Stores and provides access to parsed AST.
Definition: ParsedAST.h:47
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:344
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:214
const NamedDecl & findDecl(ParsedAST &AST, llvm::StringRef QName)
Definition: TestTU.cpp:221
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:222
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:581
const NamedDecl & findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name)
Definition: TestTU.cpp:262
void initializeModuleCache(CompilerInvocation &CI)
Definition: TestTU.cpp:81
std::function< void(ASTContext &, Preprocessor &, const CanonicalIncludes &)> PreambleParsedCallback
Definition: Preamble.h:84
const Symbol & findSymbol(const SymbolSlab &Slab, llvm::StringRef QName)
Definition: TestTU.cpp:187
SlabTuple indexHeaderSymbols(llvm::StringRef Version, ASTContext &AST, Preprocessor &PP, const CanonicalIncludes &Includes)
Index declarations from AST and macros from PP that are declared in included headers.
Definition: FileIndex.cpp:229
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:172
ParseOptions ParseOpts
Definition: TestTU.h:73
std::string HeaderFilename
Definition: TestTU.h:54
SymbolSlab headerSymbols() const
Definition: TestTU.cpp:165
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:177