diff options
Diffstat (limited to 'include/llvm/DebugInfo/DWARF/DWARFFormValue.h')
| -rw-r--r-- | include/llvm/DebugInfo/DWARF/DWARFFormValue.h | 178 | 
1 files changed, 170 insertions, 8 deletions
| diff --git a/include/llvm/DebugInfo/DWARF/DWARFFormValue.h b/include/llvm/DebugInfo/DWARF/DWARFFormValue.h index 1b7659dfb04a..c8d7a0c1ac7a 100644 --- a/include/llvm/DebugInfo/DWARF/DWARFFormValue.h +++ b/include/llvm/DebugInfo/DWARF/DWARFFormValue.h @@ -1,4 +1,4 @@ -//===-- DWARFFormValue.h ----------------------------------------*- C++ -*-===// +//===- DWARFFormValue.h -----------------------------------------*- C++ -*-===//  //  //                     The LLVM Compiler Infrastructure  // @@ -10,13 +10,15 @@  #ifndef LLVM_DEBUGINFO_DWARFFORMVALUE_H  #define LLVM_DEBUGINFO_DWARFFORMVALUE_H +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/None.h"  #include "llvm/ADT/Optional.h"  #include "llvm/Support/DataExtractor.h"  #include "llvm/Support/Dwarf.h" +#include <cstdint>  namespace llvm { -template <typename T> class ArrayRef;  class DWARFUnit;  class raw_ostream; @@ -37,7 +39,7 @@ public:  private:    struct ValueType { -    ValueType() : data(nullptr) { +    ValueType() {        uval = 0;      } @@ -46,20 +48,27 @@ private:        int64_t sval;        const char* cstr;      }; -    const uint8_t* data; +    const uint8_t* data = nullptr;    };    dwarf::Form Form; // Form for this value.    ValueType Value; // Contains all data for the form. -  const DWARFUnit *U; // Remember the DWARFUnit at extract time. +  const DWARFUnit *U = nullptr; // Remember the DWARFUnit at extract time.  public: -  DWARFFormValue(dwarf::Form F = dwarf::Form(0)) : Form(F), U(nullptr) {} +  DWARFFormValue(dwarf::Form F = dwarf::Form(0)) : Form(F) {} +    dwarf::Form getForm() const { return Form; }    void setForm(dwarf::Form F) { Form = F; }    void setUValue(uint64_t V) { Value.uval = V; }    void setSValue(int64_t V) { Value.sval = V; }    void setPValue(const char *V) { Value.cstr = V; } + +  void setBlockValue(const ArrayRef<uint8_t> &Data) { +    Value.data = Data.data(); +    setUValue(Data.size()); +  } +    bool isFormClass(FormClass FC) const;    const DWARFUnit *getUnit() const { return U; }    void dump(raw_ostream &OS) const; @@ -72,6 +81,7 @@ public:    /// \returns whether the extraction succeeded.    bool extractValue(const DataExtractor &Data, uint32_t *OffsetPtr,                      const DWARFUnit *U); +    bool isInlinedCStr() const {      return Value.data != nullptr && Value.data == (const uint8_t*)Value.cstr;    } @@ -87,6 +97,7 @@ public:    Optional<ArrayRef<uint8_t>> getAsBlock() const;    Optional<uint64_t> getAsCStringOffset() const;    Optional<uint64_t> getAsReferenceUVal() const; +    /// Get the fixed byte size for a given form.    ///    /// If the form always has a fixed valid byte size that doesn't depend on a @@ -105,6 +116,7 @@ public:    /// and was needed to calculate the byte size.    static Optional<uint8_t> getFixedByteSize(dwarf::Form Form,                                              const DWARFUnit *U = nullptr); +    /// Get the fixed byte size for a given form.    ///    /// If the form has a fixed byte size given a valid DWARF version and address @@ -133,6 +145,7 @@ public:    /// \returns true on success, false if the form was not skipped.    bool skipValue(DataExtractor debug_info_data, uint32_t *offset_ptr,                   const DWARFUnit *U) const; +    /// Skip a form in \p debug_info_data at offset specified by \p offset_ptr.    ///    /// Skips the bytes for this form in the debug info and updates the offset. @@ -145,6 +158,7 @@ public:    /// \returns true on success, false if the form was not skipped.    static bool skipValue(dwarf::Form form, DataExtractor debug_info_data,                          uint32_t *offset_ptr, const DWARFUnit *U); +    /// Skip a form in \p debug_info_data at offset specified by \p offset_ptr.    ///    /// Skips the bytes for this form in the debug info and updates the offset. @@ -164,6 +178,154 @@ private:    void dumpString(raw_ostream &OS) const;  }; -} +namespace dwarf { + +  /// Take an optional DWARFFormValue and try to extract a string value from it. +  /// +  /// \param V and optional DWARFFormValue to attempt to extract the value from. +  /// \returns an optional value that contains a value if the form value +  /// was valid and was a string. +  inline Optional<const char*> toString(const Optional<DWARFFormValue>& V) { +    if (V) +      return V->getAsCString(); +    return None; +  } +   +  /// Take an optional DWARFFormValue and extract a string value from it. +  /// +  /// \param V and optional DWARFFormValue to attempt to extract the value from. +  /// \param Default the default value to return in case of failure. +  /// \returns the string value or Default if the V doesn't have a value or the +  /// form value's encoding wasn't a string. +  inline const char* +  toString(const Optional<DWARFFormValue>& V, const char *Default) { +    return toString(V).getValueOr(Default); +  } + +  /// Take an optional DWARFFormValue and try to extract an unsigned constant. +  /// +  /// \param V and optional DWARFFormValue to attempt to extract the value from. +  /// \returns an optional value that contains a value if the form value +  /// was valid and has a unsigned constant form. +  inline Optional<uint64_t> toUnsigned(const Optional<DWARFFormValue>& V) { +    if (V) +      return V->getAsUnsignedConstant(); +    return None; +  } +   +  /// Take an optional DWARFFormValue and extract a unsigned constant. +  /// +  /// \param V and optional DWARFFormValue to attempt to extract the value from. +  /// \param Default the default value to return in case of failure. +  /// \returns the extracted unsigned value or Default if the V doesn't have a +  /// value or the form value's encoding wasn't an unsigned constant form. +  inline uint64_t +  toUnsigned(const Optional<DWARFFormValue>& V, uint64_t Default) { +    return toUnsigned(V).getValueOr(Default); +  } +   +  /// Take an optional DWARFFormValue and try to extract an reference. +  /// +  /// \param V and optional DWARFFormValue to attempt to extract the value from. +  /// \returns an optional value that contains a value if the form value +  /// was valid and has a reference form. +  inline Optional<uint64_t> toReference(const Optional<DWARFFormValue>& V) { +    if (V) +      return V->getAsReference(); +    return None; +  } +   +  /// Take an optional DWARFFormValue and extract a reference. +  /// +  /// \param V and optional DWARFFormValue to attempt to extract the value from. +  /// \param Default the default value to return in case of failure. +  /// \returns the extracted reference value or Default if the V doesn't have a +  /// value or the form value's encoding wasn't a reference form. +  inline uint64_t +  toReference(const Optional<DWARFFormValue>& V, uint64_t Default) { +    return toReference(V).getValueOr(Default); +  } +   +  /// Take an optional DWARFFormValue and try to extract an signed constant. +  /// +  /// \param V and optional DWARFFormValue to attempt to extract the value from. +  /// \returns an optional value that contains a value if the form value +  /// was valid and has a signed constant form. +  inline Optional<int64_t> toSigned(const Optional<DWARFFormValue>& V) { +    if (V) +      return V->getAsSignedConstant(); +    return None; +  } + +  /// Take an optional DWARFFormValue and extract a signed integer. +  /// +  /// \param V and optional DWARFFormValue to attempt to extract the value from. +  /// \param Default the default value to return in case of failure. +  /// \returns the extracted signed integer value or Default if the V doesn't +  /// have a value or the form value's encoding wasn't a signed integer form. +  inline int64_t +  toSigned(const Optional<DWARFFormValue>& V, int64_t Default) { +    return toSigned(V).getValueOr(Default); +  } + +  /// Take an optional DWARFFormValue and try to extract an address. +  /// +  /// \param V and optional DWARFFormValue to attempt to extract the value from. +  /// \returns an optional value that contains a value if the form value +  /// was valid and has a address form. +  inline Optional<uint64_t> toAddress(const Optional<DWARFFormValue>& V) { +    if (V) +      return V->getAsAddress(); +    return None; +  } + +  /// Take an optional DWARFFormValue and extract a address. +  /// +  /// \param V and optional DWARFFormValue to attempt to extract the value from. +  /// \param Default the default value to return in case of failure. +  /// \returns the extracted address value or Default if the V doesn't have a +  /// value or the form value's encoding wasn't an address form. +  inline uint64_t +  toAddress(const Optional<DWARFFormValue>& V, uint64_t Default) { +    return toAddress(V).getValueOr(Default); +  } + +  /// Take an optional DWARFFormValue and try to extract an section offset. +  /// +  /// \param V and optional DWARFFormValue to attempt to extract the value from. +  /// \returns an optional value that contains a value if the form value +  /// was valid and has a section offset form. +  inline Optional<uint64_t> toSectionOffset(const Optional<DWARFFormValue>& V) { +    if (V) +      return V->getAsSectionOffset(); +    return None; +  } + +  /// Take an optional DWARFFormValue and extract a section offset. +  /// +  /// \param V and optional DWARFFormValue to attempt to extract the value from. +  /// \param Default the default value to return in case of failure. +  /// \returns the extracted section offset value or Default if the V doesn't +  /// have a value or the form value's encoding wasn't a section offset form. +  inline uint64_t +  toSectionOffset(const Optional<DWARFFormValue>& V, uint64_t Default) { +    return toSectionOffset(V).getValueOr(Default); +  } + +  /// Take an optional DWARFFormValue and try to extract block data. +  /// +  /// \param V and optional DWARFFormValue to attempt to extract the value from. +  /// \returns an optional value that contains a value if the form value +  /// was valid and has a block form. +  inline Optional<ArrayRef<uint8_t>> +  toBlock(const Optional<DWARFFormValue>& V) { +    if (V) +      return V->getAsBlock(); +    return None; +  } + +} // end namespace dwarf + +} // end namespace llvm -#endif +#endif // LLVM_DEBUGINFO_DWARFFORMVALUE_H | 
