[Lldb-commits] [PATCH] D142034: [lldb][Language] List supported languages in expr error text

Michael Buch via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Wed Jan 18 09:38:35 PST 2023


Michael137 updated this revision to Diff 490203.
Michael137 added a comment.

- Language name pair can be `const &`


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D142034/new/

https://reviews.llvm.org/D142034

Files:
  lldb/include/lldb/Target/Language.h
  lldb/source/Commands/CommandObjectExpression.cpp
  lldb/source/Target/Language.cpp
  lldb/test/API/commands/expression/invalid-args/TestInvalidArgsExpression.py


Index: lldb/test/API/commands/expression/invalid-args/TestInvalidArgsExpression.py
===================================================================
--- lldb/test/API/commands/expression/invalid-args/TestInvalidArgsExpression.py
+++ lldb/test/API/commands/expression/invalid-args/TestInvalidArgsExpression.py
@@ -7,7 +7,9 @@
     @no_debug_info_test
     def test_invalid_lang(self):
         self.expect("expression -l foo --", error=True,
-                    substrs=["error: unknown language type: 'foo' for expression"])
+                    substrs=["error: unknown language type: 'foo' for expression",
+                             "List of supported languages:",
+                             "c++", "c++11", "c++14"])
 
     @no_debug_info_test
     def test_invalid_all_thread(self):
Index: lldb/source/Target/Language.cpp
===================================================================
--- lldb/source/Target/Language.cpp
+++ lldb/source/Target/Language.cpp
@@ -221,6 +221,17 @@
     return language_names[eLanguageTypeUnknown].name;
 }
 
+void Language::PrintSupportedLanguagesForExpressions(Stream &s,
+                                                     llvm::StringRef prefix,
+                                                     llvm::StringRef suffix) {
+  auto supported = Language::GetLanguagesSupportingTypeSystemsForExpressions();
+  for (size_t idx = 0; idx < num_languages; ++idx) {
+    auto const &lang = language_names[idx];
+    if (supported[lang.type])
+      s << prefix << lang.name << suffix;
+  }
+}
+
 void Language::PrintAllLanguages(Stream &s, const char *prefix,
                                  const char *suffix) {
   for (uint32_t i = 1; i < num_languages; i++) {
Index: lldb/source/Commands/CommandObjectExpression.cpp
===================================================================
--- lldb/source/Commands/CommandObjectExpression.cpp
+++ lldb/source/Commands/CommandObjectExpression.cpp
@@ -42,10 +42,15 @@
   switch (short_option) {
   case 'l':
     language = Language::GetLanguageTypeFromString(option_arg);
-    if (language == eLanguageTypeUnknown)
-      error.SetErrorStringWithFormat(
-          "unknown language type: '%s' for expression",
-          option_arg.str().c_str());
+    if (language == eLanguageTypeUnknown) {
+      StreamString sstr;
+      sstr.Printf("unknown language type: '%s' for expression. "
+                  "List of supported languages:\n",
+                  option_arg.str().c_str());
+
+      Language::PrintSupportedLanguagesForExpressions(sstr, "  ", "\n");
+      error.SetErrorString(sstr.GetString());
+    }
     break;
 
   case 'a': {
Index: lldb/include/lldb/Target/Language.h
===================================================================
--- lldb/include/lldb/Target/Language.h
+++ lldb/include/lldb/Target/Language.h
@@ -279,6 +279,16 @@
   static void PrintAllLanguages(Stream &s, const char *prefix,
                                 const char *suffix);
 
+  /// Prints to the specified stream 's' each language type that the
+  /// current target supports for expression evaluation.
+  ///
+  /// \param[out] s      Stream to which the language types are written.
+  /// \param[in]  prefix String that is prepended to the language type.
+  /// \param[in]  suffix String that is appended to the language type.
+  static void PrintSupportedLanguagesForExpressions(Stream &s,
+                                                    llvm::StringRef prefix,
+                                                    llvm::StringRef suffix);
+
   // return false from callback to stop iterating
   static void ForAllLanguages(std::function<bool(lldb::LanguageType)> callback);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142034.490203.patch
Type: text/x-patch
Size: 3684 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20230118/de787ed3/attachment.bin>


More information about the lldb-commits mailing list