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