forked from andreasfertig/cppinsights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLifetimeTracker.cpp
154 lines (115 loc) · 4.3 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
/******************************************************************************
*
* 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::Add(const VarDecl* decl)
{
RETURN_IF(not GetInsightsOptions().ShowLifetime)
QualType type{decl->getType()};
RETURN_IF(type->isPointerType() or type->isRValueReferenceType());
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