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