[llvm] r230107 - IR: Add debug info flag string conversions

Duncan P. N. Exon Smith dexonsmith at apple.com
Fri Feb 20 16:43:09 PST 2015


Author: dexonsmith
Date: Fri Feb 20 18:43:09 2015
New Revision: 230107

URL: http://llvm.org/viewvc/llvm-project?rev=230107&view=rev
Log:
IR: Add debug info flag string conversions

Add `DIDescriptor::getFlag(StringRef)` and
`DIDescriptor::getFlagString(unsigned)`.  The latter only converts exact
matches; I'll add separate API for breaking the flags bitfield up into
parts.

Modified:
    llvm/trunk/include/llvm/IR/DebugInfo.h
    llvm/trunk/lib/IR/DebugInfo.cpp
    llvm/trunk/unittests/IR/DebugInfoTest.cpp

Modified: llvm/trunk/include/llvm/IR/DebugInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/DebugInfo.h?rev=230107&r1=230106&r2=230107&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/DebugInfo.h (original)
+++ llvm/trunk/include/llvm/IR/DebugInfo.h Fri Feb 20 18:43:09 2015
@@ -137,6 +137,9 @@ public:
     FlagAccessibility = FlagPrivate | FlagProtected | FlagPublic
   };
 
+  static unsigned getFlag(StringRef Flag);
+  static const char *getFlagString(unsigned Flag);
+
 protected:
   const MDNode *DbgNode;
 

Modified: llvm/trunk/lib/IR/DebugInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/DebugInfo.cpp?rev=230107&r1=230106&r2=230107&view=diff
==============================================================================
--- llvm/trunk/lib/IR/DebugInfo.cpp (original)
+++ llvm/trunk/lib/IR/DebugInfo.cpp Fri Feb 20 18:43:09 2015
@@ -17,6 +17,7 @@
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringSwitch.h"
 #include "llvm/Analysis/ValueTracking.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/DIBuilder.h"
@@ -36,6 +37,24 @@ using namespace llvm::dwarf;
 // DIDescriptor
 //===----------------------------------------------------------------------===//
 
+unsigned DIDescriptor::getFlag(StringRef Flag) {
+  return StringSwitch<unsigned>(Flag)
+#define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME)
+#include "llvm/IR/DebugInfoFlags.def"
+      .Default(0);
+}
+
+const char *DIDescriptor::getFlagString(unsigned Flag) {
+  switch (Flag) {
+  default:
+    return "";
+#define HANDLE_DI_FLAG(ID, NAME)                                               \
+  case Flag##NAME:                                                             \
+    return "DIFlag" #NAME;
+#include "llvm/IR/DebugInfoFlags.def"
+  }
+}
+
 bool DIDescriptor::Verify() const {
   return DbgNode &&
          (DIDerivedType(DbgNode).Verify() ||

Modified: llvm/trunk/unittests/IR/DebugInfoTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/DebugInfoTest.cpp?rev=230107&r1=230106&r2=230107&view=diff
==============================================================================
--- llvm/trunk/unittests/IR/DebugInfoTest.cpp (original)
+++ llvm/trunk/unittests/IR/DebugInfoTest.cpp Fri Feb 20 18:43:09 2015
@@ -65,4 +65,50 @@ TEST(DebugInfoTest, DIHeaderFieldIterato
   ASSERT_EQ("stuff", *++MAKE_FIELD_ITERATOR("\0stuff"));
 }
 
+TEST(DIDescriptorTest, getFlag) {
+  // Some valid flags.
+  EXPECT_EQ(DIDescriptor::FlagPublic, DIDescriptor::getFlag("DIFlagPublic"));
+  EXPECT_EQ(DIDescriptor::FlagProtected,
+            DIDescriptor::getFlag("DIFlagProtected"));
+  EXPECT_EQ(DIDescriptor::FlagPrivate, DIDescriptor::getFlag("DIFlagPrivate"));
+  EXPECT_EQ(DIDescriptor::FlagVector, DIDescriptor::getFlag("DIFlagVector"));
+  EXPECT_EQ(DIDescriptor::FlagRValueReference,
+            DIDescriptor::getFlag("DIFlagRValueReference"));
+
+  // FlagAccessibility shouldn't work.
+  EXPECT_EQ(0u, DIDescriptor::getFlag("DIFlagAccessibility"));
+
+  // Some other invalid strings.
+  EXPECT_EQ(0u, DIDescriptor::getFlag("FlagVector"));
+  EXPECT_EQ(0u, DIDescriptor::getFlag("Vector"));
+  EXPECT_EQ(0u, DIDescriptor::getFlag("other things"));
+  EXPECT_EQ(0u, DIDescriptor::getFlag("DIFlagOther"));
+}
+
+TEST(DIDescriptorTest, getFlagString) {
+  // Some valid flags.
+  EXPECT_EQ(StringRef("DIFlagPublic"),
+            DIDescriptor::getFlagString(DIDescriptor::FlagPublic));
+  EXPECT_EQ(StringRef("DIFlagProtected"),
+            DIDescriptor::getFlagString(DIDescriptor::FlagProtected));
+  EXPECT_EQ(StringRef("DIFlagPrivate"),
+            DIDescriptor::getFlagString(DIDescriptor::FlagPrivate));
+  EXPECT_EQ(StringRef("DIFlagVector"),
+            DIDescriptor::getFlagString(DIDescriptor::FlagVector));
+  EXPECT_EQ(StringRef("DIFlagRValueReference"),
+            DIDescriptor::getFlagString(DIDescriptor::FlagRValueReference));
+
+  // FlagAccessibility actually equals FlagPublic.
+  EXPECT_EQ(StringRef("DIFlagPublic"),
+            DIDescriptor::getFlagString(DIDescriptor::FlagAccessibility));
+
+  // Some other invalid flags.
+  EXPECT_EQ(StringRef(), DIDescriptor::getFlagString(DIDescriptor::FlagPublic |
+                                                     DIDescriptor::FlagVector));
+  EXPECT_EQ(StringRef(),
+            DIDescriptor::getFlagString(DIDescriptor::FlagFwdDecl |
+                                        DIDescriptor::FlagArtificial));
+  EXPECT_EQ(StringRef(), DIDescriptor::getFlagString(0xffff));
+}
+
 } // end namespace





More information about the llvm-commits mailing list