llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.61k stars 11.35k forks source link

[lldb] Expose a way to identify if a variable is const in the Python API. #96954

Open mvanotti opened 1 month ago

mvanotti commented 1 month ago

It looks like there is no clear way to determine if a variable is const or constexpr in lldb. The best way I could find to determine that is by looking at the output of GetDisplayTypeName(), which contains the const keyword.

It would be nice to have a way to tell if a variable is a constant by invoking a method, or for example, by adding it to the TypeFlags: https://lldb.llvm.org/python_api_enums.html#typeflags

llvmbot commented 1 month ago

@llvm/issue-subscribers-lldb

Author: Marco Vanotti (mvanotti)

It looks like there is no clear way to determine if a variable is `const` or `constexpr` in `lldb`. The best way I could find to determine that is by looking at the output of `GetDisplayTypeName()`, which contains the `const` keyword. It would be nice to have a way to tell if a variable is a constant by invoking a method, or for example, by adding it to the TypeFlags: https://lldb.llvm.org/python_api_enums.html#typeflags
Michael137 commented 1 month ago

constexpr versus const in what contexts? E.g., for global variables or static data members they're effectively the same thing?

mvanotti commented 1 month ago

constexpr versus const in what contexts? E.g., for global variables or static data members they're effectively the same thing?

I didn't mean it in that way. The functionality I want is to tell whether a variable is const or not. I don't care about const vs constexpr.

Currently the only way I found to figure that out is by parsing the output of GetDisplayTypeName method and look for the const keyword, but it would be nice to have that in a more convenient way, either a method or in one of the TypeFlags.

Michael137 commented 1 month ago

Ah thanks for clarifying. I'm not aware of such an API (@jimingham @clayborg might know?). I do see a TypeSystem::IsConst API (and the more general TypeSystem::GetTypeQualifiers) which has the capability of retrieving that information from an SBType. But I don't see it exposed anywhere. I guess there just weren't any use-cases for it so far?

Out of curiosity, what would you do with such information?

mvanotti commented 1 month ago

I'm using the lldb python API to understand how variables are used during an execution run. Being able to identify that a variable is const simplifies the logic a bit as I can safely filter it. This is useful in programs that have a lot of global constants in scope.

Michael137 commented 1 month ago

I'm using the lldb python API to understand how variables are used during an execution run. Being able to identify that a variable is const simplifies the logic a bit as I can safely filter it. This is useful in programs that have a lot of global constants in scope.

I don't see an issue with exposing that information. @jimingham , thoughts on adding something like SBType::IsConst, which just calls into TypeSystem::IsConst?