1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "CheckFinalizerVisitor.h"
11 // Simple visitor to determine if the content of a field might be collected
12 // during finalization.
13 class MightBeCollectedVisitor
: public EdgeVisitor
{
15 explicit MightBeCollectedVisitor(bool is_eagerly_finalized
);
17 bool might_be_collected() const;
18 bool as_eagerly_finalized() const;
20 void VisitMember(Member
* edge
) override
;
21 void VisitCollection(Collection
* edge
) override
;
24 bool might_be_collected_
;
25 bool is_eagerly_finalized_
;
26 bool as_eagerly_finalized_
;
29 MightBeCollectedVisitor::MightBeCollectedVisitor(bool is_eagerly_finalized
)
30 : might_be_collected_(false),
31 is_eagerly_finalized_(is_eagerly_finalized
),
32 as_eagerly_finalized_(false) {
35 bool MightBeCollectedVisitor::might_be_collected() const {
36 return might_be_collected_
;
39 bool MightBeCollectedVisitor::as_eagerly_finalized() const {
40 return as_eagerly_finalized_
;
43 void MightBeCollectedVisitor::VisitMember(Member
* edge
) {
44 if (is_eagerly_finalized_
) {
45 if (edge
->ptr()->IsValue()) {
46 Value
* member
= static_cast<Value
*>(edge
->ptr());
47 if (member
->value()->IsEagerlyFinalized()) {
48 might_be_collected_
= true;
49 as_eagerly_finalized_
= true;
54 might_be_collected_
= true;
57 void MightBeCollectedVisitor::VisitCollection(Collection
* edge
) {
58 if (edge
->on_heap() && !is_eagerly_finalized_
) {
59 might_be_collected_
= !edge
->is_root();
61 edge
->AcceptMembers(this);
67 CheckFinalizerVisitor::CheckFinalizerVisitor(RecordCache
* cache
,
68 bool is_eagerly_finalized
)
69 : blacklist_context_(false),
71 is_eagerly_finalized_(is_eagerly_finalized
) {
74 CheckFinalizerVisitor::Errors
& CheckFinalizerVisitor::finalized_fields() {
75 return finalized_fields_
;
78 bool CheckFinalizerVisitor::WalkUpFromCXXOperatorCallExpr(
79 CXXOperatorCallExpr
* expr
) {
80 // Only continue the walk-up if the operator is a blacklisted one.
81 switch (expr
->getOperator()) {
84 this->WalkUpFromCallExpr(expr
);
91 bool CheckFinalizerVisitor::WalkUpFromCallExpr(CallExpr
* expr
) {
92 // We consider all non-operator calls to be blacklisted contexts.
93 bool prev_blacklist_context
= blacklist_context_
;
94 blacklist_context_
= true;
95 for (size_t i
= 0; i
< expr
->getNumArgs(); ++i
)
96 this->TraverseStmt(expr
->getArg(i
));
97 blacklist_context_
= prev_blacklist_context
;
101 bool CheckFinalizerVisitor::VisitMemberExpr(MemberExpr
* member
) {
102 FieldDecl
* field
= dyn_cast
<FieldDecl
>(member
->getMemberDecl());
106 RecordInfo
* info
= cache_
->Lookup(field
->getParent());
110 RecordInfo::Fields::iterator it
= info
->GetFields().find(field
);
111 if (it
== info
->GetFields().end())
114 if (seen_members_
.find(member
) != seen_members_
.end())
117 bool as_eagerly_finalized
= false;
118 if (blacklist_context_
&&
119 MightBeCollected(&it
->second
, &as_eagerly_finalized
)) {
120 finalized_fields_
.push_back(
121 Error(member
, as_eagerly_finalized
, &it
->second
));
122 seen_members_
.insert(member
);
127 bool CheckFinalizerVisitor::MightBeCollected(FieldPoint
* point
,
128 bool* as_eagerly_finalized
) {
129 MightBeCollectedVisitor
visitor(is_eagerly_finalized_
);
130 point
->edge()->Accept(&visitor
);
131 *as_eagerly_finalized
= visitor
.as_eagerly_finalized();
132 return visitor
.might_be_collected();