Raphael Isemann 4d489e9f91 Reland [lldb] Unify type name matching in FormattersContainer II
This was originally reverted because the m_valid member in TypeMatcher was
unused in builds with disabled asserts. Now the member is gone and the default
constructor is deleted (thanks Eric for the idea!).

Summary:

FormattersContainer stores LLDB's formatters. It's implemented as a templated
map-like data structures that supports any kind of value type and only allows
ConstString and RegularExpression as the key types. The keys are used for
matching type names (e.g., the ConstString key `std::vector` matches the type
with the same name while RegularExpression keys match any type where the
RegularExpression instance matches).

The fact that a single FormattersContainer can only match either by string
comparison or regex matching (depending on the KeyType) causes us to always have
two FormatterContainer instances in all the formatting code. This also leads to
us having every type name matching logic in LLDB twice. For example,
TypeCategory has to implement every method twice (one string matching one, one
regex matching one).

This patch changes FormattersContainer to instead have a single `TypeMatcher`
key that wraps the logic for string-based and regex-based type matching and is
now the only possible KeyType for the FormattersContainer. This means that a
single FormattersContainer can now match types with both regex and string
comparison.

To summarize the changes in this patch:
* Remove all the `*_Impl` methods from `FormattersContainer`
* Instead call the FormatMap functions from `FormattersContainer` with a
  `TypeMatcher` type that does the respective matching.
* Replace `ConstString` with `TypeMatcher` in the few places that directly
  interact with `FormattersContainer`.

I'm working on some follow up patches that I split up because they deserve their
own review:

* Unify FormatMap and FormattersContainer (they are nearly identical now).
* Delete the duplicated half of all the type matching code that can now use one
  interface.
* Propagate TypeMatcher through all the formatter code interfaces instead of
  always offering two functions for everything.

There is one ugly design part that I couldn't get rid of yet and that is that we
have to support getting back the string used to construct a `TypeMatcher` later
on. The reason for this is that LLDB only supports referencing existing type
matchers by just typing their respective input string again (without even
supplying if it's a regex or not).

Reviewers: davide, mib

Reviewed By: mib

Subscribers: mgorny, JDevlieghere

Differential Revision: https://reviews.llvm.org/D84151
2020-07-23 18:17:42 +02:00

125 lines
4.0 KiB
C++

//===-- DataVisualization.h -------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLDB_DATAFORMATTERS_DATAVISUALIZATION_H
#define LLDB_DATAFORMATTERS_DATAVISUALIZATION_H
#include "lldb/DataFormatters/FormatClasses.h"
#include "lldb/DataFormatters/FormatManager.h"
#include "lldb/Utility/ConstString.h"
namespace lldb_private {
// this class is the high-level front-end of LLDB Data Visualization code in
// FormatManager.h/cpp is the low-level implementation of this feature clients
// should refer to this class as the entry-point into the data formatters
// unless they have a good reason to bypass this and go to the backend
class DataVisualization {
public:
// use this call to force the FM to consider itself updated even when there
// is no apparent reason for that
static void ForceUpdate();
static uint32_t GetCurrentRevision();
static bool ShouldPrintAsOneLiner(ValueObject &valobj);
static lldb::TypeFormatImplSP GetFormat(ValueObject &valobj,
lldb::DynamicValueType use_dynamic);
static lldb::TypeFormatImplSP
GetFormatForType(lldb::TypeNameSpecifierImplSP type_sp);
static lldb::TypeSummaryImplSP
GetSummaryFormat(ValueObject &valobj, lldb::DynamicValueType use_dynamic);
static lldb::TypeSummaryImplSP
GetSummaryForType(lldb::TypeNameSpecifierImplSP type_sp);
static lldb::TypeFilterImplSP
GetFilterForType(lldb::TypeNameSpecifierImplSP type_sp);
static lldb::ScriptedSyntheticChildrenSP
GetSyntheticForType(lldb::TypeNameSpecifierImplSP type_sp);
static lldb::SyntheticChildrenSP
GetSyntheticChildren(ValueObject &valobj, lldb::DynamicValueType use_dynamic);
static bool
AnyMatches(ConstString type_name,
TypeCategoryImpl::FormatCategoryItems items =
TypeCategoryImpl::ALL_ITEM_TYPES,
bool only_enabled = true, const char **matching_category = nullptr,
TypeCategoryImpl::FormatCategoryItems *matching_type = nullptr);
class NamedSummaryFormats {
public:
static bool GetSummaryFormat(ConstString type,
lldb::TypeSummaryImplSP &entry);
static void Add(ConstString type,
const lldb::TypeSummaryImplSP &entry);
static bool Delete(ConstString type);
static void Clear();
static void ForEach(std::function<bool(const TypeMatcher &,
const lldb::TypeSummaryImplSP &)>
callback);
static uint32_t GetCount();
};
class Categories {
public:
static bool GetCategory(ConstString category,
lldb::TypeCategoryImplSP &entry,
bool allow_create = true);
static bool GetCategory(lldb::LanguageType language,
lldb::TypeCategoryImplSP &entry);
static void Add(ConstString category);
static bool Delete(ConstString category);
static void Clear();
static void Clear(ConstString category);
static void Enable(ConstString category,
TypeCategoryMap::Position = TypeCategoryMap::Default);
static void Enable(lldb::LanguageType lang_type);
static void Disable(ConstString category);
static void Disable(lldb::LanguageType lang_type);
static void Enable(const lldb::TypeCategoryImplSP &category,
TypeCategoryMap::Position = TypeCategoryMap::Default);
static void Disable(const lldb::TypeCategoryImplSP &category);
static void EnableStar();
static void DisableStar();
static void ForEach(TypeCategoryMap::ForEachCallback callback);
static uint32_t GetCount();
static lldb::TypeCategoryImplSP GetCategoryAtIndex(size_t);
};
};
} // namespace lldb_private
#endif // LLDB_DATAFORMATTERS_DATAVISUALIZATION_H