Skip to content

Commit c750115

Browse files
committed
correctly handles roxygen in cpp files
1 parent 14531db commit c750115

File tree

12 files changed

+249
-28
lines changed

12 files changed

+249
-28
lines changed

R/register.R

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,22 +233,28 @@ generate_r_functions <- function(funs, package = "cpp11", use_package = FALSE) {
233233
glue::glue_data(funs, ".Call({package_names}{params}{package_call})")
234234
)
235235

236-
roxygen_comments <- lapply(funs$file, extract_roxygen_comments)
237-
238-
out <- mapply(function(name, list_params, calls, file, line) {
236+
# Parse and associate Roxygen comments
237+
funs$roxygen_comment <- mapply(function(file, line) {
239238
comments <- extract_roxygen_comments(file)
240-
roxygen_comment <- ""
239+
matched_comment <- ""
241240
for (comment in comments) {
242-
if (comment$line < line) {
243-
roxygen_comment <- comment$text
241+
# Check if the comment directly precedes the function without gaps
242+
if (line == comment$line + 1) {
243+
matched_comment <- comment$text
244+
break
244245
}
245246
}
247+
matched_comment
248+
}, funs$file, funs$line, SIMPLIFY = TRUE)
249+
250+
# Generate R functions with or without Roxygen comments
251+
out <- mapply(function(name, list_params, calls, roxygen_comment) {
246252
if (nzchar(roxygen_comment)) {
247253
glue::glue("{roxygen_comment}\n{name} <- function({list_params}) {{\n\t{calls}\n}}")
248254
} else {
249255
glue::glue("{name} <- function({list_params}) {{\n\t{calls}\n}}")
250256
}
251-
}, funs$name, funs$list_params, funs$calls, funs$file, funs$line, SIMPLIFY = FALSE)
257+
}, funs$name, funs$list_params, funs$calls, funs$roxygen_comment, SIMPLIFY = FALSE)
252258

253259
out <- as.character(out)
254260
out <- glue::trim(out)
@@ -268,7 +274,7 @@ extract_roxygen_comments <- function(file) {
268274
roxygen_comments <- mapply(function(start, end) {
269275
roxygen_lines <- lines[(start + 1):(end - 1)]
270276
roxygen_lines <- sub("^@", "#' @", roxygen_lines)
271-
list(line = start, text = paste(roxygen_lines, collapse = "\n"))
277+
list(line = end, text = paste(roxygen_lines, collapse = "\n"))
272278
}, roxygen_start, roxygen_end, SIMPLIFY = FALSE)
273279

274280
roxygen_comments

cpp11test/NAMESPACE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(roxcpp2_)
4+
export(roxcpp3_)
5+
export(roxcpp4_)
6+
export(roxcpp5_)
7+
export(roxcpp7_)
38
export(run_tests)
49
exportPattern("_$")
510
importFrom(Rcpp,sourceCpp)

cpp11test/R/cpp11.R

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -156,42 +156,59 @@ rcpp_release_ <- function(n) {
156156
invisible(.Call(`_cpp11test_rcpp_release_`, n))
157157
}
158158

159-
#' @title Roxygenise C++ function
160-
#' @param x numeric value
161-
#' @description Dummy function to test roxygen2. It adds 1.0 to a double.
162-
#' @export
163-
#' @examples roxcpp_(1.0)
164-
roxcpp1_ <- function(x) {
165-
.Call(`_cpp11test_roxcpp1_`, x)
159+
notroxcpp1_ <- function(x) {
160+
.Call(`_cpp11test_notroxcpp1_`, x)
166161
}
167162

168-
#' @title Roxygenise C++ function
163+
#' @title Roxygenise C++ function II
169164
#' @param x numeric value
170-
#' @description Dummy function to test roxygen2. It adds 1.0 to a double.
165+
#' @description Dummy function to test roxygen2. It adds 2.0 to a double.
171166
#' @export
172-
#' @examples roxcpp_(1.0)
167+
#' @examples roxcpp2_(1.0)
173168
roxcpp2_ <- function(x) {
174169
.Call(`_cpp11test_roxcpp2_`, x)
175170
}
176171

177-
#' @title Roxygenise C++ function
172+
#' @title Roxygenise C++ function III
178173
#' @param x numeric value
179-
#' @description Dummy function to test roxygen2. It adds 1.0 to a double.
174+
#' @description Dummy function to test roxygen2. It adds 3.0 to a double.
180175
#' @export
181-
#' @examples roxcpp_(1.0)
176+
#' @examples roxcpp3_(1.0)
182177
roxcpp3_ <- function(x) {
183178
.Call(`_cpp11test_roxcpp3_`, x)
184179
}
185180

186-
#' @title Roxygenise C++ function
181+
#' @title Roxygenise C++ function IV
187182
#' @param x numeric value
188-
#' @description Dummy function to test roxygen2. It adds 1.0 to a double.
183+
#' @description Dummy function to test roxygen2. It adds 4.0 to a double.
189184
#' @export
190-
#' @examples roxcpp_(1.0)
185+
#' @examples roxcpp4_(1.0)
191186
roxcpp4_ <- function(x) {
192187
.Call(`_cpp11test_roxcpp4_`, x)
193188
}
194189

190+
#' @title Roxygenise C++ function V
191+
#' @param x numeric value
192+
#' @description Dummy function to test roxygen2. It adds 5.0 to a double.
193+
#' @export
194+
#' @examples roxcpp5_(1.0)
195+
roxcpp5_ <- function(x) {
196+
.Call(`_cpp11test_roxcpp5_`, x)
197+
}
198+
199+
notroxcpp6_ <- function(x) {
200+
.Call(`_cpp11test_notroxcpp6_`, x)
201+
}
202+
203+
#' @title Roxygenise C++ function VII
204+
#' @param x numeric value
205+
#' @description Dummy function to test roxygen2. It adds 7.0 to a double.
206+
#' @export
207+
#' @examples roxcpp7_(1.0)
208+
roxcpp7_ <- function(x) {
209+
.Call(`_cpp11test_roxcpp7_`, x)
210+
}
211+
195212
cpp11_safe_ <- function(x_sxp) {
196213
.Call(`_cpp11test_cpp11_safe_`, x_sxp)
197214
}

cpp11test/man/roxcpp2_.Rd

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cpp11test/man/roxcpp3_.Rd

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cpp11test/man/roxcpp4_.Rd

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cpp11test/man/roxcpp5_.Rd

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cpp11test/man/roxcpp7_.Rd

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cpp11test/src/cpp11.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,10 @@ extern "C" SEXP _cpp11test_rcpp_release_(SEXP n) {
304304
END_CPP11
305305
}
306306
// roxygen1.cpp
307-
double roxcpp1_(double x);
308-
extern "C" SEXP _cpp11test_roxcpp1_(SEXP x) {
307+
double notroxcpp1_(double x);
308+
extern "C" SEXP _cpp11test_notroxcpp1_(SEXP x) {
309309
BEGIN_CPP11
310-
return cpp11::as_sexp(roxcpp1_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
310+
return cpp11::as_sexp(notroxcpp1_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
311311
END_CPP11
312312
}
313313
// roxygen1.cpp
@@ -331,6 +331,27 @@ extern "C" SEXP _cpp11test_roxcpp4_(SEXP x) {
331331
return cpp11::as_sexp(roxcpp4_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
332332
END_CPP11
333333
}
334+
// roxygen3.cpp
335+
double roxcpp5_(double x);
336+
extern "C" SEXP _cpp11test_roxcpp5_(SEXP x) {
337+
BEGIN_CPP11
338+
return cpp11::as_sexp(roxcpp5_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
339+
END_CPP11
340+
}
341+
// roxygen3.cpp
342+
double notroxcpp6_(double x);
343+
extern "C" SEXP _cpp11test_notroxcpp6_(SEXP x) {
344+
BEGIN_CPP11
345+
return cpp11::as_sexp(notroxcpp6_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
346+
END_CPP11
347+
}
348+
// roxygen3.cpp
349+
double roxcpp7_(double x);
350+
extern "C" SEXP _cpp11test_roxcpp7_(SEXP x) {
351+
BEGIN_CPP11
352+
return cpp11::as_sexp(roxcpp7_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
353+
END_CPP11
354+
}
334355
// safe.cpp
335356
SEXP cpp11_safe_(SEXP x_sxp);
336357
extern "C" SEXP _cpp11test_cpp11_safe_(SEXP x_sxp) {
@@ -528,6 +549,8 @@ static const R_CallMethodDef CallEntries[] = {
528549
{"_cpp11test_my_warning_n1", (DL_FUNC) &_cpp11test_my_warning_n1, 1},
529550
{"_cpp11test_my_warning_n1fmt", (DL_FUNC) &_cpp11test_my_warning_n1fmt, 1},
530551
{"_cpp11test_my_warning_n2fmt", (DL_FUNC) &_cpp11test_my_warning_n2fmt, 2},
552+
{"_cpp11test_notroxcpp1_", (DL_FUNC) &_cpp11test_notroxcpp1_, 1},
553+
{"_cpp11test_notroxcpp6_", (DL_FUNC) &_cpp11test_notroxcpp6_, 1},
531554
{"_cpp11test_protect_many_", (DL_FUNC) &_cpp11test_protect_many_, 1},
532555
{"_cpp11test_protect_many_cpp11_", (DL_FUNC) &_cpp11test_protect_many_cpp11_, 1},
533556
{"_cpp11test_protect_many_preserve_", (DL_FUNC) &_cpp11test_protect_many_preserve_, 1},
@@ -546,10 +569,11 @@ static const R_CallMethodDef CallEntries[] = {
546569
{"_cpp11test_rcpp_sum_int_for_", (DL_FUNC) &_cpp11test_rcpp_sum_int_for_, 1},
547570
{"_cpp11test_remove_altrep", (DL_FUNC) &_cpp11test_remove_altrep, 1},
548571
{"_cpp11test_row_sums", (DL_FUNC) &_cpp11test_row_sums, 1},
549-
{"_cpp11test_roxcpp1_", (DL_FUNC) &_cpp11test_roxcpp1_, 1},
550572
{"_cpp11test_roxcpp2_", (DL_FUNC) &_cpp11test_roxcpp2_, 1},
551573
{"_cpp11test_roxcpp3_", (DL_FUNC) &_cpp11test_roxcpp3_, 1},
552574
{"_cpp11test_roxcpp4_", (DL_FUNC) &_cpp11test_roxcpp4_, 1},
575+
{"_cpp11test_roxcpp5_", (DL_FUNC) &_cpp11test_roxcpp5_, 1},
576+
{"_cpp11test_roxcpp7_", (DL_FUNC) &_cpp11test_roxcpp7_, 1},
553577
{"_cpp11test_string_proxy_assignment_", (DL_FUNC) &_cpp11test_string_proxy_assignment_, 0},
554578
{"_cpp11test_string_push_back_", (DL_FUNC) &_cpp11test_string_push_back_, 0},
555579
{"_cpp11test_sum_dbl_accumulate2_", (DL_FUNC) &_cpp11test_sum_dbl_accumulate2_, 1},

cpp11test/src/roxygen1.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "cpp11/doubles.hpp"
2+
using namespace cpp11;
3+
4+
// Test: not documented + documented
5+
6+
// Not Roxygenised C++ function I
7+
[[cpp11::register]] double notroxcpp1_(double x) {
8+
double y = x + 1.0;
9+
return y;
10+
}
11+
12+
/* roxygen start
13+
@title Roxygenise C++ function II
14+
@param x numeric value
15+
@description Dummy function to test roxygen2. It adds 2.0 to a double.
16+
@export
17+
@examples roxcpp2_(1.0)
18+
roxygen end */
19+
[[cpp11::register]] double roxcpp2_(double x) {
20+
double y = x + 2.0;
21+
return y;
22+
}

0 commit comments

Comments
 (0)