Skip to content

Commit c4a6e48

Browse files
Merge pull request #57 from HighlanderLab:gregorgorjanc/issue52
#52 Adding debug and sanitiser flags
2 parents 7288ac5 + b12577c commit c4a6e48

6 files changed

Lines changed: 87 additions & 18 deletions

File tree

RcppTskit/R/RcppExports.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,7 @@ test_tsk_trace_error_cpp <- function() {
111111
invisible(.Call(`_RcppTskit_test_tsk_trace_error_cpp`))
112112
}
113113

114+
tsk_trace_errors_defined <- function() {
115+
.Call(`_RcppTskit_tsk_trace_errors_defined`)
116+
}
117+

RcppTskit/notes_pkg_dev.Rmd

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ use_release_issue(version = NULL) # https://usethis.r-lib.org/reference/use_rele
144144
# just follow tasks in there;)
145145
```
146146

147-
## Setup
147+
## Dev tools setup / use
148148

149149
```
150150
install.packages(c("usethis", "devtools"))
151151
```
152152

153-
## Code testing coverage with covr
153+
### Code testing coverage with covr
154154

155155
https://covr.r-lib.org
156156

@@ -187,7 +187,37 @@ To ignore multiple lines use `# nocov start/stop`.
187187

188188
In `Rcpp` code use `// # nocov ...`.
189189

190-
## Air formatter of R code
190+
### Debugging C/C++
191+
192+
In `src/Makevars.in` uncomment debug flags ...
193+
194+
From terminal run:
195+
196+
```
197+
# Go to package folder
198+
R_HOME="/Library/Frameworks/R.framework/Resources"
199+
R_BIN="$R_HOME/bin/exec/R"
200+
ASAN="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/17/lib/darwin/libclang_rt.asan_osx_dynamic.dylib"
201+
UBSAN="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/17/lib/darwin/libclang_rt.ubsan_osx_dynamic.dylib"
202+
203+
env R_HOME="$R_HOME" \
204+
DYLD_INSERT_LIBRARIES="$ASAN:$UBSAN" \
205+
ASAN_OPTIONS="abort_on_error=1,detect_leaks=0,verbosity=1" \
206+
UBSAN_OPTIONS="print_stacktrace=1" \
207+
"$R_BIN" --vanilla
208+
```
209+
210+
In the R session (not from Rstudio/Positron!):
211+
212+
```
213+
# Load + compile into the current ASan session (no child R load)
214+
devtools::load_all()
215+
216+
# Run tests in the same process
217+
devtools::test()
218+
```
219+
220+
### Air formatter of R code
191221

192222
https://usethis.r-lib.org/reference/use_air.html
193223
```
@@ -205,7 +235,7 @@ air format .
205235
air format --check .
206236
```
207237

208-
## Jarl linter of R code
238+
### Jarl linter of R code
209239

210240
https://jarl.etiennebacher.com
211241

@@ -216,7 +246,7 @@ jarl check .
216246
jarl check --fix .
217247
```
218248

219-
## clang-format of C/C++ code
249+
### clang-format of C/C++ code
220250

221251
https://clang.llvm.org/docs/ClangFormat.html
222252

@@ -250,7 +280,7 @@ pre-commit run clang-format src/RcppTskit.cpp
250280
pre-commit run clang-format --all-files
251281
```
252282

253-
## clang-tidy linter of C/C++ code
283+
### clang-tidy linter of C/C++ code
254284

255285
https://clang.llvm.org/extra/clang-tidy/
256286

@@ -270,7 +300,7 @@ pre-commit run clang-tidy src/RcppTskit.cpp
270300
pre-commit run clang-tidy --all-files
271301
```
272302

273-
## GitHub actions
303+
### GitHub actions
274304

275305
```
276306
install.packages("usethis")

RcppTskit/src/Makevars.in

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ RCPPTSKIT_OBJECTS = $(RCPPTSKIT_C_SOURCES:.c=.o) $(RCPPTSKIT_CPP_SOURCES:.cpp=.o
3030
OBJECTS = $(TSKIT_OBJECTS) $(RCPPTSKIT_OBJECTS)
3131
# $(info OBJECTS = $(OBJECTS)) # for debugging, info is also a GNU extension
3232

33-
# Include paths and preprocessor defines
33+
# *Preprocessor (CPP)* flags for include paths and defines
3434
# * A bit complicated include/tskit structure due to how we include tskit headers
3535
# * $(R_INCLUDE_DIR) is for <R_ext/Error.h>
3636
PKG_CPPFLAGS = \
@@ -39,14 +39,29 @@ PKG_CPPFLAGS = \
3939
-I../inst/include/tskit/tskit \
4040
-I$(R_INCLUDE_DIR)
4141

42-
# Compiler flags
43-
PKG_CFLAGS = -DNDEBUG # to remove calls to assert() as per the R extensions manual
44-
# PKG_CFLAGS = -DNDEBUG -DTSK_TRACE_ERRORS # to also enable error tracing in tskit C
45-
# PKG_CXXFLAGS = -DTSK_TRACE_ERRORS # to also enable error tracing in tskit C as called from Rcpp
42+
# *Compiler (C/CXX)* flags
43+
# Uncomment for local debugging
44+
# SAN_FLAGS enables ASan/UBSan to catch memory/UB errors in dev builds (only run R in terminal!)
45+
# TSK_TRACE_ERRORS enables tskit C error tracing
46+
# DEBUG_OPT = -g -O1 # -O1 is needed for many of the below flags
47+
# WARN_FLAGS = $(DEBUG_OPT) -Wall -Wextra -Wpedantic -Wuninitialized
48+
# SAN_FLAGS = $(DEBUG_OPT) -fno-omit-frame-pointer -fsanitize=address,undefined -fno-common
49+
# DEV_FLAGS = $(SAN_FLAGS) $(WARN_FLAGS) -DTSK_TRACE_ERRORS
50+
# RCPPTSKIT_CFLAGS = $(DEV_FLAGS)
51+
# RCPPTSKIT_CXXFLAGS = $(DEV_FLAGS)
52+
# RCPPTSKIT_LDFLAGS = -fsanitize=address,undefined
53+
# Uncomment for release builds
54+
# NDEBUG removes calls to assert()
55+
RCPPTSKIT_CFLAGS = -DNDEBUG
56+
RCPPTSKIT_CXXFLAGS = -DNDEBUG
57+
RCPPTSKIT_LDFLAGS =
58+
# Use the above choices
59+
PKG_CFLAGS = $(RCPPTSKIT_CFLAGS)
60+
PKG_CXXFLAGS = $(RCPPTSKIT_CXXFLAGS)
4661

4762
# Explicit compile rule for tskit C files
4863
tskit/%.o: tskit/%.c
4964
$(CC) $(ALL_CPPFLAGS) $(PKG_CFLAGS) $(CFLAGS) $(CPICFLAGS) -c $< -o $@
5065

5166
# Linking
52-
PKG_LIBS = @RCPPTSKIT_LIB@
67+
PKG_LIBS = @RCPPTSKIT_LIB@ $(RCPPTSKIT_LDFLAGS)

RcppTskit/src/RcppExports.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,16 @@ BEGIN_RCPP
266266
return R_NilValue;
267267
END_RCPP
268268
}
269+
// tsk_trace_errors_defined
270+
bool tsk_trace_errors_defined();
271+
RcppExport SEXP _RcppTskit_tsk_trace_errors_defined() {
272+
BEGIN_RCPP
273+
Rcpp::RObject rcpp_result_gen;
274+
Rcpp::RNGScope rcpp_rngScope_gen;
275+
rcpp_result_gen = Rcpp::wrap(tsk_trace_errors_defined());
276+
return rcpp_result_gen;
277+
END_RCPP
278+
}
269279

270280
static const R_CallMethodDef CallEntries[] = {
271281
{"_RcppTskit_kastore_version", (DL_FUNC) &_RcppTskit_kastore_version, 0},
@@ -292,6 +302,7 @@ static const R_CallMethodDef CallEntries[] = {
292302
{"_RcppTskit_test_tsk_bug_assert_cpp", (DL_FUNC) &_RcppTskit_test_tsk_bug_assert_cpp, 0},
293303
{"_RcppTskit_test_tsk_trace_error_c", (DL_FUNC) &_RcppTskit_test_tsk_trace_error_c, 0},
294304
{"_RcppTskit_test_tsk_trace_error_cpp", (DL_FUNC) &_RcppTskit_test_tsk_trace_error_cpp, 0},
305+
{"_RcppTskit_tsk_trace_errors_defined", (DL_FUNC) &_RcppTskit_tsk_trace_errors_defined, 0},
295306
{NULL, NULL, 0}
296307
};
297308

RcppTskit/src/test_tsk_abort_stderr.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,12 @@ void test_tsk_trace_error_c() { RcppTskit_trace_error_c(); } // # nocov
1818
// This is tested if we compile with -DTSK_TRACE_ERRORS
1919
// [[Rcpp::export]]
2020
void test_tsk_trace_error_cpp() { (void)tsk_trace_error(-1); } // # nocov
21+
22+
// [[Rcpp::export]]
23+
bool tsk_trace_errors_defined() {
24+
#ifdef TSK_TRACE_ERRORS
25+
return true;
26+
#else
27+
return false;
28+
#endif
29+
}

RcppTskit/tests/testthat/test_misc.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ test_that("tskit_version() works", {
1111
})
1212

1313
test_that("tsk_bug_assert() works", {
14-
expect_error(test_tsk_bug_assert_c())
15-
expect_error(test_tsk_bug_assert_cpp())
14+
expect_error(RcppTskit:::test_tsk_bug_assert_c())
15+
expect_error(RcppTskit:::test_tsk_bug_assert_cpp())
1616
})
1717

1818
test_that("tsk_trace_error() works", {
1919
t <- "You have to compile with -DTSK_TRACE_ERRORS to run these tests. See src/Makevars.in."
20-
skip(t)
21-
expect_warning(test_tsk_trace_error_c())
22-
expect_warning(test_tsk_trace_error_cpp())
20+
skip_if_not(RcppTskit:::tsk_trace_errors_defined(), t)
21+
expect_warning(RcppTskit:::test_tsk_trace_error_c())
22+
expect_warning(RcppTskit:::test_tsk_trace_error_cpp())
2323
})

0 commit comments

Comments
 (0)