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

Reviewed By: martong

Differential Revision: https://reviews.llvm.org/D126803
This commit is contained in:
Balazs Benics 2022-06-02 19:46:38 +02:00
parent cf1f1b7240
commit 7d24641f89
4 changed files with 21 additions and 12 deletions

View File

@ -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);
};

View File

@ -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;

View File

@ -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

View File

@ -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;