clang-tools  10.0.0svn
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 
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.
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();
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 
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
llvm::Optional< std::string > Checks
Checks filter.
ParsedAST build() const
Definition: TestTU.cpp:22
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:177
llvm::Optional< std::string > ClangTidyChecks
Definition: TestTU.h:59
StoreDiags collects the diagnostics that can later be reported by clangd.
Definition: Diagnostics.h:119
std::string HeaderCode
Definition: TestTU.h:50
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
std::string Code
Definition: TestTU.h:46
tidy::ClangTidyOptions ClangTidyOpts
Definition: Compiler.h:39
const NamedDecl & findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name)
Definition: TestTU.cpp:159
ASTContext & getASTContext()
Note that the returned ast will not contain decls from the preamble that were not deserialized during...
Definition: ParsedAST.cpp:424
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
std::vector< const char * > ExtraArgs
Definition: TestTU.h:57
Context Ctx
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > buildTestFS(llvm::StringMap< std::string > const &Files, llvm::StringMap< time_t > const &Timestamps)
Definition: TestFS.cpp:22
const SymbolIndex * ExternalIndex
Definition: TestTU.h:62
std::string QName
bool ImplicitHeaderGuard
Definition: TestTU.h:65
std::string testPath(PathRef File)
Definition: TestFS.cpp:82
llvm::Optional< std::string > ClangTidyWarningsAsErrors
Definition: TestTU.h:60
tooling::CompileCommand CompileCommand
Definition: Compiler.h:45
static constexpr llvm::StringLiteral Name
const char * testRoot()
Definition: TestFS.cpp:74
std::unique_ptr< CompilerInvocation > buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D)
Builds compiler invocation that could be used to build AST or preamble.
Definition: Compiler.cpp:44
llvm::Optional< std::string > WarningsAsErrors
WarningsAsErrors filter.
Stores and provides access to parsed AST.
Definition: ParsedAST.h:46
const SymbolIndex * Index
Definition: Compiler.h:49
llvm::SmallDenseMap< const Decl *, RelSet > Decls
Definition: FindTarget.cpp:88
Information required to run clang, e.g. to parse AST or do code completion.
Definition: Compiler.h:44
SymbolSlab headerSymbols() const
Definition: TestTU.cpp:81
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
const PreambleData * Preamble
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
const Symbol & findSymbol(const SymbolSlab &Slab, llvm::StringRef QName)
Definition: TestTU.cpp:97
std::string Filename
Definition: TestTU.h:47
std::unique_ptr< SymbolIndex > index() const
Definition: TestTU.cpp:88
IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS
Definition: Compiler.h:46
std::string HeaderFilename
Definition: TestTU.h:51
llvm::StringMap< std::string > Files
llvm::StringMap< std::string > AdditionalFiles
Definition: TestTU.h:54
std::vector< Diag > take(const clang::tidy::ClangTidyContext *Tidy=nullptr)
SlabTuple indexHeaderSymbols(ASTContext &AST, std::shared_ptr< Preprocessor > PP, const CanonicalIncludes &Includes)
Idex declarations from AST and macros from PP that are declared in included headers.
Definition: FileIndex.cpp:86
const NamedDecl & findDecl(ParsedAST &AST, llvm::StringRef QName)
Definition: TestTU.cpp:118