[Lldb-commits] [lldb] [lldb][Expression] Reject languages not supported by TypeSystems for expression evaluation (PR #156648)
Michael Buch via lldb-commits
lldb-commits at lists.llvm.org
Wed Sep 3 12:04:05 PDT 2025
https://github.com/Michael137 updated https://github.com/llvm/llvm-project/pull/156648
>From 5689f9e8489c66237097891e98aba93571f8583f Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Wed, 3 Sep 2025 12:19:17 +0100
Subject: [PATCH 1/3] [lldb][Expression] Reject languages not supported by
TypeSystems for expression evaluation
There are some languages for which the `ClangExpressionParser` currently
switches the language type behind a user's back. Specifically, `C` gets
turned into `C++` and `ObjC` into `ObjC++`. That's because the Clang
expression evaluator depends on C++ features. These languages have
different semantics, so if, e.g., a user forcefully wants to evaluate an
expression in `C`, but we switch it to `C++`, we get reports from users
confused why the expression failed.
This patch rejects languages specified with `expression --language` that
we won't be able to explicitly evaluate.
rdar://159669244
---
lldb/source/Commands/CommandObjectExpression.cpp | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/lldb/source/Commands/CommandObjectExpression.cpp b/lldb/source/Commands/CommandObjectExpression.cpp
index 197bffe9c982f..accc46431d927 100644
--- a/lldb/source/Commands/CommandObjectExpression.cpp
+++ b/lldb/source/Commands/CommandObjectExpression.cpp
@@ -44,18 +44,21 @@ Status CommandObjectExpression::CommandOptions::SetOptionValue(
const int short_option = GetDefinitions()[option_idx].short_option;
switch (short_option) {
- case 'l':
+ case 'l': {
language = Language::GetLanguageTypeFromString(option_arg);
- if (language == eLanguageTypeUnknown) {
+
+ if (const LanguageSet supported_languages =
+ Language::GetLanguagesSupportingTypeSystemsForExpressions();
+ !supported_languages[language]) {
StreamString sstr;
- sstr.Printf("unknown language type: '%s' for expression. "
+ sstr.Printf("invalid language '%s' for expression. "
"List of supported languages:\n",
option_arg.str().c_str());
Language::PrintSupportedLanguagesForExpressions(sstr, " ", "\n");
error = Status(sstr.GetString().str());
}
- break;
+ } break;
case 'a': {
bool success;
>From 74e869631658d5db48f37faca35f586ab20ef2d2 Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Wed, 3 Sep 2025 12:36:57 +0100
Subject: [PATCH 2/3] fixup! tests
---
.../calculator_mode/TestCalculatorMode.py | 2 +-
.../invalid-args/TestInvalidArgsExpression.py | 14 +++++++++++++-
2 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/lldb/test/API/commands/expression/calculator_mode/TestCalculatorMode.py b/lldb/test/API/commands/expression/calculator_mode/TestCalculatorMode.py
index 138027507c7a7..f12b5b0a12814 100644
--- a/lldb/test/API/commands/expression/calculator_mode/TestCalculatorMode.py
+++ b/lldb/test/API/commands/expression/calculator_mode/TestCalculatorMode.py
@@ -21,7 +21,7 @@ def test__calculator_mode(self):
)
# Now try it with a specific language:
self.expect(
- "expression -l c -- 11 + 22",
+ "expression -l c++ -- 11 + 22",
"11 + 22 didn't get the expected result",
substrs=["33"],
)
diff --git a/lldb/test/API/commands/expression/invalid-args/TestInvalidArgsExpression.py b/lldb/test/API/commands/expression/invalid-args/TestInvalidArgsExpression.py
index 344aef318d783..ef34344627468 100644
--- a/lldb/test/API/commands/expression/invalid-args/TestInvalidArgsExpression.py
+++ b/lldb/test/API/commands/expression/invalid-args/TestInvalidArgsExpression.py
@@ -10,7 +10,19 @@ def test_invalid_lang(self):
"expression -l foo --",
error=True,
substrs=[
- "error: unknown language type: 'foo' for expression",
+ "error: invalid language 'foo' for expression.",
+ "List of supported languages:",
+ "c++",
+ "c++11",
+ "c++14",
+ ],
+ )
+
+ self.expect(
+ "expression -l c --",
+ error=True,
+ substrs=[
+ "error: invalid language 'c' for expression.",
"List of supported languages:",
"c++",
"c++11",
>From 9626c2edba3eef8381bdd94e5ce3121bf277b649 Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Wed, 3 Sep 2025 19:56:54 +0100
Subject: [PATCH 3/3] fixup! separate error messages for unknown vs. invalid
language
---
.../Commands/CommandObjectExpression.cpp | 21 ++++++++++++-------
.../invalid-args/TestInvalidArgsExpression.py | 8 +++----
2 files changed, 17 insertions(+), 12 deletions(-)
diff --git a/lldb/source/Commands/CommandObjectExpression.cpp b/lldb/source/Commands/CommandObjectExpression.cpp
index accc46431d927..1b951603563ac 100644
--- a/lldb/source/Commands/CommandObjectExpression.cpp
+++ b/lldb/source/Commands/CommandObjectExpression.cpp
@@ -46,18 +46,23 @@ Status CommandObjectExpression::CommandOptions::SetOptionValue(
switch (short_option) {
case 'l': {
language = Language::GetLanguageTypeFromString(option_arg);
-
if (const LanguageSet supported_languages =
Language::GetLanguagesSupportingTypeSystemsForExpressions();
- !supported_languages[language]) {
- StreamString sstr;
- sstr.Printf("invalid language '%s' for expression. "
- "List of supported languages:\n",
+ supported_languages[language])
+ break;
+
+ StreamString sstr;
+ if (language == eLanguageTypeUnknown)
+ sstr.Printf("unknown language '%s' for expression. ",
+ option_arg.str().c_str());
+ else
+ sstr.Printf("language '%s' is currently not supported for expression "
+ "evaluation. ",
option_arg.str().c_str());
- Language::PrintSupportedLanguagesForExpressions(sstr, " ", "\n");
- error = Status(sstr.GetString().str());
- }
+ sstr.PutCString("List of supported languages for expressions:\n");
+ Language::PrintSupportedLanguagesForExpressions(sstr, " ", "\n");
+ error = Status(sstr.GetString().str());
} break;
case 'a': {
diff --git a/lldb/test/API/commands/expression/invalid-args/TestInvalidArgsExpression.py b/lldb/test/API/commands/expression/invalid-args/TestInvalidArgsExpression.py
index ef34344627468..f0d1542d783b1 100644
--- a/lldb/test/API/commands/expression/invalid-args/TestInvalidArgsExpression.py
+++ b/lldb/test/API/commands/expression/invalid-args/TestInvalidArgsExpression.py
@@ -10,8 +10,8 @@ def test_invalid_lang(self):
"expression -l foo --",
error=True,
substrs=[
- "error: invalid language 'foo' for expression.",
- "List of supported languages:",
+ "error: unknown language 'foo' for expression.",
+ "List of supported languages for expressions:",
"c++",
"c++11",
"c++14",
@@ -22,8 +22,8 @@ def test_invalid_lang(self):
"expression -l c --",
error=True,
substrs=[
- "error: invalid language 'c' for expression.",
- "List of supported languages:",
+ "error: language 'c' is currently not supported for expression evaluation.",
+ "List of supported languages for expressions:",
"c++",
"c++11",
"c++14",
More information about the lldb-commits
mailing list