Index: include/llvm/ADT/StringMap.h =================================================================== --- include/llvm/ADT/StringMap.h +++ include/llvm/ADT/StringMap.h @@ -37,12 +37,12 @@ /// StringMapEntryBase - Shared base class of StringMapEntry instances. class StringMapEntryBase { - unsigned StrLen; + size_t StrLen; public: - explicit StringMapEntryBase(unsigned Len) : StrLen(Len) {} + explicit StringMapEntryBase(size_t Len) : StrLen(Len) {} - unsigned getKeyLength() const { return StrLen; } + size_t getKeyLength() const { return StrLen; } }; /// StringMapImpl - This is the base class of StringMap that is shared among @@ -127,10 +127,10 @@ public: ValueTy second; - explicit StringMapEntry(unsigned strLen) + explicit StringMapEntry(size_t strLen) : StringMapEntryBase(strLen), second() {} template - StringMapEntry(unsigned strLen, InitTy &&... InitVals) + StringMapEntry(size_t strLen, InitTy &&... InitVals) : StringMapEntryBase(strLen), second(std::forward(InitVals)...) {} StringMapEntry(StringMapEntry &E) = delete; @@ -155,13 +155,12 @@ template static StringMapEntry *Create(StringRef Key, AllocatorTy &Allocator, InitTy &&... InitVals) { - unsigned KeyLength = Key.size(); + size_t KeyLength = Key.size(); // Allocate a new item with space for the string at the end and a null // terminator. - unsigned AllocSize = static_cast(sizeof(StringMapEntry))+ - KeyLength+1; - unsigned Alignment = alignof(StringMapEntry); + size_t AllocSize = sizeof(StringMapEntry) + KeyLength + 1; + size_t Alignment = alignof(StringMapEntry); StringMapEntry *NewItem = static_cast(Allocator.Allocate(AllocSize,Alignment)); @@ -203,8 +202,7 @@ template void Destroy(AllocatorTy &Allocator) { // Free memory referenced by the item. - unsigned AllocSize = - static_cast(sizeof(StringMapEntry)) + getKeyLength() + 1; + size_t AllocSize = sizeof(StringMapEntry) + getKeyLength() + 1; this->~StringMapEntry(); Allocator.Deallocate(static_cast(this), AllocSize); } Index: unittests/ADT/StringMapTest.cpp =================================================================== --- unittests/ADT/StringMapTest.cpp +++ unittests/ADT/StringMapTest.cpp @@ -12,6 +12,7 @@ #include "llvm/ADT/Twine.h" #include "llvm/Support/DataTypes.h" #include "gtest/gtest.h" +#include #include using namespace llvm; @@ -492,4 +493,43 @@ EXPECT_EQ(42, Map["abcd"].Data); } +// Test that StringMapEntryBase can handle size_t wide sizes. +TEST(StringMapCustomTest, StringMapEntryBaseSize) { + size_t LargeValue; + + // Test that the entry can represent max-unsigned. + if (sizeof(size_t) <= sizeof(unsigned)) + LargeValue = std::numeric_limits::max(); + else + LargeValue = std::numeric_limits::max() + 1ULL; + StringMapEntryBase LargeBase(LargeValue); + EXPECT_EQ(LargeValue, LargeBase.getKeyLength()); + + // Test that the entry can hold at least max size_t. + LargeValue = std::numeric_limits::max(); + StringMapEntryBase LargerBase(LargeValue); + LargeValue = std::numeric_limits::max(); + EXPECT_EQ(LargeValue, LargerBase.getKeyLength()); +} + +// Test that StringMapEntry can handle size_t wide sizes. +TEST(StringMapCustomTest, StringMapEntrySize) { + size_t LargeValue; + + // Test that the entry can represent max-unsigned. + if (sizeof(size_t) <= sizeof(unsigned)) + LargeValue = std::numeric_limits::max(); + else + LargeValue = std::numeric_limits::max() + 1ULL; + StringMapEntry LargeEntry(LargeValue); + StringRef Key = LargeEntry.getKey(); + EXPECT_EQ(LargeValue, Key.size()); + + // Test that the entry can hold at least max size_t. + LargeValue = std::numeric_limits::max(); + StringMapEntry LargerEntry(LargeValue); + Key = LargerEntry.getKey(); + EXPECT_EQ(LargeValue, Key.size()); +} + } // end anonymous namespace