Skip to content

Commit b2a29f1

Browse files
committed
Redefining tsk_bug_assert to use Rcpp::stop or Rf_error #5
1 parent 92d39c6 commit b2a29f1

7 files changed

Lines changed: 90 additions & 7 deletions

File tree

tskitr/R/RcppExports.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
22
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
33

4+
test_tsk_bug_assert_c <- function() {
5+
invisible(.Call(`_tskitr_test_tsk_bug_assert_c`))
6+
}
7+
8+
test_tsk_bug_assert_cpp <- function() {
9+
invisible(.Call(`_tskitr_test_tsk_bug_assert_cpp`))
10+
}
11+
412
#' Report the version of installed kastore C API
513
#'
614
#' @details The version is stored in the installed header \code{kastore.h}.

tskitr/inst/include/tskit/core.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,29 @@
11
// Shim header to use tskit C API in Rcpp code in an R session or an R package
22
#include <tskit/tskit/core.h>
3+
4+
// Redefinition of tsk_bug_assert to avoid aborting R sessions with tskit C API
5+
// (following R extensions manual recommendations).
6+
// While tsk_bug_assert is called only in C API (atm) we provide both C and C++
7+
// macros for completeness.
8+
// TODO: Redefine TSK_BUG_ASSERT_MESSAGE or create tskitr version if we will use
9+
// tsk_bug_assert in Rcpp. Will not, yet, open GitHub issue at this stage.
10+
#undef tsk_bug_assert
11+
#ifdef __cplusplus
12+
#include <Rcpp.h>
13+
#define tsk_bug_assert(condition) \
14+
do { \
15+
if (!(condition)) { \
16+
Rcpp::stop("Bug detected in %s at line %d. %s", __FILE__, __LINE__, \
17+
TSK_BUG_ASSERT_MESSAGE); \
18+
} \
19+
} while (0)
20+
#else
21+
#include <R_ext/Error.h>
22+
#define tsk_bug_assert(condition) \
23+
do { \
24+
if (!(condition)) { \
25+
Rf_error("Bug detected in %s at line %d. %s", __FILE__, __LINE__, \
26+
TSK_BUG_ASSERT_MESSAGE); \
27+
} \
28+
} while (0)
29+
#endif

tskitr/src/Makevars.in

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,34 @@ TSKIT_SOURCES = \
1313
TSKIT_OBJECTS = $(TSKIT_SOURCES:.c=.o)
1414
# $(info TSKIT_OBJECTS = $(TSKIT_OBJECTS)) # for debugging, info is also a GNU extension
1515

16-
# tskitr C++ sources & objects
17-
# TSKITR_SOURCES = $(wildcard *.cpp) # wildcard is a GNU extension and not desired on CRAN
18-
TSKITR_SOURCES = \
16+
# tskitr C/C++ sources & objects
17+
# TSKITR_CPP_SOURCES = $(wildcard *.cpp) # wildcard is a GNU extension and not desired on CRAN
18+
TSKITR_CPP_SOURCES = \
1919
tskitr.cpp \
20+
test_tsk_bug_assert.cpp \
2021
RcppExports.cpp
21-
# $(info TSKITR_SOURCES = $(TSKITR_SOURCES)) # for debugging, info is also a GNU extension
22-
TSKITR_OBJECTS = $(TSKITR_SOURCES:.cpp=.o)
22+
# $(info TSKITR_CPP_SOURCES = $(TSKITR_CPP_SOURCES)) # for debugging, info is also a GNU extension
23+
TSKITR_C_SOURCES = \
24+
test_tsk_bug_assert_c.c
25+
# $(info TSKITR_C_SOURCES = $(TSKITR_C_SOURCES)) # for debugging, info is also a GNU extension
26+
TSKITR_OBJECTS = $(TSKITR_CPP_SOURCES:.cpp=.o) $(TSKITR_C_SOURCES:.c=.o)
2327
# $(info TSKITR_OBJECTS = $(TSKITR_OBJECTS)) # for debugging, info is also a GNU extension
2428

2529
# All objects
2630
OBJECTS = $(TSKIT_OBJECTS) $(TSKITR_OBJECTS)
2731
# $(info OBJECTS = $(OBJECTS)) # for debugging, info is also a GNU extension
2832

2933
# Include paths and preprocessor defines
30-
PKG_CPPFLAGS = -I../inst/include -I../inst/include/tskit -I../inst/include/tskit/tskit
34+
# * A bit complicated include/tskit structure due to how we include tskit headers
35+
# * $(R_HOME)/include is for <R_ext/Error.h>
36+
PKG_CPPFLAGS = \
37+
-I../inst/include \
38+
-I../inst/include/tskit \
39+
-I../inst/include/tskit/tskit \
40+
-I$(R_HOME)/include
3141

3242
# Compiler flags
33-
PKG_CFLAGS = -DNDEBUG # to remove calls to assert()
43+
PKG_CFLAGS = -DNDEBUG # to remove calls to assert() as per the R extensions manual
3444
# TODO: Should we port any flags from extern/tskit/c/meson.build to R build system? #6
3545
# https://github.com/HighlanderLab/tskitr/issues/6
3646
# See default flags used by clang (see below output from devtools::install())

tskitr/src/RcppExports.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ Rcpp::Rostream<true>& Rcpp::Rcout = Rcpp::Rcpp_cout_get();
1010
Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
1111
#endif
1212

13+
// test_tsk_bug_assert_c
14+
void test_tsk_bug_assert_c();
15+
RcppExport SEXP _tskitr_test_tsk_bug_assert_c() {
16+
BEGIN_RCPP
17+
Rcpp::RNGScope rcpp_rngScope_gen;
18+
test_tsk_bug_assert_c();
19+
return R_NilValue;
20+
END_RCPP
21+
}
22+
// test_tsk_bug_assert_cpp
23+
void test_tsk_bug_assert_cpp();
24+
RcppExport SEXP _tskitr_test_tsk_bug_assert_cpp() {
25+
BEGIN_RCPP
26+
Rcpp::RNGScope rcpp_rngScope_gen;
27+
test_tsk_bug_assert_cpp();
28+
return R_NilValue;
29+
END_RCPP
30+
}
1331
// kastore_version
1432
Rcpp::IntegerVector kastore_version();
1533
RcppExport SEXP _tskitr_kastore_version() {
@@ -210,6 +228,8 @@ END_RCPP
210228
}
211229

212230
static const R_CallMethodDef CallEntries[] = {
231+
{"_tskitr_test_tsk_bug_assert_c", (DL_FUNC) &_tskitr_test_tsk_bug_assert_c, 0},
232+
{"_tskitr_test_tsk_bug_assert_cpp", (DL_FUNC) &_tskitr_test_tsk_bug_assert_cpp, 0},
213233
{"_tskitr_kastore_version", (DL_FUNC) &_tskitr_kastore_version, 0},
214234
{"_tskitr_tskit_version", (DL_FUNC) &_tskitr_tskit_version, 0},
215235
{"_tskitr_ts_load", (DL_FUNC) &_tskitr_ts_load, 2},

tskitr/src/test_tsk_bug_assert.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <Rcpp.h>
2+
#include <tskit/core.h>
3+
4+
extern "C" void tskitr_bug_assert_c(void);
5+
6+
// [[Rcpp::export]]
7+
void test_tsk_bug_assert_c() { tskitr_bug_assert_c(); }
8+
9+
// [[Rcpp::export]]
10+
void test_tsk_bug_assert_cpp() { tsk_bug_assert(false); }

tskitr/src/test_tsk_bug_assert_c.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include <tskit/core.h>
2+
3+
void tskitr_bug_assert_c(void) { tsk_bug_assert(0); }

tskitr/tests/testthat/test_tskitr.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ test_that("tskit_version() works", {
1111
expect_true(is.integer(v))
1212
expect_equal(names(v), c("major", "minor", "patch"))
1313
})
14+
15+
test_that("tsk_bug_assert() works", {
16+
expect_error(test_tsk_bug_assert_c())
17+
expect_error(test_tsk_bug_assert_cpp())
18+
})

0 commit comments

Comments
 (0)