[lldb][NFC] Add unit test for persistent variable lookup with ClangExpressionDeclMap

This adds a unit test for looking up persistent declarations in the scratch AST
context. Also adds the `GetPersistentDecl` hook to the ClangExpressionDeclMap
that this unit test can emulate looking up persistent variables without having
a lldb_private::Target.
This commit is contained in:
Raphael Isemann 2019-12-17 16:12:07 +01:00
parent 55c57408b0
commit d8a3194987
3 changed files with 126 additions and 23 deletions

View File

@ -734,29 +734,33 @@ void ClangExpressionDeclMap::MaybeRegisterFunctionBody(
}
}
void ClangExpressionDeclMap::SearchPersistenDecls(NameSearchContext &context,
const ConstString name,
unsigned int current_id) {
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
clang::NamedDecl *ClangExpressionDeclMap::GetPersistentDecl(ConstString name) {
if (!m_parser_vars)
return;
return nullptr;
Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
if (!target)
return;
return nullptr;
ClangASTContext *scratch_clang_ast_context =
ClangASTContext::GetScratch(*target);
if (!scratch_clang_ast_context)
return;
return nullptr;
ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext();
if (!scratch_ast_context)
return;
return nullptr;
NamedDecl *persistent_decl =
m_parser_vars->m_persistent_vars->GetPersistentDecl(name);
return m_parser_vars->m_persistent_vars->GetPersistentDecl(name);
}
void ClangExpressionDeclMap::SearchPersistenDecls(NameSearchContext &context,
const ConstString name,
unsigned int current_id) {
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
NamedDecl *persistent_decl = GetPersistentDecl(name);
if (!persistent_decl)
return;
@ -1409,6 +1413,10 @@ void ClangExpressionDeclMap::FindExternalVisibleDecls(
if (name.GetStringRef().startswith("$__lldb"))
return;
// No ParserVars means we can't do register or variable lookup.
if (!m_parser_vars)
return;
ExpressionVariableSP pvar_sp(
m_parser_vars->m_persistent_vars->GetVariable(name));

View File

@ -283,6 +283,14 @@ public:
CompilerDeclContext &namespace_decl,
unsigned int current_id);
protected:
/// Retrieves the declaration with the given name from the storage of
/// persistent declarations.
///
/// \return
/// A persistent decl with the given name or a nullptr.
virtual clang::NamedDecl *GetPersistentDecl(ConstString name);
private:
ExpressionVariableList
m_found_entities; ///< All entities that were looked up for the parser.

View File

@ -11,12 +11,51 @@
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Symbol/ClangASTContext.h"
#include "lldb/Symbol/ClangUtil.h"
#include "lldb/lldb-defines.h"
#include "gtest/gtest.h"
using namespace lldb_private;
using namespace lldb;
static std::unique_ptr<ClangASTContext> createAST() {
return std::make_unique<ClangASTContext>(HostInfo::GetTargetTriple());
}
namespace {
struct FakeClangExpressionDeclMap : public ClangExpressionDeclMap {
FakeClangExpressionDeclMap(const ClangASTImporterSP &importer)
: ClangExpressionDeclMap(false, nullptr, lldb::TargetSP(), importer,
nullptr) {
m_scratch_context = createAST();
}
std::unique_ptr<ClangASTContext> 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().startswith("$"));
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 {
static void SetUpTestCase() {
@ -28,8 +67,25 @@ struct ClangExpressionDeclMapTest : public testing::Test {
FileSystem::Terminate();
}
std::unique_ptr<ClangASTContext> createAST() {
return std::make_unique<ClangASTContext>(HostInfo::GetTargetTriple());
/// The ClangASTImporter used during the test.
ClangASTImporterSP importer;
/// The ExpressionDeclMap for the current test case.
std::unique_ptr<FakeClangExpressionDeclMap> decl_map;
/// The target AST that lookup results should be imported to.
std::unique_ptr<ClangASTContext> target_ast;
void SetUp() override {
importer = std::make_shared<ClangASTImporter>();
decl_map = std::make_unique<FakeClangExpressionDeclMap>(importer);
target_ast = createAST();
decl_map->InstallASTContext(*target_ast, *target_ast->getFileManager());
}
void TearDown() override {
importer.reset();
decl_map.reset();
target_ast.reset();
}
clang::DeclarationName getDeclarationName(ClangASTContext &ast,
@ -37,23 +93,54 @@ struct ClangExpressionDeclMapTest : public testing::Test {
clang::IdentifierInfo &II = ast.getIdentifierTable()->get(name);
return ast.getASTContext()->DeclarationNames.getIdentifier(&II);
}
CompilerType createRecord(ClangASTContext &ast, llvm::StringRef name) {
CompilerType t = ast.CreateRecordType(ast.getASTContext()->getTranslationUnitDecl(),
lldb::AccessType::eAccessPublic, name, 0,
lldb::LanguageType::eLanguageTypeC);
ClangASTContext::StartTagDeclarationDefinition(t);
ClangASTContext::CompleteTagDeclarationDefinition(t);
return t;
}
};
} // namespace
TEST_F(ClangExpressionDeclMapTest, TestIdentifierLookupInEmptyTU) {
ClangASTImporterSP importer = std::make_shared<ClangASTImporter>();
ClangExpressionDeclMap map(false, nullptr, lldb::TargetSP(), importer,
nullptr);
std::unique_ptr<ClangASTContext> ast = createAST();
map.InstallASTContext(*ast, *ast->getFileManager());
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 = getDeclarationName(*ast, "does_no_exist");
const clang::DeclContext *dc = ast->GetTranslationUnitDecl();
clang::DeclarationName name = getDeclarationName(*target_ast, "foo");
const clang::DeclContext *dc = target_ast->GetTranslationUnitDecl();
NameSearchContext search(*decl_map, decls, name, dc);
NameSearchContext search(map, decls, name, dc);
map.FindExternalVisibleDecls(search);
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 =
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 = getDeclarationName(*target_ast, decl_name);
const clang::DeclContext *dc = target_ast->GetTranslationUnitDecl();
NameSearchContext search(*decl_map, 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());
}