- 
        Couldn't load subscription status. 
- Fork 15k
[llvm][Dwarf] Add DW_LNAME_ string/enum getters #162434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| @llvm/pr-subscribers-debuginfo @llvm/pr-subscribers-llvm-binary-utilities Author: Michael Buch (Michael137) ChangesThese are going to be used in a follow-up patch. And they are generally useful for consumers (we have these for most other attributes). Full diff: https://github.com/llvm/llvm-project/pull/162434.diff 3 Files Affected: 
 diff --git a/llvm/include/llvm/BinaryFormat/Dwarf.h b/llvm/include/llvm/BinaryFormat/Dwarf.h
index 2c5012510a5c3..ba74ab9515a75 100644
--- a/llvm/include/llvm/BinaryFormat/Dwarf.h
+++ b/llvm/include/llvm/BinaryFormat/Dwarf.h
@@ -997,6 +997,7 @@ LLVM_ABI StringRef VisibilityString(unsigned Visibility);
 LLVM_ABI StringRef VirtualityString(unsigned Virtuality);
 LLVM_ABI StringRef EnumKindString(unsigned EnumKind);
 LLVM_ABI StringRef LanguageString(unsigned Language);
+LLVM_ABI StringRef SourceLanguageNameString(SourceLanguageName Lang);
 LLVM_ABI StringRef CaseString(unsigned Case);
 LLVM_ABI StringRef ConventionString(unsigned Convention);
 LLVM_ABI StringRef InlineCodeString(unsigned Code);
@@ -1038,6 +1039,7 @@ LLVM_ABI unsigned getSubOperationEncoding(unsigned OpEncoding,
 LLVM_ABI unsigned getVirtuality(StringRef VirtualityString);
 LLVM_ABI unsigned getEnumKind(StringRef EnumKindString);
 LLVM_ABI unsigned getLanguage(StringRef LanguageString);
+LLVM_ABI unsigned getSourceLanguageName(StringRef SourceLanguageNameString);
 LLVM_ABI unsigned getCallingConvention(StringRef LanguageString);
 LLVM_ABI unsigned getAttributeEncoding(StringRef EncodingString);
 LLVM_ABI unsigned getMacinfo(StringRef MacinfoString);
diff --git a/llvm/lib/BinaryFormat/Dwarf.cpp b/llvm/lib/BinaryFormat/Dwarf.cpp
index 8b24044e19e50..9690ff9107df8 100644
--- a/llvm/lib/BinaryFormat/Dwarf.cpp
+++ b/llvm/lib/BinaryFormat/Dwarf.cpp
@@ -472,6 +472,26 @@ StringRef llvm::dwarf::LanguageDescription(dwarf::SourceLanguageName lname) {
   return "Unknown";
 }
 
+llvm::StringRef llvm::dwarf::SourceLanguageNameString(SourceLanguageName Lang) {
+  switch (Lang) {
+#define HANDLE_DW_LNAME(ID, NAME, DESC, LOWER_BOUND)                           \
+  case DW_LNAME_##NAME:                                                        \
+    return "DW_LNAME_" #NAME;
+#include "llvm/BinaryFormat/Dwarf.def"
+  }
+
+  return {};
+}
+
+unsigned
+llvm::dwarf::getSourceLanguageName(StringRef SourceLanguageNameString) {
+  return StringSwitch<unsigned>(SourceLanguageNameString)
+#define HANDLE_DW_LNAME(ID, NAME, DESC, LOWER_BOUND)                           \
+  .Case("DW_LNAME_" #NAME, DW_LNAME_##NAME)
+#include "llvm/BinaryFormat/Dwarf.def"
+      .Default(0);
+}
+
 StringRef llvm::dwarf::CaseString(unsigned Case) {
   switch (Case) {
   case DW_ID_case_sensitive:
diff --git a/llvm/unittests/BinaryFormat/DwarfTest.cpp b/llvm/unittests/BinaryFormat/DwarfTest.cpp
index 684e59fa2785c..0461e0ee50aba 100644
--- a/llvm/unittests/BinaryFormat/DwarfTest.cpp
+++ b/llvm/unittests/BinaryFormat/DwarfTest.cpp
@@ -219,4 +219,37 @@ TEST(DwarfTest, lname) {
   EXPECT_EQ(roundtrip(DW_LANG_##NAME), DW_LANG_##NAME);
 #include "llvm/BinaryFormat/Dwarf.def"
 }
+
+TEST(DwarfTest, lname_getSourceLanguageName) {
+  // Some basics.
+  EXPECT_EQ(getSourceLanguageName("DW_LNAME_Ada"), DW_LNAME_Ada);
+  EXPECT_EQ(getSourceLanguageName("DW_LNAME_Metal"), DW_LNAME_Metal);
+
+  // Test invalid input.
+  EXPECT_EQ(getSourceLanguageName(""), 0U);
+  EXPECT_EQ(getSourceLanguageName("blah"), 0U);
+  EXPECT_EQ(getSourceLanguageName("DW_LNAME__something_unlikely"), 0U);
+  EXPECT_EQ(getSourceLanguageName("DW_LANG_C"), 0U);
+
+  // Test that we cover all DW_LNAME_ names.
+#define HANDLE_DW_LNAME(ID, NAME, DESC, LOWER_BOUND)                           \
+  EXPECT_EQ(getSourceLanguageName("DW_LNAME_##NAME"), DW_LNAME_##NAME);
+#include "llvm/BinaryFormat/Dwarf.def"
+}
+
+TEST(DwarfTest, lname_getSourceLanguageNameString) {
+  // Some basics.
+  EXPECT_EQ(SourceLanguageNameString(DW_LNAME_C_plus_plus),
+            "DW_LNAME_C_plus_plus");
+  EXPECT_EQ(SourceLanguageNameString(DW_LNAME_CPP_for_OpenCL),
+            "DW_LNAME_CPP_for_OpenCL");
+
+  // Test invalid input.
+  EXPECT_EQ(SourceLanguageNameString(static_cast<SourceLanguageName>(0)), "");
+
+  // Test that we cover all DW_LNAME_ names.
+#define HANDLE_DW_LNAME(ID, NAME, DESC, LOWER_BOUND)                           \
+  EXPECT_EQ(SourceLanguageNameString(DW_LNAME_##NAME), "DW_LNAME_##NAME");
+#include "llvm/BinaryFormat/Dwarf.def"
+}
 } // end namespace
 | 
These are going to be used in a follow-up patch. And they are generally useful for consumers (we have these for most other attributes).
7881319    to
    ddb4a2d      
    Compare
  
    These are going to be used in a follow-up patch. And they are generally useful for consumers (we have these for most other attributes).
These are going to be used in a follow-up patch. And they are generally useful for consumers (we have these for most other attributes).
…ersion (#162449) Depends on: * #162434 * #162446 Makes sure we dump the attributes in a user-friendly way. More info on the attributes here: https://dwarfstd.org/languages-v6.html
…_language_version (#162449) Depends on: * llvm/llvm-project#162434 * llvm/llvm-project#162446 Makes sure we dump the attributes in a user-friendly way. More info on the attributes here: https://dwarfstd.org/languages-v6.html
…o DICompileUnit (#162445) Depends on: * llvm/llvm-project#162255 * llvm/llvm-project#162434 Part of a patch series to support the DWARFv6 `DW_AT_language_name`/`DW_AT_language_version` attributes.
…ersion (llvm#162449) Depends on: * llvm#162434 * llvm#162446 Makes sure we dump the attributes in a user-friendly way. More info on the attributes here: https://dwarfstd.org/languages-v6.html
…Unit (llvm#162445) Depends on: * llvm#162255 * llvm#162434 Part of a patch series to support the DWARFv6 `DW_AT_language_name`/`DW_AT_language_version` attributes.
These are going to be used in a follow-up patch. And they are generally useful for consumers (we have these for most other attributes).
…ersion (llvm#162449) Depends on: * llvm#162434 * llvm#162446 Makes sure we dump the attributes in a user-friendly way. More info on the attributes here: https://dwarfstd.org/languages-v6.html
…Unit (llvm#162445) Depends on: * llvm#162255 * llvm#162434 Part of a patch series to support the DWARFv6 `DW_AT_language_name`/`DW_AT_language_version` attributes.
These are going to be used in a follow-up patch. And they are generally useful for consumers (we have these for most other attributes). (cherry picked from commit 9ac8cd6)
These are going to be used in a follow-up patch. And they are generally useful for consumers (we have these for most other attributes). (cherry picked from commit 9ac8cd6)
These are going to be used in a follow-up patch. And they are generally useful for consumers (we have these for most other attributes).