Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 38f72d0

Browse files
committed
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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@239429 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 5c8a22f commit 38f72d0

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

include/llvm/MC/MCSymbol.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class MCSymbol {
136136
Kind(Kind) {
137137
Offset = 0;
138138
if (Name)
139-
getNameEntryPtr().NameEntry = Name;
139+
getNameEntryPtr() = Name;
140140
}
141141

142142
// Provide custom new/delete as we will only allocate space for a name
@@ -169,15 +169,13 @@ class MCSymbol {
169169
}
170170

171171
/// \brief Get a reference to the name field. Requires that we have a name
172-
NameEntryStorageTy &getNameEntryPtr() {
172+
const StringMapEntry<bool> *&getNameEntryPtr() {
173173
assert(HasName && "Name is required");
174174
NameEntryStorageTy *Name = reinterpret_cast<NameEntryStorageTy *>(this);
175-
return *(Name - 1);
175+
return (*(Name - 1)).NameEntry;
176176
}
177-
const NameEntryStorageTy &getNameEntryPtr() const {
178-
assert(HasName && "Name is required");
179-
const auto *Name = reinterpret_cast<const NameEntryStorageTy *>(this);
180-
return *(Name - 1);
177+
const StringMapEntry<bool> *&getNameEntryPtr() const {
178+
return const_cast<MCSymbol*>(this)->getNameEntryPtr();
181179
}
182180

183181
public:
@@ -186,7 +184,7 @@ class MCSymbol {
186184
if (!HasName)
187185
return StringRef();
188186

189-
return getNameEntryPtr().NameEntry->first();
187+
return getNameEntryPtr()->first();
190188
}
191189

192190
bool isRegistered() const { return IsRegistered; }

lib/MC/MCSymbol.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ void *MCSymbol::operator new(size_t s, const StringMapEntry<bool> *Name,
2828
// For safety, ensure that the alignment of a pointer is enough for an
2929
// MCSymbol. This also ensures we don't need padding between the name and
3030
// symbol.
31+
// FIXME: Use static_assert when constexpr is supported.
3132
assert(alignOf<MCSymbol>() <= alignOf<NameEntryStorageTy>() &&
3233
"Bad alignment of MCSymbol");
3334
void *Storage = Ctx.allocate(Size, alignOf<NameEntryStorageTy>());

0 commit comments

Comments
 (0)