Skip to content

Commit 1a588e5

Browse files
authored
Merge pull request #9745 from augusto2112/orig-defined-in-imported-decl
[lldb] Introduce an ImportedDeclaration and [lldb] Handle @_originallyDefinedIn [main]
2 parents 9f2ac20 + ce1a9d5 commit 1a588e5

19 files changed

+597
-4
lines changed

lldb/include/lldb/Core/Module.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "lldb/Core/Address.h"
1313
#include "lldb/Core/ModuleList.h"
1414
#include "lldb/Core/ModuleSpec.h"
15+
#include "lldb/Symbol/ImportedDeclaration.h"
1516
#include "lldb/Symbol/ObjectFile.h"
1617
#include "lldb/Symbol/SymbolContextScope.h"
1718
#include "lldb/Symbol/TypeSystem.h"
@@ -437,6 +438,21 @@ class Module : public std::enable_shared_from_this<Module>,
437438
/// TypeMap::InsertUnique(...).
438439
void FindTypes(const TypeQuery &query, TypeResults &results);
439440

441+
/// Finds imported declarations whose name match \p name.
442+
///
443+
/// \param[in] name
444+
/// The name to search the imported declaration by.
445+
///
446+
/// \param[in] results
447+
/// Any matching types will be populated into the \a results object.
448+
///
449+
/// \param[in] find_one
450+
/// If set to true, the search will stop after the first imported
451+
/// declaration is found.
452+
void FindImportedDeclarations(ConstString name,
453+
std::vector<ImportedDeclaration> &results,
454+
bool find_one);
455+
440456
/// Get const accessor for the module architecture.
441457
///
442458
/// \return

lldb/include/lldb/Core/ModuleList.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "lldb/Core/Address.h"
1313
#include "lldb/Core/ModuleSpec.h"
1414
#include "lldb/Core/UserSettingsController.h"
15+
#include "lldb/Symbol/ImportedDeclaration.h"
1516
#include "lldb/Utility/FileSpec.h"
1617
#include "lldb/Utility/Iterable.h"
1718
#include "lldb/Utility/Status.h"
@@ -397,6 +398,25 @@ class ModuleList {
397398
void FindTypes(Module *search_first, const TypeQuery &query,
398399
lldb_private::TypeResults &results) const;
399400

401+
/// Finds imported declarations whose name match \p name.
402+
///
403+
/// \param[in] search_first
404+
/// If non-null, this module will be searched before any other
405+
/// modules.
406+
///
407+
/// \param[in] name
408+
/// The name to search the imported declaration by.
409+
///
410+
/// \param[in] results
411+
/// Any matching types will be populated into the \a results object.
412+
///
413+
/// \param[in] find_one
414+
/// If set to true, the search will stop after the first imported
415+
/// declaration is found.
416+
void FindImportedDeclarations(Module *search_first, ConstString name,
417+
std::vector<ImportedDeclaration> &results,
418+
bool find_one) const;
419+
400420
bool FindSourceFile(const FileSpec &orig_spec, FileSpec &new_spec) const;
401421

402422
/// Find addresses by file/line
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===-- ImportedDeclaration.h -----------------------------------*- C++ -*-===//
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+
#ifndef LLDB_SYMBOL_IMPORTED_DECLARATION_H
10+
#define LLDB_SYMBOL_IMPORTED_DECLARATION_H
11+
12+
#include "lldb/Utility/ConstString.h"
13+
#include "lldb/Utility/UserID.h"
14+
15+
namespace lldb_private {
16+
17+
struct ImportedDeclaration : public UserID {
18+
19+
ImportedDeclaration(lldb::user_id_t uid, ConstString name,
20+
SymbolFile *symbol_file)
21+
: UserID(uid), m_name(name), m_symbol_file(symbol_file) {}
22+
23+
ConstString GetName() const { return m_name; }
24+
25+
std::vector<lldb_private::CompilerContext> GetDeclContext() const;
26+
27+
private:
28+
ConstString m_name;
29+
SymbolFile *m_symbol_file = nullptr;
30+
};
31+
32+
} // namespace lldb_private
33+
34+
#endif // LLDB_SYMBOL_IMPORTED_DECLARATION_H

lldb/include/lldb/Symbol/SymbolFile.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "lldb/Symbol/CompilerType.h"
1919
#include "lldb/Symbol/Function.h"
2020
#include "lldb/Symbol/SourceModule.h"
21+
#include "lldb/Symbol/Symbol.h"
22+
#include "lldb/Symbol/SymbolContext.h"
2123
#include "lldb/Symbol/Type.h"
2224
#include "lldb/Symbol/TypeList.h"
2325
#include "lldb/Symbol/TypeSystem.h"
@@ -305,6 +307,21 @@ class SymbolFile : public PluginInterface {
305307
bool include_inlines, SymbolContextList &sc_list);
306308
virtual void FindFunctions(const RegularExpression &regex,
307309
bool include_inlines, SymbolContextList &sc_list);
310+
/// Finds imported declarations whose name match \p name.
311+
///
312+
/// \param[in] name
313+
/// The name to search the imported declaration by.
314+
///
315+
/// \param[in] results
316+
/// Any matching types will be populated into the \a results object.
317+
///
318+
/// \param[in] find_one
319+
/// If set to true, the search will stop after the first imported
320+
/// declaration is found.
321+
virtual void
322+
FindImportedDeclaration(ConstString name,
323+
std::vector<ImportedDeclaration> &declarations,
324+
bool find_one) {}
308325

309326
/// Find types using a type-matching object that contains all search
310327
/// parameters.

lldb/source/Core/Module.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,12 @@ void Module::FindTypes(const TypeQuery &query, TypeResults &results) {
10111011
if (SymbolFile *symbols = GetSymbolFile())
10121012
symbols->FindTypes(query, results);
10131013
}
1014+
void Module::FindImportedDeclarations(ConstString name,
1015+
std::vector<ImportedDeclaration> &results,
1016+
bool find_one) {
1017+
if (SymbolFile *symbols = GetSymbolFile())
1018+
symbols->FindImportedDeclaration(name, results, find_one);
1019+
}
10141020

10151021
static Debugger::DebuggerList
10161022
DebuggersOwningModuleRequestingInterruption(Module &module) {

lldb/source/Core/ModuleList.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,24 @@ void ModuleList::FindTypes(Module *search_first, const TypeQuery &query,
831831
}
832832
}
833833

834+
void ModuleList::FindImportedDeclarations(
835+
Module *search_first, ConstString name,
836+
std::vector<ImportedDeclaration> &results, bool find_one) const {
837+
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
838+
if (search_first) {
839+
search_first->FindImportedDeclarations(name, results, find_one);
840+
if (find_one && !results.empty())
841+
return;
842+
}
843+
for (const auto &module_sp : m_modules) {
844+
if (search_first != module_sp.get()) {
845+
module_sp->FindImportedDeclarations(name, results, find_one);
846+
}
847+
if (find_one && !results.empty())
848+
return;
849+
}
850+
}
851+
834852
bool ModuleList::FindSourceFile(const FileSpec &orig_spec,
835853
FileSpec &new_spec) const {
836854
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2677,6 +2677,22 @@ void SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
26772677
});
26782678
}
26792679

2680+
void SymbolFileDWARF::FindImportedDeclaration(
2681+
ConstString name, std::vector<ImportedDeclaration> &sc_list,
2682+
bool find_one) {
2683+
llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2684+
m_index->GetNamespaces(name, [&](DWARFDIE die) {
2685+
if (die.Tag() != llvm::dwarf::DW_TAG_imported_declaration)
2686+
return true;
2687+
2688+
if (name != die.GetName())
2689+
return true;
2690+
2691+
sc_list.emplace_back(die.GetID(), name, this);
2692+
return !find_one;
2693+
});
2694+
}
2695+
26802696
void SymbolFileDWARF::GetMangledNamesForFunction(
26812697
const std::string &scope_qualified_name,
26822698
std::vector<ConstString> &mangled_names) {

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ class SymbolFileDWARF : public SymbolFileCommon {
195195
void FindFunctions(const RegularExpression &regex, bool include_inlines,
196196
SymbolContextList &sc_list) override;
197197

198+
void FindImportedDeclaration(ConstString name,
199+
std::vector<ImportedDeclaration> &sc_list,
200+
bool find_one) override;
201+
198202
void
199203
GetMangledNamesForFunction(const std::string &scope_qualified_name,
200204
std::vector<ConstString> &mangled_names) override;

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,17 @@ void SymbolFileDWARFDebugMap::FindFunctions(const RegularExpression &regex,
11221122
});
11231123
}
11241124

1125+
void SymbolFileDWARFDebugMap::FindImportedDeclaration(
1126+
ConstString name, std::vector<ImportedDeclaration> &declarations,
1127+
bool find_one) {
1128+
ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) {
1129+
oso_dwarf->FindImportedDeclaration(name, declarations, find_one);
1130+
if (find_one && !declarations.empty())
1131+
return IterationAction::Stop;
1132+
return IterationAction::Continue;
1133+
});
1134+
}
1135+
11251136
void SymbolFileDWARFDebugMap::GetTypes(SymbolContextScope *sc_scope,
11261137
lldb::TypeClass type_mask,
11271138
TypeList &type_list) {

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ class SymbolFileDWARFDebugMap : public SymbolFileCommon {
123123
bool include_inlines, SymbolContextList &sc_list) override;
124124
void FindFunctions(const RegularExpression &regex, bool include_inlines,
125125
SymbolContextList &sc_list) override;
126+
void FindImportedDeclaration(ConstString name,
127+
std::vector<ImportedDeclaration> &declarations,
128+
bool find_one) override;
126129
void FindTypes(const lldb_private::TypeQuery &match,
127130
lldb_private::TypeResults &results) override;
128131
CompilerDeclContext FindNamespace(ConstString name,

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4784,6 +4784,16 @@ SwiftASTContext::ReconstructType(ConstString mangled_typename) {
47844784
.getPointer();
47854785
assert(!found_type || &found_type->getASTContext() == *ast_ctx);
47864786

4787+
// This type might have been been found in reflection and annotated with
4788+
// @_originallyDefinedIn. The compiler emits a typelias for these type
4789+
// pointing them back to the types with the real module name.
4790+
if (!found_type) {
4791+
auto adjusted =
4792+
GetTypeSystemSwiftTypeRef()->AdjustTypeForOriginallyDefinedInModule(
4793+
mangled_typename);
4794+
found_type =
4795+
swift::Demangle::getTypeForMangling(**ast_ctx, adjusted).getPointer();
4796+
}
47874797
// Objective-C classes sometimes have private subclasses that are invisible
47884798
// to the Swift compiler because they are declared and defined in a .m file.
47894799
// If we can't reconstruct an ObjC type, walk up the type hierarchy until we

lldb/source/Plugins/TypeSystem/Swift/SwiftDemangle.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,23 @@ NodeAtPath(swift::Demangle::NodePointer root,
6060
return ChildAtPath(root, kind_path.drop_front());
6161
}
6262

63-
/// \return the child of the \p Type node.
64-
static swift::Demangle::NodePointer GetType(swift::Demangle::NodePointer n) {
63+
/// \return the child of the TypeMangling node.
64+
static swift::Demangle::NodePointer
65+
GetTypeMangling(swift::Demangle::NodePointer n) {
6566
using namespace swift::Demangle;
6667
if (!n || n->getKind() != Node::Kind::Global)
6768
return nullptr;
6869
n = n->getFirstChild();
6970
if (!n || n->getKind() != Node::Kind::TypeMangling || !n->hasChildren())
7071
return nullptr;
7172
n = n->getFirstChild();
73+
return n;
74+
}
75+
76+
/// \return the child of the \p Type node.
77+
static swift::Demangle::NodePointer GetType(swift::Demangle::NodePointer n) {
78+
using namespace swift::Demangle;
79+
n = GetTypeMangling(n);
7280
if (!n || n->getKind() != Node::Kind::Type || !n->hasChildren())
7381
return nullptr;
7482
n = n->getFirstChild();
@@ -81,6 +89,14 @@ GetDemangledType(swift::Demangle::Demangler &dem, llvm::StringRef name) {
8189
return GetType(dem.demangleSymbol(name));
8290
}
8391

92+
/// Demangle a mangled type name and return the child of the \p TypeMangling
93+
/// node.
94+
inline swift::Demangle::NodePointer
95+
GetDemangledTypeMangling(swift::Demangle::Demangler &dem,
96+
llvm::StringRef name) {
97+
return GetTypeMangling(dem.demangleSymbol(name));
98+
}
99+
84100
/// Wrap node in Global/TypeMangling/Type.
85101
static swift::Demangle::NodePointer
86102
mangleType(swift::Demangle::Demangler &dem,

0 commit comments

Comments
 (0)