Skip to content

Commit

Permalink
correctly handles roxygen in cpp files
Browse files Browse the repository at this point in the history
  • Loading branch information
pachadotdev committed Jan 4, 2025
1 parent 14531db commit c750115
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 28 deletions.
22 changes: 14 additions & 8 deletions R/register.R
Original file line number Diff line number Diff line change
Expand Up @@ -233,22 +233,28 @@ generate_r_functions <- function(funs, package = "cpp11", use_package = FALSE) {
glue::glue_data(funs, ".Call({package_names}{params}{package_call})")
)

roxygen_comments <- lapply(funs$file, extract_roxygen_comments)

out <- mapply(function(name, list_params, calls, file, line) {
# Parse and associate Roxygen comments
funs$roxygen_comment <- mapply(function(file, line) {
comments <- extract_roxygen_comments(file)
roxygen_comment <- ""
matched_comment <- ""
for (comment in comments) {
if (comment$line < line) {
roxygen_comment <- comment$text
# Check if the comment directly precedes the function without gaps
if (line == comment$line + 1) {
matched_comment <- comment$text
break
}
}
matched_comment
}, funs$file, funs$line, SIMPLIFY = TRUE)

# Generate R functions with or without Roxygen comments
out <- mapply(function(name, list_params, calls, roxygen_comment) {
if (nzchar(roxygen_comment)) {
glue::glue("{roxygen_comment}\n{name} <- function({list_params}) {{\n\t{calls}\n}}")
} else {
glue::glue("{name} <- function({list_params}) {{\n\t{calls}\n}}")
}
}, funs$name, funs$list_params, funs$calls, funs$file, funs$line, SIMPLIFY = FALSE)
}, funs$name, funs$list_params, funs$calls, funs$roxygen_comment, SIMPLIFY = FALSE)

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

roxygen_comments
Expand Down
5 changes: 5 additions & 0 deletions cpp11test/NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Generated by roxygen2: do not edit by hand

export(roxcpp2_)
export(roxcpp3_)
export(roxcpp4_)
export(roxcpp5_)
export(roxcpp7_)
export(run_tests)
exportPattern("_$")
importFrom(Rcpp,sourceCpp)
Expand Down
49 changes: 33 additions & 16 deletions cpp11test/R/cpp11.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,42 +156,59 @@ rcpp_release_ <- function(n) {
invisible(.Call(`_cpp11test_rcpp_release_`, n))
}

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

#' @title Roxygenise C++ function
#' @title Roxygenise C++ function II
#' @param x numeric value
#' @description Dummy function to test roxygen2. It adds 1.0 to a double.
#' @description Dummy function to test roxygen2. It adds 2.0 to a double.
#' @export
#' @examples roxcpp_(1.0)
#' @examples roxcpp2_(1.0)
roxcpp2_ <- function(x) {
.Call(`_cpp11test_roxcpp2_`, x)
}

#' @title Roxygenise C++ function
#' @title Roxygenise C++ function III
#' @param x numeric value
#' @description Dummy function to test roxygen2. It adds 1.0 to a double.
#' @description Dummy function to test roxygen2. It adds 3.0 to a double.
#' @export
#' @examples roxcpp_(1.0)
#' @examples roxcpp3_(1.0)
roxcpp3_ <- function(x) {
.Call(`_cpp11test_roxcpp3_`, x)
}

#' @title Roxygenise C++ function
#' @title Roxygenise C++ function IV
#' @param x numeric value
#' @description Dummy function to test roxygen2. It adds 1.0 to a double.
#' @description Dummy function to test roxygen2. It adds 4.0 to a double.
#' @export
#' @examples roxcpp_(1.0)
#' @examples roxcpp4_(1.0)
roxcpp4_ <- function(x) {
.Call(`_cpp11test_roxcpp4_`, x)
}

#' @title Roxygenise C++ function V
#' @param x numeric value
#' @description Dummy function to test roxygen2. It adds 5.0 to a double.
#' @export
#' @examples roxcpp5_(1.0)
roxcpp5_ <- function(x) {
.Call(`_cpp11test_roxcpp5_`, x)
}

notroxcpp6_ <- function(x) {
.Call(`_cpp11test_notroxcpp6_`, x)
}

#' @title Roxygenise C++ function VII
#' @param x numeric value
#' @description Dummy function to test roxygen2. It adds 7.0 to a double.
#' @export
#' @examples roxcpp7_(1.0)
roxcpp7_ <- function(x) {
.Call(`_cpp11test_roxcpp7_`, x)
}

cpp11_safe_ <- function(x_sxp) {
.Call(`_cpp11test_cpp11_safe_`, x_sxp)
}
Expand Down
17 changes: 17 additions & 0 deletions cpp11test/man/roxcpp2_.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions cpp11test/man/roxcpp3_.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions cpp11test/man/roxcpp4_.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions cpp11test/man/roxcpp5_.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions cpp11test/man/roxcpp7_.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 28 additions & 4 deletions cpp11test/src/cpp11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,10 @@ extern "C" SEXP _cpp11test_rcpp_release_(SEXP n) {
END_CPP11
}
// roxygen1.cpp
double roxcpp1_(double x);
extern "C" SEXP _cpp11test_roxcpp1_(SEXP x) {
double notroxcpp1_(double x);
extern "C" SEXP _cpp11test_notroxcpp1_(SEXP x) {
BEGIN_CPP11
return cpp11::as_sexp(roxcpp1_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
return cpp11::as_sexp(notroxcpp1_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
END_CPP11
}
// roxygen1.cpp
Expand All @@ -331,6 +331,27 @@ extern "C" SEXP _cpp11test_roxcpp4_(SEXP x) {
return cpp11::as_sexp(roxcpp4_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
END_CPP11
}
// roxygen3.cpp
double roxcpp5_(double x);
extern "C" SEXP _cpp11test_roxcpp5_(SEXP x) {
BEGIN_CPP11
return cpp11::as_sexp(roxcpp5_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
END_CPP11
}
// roxygen3.cpp
double notroxcpp6_(double x);
extern "C" SEXP _cpp11test_notroxcpp6_(SEXP x) {
BEGIN_CPP11
return cpp11::as_sexp(notroxcpp6_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
END_CPP11
}
// roxygen3.cpp
double roxcpp7_(double x);
extern "C" SEXP _cpp11test_roxcpp7_(SEXP x) {
BEGIN_CPP11
return cpp11::as_sexp(roxcpp7_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
END_CPP11
}
// safe.cpp
SEXP cpp11_safe_(SEXP x_sxp);
extern "C" SEXP _cpp11test_cpp11_safe_(SEXP x_sxp) {
Expand Down Expand Up @@ -528,6 +549,8 @@ static const R_CallMethodDef CallEntries[] = {
{"_cpp11test_my_warning_n1", (DL_FUNC) &_cpp11test_my_warning_n1, 1},
{"_cpp11test_my_warning_n1fmt", (DL_FUNC) &_cpp11test_my_warning_n1fmt, 1},
{"_cpp11test_my_warning_n2fmt", (DL_FUNC) &_cpp11test_my_warning_n2fmt, 2},
{"_cpp11test_notroxcpp1_", (DL_FUNC) &_cpp11test_notroxcpp1_, 1},
{"_cpp11test_notroxcpp6_", (DL_FUNC) &_cpp11test_notroxcpp6_, 1},
{"_cpp11test_protect_many_", (DL_FUNC) &_cpp11test_protect_many_, 1},
{"_cpp11test_protect_many_cpp11_", (DL_FUNC) &_cpp11test_protect_many_cpp11_, 1},
{"_cpp11test_protect_many_preserve_", (DL_FUNC) &_cpp11test_protect_many_preserve_, 1},
Expand All @@ -546,10 +569,11 @@ static const R_CallMethodDef CallEntries[] = {
{"_cpp11test_rcpp_sum_int_for_", (DL_FUNC) &_cpp11test_rcpp_sum_int_for_, 1},
{"_cpp11test_remove_altrep", (DL_FUNC) &_cpp11test_remove_altrep, 1},
{"_cpp11test_row_sums", (DL_FUNC) &_cpp11test_row_sums, 1},
{"_cpp11test_roxcpp1_", (DL_FUNC) &_cpp11test_roxcpp1_, 1},
{"_cpp11test_roxcpp2_", (DL_FUNC) &_cpp11test_roxcpp2_, 1},
{"_cpp11test_roxcpp3_", (DL_FUNC) &_cpp11test_roxcpp3_, 1},
{"_cpp11test_roxcpp4_", (DL_FUNC) &_cpp11test_roxcpp4_, 1},
{"_cpp11test_roxcpp5_", (DL_FUNC) &_cpp11test_roxcpp5_, 1},
{"_cpp11test_roxcpp7_", (DL_FUNC) &_cpp11test_roxcpp7_, 1},
{"_cpp11test_string_proxy_assignment_", (DL_FUNC) &_cpp11test_string_proxy_assignment_, 0},
{"_cpp11test_string_push_back_", (DL_FUNC) &_cpp11test_string_push_back_, 0},
{"_cpp11test_sum_dbl_accumulate2_", (DL_FUNC) &_cpp11test_sum_dbl_accumulate2_, 1},
Expand Down
22 changes: 22 additions & 0 deletions cpp11test/src/roxygen1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "cpp11/doubles.hpp"
using namespace cpp11;

// Test: not documented + documented

// Not Roxygenised C++ function I
[[cpp11::register]] double notroxcpp1_(double x) {
double y = x + 1.0;
return y;
}

/* roxygen start
@title Roxygenise C++ function II
@param x numeric value
@description Dummy function to test roxygen2. It adds 2.0 to a double.
@export
@examples roxcpp2_(1.0)
roxygen end */
[[cpp11::register]] double roxcpp2_(double x) {
double y = x + 2.0;
return y;
}
28 changes: 28 additions & 0 deletions cpp11test/src/roxygen2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "cpp11/doubles.hpp"
using namespace cpp11;

// Test: documented + documented

/* roxygen start
@title Roxygenise C++ function III
@param x numeric value
@description Dummy function to test roxygen2. It adds 3.0 to a double.
@export
@examples roxcpp3_(1.0)
roxygen end */
[[cpp11::register]] double roxcpp3_(double x) {
double y = x + 3.0;
return y;
}

/* roxygen start
@title Roxygenise C++ function IV
@param x numeric value
@description Dummy function to test roxygen2. It adds 4.0 to a double.
@export
@examples roxcpp4_(1.0)
roxygen end */
[[cpp11::register]] double roxcpp4_(double x) {
double y = x + 4.0;
return y;
}
34 changes: 34 additions & 0 deletions cpp11test/src/roxygen3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "cpp11/doubles.hpp"
using namespace cpp11;

// Test: documented + not documented + documented

/* roxygen start
@title Roxygenise C++ function V
@param x numeric value
@description Dummy function to test roxygen2. It adds 5.0 to a double.
@export
@examples roxcpp5_(1.0)
roxygen end */
[[cpp11::register]] double roxcpp5_(double x) {
double y = x + 5.0;
return y;
}

// Not Roxygenised C++ function VI
[[cpp11::register]] double notroxcpp6_(double x) {
double y = x + 6.0;
return y;
}

/* roxygen start
@title Roxygenise C++ function VII
@param x numeric value
@description Dummy function to test roxygen2. It adds 7.0 to a double.
@export
@examples roxcpp7_(1.0)
roxygen end */
[[cpp11::register]] double roxcpp7_(double x) {
double y = x + 7.0;
return y;
}

0 comments on commit c750115

Please sign in to comment.