[Lldb-commits] [lldb] [LLDB] Add `ScalarLiteralNode` and literal parsing in DIL (PR #152308)
Pavel Labath via lldb-commits
lldb-commits at lists.llvm.org
Fri Aug 8 03:58:10 PDT 2025
================
@@ -402,4 +404,122 @@ Interpreter::Visit(const BitFieldExtractionNode *node) {
return child_valobj_sp;
}
+static lldb::TypeSystemSP GetTypeSystemFromCU(std::shared_ptr<StackFrame> ctx) {
+ SymbolContext symbol_context =
+ ctx->GetSymbolContext(lldb::eSymbolContextCompUnit);
+ auto language = symbol_context.comp_unit->GetLanguage();
+
+ symbol_context = ctx->GetSymbolContext(lldb::eSymbolContextModule);
+ auto type_system =
+ symbol_context.module_sp->GetTypeSystemForLanguage(language);
+
+ if (type_system)
+ return *type_system;
+
+ return lldb::TypeSystemSP();
+}
+
+static CompilerType GetBasicType(lldb::TypeSystemSP type_system,
+ lldb::BasicType basic_type) {
+ if (type_system)
+ if (auto compiler_type = type_system.get()->GetBasicTypeFromAST(basic_type))
+ return compiler_type;
+
+ CompilerType empty_type;
+ return empty_type;
+}
+
+llvm::Expected<CompilerType>
+Interpreter::PickLiteralType(lldb::TypeSystemSP type_system,
+ std::shared_ptr<ExecutionContextScope> ctx,
+ const ScalarLiteralNode *literal) {
+ Scalar scalar = literal->GetValue();
+ if (scalar.GetType() == Scalar::e_float) {
+ if (literal->IsFloat())
+ return GetBasicType(type_system, lldb::eBasicTypeFloat);
+ return GetBasicType(type_system, lldb::eBasicTypeDouble);
+ } else if (scalar.GetType() == Scalar::e_int) {
+ // Binary, Octal, Hexadecimal and literals with a U suffix are allowed to be
+ // an unsigned integer.
+ bool unsigned_is_allowed =
+ literal->IsUnsigned() || literal->GetRadix() != 10;
+
+ // Try int/unsigned int.
+ uint64_t int_byte_size = 0;
+ if (auto temp = GetBasicType(type_system, lldb::eBasicTypeInt)
+ .GetByteSize(ctx.get()))
+ int_byte_size = *temp;
+ unsigned int_size = int_byte_size * CHAR_BIT;
+ llvm::APInt apint = scalar.GetAPSInt();
+ if (!literal->IsLong() && !literal->IsLongLong() &&
+ apint.isIntN(int_size)) {
+ if (!literal->IsUnsigned() && apint.isIntN(int_size - 1))
+ return GetBasicType(type_system, lldb::eBasicTypeInt);
+ if (unsigned_is_allowed)
+ return GetBasicType(type_system, lldb::eBasicTypeUnsignedInt);
+ }
+ // Try long/unsigned long.
+ uint64_t long_byte_size = 0;
+ if (auto temp = GetBasicType(type_system, lldb::eBasicTypeLong)
+ .GetByteSize(ctx.get()))
+ long_byte_size = *temp;
+ unsigned long_size = long_byte_size * CHAR_BIT;
+ if (!literal->IsLongLong() && apint.isIntN(long_size)) {
+ if (!literal->IsUnsigned() && apint.isIntN(long_size - 1))
+ return GetBasicType(type_system, lldb::eBasicTypeLong);
+ if (unsigned_is_allowed)
+ return GetBasicType(type_system, lldb::eBasicTypeUnsignedLong);
+ }
+ // Try long long/unsigned long long.
+ uint64_t long_long_byte_size = 0;
+ if (auto temp = GetBasicType(type_system, lldb::eBasicTypeLongLong)
+ .GetByteSize(ctx.get()))
+ long_long_byte_size = *temp;
+ unsigned long_long_size = long_long_byte_size * CHAR_BIT;
+ if (apint.isIntN(long_long_size)) {
+ if (!literal->IsUnsigned() && apint.isIntN(long_long_size - 1))
+ return GetBasicType(type_system, lldb::eBasicTypeLongLong);
+ // If we still couldn't decide a type, we probably have something that
+ // does not fit in a signed long long, but has no U suffix. Also known as:
+ //
+ // warning: integer literal is too large to be represented in a signed
+ // integer type, interpreting as unsigned [-Wimplicitly-unsigned-literal]
+ //
+ return GetBasicType(type_system, lldb::eBasicTypeUnsignedLongLong);
+ }
+ return llvm::make_error<DILDiagnosticError>(
+ m_expr,
+ "integer literal is too large to be represented in any integer type",
+ literal->GetLocation());
+ }
+ return llvm::make_error<DILDiagnosticError>(
+ m_expr, "unable to create a const literal", literal->GetLocation());
+}
+
+llvm::Expected<lldb::ValueObjectSP>
+Interpreter::Visit(const ScalarLiteralNode *node) {
+ auto type_system = GetTypeSystemFromCU(m_exe_ctx_scope);
+ if (type_system) {
----------------
labath wrote:
Invert the condition and make this an early exit: https://llvm.org/docs/CodingStandards.html#use-early-exits-and-continue-to-simplify-code
https://github.com/llvm/llvm-project/pull/152308
More information about the lldb-commits
mailing list