[clang-tools-extra] 0cb2dd5 - [include-cleaner] Make Symbol (and Macro) hashable.

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 24 06:15:58 PST 2022


Author: Sam McCall
Date: 2022-11-24T15:15:50+01:00
New Revision: 0cb2dd5322f494769a7c31c8ed8aab930919f5f3

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

LOG: [include-cleaner] Make Symbol (and Macro) hashable.

For now, we decided not to add operator< or handle other variants.
(If we do so in future we may want to extract a base class).

Differential Revision: https://reviews.llvm.org/D138648

Added: 
    

Modified: 
    clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h b/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
index 29a3aa7ea7f73..405ded21561c3 100644
--- a/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
+++ b/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
@@ -64,9 +64,11 @@ struct Symbol {
   struct Macro macro() const { return std::get<Macro>(Storage); }
 
 private:
-  // FIXME: Add support for macros.
   // Order must match Kind enum!
   std::variant<const Decl *, struct Macro> Storage;
+
+  Symbol(decltype(Storage) Sentinel) : Storage(std::move(Sentinel)) {}
+  friend llvm::DenseMapInfo<Symbol>;
 };
 llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Symbol &);
 
@@ -137,4 +139,36 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Include &);
 } // namespace include_cleaner
 } // namespace clang
 
+namespace llvm {
+
+template <> struct DenseMapInfo<clang::include_cleaner::Symbol> {
+  using Outer = clang::include_cleaner::Symbol;
+  using Base = DenseMapInfo<decltype(Outer::Storage)>;
+
+  static inline Outer getEmptyKey() { return {Base::getEmptyKey()}; }
+  static inline Outer getTombstoneKey() { return {Base::getTombstoneKey()}; }
+  static unsigned getHashValue(const Outer &Val) {
+    return Base::getHashValue(Val.Storage);
+  }
+  static bool isEqual(const Outer &LHS, const Outer &RHS) {
+    return Base::isEqual(LHS.Storage, RHS.Storage);
+  }
+};
+template <> struct DenseMapInfo<clang::include_cleaner::Macro> {
+  using Outer = clang::include_cleaner::Macro;
+  using Base = DenseMapInfo<decltype(Outer::Definition)>;
+
+  static inline Outer getEmptyKey() { return {nullptr, Base::getEmptyKey()}; }
+  static inline Outer getTombstoneKey() {
+    return {nullptr, Base::getTombstoneKey()};
+  }
+  static unsigned getHashValue(const Outer &Val) {
+    return Base::getHashValue(Val.Definition);
+  }
+  static bool isEqual(const Outer &LHS, const Outer &RHS) {
+    return Base::isEqual(LHS.Definition, RHS.Definition);
+  }
+};
+} // namespace llvm
+
 #endif


        


More information about the cfe-commits mailing list