From d985b0bf5c8f74c174d47a56809fbb094221322c Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 11 Apr 2020 22:14:35 -0700 Subject: [PATCH] A few cosmetic cleanups to StringMap/StringSet.h, including fixing the indentation of the StringSet.h file and its file comment header, and significantly reduce redundant #includes that are already pulled in transitively. NFC. This is in preparation for a more interesting patch I'll post to phab. --- llvm/include/llvm/ADT/StringMap.h | 107 ++++++++---------- llvm/include/llvm/ADT/StringSet.h | 57 +++++----- .../llvm/Support/PointerLikeTypeTraits.h | 7 +- 3 files changed, 78 insertions(+), 93 deletions(-) diff --git a/llvm/include/llvm/ADT/StringMap.h b/llvm/include/llvm/ADT/StringMap.h index 8e8323f4dd9c..fb15c00576c3 100644 --- a/llvm/include/llvm/ADT/StringMap.h +++ b/llvm/include/llvm/ADT/StringMap.h @@ -14,24 +14,16 @@ #define LLVM_ADT_STRINGMAP_H #include "llvm/ADT/StringRef.h" -#include "llvm/ADT/iterator.h" -#include "llvm/ADT/iterator_range.h" #include "llvm/Support/AllocatorBase.h" #include "llvm/Support/PointerLikeTypeTraits.h" -#include -#include -#include -#include -#include #include #include -#include namespace llvm { -template class StringMapConstIterator; -template class StringMapIterator; -template class StringMapKeyIterator; +template class StringMapConstIterator; +template class StringMapIterator; +template class StringMapKeyIterator; /// StringMapEntryBase - Shared base class of StringMapEntry instances. class StringMapEntryBase { @@ -57,8 +49,7 @@ protected: unsigned ItemSize; protected: - explicit StringMapImpl(unsigned itemSize) - : ItemSize(itemSize) {} + explicit StringMapImpl(unsigned itemSize) : ItemSize(itemSize) {} StringMapImpl(StringMapImpl &&RHS) : TheTable(RHS.TheTable), NumBuckets(RHS.NumBuckets), NumItems(RHS.NumItems), NumTombstones(RHS.NumTombstones), @@ -122,13 +113,13 @@ public: /// Factored out into a separate base class to make it easier to specialize. /// This is primarily intended to support StringSet, which doesn't need a value /// stored at all. -template +template class StringMapEntryStorage : public StringMapEntryBase { public: ValueTy second; explicit StringMapEntryStorage(size_t strLen) - : StringMapEntryBase(strLen), second() {} + : StringMapEntryBase(strLen), second() {} template StringMapEntryStorage(size_t strLen, InitTy &&... InitVals) : StringMapEntryBase(strLen), second(std::forward(InitVals)...) {} @@ -140,11 +131,10 @@ public: void setValue(const ValueTy &V) { second = V; } }; -template<> -class StringMapEntryStorage : public StringMapEntryBase { +template <> class StringMapEntryStorage : public StringMapEntryBase { public: explicit StringMapEntryStorage(size_t strLen, NoneType none = None) - : StringMapEntryBase(strLen) {} + : StringMapEntryBase(strLen) {} StringMapEntryStorage(StringMapEntryStorage &E) = delete; NoneType getValue() const { return None; } @@ -153,7 +143,7 @@ public: /// StringMapEntry - This is used to represent one value that is inserted into /// a StringMap. It contains the Value itself and the key: the string length /// and data. -template +template class StringMapEntry final : public StringMapEntryStorage { public: using StringMapEntryStorage::StringMapEntryStorage; @@ -165,7 +155,9 @@ public: /// getKeyData - Return the start of the string data that is the key for this /// value. The string data is always stored immediately after the /// StringMapEntry object. - const char *getKeyData() const {return reinterpret_cast(this+1);} + const char *getKeyData() const { + return reinterpret_cast(this + 1); + } StringRef first() const { return StringRef(getKeyData(), this->getKeyLength()); @@ -184,17 +176,17 @@ public: size_t Alignment = alignof(StringMapEntry); StringMapEntry *NewItem = - static_cast(Allocator.Allocate(AllocSize,Alignment)); + static_cast(Allocator.Allocate(AllocSize, Alignment)); assert(NewItem && "Unhandled out-of-memory"); // Construct the value. new (NewItem) StringMapEntry(KeyLength, std::forward(InitVals)...); // Copy the string information. - char *StrBuffer = const_cast(NewItem->getKeyData()); + char *StrBuffer = const_cast(NewItem->getKeyData()); if (KeyLength > 0) memcpy(StrBuffer, Key.data(), KeyLength); - StrBuffer[KeyLength] = 0; // Null terminate for convenience of clients. + StrBuffer[KeyLength] = 0; // Null terminate for convenience of clients. return NewItem; } @@ -212,14 +204,13 @@ public: /// GetStringMapEntryFromKeyData - Given key data that is known to be embedded /// into a StringMapEntry, return the StringMapEntry itself. static StringMapEntry &GetStringMapEntryFromKeyData(const char *KeyData) { - char *Ptr = const_cast(KeyData) - sizeof(StringMapEntry); - return *reinterpret_cast(Ptr); + char *Ptr = const_cast(KeyData) - sizeof(StringMapEntry); + return *reinterpret_cast(Ptr); } /// Destroy - Destroy this StringMapEntry, releasing memory back to the /// specified allocator. - template - void Destroy(AllocatorTy &Allocator) { + template void Destroy(AllocatorTy &Allocator) { // Free memory referenced by the item. size_t AllocSize = sizeof(StringMapEntry) + this->getKeyLength() + 1; this->~StringMapEntry(); @@ -237,7 +228,7 @@ public: /// keys that are "strings", which are basically ranges of bytes. This does some /// funky memory allocation and hashing things to make it extremely efficient, /// storing the string data *after* the value in the map. -template +template class StringMap : public StringMapImpl { AllocatorTy Allocator; @@ -247,14 +238,15 @@ public: StringMap() : StringMapImpl(static_cast(sizeof(MapEntryTy))) {} explicit StringMap(unsigned InitialSize) - : StringMapImpl(InitialSize, static_cast(sizeof(MapEntryTy))) {} + : StringMapImpl(InitialSize, static_cast(sizeof(MapEntryTy))) {} explicit StringMap(AllocatorTy A) - : StringMapImpl(static_cast(sizeof(MapEntryTy))), Allocator(A) {} + : StringMapImpl(static_cast(sizeof(MapEntryTy))), Allocator(A) { + } StringMap(unsigned InitialSize, AllocatorTy A) - : StringMapImpl(InitialSize, static_cast(sizeof(MapEntryTy))), - Allocator(A) {} + : StringMapImpl(InitialSize, static_cast(sizeof(MapEntryTy))), + Allocator(A) {} StringMap(std::initializer_list> List) : StringMapImpl(List.size(), static_cast(sizeof(MapEntryTy))) { @@ -266,9 +258,9 @@ public: StringMap(StringMap &&RHS) : StringMapImpl(std::move(RHS)), Allocator(std::move(RHS.Allocator)) {} - StringMap(const StringMap &RHS) : - StringMapImpl(static_cast(sizeof(MapEntryTy))), - Allocator(RHS.Allocator) { + StringMap(const StringMap &RHS) + : StringMapImpl(static_cast(sizeof(MapEntryTy))), + Allocator(RHS.Allocator) { if (RHS.empty()) return; @@ -315,7 +307,7 @@ public: for (unsigned I = 0, E = NumBuckets; I != E; ++I) { StringMapEntryBase *Bucket = TheTable[I]; if (Bucket && Bucket != getTombstoneVal()) { - static_cast(Bucket)->Destroy(Allocator); + static_cast(Bucket)->Destroy(Allocator); } } } @@ -325,7 +317,7 @@ public: AllocatorTy &getAllocator() { return Allocator; } const AllocatorTy &getAllocator() const { return Allocator; } - using key_type = const char*; + using key_type = const char *; using mapped_type = ValueTy; using value_type = StringMapEntry; using size_type = size_t; @@ -333,17 +325,13 @@ public: using const_iterator = StringMapConstIterator; using iterator = StringMapIterator; - iterator begin() { - return iterator(TheTable, NumBuckets == 0); - } - iterator end() { - return iterator(TheTable+NumBuckets, true); - } + iterator begin() { return iterator(TheTable, NumBuckets == 0); } + iterator end() { return iterator(TheTable + NumBuckets, true); } const_iterator begin() const { return const_iterator(TheTable, NumBuckets == 0); } const_iterator end() const { - return const_iterator(TheTable+NumBuckets, true); + return const_iterator(TheTable + NumBuckets, true); } iterator_range> keys() const { @@ -353,14 +341,16 @@ public: iterator find(StringRef Key) { int Bucket = FindKey(Key); - if (Bucket == -1) return end(); - return iterator(TheTable+Bucket, true); + if (Bucket == -1) + return end(); + return iterator(TheTable + Bucket, true); } const_iterator find(StringRef Key) const { int Bucket = FindKey(Key); - if (Bucket == -1) return end(); - return const_iterator(TheTable+Bucket, true); + if (Bucket == -1) + return end(); + return const_iterator(TheTable + Bucket, true); } /// lookup - Return the entry for the specified key, or a default @@ -377,9 +367,7 @@ public: ValueTy &operator[](StringRef Key) { return try_emplace(Key).first->second; } /// count - Return 1 if the element is in the map, 0 otherwise. - size_type count(StringRef Key) const { - return find(Key) == end() ? 0 : 1; - } + size_type count(StringRef Key) const { return find(Key) == end() ? 0 : 1; } template size_type count(const StringMapEntry &MapEntry) const { @@ -393,7 +381,7 @@ public: unsigned BucketNo = LookupBucketFor(KeyValue->getKey()); StringMapEntryBase *&Bucket = TheTable[BucketNo]; if (Bucket && Bucket != getTombstoneVal()) - return false; // Already exists in map. + return false; // Already exists in map. if (Bucket == getTombstoneVal()) --NumTombstones; @@ -447,14 +435,15 @@ public: // clear - Empties out the StringMap void clear() { - if (empty()) return; + if (empty()) + return; // Zap all values, resetting the keys back to non-present (not tombstone), // which is safe because we're removing all elements. for (unsigned I = 0, E = NumBuckets; I != E; ++I) { StringMapEntryBase *&Bucket = TheTable[I]; if (Bucket && Bucket != getTombstoneVal()) { - static_cast(Bucket)->Destroy(Allocator); + static_cast(Bucket)->Destroy(Allocator); } Bucket = nullptr; } @@ -465,9 +454,7 @@ public: /// remove - Remove the specified key/value pair from the map, but do not /// erase it. This aborts if the key is not in the map. - void remove(MapEntryTy *KeyValue) { - RemoveKey(KeyValue); - } + void remove(MapEntryTy *KeyValue) { RemoveKey(KeyValue); } void erase(iterator I) { MapEntryTy &V = *I; @@ -477,7 +464,8 @@ public: bool erase(StringRef Key) { iterator I = find(Key); - if (I == end()) return false; + if (I == end()) + return false; erase(I); return true; } @@ -496,7 +484,8 @@ public: explicit StringMapIterBase(StringMapEntryBase **Bucket, bool NoAdvance = false) : Ptr(Bucket) { - if (!NoAdvance) AdvancePastEmptyBuckets(); + if (!NoAdvance) + AdvancePastEmptyBuckets(); } DerivedTy &operator=(const DerivedTy &Other) { diff --git a/llvm/include/llvm/ADT/StringSet.h b/llvm/include/llvm/ADT/StringSet.h index 7f3bbd490171..2a17db0ccf27 100644 --- a/llvm/include/llvm/ADT/StringSet.h +++ b/llvm/include/llvm/ADT/StringSet.h @@ -1,4 +1,4 @@ -//===- StringSet.h - The LLVM Compiler Driver -------------------*- C++ -*-===// +//===- StringSet.h - An efficient set built on StringMap --------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -14,43 +14,38 @@ #define LLVM_ADT_STRINGSET_H #include "llvm/ADT/StringMap.h" -#include "llvm/ADT/StringRef.h" -#include "llvm/Support/AllocatorBase.h" -#include -#include -#include namespace llvm { - /// StringSet - A wrapper for StringMap that provides set-like functionality. - template - class StringSet : public StringMap { - using base = StringMap; +/// StringSet - A wrapper for StringMap that provides set-like functionality. +template +class StringSet : public StringMap { + using Base = StringMap; - public: - StringSet() = default; - StringSet(std::initializer_list S) { - for (StringRef X : S) - insert(X); - } - explicit StringSet(AllocatorTy A) : base(A) {} +public: + StringSet() = default; + StringSet(std::initializer_list initializer) { + for (StringRef str : initializer) + insert(str); + } + explicit StringSet(AllocatorTy a) : Base(a) {} - std::pair insert(StringRef Key) { - return base::insert(std::make_pair(Key, None)); - } + std::pair insert(StringRef key) { + return Base::insert(std::make_pair(key, None)); + } - template - void insert(const InputIt &Begin, const InputIt &End) { - for (auto It = Begin; It != End; ++It) - base::insert(std::make_pair(*It, None)); - } + template + void insert(const InputIt &begin, const InputIt &end) { + for (auto it = begin; it != end; ++it) + Base::insert(std::make_pair(*it, None)); + } - template - std::pair - insert(const StringMapEntry &MapEntry) { - return insert(MapEntry.getKey()); - } - }; + template + std::pair + insert(const StringMapEntry &mapEntry) { + return insert(mapEntry.getKey()); + } +}; } // end namespace llvm diff --git a/llvm/include/llvm/Support/PointerLikeTypeTraits.h b/llvm/include/llvm/Support/PointerLikeTypeTraits.h index 71ec81807517..1b15f930bd87 100644 --- a/llvm/include/llvm/Support/PointerLikeTypeTraits.h +++ b/llvm/include/llvm/Support/PointerLikeTypeTraits.h @@ -15,7 +15,7 @@ #define LLVM_SUPPORT_POINTERLIKETYPETRAITS_H #include "llvm/Support/DataTypes.h" -#include +#include #include namespace llvm { @@ -37,8 +37,9 @@ template struct HasPointerLikeTypeTraits { }; // sizeof(T) is valid only for a complete T. -template struct HasPointerLikeTypeTraits< - T, decltype((sizeof(PointerLikeTypeTraits) + sizeof(T)), void())> { +template +struct HasPointerLikeTypeTraits< + T, decltype((sizeof(PointerLikeTypeTraits) + sizeof(T)), void())> { static const bool value = true; };