clang
20.0.0git
lib
StaticAnalyzer
Core
ProgramState.cpp
Go to the documentation of this file.
1
//= ProgramState.cpp - Path-Sensitive "State" for tracking values --*- 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
// This file implements ProgramState and ProgramStateManager.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "
clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h
"
14
#include "
clang/Analysis/CFG.h
"
15
#include "
clang/Basic/JsonSupport.h
"
16
#include "
clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h
"
17
#include "
clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h
"
18
#include "
clang/StaticAnalyzer/Core/PathSensitive/DynamicType.h
"
19
#include "
clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h
"
20
#include "
clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h
"
21
#include "llvm/Support/raw_ostream.h"
22
#include <optional>
23
24
using namespace
clang
;
25
using namespace
ento;
26
27
namespace
clang
{
namespace
ento {
28
/// Increments the number of times this state is referenced.
29
30
void
ProgramStateRetain
(
const
ProgramState
*state) {
31
++
const_cast<
ProgramState
*
>
(state)->refCount;
32
}
33
34
/// Decrement the number of times this state is referenced.
35
void
ProgramStateRelease
(
const
ProgramState
*state) {
36
assert(state->refCount > 0);
37
ProgramState
*
s
=
const_cast<
ProgramState
*
>
(state);
38
if
(--
s
->refCount == 0) {
39
ProgramStateManager
&Mgr =
s
->getStateManager();
40
Mgr.StateSet.RemoveNode(
s
);
41
s
->~ProgramState();
42
Mgr.freeStates.push_back(
s
);
43
}
44
}
45
}}
46
47
ProgramState::ProgramState
(
ProgramStateManager
*mgr,
const
Environment
& env,
48
StoreRef
st,
GenericDataMap
gdm)
49
: stateMgr(mgr),
50
Env
(env),
51
store(st.getStore()),
52
GDM(gdm),
53
refCount(0) {
54
stateMgr->
getStoreManager
().
incrementReferenceCount
(store);
55
}
56
57
ProgramState::ProgramState
(
const
ProgramState
&RHS)
58
: stateMgr(RHS.stateMgr),
Env
(RHS.
Env
), store(RHS.store), GDM(RHS.GDM),
59
PosteriorlyOverconstrained(RHS.PosteriorlyOverconstrained), refCount(0) {
60
stateMgr->
getStoreManager
().
incrementReferenceCount
(store);
61
}
62
63
ProgramState::~ProgramState
() {
64
if
(store)
65
stateMgr->
getStoreManager
().
decrementReferenceCount
(store);
66
}
67
68
int64_t
ProgramState::getID
()
const
{
69
return
getStateManager
().Alloc.identifyKnownAlignedObject<
ProgramState
>(
this
);
70
}
71
72
ProgramStateManager::ProgramStateManager
(
ASTContext
&Ctx,
73
StoreManagerCreator
CreateSMgr,
74
ConstraintManagerCreator
CreateCMgr,
75
llvm::BumpPtrAllocator &alloc,
76
ExprEngine
*ExprEng)
77
: Eng(ExprEng), EnvMgr(alloc), GDMFactory(alloc),
78
svalBuilder(
createSimpleSValBuilder
(alloc, Ctx, *this)),
79
CallEventMgr(new
CallEventManager
(alloc)), Alloc(alloc) {
80
StoreMgr = (*CreateSMgr)(*this);
81
ConstraintMgr = (*CreateCMgr)(*
this
, ExprEng);
82
}
83
84
85
ProgramStateManager::~ProgramStateManager
() {