clang-tools 10.0.0

TestTU.cpp
Go to the documentation of this file.
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
22 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
81 SymbolSlab TestTU::headerSymbols() const {
82 auto AST = build();
83 return std::get<0>(indexHeaderSymbols(AST.getASTContext(),
84 AST.getPreprocessorPtr(),
85 AST.getCanonicalIncludes()));
86 }
87
88 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
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
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
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
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
clang::tidy::ClangTidyOptions::Checks
llvm::Optional< std::string > Checks
Checks filter.
Definition: ClangTidyOptions.h:64
clang::clangd::TestTU::build
ParsedAST build() const
Definition: TestTU.cpp:22
clang::clangd::SymbolSlab
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:177
clang::clangd::TestTU::ClangTidyChecks
llvm::Optional< std::string > ClangTidyChecks
Definition: TestTU.h:59
clang::clangd::StoreDiags
StoreDiags collects the diagnostics that can later be reported by clangd.
Definition: Diagnostics.h:119
clang::clangd::TestTU::HeaderCode
std::string HeaderCode
Definition: TestTU.h:50
clang::clangd::buildAST
llvm::Optional< ParsedAST > buildAST(PathRef FileName, std::unique_ptr< CompilerInvocation > Invocation, llvm::ArrayRef< Diag > CompilerInvocationDiags, const ParseInputs &Inputs, std::shared_ptr< const PreambleData > Preamble)
Build an AST from provided user inputs.
Definition: ParsedAST.cpp:505
clang::clangd::TestTU::Code
std::string Code
Definition: TestTU.h:46
clang::clangd::ParseOptions::SuggestMissingIncludes
bool SuggestMissingIncludes
Definition: Compiler.h:40
MemIndex.h
clang::clangd::ParseOptions::ClangTidyOpts
tidy::ClangTidyOptions ClangTidyOpts
Definition: Compiler.h:39
TestFS.h
clang::clangd::findUnqualifiedDecl
const NamedDecl & findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name)
Definition: TestTU.cpp:159
TestTU.h
clang::clangd::ParseInputs::Contents
std::string Contents
Definition: Compiler.h:47
clang::clangd::ParsedAST::getASTContext
ASTContext & getASTContext()
Note that the returned ast will not contain decls from the preamble that were not deserialized during...
Definition: ParsedAST.cpp:424
clang::clangd::buildPreamble
std::shared_ptr< const PreambleData > buildPreamble(PathRef FileName, CompilerInvocation &CI, std::shared_ptr< const PreambleData > OldPreamble, const tooling::CompileCommand &OldCompileCommand, const ParseInputs &Inputs, bool StoreInMemory, PreambleParsedCallback PreambleCallback)
Build a preamble for the new inputs unless an old one can be reused.
Definition: Preamble.cpp:89
clang::clangd::TestTU::ExtraArgs
std::vector< const char * > ExtraArgs
Definition: TestTU.h:57
Ctx
Context Ctx
Definition: TUScheduler.cpp:237
clang::clangd::buildTestFS
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > buildTestFS(llvm::StringMap< std::string > const &Files, llvm::StringMap< time_t > const &Timestamps)
Definition: TestFS.cpp:22
clang::clangd::TestTU::ExternalIndex
const SymbolIndex * ExternalIndex
Definition: TestTU.h:62
QName
std::string QName
Definition: DiagnosticsTests.cpp:523
clang::clangd::SymbolOrigin::AST
clang::clangd::TestTU::ImplicitHeaderGuard
bool ImplicitHeaderGuard
Definition: TestTU.h:65
clang::clangd::ParseOptions
Definition: Compiler.h:38
clang::clangd::testPath
std::string testPath(PathRef File)
Definition: TestFS.cpp:82
Compiler.h
clang::clangd::TestTU::ClangTidyWarningsAsErrors
llvm::Optional< std::string > ClangTidyWarningsAsErrors
Definition: TestTU.h:60
Diagnostics.h
clang::clangd::ParseInputs::Opts
ParseOptions Opts
Definition: Compiler.h:50
clang::clangd::ParseInputs::CompileCommand
tooling::CompileCommand CompileCommand
Definition: Compiler.h:45
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::clangd::testRoot
const char * testRoot()
Definition: TestFS.cpp:74
clang::tidy::ClangTidyOptions::WarningsAsErrors
llvm::Optional< std::string > WarningsAsErrors
WarningsAsErrors filter.
Definition: ClangTidyOptions.h:67
clang::clangd::ParsedAST
Stores and provides access to parsed AST.
Definition: ParsedAST.h:46
clang::clangd::ParseInputs::Index
const SymbolIndex * Index
Definition: Compiler.h:49
clang::clangd::ParseInputs
Information required to run clang, e.g. to parse AST or do code completion.
Definition: Compiler.h:44
clang::clangd::TestTU::headerSymbols
SymbolSlab headerSymbols() const
Definition: TestTU.cpp:81
clang::clangd::Symbol
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
Preamble
const PreambleData * Preamble
Definition: CodeComplete.cpp:1020
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::findSymbol
const Symbol & findSymbol(const SymbolSlab &Slab, llvm::StringRef QName)
Definition: TestTU.cpp:97
clang::clangd::buildCompilerInvocation
std::unique_ptr< CompilerInvocation > buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D, std::vector< std::string > *CC1Args)
Builds compiler invocation that could be used to build AST or preamble.
Definition: Compiler.cpp:44
clang::clangd::TestTU::Filename
std::string Filename
Definition: TestTU.h:47
clang::clangd::TestTU::index
std::unique_ptr< SymbolIndex > index() const
Definition: TestTU.cpp:88
clang::clangd::ParseInputs::FS
IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS
Definition: Compiler.h:46
FileIndex.h
clang::clangd::TestTU::HeaderFilename
std::string HeaderFilename
Definition: TestTU.h:51
Files
llvm::StringMap< std::string > Files
Definition: TUSchedulerTests.cpp:119
clang::clangd::TestTU::AdditionalFiles
llvm::StringMap< std::string > AdditionalFiles
Definition: TestTU.h:54
clang::clangd::StoreDiags::take
std::vector< Diag > take(const clang::tidy::ClangTidyContext *Tidy=nullptr)
Definition: Diagnostics.cpp:417
clang::clangd::indexHeaderSymbols
SlabTuple indexHeaderSymbols(ASTContext &AST, std::shared_ptr< Preprocessor > PP, const CanonicalIncludes &Includes)
Index declarations from AST and macros from PP that are declared in included headers.
Definition: FileIndex.cpp:93
clang::clangd::findDecl
const NamedDecl & findDecl(ParsedAST &AST, llvm::StringRef QName)
Definition: TestTU.cpp:118