#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) {
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;
void AddPersistentDeclForTest(clang::NamedDecl *d) {
assert(d);
assert(d->getName().starts_with("$"));
assert(&d->getASTContext() == &m_scratch_context->getASTContext());
m_persistent_decls[d->getName()] = d;
}
protected:
clang::NamedDecl *GetPersistentDecl(ConstString name) override {
return m_persistent_decls.lookup(name.GetStringRef());
}
private:
llvm::DenseMap<llvm::StringRef, clang::NamedDecl *> m_persistent_decls;
};
}
namespace {
struct ClangExpressionDeclMapTest : public testing::Test {
SubsystemRAII<FileSystem, HostInfo> subsystems;
std::shared_ptr<ClangASTImporter> importer;
std::unique_ptr<FakeClangExpressionDeclMap> decl_map;
std::unique_ptr<clang_utils::TypeSystemClangHolder> holder;
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();
}
};
}
TEST_F(ClangExpressionDeclMapTest, TestUnknownIdentifierLookup) {
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);
EXPECT_EQ(0U, decls.size());
}
TEST_F(ClangExpressionDeclMapTest, TestPersistentDeclLookup) {
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));
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);
decl_map->FindExternalVisibleDecls(search);
EXPECT_EQ(1U, decls.size());
EXPECT_EQ(decl_name, decls.front()->getQualifiedNameAsString());
auto *record = llvm::cast<clang::RecordDecl>(decls.front());
EXPECT_TRUE(record->hasExternalLexicalStorage());
}