Why SBValue::Cast is deprecated and how to replace it with expression?


SBValue::Cast marked as deprecated in LLDB headers:

// Deprecated - use the expression evaluator to perform type casting
lldb::SBValue Cast(lldb::SBType type);

But I can’t understand how to replace it with expression evaluation.

Suppose I have:

auto casted_val = my_value.Cast(my_type);

What would be equivalent expression, taking into account that both value and type can be out of current scope?

Same question on stackoverflow:


Thanks in advance for help,


Cast is deprecated because it isn't a real C++ cast. In particular if you cast a pointer to a base class to it's parent class, it doesn't do the work to offset the pointer to take into account the position of the base class in it's parent. All it does is extract the address of the original value object and cast THAT to the new type.

For C & ObjC types, Cast is not going to cause any problems. But for C++ it is not always correct.

Any of the "evaluate expression" calls with a proper cast expression should work, clang will get the offsetting right in the code it generates. So for instance:

casted_value = frame.EvaluateExpression("(MyType *) var_to_cast");

SBFrame::EvaluateExpression will search for types & globally visible objects searching outward from the current scope. So finding variables & types from other contexts should only be problem if there are multiple incompatible types/variables globally visible.

To handle the possible ambiguities here, we need to add some syntax to the expression parser to indicate "this type/varname from this scope". gdb did this with:


which was unfortunate since it collides with C++ and can make entering C++ expressions awkward.

Our tentative plan is to use the "$$" to indicate "names with special sauce" since we're already using initial $ specially, and then do something like:


to mean the variable called varname in foo.dylib.