Support: Add string => unsigned mapping for DW_TAG
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>
Tue, 3 Feb 2015 21:16:49 +0000 (21:16 +0000)
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>
Tue, 3 Feb 2015 21:16:49 +0000 (21:16 +0000)
Add `dwarf::getTag()` to translate from `StringRef` to `unsigned`.

llvm-svn: 228031

llvm/include/llvm/Support/Dwarf.h
llvm/lib/Support/Dwarf.cpp
llvm/unittests/Support/DwarfTest.cpp

index e5d0c07..df13df4 100644 (file)
@@ -20,6 +20,7 @@
 #ifndef LLVM_SUPPORT_DWARF_H
 #define LLVM_SUPPORT_DWARF_H
 
+#include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/DataTypes.h"
 
@@ -783,6 +784,9 @@ const char *GDBIndexEntryKindString(GDBIndexEntryKind Kind);
 const char *GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage);
 /// @}
 
+/// \brief Get the tag number associated with a tag string.
+unsigned getTag(StringRef TagString);
+
 /// \brief Returns the symbolic string representing Val when used as a value
 /// for attribute Attr.
 const char *AttributeValueString(uint16_t Attr, unsigned Val);
index 255d357..80dfd10 100644 (file)
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Support/Dwarf.h"
+#include "llvm/ADT/StringSwitch.h"
 #include "llvm/Support/ErrorHandling.h"
 
 using namespace llvm;
@@ -27,6 +28,13 @@ const char *llvm::dwarf::TagString(unsigned Tag) {
   }
 }
 
+unsigned llvm::dwarf::getTag(StringRef TagString) {
+  return StringSwitch<unsigned>(TagString)
+#define HANDLE_DW_TAG(ID, NAME) .Case("DW_TAG_" #NAME, DW_TAG_##NAME)
+#include "llvm/Support/Dwarf.def"
+      .Default(DW_TAG_invalid);
+}
+
 const char *llvm::dwarf::ChildrenString(unsigned Children) {
   switch (Children) {
   case DW_CHILDREN_no:                   return "DW_CHILDREN_no";
index 9b14160..d016aac 100644 (file)
@@ -26,4 +26,20 @@ TEST(DwarfTest, TagStringOnInvalid) {
   EXPECT_EQ(nullptr, TagString(DW_TAG_user_base));
 }
 
+TEST(DwarfTest, getTag) {
+  // A couple of valid tags.
+  EXPECT_EQ(DW_TAG_array_type, getTag("DW_TAG_array_type"));
+  EXPECT_EQ(DW_TAG_module, getTag("DW_TAG_module"));
+
+  // Invalid tags.
+  EXPECT_EQ(DW_TAG_invalid, getTag("DW_TAG_invalid"));
+  EXPECT_EQ(DW_TAG_invalid, getTag("DW_TAG_madeuptag"));
+  EXPECT_EQ(DW_TAG_invalid, getTag("something else"));
+
+  // Tag range markers should not be recognized.
+  EXPECT_EQ(DW_TAG_invalid, getTag("DW_TAG_lo_user"));
+  EXPECT_EQ(DW_TAG_invalid, getTag("DW_TAG_hi_user"));
+  EXPECT_EQ(DW_TAG_invalid, getTag("DW_TAG_user_base"));
+}
+
 } // end namespace