[Lldb-commits] [lldb] [lldb] Mark single-argument SourceLanguage constructors explicit (PR #166527)
Michael Buch via lldb-commits
lldb-commits at lists.llvm.org
Wed Nov 5 08:59:38 PST 2025
https://github.com/Michael137 updated https://github.com/llvm/llvm-project/pull/166527
>From 2b5d98553f6df4e6808fe122c68ac95a32462a87 Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Wed, 5 Nov 2025 10:03:53 +0000
Subject: [PATCH 1/4] [lldb] Mark single-argument SourceLanguage constructors
explicit
This avoids unintentional comparisons between `SourceLanguage` and `LanguageType`.
Also marks `operator bool` explicit so we don't implicitly convert to bool.
---
lldb/include/lldb/lldb-private-types.h | 12 +++++++++---
lldb/source/Breakpoint/BreakpointLocation.cpp | 2 +-
lldb/source/Commands/CommandObjectDWIMPrint.cpp | 12 ++++++------
lldb/source/Expression/UserExpression.cpp | 2 +-
.../ExpressionParser/Clang/ClangExpressionParser.cpp | 2 +-
lldb/source/Target/Language.cpp | 4 ++++
lldb/source/Target/StackFrame.cpp | 6 +++---
lldb/source/Target/Target.cpp | 2 +-
8 files changed, 26 insertions(+), 16 deletions(-)
diff --git a/lldb/include/lldb/lldb-private-types.h b/lldb/include/lldb/lldb-private-types.h
index b82a2b8aa0574..aff6702af2efa 100644
--- a/lldb/include/lldb/lldb-private-types.h
+++ b/lldb/include/lldb/lldb-private-types.h
@@ -102,18 +102,24 @@ struct RegisterSet {
/// A type-erased pair of llvm::dwarf::SourceLanguageName and version.
struct SourceLanguage {
SourceLanguage() = default;
- SourceLanguage(lldb::LanguageType language_type);
+ explicit SourceLanguage(lldb::LanguageType language_type);
+
SourceLanguage(uint16_t name, uint32_t version)
: name(name), version(version) {}
- SourceLanguage(std::optional<std::pair<uint16_t, uint32_t>> name_vers)
+
+ explicit SourceLanguage(
+ std::optional<std::pair<uint16_t, uint32_t>> name_vers)
: name(name_vers ? name_vers->first : 0),
version(name_vers ? name_vers->second : 0) {}
- operator bool() const { return name > 0; }
+
+ explicit operator bool() const { return name > 0; }
+
lldb::LanguageType AsLanguageType() const;
llvm::StringRef GetDescription() const;
bool IsC() const;
bool IsObjC() const;
bool IsCPlusPlus() const;
+ bool IsSwift() const;
uint16_t name = 0;
uint32_t version = 0;
};
diff --git a/lldb/source/Breakpoint/BreakpointLocation.cpp b/lldb/source/Breakpoint/BreakpointLocation.cpp
index f25209c15e007..25285beb7ffd5 100644
--- a/lldb/source/Breakpoint/BreakpointLocation.cpp
+++ b/lldb/source/Breakpoint/BreakpointLocation.cpp
@@ -251,7 +251,7 @@ bool BreakpointLocation::ConditionSaysStop(ExecutionContext &exe_ctx,
}
m_user_expression_sp.reset(GetTarget().GetUserExpressionForLanguage(
- condition.GetText(), llvm::StringRef(), language,
+ condition.GetText(), llvm::StringRef(), SourceLanguage{language},
Expression::eResultTypeAny, EvaluateExpressionOptions(), nullptr,
error));
if (error.Fail()) {
diff --git a/lldb/source/Commands/CommandObjectDWIMPrint.cpp b/lldb/source/Commands/CommandObjectDWIMPrint.cpp
index 0d9eb45732161..df4c5c4ba6129 100644
--- a/lldb/source/Commands/CommandObjectDWIMPrint.cpp
+++ b/lldb/source/Commands/CommandObjectDWIMPrint.cpp
@@ -95,9 +95,9 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
StackFrame *frame = m_exe_ctx.GetFramePtr();
// Either the language was explicitly specified, or we check the frame.
- lldb::LanguageType language = m_expr_options.language;
- if (language == lldb::eLanguageTypeUnknown && frame)
- language = frame->GuessLanguage().AsLanguageType();
+ SourceLanguage language{m_expr_options.language};
+ if (!language && frame)
+ language = frame->GuessLanguage();
// Add a hint if object description was requested, but no description
// function was implemented.
@@ -119,8 +119,7 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
"^<\\S+: 0x[[:xdigit:]]{5,}>\\s*$");
if (GetDebugger().GetShowDontUsePoHint() && target_ptr &&
- (language == lldb::eLanguageTypeSwift ||
- language == lldb::eLanguageTypeObjC) &&
+ (language.IsSwift() || language.IsObjC()) &&
std::regex_match(output.data(), swift_class_regex)) {
result.AppendNote(
@@ -193,7 +192,8 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
// Second, try `expr` as a persistent variable.
if (expr.starts_with("$"))
- if (auto *state = target.GetPersistentExpressionStateForLanguage(language))
+ if (auto *state = target.GetPersistentExpressionStateForLanguage(
+ language.AsLanguageType()))
if (auto var_sp = state->GetVariable(expr))
if (auto valobj_sp = var_sp->GetValueObject()) {
dump_val_object(*valobj_sp);
diff --git a/lldb/source/Expression/UserExpression.cpp b/lldb/source/Expression/UserExpression.cpp
index af4b477660eeb..3d569d250f9d2 100644
--- a/lldb/source/Expression/UserExpression.cpp
+++ b/lldb/source/Expression/UserExpression.cpp
@@ -246,7 +246,7 @@ UserExpression::Evaluate(ExecutionContext &exe_ctx,
// language in the target's properties if specified, else default to the
// langage for the frame.
if (!language) {
- if (target->GetLanguage() != lldb::eLanguageTypeUnknown)
+ if (!target->GetLanguage())
language = target->GetLanguage();
else if (StackFrame *frame = exe_ctx.GetFramePtr())
language = frame->GetLanguage();
diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
index 990074566be7e..16795247aba68 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
+++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
@@ -1502,7 +1502,7 @@ lldb_private::Status ClangExpressionParser::DoPrepareForExecution(
LLDB_LOGF(log, "%s - Current expression language is %s\n", __FUNCTION__,
lang.GetDescription().data());
lldb::ProcessSP process_sp = exe_ctx.GetProcessSP();
- if (process_sp && lang != lldb::eLanguageTypeUnknown) {
+ if (process_sp && !lang) {
auto runtime = process_sp->GetLanguageRuntime(lang.AsLanguageType());
if (runtime)
runtime->GetIRPasses(custom_passes);
diff --git a/lldb/source/Target/Language.cpp b/lldb/source/Target/Language.cpp
index 8268d4ae4bb27..cc8563d6bd1c7 100644
--- a/lldb/source/Target/Language.cpp
+++ b/lldb/source/Target/Language.cpp
@@ -593,3 +593,7 @@ bool SourceLanguage::IsObjC() const {
bool SourceLanguage::IsCPlusPlus() const {
return name == llvm::dwarf::DW_LNAME_C_plus_plus;
}
+
+bool SourceLanguage::IsSwift() const {
+ return name == llvm::dwarf::DW_LNAME_Swift;
+}
diff --git a/lldb/source/Target/StackFrame.cpp b/lldb/source/Target/StackFrame.cpp
index 2ed58c5331df4..95b515412d693 100644
--- a/lldb/source/Target/StackFrame.cpp
+++ b/lldb/source/Target/StackFrame.cpp
@@ -1344,18 +1344,18 @@ const char *StackFrame::GetDisplayFunctionName() {
SourceLanguage StackFrame::GetLanguage() {
CompileUnit *cu = GetSymbolContext(eSymbolContextCompUnit).comp_unit;
if (cu)
- return cu->GetLanguage();
+ return SourceLanguage{cu->GetLanguage()};
return {};
}
SourceLanguage StackFrame::GuessLanguage() {
SourceLanguage lang_type = GetLanguage();
- if (lang_type == eLanguageTypeUnknown) {
+ if (!lang_type) {
SymbolContext sc =
GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol);
if (sc.function)
- lang_type = LanguageType(sc.function->GetMangled().GuessLanguage());
+ lang_type = SourceLanguage(sc.function->GetMangled().GuessLanguage());
else if (sc.symbol)
lang_type = SourceLanguage(sc.symbol->GetMangled().GuessLanguage());
}
diff --git a/lldb/source/Target/Target.cpp b/lldb/source/Target/Target.cpp
index a23091ad09c6d..e53fc7a1e1bda 100644
--- a/lldb/source/Target/Target.cpp
+++ b/lldb/source/Target/Target.cpp
@@ -4945,7 +4945,7 @@ void TargetProperties::SetStandardErrorPath(llvm::StringRef path) {
SourceLanguage TargetProperties::GetLanguage() const {
const uint32_t idx = ePropertyLanguage;
- return {GetPropertyAtIndexAs<LanguageType>(idx, {})};
+ return SourceLanguage{GetPropertyAtIndexAs<LanguageType>(idx, {})};
}
llvm::StringRef TargetProperties::GetExpressionPrefixContents() {
>From 8f4e7a5d399d338cbf21c031a769987ddcbcc9eb Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Wed, 5 Nov 2025 10:28:43 +0000
Subject: [PATCH 2/4] fixup! fix condition
---
.../Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
index 16795247aba68..6bab880b4d521 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
+++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
@@ -1502,7 +1502,7 @@ lldb_private::Status ClangExpressionParser::DoPrepareForExecution(
LLDB_LOGF(log, "%s - Current expression language is %s\n", __FUNCTION__,
lang.GetDescription().data());
lldb::ProcessSP process_sp = exe_ctx.GetProcessSP();
- if (process_sp && !lang) {
+ if (process_sp && lang) {
auto runtime = process_sp->GetLanguageRuntime(lang.AsLanguageType());
if (runtime)
runtime->GetIRPasses(custom_passes);
>From e7c102745af34ecb90ec382ccebe38c5f59e38e8 Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Wed, 5 Nov 2025 10:39:58 +0000
Subject: [PATCH 3/4] fixup! fix another condition
---
lldb/source/Expression/UserExpression.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lldb/source/Expression/UserExpression.cpp b/lldb/source/Expression/UserExpression.cpp
index 3d569d250f9d2..5563eba21777e 100644
--- a/lldb/source/Expression/UserExpression.cpp
+++ b/lldb/source/Expression/UserExpression.cpp
@@ -246,7 +246,7 @@ UserExpression::Evaluate(ExecutionContext &exe_ctx,
// language in the target's properties if specified, else default to the
// langage for the frame.
if (!language) {
- if (!target->GetLanguage())
+ if (target->GetLanguage())
language = target->GetLanguage();
else if (StackFrame *frame = exe_ctx.GetFramePtr())
language = frame->GetLanguage();
>From 0154c54c6135f4fa49afbfac8b560c9998efe431 Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Wed, 5 Nov 2025 16:59:25 +0000
Subject: [PATCH 4/4] fixup! remove IsSwift API
---
lldb/include/lldb/lldb-private-types.h | 1 -
lldb/source/Commands/CommandObjectDWIMPrint.cpp | 3 ++-
lldb/source/Target/Language.cpp | 4 ----
3 files changed, 2 insertions(+), 6 deletions(-)
diff --git a/lldb/include/lldb/lldb-private-types.h b/lldb/include/lldb/lldb-private-types.h
index aff6702af2efa..185467e91bf62 100644
--- a/lldb/include/lldb/lldb-private-types.h
+++ b/lldb/include/lldb/lldb-private-types.h
@@ -119,7 +119,6 @@ struct SourceLanguage {
bool IsC() const;
bool IsObjC() const;
bool IsCPlusPlus() const;
- bool IsSwift() const;
uint16_t name = 0;
uint32_t version = 0;
};
diff --git a/lldb/source/Commands/CommandObjectDWIMPrint.cpp b/lldb/source/Commands/CommandObjectDWIMPrint.cpp
index df4c5c4ba6129..40f00c90bbbfb 100644
--- a/lldb/source/Commands/CommandObjectDWIMPrint.cpp
+++ b/lldb/source/Commands/CommandObjectDWIMPrint.cpp
@@ -119,7 +119,8 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
"^<\\S+: 0x[[:xdigit:]]{5,}>\\s*$");
if (GetDebugger().GetShowDontUsePoHint() && target_ptr &&
- (language.IsSwift() || language.IsObjC()) &&
+ (language.AsLanguageType() == lldb::eLanguageTypeSwift ||
+ language.IsObjC()) &&
std::regex_match(output.data(), swift_class_regex)) {
result.AppendNote(
diff --git a/lldb/source/Target/Language.cpp b/lldb/source/Target/Language.cpp
index cc8563d6bd1c7..8268d4ae4bb27 100644
--- a/lldb/source/Target/Language.cpp
+++ b/lldb/source/Target/Language.cpp
@@ -593,7 +593,3 @@ bool SourceLanguage::IsObjC() const {
bool SourceLanguage::IsCPlusPlus() const {
return name == llvm::dwarf::DW_LNAME_C_plus_plus;
}
-
-bool SourceLanguage::IsSwift() const {
- return name == llvm::dwarf::DW_LNAME_Swift;
-}
More information about the lldb-commits
mailing list