[clang] 7d24641 - [llvm][analyzer][NFC] Introduce SFINAE for specializing FoldingSetTraits

Balazs Benics via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 2 10:54:32 PDT 2022


Author: Balazs Benics
Date: 2022-06-02T19:46:38+02:00
New Revision: 7d24641f8916c2af16870a7b8628c1458cf15462

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

LOG: [llvm][analyzer][NFC] Introduce SFINAE for specializing FoldingSetTraits

Reviewed By: martong

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

Added: 
    

Modified: 
    clang/include/clang/Basic/SourceLocation.h
    clang/lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp
    llvm/include/llvm/ADT/FoldingSet.h
    llvm/include/llvm/CodeGen/SelectionDAG.h

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/SourceLocation.h b/clang/include/clang/Basic/SourceLocation.h
index 543245a811db5..4451523bc9450 100644
--- a/clang/include/clang/Basic/SourceLocation.h
+++ b/clang/include/clang/Basic/SourceLocation.h
@@ -24,7 +24,7 @@
 namespace llvm {
 
 class FoldingSetNodeID;
-template <typename T> struct FoldingSetTrait;
+template <typename T, typename Enable> struct FoldingSetTrait;
 
 } // namespace llvm
 
@@ -87,7 +87,7 @@ class SourceLocation {
   friend class ASTReader;
   friend class ASTWriter;
   friend class SourceManager;
-  friend struct llvm::FoldingSetTrait<SourceLocation>;
+  friend struct llvm::FoldingSetTrait<SourceLocation, void>;
 
 public:
   using UIntTy = uint32_t;
@@ -507,7 +507,7 @@ namespace llvm {
   };
 
   // Allow calling FoldingSetNodeID::Add with SourceLocation object as parameter
-  template <> struct FoldingSetTrait<clang::SourceLocation> {
+  template <> struct FoldingSetTrait<clang::SourceLocation, void> {
     static void Profile(const clang::SourceLocation &X, FoldingSetNodeID &ID);
   };
 

diff  --git a/clang/lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp
index 49e939be69017..cbb22ebe3e0d6 100644
--- a/clang/lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp
@@ -94,7 +94,7 @@ enum SelfFlagEnum {
 };
 }
 
-REGISTER_MAP_WITH_PROGRAMSTATE(SelfFlag, SymbolRef, unsigned)
+REGISTER_MAP_WITH_PROGRAMSTATE(SelfFlag, SymbolRef, SelfFlagEnum)
 REGISTER_TRAIT_WITH_PROGRAMSTATE(CalledInit, bool)
 
 /// A call receiving a reference to 'self' invalidates the object that
@@ -105,8 +105,8 @@ REGISTER_TRAIT_WITH_PROGRAMSTATE(PreCallSelfFlags, SelfFlagEnum)
 
 static SelfFlagEnum getSelfFlags(SVal val, ProgramStateRef state) {
   if (SymbolRef sym = val.getAsSymbol())
-    if (const unsigned *attachedFlags = state->get<SelfFlag>(sym))
-      return (SelfFlagEnum)*attachedFlags;
+    if (const SelfFlagEnum *attachedFlags = state->get<SelfFlag>(sym))
+      return *attachedFlags;
   return SelfFlag_None;
 }
 
@@ -118,7 +118,8 @@ static void addSelfFlag(ProgramStateRef state, SVal val,
                         SelfFlagEnum flag, CheckerContext &C) {
   // We tag the symbol that the SVal wraps.
   if (SymbolRef sym = val.getAsSymbol()) {
-    state = state->set<SelfFlag>(sym, getSelfFlags(val, state) | flag);
+    state = state->set<SelfFlag>(sym,
+                                 SelfFlagEnum(getSelfFlags(val, state) | flag));
     C.addTransition(state);
   }
 }
@@ -271,7 +272,7 @@ void ObjCSelfInitChecker::checkPostCall(const CallEvent &CE,
     return;
 
   ProgramStateRef state = C.getState();
-  SelfFlagEnum prevFlags = (SelfFlagEnum)state->get<PreCallSelfFlags>();
+  SelfFlagEnum prevFlags = state->get<PreCallSelfFlags>();
   if (!prevFlags)
     return;
   state = state->remove<PreCallSelfFlags>();
@@ -339,7 +340,7 @@ void ObjCSelfInitChecker::printState(raw_ostream &Out, ProgramStateRef State,
                                      const char *NL, const char *Sep) const {
   SelfFlagTy FlagMap = State->get<SelfFlag>();
   bool DidCallInit = State->get<CalledInit>();
-  SelfFlagEnum PreCallFlags = (SelfFlagEnum)State->get<PreCallSelfFlags>();
+  SelfFlagEnum PreCallFlags = State->get<PreCallSelfFlags>();
 
   if (FlagMap.isEmpty() && !DidCallInit && !PreCallFlags)
     return;

diff  --git a/llvm/include/llvm/ADT/FoldingSet.h b/llvm/include/llvm/ADT/FoldingSet.h
index 2c522708d742d..ec276d41da801 100644
--- a/llvm/include/llvm/ADT/FoldingSet.h
+++ b/llvm/include/llvm/ADT/FoldingSet.h
@@ -23,6 +23,7 @@
 #include <cassert>
 #include <cstddef>
 #include <cstdint>
+#include <type_traits>
 #include <utility>
 
 namespace llvm {
@@ -256,8 +257,8 @@ template<typename T> struct DefaultFoldingSetTrait {
 /// through template specialization the behavior can be tailored for specific
 /// types.  Combined with the FoldingSetNodeWrapper class, one can add objects
 /// to FoldingSets that were not originally designed to have that behavior.
-template<typename T> struct FoldingSetTrait
-  : public DefaultFoldingSetTrait<T> {};
+template <typename T, typename Enable = void>
+struct FoldingSetTrait : public DefaultFoldingSetTrait<T> {};
 
 /// DefaultContextualFoldingSetTrait - Like DefaultFoldingSetTrait, but
 /// for ContextualFoldingSets.
@@ -828,6 +829,13 @@ struct FoldingSetTrait<std::pair<T1, T2>> {
   }
 };
 
+template <typename T>
+struct FoldingSetTrait<T, typename std::enable_if_t<std::is_enum<T>::value>> {
+  static void Profile(const T &X, FoldingSetNodeID &ID) {
+    ID.AddInteger(static_cast<typename std::underlying_type_t<T>>(X));
+  }
+};
+
 } // end namespace llvm
 
 #endif // LLVM_ADT_FOLDINGSET_H

diff  --git a/llvm/include/llvm/CodeGen/SelectionDAG.h b/llvm/include/llvm/CodeGen/SelectionDAG.h
index 82eda0a917783..12a64ccaada55 100644
--- a/llvm/include/llvm/CodeGen/SelectionDAG.h
+++ b/llvm/include/llvm/CodeGen/SelectionDAG.h
@@ -58,7 +58,7 @@ class Pass;
 class Type;
 template <class GraphType> struct GraphTraits;
 template <typename T, unsigned int N> class SmallSetVector;
-template <typename T> struct FoldingSetTrait;
+template <typename T, typename Enable> struct FoldingSetTrait;
 class AAResults;
 class BlockAddress;
 class BlockFrequencyInfo;


        


More information about the cfe-commits mailing list