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