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