clang 24.0.0git
TaggedUnionModeling.h
Go to the documentation of this file.
1//===- TaggedUnionModeling.h -------------------------------------*- C++ -*-==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_TAGGEDUNIONMODELING_H
10#define LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_TAGGEDUNIONMODELING_H
11
18#include <numeric>
19
21
22// The implementation of all these functions can be found in the file
23// StdVariantChecker.cpp under the same directory as this file.
24
25bool isCopyConstructorCall(const CallEvent &Call);
26bool isCopyAssignmentCall(const CallEvent &Call);
27bool isMoveAssignmentCall(const CallEvent &Call);
28bool isMoveConstructorCall(const CallEvent &Call);
29bool isStdVariant(const Type *Type);
30
31// When invalidating regions, we also have to follow that by invalidating the
32// corresponding custom data in the program state.
33template <class TypeMap>
36 ProgramStateRef State,
38 // If we do not know anything about the call we shall not continue.
39 // If the call is happens within a system header it is implementation detail.
40 // We should not take it into consideration.
41 if (Call.isInSystemHeader())
42 return State;
43
44 for (const MemRegion *Region : Regions)
45 State = State->remove<TypeMap>(Region);
46
47 return State;
48}
49
50template <class TypeMap>
52 SVal ThisSVal) {
53 ProgramStateRef State = C.getState();
54
55 if (!State)
56 return;
57
58 auto ArgSVal = Call.getArgSVal(0);
59 const auto *ThisRegion = ThisSVal.getAsRegion();
60 const auto *ArgMemRegion = ArgSVal.getAsRegion();
61
62 // Make changes to the state according to type of constructor/assignment
65 // First we handle copy and move operations
66 if (IsCopy || IsMove) {
67 const QualType *OtherQType = State->get<TypeMap>(ArgMemRegion);
68
69 // If the argument of a copy constructor or assignment is unknown then
70 // we will not know the argument of the copied to object.
71 if (!OtherQType) {
72 State = State->remove<TypeMap>(ThisRegion);
73 } else {
74 // When move semantics is used we can only know that the moved from
75 // object must be in a destructible state. Other usage of the object
76 // than destruction is undefined.
77 if (IsMove)
78 State = State->remove<TypeMap>(ArgMemRegion);
79
80 State = State->set<TypeMap>(ThisRegion, *OtherQType);
81 }
82 } else {
83 // Value constructor
84 auto ArgQType = ArgSVal.getType(C.getASTContext());
85 const Type *ArgTypePtr = ArgQType.getTypePtr();
86
87 QualType WoPointer = ArgTypePtr->getPointeeType();
88 State = State->set<TypeMap>(ThisRegion, WoPointer);
89 }
90
91 C.addTransition(State);
92}
93
94} // namespace clang::ento::tagged_union_modeling
95
96#endif // LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_TAGGEDUNIONMODELING_H
A (possibly-)qualified type.
Definition TypeBase.h:938
The base class of the type hierarchy.
Definition TypeBase.h:1879
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:885
Represents an abstract call to a function or method along a particular path.
Definition CallEvent.h:152
MemRegion - The root abstract class for all memory regions.
Definition MemRegion.h:97
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition SVals.h:57
const MemRegion * getAsRegion() const
Definition SVals.cpp:119
ProgramStateRef removeInformationStoredForDeadInstances(const CallEvent &Call, ProgramStateRef State, ArrayRef< const MemRegion * > Regions)
bool isCopyAssignmentCall(const CallEvent &Call)
bool isMoveAssignmentCall(const CallEvent &Call)
bool isCopyConstructorCall(const CallEvent &Call)
void handleConstructorAndAssignment(const CallEvent &Call, CheckerContext &C, SVal ThisSVal)
bool isMoveConstructorCall(const CallEvent &Call)
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef