GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 9 / 0 / 9
Functions: 100.0% 3 / 0 / 3
Branches: 60.0% 3 / 0 / 5

ruby/bindings/ModelicaAntlrCleanup.cpp
Line Branch Exec Source
1 #include "ModelicaAntlrCleanup.hpp"
2
3 #include <antlr4-runtime.h>
4 #include <ruby.h>
5
6 #include <memory>
7 #include <mutex>
8
9 namespace {
10
11 using FactoryPtr = std::unique_ptr<antlr4::TokenFactory<antlr4::CommonToken>>;
12
13 353 extern "C" void modelica_antlr_cleanup(VALUE) {
14 // This runs as part of Ruby's end_proc chain before the VM dlcloses openstudio.so. We explicitly
15 // release the singleton so ANTLR's own static destructor becomes a no-op; otherwise Ruby would
16 // run the destructor after parts of the interpreter are gone and we'd segfault while unloading.
17 353 auto* defaultFactory = const_cast<FactoryPtr*>(&antlr4::CommonTokenFactory::DEFAULT);
18 353 auto* raw = defaultFactory->release();
19
1/2
✓ Branch 0 taken 353 times.
✗ Branch 1 not taken.
353 if (raw != nullptr) {
20
1/2
✓ Branch 0 taken 353 times.
✗ Branch 1 not taken.
353 delete raw;
21 }
22 353 }
23
24 } // namespace
25
26 namespace openstudio::ruby {
27
28 355 void registerModelicaAntlrCleanup() {
29 // Ruby's Init_* entry points are called once per interpreter, so guard the end_proc registration
30 // with std::call_once to avoid stacking duplicate callbacks when Init_openstudiomodelica is
31 // invoked multiple times (eg. when tests reload the bindings). This static also satisfies the
32 // "just use a registrar" idea without running Ruby APIs during global constructors; rb_set_end_proc
33 // must execute while the VM is alive, so the call_once guard lives inside the Init path instead of
34 // at the translation-unit scope.
35 static std::once_flag registeredFlag;
36
1/1
✓ Branch 1 taken 355 times.
710 std::call_once(registeredFlag, []() { rb_set_end_proc(modelica_antlr_cleanup, Qnil); });
37 355 }
38
39 } // namespace openstudio::ruby
40