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/Frontend/CompilerInvocation.h"
17 #include "clang/Frontend/Utils.h"
18
19 namespace clang {
20 namespace clangd {
21
build() const22 ParsedAST TestTU::build() const {
23 std::string FullFilename = testPath(Filename),
24 FullHeaderName = testPath(HeaderFilename),
25 ImportThunk = testPath("import_thunk.h");
26 // We want to implicitly include HeaderFilename without messing up offsets.
27 // -include achieves this, but sometimes we want #import (to simulate a header
28 // guard without messing up offsets). In this case, use an intermediate file.
29 std::string ThunkContents = "#import \"" + FullHeaderName + "\"\n";
30
31 llvm::StringMap<std::string> Files(AdditionalFiles);
32 Files[FullFilename] = Code;
33 Files[FullHeaderName] = HeaderCode;
34 Files[ImportThunk] = ThunkContents;
35
36 std::vector<const char *> Cmd = {"clang"};
37 // FIXME: this shouldn't need to be conditional, but it breaks a
38 // GoToDefinition test for some reason (getMacroArgExpandedLocation fails).
39 if (!HeaderCode.empty()) {
40 Cmd.push_back("-include");
41 Cmd.push_back(ImplicitHeaderGuard ? ImportThunk.c_str()
42 : FullHeaderName.c_str());
43 // ms-compatibility changes the meaning of #import.
44 // The default is OS-dependent (on on windows), ensure it's off.
45 if (ImplicitHeaderGuard)
46 Cmd.push_back("-fno-ms-compatibility");
47 }
48 Cmd.insert(Cmd.end(), ExtraArgs.begin(), ExtraArgs.end());
49 // Put the file name at the end -- this allows the extra arg (-xc++) to
50 // override the language setting.
51 Cmd.push_back(FullFilename.c_str());
52 ParseInputs Inputs;
53 Inputs.CompileCommand.Filename = FullFilename;
54 Inputs.CompileCommand.CommandLine = {Cmd.begin(), Cmd.end()};
55 Inputs.CompileCommand.Directory = testRoot();
56 Inputs.Contents = Code;
57 Inputs.FS = buildTestFS(Files);
58 Inputs.Opts = ParseOptions();
59 Inputs.Opts.ClangTidyOpts.Checks = ClangTidyChecks;
60 Inputs.Opts.ClangTidyOpts.WarningsAsErrors = ClangTidyWarningsAsErrors;
61 Inputs.Index = ExternalIndex;
62 if (Inputs.Index)
63 Inputs.Opts.SuggestMissingIncludes = true;
64 StoreDiags Diags;
65 auto CI = buildCompilerInvocation(Inputs, Diags);
66 assert(CI && "Failed to build compilation invocation.");
67 auto Preamble =
68 buildPreamble(FullFilename, *CI,
69 /*OldPreamble=*/nullptr,
70 /*OldCompileCommand=*/Inputs.CompileCommand, Inputs,
71 /*StoreInMemory=*/true, /*PreambleCallback=*/nullptr);
72 auto AST =
73 buildAST(FullFilename, std::move(CI), Diags.take(), Inputs, Preamble);
74 if (!AST.hasValue()) {
75 ADD_FAILURE() << "Failed to build code:\n" << Code;
76 llvm_unreachable("Failed to build TestTU!");
77 }
78 return std::move(*AST);
79 }
80
headerSymbols() const81 SymbolSlab TestTU::headerSymbols() const {
82 auto AST = build();
83 return std::get<0>(indexHeaderSymbols(AST.getASTContext(),
84 AST.getPreprocessorPtr(),
85 AST.getCanonicalIncludes()));
86 }
87
index() const88 std::unique_ptr<SymbolIndex> TestTU::index() const {
89 auto AST = build();
90 auto Idx = std::make_unique<FileIndex>(/*UseDex=*/true);
91 Idx->updatePreamble(Filename, AST.getASTContext(), AST.getPreprocessorPtr(),
92 AST.getCanonicalIncludes());
93 Idx->updateMain(Filename, AST);
94 return std::move(Idx);
95 }
96
findSymbol(const SymbolSlab & Slab,llvm::StringRef QName)97 const Symbol &findSymbol(const SymbolSlab &Slab, llvm::StringRef QName) {
98 const Symbol *Result = nullptr;
99 for (const Symbol &S : Slab) {
100 if (QName != (S.Scope + S.Name).str())
101 continue;
102 if (Result) {
103 ADD_FAILURE() << "Multiple symbols named " << QName << ":\n"
104 << *Result << "\n---\n"
105 << S;
106 assert(false && "QName is not unique");
107 }
108 Result = &S;
109 }
110 if (!Result) {
111 ADD_FAILURE() << "No symbol named " << QName << " in "
112 << ::testing::PrintToString(Slab);
113 assert(false && "No symbol with QName");
114 }
115 return *Result;
116 }
117
findDecl(ParsedAST & AST,llvm::StringRef QName)118 const NamedDecl &findDecl(ParsedAST &AST, llvm::StringRef QName) {
119 llvm::SmallVector<llvm::StringRef, 4> Components;
120 QName.split(Components, "::");
121
122 auto &Ctx = AST.getASTContext();
123 auto LookupDecl = [&Ctx](const DeclContext &Scope,
124 llvm::StringRef Name) -> const NamedDecl & {
125 auto LookupRes = Scope.lookup(DeclarationName(&Ctx.Idents.get(Name)));
126 assert(!LookupRes.empty() && "Lookup failed");
127 assert(LookupRes.size() == 1 && "Lookup returned multiple results");
128 return *LookupRes.front();
129 };
130
131 const DeclContext *Scope = Ctx.getTranslationUnitDecl();
132 for (auto NameIt = Components.begin(), End = Components.end() - 1;
133 NameIt != End; ++NameIt) {
134 Scope = &cast<DeclContext>(LookupDecl(*Scope, *NameIt));
135 }
136 return LookupDecl(*Scope, Components.back());
137 }
138
findDecl(ParsedAST & AST,std::function<bool (const NamedDecl &)> Filter)139 const NamedDecl &findDecl(ParsedAST &AST,
140 std::function<bool(const NamedDecl &)> Filter) {
141 struct Visitor : RecursiveASTVisitor<Visitor> {
142 decltype(Filter) F;
143 llvm::SmallVector<const NamedDecl *, 1> Decls;
144 bool VisitNamedDecl(const NamedDecl *ND) {
145 if (F(*ND))
146 Decls.push_back(ND);
147 return true;
148 }
149 } Visitor;
150 Visitor.F = Filter;
151 Visitor.TraverseDecl(AST.getASTContext().getTranslationUnitDecl());
152 if (Visitor.Decls.size() != 1) {
153 ADD_FAILURE() << Visitor.Decls.size() << " symbols matched.";
154 assert(Visitor.Decls.size() == 1);
155 }
156 return *Visitor.Decls.front();
157 }
158
findUnqualifiedDecl(ParsedAST & AST,llvm::StringRef Name)159 const NamedDecl &findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name) {
160 return findDecl(AST, [Name](const NamedDecl &ND) {
161 if (auto *ID = ND.getIdentifier())
162 if (ID->getName() == Name)
163 return true;
164 return false;
165 });
166 }
167
168 } // namespace clangd
169 } // namespace clang
170