clang-tools  17.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 "CompileCommands.h"
11 #include "Compiler.h"
12 #include "Diagnostics.h"
13 #include "TestFS.h"
14 #include "index/FileIndex.h"
15 #include "clang/AST/RecursiveASTVisitor.h"
16 #include "clang/Basic/Diagnostic.h"
17 #include "clang/Frontend/CompilerInvocation.h"
18 #include "llvm/ADT/ScopeExit.h"
19 #include "llvm/Support/ScopedPrinter.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include <cstdlib>
22 
23 namespace clang {
24 namespace clangd {
25 
27  std::string FullFilename = testPath(Filename),
28  FullHeaderName = testPath(HeaderFilename),
29  ImportThunk = testPath("import_thunk.h");
30  // We want to implicitly include HeaderFilename without messing up offsets.
31  // -include achieves this, but sometimes we want #import (to simulate a header
32  // guard without messing up offsets). In this case, use an intermediate file.
33  std::string ThunkContents = "#import \"" + FullHeaderName + "\"\n";
34 
35  FS.Files = AdditionalFiles;
36  FS.Files[FullFilename] = Code;
37  FS.Files[FullHeaderName] = HeaderCode;
38  FS.Files[ImportThunk] = ThunkContents;
39 
41  Inputs.FeatureModules = FeatureModules;
42  auto &Argv = Inputs.CompileCommand.CommandLine;
43  Argv = {"clang"};
44  // In tests, unless explicitly specified otherwise, omit predefined macros
45  // (__GNUC__ etc) for a 25% speedup. There are hundreds, and we'd generate,
46  // parse, serialize, and re-parse them!
47  if (!PredefineMacros) {
48  Argv.push_back("-Xclang");
49  Argv.push_back("-undef");
50  }
51  // FIXME: this shouldn't need to be conditional, but it breaks a
52  // GoToDefinition test for some reason (getMacroArgExpandedLocation fails).
53  if (!HeaderCode.empty()) {
54  Argv.push_back("-include");
55  Argv.push_back(ImplicitHeaderGuard ? ImportThunk : FullHeaderName);
56  // ms-compatibility changes the meaning of #import.
57  // The default is OS-dependent (on windows), ensure it's off.
59  Inputs.CompileCommand.CommandLine.push_back("-fno-ms-compatibility");
60  }
61  Argv.insert(Argv.end(), ExtraArgs.begin(), ExtraArgs.end());
62  // Put the file name at the end -- this allows the extra arg (-xc++) to
63  // override the language setting.
64  Argv.push_back(FullFilename);
65 
66  auto Mangler = CommandMangler::forTests();
67  Mangler(Inputs.CompileCommand, FullFilename);
68  Inputs.CompileCommand.Filename = FullFilename;
69  Inputs.CompileCommand.Directory = testRoot();
70  Inputs.Contents = Code;
72  FS.OverlayRealFileSystemForModules = true;
73  Inputs.TFS = &FS;
74  Inputs.Opts = ParseOptions();
76  Inputs.ClangTidyProvider = ClangTidyProvider;
77  Inputs.Index = ExternalIndex;
78  return Inputs;
79 }
80 
81 void initializeModuleCache(CompilerInvocation &CI) {
82  llvm::SmallString<128> ModuleCachePath;
83  if (llvm::sys::fs::createUniqueDirectory("module-cache", ModuleCachePath)) {
84  llvm::errs() << "Failed to create temp directory for module-cache";
85  std::abort();
86  }
87  CI.getHeaderSearchOpts().ModuleCachePath = ModuleCachePath.c_str();
88 }
89 
90 void deleteModuleCache(const std::string ModuleCachePath) {
91  if (!ModuleCachePath.empty()) {
92  if (llvm::sys::fs::remove_directories(ModuleCachePath)) {
93  llvm::errs() << "Failed to delete temp directory for module-cache";
94  std::abort();
95  }
96  }
97 }
98 
99 std::shared_ptr<const PreambleData>
100 TestTU::preamble(PreambleParsedCallback PreambleCallback) const {
101  MockFS FS;
102  auto Inputs = inputs(FS);
105  assert(CI && "Failed to build compilation invocation.");
108  auto ModuleCacheDeleter = llvm::make_scope_exit(
109  std::bind(deleteModuleCache, CI->getHeaderSearchOpts().ModuleCachePath));
111  /*StoreInMemory=*/true, PreambleCallback);
112 }
113 
115  MockFS FS;
116  auto Inputs = inputs(FS);
117  Inputs.Opts = ParseOpts;
120  assert(CI && "Failed to build compilation invocation.");
123  auto ModuleCacheDeleter = llvm::make_scope_exit(
124  std::bind(deleteModuleCache, CI->getHeaderSearchOpts().ModuleCachePath));
125 
127  /*StoreInMemory=*/true,
128  /*PreambleCallback=*/nullptr);
129  auto AST = ParsedAST::build(testPath(Filename), Inputs, std::move(CI),
130  Diags.take(), Preamble);
131  if (!AST) {
132  llvm::errs() << "Failed to build code:\n" << Code;
133  std::abort();
134  }
135  assert(AST->getDiagnostics() &&
136  "TestTU should always build an AST with a fresh Preamble");
137  // Check for error diagnostics and report gtest failures (unless expected).
138  // This guards against accidental syntax errors silently subverting tests.
139  // error-ok is awfully primitive - using clang -verify would be nicer.
140  // Ownership and layering makes it pretty hard.
141  bool ErrorOk = [&, this] {
142  llvm::StringLiteral Marker = "error-ok";
143  if (llvm::StringRef(Code).contains(Marker) ||
144  llvm::StringRef(HeaderCode).contains(Marker))
145  return true;
146  for (const auto &KV : this->AdditionalFiles)
147  if (llvm::StringRef(KV.second).contains(Marker))
148  return true;
149  return false;
150  }();
151  if (!ErrorOk) {
152  // We always build AST with a fresh preamble in TestTU.
153  for (const auto &D : *AST->getDiagnostics())
154  if (D.Severity >= DiagnosticsEngine::Error) {
155  llvm::errs()
156  << "TestTU failed to build (suppress with /*error-ok*/): \n"
157  << D << "\n\nFor code:\n"
158  << Code;
159  std::abort(); // Stop after first error for simplicity.
160  }
161  }
162  return std::move(*AST);
163 }
164 
166  auto AST = build();
167  return std::get<0>(indexHeaderSymbols(/*Version=*/"null", AST.getASTContext(),
168  AST.getPreprocessor(),
169  AST.getCanonicalIncludes()));
170 }
171 
173  auto AST = build();
174  return std::get<1>(indexMainDecls(AST));
175 }
176 
177 std::unique_ptr<SymbolIndex> TestTU::index() const {
178  auto AST = build();
179  auto Idx = std::make_unique<FileIndex>();
180  Idx->updatePreamble(testPath(Filename), /*Version=*/"null",
181  AST.getASTContext(), AST.getPreprocessor(),
182  AST.getCanonicalIncludes());
183  Idx->updateMain(testPath(Filename), AST);
184  return std::move(Idx);
185 }
186 
187 const Symbol &findSymbol(const SymbolSlab &Slab, llvm::StringRef QName) {
188  const Symbol *Result = nullptr;
189  for (const Symbol &S : Slab) {
190  if (QName != (S.Scope + S.Name).str())
191  continue;
192  if (Result) {
193  llvm::errs() << "Multiple symbols named " << QName << ":\n"
194  << *Result << "\n---\n"
195  << S;
196  assert(false && "QName is not unique");
197  }
198  Result = &S;
199  }
200  if (!Result) {
201  llvm::errs() << "No symbol named " << QName << " in "
202  << llvm::to_string(Slab);
203  assert(false && "No symbol with QName");
204  }
205  return *Result;
206 }
207 
208 // RAII scoped class to disable TraversalScope for a ParsedAST.
210  ASTContext &Ctx;
211  std::vector<Decl *> ScopeToRestore;
212 
213 public:
215  : Ctx(AST.getASTContext()), ScopeToRestore(Ctx.getTraversalScope()) {
216  Ctx.setTraversalScope({Ctx.getTranslationUnitDecl()});
217  }
218  ~TraverseHeadersToo() { Ctx.setTraversalScope(std::move(ScopeToRestore)); }
219 };
220 
221 const NamedDecl &findDecl(ParsedAST &AST, llvm::StringRef QName) {
222  auto &Ctx = AST.getASTContext();
223  auto LookupDecl = [&Ctx](const DeclContext &Scope,
224  llvm::StringRef Name) -> const NamedDecl & {
225  auto LookupRes = Scope.lookup(DeclarationName(&Ctx.Idents.get(Name)));
226  assert(!LookupRes.empty() && "Lookup failed");
227  assert(LookupRes.isSingleResult() && "Lookup returned multiple results");
228  return *LookupRes.front();
229  };
230 
231  const DeclContext *Scope = Ctx.getTranslationUnitDecl();
232 
233  StringRef Cur, Rest;
234  for (std::tie(Cur, Rest) = QName.split("::"); !Rest.empty();
235  std::tie(Cur, Rest) = Rest.split("::")) {
236  Scope = &cast<DeclContext>(LookupDecl(*Scope, Cur));
237  }
238  return LookupDecl(*Scope, Cur);
239 }
240 
241 const NamedDecl &findDecl(ParsedAST &AST,
242  std::function<bool(const NamedDecl &)> Filter) {
243  TraverseHeadersToo Too(AST);
244  struct Visitor : RecursiveASTVisitor<Visitor> {
245  decltype(Filter) F;
246  llvm::SmallVector<const NamedDecl *, 1> Decls;
247  bool VisitNamedDecl(const NamedDecl *ND) {
248  if (F(*ND))
249  Decls.push_back(ND);
250  return true;
251  }
252  } Visitor;
253  Visitor.F = Filter;
254  Visitor.TraverseDecl(AST.getASTContext().getTranslationUnitDecl());
255  if (Visitor.Decls.size() != 1) {
256  llvm::errs() << Visitor.Decls.size() << " symbols matched.\n";
257  assert(Visitor.Decls.size() == 1);
258  }
259  return *Visitor.Decls.front();
260 }
261 
262 const NamedDecl &findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name) {
263  return findDecl(AST, [Name](const NamedDecl &ND) {
264  if (auto *ID = ND.getIdentifier())
265  if (ID->getName() == Name)
266  return true;
267  return false;
268  });
269 }
270 
271 } // namespace clangd
272 } // namespace clang
clang::clangd::TestTU::ExtraArgs
std::vector< std::string > ExtraArgs
Definition: TestTU.h:60
clang::clangd::TestTU::HeaderFilename
std::string HeaderFilename
Definition: TestTU.h:54
clang::clangd::buildPreamble
std::shared_ptr< const PreambleData > buildPreamble(PathRef FileName, CompilerInvocation CI, const ParseInputs &Inputs, bool StoreInMemory, PreambleParsedCallback PreambleCallback, PreambleBuildStats *Stats)
Build a preamble for the new inputs unless an old one can be reused.
Definition: Preamble.cpp:473
clang::clangd::findDecl
const NamedDecl & findDecl(ParsedAST &AST, llvm::StringRef QName)
Definition: TestTU.cpp:221
clang::clangd::indexHeaderSymbols
SlabTuple indexHeaderSymbols(llvm::StringRef Version, ASTContext &AST, Preprocessor &PP, const CanonicalIncludes &Includes)
Index declarations from AST and macros from PP that are declared in included headers.
Definition: FileIndex.cpp:229
clang::clangd::TraverseHeadersToo::TraverseHeadersToo
TraverseHeadersToo(ParsedAST &AST)
Definition: TestTU.cpp:214
RecursiveASTVisitor
clang::clangd::deleteModuleCache
void deleteModuleCache(const std::string ModuleCachePath)
Definition: TestTU.cpp:90
clang::clangd::testPath
std::string testPath(PathRef File, llvm::sys::path::Style Style)
Definition: TestFS.cpp:93
clang::clangd::TestTU::build
ParsedAST build() const
Definition: TestTU.cpp:114
clang::clangd::TestTU::headerSymbols
SymbolSlab headerSymbols() const
Definition: TestTU.cpp:165
clang::clangd::initializeModuleCache
void initializeModuleCache(CompilerInvocation &CI)
Definition: TestTU.cpp:81
clang::clangd::TestTU::headerRefs
RefSlab headerRefs() const
Definition: TestTU.cpp:172
clang::clangd::Context::get
const Type * get(const Key< Type > &Key) const
Get data stored for a typed Key.
Definition: Context.h:98
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:716
CI
std::unique_ptr< CompilerInvocation > CI
Definition: TUScheduler.cpp:548
TestTU.h
Ctx
Context Ctx
Definition: TUScheduler.cpp:552
clang::clangd::RefSlab
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:108
clang::clangd::TestTU::preamble
std::shared_ptr< const PreambleData > preamble(PreambleParsedCallback PreambleCallback=nullptr) const
Definition: TestTU.cpp:100
clang::clangd::TraverseHeadersToo
Definition: TestTU.cpp:209
clang::clangd::TestTU::PredefineMacros
bool PredefineMacros
Definition: TestTU.h:63
clang::clangd::TestTU::Code
std::string Code
Definition: TestTU.h:49
clang::clangd::findUnqualifiedDecl
const NamedDecl & findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name)
Definition: TestTU.cpp:262
Preamble
const PreambleData & Preamble
Definition: CodeComplete.cpp:1241
clang::clangd::TestTU::ExternalIndex
const SymbolIndex * ExternalIndex
Definition: TestTU.h:67
clang::clangd::TestTU::inputs
ParseInputs inputs(MockFS &FS) const
Definition: TestTU.cpp:26
Error
constexpr static llvm::SourceMgr::DiagKind Error
Definition: ConfigCompile.cpp:592
Inputs
ParseInputs Inputs
Definition: TUScheduler.cpp:549
clang::clangd::TestTU::ClangTidyProvider
TidyProvider ClangTidyProvider
Definition: TestTU.h:65
clang::clangd::ParseInputs
Information required to run clang, e.g. to parse AST or do code completion.
Definition: Compiler.h:48
ns1::ns2::D
@ D
Definition: CategoricalFeature.h:3
clang::clangd::testRoot
const char * testRoot()
Definition: TestFS.cpp:85
clang::clangd::MockFS
Definition: TestFS.h:32
CompileCommands.h
clang::clangd::TestTU::Filename
std::string Filename
Definition: TestTU.h:50
TestFS.h
clang::clangd::Symbol
The class presents a C++ symbol, e.g.
Definition: Symbol.h:39
clang::clangd::TestTU::OverlayRealFileSystemForModules
bool OverlayRealFileSystemForModules
Definition: TestTU.h:83
Diagnostics.h
Name
Token Name
Definition: MacroToEnumCheck.cpp:87
FileIndex.h
clang::clangd::TraverseHeadersToo::~TraverseHeadersToo
~TraverseHeadersToo()
Definition: TestTU.cpp:218
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:95
clang::clangd::TestTU::ImplicitHeaderGuard
bool ImplicitHeaderGuard
Definition: TestTU.h:70
Compiler.h
ID
static char ID
Definition: Logger.cpp:74
clang::clangd::ParseOptions
Definition: Compiler.h:41
clang::clangd::TestTU::ParseOpts
ParseOptions ParseOpts
Definition: TestTU.h:73
clang::clangd::TestTU::index
std::unique_ptr< SymbolIndex > index() const
Definition: TestTU.cpp:177
if
if(CLANG_PLUGIN_SUPPORT) export_executable_symbols_for_plugins(clang-tidy) endif() install(PROGRAMS clang-tidy-diff.py DESTINATION "$
Definition: clang-tidy/tool/CMakeLists.txt:59
clang::clangd::TestTU::FeatureModules
FeatureModuleSet * FeatureModules
Definition: TestTU.h:85
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::StoreDiags
StoreDiags collects the diagnostics that can later be reported by clangd.
Definition: Diagnostics.h:137
Diags
CapturedDiags Diags
Definition: ConfigCompileTests.cpp:38
clang::clangd::ParsedAST
Stores and provides access to parsed AST.
Definition: ParsedAST.h:47
clang::clangd::CommandMangler::forTests
static CommandMangler forTests()
Definition: CompileCommands.cpp:196
clang::clangd::SymbolSlab
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:199
clang::clangd::PreambleParsedCallback
std::function< void(ASTContext &, Preprocessor &, const CanonicalIncludes &)> PreambleParsedCallback
Definition: Preamble.h:79
clang::clangd::ParsedAST::build
static std::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:345
clang::clangd::TestTU::AdditionalFiles
llvm::StringMap< std::string > AdditionalFiles
Definition: TestTU.h:57
clang::clangd::findSymbol
const Symbol & findSymbol(const SymbolSlab &Slab, llvm::StringRef QName)
Definition: TestTU.cpp:187
clang::clangd::IgnoreDiagnostics
Definition: Compiler.h:31
clang::clangd::TestTU::HeaderCode
std::string HeaderCode
Definition: TestTU.h:53
contains
This directory contains
Definition: README.txt:6
clang::clangd::indexMainDecls
SlabTuple indexMainDecls(ParsedAST &AST)
Retrieves symbols and refs of local top level decls in AST (i.e.
Definition: FileIndex.cpp:222