2019-12-17 20:38:07 +08:00
|
|
|
//===-- 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"
|
[lldb] Move clang-based files out of Symbol
Summary:
This change represents the move of ClangASTImporter, ClangASTMetadata,
ClangExternalASTSourceCallbacks, ClangUtil, CxxModuleHandler, and
TypeSystemClang from lldbSource to lldbPluginExpressionParserClang.h
This explicitly removes knowledge of clang internals from lldbSymbol,
moving towards a more generic core implementation of lldb.
Reviewers: JDevlieghere, davide, aprantl, teemperor, clayborg, labath, jingham, shafik
Subscribers: emaste, mgorny, arphaman, jfb, usaxena95, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D73661
2020-01-30 03:59:28 +08:00
|
|
|
#include "Plugins/ExpressionParser/Clang/ClangUtil.h"
|
|
|
|
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
|
2019-12-23 17:38:12 +08:00
|
|
|
#include "TestingSupport/SubsystemRAII.h"
|
2019-12-20 22:09:40 +08:00
|
|
|
#include "TestingSupport/Symbol/ClangTestUtils.h"
|
2019-12-17 20:38:07 +08:00
|
|
|
#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;
|
|
|
|
|
2019-12-17 23:12:07 +08:00
|
|
|
namespace {
|
|
|
|
struct FakeClangExpressionDeclMap : public ClangExpressionDeclMap {
|
2020-02-04 07:55:18 +08:00
|
|
|
FakeClangExpressionDeclMap(const std::shared_ptr<ClangASTImporter> &importer)
|
2019-12-17 23:12:07 +08:00
|
|
|
: ClangExpressionDeclMap(false, nullptr, lldb::TargetSP(), importer,
|
|
|
|
nullptr) {
|
2019-12-21 01:44:39 +08:00
|
|
|
m_scratch_context = clang_utils::createAST();
|
2019-12-17 23:12:07 +08:00
|
|
|
}
|
[lldb][NFC] Rename ClangASTContext to TypeSystemClang
Summary:
This commit renames ClangASTContext to TypeSystemClang to better reflect what this class is actually supposed to do
(implement the TypeSystem interface for Clang). It also gets rid of the very confusing situation that we have both a
`clang::ASTContext` and a `ClangASTContext` in clang (which sometimes causes Clang people to think I'm fiddling
with Clang's ASTContext when I'm actually just doing LLDB work).
I also have plans to potentially have multiple clang::ASTContext instances associated with one ClangASTContext so
the ASTContext naming will then become even more confusing to people.
Reviewers: #lldb, aprantl, shafik, clayborg, labath, JDevlieghere, davide, espindola, jdoerfert, xiaobai
Reviewed By: clayborg, labath, xiaobai
Subscribers: wuzish, emaste, nemanjai, mgorny, kbarton, MaskRay, arphaman, jfb, usaxena95, jingham, xiaobai, abidh, JDevlieghere, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D72684
2020-01-23 17:04:13 +08:00
|
|
|
std::unique_ptr<TypeSystemClang> m_scratch_context;
|
2019-12-17 23:12:07 +08:00
|
|
|
/// 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("$"));
|
2019-12-22 05:40:52 +08:00
|
|
|
assert(&d->getASTContext() == &m_scratch_context->getASTContext());
|
2019-12-17 23:12:07 +08:00
|
|
|
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
|
|
|
|
|
2019-12-17 20:38:07 +08:00
|
|
|
namespace {
|
|
|
|
struct ClangExpressionDeclMapTest : public testing::Test {
|
2019-12-23 17:38:12 +08:00
|
|
|
SubsystemRAII<FileSystem, HostInfo> subsystems;
|
2019-12-17 20:38:07 +08:00
|
|
|
|
2019-12-17 23:12:07 +08:00
|
|
|
/// The ClangASTImporter used during the test.
|
2020-02-04 07:55:18 +08:00
|
|
|
std::shared_ptr<ClangASTImporter> importer;
|
2019-12-17 23:12:07 +08:00
|
|
|
/// The ExpressionDeclMap for the current test case.
|
|
|
|
std::unique_ptr<FakeClangExpressionDeclMap> decl_map;
|
|
|
|
|
|
|
|
/// The target AST that lookup results should be imported to.
|
[lldb][NFC] Rename ClangASTContext to TypeSystemClang
Summary:
This commit renames ClangASTContext to TypeSystemClang to better reflect what this class is actually supposed to do
(implement the TypeSystem interface for Clang). It also gets rid of the very confusing situation that we have both a
`clang::ASTContext` and a `ClangASTContext` in clang (which sometimes causes Clang people to think I'm fiddling
with Clang's ASTContext when I'm actually just doing LLDB work).
I also have plans to potentially have multiple clang::ASTContext instances associated with one ClangASTContext so
the ASTContext naming will then become even more confusing to people.
Reviewers: #lldb, aprantl, shafik, clayborg, labath, JDevlieghere, davide, espindola, jdoerfert, xiaobai
Reviewed By: clayborg, labath, xiaobai
Subscribers: wuzish, emaste, nemanjai, mgorny, kbarton, MaskRay, arphaman, jfb, usaxena95, jingham, xiaobai, abidh, JDevlieghere, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D72684
2020-01-23 17:04:13 +08:00
|
|
|
std::unique_ptr<TypeSystemClang> target_ast;
|
2019-12-17 23:12:07 +08:00
|
|
|
|
|
|
|
void SetUp() override {
|
|
|
|
importer = std::make_shared<ClangASTImporter>();
|
|
|
|
decl_map = std::make_unique<FakeClangExpressionDeclMap>(importer);
|
2019-12-20 22:09:40 +08:00
|
|
|
target_ast = clang_utils::createAST();
|
2019-12-21 22:26:24 +08:00
|
|
|
decl_map->InstallASTContext(*target_ast);
|
2019-12-17 23:12:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TearDown() override {
|
|
|
|
importer.reset();
|
|
|
|
decl_map.reset();
|
|
|
|
target_ast.reset();
|
2019-12-17 20:38:07 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2019-12-17 23:12:07 +08:00
|
|
|
TEST_F(ClangExpressionDeclMapTest, TestUnknownIdentifierLookup) {
|
|
|
|
// Tests looking up an identifier that can't be found anywhere.
|
2019-12-17 20:38:07 +08:00
|
|
|
|
2019-12-17 23:12:07 +08:00
|
|
|
// Setup a NameSearchContext for 'foo'.
|
2019-12-17 20:38:07 +08:00
|
|
|
llvm::SmallVector<clang::NamedDecl *, 16> decls;
|
2019-12-20 22:09:40 +08:00
|
|
|
clang::DeclarationName name =
|
|
|
|
clang_utils::getDeclarationName(*target_ast, "foo");
|
2019-12-17 23:12:07 +08:00
|
|
|
const clang::DeclContext *dc = target_ast->GetTranslationUnitDecl();
|
2020-02-25 19:05:46 +08:00
|
|
|
NameSearchContext search(*target_ast, decls, name, dc);
|
2019-12-17 20:38:07 +08:00
|
|
|
|
2019-12-17 23:12:07 +08:00
|
|
|
decl_map->FindExternalVisibleDecls(search);
|
2019-12-17 20:38:07 +08:00
|
|
|
|
2019-12-17 23:12:07 +08:00
|
|
|
// This shouldn't exist so we should get no lookups.
|
2019-12-17 20:38:07 +08:00
|
|
|
EXPECT_EQ(0U, decls.size());
|
|
|
|
}
|
2019-12-17 23:12:07 +08:00
|
|
|
|
|
|
|
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 =
|
2019-12-20 22:09:40 +08:00
|
|
|
clang_utils::createRecord(*decl_map->m_scratch_context, decl_name);
|
2019-12-17 23:12:07 +08:00
|
|
|
decl_map->AddPersistentDeclForTest(ClangUtil::GetAsTagDecl(persistent_type));
|
|
|
|
|
|
|
|
// Setup a NameSearchContext for $persistent_class;
|
|
|
|
llvm::SmallVector<clang::NamedDecl *, 16> decls;
|
2019-12-20 22:09:40 +08:00
|
|
|
clang::DeclarationName name =
|
|
|
|
clang_utils::getDeclarationName(*target_ast, decl_name);
|
2019-12-17 23:12:07 +08:00
|
|
|
const clang::DeclContext *dc = target_ast->GetTranslationUnitDecl();
|
2020-02-25 19:05:46 +08:00
|
|
|
NameSearchContext search(*target_ast, decls, name, dc);
|
2019-12-17 23:12:07 +08:00
|
|
|
|
|
|
|
// 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());
|
|
|
|
}
|