Skip to content

Commit e55e378

Browse files
committed
Add test
1 parent 8b04ffa commit e55e378

File tree

4 files changed

+154
-12
lines changed

4 files changed

+154
-12
lines changed

lldb/include/lldb/Core/Module.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,6 @@ class Module : public std::enable_shared_from_this<Module>,
320320
/// If the function is an inlined function, it will have a block,
321321
/// representing the inlined function, and the function will be the
322322
/// containing function. If it is not inlined, then the block will be NULL.
323-
///
324-
/// \param[in] lookup_infos
325-
/// The vector of lookup infos of the function we are looking for.
326-
///
327-
/// \param[out] sc_list
328-
/// A symbol context list that gets filled in with all of the
329-
/// matches.
330323
void FindFunctions(const std::vector<LookupInfo> &lookup_infos,
331324
const CompilerDeclContext &parent_decl_ctx,
332325
const ModuleFunctionSearchOptions &options,
@@ -945,7 +938,7 @@ class Module : public std::enable_shared_from_this<Module>,
945938
/// eFunctionNameTypeMethod, eFunctionNameTypeAuto). Multiple types
946939
/// can be combined with bitwise OR.
947940
///
948-
/// \param[in] requested_lang_type
941+
/// \param[in] lang_type
949942
/// The language to create lookups for. If eLanguageTypeUnknown is
950943
/// passed, creates one LookupInfo for each language plugin currently
951944
/// available in LLDB. If a specific language is provided, creates only
@@ -955,7 +948,7 @@ class Module : public std::enable_shared_from_this<Module>,
955948
/// A vector of LookupInfo objects, one per relevant language.
956949
static std::vector<LookupInfo>
957950
MakeLookupInfos(ConstString name, lldb::FunctionNameType name_type_mask,
958-
lldb::LanguageType requested_lang_type);
951+
lldb::LanguageType lang_type);
959952

960953
ConstString GetName() const { return m_name; }
961954

lldb/source/Core/Module.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -699,10 +699,10 @@ Module::LookupInfo::LookupInfo(ConstString name,
699699
std::vector<Module::LookupInfo>
700700
Module::LookupInfo::MakeLookupInfos(ConstString name,
701701
lldb::FunctionNameType name_type_mask,
702-
lldb::LanguageType requested_lang_type) {
702+
lldb::LanguageType lang_type) {
703703
std::vector<LanguageType> lang_types;
704-
if (requested_lang_type != eLanguageTypeUnknown) {
705-
lang_types.push_back(requested_lang_type);
704+
if (lang_type != eLanguageTypeUnknown) {
705+
lang_types.push_back(lang_type);
706706
} else {
707707
// If the language type was not specified, look up in every language
708708
// available.

lldb/unittests/Core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ add_lldb_unittest(LLDBCoreTests
99
MangledTest.cpp
1010
ModuleListTest.cpp
1111
ModuleSpecTest.cpp
12+
ModuleTest.cpp
1213
PluginManagerTest.cpp
1314
ProgressReportTest.cpp
1415
RichManglingContextTest.cpp

lldb/unittests/Core/ModuleTest.cpp

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
//===-- ModuleTest.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 "lldb/Core/Module.h"
10+
#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
11+
#include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
12+
#include "Plugins/Platform/Linux/PlatformLinux.h"
13+
#include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
14+
#include "TestingSupport/SubsystemRAII.h"
15+
#include "TestingSupport/TestUtilities.h"
16+
#include "lldb/Core/Debugger.h"
17+
#include "lldb/Core/PluginManager.h"
18+
#include "lldb/Host/FileSystem.h"
19+
#include "lldb/Host/HostInfo.h"
20+
#include "lldb/Target/Language.h"
21+
#include "lldb/lldb-enumerations.h"
22+
#include "gtest/gtest.h"
23+
#include <optional>
24+
25+
using namespace lldb;
26+
using namespace lldb_private;
27+
28+
// Test that Module::FindFunctions correctly finds C++ mangled symbols
29+
// even when multiple language plugins are registered.
30+
TEST(ModuleTest, FindFunctionsCppMangledName) {
31+
// Create a mock language. The point of this language is to return something
32+
// in GetFunctionNameInfo that would interfere with the C++ language plugin,
33+
// were they sharing the same LookupInfo.
34+
class MockLanguageWithBogusLookupInfo : public Language {
35+
public:
36+
MockLanguageWithBogusLookupInfo() = default;
37+
~MockLanguageWithBogusLookupInfo() override = default;
38+
39+
lldb::LanguageType GetLanguageType() const override {
40+
// The language here doesn't really matter, it just has to be something
41+
// that is not C/C++/ObjC.
42+
return lldb::eLanguageTypeSwift;
43+
}
44+
45+
llvm::StringRef GetPluginName() override { return "mock-bogus-language"; }
46+
47+
bool IsSourceFile(llvm::StringRef file_path) const override {
48+
return file_path.ends_with(".swift");
49+
}
50+
51+
std::pair<lldb::FunctionNameType, std::optional<ConstString>>
52+
GetFunctionNameInfo(ConstString name) const override {
53+
// Say that every function is a selector.
54+
return {lldb::eFunctionNameTypeSelector, ConstString("BOGUS_BASENAME")};
55+
}
56+
57+
static void Initialize() {
58+
PluginManager::RegisterPlugin(GetPluginNameStatic(), "Mock Language",
59+
CreateInstance);
60+
}
61+
62+
static void Terminate() { PluginManager::UnregisterPlugin(CreateInstance); }
63+
64+
static lldb_private::Language *CreateInstance(lldb::LanguageType language) {
65+
if (language == lldb::eLanguageTypeSwift)
66+
return new MockLanguageWithBogusLookupInfo();
67+
return nullptr;
68+
}
69+
70+
static llvm::StringRef GetPluginNameStatic() {
71+
return "mock-bogus-language";
72+
}
73+
};
74+
SubsystemRAII<FileSystem, HostInfo, ObjectFileELF, SymbolFileSymtab,
75+
CPlusPlusLanguage, platform_linux::PlatformLinux,
76+
MockLanguageWithBogusLookupInfo>
77+
subsystems;
78+
79+
ArchSpec arch("x86_64-pc-linux");
80+
Platform::SetHostPlatform(
81+
platform_linux::PlatformLinux::CreateInstance(true, &arch));
82+
83+
std::call_once(TestUtilities::g_debugger_initialize_flag,
84+
[]() { Debugger::Initialize(nullptr); });
85+
86+
// Create a simple ELF module with std::vector::size() as the only symbol.
87+
auto ExpectedFile = TestFile::fromYaml(R"(
88+
--- !ELF
89+
FileHeader:
90+
Class: ELFCLASS64
91+
Data: ELFDATA2LSB
92+
Type: ET_DYN
93+
Machine: EM_X86_64
94+
Sections:
95+
- Name: .text
96+
Type: SHT_PROGBITS
97+
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
98+
Address: 0x1000
99+
AddressAlign: 0x10
100+
Size: 0x100
101+
Symbols:
102+
- Name: _ZNSt6vectorIiE4sizeEv
103+
Type: STT_FUNC
104+
Section: .text
105+
Value: 0x1030
106+
Size: 0x20
107+
...
108+
)");
109+
ASSERT_THAT_EXPECTED(ExpectedFile, llvm::Succeeded());
110+
111+
DebuggerSP debugger_sp = Debugger::CreateInstance();
112+
ASSERT_TRUE(debugger_sp);
113+
114+
TargetSP target_sp;
115+
PlatformSP platform_sp;
116+
117+
Status error = debugger_sp->GetTargetList().CreateTarget(
118+
*debugger_sp, "", arch, eLoadDependentsNo, platform_sp, target_sp);
119+
ASSERT_TRUE(error.Success());
120+
ASSERT_TRUE(target_sp);
121+
122+
auto module_sp = std::make_shared<Module>(ExpectedFile->moduleSpec());
123+
target_sp->GetImages().Append(module_sp);
124+
125+
// Verify both C++ and our mock language are registered.
126+
Language *cpp_lang = Language::FindPlugin(lldb::eLanguageTypeC_plus_plus);
127+
Language *mock_lang = Language::FindPlugin(lldb::eLanguageTypeSwift);
128+
ASSERT_NE(cpp_lang, nullptr) << "C++ language plugin should be registered";
129+
ASSERT_NE(mock_lang, nullptr)
130+
<< "Mock Swift language plugin should be registered";
131+
132+
ModuleFunctionSearchOptions function_options;
133+
function_options.include_symbols = true;
134+
135+
ConstString symbol_name("_ZNSt6vectorIiE4sizeEv");
136+
SymbolContextList results;
137+
module_sp->FindFunctions(symbol_name, CompilerDeclContext(),
138+
eFunctionNameTypeAuto, function_options, results);
139+
140+
// Assert that we found one symbol.
141+
ASSERT_EQ(results.GetSize(), 1u);
142+
143+
auto result = results[0];
144+
auto name = result.GetFunctionName();
145+
// Assert that the symbol we found is what we expected.
146+
ASSERT_EQ(name, "std::vector<int>::size()");
147+
ASSERT_EQ(result.GetLanguage(), eLanguageTypeC_plus_plus);
148+
}

0 commit comments

Comments
 (0)