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