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
12#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
13#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
14#include "clang/StaticAnalyzer/Core/Checker.h"
15#include "clang/StaticAnalyzer/Core/CheckerManager.h"
16#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
17#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
18#include <numeric>
19
20namespace clang::ento::tagged_union_modeling {
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>
34ProgramStateRef
35removeInformationStoredForDeadInstances(const CallEvent &Call,
36 ProgramStateRef State,
37 ArrayRef<const MemRegion *> Regions) {
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>
51void handleConstructorAndAssignment(const CallEvent &Call, CheckerContext &C,
52 SVal ThisSVal) {
53 ProgramStateRef State = C.getState();
54
55 if (!State)
56 return;
57
58 auto ArgSVal = Call.getArgSVal(Index: 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
63 bool IsCopy = isCopyConstructorCall(Call) || isCopyAssignmentCall(Call);
64 bool IsMove = isMoveConstructorCall(Call) || isMoveAssignmentCall(Call);
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
97