1//===-- ClangExpressionDeclMapTest.cpp ------------------------------------===//
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 "Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.h"
10#include "Plugins/ExpressionParser/Clang/ClangUtil.h"
11#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
12#include "TestingSupport/SubsystemRAII.h"
13#include "TestingSupport/Symbol/ClangTestUtils.h"
14#include "lldb/Host/FileSystem.h"
15#include "lldb/Host/HostInfo.h"
16#include "lldb/lldb-defines.h"
17#include "gtest/gtest.h"
18
19using namespace lldb_private;
20using namespace lldb;
21
22namespace {
23struct FakeClangExpressionDeclMap : public ClangExpressionDeclMap {
24 FakeClangExpressionDeclMap(const std::shared_ptr<ClangASTImporter> &importer)
25 : ClangExpressionDeclMap(false, nullptr, lldb::TargetSP(), importer,
26 nullptr) {
27 m_holder = std::make_unique<clang_utils::TypeSystemClangHolder>(args: "ast");
28 m_scratch_context = m_holder->GetAST();
29 }
30 std::unique_ptr<clang_utils::TypeSystemClangHolder> m_holder;
31 TypeSystemClang *m_scratch_context;
32 /// Adds a persistent decl that can be found by the ClangExpressionDeclMap
33 /// via GetPersistentDecl.
34 void AddPersistentDeclForTest(clang::NamedDecl *d) {
35 // The declaration needs to have '$' prefix in its name like every
36 // persistent declaration and must be inside the scratch AST context.
37 assert(d);
38 assert(d->getName().starts_with("$"));
39 assert(&d->getASTContext() == &m_scratch_context->getASTContext());
40 m_persistent_decls[d->getName()] = d;
41 }
42
43protected:
44 // ClangExpressionDeclMap hooks.
45
46 clang::NamedDecl *GetPersistentDecl(ConstString name) override {
47 // ClangExpressionDeclMap wants to know if there is a persistent decl
48 // with the given name. Check the
49 return m_persistent_decls.lookup(Val: name.GetStringRef());
50 }
51
52private:
53 /// The persistent decls in this test with their names as keys.
54 llvm::DenseMap<llvm::StringRef, clang::NamedDecl *> m_persistent_decls;
55};
56} // namespace
57
58namespace {
59struct ClangExpressionDeclMapTest : public testing::Test {
60 SubsystemRAII<FileSystem, HostInfo> subsystems;
61
62 /// The ClangASTImporter used during the test.
63 std::shared_ptr<ClangASTImporter> importer;
64 /// The ExpressionDeclMap for the current test case.
65 std::unique_ptr<FakeClangExpressionDeclMap> decl_map;
66
67 std::unique_ptr<clang_utils::TypeSystemClangHolder> holder;
68
69 /// The target AST that lookup results should be imported to.
70 TypeSystemClang *target_ast;
71
72 void SetUp() override {
73 importer = std::make_shared<ClangASTImporter>();
74 decl_map = std::make_unique<FakeClangExpressionDeclMap>(args&: importer);
75 holder = std::make_unique<clang_utils::TypeSystemClangHolder>(args: "target ast");
76 target_ast = holder->GetAST();
77 decl_map->InstallASTContext(ast_context&: *target_ast);
78 }
79
80 void TearDown() override {
81 importer.reset();
82 decl_map.reset();
83 holder.reset();
84 }
85};
86} // namespace
87
88TEST_F(ClangExpressionDeclMapTest, TestUnknownIdentifierLookup) {
89 // Tests looking up an identifier that can't be found anywhere.
90
91 // Setup a NameSearchContext for 'foo'.
92 llvm::SmallVector<clang::NamedDecl *, 16> decls;
93 clang::DeclarationName name =
94 clang_utils::getDeclarationName(ast&: *target_ast, name: "foo");
95 const clang::DeclContext *dc = target_ast->GetTranslationUnitDecl();
96 NameSearchContext search(*target_ast, decls, name, dc);
97
98 decl_map->FindExternalVisibleDecls(context&: search);
99
100 // This shouldn't exist so we should get no lookups.
101 EXPECT_EQ(0U, decls.size());
102}
103
104TEST_F(ClangExpressionDeclMapTest, TestPersistentDeclLookup) {
105 // Tests looking up a persistent decl from the scratch AST context.
106
107 // Create a '$persistent_class' record and add it as a persistent variable
108 // to the scratch AST context.
109 llvm::StringRef decl_name = "$persistent_class";
110 CompilerType persistent_type =
111 clang_utils::createRecord(ast&: *decl_map->m_scratch_context, name: decl_name);
112 decl_map->AddPersistentDeclForTest(ClangUtil::GetAsTagDecl(type: persistent_type));
113
114 // Setup a NameSearchContext for $persistent_class;
115 llvm::SmallVector<clang::NamedDecl *, 16> decls;
116 clang::DeclarationName name =
117 clang_utils::getDeclarationName(ast&: *target_ast, name: decl_name);
118 const clang::DeclContext *dc = target_ast->GetTranslationUnitDecl();
119 NameSearchContext search(*target_ast, decls, name, dc);
120
121 // Search and check that we found $persistent_class.
122 decl_map->FindExternalVisibleDecls(context&: search);
123 EXPECT_EQ(1U, decls.size());
124 EXPECT_EQ(decl_name, decls.front()->getQualifiedNameAsString());
125 auto *record = llvm::cast<clang::RecordDecl>(Val: decls.front());
126 // The class was minimally imported from the scratch AST context.
127 EXPECT_TRUE(record->hasExternalLexicalStorage());
128}
129

source code of lldb/unittests/Expression/ClangExpressionDeclMapTest.cpp