Skip to content

Commit c2ef45f

Browse files
Michael137bulbazord
authored andcommitted
[lldb][Language] List supported languages in expr error text
Before: ``` (lldb) expr --language abc -- 1 + 1 error: unknown language type: 'abc' for expression ``` After: ``` (lldb) expr --language abc -- 1 + 1 error: unknown language type: 'abc' for expression. List of supported languages: c++ objective-c++ c++03 c++11 c++14 objc++ ``` We choose to only list the languages which `expr` will actually accept instead of all the language constants defined in `Language.cpp` since that's what the user will most likely need. Differential Revision: https://reviews.llvm.org/D142034 (cherry picked from commit b4a0b9f)
1 parent 7fee1ab commit c2ef45f

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

lldb/include/lldb/Target/Language.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,16 @@ class Language : public PluginInterface {
281281
static void PrintAllLanguages(Stream &s, const char *prefix,
282282
const char *suffix);
283283

284+
/// Prints to the specified stream 's' each language type that the
285+
/// current target supports for expression evaluation.
286+
///
287+
/// \param[out] s Stream to which the language types are written.
288+
/// \param[in] prefix String that is prepended to the language type.
289+
/// \param[in] suffix String that is appended to the language type.
290+
static void PrintSupportedLanguagesForExpressions(Stream &s,
291+
llvm::StringRef prefix,
292+
llvm::StringRef suffix);
293+
284294
// return false from callback to stop iterating
285295
static void ForAllLanguages(std::function<bool(lldb::LanguageType)> callback);
286296

lldb/source/Commands/CommandObjectExpression.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,15 @@ Status CommandObjectExpression::CommandOptions::SetOptionValue(
4747
switch (short_option) {
4848
case 'l':
4949
language = Language::GetLanguageTypeFromString(option_arg);
50-
if (language == eLanguageTypeUnknown)
51-
error.SetErrorStringWithFormat(
52-
"unknown language type: '%s' for expression",
53-
option_arg.str().c_str());
50+
if (language == eLanguageTypeUnknown) {
51+
StreamString sstr;
52+
sstr.Printf("unknown language type: '%s' for expression. "
53+
"List of supported languages:\n",
54+
option_arg.str().c_str());
55+
56+
Language::PrintSupportedLanguagesForExpressions(sstr, " ", "\n");
57+
error.SetErrorString(sstr.GetString());
58+
}
5459
break;
5560

5661
case 'a': {

lldb/source/Target/Language.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,17 @@ const char *Language::GetNameForLanguageType(LanguageType language) {
236236
return language_names[eLanguageTypeUnknown].name;
237237
}
238238

239+
void Language::PrintSupportedLanguagesForExpressions(Stream &s,
240+
llvm::StringRef prefix,
241+
llvm::StringRef suffix) {
242+
auto supported = Language::GetLanguagesSupportingTypeSystemsForExpressions();
243+
for (size_t idx = 0; idx < num_languages; ++idx) {
244+
auto const &lang = language_names[idx];
245+
if (supported[lang.type])
246+
s << prefix << lang.name << suffix;
247+
}
248+
}
249+
239250
void Language::PrintAllLanguages(Stream &s, const char *prefix,
240251
const char *suffix) {
241252
for (uint32_t i = 1; i < num_languages; i++) {

lldb/test/API/commands/expression/invalid-args/TestInvalidArgsExpression.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ class InvalidArgsExpressionTestCase(TestBase):
77
@no_debug_info_test
88
def test_invalid_lang(self):
99
self.expect("expression -l foo --", error=True,
10-
substrs=["error: unknown language type: 'foo' for expression"])
10+
substrs=["error: unknown language type: 'foo' for expression",
11+
"List of supported languages:",
12+
"c++", "c++11", "c++14"])
1113

1214
@no_debug_info_test
1315
def test_invalid_all_thread(self):

0 commit comments

Comments
 (0)