[Lldb-commits] [lldb] [lldb][Lanugage] Add Language::GetDisplayNameForLanguageType API (PR #161803)

Michael Buch via lldb-commits lldb-commits at lists.llvm.org
Fri Oct 3 01:40:58 PDT 2025


https://github.com/Michael137 updated https://github.com/llvm/llvm-project/pull/161803

>From 159ee47db1b9f6f609fe8b6b3e866f995d8f2e5c Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Fri, 3 Oct 2025 09:37:52 +0100
Subject: [PATCH 1/2] [lldb][Language] Simplify SourceLanguage::GetDescription

Currently we don't benefit from the user-friendly names that `LanguageDescription` returns because we would always use `Language::GetNameForLanguageType`. I'm not aware of a situation where `GetDescription` should prefer the non-human readable form of the name with. This patch removes the call to `GetNameForLanguageType`.
---
 lldb/source/Target/Language.cpp | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/lldb/source/Target/Language.cpp b/lldb/source/Target/Language.cpp
index 484d9badde397..7ce85dd93f091 100644
--- a/lldb/source/Target/Language.cpp
+++ b/lldb/source/Target/Language.cpp
@@ -559,11 +559,8 @@ lldb::LanguageType SourceLanguage::AsLanguageType() const {
 }
 
 llvm::StringRef SourceLanguage::GetDescription() const {
-  LanguageType type = AsLanguageType();
-  if (type)
-    return Language::GetNameForLanguageType(type);
   return llvm::dwarf::LanguageDescription(
-      (llvm::dwarf::SourceLanguageName)name);
+      static_cast<llvm::dwarf::SourceLanguageName>(name));
 }
 bool SourceLanguage::IsC() const { return name == llvm::dwarf::DW_LNAME_C; }
 

>From b728905bdba9a19048ed9cb80c9f9bf3817ed3fc Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Fri, 3 Oct 2025 09:24:49 +0100
Subject: [PATCH 2/2] [lldb][Lanugage] Add
 Language::GetDisplayNameForLanguageType API

The intention for this API is to be used when presenting language names to the user, e.g., in expression evaluation diagnostics or LLDB errors.

Most uses of `GetNameForLanguageType` can be probably replaced with `GetDisplayNameForLanguageType`, but that's out of scope of this PR.

This uses `llvm::dwarf::LanguageDescription` under the hood, so we would lose the version numbers in the names. If we deem those to be important, we could switch to an implementation that hardcodes a list of user-friendly names with version numbers included.
---
 lldb/include/lldb/Target/Language.h | 7 +++++++
 lldb/source/Target/Language.cpp     | 4 ++++
 2 files changed, 11 insertions(+)

diff --git a/lldb/include/lldb/Target/Language.h b/lldb/include/lldb/Target/Language.h
index 3d0aa326d5a6d..48a7ac2cea42f 100644
--- a/lldb/include/lldb/Target/Language.h
+++ b/lldb/include/lldb/Target/Language.h
@@ -404,8 +404,15 @@ class Language : public PluginInterface {
   GetLanguageTypeFromString(const char *string) = delete;
   static lldb::LanguageType GetLanguageTypeFromString(llvm::StringRef string);
 
+  /// Returns the internal LLDB name for the specified language. When presenting
+  /// the language name to users, use \ref GetDisplayNameForLanguageType
+  /// instead.
   static const char *GetNameForLanguageType(lldb::LanguageType language);
 
+  /// Returns a user-friendly name for the specified language.
+  static llvm::StringRef
+  GetDisplayNameForLanguageType(lldb::LanguageType language);
+
   static void PrintAllLanguages(Stream &s, const char *prefix,
                                 const char *suffix);
 
diff --git a/lldb/source/Target/Language.cpp b/lldb/source/Target/Language.cpp
index 7ce85dd93f091..e20152d541679 100644
--- a/lldb/source/Target/Language.cpp
+++ b/lldb/source/Target/Language.cpp
@@ -270,6 +270,10 @@ const char *Language::GetNameForLanguageType(LanguageType language) {
     return language_names[eLanguageTypeUnknown].name;
 }
 
+llvm::StringRef Language::GetDisplayNameForLanguageType(LanguageType language) {
+  return SourceLanguage(language).GetDescription();
+}
+
 void Language::PrintSupportedLanguagesForExpressions(Stream &s,
                                                      llvm::StringRef prefix,
                                                      llvm::StringRef suffix) {



More information about the lldb-commits mailing list