https://github.com/cmtice updated https://github.com/llvm/llvm-project/pull/117401
>From b6051edba2a1ecae144ead48712fb511c204131a Mon Sep 17 00:00:00 2001 From: Caroline Tice <cmt...@google.com> Date: Fri, 22 Nov 2024 15:33:42 -0800 Subject: [PATCH 1/3] [LLDB] Fix error returns in CastToBasicType and CastToEnumType in ValueObject. Update the error returns in ValueObject::CastToBasicType and ValueObject::CastToEnumType to create new errors and return a ValueObjectConstResult with the error, rather tnan updating the error in (and returning) the input ValueObject. --- lldb/source/ValueObject/ValueObject.cpp | 87 +++++++++++++++---------- 1 file changed, 53 insertions(+), 34 deletions(-) diff --git a/lldb/source/ValueObject/ValueObject.cpp b/lldb/source/ValueObject/ValueObject.cpp index 86172ad1b561f9..1093c4b665f4eb 100644 --- a/lldb/source/ValueObject/ValueObject.cpp +++ b/lldb/source/ValueObject/ValueObject.cpp @@ -3194,16 +3194,19 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) { GetCompilerType().IsPointerType() || GetCompilerType().IsNullPtrType(); bool is_float = GetCompilerType().IsFloat(); bool is_integer = GetCompilerType().IsInteger(); + ExecutionContext exe_ctx(GetExecutionContextRef()); if (!type.IsScalarType()) { - m_error = Status::FromErrorString("target type must be a scalar"); - return GetSP(); + Status error = Status::FromErrorString("target type must be a scalar"); + return ValueObjectConstResult::Create( + exe_ctx.GetBestExecutionContextScope(), error.Clone()); } if (!is_scalar && !is_enum && !is_pointer) { - m_error = + Status error = Status::FromErrorString("argument must be a scalar, enum, or pointer"); - return GetSP(); + return ValueObjectConstResult::Create( + exe_ctx.GetBestExecutionContextScope(), error.Clone()); } lldb::TargetSP target = GetTargetSP(); @@ -3216,14 +3219,16 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) { if (is_pointer) { if (!type.IsInteger() && !type.IsBoolean()) { - m_error = + Status error = Status::FromErrorString("target type must be an integer or boolean"); - return GetSP(); + return ValueObjectConstResult::Create( + exe_ctx.GetBestExecutionContextScope(), error.Clone()); } if (!type.IsBoolean() && type_byte_size < val_byte_size) { - m_error = Status::FromErrorString( + Status error = Status::FromErrorString( "target type cannot be smaller than the pointer type"); - return GetSP(); + return ValueObjectConstResult::Create( + exe_ctx.GetBestExecutionContextScope(), error.Clone()); } } @@ -3237,10 +3242,11 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) { return ValueObject::CreateValueObjectFromBool( target, !float_value_or_err->isZero(), "result"); else { - m_error = Status::FromErrorStringWithFormat( + Status error = Status::FromErrorStringWithFormat( "cannot get value as APFloat: %s", llvm::toString(float_value_or_err.takeError()).c_str()); - return GetSP(); + return ValueObjectConstResult::Create( + exe_ctx.GetBestExecutionContextScope(), error.Clone()); } } } @@ -3256,11 +3262,12 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) { return ValueObject::CreateValueObjectFromAPInt(target, ext, type, "result"); } else { - m_error = Status::FromErrorStringWithFormat( + Status error = Status::FromErrorStringWithFormat( "cannot get value as APSInt: %s", llvm::toString(int_value_or_err.takeError()).c_str()); ; - return GetSP(); + return ValueObjectConstResult::Create( + exe_ctx.GetBestExecutionContextScope(), error.Clone()); } } else if (is_scalar && is_float) { llvm::APSInt integer(type_byte_size * CHAR_BIT, !type.IsSigned()); @@ -3274,10 +3281,11 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) { // Casting floating point values that are out of bounds of the target // type is undefined behaviour. if (status & llvm::APFloatBase::opInvalidOp) { - m_error = Status::FromErrorStringWithFormat( + Status error = Status::FromErrorStringWithFormat( "invalid type cast detected: %s", llvm::toString(float_value_or_err.takeError()).c_str()); - return GetSP(); + return ValueObjectConstResult::Create( + exe_ctx.GetBestExecutionContextScope(), error.Clone()); } return ValueObject::CreateValueObjectFromAPInt(target, integer, type, "result"); @@ -3297,10 +3305,11 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) { return ValueObject::CreateValueObjectFromAPFloat(target, f, type, "result"); } else { - m_error = Status::FromErrorStringWithFormat( + Status error = Status::FromErrorStringWithFormat( "cannot get value as APSInt: %s", llvm::toString(int_value_or_err.takeError()).c_str()); - return GetSP(); + return ValueObjectConstResult::Create( + exe_ctx.GetBestExecutionContextScope(), error.Clone()); } } else { if (is_integer) { @@ -3312,10 +3321,11 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) { return ValueObject::CreateValueObjectFromAPFloat(target, f, type, "result"); } else { - m_error = Status::FromErrorStringWithFormat( + Status error = Status::FromErrorStringWithFormat( "cannot get value as APSInt: %s", llvm::toString(int_value_or_err.takeError()).c_str()); - return GetSP(); + return ValueObjectConstResult::Create( + exe_ctx.GetBestExecutionContextScope(), error.Clone()); } } if (is_float) { @@ -3327,33 +3337,38 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) { return ValueObject::CreateValueObjectFromAPFloat(target, f, type, "result"); } else { - m_error = Status::FromErrorStringWithFormat( + Status error = Status::FromErrorStringWithFormat( "cannot get value as APFloat: %s", llvm::toString(float_value_or_err.takeError()).c_str()); - return GetSP(); + return ValueObjectConstResult::Create( + exe_ctx.GetBestExecutionContextScope(), error.Clone()); } } } } - m_error = Status::FromErrorString("Unable to perform requested cast"); - return GetSP(); + Status error = Status::FromErrorString("Unable to perform requested cast"); + return ValueObjectConstResult::Create( + exe_ctx.GetBestExecutionContextScope(), error.Clone()); } lldb::ValueObjectSP ValueObject::CastToEnumType(CompilerType type) { bool is_enum = GetCompilerType().IsEnumerationType(); bool is_integer = GetCompilerType().IsInteger(); bool is_float = GetCompilerType().IsFloat(); + ExecutionContext exe_ctx(GetExecutionContextRef()); if (!is_enum && !is_integer && !is_float) { - m_error = Status::FromErrorString( + Status error = Status::FromErrorString( "argument must be an integer, a float, or an enum"); - return GetSP(); + return ValueObjectConstResult::Create( + exe_ctx.GetBestExecutionContextScope(), error.Clone()); } if (!type.IsEnumerationType()) { - m_error = Status::FromErrorString("target type must be an enum"); - return GetSP(); + Status error = Status::FromErrorString("target type must be an enum"); + return ValueObjectConstResult::Create( + exe_ctx.GetBestExecutionContextScope(), error.Clone()); } lldb::TargetSP target = GetTargetSP(); @@ -3372,16 +3387,18 @@ lldb::ValueObjectSP ValueObject::CastToEnumType(CompilerType type) { // Casting floating point values that are out of bounds of the target // type is undefined behaviour. if (status & llvm::APFloatBase::opInvalidOp) { - m_error = Status::FromErrorStringWithFormat( + Status error = Status::FromErrorStringWithFormat( "invalid type cast detected: %s", llvm::toString(value_or_err.takeError()).c_str()); - return GetSP(); + return ValueObjectConstResult::Create( + exe_ctx.GetBestExecutionContextScope(), error.Clone()); } return ValueObject::CreateValueObjectFromAPInt(target, integer, type, "result"); } else { - m_error = Status::FromErrorString("cannot get value as APFloat"); - return GetSP(); + Status error = Status::FromErrorString("cannot get value as APFloat"); + return ValueObjectConstResult::Create( + exe_ctx.GetBestExecutionContextScope(), error.Clone()); } } else { // Get the value as APSInt and extend or truncate it to the requested size. @@ -3391,14 +3408,16 @@ lldb::ValueObjectSP ValueObject::CastToEnumType(CompilerType type) { return ValueObject::CreateValueObjectFromAPInt(target, ext, type, "result"); } else { - m_error = Status::FromErrorStringWithFormat( + Status error = Status::FromErrorStringWithFormat( "cannot get value as APSInt: %s", llvm::toString(value_or_err.takeError()).c_str()); - return GetSP(); + return ValueObjectConstResult::Create( + exe_ctx.GetBestExecutionContextScope(), error.Clone()); } } - m_error = Status::FromErrorString("Cannot perform requested cast"); - return GetSP(); + Status error = Status::FromErrorString("Cannot perform requested cast"); + return ValueObjectConstResult::Create( + exe_ctx.GetBestExecutionContextScope(), error.Clone()); } ValueObject::EvaluationPoint::EvaluationPoint() : m_mod_id(), m_exe_ctx_ref() {} >From 5cfd97e524e8fc44748e8b4f090898a9d5f38c8d Mon Sep 17 00:00:00 2001 From: Caroline Tice <cmt...@google.com> Date: Sun, 24 Nov 2024 16:33:31 -0800 Subject: [PATCH 2/3] Fix clang-format errors. --- lldb/source/ValueObject/ValueObject.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lldb/source/ValueObject/ValueObject.cpp b/lldb/source/ValueObject/ValueObject.cpp index 1093c4b665f4eb..af55379d47bd27 100644 --- a/lldb/source/ValueObject/ValueObject.cpp +++ b/lldb/source/ValueObject/ValueObject.cpp @@ -3348,8 +3348,8 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) { } Status error = Status::FromErrorString("Unable to perform requested cast"); - return ValueObjectConstResult::Create( - exe_ctx.GetBestExecutionContextScope(), error.Clone()); + return ValueObjectConstResult::Create(exe_ctx.GetBestExecutionContextScope(), + error.Clone()); } lldb::ValueObjectSP ValueObject::CastToEnumType(CompilerType type) { @@ -3416,8 +3416,8 @@ lldb::ValueObjectSP ValueObject::CastToEnumType(CompilerType type) { } } Status error = Status::FromErrorString("Cannot perform requested cast"); - return ValueObjectConstResult::Create( - exe_ctx.GetBestExecutionContextScope(), error.Clone()); + return ValueObjectConstResult::Create(exe_ctx.GetBestExecutionContextScope(), + error.Clone()); } ValueObject::EvaluationPoint::EvaluationPoint() : m_mod_id(), m_exe_ctx_ref() {} >From 0cbce7452353b1c4af4291f18c2d3185615c730d Mon Sep 17 00:00:00 2001 From: Caroline Tice <cmt...@google.com> Date: Tue, 3 Dec 2024 10:52:10 -0800 Subject: [PATCH 3/3] Clean up code & remove unnecessary extra variables. --- lldb/source/ValueObject/ValueObject.cpp | 147 +++++++++++------------- 1 file changed, 66 insertions(+), 81 deletions(-) diff --git a/lldb/source/ValueObject/ValueObject.cpp b/lldb/source/ValueObject/ValueObject.cpp index af55379d47bd27..2864af107b925f 100644 --- a/lldb/source/ValueObject/ValueObject.cpp +++ b/lldb/source/ValueObject/ValueObject.cpp @@ -3196,18 +3196,15 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) { bool is_integer = GetCompilerType().IsInteger(); ExecutionContext exe_ctx(GetExecutionContextRef()); - if (!type.IsScalarType()) { - Status error = Status::FromErrorString("target type must be a scalar"); + if (!type.IsScalarType()) return ValueObjectConstResult::Create( - exe_ctx.GetBestExecutionContextScope(), error.Clone()); - } + exe_ctx.GetBestExecutionContextScope(), + Status::FromErrorString("target type must be a scalar")); - if (!is_scalar && !is_enum && !is_pointer) { - Status error = - Status::FromErrorString("argument must be a scalar, enum, or pointer"); + if (!is_scalar && !is_enum && !is_pointer) return ValueObjectConstResult::Create( - exe_ctx.GetBestExecutionContextScope(), error.Clone()); - } + exe_ctx.GetBestExecutionContextScope(), + Status::FromErrorString("argument must be a scalar, enum, or pointer")); lldb::TargetSP target = GetTargetSP(); uint64_t type_byte_size = 0; @@ -3218,18 +3215,15 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) { val_byte_size = temp.value(); if (is_pointer) { - if (!type.IsInteger() && !type.IsBoolean()) { - Status error = - Status::FromErrorString("target type must be an integer or boolean"); + if (!type.IsInteger() && !type.IsBoolean()) return ValueObjectConstResult::Create( - exe_ctx.GetBestExecutionContextScope(), error.Clone()); - } - if (!type.IsBoolean() && type_byte_size < val_byte_size) { - Status error = Status::FromErrorString( - "target type cannot be smaller than the pointer type"); + exe_ctx.GetBestExecutionContextScope(), + Status::FromErrorString("target type must be an integer or boolean")); + if (!type.IsBoolean() && type_byte_size < val_byte_size) return ValueObjectConstResult::Create( - exe_ctx.GetBestExecutionContextScope(), error.Clone()); - } + exe_ctx.GetBestExecutionContextScope(), + Status::FromErrorString( + "target type cannot be smaller than the pointer type")); } if (type.IsBoolean()) { @@ -3241,13 +3235,12 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) { if (float_value_or_err) return ValueObject::CreateValueObjectFromBool( target, !float_value_or_err->isZero(), "result"); - else { - Status error = Status::FromErrorStringWithFormat( - "cannot get value as APFloat: %s", - llvm::toString(float_value_or_err.takeError()).c_str()); + else return ValueObjectConstResult::Create( - exe_ctx.GetBestExecutionContextScope(), error.Clone()); - } + exe_ctx.GetBestExecutionContextScope(), + Status::FromErrorStringWithFormat( + "cannot get value as APFloat: %s", + llvm::toString(float_value_or_err.takeError()).c_str())); } } @@ -3261,14 +3254,12 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) { int_value_or_err->extOrTrunc(type_byte_size * CHAR_BIT); return ValueObject::CreateValueObjectFromAPInt(target, ext, type, "result"); - } else { - Status error = Status::FromErrorStringWithFormat( - "cannot get value as APSInt: %s", - llvm::toString(int_value_or_err.takeError()).c_str()); - ; + } else return ValueObjectConstResult::Create( - exe_ctx.GetBestExecutionContextScope(), error.Clone()); - } + exe_ctx.GetBestExecutionContextScope(), + Status::FromErrorStringWithFormat( + "cannot get value as APSInt: %s", + llvm::toString(int_value_or_err.takeError()).c_str())); } else if (is_scalar && is_float) { llvm::APSInt integer(type_byte_size * CHAR_BIT, !type.IsSigned()); bool is_exact; @@ -3280,13 +3271,12 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) { // Casting floating point values that are out of bounds of the target // type is undefined behaviour. - if (status & llvm::APFloatBase::opInvalidOp) { - Status error = Status::FromErrorStringWithFormat( - "invalid type cast detected: %s", - llvm::toString(float_value_or_err.takeError()).c_str()); + if (status & llvm::APFloatBase::opInvalidOp) return ValueObjectConstResult::Create( - exe_ctx.GetBestExecutionContextScope(), error.Clone()); - } + exe_ctx.GetBestExecutionContextScope(), + Status::FromErrorStringWithFormat( + "invalid type cast detected: %s", + llvm::toString(float_value_or_err.takeError()).c_str())); return ValueObject::CreateValueObjectFromAPInt(target, integer, type, "result"); } @@ -3305,11 +3295,11 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) { return ValueObject::CreateValueObjectFromAPFloat(target, f, type, "result"); } else { - Status error = Status::FromErrorStringWithFormat( - "cannot get value as APSInt: %s", - llvm::toString(int_value_or_err.takeError()).c_str()); return ValueObjectConstResult::Create( - exe_ctx.GetBestExecutionContextScope(), error.Clone()); + exe_ctx.GetBestExecutionContextScope(), + Status::FromErrorStringWithFormat( + "cannot get value as APSInt: %s", + llvm::toString(int_value_or_err.takeError()).c_str())); } } else { if (is_integer) { @@ -3321,11 +3311,11 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) { return ValueObject::CreateValueObjectFromAPFloat(target, f, type, "result"); } else { - Status error = Status::FromErrorStringWithFormat( - "cannot get value as APSInt: %s", - llvm::toString(int_value_or_err.takeError()).c_str()); return ValueObjectConstResult::Create( - exe_ctx.GetBestExecutionContextScope(), error.Clone()); + exe_ctx.GetBestExecutionContextScope(), + Status::FromErrorStringWithFormat( + "cannot get value as APSInt: %s", + llvm::toString(int_value_or_err.takeError()).c_str())); } } if (is_float) { @@ -3337,19 +3327,19 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) { return ValueObject::CreateValueObjectFromAPFloat(target, f, type, "result"); } else { - Status error = Status::FromErrorStringWithFormat( - "cannot get value as APFloat: %s", - llvm::toString(float_value_or_err.takeError()).c_str()); return ValueObjectConstResult::Create( - exe_ctx.GetBestExecutionContextScope(), error.Clone()); + exe_ctx.GetBestExecutionContextScope(), + Status::FromErrorStringWithFormat( + "cannot get value as APFloat: %s", + llvm::toString(float_value_or_err.takeError()).c_str())); } } } } - Status error = Status::FromErrorString("Unable to perform requested cast"); - return ValueObjectConstResult::Create(exe_ctx.GetBestExecutionContextScope(), - error.Clone()); + return ValueObjectConstResult::Create( + exe_ctx.GetBestExecutionContextScope(), + Status::FromErrorString("Unable to perform requested cast")); } lldb::ValueObjectSP ValueObject::CastToEnumType(CompilerType type) { @@ -3358,18 +3348,16 @@ lldb::ValueObjectSP ValueObject::CastToEnumType(CompilerType type) { bool is_float = GetCompilerType().IsFloat(); ExecutionContext exe_ctx(GetExecutionContextRef()); - if (!is_enum && !is_integer && !is_float) { - Status error = Status::FromErrorString( - "argument must be an integer, a float, or an enum"); + if (!is_enum && !is_integer && !is_float) return ValueObjectConstResult::Create( - exe_ctx.GetBestExecutionContextScope(), error.Clone()); - } + exe_ctx.GetBestExecutionContextScope(), + Status::FromErrorString( + "argument must be an integer, a float, or an enum")); - if (!type.IsEnumerationType()) { - Status error = Status::FromErrorString("target type must be an enum"); + if (!type.IsEnumerationType()) return ValueObjectConstResult::Create( - exe_ctx.GetBestExecutionContextScope(), error.Clone()); - } + exe_ctx.GetBestExecutionContextScope(), + Status::FromErrorString("target type must be an enum")); lldb::TargetSP target = GetTargetSP(); uint64_t byte_size = 0; @@ -3386,20 +3374,18 @@ lldb::ValueObjectSP ValueObject::CastToEnumType(CompilerType type) { // Casting floating point values that are out of bounds of the target // type is undefined behaviour. - if (status & llvm::APFloatBase::opInvalidOp) { - Status error = Status::FromErrorStringWithFormat( - "invalid type cast detected: %s", - llvm::toString(value_or_err.takeError()).c_str()); + if (status & llvm::APFloatBase::opInvalidOp) return ValueObjectConstResult::Create( - exe_ctx.GetBestExecutionContextScope(), error.Clone()); - } + exe_ctx.GetBestExecutionContextScope(), + Status::FromErrorStringWithFormat( + "invalid type cast detected: %s", + llvm::toString(value_or_err.takeError()).c_str())); return ValueObject::CreateValueObjectFromAPInt(target, integer, type, "result"); - } else { - Status error = Status::FromErrorString("cannot get value as APFloat"); + } else return ValueObjectConstResult::Create( - exe_ctx.GetBestExecutionContextScope(), error.Clone()); - } + exe_ctx.GetBestExecutionContextScope(), + Status::FromErrorString("cannot get value as APFloat")); } else { // Get the value as APSInt and extend or truncate it to the requested size. auto value_or_err = GetValueAsAPSInt(); @@ -3407,17 +3393,16 @@ lldb::ValueObjectSP ValueObject::CastToEnumType(CompilerType type) { llvm::APSInt ext = value_or_err->extOrTrunc(byte_size * CHAR_BIT); return ValueObject::CreateValueObjectFromAPInt(target, ext, type, "result"); - } else { - Status error = Status::FromErrorStringWithFormat( - "cannot get value as APSInt: %s", - llvm::toString(value_or_err.takeError()).c_str()); + } else return ValueObjectConstResult::Create( - exe_ctx.GetBestExecutionContextScope(), error.Clone()); - } + exe_ctx.GetBestExecutionContextScope(), + Status::FromErrorStringWithFormat( + "cannot get value as APSInt: %s", + llvm::toString(value_or_err.takeError()).c_str())); } - Status error = Status::FromErrorString("Cannot perform requested cast"); - return ValueObjectConstResult::Create(exe_ctx.GetBestExecutionContextScope(), - error.Clone()); + return ValueObjectConstResult::Create( + exe_ctx.GetBestExecutionContextScope(), + Status::FromErrorString("Cannot perform requested cast")); } ValueObject::EvaluationPoint::EvaluationPoint() : m_mod_id(), m_exe_ctx_ref() {} _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits