Skip to content

Commit fc36d22

Browse files
committed
[LLDB] Add optional callback function to TypeMatcher
1 parent e64f8e0 commit fc36d22

File tree

10 files changed

+85
-20
lines changed

10 files changed

+85
-20
lines changed

lldb/include/lldb/DataFormatters/FormatCache.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <map>
1414
#include <mutex>
1515

16+
#include "lldb/DataFormatters/FormatClasses.h"
1617
#include "lldb/Utility/ConstString.h"
1718
#include "lldb/lldb-public.h"
1819

@@ -32,7 +33,7 @@ class FormatCache {
3233
public:
3334
Entry();
3435

35-
template<typename ImplSP> bool IsCached();
36+
template <typename ImplSP> bool IsCached(FormattersMatchData &match_data);
3637
bool IsFormatCached();
3738
bool IsSummaryCached();
3839
bool IsSyntheticCached();
@@ -54,7 +55,8 @@ class FormatCache {
5455
public:
5556
FormatCache() = default;
5657

57-
template <typename ImplSP> bool Get(ConstString type, ImplSP &format_impl_sp);
58+
template <typename ImplSP>
59+
bool Get(FormattersMatchData &match_data, ImplSP &format_impl_sp);
5860
void Set(ConstString type, lldb::TypeFormatImplSP &format_sp);
5961
void Set(ConstString type, lldb::TypeSummaryImplSP &summary_sp);
6062
void Set(ConstString type, lldb::SyntheticChildrenSP &synthetic_sp);

lldb/include/lldb/DataFormatters/FormatClasses.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ class TypeNameSpecifierImpl {
154154
TypeNameSpecifierImpl() = default;
155155

156156
TypeNameSpecifierImpl(llvm::StringRef name,
157-
lldb::FormatterMatchType match_type)
158-
: m_match_type(match_type) {
157+
lldb::FormatterMatchType match_type, uint32_t tag = 0)
158+
: m_match_type(match_type), m_tag(tag) {
159159
m_type.m_type_name = std::string(name);
160160
}
161161

@@ -192,6 +192,8 @@ class TypeNameSpecifierImpl {
192192

193193
bool IsRegex() { return m_match_type == lldb::eFormatterMatchRegex; }
194194

195+
uint32_t GetTag() const { return m_tag; }
196+
195197
private:
196198
lldb::FormatterMatchType m_match_type = lldb::eFormatterMatchExact;
197199
// TODO: Replace this with TypeAndOrName.
@@ -200,6 +202,7 @@ class TypeNameSpecifierImpl {
200202
CompilerType m_compiler_type;
201203
};
202204
TypeOrName m_type;
205+
uint32_t m_tag = 0;
203206

204207
TypeNameSpecifierImpl(const TypeNameSpecifierImpl &) = delete;
205208
const TypeNameSpecifierImpl &

lldb/include/lldb/DataFormatters/FormattersContainer.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class TypeMatcher {
5050
/// - eFormatterMatchCallback: run the function in m_name to decide if a type
5151
/// matches or not.
5252
lldb::FormatterMatchType m_match_type;
53+
uint32_t m_tag = 0;
5354

5455
// if the user tries to add formatters for, say, "struct Foo" those will not
5556
// match any type because of the way we strip qualifiers from typenames this
@@ -86,7 +87,8 @@ class TypeMatcher {
8687
/// name specifier.
8788
TypeMatcher(lldb::TypeNameSpecifierImplSP type_specifier)
8889
: m_name(type_specifier->GetName()),
89-
m_match_type(type_specifier->GetMatchType()) {
90+
m_match_type(type_specifier->GetMatchType()),
91+
m_tag(type_specifier->GetTag()) {
9092
if (m_match_type == lldb::eFormatterMatchRegex)
9193
m_type_name_regex = RegularExpression(type_specifier->GetName());
9294
}
@@ -134,7 +136,7 @@ class TypeMatcher {
134136
/// (lldb) type summary add --summary-string \"A\" -x TypeName
135137
/// (lldb) type summary delete TypeName
136138
bool CreatedBySameMatchString(TypeMatcher other) const {
137-
return GetMatchString() == other.GetMatchString();
139+
return GetMatchString() == other.GetMatchString() && m_tag == other.m_tag;
138140
}
139141
};
140142

@@ -181,6 +183,11 @@ template <typename ValueType> class FormattersContainer {
181183
std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
182184
for (auto &formatter : llvm::reverse(m_map)) {
183185
if (formatter.first.Matches(candidate)) {
186+
if (formatter.second && formatter.second->GetTypeValidator() &&
187+
!formatter.second->GetTypeValidator()(
188+
candidate.GetType().GetCompilerType(false)))
189+
continue;
190+
184191
entry = formatter.second;
185192
return true;
186193
}

lldb/include/lldb/DataFormatters/TypeFormat.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,14 @@ class TypeFormatImpl {
151151

152152
virtual std::string GetDescription() = 0;
153153

154+
CxxTypeValidatorFn *GetTypeValidator() const { return m_validator_fn; }
155+
void SetTypeValidator(CxxTypeValidatorFn *value) { m_validator_fn = value; }
156+
154157
protected:
155158
Flags m_flags;
156159
uint32_t m_my_revision = 0;
157160
uint32_t m_ptr_match_depth = 1;
161+
CxxTypeValidatorFn *m_validator_fn = nullptr;
158162

159163
private:
160164
TypeFormatImpl(const TypeFormatImpl &) = delete;

lldb/include/lldb/DataFormatters/TypeSummary.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <memory>
1616
#include <string>
1717

18+
#include "lldb/DataFormatters/TypeValidator.h"
1819
#include "lldb/lldb-enumerations.h"
1920
#include "lldb/lldb-public.h"
2021

@@ -276,6 +277,9 @@ class TypeSummaryImpl {
276277

277278
uint32_t &GetRevision() { return m_my_revision; }
278279

280+
CxxTypeValidatorFn *GetTypeValidator() const { return m_validator_fn; }
281+
void SetTypeValidator(CxxTypeValidatorFn *value) { m_validator_fn = value; }
282+
279283
typedef std::shared_ptr<TypeSummaryImpl> SharedPointer;
280284

281285
protected:
@@ -288,6 +292,7 @@ class TypeSummaryImpl {
288292
private:
289293
Kind m_kind;
290294
uint32_t m_ptr_match_depth = 1;
295+
CxxTypeValidatorFn *m_validator_fn = nullptr;
291296
TypeSummaryImpl(const TypeSummaryImpl &) = delete;
292297
const TypeSummaryImpl &operator=(const TypeSummaryImpl &) = delete;
293298
};

lldb/include/lldb/DataFormatters/TypeSynthetic.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,17 @@ class SyntheticChildren {
277277

278278
void SetPtrMatchDepth(uint32_t value) { m_ptr_match_depth = value; }
279279

280+
CxxTypeValidatorFn *GetTypeValidator() const { return m_validator_fn; }
281+
void SetTypeValidator(CxxTypeValidatorFn *value) { m_validator_fn = value; }
282+
280283
protected:
281284
uint32_t m_my_revision = 0;
282285
Flags m_flags;
283286
uint32_t m_ptr_match_depth = 1;
284287

285288
private:
289+
CxxTypeValidatorFn *m_validator_fn = nullptr;
290+
286291
SyntheticChildren(const SyntheticChildren &) = delete;
287292
const SyntheticChildren &operator=(const SyntheticChildren &) = delete;
288293
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- TypeValidator.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_DATAFORMATTERS_TYPEVALIDATOR_H
10+
#define LLDB_DATAFORMATTERS_TYPEVALIDATOR_H
11+
12+
#include "lldb/Symbol/CompilerType.h"
13+
14+
namespace lldb_private {
15+
16+
using CxxTypeValidatorFn = bool(const CompilerType &);
17+
18+
} // namespace lldb_private
19+
20+
#endif // LLDB_DATAFORMATTERS_TYPEVALIDATOR_H

lldb/source/DataFormatters/FormatCache.cpp

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,41 @@ void FormatCache::Entry::Set(lldb::SyntheticChildrenSP synthetic_sp) {
5353

5454
namespace lldb_private {
5555

56-
template<> bool FormatCache::Entry::IsCached<lldb::TypeFormatImplSP>() {
57-
return IsFormatCached();
56+
template <typename ImplSP>
57+
static bool passesValidator(const ImplSP &impl_sp,
58+
FormattersMatchData &match_data) {
59+
if (!impl_sp || !impl_sp->GetTypeValidator())
60+
return true;
61+
62+
ValueObject &valobj = match_data.GetValueObject();
63+
lldb::ValueObjectSP valobj_sp = valobj.GetQualifiedRepresentationIfAvailable(
64+
match_data.GetDynamicValueType(), valobj.IsSynthetic());
65+
return valobj_sp && impl_sp->GetTypeValidator()(valobj_sp->GetCompilerType());
66+
}
67+
68+
template <>
69+
bool FormatCache::Entry::IsCached<lldb::TypeFormatImplSP>(
70+
FormattersMatchData &match_data) {
71+
return IsFormatCached() && passesValidator(m_format_sp, match_data);
5872
}
59-
template<> bool FormatCache::Entry::IsCached<lldb::TypeSummaryImplSP> () {
60-
return IsSummaryCached();
73+
template <>
74+
bool FormatCache::Entry::IsCached<lldb::TypeSummaryImplSP>(
75+
FormattersMatchData &match_data) {
76+
return IsSummaryCached() && passesValidator(m_summary_sp, match_data);
6177
}
62-
template<> bool FormatCache::Entry::IsCached<lldb::SyntheticChildrenSP>() {
63-
return IsSyntheticCached();
78+
template <>
79+
bool FormatCache::Entry::IsCached<lldb::SyntheticChildrenSP>(
80+
FormattersMatchData &match_data) {
81+
return IsSyntheticCached() && passesValidator(m_synthetic_sp, match_data);
6482
}
6583

6684
} // namespace lldb_private
6785

6886
template <typename ImplSP>
69-
bool FormatCache::Get(ConstString type, ImplSP &format_impl_sp) {
87+
bool FormatCache::Get(FormattersMatchData &match_data, ImplSP &format_impl_sp) {
7088
std::lock_guard<std::recursive_mutex> guard(m_mutex);
71-
auto entry = m_entries[type];
72-
if (entry.IsCached<ImplSP>()) {
89+
auto entry = m_entries[match_data.GetTypeForCache()];
90+
if (entry.IsCached<ImplSP>(match_data)) {
7391
m_cache_hits++;
7492
entry.Get(format_impl_sp);
7593
return true;
@@ -82,12 +100,13 @@ bool FormatCache::Get(ConstString type, ImplSP &format_impl_sp) {
82100
/// Explicit instantiations for the three types.
83101
/// \{
84102
template bool
85-
FormatCache::Get<lldb::TypeFormatImplSP>(ConstString, lldb::TypeFormatImplSP &);
103+
FormatCache::Get<lldb::TypeFormatImplSP>(FormattersMatchData &,
104+
lldb::TypeFormatImplSP &);
86105
template bool
87-
FormatCache::Get<lldb::TypeSummaryImplSP>(ConstString,
106+
FormatCache::Get<lldb::TypeSummaryImplSP>(FormattersMatchData &,
88107
lldb::TypeSummaryImplSP &);
89108
template bool
90-
FormatCache::Get<lldb::SyntheticChildrenSP>(ConstString,
109+
FormatCache::Get<lldb::SyntheticChildrenSP>(FormattersMatchData &,
91110
lldb::SyntheticChildrenSP &);
92111
/// \}
93112

lldb/source/DataFormatters/FormatManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ ImplSP FormatManager::GetCached(FormattersMatchData &match_data) {
657657
if (match_data.GetTypeForCache()) {
658658
LLDB_LOGF(log, "\n\n" FORMAT_LOG("Looking into cache for type %s"),
659659
match_data.GetTypeForCache().AsCString("<invalid>"));
660-
if (m_format_cache.Get(match_data.GetTypeForCache(), retval_sp)) {
660+
if (m_format_cache.Get(match_data, retval_sp)) {
661661
if (log) {
662662
LLDB_LOGF(log, FORMAT_LOG("Cache search success. Returning."));
663663
LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}",

lldb/source/DataFormatters/LanguageCategory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ bool LanguageCategory::Get(FormattersMatchData &match_data,
4040
return false;
4141

4242
if (match_data.GetTypeForCache()) {
43-
if (m_format_cache.Get(match_data.GetTypeForCache(), retval_sp))
43+
if (m_format_cache.Get(match_data, retval_sp))
4444
return (bool)retval_sp;
4545
}
4646

0 commit comments

Comments
 (0)