Files
TheRock/compiler/amd-llvm/lldb/unittests/Expression/ClangExpressionDeclMapTest.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

129 lines
4.9 KiB
C++

//===-- ClangExpressionDeclMapTest.cpp ------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.h"
#include "Plugins/ExpressionParser/Clang/ClangUtil.h"
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
#include "TestingSupport/SubsystemRAII.h"
#include "TestingSupport/Symbol/ClangTestUtils.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/lldb-defines.h"
#include "gtest/gtest.h"
using namespace lldb_private;
using namespace lldb;
namespace {
struct FakeClangExpressionDeclMap : public ClangExpressionDeclMap {
FakeClangExpressionDeclMap(const std::shared_ptr<ClangASTImporter> &importer)
: ClangExpressionDeclMap(false, nullptr, lldb::TargetSP(), importer,
nullptr, /*ignore_context_qualifiers=*/false) {
m_holder = std::make_unique<clang_utils::TypeSystemClangHolder>("ast");
m_scratch_context = m_holder->GetAST();
}
std::unique_ptr<clang_utils::TypeSystemClangHolder> m_holder;
TypeSystemClang *m_scratch_context;
/// Adds a persistent decl that can be found by the ClangExpressionDeclMap
/// via GetPersistentDecl.
void AddPersistentDeclForTest(clang::NamedDecl *d) {
// The declaration needs to have '$' prefix in its name like every
// persistent declaration and must be inside the scratch AST context.
assert(d);
assert(d->getName().starts_with("$"));
assert(&d->getASTContext() == &m_scratch_context->getASTContext());
m_persistent_decls[d->getName()] = d;
}
protected:
// ClangExpressionDeclMap hooks.
clang::NamedDecl *GetPersistentDecl(ConstString name) override {
// ClangExpressionDeclMap wants to know if there is a persistent decl
// with the given name. Check the
return m_persistent_decls.lookup(name.GetStringRef());
}
private:
/// The persistent decls in this test with their names as keys.
llvm::DenseMap<llvm::StringRef, clang::NamedDecl *> m_persistent_decls;
};
} // namespace
namespace {
struct ClangExpressionDeclMapTest : public testing::Test {
SubsystemRAII<FileSystem, HostInfo> subsystems;
/// The ClangASTImporter used during the test.
std::shared_ptr<ClangASTImporter> importer;
/// The ExpressionDeclMap for the current test case.
std::unique_ptr<FakeClangExpressionDeclMap> decl_map;
std::unique_ptr<clang_utils::TypeSystemClangHolder> holder;
/// The target AST that lookup results should be imported to.
TypeSystemClang *target_ast;
void SetUp() override {
importer = std::make_shared<ClangASTImporter>();
decl_map = std::make_unique<FakeClangExpressionDeclMap>(importer);
holder = std::make_unique<clang_utils::TypeSystemClangHolder>("target ast");
target_ast = holder->GetAST();
decl_map->InstallASTContext(*target_ast);
}
void TearDown() override {
importer.reset();
decl_map.reset();
holder.reset();
}
};
} // namespace
TEST_F(ClangExpressionDeclMapTest, TestUnknownIdentifierLookup) {
// Tests looking up an identifier that can't be found anywhere.
// Setup a NameSearchContext for 'foo'.
llvm::SmallVector<clang::NamedDecl *, 16> decls;
clang::DeclarationName name =
clang_utils::getDeclarationName(*target_ast, "foo");
const clang::DeclContext *dc = target_ast->GetTranslationUnitDecl();
NameSearchContext search(*target_ast, decls, name, dc);
decl_map->FindExternalVisibleDecls(search);
// This shouldn't exist so we should get no lookups.
EXPECT_EQ(0U, decls.size());
}
TEST_F(ClangExpressionDeclMapTest, TestPersistentDeclLookup) {
// Tests looking up a persistent decl from the scratch AST context.
// Create a '$persistent_class' record and add it as a persistent variable
// to the scratch AST context.
llvm::StringRef decl_name = "$persistent_class";
CompilerType persistent_type =
clang_utils::createRecord(*decl_map->m_scratch_context, decl_name);
decl_map->AddPersistentDeclForTest(ClangUtil::GetAsTagDecl(persistent_type));
// Setup a NameSearchContext for $persistent_class;
llvm::SmallVector<clang::NamedDecl *, 16> decls;
clang::DeclarationName name =
clang_utils::getDeclarationName(*target_ast, decl_name);
const clang::DeclContext *dc = target_ast->GetTranslationUnitDecl();
NameSearchContext search(*target_ast, decls, name, dc);
// Search and check that we found $persistent_class.
decl_map->FindExternalVisibleDecls(search);
EXPECT_EQ(1U, decls.size());
EXPECT_EQ(decl_name, decls.front()->getQualifiedNameAsString());
auto *record = llvm::cast<clang::RecordDecl>(decls.front());
// The class was minimally imported from the scratch AST context.
EXPECT_TRUE(record->hasExternalLexicalStorage());
}