diff --git a/llvm/include/llvm/Support/Dwarf.h b/llvm/include/llvm/Support/Dwarf.h index be1273604f5c..b1bcb160fb6a 100644 --- a/llvm/include/llvm/Support/Dwarf.h +++ b/llvm/include/llvm/Support/Dwarf.h @@ -749,6 +749,7 @@ const char *GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage); /// @{ unsigned getTag(StringRef TagString); unsigned getLanguage(StringRef LanguageString); +unsigned getAttributeEncoding(StringRef EncodingString); /// @} /// \brief Returns the symbolic string representing Val when used as a value diff --git a/llvm/lib/Support/Dwarf.cpp b/llvm/lib/Support/Dwarf.cpp index 553a436938bf..af6db59302cd 100644 --- a/llvm/lib/Support/Dwarf.cpp +++ b/llvm/lib/Support/Dwarf.cpp @@ -414,6 +414,13 @@ const char *llvm::dwarf::AttributeEncodingString(unsigned Encoding) { } } +unsigned llvm::dwarf::getAttributeEncoding(StringRef EncodingString) { + return StringSwitch(EncodingString) +#define HANDLE_DW_ATE(ID, NAME) .Case("DW_ATE_" #NAME, DW_ATE_##NAME) +#include "llvm/Support/Dwarf.def" + .Default(0); +} + const char *llvm::dwarf::DecimalSignString(unsigned Sign) { switch (Sign) { case DW_DS_unsigned: return "DW_DS_unsigned"; diff --git a/llvm/unittests/Support/DwarfTest.cpp b/llvm/unittests/Support/DwarfTest.cpp index b1a23b31c1c2..25f5574ad7b9 100644 --- a/llvm/unittests/Support/DwarfTest.cpp +++ b/llvm/unittests/Support/DwarfTest.cpp @@ -79,4 +79,20 @@ TEST(DwarfTest, AttributeEncodingStringOnInvalid) { EXPECT_EQ(nullptr, AttributeEncodingString(DW_ATE_hi_user)); } +TEST(DwarfTest, getAttributeEncoding) { + // A couple of valid languages. + EXPECT_EQ(DW_ATE_boolean, getAttributeEncoding("DW_ATE_boolean")); + EXPECT_EQ(DW_ATE_imaginary_float, + getAttributeEncoding("DW_ATE_imaginary_float")); + + // Invalid languages. + EXPECT_EQ(0u, getAttributeEncoding("DW_ATE_invalid")); + EXPECT_EQ(0u, getAttributeEncoding("DW_TAG_array_type")); + EXPECT_EQ(0u, getAttributeEncoding("something else")); + + // AttributeEncoding range markers should not be recognized. + EXPECT_EQ(0u, getAttributeEncoding("DW_ATE_lo_user")); + EXPECT_EQ(0u, getAttributeEncoding("DW_ATE_hi_user")); +} + } // end namespace