Skip to content

Commit b5e5e34

Browse files
authored
Merge branch 'main' into complex
2 parents 511a7ed + 5e945fc commit b5e5e34

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2623
-1574
lines changed

.github/workflows/R-CMD-check.yaml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,21 @@ jobs:
2424
fail-fast: false
2525
matrix:
2626
config:
27+
- {os: macos-13, r: 'oldrel'}
2728
- {os: macos-latest, r: 'release'}
2829

2930
- {os: windows-latest, r: 'release'}
3031
# use 4.1 to check with rtools40's older compiler
3132
- {os: windows-latest, r: '4.1'}
3233

3334
# Use older ubuntu to maximise backward compatibility
34-
- {os: ubuntu-20.04, r: 'devel', http-user-agent: 'release'}
35-
- {os: ubuntu-20.04, r: 'release'}
36-
- {os: ubuntu-20.04, r: 'release', custom: 'no-cpp11test'}
37-
- {os: ubuntu-20.04, r: 'oldrel-1'}
38-
- {os: ubuntu-20.04, r: 'oldrel-2'}
39-
- {os: ubuntu-20.04, r: 'oldrel-3'}
40-
- {os: ubuntu-20.04, r: 'oldrel-4'}
35+
- {os: ubuntu-22.04, r: 'devel', http-user-agent: 'release'}
36+
- {os: ubuntu-22.04, r: 'release'}
37+
- {os: ubuntu-22.04, r: 'release', custom: 'no-cpp11test'}
38+
- {os: ubuntu-22.04, r: 'oldrel-1'}
39+
- {os: ubuntu-22.04, r: 'oldrel-2'}
40+
- {os: ubuntu-22.04, r: 'oldrel-3'}
41+
- {os: ubuntu-22.04, r: 'oldrel-4'}
4142

4243
env:
4344
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/format.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ name: format_check
99

1010
jobs:
1111
format_check:
12-
runs-on: ubuntu-20.04
12+
runs-on: ubuntu-22.04
1313
steps:
1414
- uses: actions/checkout@v2
1515

1616
- name: Install ClangFormat
17-
run: sudo apt-get install -y clang-format-10
17+
run: sudo apt-get install -y clang-format-12
1818

1919
- name: Run ClangFormat
20-
run: make format clang_format=clang-format-10
20+
run: make format clang_format=clang-format-12
2121

2222
- name: Check for a non-empty diff
2323
run: git diff-files -U --exit-code

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# cpp11 (development version)
22

3+
# cpp11 0.5.2
4+
5+
* Fixed an issue related to `-Wdeprecated-literal-operator` (#447, @andrjohns).
6+
37
# cpp11 0.5.1
48

59
* cpp11 now requires R >=4.0.0, in line with the

R/register.R

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,16 @@ cpp_register <- function(path = ".", quiet = !is_interactive(), extension = c(".
7777
cli::cli_alert_success("generated file {.file {basename(r_path)}}")
7878
}
7979

80-
8180
call_entries <- get_call_entries(path, funs$name, package)
8281

8382
cpp_function_registration <- glue::glue_data(funs, ' {{
8483
"_cpp11_{name}", (DL_FUNC) &_{package}_{name}, {n_args}}}, ',
8584
n_args = viapply(funs$args, nrow)
8685
)
8786

88-
cpp_function_registration <- glue::glue_collapse(cpp_function_registration, sep = "\n")
87+
cpp_function_registration <- glue::glue_collapse(cpp_function_registration, sep = "\n")
8988

90-
extra_includes <- character()
89+
extra_includes <- character()
9190
if (pkg_links_to_rcpp(path)) {
9291
extra_includes <- c(extra_includes, "#include <cpp11/R.hpp>", "#include <Rcpp.h>", "using namespace Rcpp;")
9392
}
@@ -215,35 +214,75 @@ generate_init_functions <- function(funs) {
215214
}
216215

217216
generate_r_functions <- function(funs, package = "cpp11", use_package = FALSE) {
218-
funs <- funs[c("name", "return_type", "args")]
217+
funs <- funs[c("name", "return_type", "args", "file", "line", "decoration")]
219218

220219
if (use_package) {
221220
package_call <- glue::glue(', PACKAGE = "{package}"')
222221
package_names <- glue::glue_data(funs, '"_{package}_{name}"')
223222
} else {
224-
package_names <- glue::glue_data(funs, '`_{package}_{name}`')
223+
package_names <- glue::glue_data(funs, "`_{package}_{name}`")
225224
package_call <- ""
226225
}
227226

228-
funs$package <- package
229227
funs$package_call <- package_call
230228
funs$list_params <- vcapply(funs$args, glue_collapse_data, "{name}")
231229
funs$params <- vcapply(funs$list_params, function(x) if (nzchar(x)) paste0(", ", x) else x)
232230
is_void <- funs$return_type == "void"
233231
funs$calls <- ifelse(is_void,
234-
glue::glue_data(funs, 'invisible(.Call({package_names}{params}{package_call}))'),
235-
glue::glue_data(funs, '.Call({package_names}{params}{package_call})')
232+
glue::glue_data(funs, "invisible(.Call({package_names}{params}{package_call}))"),
233+
glue::glue_data(funs, ".Call({package_names}{params}{package_call})")
236234
)
237235

238-
out <- glue::glue_data(funs, '
239-
{name} <- function({list_params}) {{
240-
{calls}
241-
}}
242-
')
236+
# Parse and associate Roxygen comments
237+
funs$roxygen_comment <- mapply(function(file, line) {
238+
if (file.exists(file)) {
239+
comments <- extract_roxygen_comments(file)
240+
matched_comment <- ""
241+
for (comment in comments) {
242+
# Check if the comment directly precedes the function without gaps
243+
if (line == comment$line + 1) {
244+
matched_comment <- comment$text
245+
break
246+
}
247+
}
248+
matched_comment
249+
} else {
250+
""
251+
}
252+
}, funs$file, funs$line, SIMPLIFY = TRUE)
253+
254+
# Generate R functions with or without Roxygen comments
255+
out <- mapply(function(name, list_params, calls, roxygen_comment) {
256+
if (nzchar(roxygen_comment)) {
257+
glue::glue("{roxygen_comment}\n{name} <- function({list_params}) {{\n\t{calls}\n}}")
258+
} else {
259+
glue::glue("{name} <- function({list_params}) {{\n {calls}\n}}")
260+
}
261+
}, funs$name, funs$list_params, funs$calls, funs$roxygen_comment, SIMPLIFY = TRUE)
262+
263+
out <- glue::trim(out)
243264
out <- glue::glue_collapse(out, sep = "\n\n")
244265
unclass(out)
245266
}
246267

268+
extract_roxygen_comments <- function(file) {
269+
lines <- readLines(file)
270+
roxygen_start <- grep("^/\\* roxygen start", lines)
271+
roxygen_end <- grep("roxygen end \\*/$", lines)
272+
273+
if (length(roxygen_start) == 0 || length(roxygen_end) == 0) {
274+
return(list())
275+
}
276+
277+
roxygen_comments <- mapply(function(start, end) {
278+
roxygen_lines <- lines[(start + 1):(end - 1)]
279+
roxygen_lines <- sub("^", "#' ", roxygen_lines)
280+
list(line = end, text = paste(roxygen_lines, collapse = "\n"))
281+
}, roxygen_start, roxygen_end, SIMPLIFY = FALSE)
282+
283+
roxygen_comments
284+
}
285+
247286
wrap_call <- function(name, return_type, args) {
248287
call <- glue::glue('{name}({list_params})', list_params = glue_collapse_data(args, "cpp11::as_cpp<cpp11::decay_t<{type}>>({name})"))
249288
if (return_type == "void") {

R/source.R

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
#' uses 'CXX11' if unset.
1919
#' @param dir The directory to store the generated source files. `tempfile()` is
2020
#' used by default. The directory will be removed if `clean` is `TRUE`.
21+
#' @param local Passed to [dyn.load()]. If `TRUE` (the default) the shared
22+
#' library is loaded with local symbols; if `FALSE` symbols are made global
23+
#' (equivalent to `dyn.load(..., local = FALSE)`), which can be required when
24+
#' other shared objects need to see RTTI/vtable symbols from this library.
25+
#' @note See the unit test that demonstrates this usage at
26+
#' \code{tests/testthat/test-source-local.R} (shows how `local = FALSE` exports
27+
#' the necessary symbols so separate shared objects can link against them).
2128
#' @return For [cpp_source()] and `[cpp_function()]` the results of
2229
#' [dyn.load()] (invisibly). For `[cpp_eval()]` the results of the evaluated
2330
#' expression.
@@ -65,7 +72,7 @@
6572
#' }
6673
#'
6774
#' @export
68-
cpp_source <- function(file, code = NULL, env = parent.frame(), clean = TRUE, quiet = TRUE, cxx_std = Sys.getenv("CXX_STD", "CXX11"), dir = tempfile()) {
75+
cpp_source <- function(file, code = NULL, env = parent.frame(), clean = TRUE, quiet = TRUE, cxx_std = Sys.getenv("CXX_STD", "CXX11"), dir = tempfile(), local = TRUE) {
6976
stop_unless_installed(c("brio", "callr", "cli", "decor", "desc", "glue", "tibble", "vctrs"))
7077
if (!missing(file) && !file.exists(file)) {
7178
stop("Can't find `file` at this path:\n", file, "\n", call. = FALSE)
@@ -145,7 +152,7 @@ cpp_source <- function(file, code = NULL, env = parent.frame(), clean = TRUE, qu
145152
brio::write_lines(r_functions, r_path)
146153
source(r_path, local = env)
147154

148-
dyn.load(shared_lib, local = TRUE, now = TRUE)
155+
dyn.load(shared_lib, local = local, now = TRUE)
149156
}
150157

151158
the <- new.env(parent = emptyenv())
@@ -183,7 +190,7 @@ generate_makevars <- function(includes, cxx_std) {
183190

184191
#' @rdname cpp_source
185192
#' @export
186-
cpp_function <- function(code, env = parent.frame(), clean = TRUE, quiet = TRUE, cxx_std = Sys.getenv("CXX_STD", "CXX11")) {
193+
cpp_function <- function(code, env = parent.frame(), clean = TRUE, quiet = TRUE, cxx_std = Sys.getenv("CXX_STD", "CXX11"), local = TRUE) {
187194
cpp_source(code = paste(c('#include "cpp11.hpp"',
188195
"using namespace ::cpp11;",
189196
"namespace writable = ::cpp11::writable;",
@@ -193,15 +200,16 @@ cpp_function <- function(code, env = parent.frame(), clean = TRUE, quiet = TRUE,
193200
env = env,
194201
clean = clean,
195202
quiet = quiet,
196-
cxx_std = cxx_std
203+
cxx_std = cxx_std,
204+
local = local
197205
)
198206
}
199207

200208
utils::globalVariables("f")
201209

202210
#' @rdname cpp_source
203211
#' @export
204-
cpp_eval <- function(code, env = parent.frame(), clean = TRUE, quiet = TRUE, cxx_std = Sys.getenv("CXX_STD", "CXX11")) {
212+
cpp_eval <- function(code, env = parent.frame(), clean = TRUE, quiet = TRUE, cxx_std = Sys.getenv("CXX_STD", "CXX11"), local = TRUE) {
205213
cpp_source(code = paste(c('#include "cpp11.hpp"',
206214
"using namespace ::cpp11;",
207215
"namespace writable = ::cpp11::writable;",
@@ -214,7 +222,8 @@ cpp_eval <- function(code, env = parent.frame(), clean = TRUE, quiet = TRUE, cxx
214222
env = env,
215223
clean = clean,
216224
quiet = quiet,
217-
cxx_std = cxx_std
225+
cxx_std = cxx_std,
226+
local = local
218227
)
219228
f()
220229
}

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,13 @@ Please note that the cpp11 project is released with a [Contributor Code of Condu
7878

7979
cpp11 would not exist without Rcpp.
8080
Thanks to the Rcpp authors, Dirk Eddelbuettel, Romain Francois, JJ Allaire, Kevin Ushey, Qiang Kou, Nathan Russell, Douglas Bates and John Chambers for their work writing and maintaining Rcpp.
81+
82+
## Clang format
83+
84+
To match GHA, use clang-format-12 to format C++ code. With systems that provide clang-format-14 or newer, you can use Docker:
85+
86+
```bash
87+
docker run --rm -v "$PWD":/work -w /work ubuntu:22.04 bash -lc "\
88+
apt-get update && apt-get install -y clang-format-12 && \
89+
find . -name '*.cpp' -o -name '*.hpp' -o -name '*.h' | xargs -r clang-format-12 -i"
90+
```

cpp11test/.Rbuildignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
^\.here$
2+
^bench$
3+
^LICENSE\.md$

cpp11test/DESCRIPTION

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ Authors@R:
1212
Description: Provides a test suite and benchmarking code for the 'cpp11' package.
1313
License: MIT + file LICENSE
1414
Encoding: UTF-8
15-
Imports: Rcpp
16-
LinkingTo: Rcpp, cpp11, testthat
15+
LinkingTo: cpp11, Rcpp, testthat
16+
Imports: cpp11, Rcpp
1717
Suggests:
1818
covr,
19-
testthat,
19+
testthat (>= 3.0.0),
2020
pkgload,
2121
xml2
2222
LazyData: true
2323
Roxygen: list(markdown = TRUE)
2424
RoxygenNote: 7.3.2
25+
Config/testthat/edition: 3

cpp11test/LICENSE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
YEAR: 2025
2+
COPYRIGHT HOLDER: cpp11test authors

cpp11test/LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# MIT License
2+
3+
Copyright (c) 2025 cpp11test authors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

0 commit comments

Comments
 (0)