Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:46

0001 //===-- FormatClasses.h -----------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_DATAFORMATTERS_FORMATCLASSES_H
0010 #define LLDB_DATAFORMATTERS_FORMATCLASSES_H
0011 
0012 #include <functional>
0013 #include <memory>
0014 #include <string>
0015 #include <vector>
0016 
0017 #include "lldb/DataFormatters/TypeFormat.h"
0018 #include "lldb/DataFormatters/TypeSummary.h"
0019 #include "lldb/DataFormatters/TypeSynthetic.h"
0020 #include "lldb/Interpreter/ScriptInterpreter.h"
0021 #include "lldb/Symbol/CompilerType.h"
0022 #include "lldb/Symbol/Type.h"
0023 #include "lldb/lldb-enumerations.h"
0024 #include "lldb/lldb-public.h"
0025 
0026 namespace lldb_private {
0027 
0028 class HardcodedFormatters {
0029 public:
0030   template <typename FormatterType>
0031   using HardcodedFormatterFinder =
0032       std::function<typename FormatterType::SharedPointer(
0033           lldb_private::ValueObject &, lldb::DynamicValueType,
0034           FormatManager &)>;
0035 
0036   template <typename FormatterType>
0037   using HardcodedFormatterFinders =
0038       std::vector<HardcodedFormatterFinder<FormatterType>>;
0039 
0040   typedef HardcodedFormatterFinders<TypeFormatImpl> HardcodedFormatFinder;
0041   typedef HardcodedFormatterFinders<TypeSummaryImpl> HardcodedSummaryFinder;
0042   typedef HardcodedFormatterFinders<SyntheticChildren> HardcodedSyntheticFinder;
0043 };
0044 
0045 class FormattersMatchCandidate {
0046 public:
0047   // Contains flags to indicate how this candidate was generated (e.g. if
0048   // typedefs were stripped, or pointers were skipped). These are later compared
0049   // to flags in formatters to confirm a string match.
0050   struct Flags {
0051     bool stripped_pointer = false;
0052     bool stripped_reference = false;
0053     bool stripped_typedef = false;
0054 
0055     // Returns a copy of this with the "stripped pointer" flag set.
0056     Flags WithStrippedPointer() {
0057       Flags result(*this);
0058       result.stripped_pointer = true;
0059       return result;
0060     }
0061 
0062     // Returns a copy of this with the "stripped reference" flag set.
0063     Flags WithStrippedReference() {
0064       Flags result(*this);
0065       result.stripped_reference = true;
0066       return result;
0067     }
0068 
0069     // Returns a copy of this with the "stripped typedef" flag set.
0070     Flags WithStrippedTypedef() {
0071       Flags result(*this);
0072       result.stripped_typedef = true;
0073       return result;
0074     }
0075   };
0076 
0077   FormattersMatchCandidate(ConstString name,
0078                            ScriptInterpreter *script_interpreter, TypeImpl type,
0079                            Flags flags)
0080       : m_type_name(name), m_script_interpreter(script_interpreter),
0081         m_type(type), m_flags(flags) {}
0082 
0083   ~FormattersMatchCandidate() = default;
0084 
0085   ConstString GetTypeName() const { return m_type_name; }
0086 
0087   TypeImpl GetType() const { return m_type; }
0088 
0089   ScriptInterpreter *GetScriptInterpreter() const {
0090     return m_script_interpreter;
0091   }
0092 
0093   bool DidStripPointer() const { return m_flags.stripped_pointer; }
0094 
0095   bool DidStripReference() const { return m_flags.stripped_reference; }
0096 
0097   bool DidStripTypedef() const { return m_flags.stripped_typedef; }
0098 
0099   template <class Formatter>
0100   bool IsMatch(const std::shared_ptr<Formatter> &formatter_sp) const {
0101     if (!formatter_sp)
0102       return false;
0103     if (formatter_sp->Cascades() == false && DidStripTypedef())
0104       return false;
0105     if (formatter_sp->SkipsPointers() && DidStripPointer())
0106       return false;
0107     if (formatter_sp->SkipsReferences() && DidStripReference())
0108       return false;
0109     return true;
0110   }
0111 
0112 private:
0113   ConstString m_type_name;
0114   // If a formatter provides a matching callback function, we need the script
0115   // interpreter and the type object (as an argument to the callback).
0116   ScriptInterpreter *m_script_interpreter;
0117   TypeImpl m_type;
0118   Flags m_flags;
0119 };
0120 
0121 typedef std::vector<FormattersMatchCandidate> FormattersMatchVector;
0122 typedef std::vector<lldb::LanguageType> CandidateLanguagesVector;
0123 
0124 class FormattersMatchData {
0125 public:
0126   FormattersMatchData(ValueObject &, lldb::DynamicValueType);
0127 
0128   FormattersMatchVector GetMatchesVector();
0129 
0130   ConstString GetTypeForCache();
0131 
0132   CandidateLanguagesVector GetCandidateLanguages();
0133 
0134   ValueObject &GetValueObject();
0135 
0136   lldb::DynamicValueType GetDynamicValueType();
0137 
0138 private:
0139   ValueObject &m_valobj;
0140   lldb::DynamicValueType m_dynamic_value_type;
0141   std::pair<FormattersMatchVector, bool> m_formatters_match_vector;
0142   ConstString m_type_for_cache;
0143   CandidateLanguagesVector m_candidate_languages;
0144 };
0145 
0146 class TypeNameSpecifierImpl {
0147 public:
0148   TypeNameSpecifierImpl() = default;
0149 
0150   TypeNameSpecifierImpl(llvm::StringRef name,
0151                         lldb::FormatterMatchType match_type)
0152       : m_match_type(match_type) {
0153     m_type.m_type_name = std::string(name);
0154   }
0155 
0156   // if constructing with a given type, we consider that a case of exact match.
0157   TypeNameSpecifierImpl(lldb::TypeSP type)
0158       : m_match_type(lldb::eFormatterMatchExact) {
0159     if (type) {
0160       m_type.m_type_name = std::string(type->GetName().GetStringRef());
0161       m_type.m_compiler_type = type->GetForwardCompilerType();
0162     }
0163   }
0164 
0165   TypeNameSpecifierImpl(CompilerType type)
0166       : m_match_type(lldb::eFormatterMatchExact) {
0167     if (type.IsValid()) {
0168       m_type.m_type_name.assign(type.GetTypeName().GetCString());
0169       m_type.m_compiler_type = type;
0170     }
0171   }
0172 
0173   const char *GetName() {
0174     if (m_type.m_type_name.size())
0175       return m_type.m_type_name.c_str();
0176     return nullptr;
0177   }
0178 
0179   CompilerType GetCompilerType() {
0180     if (m_type.m_compiler_type.IsValid())
0181       return m_type.m_compiler_type;
0182     return CompilerType();
0183   }
0184 
0185   lldb::FormatterMatchType GetMatchType() { return m_match_type; }
0186 
0187   bool IsRegex() { return m_match_type == lldb::eFormatterMatchRegex; }
0188 
0189 private:
0190   lldb::FormatterMatchType m_match_type = lldb::eFormatterMatchExact;
0191   // TODO: Replace this with TypeAndOrName.
0192   struct TypeOrName {
0193     std::string m_type_name;
0194     CompilerType m_compiler_type;
0195   };
0196   TypeOrName m_type;
0197 
0198   TypeNameSpecifierImpl(const TypeNameSpecifierImpl &) = delete;
0199   const TypeNameSpecifierImpl &
0200   operator=(const TypeNameSpecifierImpl &) = delete;
0201 };
0202 
0203 } // namespace lldb_private
0204 
0205 #endif // LLDB_DATAFORMATTERS_FORMATCLASSES_H