[llvm] d985b0b - A few cosmetic cleanups to StringMap/StringSet.h, including fixing

Chris Lattner via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 11 22:15:39 PDT 2020


Author: Chris Lattner
Date: 2020-04-11T22:14:35-07:00
New Revision: d985b0bf5c8f74c174d47a56809fbb094221322c

URL: https://github.com/llvm/llvm-project/commit/d985b0bf5c8f74c174d47a56809fbb094221322c
DIFF: https://github.com/llvm/llvm-project/commit/d985b0bf5c8f74c174d47a56809fbb094221322c.diff

LOG: 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.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/StringMap.h
    llvm/include/llvm/ADT/StringSet.h
    llvm/include/llvm/Support/PointerLikeTypeTraits.h

Removed: 
    


################################################################################
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 <algorithm>
-#include <cassert>
-#include <cstdint>
-#include <cstdlib>
-#include <cstring>
 #include <initializer_list>
 #include <iterator>
-#include <utility>
 
 namespace llvm {
 
-template<typename ValueTy> class StringMapConstIterator;
-template<typename ValueTy> class StringMapIterator;
-template<typename ValueTy> class StringMapKeyIterator;
+template <typename ValueTy> class StringMapConstIterator;
+template <typename ValueTy> class StringMapIterator;
+template <typename ValueTy> class StringMapKeyIterator;
 
 /// StringMapEntryBase - Shared base class of StringMapEntry instances.
 class StringMapEntryBase {
@@ -57,8 +49,7 @@ class StringMapImpl {
   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 @@ class StringMapImpl {
 /// 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<typename ValueTy>
+template <typename ValueTy>
 class StringMapEntryStorage : public StringMapEntryBase {
 public:
   ValueTy second;
 
   explicit StringMapEntryStorage(size_t strLen)
-    : StringMapEntryBase(strLen), second() {}
+      : StringMapEntryBase(strLen), second() {}
   template <typename... InitTy>
   StringMapEntryStorage(size_t strLen, InitTy &&... InitVals)
       : StringMapEntryBase(strLen), second(std::forward<InitTy>(InitVals)...) {}
@@ -140,11 +131,10 @@ class StringMapEntryStorage : public StringMapEntryBase {
   void setValue(const ValueTy &V) { second = V; }
 };
 
-template<>
-class StringMapEntryStorage<NoneType> : public StringMapEntryBase {
+template <> class StringMapEntryStorage<NoneType> : 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 @@ class StringMapEntryStorage<NoneType> : public StringMapEntryBase {
 /// 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<typename ValueTy>
+template <typename ValueTy>
 class StringMapEntry final : public StringMapEntryStorage<ValueTy> {
 public:
   using StringMapEntryStorage<ValueTy>::StringMapEntryStorage;
@@ -165,7 +155,9 @@ class StringMapEntry final : public StringMapEntryStorage<ValueTy> {
   /// 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<const char*>(this+1);}
+  const char *getKeyData() const {
+    return reinterpret_cast<const char *>(this + 1);
+  }
 
   StringRef first() const {
     return StringRef(getKeyData(), this->getKeyLength());
@@ -184,17 +176,17 @@ class StringMapEntry final : public StringMapEntryStorage<ValueTy> {
     size_t Alignment = alignof(StringMapEntry);
 
     StringMapEntry *NewItem =
-      static_cast<StringMapEntry*>(Allocator.Allocate(AllocSize,Alignment));
+        static_cast<StringMapEntry *>(Allocator.Allocate(AllocSize, Alignment));
     assert(NewItem && "Unhandled out-of-memory");
 
     // Construct the value.
     new (NewItem) StringMapEntry(KeyLength, std::forward<InitTy>(InitVals)...);
 
     // Copy the string information.
-    char *StrBuffer = const_cast<char*>(NewItem->getKeyData());
+    char *StrBuffer = const_cast<char *>(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 @@ class StringMapEntry final : public StringMapEntryStorage<ValueTy> {
   /// 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<char*>(KeyData) - sizeof(StringMapEntry<ValueTy>);
-    return *reinterpret_cast<StringMapEntry*>(Ptr);
+    char *Ptr = const_cast<char *>(KeyData) - sizeof(StringMapEntry<ValueTy>);
+    return *reinterpret_cast<StringMapEntry *>(Ptr);
   }
 
   /// Destroy - Destroy this StringMapEntry, releasing memory back to the
   /// specified allocator.
-  template<typename AllocatorTy>
-  void Destroy(AllocatorTy &Allocator) {
+  template <typename AllocatorTy> void Destroy(AllocatorTy &Allocator) {
     // Free memory referenced by the item.
     size_t AllocSize = sizeof(StringMapEntry) + this->getKeyLength() + 1;
     this->~StringMapEntry();
@@ -237,7 +228,7 @@ class StringMapEntry final : public StringMapEntryStorage<ValueTy> {
 /// 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<typename ValueTy, typename AllocatorTy = MallocAllocator>
+template <typename ValueTy, typename AllocatorTy = MallocAllocator>
 class StringMap : public StringMapImpl {
   AllocatorTy Allocator;
 
@@ -247,14 +238,15 @@ class StringMap : public StringMapImpl {
   StringMap() : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))) {}
 
   explicit StringMap(unsigned InitialSize)
-    : StringMapImpl(InitialSize, static_cast<unsigned>(sizeof(MapEntryTy))) {}
+      : StringMapImpl(InitialSize, static_cast<unsigned>(sizeof(MapEntryTy))) {}
 
   explicit StringMap(AllocatorTy A)
-    : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))), Allocator(A) {}
+      : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))), Allocator(A) {
+  }
 
   StringMap(unsigned InitialSize, AllocatorTy A)
-    : StringMapImpl(InitialSize, static_cast<unsigned>(sizeof(MapEntryTy))),
-      Allocator(A) {}
+      : StringMapImpl(InitialSize, static_cast<unsigned>(sizeof(MapEntryTy))),
+        Allocator(A) {}
 
   StringMap(std::initializer_list<std::pair<StringRef, ValueTy>> List)
       : StringMapImpl(List.size(), static_cast<unsigned>(sizeof(MapEntryTy))) {
@@ -266,9 +258,9 @@ class StringMap : public StringMapImpl {
   StringMap(StringMap &&RHS)
       : StringMapImpl(std::move(RHS)), Allocator(std::move(RHS.Allocator)) {}
 
-  StringMap(const StringMap &RHS) :
-    StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))),
-    Allocator(RHS.Allocator) {
+  StringMap(const StringMap &RHS)
+      : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))),
+        Allocator(RHS.Allocator) {
     if (RHS.empty())
       return;
 
@@ -315,7 +307,7 @@ class StringMap : public StringMapImpl {
       for (unsigned I = 0, E = NumBuckets; I != E; ++I) {
         StringMapEntryBase *Bucket = TheTable[I];
         if (Bucket && Bucket != getTombstoneVal()) {
-          static_cast<MapEntryTy*>(Bucket)->Destroy(Allocator);
+          static_cast<MapEntryTy *>(Bucket)->Destroy(Allocator);
         }
       }
     }
@@ -325,7 +317,7 @@ class StringMap : public StringMapImpl {
   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<ValueTy>;
   using size_type = size_t;
@@ -333,17 +325,13 @@ class StringMap : public StringMapImpl {
   using const_iterator = StringMapConstIterator<ValueTy>;
   using iterator = StringMapIterator<ValueTy>;
 
-  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<StringMapKeyIterator<ValueTy>> keys() const {
@@ -353,14 +341,16 @@ class StringMap : public StringMapImpl {
 
   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 @@ class StringMap : public StringMapImpl {
   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 <typename InputTy>
   size_type count(const StringMapEntry<InputTy> &MapEntry) const {
@@ -393,7 +381,7 @@ class StringMap : public StringMapImpl {
     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 @@ class StringMap : public StringMapImpl {
 
   // 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<MapEntryTy*>(Bucket)->Destroy(Allocator);
+        static_cast<MapEntryTy *>(Bucket)->Destroy(Allocator);
       }
       Bucket = nullptr;
     }
@@ -465,9 +454,7 @@ class StringMap : public StringMapImpl {
 
   /// 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 @@ class StringMap : public StringMapImpl {
 
   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 @@ class StringMapIterBase
   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 <cassert>
-#include <initializer_list>
-#include <utility>
 
 namespace llvm {
 
-  /// StringSet - A wrapper for StringMap that provides set-like functionality.
-  template <class AllocatorTy = MallocAllocator>
-  class StringSet : public StringMap<NoneType, AllocatorTy> {
-    using base = StringMap<NoneType, AllocatorTy>;
-
-  public:
-    StringSet() = default;
-    StringSet(std::initializer_list<StringRef> S) {
-      for (StringRef X : S)
-        insert(X);
-    }
-    explicit StringSet(AllocatorTy A) : base(A) {}
-
-    std::pair<typename base::iterator, bool> insert(StringRef Key) {
-      return base::insert(std::make_pair(Key, None));
-    }
-
-    template <typename InputIt>
-    void insert(const InputIt &Begin, const InputIt &End) {
-      for (auto It = Begin; It != End; ++It)
-        base::insert(std::make_pair(*It, None));
-    }
-
-    template <typename ValueTy>
-    std::pair<typename base::iterator, bool>
-    insert(const StringMapEntry<ValueTy> &MapEntry) {
-      return insert(MapEntry.getKey());
-    }
-  };
+/// StringSet - A wrapper for StringMap that provides set-like functionality.
+template <class AllocatorTy = MallocAllocator>
+class StringSet : public StringMap<NoneType, AllocatorTy> {
+  using Base = StringMap<NoneType, AllocatorTy>;
+
+public:
+  StringSet() = default;
+  StringSet(std::initializer_list<StringRef> initializer) {
+    for (StringRef str : initializer)
+      insert(str);
+  }
+  explicit StringSet(AllocatorTy a) : Base(a) {}
+
+  std::pair<typename Base::iterator, bool> insert(StringRef key) {
+    return Base::insert(std::make_pair(key, None));
+  }
+
+  template <typename InputIt>
+  void insert(const InputIt &begin, const InputIt &end) {
+    for (auto it = begin; it != end; ++it)
+      Base::insert(std::make_pair(*it, None));
+  }
+
+  template <typename ValueTy>
+  std::pair<typename Base::iterator, bool>
+  insert(const StringMapEntry<ValueTy> &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 <assert.h>
+#include <cassert>
 #include <type_traits>
 
 namespace llvm {
@@ -37,8 +37,9 @@ template <typename T, typename U = void> struct HasPointerLikeTypeTraits {
 };
 
 // sizeof(T) is valid only for a complete T.
-template <typename T> struct HasPointerLikeTypeTraits<
-  T, decltype((sizeof(PointerLikeTypeTraits<T>) + sizeof(T)), void())> {
+template <typename T>
+struct HasPointerLikeTypeTraits<
+    T, decltype((sizeof(PointerLikeTypeTraits<T>) + sizeof(T)), void())> {
   static const bool value = true;
 };
 


        


More information about the llvm-commits mailing list