clang-tools  12.0.0git
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/Basic/Diagnostic.h"
17 #include "clang/Frontend/CompilerInvocation.h"
18 #include "clang/Frontend/Utils.h"
19 
20 namespace clang {
21 namespace clangd {
22 
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 
33  FS.Files[FullFilename] = Code;
34  FS.Files[FullHeaderName] = HeaderCode;
35  FS.Files[ImportThunk] = ThunkContents;
36 
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.
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;
63  Inputs.Index = ExternalIndex;
64  if (Inputs.Index)
65  Inputs.Opts.SuggestMissingIncludes = true;
66  return Inputs;
67 }
68 
69 std::shared_ptr<const PreambleData> TestTU::preamble() const {
70  MockFS FS;
71  auto Inputs = inputs(FS);
73  auto CI = buildCompilerInvocation(Inputs, Diags);
74  assert(CI && "Failed to build compilation invocation.");
76  /*StoreInMemory=*/true,
77  /*PreambleCallback=*/nullptr);
78 }
79 
81  MockFS FS;
82  auto Inputs = inputs(FS);
84  auto CI = buildCompilerInvocation(Inputs, Diags);
85  assert(CI && "Failed to build compilation invocation.");
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 
123  auto AST = build();
124  return std::get<0>(indexHeaderSymbols(/*Version=*/"null", AST.getASTContext(),
125  AST.getPreprocessorPtr(),
126  AST.getCanonicalIncludes()));
127 }
128 
130  auto AST = build();
131  return std::get<1>(indexMainDecls(AST));
132 }
133 
134 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 
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 
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 
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 
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
llvm::Optional< std::string > Checks
Checks filter.
ParsedAST build() const
Definition: TestTU.cpp:80
SlabTuple indexHeaderSymbols(llvm::StringRef Version, 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:215
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:177
llvm::Optional< std::string > ClangTidyChecks
Definition: TestTU.h:61
StoreDiags collects the diagnostics that can later be reported by clangd.
Definition: Diagnostics.h:120
const ThreadsafeFS * TFS
Definition: Compiler.h:49
std::string HeaderCode
Definition: TestTU.h:52
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:104
std::shared_ptr< const PreambleData > preamble() const
Definition: TestTU.cpp:69
ParseInputs Inputs
MockFS FS
std::string Code
Definition: TestTU.h:48
SlabTuple indexMainDecls(ParsedAST &AST)
Retrieves symbols and refs of local top level decls in AST (i.e.
Definition: FileIndex.cpp:208
tidy::ClangTidyOptions ClangTidyOpts
Definition: Compiler.h:40
const NamedDecl & findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name)
Definition: TestTU.cpp:206
ASTContext & getASTContext()
Note that the returned ast will not contain decls from the preamble that were not deserialized during...
Definition: ParsedAST.cpp:471
std::unique_ptr< CompilerInvocation > CI
const PreambleData & Preamble
std::shared_ptr< const PreambleData > buildPreamble(PathRef FileName, CompilerInvocation CI, const ParseInputs &Inputs, bool StoreInMemory, PreambleParsedCallback PreambleCallback)
Build a preamble for the new inputs unless an old one can be reused.
Definition: Preamble.cpp:321
Context Ctx
const SymbolIndex * ExternalIndex
Definition: TestTU.h:64
std::string QName
bool ImplicitHeaderGuard
Definition: TestTU.h:67
llvm::Optional< std::string > ClangTidyWarningsAsErrors
Definition: TestTU.h:62
std::string testPath(PathRef File, llvm::sys::path::Style Style)
Definition: TestFS.cpp:82
tooling::CompileCommand CompileCommand
Definition: Compiler.h:48
CapturedDiags Diags
static constexpr llvm::StringLiteral Name
const char * testRoot()
Definition: TestFS.cpp:74
llvm::Optional< std::string > WarningsAsErrors
WarningsAsErrors filter.
Stores and provides access to parsed AST.
Definition: ParsedAST.h:48
const SymbolIndex * Index
Definition: Compiler.h:57
Information required to run clang, e.g. to parse AST or do code completion.
Definition: Compiler.h:47
SymbolSlab headerSymbols() const
Definition: TestTU.cpp:122
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
llvm::StringMap< std::string > Files
Definition: TestFS.h:41
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
RefSlab headerRefs() const
Definition: TestTU.cpp:129
const Symbol & findSymbol(const SymbolSlab &Slab, llvm::StringRef QName)
Definition: TestTU.cpp:144
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:45
std::string Filename
Definition: TestTU.h:49
std::unique_ptr< SymbolIndex > index() const
Definition: TestTU.cpp:134
static llvm::Optional< ParsedAST > build(llvm::StringRef Filename, const ParseInputs &Inputs, std::unique_ptr< clang::CompilerInvocation > CI, llvm::ArrayRef< Diag > CompilerInvocationDiags, std::shared_ptr< const PreambleData > Preamble)
Attempts to run Clang and store the parsed AST.
Definition: ParsedAST.cpp:246
std::string HeaderFilename
Definition: TestTU.h:53
llvm::StringMap< std::string > AdditionalFiles
Definition: TestTU.h:56
std::vector< Diag > take(const clang::tidy::ClangTidyContext *Tidy=nullptr)
ParseInputs inputs(MockFS &FS) const
Definition: TestTU.cpp:23
std::vector< std::string > ExtraArgs
Definition: TestTU.h:59
const NamedDecl & findDecl(ParsedAST &AST, llvm::StringRef QName)
Definition: TestTU.cpp:165