LLDB mainline
RustLanguage.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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 "RustLanguage.h"
10
11#include "lldb/Core/Mangled.h"
17#include "lldb/Utility/Log.h"
19
20#include "llvm/Demangle/Demangle.h"
21
22#include <cassert>
23#include <cstdlib>
24
25using namespace llvm;
26using namespace lldb;
27using namespace lldb_private;
28using namespace lldb_private::formatters;
29
31
32/// The mangling scheme that this plugin registered.
33static Mangled::ManglingScheme g_rust_v0_scheme = Mangled::eManglingSchemeNone;
34
40
45
46// Static Functions
48 if (Language::LanguageIsRust(language))
49 return new RustLanguage();
50 return nullptr;
51}
52
53bool RustLanguage::IsSourceFile(StringRef file_path) const {
54 return file_path.ends_with_insensitive(".rs");
55}
56
57std::unique_ptr<Language::TypeScavenger> RustLanguage::GetTypeScavenger() {
58 class RustTypeScavenger : public Language::ImageListTypeScavenger {
59 public:
60 CompilerType AdjustForInclusion(CompilerType &candidate) override {
61 LanguageType lang_type(candidate.GetMinimumLanguage());
62 if (!Language::LanguageIsC(lang_type) &&
63 !Language::LanguageIsRust(lang_type))
64 return CompilerType();
65 if (candidate.IsTypedefType())
66 return candidate.GetTypedefedType();
67 return candidate;
68 }
69 };
70
71 return std::make_unique<RustTypeScavenger>();
72}
73
76 !valobj.IsPointerType())
77 return false;
78 bool canReadValue = true;
79 bool isZero = valobj.GetValueAsUnsigned(0, &canReadValue) == 0;
80 return canReadValue && isZero;
81}
82
86 const DemangleOptions &options) const {
87 assert(scheme == g_rust_v0_scheme);
88 llvm::StringRef mangled_str = mangled.GetStringRef();
89 char *demangled_cstr = llvm::rustDemangle(mangled_str);
90
91 if (Log *log = GetLog(LLDBLog::Demangle)) {
92 if (demangled_cstr && demangled_cstr[0])
93 LLDB_LOG(log, "demangled rustv0: {0} -> \"{1}\"", mangled_str,
94 demangled_cstr);
95 else
96 LLDB_LOG(log, "demangled rustv0: {0} -> error: failed to demangle",
97 mangled_str);
98 }
99
100 DemangledName demangled;
101 if (demangled_cstr) {
102 demangled.name = demangled_cstr;
103 std::free(demangled_cstr);
104 }
105 return demangled;
106}
107
108static void LoadRustFormatters(lldb::TypeCategoryImplSP rust_category_sp) {
109 if (!rust_category_sp)
110 return;
111
112 TypeSummaryImpl::Flags summary_flags;
113 summary_flags.SetCascades(true)
114 .SetSkipPointers(false)
115 .SetSkipReferences(false)
116 .SetDontShowChildren(false)
117 .SetDontShowValue(false)
118 .SetShowMembersOneLiner(false)
119 .SetHideItemNames(false);
120
121 AddCXXSummary(rust_category_sp,
123 "Rust vector summary provider",
124 "^(alloc::([a-z_]+::)+)Vec<.+>$", summary_flags, true);
125
126 SyntheticChildren::Flags synth_flags;
127 synth_flags.SetCascades(true)
128 .SetSkipPointers(false)
129 .SetSkipReferences(false)
130 .SetFrontEndWantsDereference();
131
132 AddCXXSynthetic(rust_category_sp,
134 "Rust vector synthetic children",
135 "^(alloc::([a-z_]+::)+)Vec<.+>$", synth_flags, true);
136}
137
139 static llvm::once_flag g_initialize;
140 static TypeCategoryImplSP g_category;
141
142 llvm::call_once(g_initialize, [this]() -> void {
144 if (g_category)
145 LoadRustFormatters(g_category);
146 });
147 return g_category;
148}
149
150std::vector<lldb::LanguageType>
157
159 lldb::LanguageType category_language,
160 lldb::LanguageType valobj_language) const {
161 return Language::LanguageIsC(valobj_language) ||
162 valobj_language == lldb::eLanguageTypeRust;
163}
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:375
#define LLDB_PLUGIN_DEFINE(PluginName)
static Mangled::ManglingScheme g_rust_v0_scheme
The mangling scheme that this plugin registered.
static void LoadRustFormatters(lldb::TypeCategoryImplSP rust_category_sp)
Generic representation of a type in a programming language.
lldb::LanguageType GetMinimumLanguage()
CompilerType GetTypedefedType() const
If the current object represents a typedef type, get the underlying type.
A uniqued constant string class.
Definition ConstString.h:40
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
static bool GetCategory(llvm::StringRef category, lldb::TypeCategoryImplSP &entry, bool allow_create=true)
static bool LanguageIsRust(lldb::LanguageType language)
Definition Language.cpp:409
static bool LanguageIsC(lldb::LanguageType language)
Definition Language.cpp:354
A class that handles mangled names.
Definition Mangled.h:36
static ManglingScheme RegisterManglingScheme(llvm::ArrayRef< llvm::StringRef > prefixes, lldb::LanguageType language, ManglingSchemeMatcher matches=nullptr)
Register a mangling scheme for language.
Definition Mangled.cpp:163
static void UnregisterManglingScheme(ManglingScheme scheme)
Stop recognizing the names of scheme, a scheme returned by RegisterManglingScheme().
Definition Mangled.cpp:169
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
std::unique_ptr< TypeScavenger > GetTypeScavenger() override
std::vector< lldb::LanguageType > GetFormatterLanguages(lldb::LanguageType language) const override
Returns the languages whose formatters apply to values of language, in the order in which to consult ...
llvm::StringRef GetPluginName() override
PluginInterface protocol.
static Language * CreateInstance(lldb::LanguageType language)
DemangledName DemangleSymbol(ConstString mangled, Mangled::ManglingScheme scheme, const DemangleOptions &options) const override
Demangle mangled, a name mangled with scheme, a scheme that this language registered with Mangled::Re...
bool IsSourceFile(llvm::StringRef file_path) const override
static llvm::StringRef GetPluginNameStatic()
lldb::TypeCategoryImplSP GetFormatters() override
bool IsNilReference(ValueObject &valobj) override
bool IsCategoryApplicable(lldb::LanguageType category_language, lldb::LanguageType valobj_language) const override
Returns whether the formatters in a category for category_language, one of the languages of this plug...
Flags & SetCascades(bool value=true)
Flags & SetCascades(bool value=true)
Definition TypeSummary.h:93
virtual uint64_t GetValueAsUnsigned(uint64_t fail_value, bool *success=nullptr)
lldb::LanguageType GetObjectRuntimeLanguage()
void AddCXXSummary(TypeCategoryImpl::SharedPointer category_sp, CXXFunctionSummaryFormat::Callback funct, const char *description, llvm::StringRef type_name, TypeSummaryImpl::Flags flags, bool regex=false)
Add a summary that is implemented by a C++ callback.
void AddCXXSynthetic(TypeCategoryImpl::SharedPointer category_sp, CXXSyntheticChildren::CreateFrontEndCallback generator, const char *description, llvm::StringRef type_name, ScriptedSyntheticChildren::Flags flags, bool regex=false)
Add a synthetic that is implemented by a C++ callback.
bool ContainerSizeSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
SyntheticChildrenFrontEnd * RustVectorSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp)
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:338
LanguageType
Programming language type.
@ eLanguageTypeRust
Rust.
@ eLanguageTypeObjC
Objective-C.
@ eLanguageTypeC_plus_plus
ISO C++:1998.
std::shared_ptr< lldb_private::TypeCategoryImpl > TypeCategoryImplSP
How to demangle a name in DemangleSymbol().
Definition Language.h:359
A demangled name and, if requested, where its parts begin and end.
Definition Language.h:365
std::string name
Empty if the name couldn't be demangled.
Definition Language.h:367