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/MemIndex.h"
16 #include "clang/AST/RecursiveASTVisitor.h"
17 #include "clang/Basic/Diagnostic.h"
18 #include "clang/Frontend/CompilerInvocation.h"
19 #include "clang/Frontend/Utils.h"
20 #include "llvm/ADT/ScopeExit.h"
21 #include "llvm/Support/ScopedPrinter.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include <cstdlib>
24 
25 namespace clang {
26 namespace clangd {
27 
inputs(MockFS & FS) const28 ParseInputs TestTU::inputs(MockFS &FS) const {
29   std::string FullFilename = testPath(Filename),
30               FullHeaderName = testPath(HeaderFilename),
31               ImportThunk = testPath("import_thunk.h");
32   // We want to implicitly include HeaderFilename without messing up offsets.
33   // -include achieves this, but sometimes we want #import (to simulate a header
34   // guard without messing up offsets). In this case, use an intermediate file.
35   std::string ThunkContents = "#import \"" + FullHeaderName + "\"\n";
36 
37   FS.Files = AdditionalFiles;
38   FS.Files[FullFilename] = Code;
39   FS.Files[FullHeaderName] = HeaderCode;
40   FS.Files[ImportThunk] = ThunkContents;
41 
42   ParseInputs Inputs;
43   Inputs.FeatureModules = FeatureModules;
44   auto &Argv = Inputs.CompileCommand.CommandLine;
45   Argv = {"clang"};
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   StoreDiags Diags;
113   auto CI = buildCompilerInvocation(Inputs, Diags);
114   assert(CI && "Failed to build compilation invocation.");
115   if (OverlayRealFileSystemForModules)
116     initializeModuleCache(*CI);
117   auto ModuleCacheDeleter = llvm::make_scope_exit(
118       std::bind(deleteModuleCache, CI->getHeaderSearchOpts().ModuleCachePath));
119 
120   auto Preamble = clang::clangd::buildPreamble(testPath(Filename), *CI, Inputs,
121                                                /*StoreInMemory=*/true,
122                                                /*PreambleCallback=*/nullptr);
123   auto AST = ParsedAST::build(testPath(Filename), Inputs, std::move(CI),
124                               Diags.take(), Preamble);
125   if (!AST.hasValue()) {
126     llvm::errs() << "Failed to build code:\n" << Code;
127     std::abort();
128   }
129   assert(AST->getDiagnostics() &&
130          "TestTU should always build an AST with a fresh Preamble");
131   // Check for error diagnostics and report gtest failures (unless expected).
132   // This guards against accidental syntax errors silently subverting tests.
133   // error-ok is awfully primitive - using clang -verify would be nicer.
134   // Ownership and layering makes it pretty hard.
135   bool ErrorOk = [&, this] {
136     llvm::StringLiteral Marker = "error-ok";
137     if (llvm::StringRef(Code).contains(Marker) ||
138         llvm::StringRef(HeaderCode).contains(Marker))
139       return true;
140     for (const auto &KV : this->AdditionalFiles)
141       if (llvm::StringRef(KV.second).contains(Marker))
142         return true;
143     return false;
144   }();
145   if (!ErrorOk) {
146     // We always build AST with a fresh preamble in TestTU.
147     for (const auto &D : *AST->getDiagnostics())
148       if (D.Severity >= DiagnosticsEngine::Error) {
149         llvm::errs()
150             << "TestTU failed to build (suppress with /*error-ok*/): \n"
151             << D << "\n\nFor code:\n"
152             << Code;
153         std::abort(); // Stop after first error for simplicity.
154       }
155   }
156   return std::move(*AST);
157 }
158 
headerSymbols() const159 SymbolSlab TestTU::headerSymbols() const {
160   auto AST = build();
161   return std::get<0>(indexHeaderSymbols(/*Version=*/"null", AST.getASTContext(),
162                                         AST.getPreprocessorPtr(),
163                                         AST.getCanonicalIncludes()));
164 }
165 
headerRefs() const166 RefSlab TestTU::headerRefs() const {
167   auto AST = build();
168   return std::get<1>(indexMainDecls(AST));
169 }
170 
index() const171 std::unique_ptr<SymbolIndex> TestTU::index() const {
172   auto AST = build();
173   auto Idx = std::make_unique<FileIndex>();
174   Idx->updatePreamble(testPath(Filename), /*Version=*/"null",
175                       AST.getASTContext(), AST.getPreprocessorPtr(),
176                       AST.getCanonicalIncludes());
177   Idx->updateMain(testPath(Filename), AST);
178   return std::move(Idx);
179 }
180 
findSymbol(const SymbolSlab & Slab,llvm::StringRef QName)181 const Symbol &findSymbol(const SymbolSlab &Slab, llvm::StringRef QName) {
182   const Symbol *Result = nullptr;
183   for (const Symbol &S : Slab) {
184     if (QName != (S.Scope + S.Name).str())
185       continue;
186     if (Result) {
187       llvm::errs() << "Multiple symbols named " << QName << ":\n"
188                    << *Result << "\n---\n"
189                    << S;
190       assert(false && "QName is not unique");
191     }
192     Result = &S;
193   }
194   if (!Result) {
195     llvm::errs() << "No symbol named " << QName << " in "
196                  << llvm::to_string(Slab);
197     assert(false && "No symbol with QName");
198   }
199   return *Result;
200 }
201 
202 // RAII scoped class to disable TraversalScope for a ParsedAST.
203 class TraverseHeadersToo {
204   ASTContext &Ctx;
205   std::vector<Decl *> ScopeToRestore;
206 
207 public:
TraverseHeadersToo(ParsedAST & AST)208   TraverseHeadersToo(ParsedAST &AST)
209       : Ctx(AST.getASTContext()), ScopeToRestore(Ctx.getTraversalScope()) {
210     Ctx.setTraversalScope({Ctx.getTranslationUnitDecl()});
211   }
~TraverseHeadersToo()212   ~TraverseHeadersToo() { Ctx.setTraversalScope(std::move(ScopeToRestore)); }
213 };
214 
findDecl(ParsedAST & AST,llvm::StringRef QName)215 const NamedDecl &findDecl(ParsedAST &AST, llvm::StringRef QName) {
216   auto &Ctx = AST.getASTContext();
217   auto LookupDecl = [&Ctx](const DeclContext &Scope,
218                            llvm::StringRef Name) -> const NamedDecl & {
219     auto LookupRes = Scope.lookup(DeclarationName(&Ctx.Idents.get(Name)));
220     assert(!LookupRes.empty() && "Lookup failed");
221     assert(LookupRes.isSingleResult() && "Lookup returned multiple results");
222     return *LookupRes.front();
223   };
224 
225   const DeclContext *Scope = Ctx.getTranslationUnitDecl();
226 
227   StringRef Cur, Rest;
228   for (std::tie(Cur, Rest) = QName.split("::"); !Rest.empty();
229        std::tie(Cur, Rest) = Rest.split("::")) {
230     Scope = &cast<DeclContext>(LookupDecl(*Scope, Cur));
231   }
232   return LookupDecl(*Scope, Cur);
233 }
234 
findDecl(ParsedAST & AST,std::function<bool (const NamedDecl &)> Filter)235 const NamedDecl &findDecl(ParsedAST &AST,
236                           std::function<bool(const NamedDecl &)> Filter) {
237   TraverseHeadersToo Too(AST);
238   struct Visitor : RecursiveASTVisitor<Visitor> {
239     decltype(Filter) F;
240     llvm::SmallVector<const NamedDecl *, 1> Decls;
241     bool VisitNamedDecl(const NamedDecl *ND) {
242       if (F(*ND))
243         Decls.push_back(ND);
244       return true;
245     }
246   } Visitor;
247   Visitor.F = Filter;
248   Visitor.TraverseDecl(AST.getASTContext().getTranslationUnitDecl());
249   if (Visitor.Decls.size() != 1) {
250     llvm::errs() << Visitor.Decls.size() << " symbols matched.";
251     assert(Visitor.Decls.size() == 1);
252   }
253   return *Visitor.Decls.front();
254 }
255 
findUnqualifiedDecl(ParsedAST & AST,llvm::StringRef Name)256 const NamedDecl &findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name) {
257   return findDecl(AST, [Name](const NamedDecl &ND) {
258     if (auto *ID = ND.getIdentifier())
259       if (ID->getName() == Name)
260         return true;
261     return false;
262   });
263 }
264 
265 } // namespace clangd
266 } // namespace clang
267