| 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 | |
| 26 | ParseInputs TestTU::inputs(MockFS &FS) const { |
| 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 | |
| 40 | ParseInputs Inputs; |
| 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. |
| 58 | if (ImplicitHeaderGuard) |
| 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; |
| 71 | if (OverlayRealFileSystemForModules) |
| 72 | FS.OverlayRealFileSystemForModules = true; |
| 73 | Inputs.TFS = &FS; |
| 74 | Inputs.Opts = ParseOptions(); |
| 75 | if (ClangTidyProvider) |
| 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); |
| 103 | IgnoreDiagnostics Diags; |
| 104 | auto CI = buildCompilerInvocation(Inputs, Diags); |
| 105 | assert(CI && "Failed to build compilation invocation."); |
| 106 | if (OverlayRealFileSystemForModules) |
| 107 | initializeModuleCache(*CI); |
| 108 | auto ModuleCacheDeleter = llvm::make_scope_exit( |
| 109 | std::bind(deleteModuleCache, CI->getHeaderSearchOpts().ModuleCachePath)); |
| 110 | return clang::clangd::buildPreamble(testPath(Filename), *CI, Inputs, |
| 111 | /*StoreInMemory=*/true, PreambleCallback); |
| 112 | } |
| 113 | |
| 114 | ParsedAST TestTU::build() const { |
| 115 | MockFS FS; |
| 116 | auto Inputs = inputs(FS); |
| 117 | Inputs.Opts = ParseOpts; |
| 118 | StoreDiags Diags; |
| 119 | auto CI = buildCompilerInvocation(Inputs, Diags); |
| 120 | assert(CI && "Failed to build compilation invocation."); |
| 121 | if (OverlayRealFileSystemForModules) |
| 122 | initializeModuleCache(*CI); |
| 123 | auto ModuleCacheDeleter = llvm::make_scope_exit( |
| 124 | std::bind(deleteModuleCache, CI->getHeaderSearchOpts().ModuleCachePath)); |
| 125 | |
| 126 | auto Preamble = clang::clangd::buildPreamble(testPath(Filename), *CI, Inputs, |
| 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 | |
| 165 | SymbolSlab TestTU::headerSymbols() const { |
| 166 | auto AST = build(); |
| 167 | return std::get<0>(indexHeaderSymbols(/*Version=*/"null", AST.getASTContext(), |
| 168 | AST.getPreprocessor(), |
| 169 | AST.getCanonicalIncludes())); |
| 170 | } |
| 171 | |
| 172 | RefSlab TestTU::headerRefs() const { |
| 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. |
| 209 | class TraverseHeadersToo { |
| 210 | ASTContext &Ctx; |
| 211 | std::vector<Decl *> ScopeToRestore; |
| 212 | |
| 213 | public: |
| 214 | TraverseHeadersToo(ParsedAST &AST) |
| 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 |