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 "Compiler.h"
11 #include "Diagnostics.h"
12 #include "TestFS.h"
13 #include "index/FileIndex.h"
14 #include "index/MemIndex.h"
15 #include "clang/AST/RecursiveASTVisitor.h"
16 #include "clang/Basic/Diagnostic.h"
17 #include "clang/Frontend/CompilerInvocation.h"
18 #include "clang/Frontend/Utils.h"
19 #include "llvm/ADT/ScopeExit.h"
20
21 namespace clang {
22 namespace clangd {
23
inputs(MockFS & FS) const24 ParseInputs TestTU::inputs(MockFS &FS) const {
25 std::string FullFilename = testPath(Filename),
26 FullHeaderName = testPath(HeaderFilename),
27 ImportThunk = testPath("import_thunk.h");
28 // We want to implicitly include HeaderFilename without messing up offsets.
29 // -include achieves this, but sometimes we want #import (to simulate a header
30 // guard without messing up offsets). In this case, use an intermediate file.
31 std::string ThunkContents = "#import \"" + FullHeaderName + "\"\n";
32
33 FS.Files = AdditionalFiles;
34 FS.Files[FullFilename] = Code;
35 FS.Files[FullHeaderName] = HeaderCode;
36 FS.Files[ImportThunk] = ThunkContents;
37
38 ParseInputs Inputs;
39 auto &Argv = Inputs.CompileCommand.CommandLine;
40 Argv = {"clang"};
41 // FIXME: this shouldn't need to be conditional, but it breaks a
42 // GoToDefinition test for some reason (getMacroArgExpandedLocation fails).
43 if (!HeaderCode.empty()) {
44 Argv.push_back("-include");
45 Argv.push_back(ImplicitHeaderGuard ? ImportThunk : FullHeaderName);
46 // ms-compatibility changes the meaning of #import.
47 // The default is OS-dependent (on on windows), ensure it's off.
48 if (ImplicitHeaderGuard)
49 Inputs.CompileCommand.CommandLine.push_back("-fno-ms-compatibility");
50 }
51 Argv.insert(Argv.end(), ExtraArgs.begin(), ExtraArgs.end());
52 // Put the file name at the end -- this allows the extra arg (-xc++) to
53 // override the language setting.
54 Argv.push_back(FullFilename);
55 Inputs.CompileCommand.Filename = FullFilename;
56 Inputs.CompileCommand.Directory = testRoot();
57 Inputs.Contents = Code;
58 if (OverlayRealFileSystemForModules)
59 FS.OverlayRealFileSystemForModules = true;
60 Inputs.TFS = &FS;
61 Inputs.Opts = ParseOptions();
62 if (ClangTidyProvider)
63 Inputs.ClangTidyProvider = ClangTidyProvider;
64 Inputs.Index = ExternalIndex;
65 if (Inputs.Index)
66 Inputs.Opts.SuggestMissingIncludes = true;
67 return Inputs;
68 }
69
initializeModuleCache(CompilerInvocation & CI)70 void initializeModuleCache(CompilerInvocation &CI) {
71 llvm::SmallString<128> ModuleCachePath;
72 ASSERT_FALSE(
73 llvm::sys::fs::createUniqueDirectory("module-cache", ModuleCachePath));
74 CI.getHeaderSearchOpts().ModuleCachePath = ModuleCachePath.c_str();
75 }
76
deleteModuleCache(const std::string ModuleCachePath)77 void deleteModuleCache(const std::string ModuleCachePath) {
78 if (!ModuleCachePath.empty()) {
79 ASSERT_FALSE(llvm::sys::fs::remove_directories(ModuleCachePath));
80 }
81 }
82
83 std::shared_ptr<const PreambleData>
preamble(PreambleParsedCallback PreambleCallback) const84 TestTU::preamble(PreambleParsedCallback PreambleCallback) const {
85 MockFS FS;
86 auto Inputs = inputs(FS);
87 IgnoreDiagnostics Diags;
88 auto CI = buildCompilerInvocation(Inputs, Diags);
89 assert(CI && "Failed to build compilation invocation.");
90 if (OverlayRealFileSystemForModules)
91 initializeModuleCache(*CI);
92 auto ModuleCacheDeleter = llvm::make_scope_exit(
93 std::bind(deleteModuleCache, CI->getHeaderSearchOpts().ModuleCachePath));
94 return clang::clangd::buildPreamble(testPath(Filename), *CI, Inputs,
95 /*StoreInMemory=*/true, PreambleCallback);
96 }
97
build() const98 ParsedAST TestTU::build() const {
99 MockFS FS;
100 auto Inputs = inputs(FS);
101 StoreDiags Diags;
102 auto CI = buildCompilerInvocation(Inputs, Diags);
103 assert(CI && "Failed to build compilation invocation.");
104 if (OverlayRealFileSystemForModules)
105 initializeModuleCache(*CI);
106 auto ModuleCacheDeleter = llvm::make_scope_exit(
107 std::bind(deleteModuleCache, CI->getHeaderSearchOpts().ModuleCachePath));
108
109 auto Preamble = clang::clangd::buildPreamble(testPath(Filename), *CI, Inputs,
110 /*StoreInMemory=*/true,
111 /*PreambleCallback=*/nullptr);
112 auto AST = ParsedAST::build(testPath(Filename), Inputs, std::move(CI),
113 Diags.take(), Preamble);
114 if (!AST.hasValue()) {
115 ADD_FAILURE() << "Failed to build code:\n" << Code;
116 llvm_unreachable("Failed to build TestTU!");
117 }
118 // Check for error diagnostics and report gtest failures (unless expected).
119 // This guards against accidental syntax errors silently subverting tests.
120 // error-ok is awfully primitive - using clang -verify would be nicer.
121 // Ownership and layering makes it pretty hard.
122 bool ErrorOk = [&, this] {
123 llvm::StringLiteral Marker = "error-ok";
124 if (llvm::StringRef(Code).contains(Marker) ||
125 llvm::StringRef(HeaderCode).contains(Marker))
126 return true;
127 for (const auto &KV : this->AdditionalFiles)
128 if (llvm::StringRef(KV.second).contains(Marker))
129 return true;
130 return false;
131 }();
132 if (!ErrorOk) {
133 for (const auto &D : AST->getDiagnostics())
134 if (D.Severity >= DiagnosticsEngine::Error) {
135 ADD_FAILURE()
136 << "TestTU failed to build (suppress with /*error-ok*/): \n"
137 << D << "\n\nFor code:\n"
138 << Code;
139 break; // Just report first error for simplicity.
140 }
141 }
142 return std::move(*AST);
143 }
144
headerSymbols() const145 SymbolSlab TestTU::headerSymbols() const {
146 auto AST = build();
147 return std::get<0>(indexHeaderSymbols(/*Version=*/"null", AST.getASTContext(),
148 AST.getPreprocessorPtr(),
149 AST.getCanonicalIncludes()));
150 }
151
headerRefs() const152 RefSlab TestTU::headerRefs() const {
153 auto AST = build();
154 return std::get<1>(indexMainDecls(AST));
155 }
156
index() const157 std::unique_ptr<SymbolIndex> TestTU::index() const {
158 auto AST = build();
159 auto Idx = std::make_unique<FileIndex>(/*UseDex=*/true,
160 /*CollectMainFileRefs=*/true);
161 Idx->updatePreamble(testPath(Filename), /*Version=*/"null",
162 AST.getASTContext(), AST.getPreprocessorPtr(),
163 AST.getCanonicalIncludes());
164 Idx->updateMain(testPath(Filename), AST);
165 return std::move(Idx);
166 }
167
findSymbol(const SymbolSlab & Slab,llvm::StringRef QName)168 const Symbol &findSymbol(const SymbolSlab &Slab, llvm::StringRef QName) {
169 const Symbol *Result = nullptr;
170 for (const Symbol &S : Slab) {
171 if (QName != (S.Scope + S.Name).str())
172 continue;
173 if (Result) {
174 ADD_FAILURE() << "Multiple symbols named " << QName << ":\n"
175 << *Result << "\n---\n"
176 << S;
177 assert(false && "QName is not unique");
178 }
179 Result = &S;
180 }
181 if (!Result) {
182 ADD_FAILURE() << "No symbol named " << QName << " in "
183 << ::testing::PrintToString(Slab);
184 assert(false && "No symbol with QName");
185 }
186 return *Result;
187 }
188
findDecl(ParsedAST & AST,llvm::StringRef QName)189 const NamedDecl &findDecl(ParsedAST &AST, llvm::StringRef QName) {
190 llvm::SmallVector<llvm::StringRef, 4> Components;
191 QName.split(Components, "::");
192
193 auto &Ctx = AST.getASTContext();
194 auto LookupDecl = [&Ctx](const DeclContext &Scope,
195 llvm::StringRef Name) -> const NamedDecl & {
196 auto LookupRes = Scope.lookup(DeclarationName(&Ctx.Idents.get(Name)));
197 assert(!LookupRes.empty() && "Lookup failed");
198 assert(LookupRes.size() == 1 && "Lookup returned multiple results");
199 return *LookupRes.front();
200 };
201
202 const DeclContext *Scope = Ctx.getTranslationUnitDecl();
203 for (auto NameIt = Components.begin(), End = Components.end() - 1;
204 NameIt != End; ++NameIt) {
205 Scope = &cast<DeclContext>(LookupDecl(*Scope, *NameIt));
206 }
207 return LookupDecl(*Scope, Components.back());
208 }
209
findDecl(ParsedAST & AST,std::function<bool (const NamedDecl &)> Filter)210 const NamedDecl &findDecl(ParsedAST &AST,
211 std::function<bool(const NamedDecl &)> Filter) {
212 struct Visitor : RecursiveASTVisitor<Visitor> {
213 decltype(Filter) F;
214 llvm::SmallVector<const NamedDecl *, 1> Decls;
215 bool VisitNamedDecl(const NamedDecl *ND) {
216 if (F(*ND))
217 Decls.push_back(ND);
218 return true;
219 }
220 } Visitor;
221 Visitor.F = Filter;
222 Visitor.TraverseDecl(AST.getASTContext().getTranslationUnitDecl());
223 if (Visitor.Decls.size() != 1) {
224 ADD_FAILURE() << Visitor.Decls.size() << " symbols matched.";
225 assert(Visitor.Decls.size() == 1);
226 }
227 return *Visitor.Decls.front();
228 }
229
findUnqualifiedDecl(ParsedAST & AST,llvm::StringRef Name)230 const NamedDecl &findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name) {
231 return findDecl(AST, [Name](const NamedDecl &ND) {
232 if (auto *ID = ND.getIdentifier())
233 if (ID->getName() == Name)
234 return true;
235 return false;
236 });
237 }
238
239 } // namespace clangd
240 } // namespace clang
241