• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vla5924-practice / compiler-project / 14007434834

22 Mar 2025 09:48AM UTC coverage: 82.886%. Remained the same
14007434834

push

github

web-flow
Resolve issues with OptBuilder::Notifier (#226)

Prevent dangling reference and nullptr invocation.

1 of 2 new or added lines in 2 files covered. (50.0%)

4722 of 5697 relevant lines covered (82.89%)

270.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.83
/compiler/lib/backend/optree/optimizer/opt_builder.cpp
1
#include "optimizer/opt_builder.hpp"
2

3
#include <functional>
4

5
#include "compiler/optree/builder.hpp"
6
#include "compiler/optree/operation.hpp"
7
#include "compiler/utils/debug.hpp"
8
#include "compiler/utils/helpers.hpp"
9

10
using namespace optree;
11
using namespace optree::optimizer;
12

13
using dbg = utils::DebugPrinter;
14

15
namespace {
16

17
void notifyInsertRecursively(const Operation::Ptr &op, const OptBuilder::Notifier &notifier) {
4✔
18
    for (const auto &nestedOp : op->body) {
4✔
19
        notifyInsertRecursively(nestedOp, notifier);
×
NEW
20
        notifier.notifyInsert(nestedOp);
×
21
    }
22
}
4✔
23

24
} // namespace
25

26
OptBuilder::Notifier::Notifier(const Callback &onInsert, const Callback &onUpdate, const Callback &onErase)
×
27
    : onInsert(onInsert), onUpdate(onUpdate), onErase(onErase) {
×
28
}
×
29

30
void OptBuilder::Notifier::notifyInsert(const Operation::Ptr &op) const {
37✔
31
    if (onInsert)
37✔
32
        onInsert(op);
37✔
33
}
37✔
34

35
void OptBuilder::Notifier::notifyUpdate(const Operation::Ptr &op) const {
4✔
36
    if (onUpdate)
4✔
37
        onUpdate(op);
4✔
38
}
4✔
39

40
void OptBuilder::Notifier::notifyErase(const Operation::Ptr &op) const {
83✔
41
    if (onErase)
83✔
42
        onErase(op);
83✔
43
}
83✔
44

45
void OptBuilder::insert(const Operation::Ptr &op) {
37✔
46
    COMPILER_DEBUG(dbg::get() << "  Insert " << op->name << '\n');
37✔
47
    Builder::insert(op);
37✔
48
    notifier.notifyInsert(op);
37✔
49
}
37✔
50

51
Operation::Ptr OptBuilder::clone(const Operation::Ptr &op) {
4✔
52
    COMPILER_DEBUG(dbg::get() << "  Clone " << op->name << "{\n");
4✔
53
    auto newOp = op->clone();
4✔
54
    notifyInsertRecursively(newOp, notifier);
4✔
55
    insert(newOp);
4✔
56
    COMPILER_DEBUG(dbg::get() << "  }\n");
4✔
57
    return newOp;
4✔
58
}
×
59

60
void OptBuilder::erase(const Operation::Ptr &op) {
83✔
61
    if (op->parent)
83✔
62
        setInsertPointAfter(op);
83✔
63
    while (!op->body.empty()) {
113✔
64
        erase(op->body.back());
30✔
65
    }
66
    COMPILER_DEBUG(dbg::get() << "  Erase " << op->name << '\n');
83✔
67
    notifier.notifyErase(op);
83✔
68
    op->erase();
83✔
69
}
83✔
70

71
void OptBuilder::update(const Operation::Ptr &op, const std::function<void()> &actor) {
4✔
72
    COMPILER_DEBUG(dbg::get() << "  Update " << op->name << '\n');
4✔
73
    if (actor)
4✔
74
        actor();
4✔
75
    notifier.notifyUpdate(op);
4✔
76
}
4✔
77

78
void OptBuilder::replace(const Operation::Ptr &op, const Operation::Ptr &newOp) {
37✔
79
    COMPILER_DEBUG(dbg::get() << "  Replace " << op->name << '\n');
37✔
80
    for (const auto &[oldResult, newResult] : utils::zip(op->results, newOp->results)) {
74✔
81
        for (const auto &use : oldResult->uses) {
41✔
82
            auto user = use.lock();
4✔
83
            update(user, [&] { user->operand(use.operandNumber) = newResult; });
8✔
84
        }
4✔
85
        newResult->uses.splice_after(newResult->uses.before_begin(), oldResult->uses);
37✔
86
    }
37✔
87
    erase(op);
37✔
88
}
37✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc