From 58b920affc987bdd2de40847c813d0eb46231e8d Mon Sep 17 00:00:00 2001 From: Nick Christofides <118103879+NicChr@users.noreply.github.com> Date: Fri, 29 Mar 2024 12:45:38 +0000 Subject: [PATCH] Fix for mac CMD check. --- src/attrs.cpp | 41 +++++++++++++++++++++-------------------- src/cheapr_cpp.h | 2 +- src/utils.cpp | 2 +- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/attrs.cpp b/src/attrs.cpp index 34367cd..7975a3d 100644 --- a/src/attrs.cpp +++ b/src/attrs.cpp @@ -24,43 +24,44 @@ SEXP cpp_set_rm_attributes(SEXP x){ [[cpp11::register]] SEXP cpp_set_add_attr(SEXP x, SEXP which, SEXP value) { int n_protect; - Rf_protect(x = x); - Rf_protect(which = which); - Rf_protect(value = value); - SEXP attr_char = Rf_protect(Rf_install(CHAR(STRING_ELT(which, 0)))); - if (cpp_obj_address(x) == cpp_obj_address(value)){ - Rf_protect(value = Rf_duplicate(value)); + SEXP x2 = Rf_protect(x); + SEXP which2 = Rf_protect(which); + SEXP value2 = Rf_protect(value); + SEXP attr_char = Rf_protect(Rf_install(CHAR(STRING_ELT(which2, 0)))); + if (r_address(x2) == r_address(value2)){ + Rf_protect(value2 = Rf_duplicate(value2)); n_protect = 5; } else { n_protect = 4; } - Rf_setAttrib(x, attr_char, value); + Rf_setAttrib(x2, attr_char, value2); Rf_unprotect(n_protect); - return x; + return x2; } [[cpp11::register]] SEXP cpp_set_rm_attr(SEXP x, SEXP which) { - Rf_protect(x = x); - Rf_protect(which = which); - SEXP attr_char = Rf_protect(Rf_installChar(STRING_ELT(which, 0))); - Rf_setAttrib(x, attr_char, R_NilValue); + SEXP x2 = Rf_protect(x); + SEXP which2 = Rf_protect(which); + SEXP attr_char = Rf_protect(Rf_installChar(STRING_ELT(which2, 0))); + Rf_setAttrib(x2, attr_char, R_NilValue); Rf_unprotect(3); - return x; + return x2; } // Set attributes of x in-place, when add = F, attrs of x are first removed [[cpp11::register]] SEXP cpp_set_attributes(SEXP x, SEXP attributes, bool add) { - int n_protect; - if (add){ - Rf_protect(x = x); - } else { - Rf_protect(x = cpp_set_rm_attributes(x)); + int n_protect = 0; + SEXP x2 = Rf_protect(x); + ++n_protect; + if (!add){ + Rf_protect(x2 = cpp_set_rm_attributes(x2)); + ++n_protect; } SEXP names = Rf_protect(Rf_getAttrib(attributes, R_NamesSymbol)); - n_protect = 2; + ++n_protect; if (!Rf_isVectorList(attributes) || Rf_isNull(names)){ Rf_unprotect(n_protect); Rf_error("attributes must be a named list"); @@ -71,7 +72,7 @@ SEXP cpp_set_attributes(SEXP x, SEXP attributes, bool add) { for (int i = 0; i < n; ++i){ SEXP attr_nm = Rf_protect(Rf_installChar(p_names[i])); ++n_protect; - if (cpp_obj_address(x) == cpp_obj_address(p_attributes[i])){ + if (r_address(x) == r_address(p_attributes[i])){ SEXP dup_attr = Rf_protect(Rf_duplicate(p_attributes[i])); ++n_protect; Rf_setAttrib(x, attr_nm, dup_attr); diff --git a/src/cheapr_cpp.h b/src/cheapr_cpp.h index ef4f548..04d6148 100644 --- a/src/cheapr_cpp.h +++ b/src/cheapr_cpp.h @@ -42,6 +42,6 @@ R_xlen_t cpp_df_nrow(SEXP x); R_xlen_t cpp_unnested_length(SEXP x); SEXP xlen_to_r(R_xlen_t x); R_xlen_t cpp_vec_length(SEXP x); -SEXP cpp_obj_address(SEXP x); +SEXP r_address(SEXP x); #endif diff --git a/src/utils.cpp b/src/utils.cpp index 2876bb2..c00b965 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -170,7 +170,7 @@ SEXP cpp_list_as_df(SEXP x) { } } -SEXP cpp_obj_address(SEXP x) { +SEXP r_address(SEXP x) { static char buf[1000]; snprintf(buf, 1000, "%p", (void*) x); return Rf_mkChar(buf);