[clang] 33ca5a4 - [analyzer][NFC] Add partial specializations for ProgramStateTraits
Balazs Benics via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 2 10:47:00 PDT 2022
Author: Balazs Benics
Date: 2022-06-02T19:46:38+02:00
New Revision: 33ca5a447e7f0f9778f5adc96e24a1ef4a899213
URL: https://github.com/llvm/llvm-project/commit/33ca5a447e7f0f9778f5adc96e24a1ef4a899213
DIFF: https://github.com/llvm/llvm-project/commit/33ca5a447e7f0f9778f5adc96e24a1ef4a899213.diff
LOG: [analyzer][NFC] Add partial specializations for ProgramStateTraits
I'm also hoisting common code from the existing specializations into a
common trait impl to reduce code duplication.
Reviewed By: martong
Differential Revision: https://reviews.llvm.org/D126801
Added:
Modified:
clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h
clang/lib/StaticAnalyzer/Checkers/cert/InvalidPtrChecker.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h
index da82a55e36256..15bec97c5be8f 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h
@@ -21,29 +21,32 @@
#include "llvm/ADT/ImmutableSet.h"
#include "llvm/Support/Allocator.h"
#include <cstdint>
+#include <type_traits>
namespace clang {
namespace ento {
- template <typename T> struct ProgramStatePartialTrait;
-
- /// Declares a program state trait for type \p Type called \p Name, and
- /// introduce a type named \c NameTy.
- /// The macro should not be used inside namespaces.
- #define REGISTER_TRAIT_WITH_PROGRAMSTATE(Name, Type) \
- namespace { \
- class Name {}; \
- using Name ## Ty = Type; \
- } \
- namespace clang { \
- namespace ento { \
- template <> \
- struct ProgramStateTrait<Name> \
- : public ProgramStatePartialTrait<Name ## Ty> { \
- static void *GDMIndex() { static int Index; return &Index; } \
- }; \
- } \
- }
+template <typename T, typename Enable = void> struct ProgramStatePartialTrait;
+
+/// Declares a program state trait for type \p Type called \p Name, and
+/// introduce a type named \c NameTy.
+/// The macro should not be used inside namespaces.
+#define REGISTER_TRAIT_WITH_PROGRAMSTATE(Name, Type) \
+ namespace { \
+ class Name {}; \
+ using Name##Ty = Type; \
+ } \
+ namespace clang { \
+ namespace ento { \
+ template <> \
+ struct ProgramStateTrait<Name> : public ProgramStatePartialTrait<Name##Ty> { \
+ static void *GDMIndex() { \
+ static int Index; \
+ return &Index; \
+ } \
+ }; \
+ } \
+ }
/// Declares a factory for objects of type \p Type in the program state
/// manager. The type must provide a ::Factory sub-class. Commonly used for
@@ -267,60 +270,27 @@ namespace ento {
}
};
- // Partial specialization for bool.
- template <> struct ProgramStatePartialTrait<bool> {
- using data_type = bool;
-
- static data_type MakeData(void *const *p) {
- return p ? (data_type) (uintptr_t) *p
- : data_type();
- }
-
- static void *MakeVoidPtr(data_type d) {
- return (void *) (uintptr_t) d;
- }
+ template <typename T> struct DefaultProgramStatePartialTraitImpl {
+ using data_type = T;
+ static T MakeData(void *const *P) { return P ? (T)(uintptr_t)*P : T{}; }
+ static void *MakeVoidPtr(T D) { return (void *)(uintptr_t)D; }
};
- // Partial specialization for unsigned.
- template <> struct ProgramStatePartialTrait<unsigned> {
- using data_type = unsigned;
-
- static data_type MakeData(void *const *p) {
- return p ? (data_type) (uintptr_t) *p
- : data_type();
- }
-
- static void *MakeVoidPtr(data_type d) {
- return (void *) (uintptr_t) d;
- }
- };
-
- // Partial specialization for void*.
- template <> struct ProgramStatePartialTrait<void *> {
- using data_type = void *;
-
- static data_type MakeData(void *const *p) {
- return p ? *p
- : data_type();
- }
-
- static void *MakeVoidPtr(data_type d) {
- return d;
- }
- };
-
- // Partial specialization for const void *.
- template <> struct ProgramStatePartialTrait<const void *> {
- using data_type = const void *;
+ // Partial specialization for integral types.
+ template <typename T>
+ struct ProgramStatePartialTrait<T,
+ std::enable_if_t<std::is_integral<T>::value>>
+ : DefaultProgramStatePartialTraitImpl<T> {};
- static data_type MakeData(void *const *p) {
- return p ? *p : data_type();
- }
+ // Partial specialization for enums.
+ template <typename T>
+ struct ProgramStatePartialTrait<T, std::enable_if_t<std::is_enum<T>::value>>
+ : DefaultProgramStatePartialTraitImpl<T> {};
- static void *MakeVoidPtr(data_type d) {
- return const_cast<void *>(d);
- }
- };
+ // Partial specialization for pointers.
+ template <typename T>
+ struct ProgramStatePartialTrait<T *, void>
+ : DefaultProgramStatePartialTraitImpl<T *> {};
} // namespace ento
} // namespace clang
diff --git a/clang/lib/StaticAnalyzer/Checkers/cert/InvalidPtrChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/cert/InvalidPtrChecker.cpp
index a09fa3d33892c..084789509533e 100644
--- a/clang/lib/StaticAnalyzer/Checkers/cert/InvalidPtrChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/cert/InvalidPtrChecker.cpp
@@ -82,9 +82,7 @@ class InvalidPtrChecker
REGISTER_SET_WITH_PROGRAMSTATE(InvalidMemoryRegions, const MemRegion *)
// Stores the region of the environment pointer of 'main' (if present).
-// Note: This pointer has type 'const MemRegion *', however the trait is only
-// specialized to 'const void*' and 'void*'
-REGISTER_TRAIT_WITH_PROGRAMSTATE(EnvPtrRegion, const void *)
+REGISTER_TRAIT_WITH_PROGRAMSTATE(EnvPtrRegion, const MemRegion *)
// Stores key-value pairs, where key is function declaration and value is
// pointer to memory region returned by previous call of this function
@@ -95,11 +93,9 @@ void InvalidPtrChecker::EnvpInvalidatingCall(const CallEvent &Call,
CheckerContext &C) const {
StringRef FunctionName = Call.getCalleeIdentifier()->getName();
ProgramStateRef State = C.getState();
- const auto *Reg = State->get<EnvPtrRegion>();
- if (!Reg)
+ const MemRegion *SymbolicEnvPtrRegion = State->get<EnvPtrRegion>();
+ if (!SymbolicEnvPtrRegion)
return;
- const auto *SymbolicEnvPtrRegion =
- reinterpret_cast<const MemRegion *>(const_cast<const void *>(Reg));
State = State->add<InvalidMemoryRegions>(SymbolicEnvPtrRegion);
@@ -245,9 +241,7 @@ void InvalidPtrChecker::checkBeginFunction(CheckerContext &C) const {
// Save the memory region pointed by the environment pointer parameter of
// 'main'.
- State = State->set<EnvPtrRegion>(
- reinterpret_cast<void *>(const_cast<MemRegion *>(EnvpReg)));
- C.addTransition(State);
+ C.addTransition(State->set<EnvPtrRegion>(EnvpReg));
}
// Check if invalidated region is being dereferenced.
More information about the cfe-commits
mailing list