[llvm] r239429 - Reduce duplication in MCSymbol Name handling. NFC>

Pete Cooper peter_cooper at apple.com
Tue Jun 9 13:41:08 PDT 2015


Author: pete
Date: Tue Jun  9 15:41:08 2015
New Revision: 239429

URL: http://llvm.org/viewvc/llvm-project?rev=239429&view=rev
Log:
Reduce duplication in MCSymbol Name handling.  NFC>

Based on feedback to r239428 by David Blaikie, use const_cast to reduce
duplication of the const and non-const versions of getNameEntryPtr.

Also have that method return the pointer to the name directly instead
of users having to then get the name from the union.

Finally, add a FIXME that we should use a static_assert once available in
the new operator.

Modified:
    llvm/trunk/include/llvm/MC/MCSymbol.h
    llvm/trunk/lib/MC/MCSymbol.cpp

Modified: llvm/trunk/include/llvm/MC/MCSymbol.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/MC/MCSymbol.h?rev=239429&r1=239428&r2=239429&view=diff
==============================================================================
--- llvm/trunk/include/llvm/MC/MCSymbol.h (original)
+++ llvm/trunk/include/llvm/MC/MCSymbol.h Tue Jun  9 15:41:08 2015
@@ -136,7 +136,7 @@ protected: // MCContext creates and uniq
         Kind(Kind) {
     Offset = 0;
     if (Name)
-      getNameEntryPtr().NameEntry = Name;
+      getNameEntryPtr() = Name;
   }
 
   // Provide custom new/delete as we will only allocate space for a name
@@ -169,15 +169,13 @@ private:
   }
 
   /// \brief Get a reference to the name field.  Requires that we have a name
-  NameEntryStorageTy &getNameEntryPtr() {
+  const StringMapEntry<bool> *&getNameEntryPtr() {
     assert(HasName && "Name is required");
     NameEntryStorageTy *Name = reinterpret_cast<NameEntryStorageTy *>(this);
-    return *(Name - 1);
+    return (*(Name - 1)).NameEntry;
   }
-  const NameEntryStorageTy &getNameEntryPtr() const {
-    assert(HasName && "Name is required");
-    const auto *Name = reinterpret_cast<const NameEntryStorageTy *>(this);
-    return *(Name - 1);
+  const StringMapEntry<bool> *&getNameEntryPtr() const {
+    return const_cast<MCSymbol*>(this)->getNameEntryPtr();
   }
 
 public:
@@ -186,7 +184,7 @@ public:
     if (!HasName)
       return StringRef();
 
-    return getNameEntryPtr().NameEntry->first();
+    return getNameEntryPtr()->first();
   }
 
   bool isRegistered() const { return IsRegistered; }

Modified: llvm/trunk/lib/MC/MCSymbol.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCSymbol.cpp?rev=239429&r1=239428&r2=239429&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCSymbol.cpp (original)
+++ llvm/trunk/lib/MC/MCSymbol.cpp Tue Jun  9 15:41:08 2015
@@ -28,6 +28,7 @@ void *MCSymbol::operator new(size_t s, c
   // For safety, ensure that the alignment of a pointer is enough for an
   // MCSymbol.  This also ensures we don't need padding between the name and
   // symbol.
+  // FIXME: Use static_assert when constexpr is supported.
   assert(alignOf<MCSymbol>() <= alignOf<NameEntryStorageTy>() &&
          "Bad alignment of MCSymbol");
   void *Storage = Ctx.allocate(Size, alignOf<NameEntryStorageTy>());





More information about the llvm-commits mailing list