-
-
Notifications
You must be signed in to change notification settings - Fork 243
/
LifetimeTracker.cpp
169 lines (128 loc) · 4.99 KB
/
LifetimeTracker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/******************************************************************************
*
* C++ Insights, copyright (C) by Andreas Fertig
* Distributed under an MIT license. See LICENSE for details
*
****************************************************************************/
#include <algorithm>
#include "ASTHelpers.h"
#include "CodeGenerator.h"
#include "DPrint.h"
#include "Insights.h"
#include "InsightsHelpers.h"
#include "NumberIterator.h"
//-----------------------------------------------------------------------------
namespace clang::insights {
using namespace asthelpers;
//-----------------------------------------------------------------------------
void LifetimeTracker::StartScope(bool funcStart)
{
RETURN_IF(not GetInsightsOptions().ShowLifetime)
++scopeCounter;
objects.push_back(
{.funcStart = funcStart ? LifetimeEntry::FuncStart::Yes : LifetimeEntry::FuncStart::No, .scope = scopeCounter});
}
//-----------------------------------------------------------------------------
void LifetimeTracker::AddExtended(const VarDecl* decl, const ValueDecl* extending)
{
// Search for the extending VarlDecl which is already in `objects`. Insert this decl _after_
if(auto it = std::ranges::find_if(objects, [&](const auto& e) { return e.item == extending; });
it != objects.end()) {
const auto scope = (*it).scope;
objects.insert(std::next(it), {decl, LifetimeEntry::FuncStart::No, scope});
}
}
//-----------------------------------------------------------------------------
void LifetimeTracker::Add(const VarDecl* decl)
{
RETURN_IF(not GetInsightsOptions().ShowLifetime)
QualType type{decl->getType()};
RETURN_IF(type->isPointerType() or type->isRValueReferenceType());
// For life-time extended objects
// XXX contains in C++23
RETURN_IF(std::ranges::find_if(objects, [&](const auto& e) { return e.item == decl; }) != objects.end());
objects.push_back({decl, LifetimeEntry::FuncStart::No, scopeCounter});
}
//-----------------------------------------------------------------------------
void LifetimeTracker::InsertDtorCall(const VarDecl* vd, OutputFormatHelper& ofm)
{
QualType type{vd->getType()};
if(const auto* ar = dyn_cast_or_null<ConstantArrayType>(type)) {
type = ar->getElementType();
}
if(type->isLValueReferenceType()) {
type = type.getNonReferenceType();
}
if(const auto& ctx = GetGlobalAST(); QualType::DK_cxx_destructor != vd->needsDestruction(ctx)) {
CodeGeneratorVariant cg{ofm};
cg->InsertArg(Comment(StrCat(GetName(*vd), " // lifetime ends here")));
return;
}
auto* dtorDecl = type->getAsCXXRecordDecl()->getDestructor();
auto* ic = CastLToRValue(vd);
CodeGeneratorVariant cg{ofm};
auto insertDtor = [&](Expr* member) {
auto* mem = AccessMember(member, dtorDecl, vd->getType()->isPointerType());
cg->InsertArg(CallMemberFun(mem, dtorDecl->getType()));
ofm.AppendSemiNewLine();
};
if(const auto* ar = dyn_cast_or_null<ConstantArrayType>(vd->getType()); ar and not GetInsightsOptions().UseShow2C) {
// not nice but call the destructor for each array element
for(const auto& i : NumberIterator{GetSize(ar)}) {
insertDtor(ArraySubscript(ic, i, type));
}
return;
}
insertDtor(ic);
}
//-----------------------------------------------------------------------------
bool LifetimeTracker::Return(OutputFormatHelper& ofm)
{
RETURN_FALSE_IF(not GetInsightsOptions().ShowLifetime or objects.empty())
bool ret{};
for(OnceTrue needsSemi{}; auto& e : llvm::reverse(objects)) {
if(LifetimeEntry::FuncStart::Yes == e.funcStart) {
break;
}
if(nullptr == e.item) {
continue;
}
if(needsSemi) {
CodeGeneratorVariant cg{ofm};
cg->InsertArg(mkNullStmt());
}
InsertDtorCall(e.item, ofm);
ret = true;
}
return ret;
}
//-----------------------------------------------------------------------------
void LifetimeTracker::removeTop()
{
objects.pop_back();
auto it = std::ranges::remove_if(objects, [&](const LifetimeEntry& e) { return (e.scope == scopeCounter); });
objects.erase(it.begin(), it.end());
--scopeCounter;
}
//-----------------------------------------------------------------------------
bool LifetimeTracker::EndScope(OutputFormatHelper& ofm, bool coveredByReturn)
{
RETURN_FALSE_IF(not GetInsightsOptions().ShowLifetime or objects.empty())
bool ret{};
if(not coveredByReturn) {
for(auto& e : llvm::reverse(objects)) {
if(e.scope != scopeCounter) {
break;
}
if(nullptr == e.item) {
break;
}
InsertDtorCall(e.item, ofm);
ret = true;
}
}
removeTop();
return ret;
}
//-----------------------------------------------------------------------------
} // namespace clang::insights