brintos

brintos / llvm-project-archived public Read only

0
0
Text · 9.6 KiB · a733fac Raw
272 lines · cpp
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-exception6//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 "index/SymbolOrigin.h"16#include "clang/AST/RecursiveASTVisitor.h"17#include "clang/Basic/Diagnostic.h"18#include "clang/Frontend/CompilerInvocation.h"19#include "llvm/ADT/ScopeExit.h"20#include "llvm/Support/ScopedPrinter.h"21#include "llvm/Support/raw_ostream.h"22#include <cstdlib>23 24namespace clang {25namespace clangd {26 27ParseInputs TestTU::inputs(MockFS &FS) const {28  std::string FullFilename = testPath(Filename),29              FullHeaderName = testPath(HeaderFilename),30              ImportThunk = testPath("import_thunk.h");31  // We want to implicitly include HeaderFilename without messing up offsets.32  // -include achieves this, but sometimes we want #import (to simulate a header33  // guard without messing up offsets). In this case, use an intermediate file.34  std::string ThunkContents = "#import \"" + FullHeaderName + "\"\n";35 36  FS.Files = AdditionalFiles;37  FS.Files[FullFilename] = Code;38  FS.Files[FullHeaderName] = HeaderCode;39  FS.Files[ImportThunk] = ThunkContents;40 41  ParseInputs Inputs;42  Inputs.FeatureModules = FeatureModules;43  auto &Argv = Inputs.CompileCommand.CommandLine;44  Argv = {"clang"};45  // In tests, unless explicitly specified otherwise, omit predefined macros46  // (__GNUC__ etc) for a 25% speedup. There are hundreds, and we'd generate,47  // parse, serialize, and re-parse them!48  if (!PredefineMacros) {49    Argv.push_back("-Xclang");50    Argv.push_back("-undef");51  }52  // FIXME: this shouldn't need to be conditional, but it breaks a53  // GoToDefinition test for some reason (getMacroArgExpandedLocation fails).54  if (!HeaderCode.empty()) {55    Argv.push_back("-include");56    Argv.push_back(ImplicitHeaderGuard ? ImportThunk : FullHeaderName);57    // ms-compatibility changes the meaning of #import.58    // The default is OS-dependent (on windows), ensure it's off.59    if (ImplicitHeaderGuard)60      Inputs.CompileCommand.CommandLine.push_back("-fno-ms-compatibility");61  }62  Argv.insert(Argv.end(), ExtraArgs.begin(), ExtraArgs.end());63  // Put the file name at the end -- this allows the extra arg (-xc++) to64  // override the language setting.65  Argv.push_back(FullFilename);66 67  auto Mangler = CommandMangler::forTests();68  Mangler(Inputs.CompileCommand, FullFilename);69  Inputs.CompileCommand.Filename = FullFilename;70  Inputs.CompileCommand.Directory = testRoot();71  Inputs.Contents = Code;72  if (OverlayRealFileSystemForModules)73    FS.OverlayRealFileSystemForModules = true;74  Inputs.TFS = &FS;75  Inputs.Opts = ParseOptions();76  if (ClangTidyProvider)77    Inputs.ClangTidyProvider = ClangTidyProvider;78  Inputs.Index = ExternalIndex;79  return Inputs;80}81 82void initializeModuleCache(CompilerInvocation &CI) {83  llvm::SmallString<128> ModuleCachePath;84  if (llvm::sys::fs::createUniqueDirectory("module-cache", ModuleCachePath)) {85    llvm::errs() << "Failed to create temp directory for module-cache";86    std::abort();87  }88  CI.getHeaderSearchOpts().ModuleCachePath = ModuleCachePath.c_str();89}90 91void deleteModuleCache(const std::string ModuleCachePath) {92  if (!ModuleCachePath.empty()) {93    if (llvm::sys::fs::remove_directories(ModuleCachePath)) {94      llvm::errs() << "Failed to delete temp directory for module-cache";95      std::abort();96    }97  }98}99 100std::shared_ptr<const PreambleData>101TestTU::preamble(PreambleParsedCallback PreambleCallback) const {102  MockFS FS;103  auto Inputs = inputs(FS);104  IgnoreDiagnostics Diags;105  auto CI = buildCompilerInvocation(Inputs, Diags);106  assert(CI && "Failed to build compilation invocation.");107  if (OverlayRealFileSystemForModules)108    initializeModuleCache(*CI);109  auto ModuleCacheDeleter = llvm::make_scope_exit(110      std::bind(deleteModuleCache, CI->getHeaderSearchOpts().ModuleCachePath));111  return clang::clangd::buildPreamble(testPath(Filename), *CI, Inputs,112                                      /*StoreInMemory=*/true, PreambleCallback);113}114 115ParsedAST TestTU::build() const {116  MockFS FS;117  auto Inputs = inputs(FS);118  Inputs.Opts = ParseOpts;119  StoreDiags Diags;120  auto CI = buildCompilerInvocation(Inputs, Diags);121  assert(CI && "Failed to build compilation invocation.");122  if (OverlayRealFileSystemForModules)123    initializeModuleCache(*CI);124  auto ModuleCacheDeleter = llvm::make_scope_exit(125      std::bind(deleteModuleCache, CI->getHeaderSearchOpts().ModuleCachePath));126 127  auto Preamble = clang::clangd::buildPreamble(testPath(Filename), *CI, Inputs,128                                               /*StoreInMemory=*/true,129                                               /*PreambleCallback=*/nullptr);130  auto AST = ParsedAST::build(testPath(Filename), Inputs, std::move(CI),131                              Diags.take(), Preamble);132  if (!AST) {133    llvm::errs() << "Failed to build code:\n" << Code;134    std::abort();135  }136  // Check for error diagnostics and report gtest failures (unless expected).137  // This guards against accidental syntax errors silently subverting tests.138  // error-ok is awfully primitive - using clang -verify would be nicer.139  // Ownership and layering makes it pretty hard.140  bool ErrorOk = [&, this] {141    llvm::StringLiteral Marker = "error-ok";142    if (llvm::StringRef(Code).contains(Marker) ||143        llvm::StringRef(HeaderCode).contains(Marker))144      return true;145    for (const auto &KV : this->AdditionalFiles)146      if (llvm::StringRef(KV.second).contains(Marker))147        return true;148    return false;149  }();150  if (!ErrorOk) {151    // We always build AST with a fresh preamble in TestTU.152    for (const auto &D : AST->getDiagnostics())153      if (D.Severity >= DiagnosticsEngine::Error) {154        llvm::errs()155            << "TestTU failed to build (suppress with /*error-ok*/): \n"156            << D << "\n\nFor code:\n"157            << Code;158        std::abort(); // Stop after first error for simplicity.159      }160  }161  return std::move(*AST);162}163 164SymbolSlab TestTU::headerSymbols() const {165  auto AST = build();166  return std::get<0>(indexHeaderSymbols(167      /*Version=*/"null", AST.getASTContext(), AST.getPreprocessor(),168      AST.getPragmaIncludes(), SymbolOrigin::Preamble));169}170 171RefSlab TestTU::headerRefs() const {172  auto AST = build();173  return std::get<1>(indexMainDecls(AST));174}175 176std::unique_ptr<SymbolIndex> TestTU::index() const {177  auto AST = build();178  auto Idx = std::make_unique<FileIndex>(/*SupportContainedRefs=*/true);179  Idx->updatePreamble(testPath(Filename), /*Version=*/"null",180                      AST.getASTContext(), AST.getPreprocessor(),181                      AST.getPragmaIncludes());182  Idx->updateMain(testPath(Filename), AST);183  return std::move(Idx);184}185 186const Symbol &findSymbol(const SymbolSlab &Slab, llvm::StringRef QName) {187  const Symbol *Result = nullptr;188  for (const Symbol &S : Slab) {189    if (QName != (S.Scope + S.Name).str())190      continue;191    if (Result) {192      llvm::errs() << "Multiple symbols named " << QName << ":\n"193                   << *Result << "\n---\n"194                   << S;195      assert(false && "QName is not unique");196    }197    Result = &S;198  }199  if (!Result) {200    llvm::errs() << "No symbol named " << QName << " in "201                 << llvm::to_string(Slab);202    assert(false && "No symbol with QName");203  }204  return *Result;205}206 207// RAII scoped class to disable TraversalScope for a ParsedAST.208class TraverseHeadersToo {209  ASTContext &Ctx;210  std::vector<Decl *> ScopeToRestore;211 212public:213  TraverseHeadersToo(ParsedAST &AST)214      : Ctx(AST.getASTContext()), ScopeToRestore(Ctx.getTraversalScope()) {215    Ctx.setTraversalScope({Ctx.getTranslationUnitDecl()});216  }217  ~TraverseHeadersToo() { Ctx.setTraversalScope(std::move(ScopeToRestore)); }218};219 220const NamedDecl &findDecl(ParsedAST &AST, llvm::StringRef QName) {221  auto &Ctx = AST.getASTContext();222  auto LookupDecl = [&Ctx](const DeclContext &Scope,223                           llvm::StringRef Name) -> const NamedDecl & {224    auto LookupRes = Scope.lookup(DeclarationName(&Ctx.Idents.get(Name)));225    assert(!LookupRes.empty() && "Lookup failed");226    assert(LookupRes.isSingleResult() && "Lookup returned multiple results");227    return *LookupRes.front();228  };229 230  const DeclContext *Scope = Ctx.getTranslationUnitDecl();231 232  StringRef Cur, Rest;233  for (std::tie(Cur, Rest) = QName.split("::"); !Rest.empty();234       std::tie(Cur, Rest) = Rest.split("::")) {235    Scope = &cast<DeclContext>(LookupDecl(*Scope, Cur));236  }237  return LookupDecl(*Scope, Cur);238}239 240const NamedDecl &findDecl(ParsedAST &AST,241                          std::function<bool(const NamedDecl &)> Filter) {242  TraverseHeadersToo Too(AST);243  struct Visitor : RecursiveASTVisitor<Visitor> {244    decltype(Filter) F;245    llvm::SmallVector<const NamedDecl *, 1> Decls;246    bool VisitNamedDecl(const NamedDecl *ND) {247      if (F(*ND))248        Decls.push_back(ND);249      return true;250    }251  } Visitor;252  Visitor.F = Filter;253  Visitor.TraverseDecl(AST.getASTContext().getTranslationUnitDecl());254  if (Visitor.Decls.size() != 1) {255    llvm::errs() << Visitor.Decls.size() << " symbols matched.\n";256    assert(Visitor.Decls.size() == 1);257  }258  return *Visitor.Decls.front();259}260 261const NamedDecl &findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name) {262  return findDecl(AST, [Name](const NamedDecl &ND) {263    if (auto *ID = ND.getIdentifier())264      if (ID->getName() == Name)265        return true;266    return false;267  });268}269 270} // namespace clangd271} // namespace clang272