[llvm] r231299 - DWARFFormValue: Add getAsSignedConstant method.

Aaron Ballman aaron at aaronballman.com
Thu Mar 5 06:17:55 PST 2015


On Wed, Mar 4, 2015 at 5:07 PM, Frederic Riss <friss at apple.com> wrote:
> Author: friss
> Date: Wed Mar  4 16:07:41 2015
> New Revision: 231299
>
> URL: http://llvm.org/viewvc/llvm-project?rev=231299&view=rev
> Log:
> DWARFFormValue: Add getAsSignedConstant method.
>
> The implementation accepts explicitely signed forms (DW_FORM_sdata),
> but also unsigned forms as long as they fit in an int64_t.
>
> Modified:
>     llvm/trunk/include/llvm/DebugInfo/DWARF/DWARFFormValue.h
>     llvm/trunk/lib/DebugInfo/DWARF/DWARFFormValue.cpp
>     llvm/trunk/unittests/DebugInfo/DWARF/DWARFFormValueTest.cpp
>
> Modified: llvm/trunk/include/llvm/DebugInfo/DWARF/DWARFFormValue.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/DWARF/DWARFFormValue.h?rev=231299&r1=231298&r2=231299&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/DebugInfo/DWARF/DWARFFormValue.h (original)
> +++ llvm/trunk/include/llvm/DebugInfo/DWARF/DWARFFormValue.h Wed Mar  4 16:07:41 2015
> @@ -74,6 +74,7 @@ public:
>    /// DWARFFormValue has form class is suitable for representing Foo.
>    Optional<uint64_t> getAsReference(const DWARFUnit *U) const;
>    Optional<uint64_t> getAsUnsignedConstant() const;
> +  Optional<int64_t> getAsSignedConstant() const;
>    Optional<const char *> getAsCString(const DWARFUnit *U) const;
>    Optional<uint64_t> getAsAddress(const DWARFUnit *U) const;
>    Optional<uint64_t> getAsSectionOffset() const;
>
> Modified: llvm/trunk/lib/DebugInfo/DWARF/DWARFFormValue.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/DWARF/DWARFFormValue.cpp?rev=231299&r1=231298&r2=231299&view=diff
> ==============================================================================
> --- llvm/trunk/lib/DebugInfo/DWARF/DWARFFormValue.cpp (original)
> +++ llvm/trunk/lib/DebugInfo/DWARF/DWARFFormValue.cpp Wed Mar  4 16:07:41 2015
> @@ -18,6 +18,7 @@
>  #include "llvm/Support/Format.h"
>  #include "llvm/Support/raw_ostream.h"
>  #include <cassert>
> +#include <climits>
>  using namespace llvm;
>  using namespace dwarf;
>  using namespace syntax;
> @@ -557,6 +558,24 @@ Optional<uint64_t> DWARFFormValue::getAs
>    return Value.uval;
>  }
>
> +Optional<int64_t> DWARFFormValue::getAsSignedConstant() const {
> +  if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
> +      (Form == DW_FORM_udata && uint64_t(LLONG_MAX) < Value.uval))
> +    return None;
> +  switch (Form) {
> +  case DW_FORM_data4:
> +    return int32_t(Value.uval);
> +  case DW_FORM_data2:
> +    return int16_t(Value.uval);
> +  case DW_FORM_data1:
> +    return int8_t(Value.uval);
> +  case DW_FORM_sdata:
> +  case DW_FORM_data8:
> +  default:
> +    return Value.sval;
> +  }
> +}
> +
>  Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
>    if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc))
>      return None;
>
> Modified: llvm/trunk/unittests/DebugInfo/DWARF/DWARFFormValueTest.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/DebugInfo/DWARF/DWARFFormValueTest.cpp?rev=231299&r1=231298&r2=231299&view=diff
> ==============================================================================
> --- llvm/trunk/unittests/DebugInfo/DWARF/DWARFFormValueTest.cpp (original)
> +++ llvm/trunk/unittests/DebugInfo/DWARF/DWARFFormValueTest.cpp Wed Mar  4 16:07:41 2015
> @@ -8,7 +8,10 @@
>  //===----------------------------------------------------------------------===//
>
>  #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
> +#include "llvm/ADT/SmallString.h"
>  #include "llvm/Support/Dwarf.h"
> +#include "llvm/Support/LEB128.h"
> +#include "llvm/Support/Host.h"
>  #include "gtest/gtest.h"
>  using namespace llvm;
>  using namespace dwarf;
> @@ -46,4 +49,75 @@ TEST(DWARFFormValue, FormClass) {
>    EXPECT_TRUE(isFormClass(DW_FORM_ref_sig8, DWARFFormValue::FC_Reference));
>  }
>
> +template<typename RawTypeT>
> +DWARFFormValue createDataXFormValue(uint16_t Form, RawTypeT Value) {
> +  char Raw[sizeof(RawTypeT)];
> +  memcpy(Raw, &Value, sizeof(RawTypeT));
> +  uint32_t Offset = 0;
> +  DWARFFormValue Result(Form);
> +  DataExtractor Data(StringRef(Raw, sizeof(RawTypeT)),
> +                     sys::IsLittleEndianHost, sizeof(void*));
> +  Result.extractValue(Data, &Offset, nullptr);
> +  return Result;
> +}
> +
> +DWARFFormValue createULEBFormValue(uint64_t Value) {
> +  SmallString<10> RawData;
> +  raw_svector_ostream OS(RawData);
> +  encodeULEB128(Value, OS);
> +  uint32_t Offset = 0;
> +  DWARFFormValue Result(DW_FORM_udata);
> +  DataExtractor Data(OS.str(), sys::IsLittleEndianHost, sizeof(void*));
> +  Result.extractValue(Data, &Offset, nullptr);
> +  return Result;
> +}
> +
> +DWARFFormValue createSLEBFormValue(int64_t Value) {
> +  SmallString<10> RawData;
> +  raw_svector_ostream OS(RawData);
> +  encodeSLEB128(Value, OS);
> +  uint32_t Offset = 0;
> +  DWARFFormValue Result(DW_FORM_sdata);
> +  DataExtractor Data(OS.str(), sys::IsLittleEndianHost, sizeof(void*));
> +  Result.extractValue(Data, &Offset, nullptr);
> +  return Result;
> +}
> +
> +TEST(DWARFFormValue, SignedConstantForms) {
> +  // Check that we correctly sign extend fixed size forms.
> +  auto Sign1 = createDataXFormValue<uint8_t>(DW_FORM_data1, -123);
> +  auto Sign2 = createDataXFormValue<uint16_t>(DW_FORM_data2, -12345);
> +  auto Sign4 = createDataXFormValue<uint32_t>(DW_FORM_data4, -123456789);
> +  auto Sign8 = createDataXFormValue<uint64_t>(DW_FORM_data8, -1);
> +  EXPECT_EQ(Sign1.getAsSignedConstant().getValue(), -123);
> +  EXPECT_EQ(Sign2.getAsSignedConstant().getValue(), -12345);
> +  EXPECT_EQ(Sign4.getAsSignedConstant().getValue(), -123456789);
> +  EXPECT_EQ(Sign8.getAsSignedConstant().getValue(), -1);
> +
> +  // Check that we can handle big positive values, but that we return
> +  // an error just over the limit.
> +  auto UMax = createULEBFormValue(LLONG_MAX);
> +  auto TooBig = createULEBFormValue(LLONG_MAX + 1);

This test is now triggering:

DWARFFormValueTest.cpp: In member function ‘virtual void
{anonymous}::DWARFFormValue_SignedConstantForms_Test::TestBody()’:
DWARFFormValueTest.cpp:100:49: warning: integer overflow in expression
[-Woverflow]

We can silence this with casts, but I'm not certain if that alters
your test case to no longer test what you require.

~Aaron

> +  EXPECT_EQ(UMax.getAsSignedConstant().getValue(), LLONG_MAX);
> +  EXPECT_EQ(TooBig.getAsSignedConstant().hasValue(), false);
> +
> +  // Sanity check some other forms.
> +  auto Data1 = createDataXFormValue<uint8_t>(DW_FORM_data1, 120);
> +  auto Data2 = createDataXFormValue<uint16_t>(DW_FORM_data2, 32000);
> +  auto Data4 = createDataXFormValue<uint32_t>(DW_FORM_data4, 2000000000);
> +  auto Data8 = createDataXFormValue<uint64_t>(DW_FORM_data8, 0x1234567812345678LL);
> +  auto LEBMin = createSLEBFormValue(LLONG_MIN);
> +  auto LEBMax = createSLEBFormValue(LLONG_MAX);
> +  auto LEB1 = createSLEBFormValue(-42);
> +  auto LEB2 = createSLEBFormValue(42);
> +  EXPECT_EQ(Data1.getAsSignedConstant().getValue(), 120);
> +  EXPECT_EQ(Data2.getAsSignedConstant().getValue(), 32000);
> +  EXPECT_EQ(Data4.getAsSignedConstant().getValue(), 2000000000);
> +  EXPECT_EQ(Data8.getAsSignedConstant().getValue(), 0x1234567812345678LL);
> +  EXPECT_EQ(LEBMin.getAsSignedConstant().getValue(), LLONG_MIN);
> +  EXPECT_EQ(LEBMax.getAsSignedConstant().getValue(), LLONG_MAX);
> +  EXPECT_EQ(LEB1.getAsSignedConstant().getValue(), -42);
> +  EXPECT_EQ(LEB2.getAsSignedConstant().getValue(), 42);
> +}
> +
>  } // end anonymous namespace
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits




More information about the llvm-commits mailing list