Skip to content

Commit 08733bd

Browse files
committed
Fix all rchk warnings
I think all of these are really false positives, but still nice to avoid them.
1 parent 8143817 commit 08733bd

File tree

14 files changed

+93
-80
lines changed

14 files changed

+93
-80
lines changed

cpp11test/R/cpp11.R

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,26 @@ cpp11_safe_ <- function(x_sxp) {
108108
.Call("_cpp11test_cpp11_safe_", x_sxp)
109109
}
110110

111+
sum_int_for_ <- function(x) {
112+
.Call("_cpp11test_sum_int_for_", x)
113+
}
114+
115+
rcpp_sum_dbl_for_ <- function(x_sxp) {
116+
.Call("_cpp11test_rcpp_sum_dbl_for_", x_sxp)
117+
}
118+
119+
rcpp_sum_dbl_foreach_ <- function(x_sxp) {
120+
.Call("_cpp11test_rcpp_sum_dbl_foreach_", x_sxp)
121+
}
122+
123+
rcpp_sum_dbl_accumulate_ <- function(x_sxp) {
124+
.Call("_cpp11test_rcpp_sum_dbl_accumulate_", x_sxp)
125+
}
126+
127+
rcpp_grow_ <- function(n_sxp) {
128+
.Call("_cpp11test_rcpp_grow_", n_sxp)
129+
}
130+
111131
sum_dbl_for_ <- function(x) {
112132
.Call("_cpp11test_sum_dbl_for_", x)
113133
}
@@ -135,23 +155,3 @@ sum_dbl_accumulate_ <- function(x) {
135155
sum_dbl_accumulate2_ <- function(x_sxp) {
136156
.Call("_cpp11test_sum_dbl_accumulate2_", x_sxp)
137157
}
138-
139-
sum_int_for_ <- function(x) {
140-
.Call("_cpp11test_sum_int_for_", x)
141-
}
142-
143-
rcpp_sum_dbl_for_ <- function(x_sxp) {
144-
.Call("_cpp11test_rcpp_sum_dbl_for_", x_sxp)
145-
}
146-
147-
rcpp_sum_dbl_foreach_ <- function(x_sxp) {
148-
.Call("_cpp11test_rcpp_sum_dbl_foreach_", x_sxp)
149-
}
150-
151-
rcpp_sum_dbl_accumulate_ <- function(x_sxp) {
152-
.Call("_cpp11test_rcpp_sum_dbl_accumulate_", x_sxp)
153-
}
154-
155-
rcpp_grow_ <- function(n_sxp) {
156-
.Call("_cpp11test_rcpp_grow_", n_sxp)
157-
}

cpp11test/src/cpp11.cpp

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,41 @@ extern "C" SEXP _cpp11test_cpp11_safe_(SEXP x_sxp) {
205205
return cpp11::as_sexp(cpp11_safe_(cpp11::unmove(cpp11::as_cpp<SEXP>(x_sxp))));
206206
END_CPP11
207207
}
208+
// sum_int.cpp
209+
int sum_int_for_(cpp11::integers x);
210+
extern "C" SEXP _cpp11test_sum_int_for_(SEXP x) {
211+
BEGIN_CPP11
212+
return cpp11::as_sexp(sum_int_for_(cpp11::unmove(cpp11::as_cpp<cpp11::integers>(x))));
213+
END_CPP11
214+
}
215+
// sum_rcpp.cpp
216+
SEXP rcpp_sum_dbl_for_(SEXP x_sxp);
217+
extern "C" SEXP _cpp11test_rcpp_sum_dbl_for_(SEXP x_sxp) {
218+
BEGIN_CPP11
219+
return cpp11::as_sexp(rcpp_sum_dbl_for_(cpp11::unmove(cpp11::as_cpp<SEXP>(x_sxp))));
220+
END_CPP11
221+
}
222+
// sum_rcpp.cpp
223+
SEXP rcpp_sum_dbl_foreach_(SEXP x_sxp);
224+
extern "C" SEXP _cpp11test_rcpp_sum_dbl_foreach_(SEXP x_sxp) {
225+
BEGIN_CPP11
226+
return cpp11::as_sexp(rcpp_sum_dbl_foreach_(cpp11::unmove(cpp11::as_cpp<SEXP>(x_sxp))));
227+
END_CPP11
228+
}
229+
// sum_rcpp.cpp
230+
SEXP rcpp_sum_dbl_accumulate_(SEXP x_sxp);
231+
extern "C" SEXP _cpp11test_rcpp_sum_dbl_accumulate_(SEXP x_sxp) {
232+
BEGIN_CPP11
233+
return cpp11::as_sexp(rcpp_sum_dbl_accumulate_(cpp11::unmove(cpp11::as_cpp<SEXP>(x_sxp))));
234+
END_CPP11
235+
}
236+
// sum_rcpp.cpp
237+
SEXP rcpp_grow_(SEXP n_sxp);
238+
extern "C" SEXP _cpp11test_rcpp_grow_(SEXP n_sxp) {
239+
BEGIN_CPP11
240+
return cpp11::as_sexp(rcpp_grow_(cpp11::unmove(cpp11::as_cpp<SEXP>(n_sxp))));
241+
END_CPP11
242+
}
208243
// sum.cpp
209244
double sum_dbl_for_(cpp11::doubles x);
210245
extern "C" SEXP _cpp11test_sum_dbl_for_(SEXP x) {
@@ -254,41 +289,6 @@ extern "C" SEXP _cpp11test_sum_dbl_accumulate2_(SEXP x_sxp) {
254289
return cpp11::as_sexp(sum_dbl_accumulate2_(cpp11::unmove(cpp11::as_cpp<SEXP>(x_sxp))));
255290
END_CPP11
256291
}
257-
// sum_int.cpp
258-
int sum_int_for_(cpp11::integers x);
259-
extern "C" SEXP _cpp11test_sum_int_for_(SEXP x) {
260-
BEGIN_CPP11
261-
return cpp11::as_sexp(sum_int_for_(cpp11::unmove(cpp11::as_cpp<cpp11::integers>(x))));
262-
END_CPP11
263-
}
264-
// sum_rcpp.cpp
265-
SEXP rcpp_sum_dbl_for_(SEXP x_sxp);
266-
extern "C" SEXP _cpp11test_rcpp_sum_dbl_for_(SEXP x_sxp) {
267-
BEGIN_CPP11
268-
return cpp11::as_sexp(rcpp_sum_dbl_for_(cpp11::unmove(cpp11::as_cpp<SEXP>(x_sxp))));
269-
END_CPP11
270-
}
271-
// sum_rcpp.cpp
272-
SEXP rcpp_sum_dbl_foreach_(SEXP x_sxp);
273-
extern "C" SEXP _cpp11test_rcpp_sum_dbl_foreach_(SEXP x_sxp) {
274-
BEGIN_CPP11
275-
return cpp11::as_sexp(rcpp_sum_dbl_foreach_(cpp11::unmove(cpp11::as_cpp<SEXP>(x_sxp))));
276-
END_CPP11
277-
}
278-
// sum_rcpp.cpp
279-
SEXP rcpp_sum_dbl_accumulate_(SEXP x_sxp);
280-
extern "C" SEXP _cpp11test_rcpp_sum_dbl_accumulate_(SEXP x_sxp) {
281-
BEGIN_CPP11
282-
return cpp11::as_sexp(rcpp_sum_dbl_accumulate_(cpp11::unmove(cpp11::as_cpp<SEXP>(x_sxp))));
283-
END_CPP11
284-
}
285-
// sum_rcpp.cpp
286-
SEXP rcpp_grow_(SEXP n_sxp);
287-
extern "C" SEXP _cpp11test_rcpp_grow_(SEXP n_sxp) {
288-
BEGIN_CPP11
289-
return cpp11::as_sexp(rcpp_grow_(cpp11::unmove(cpp11::as_cpp<SEXP>(n_sxp))));
290-
END_CPP11
291-
}
292292

293293
extern "C" {
294294
/* .Call calls */
@@ -322,13 +322,13 @@ extern SEXP _cpp11test_rcpp_sum_dbl_for_(SEXP);
322322
extern SEXP _cpp11test_rcpp_sum_dbl_foreach_(SEXP);
323323
extern SEXP _cpp11test_remove_altrep(SEXP);
324324
extern SEXP _cpp11test_row_sums(SEXP);
325-
extern SEXP _cpp11test_sum_dbl_accumulate2_(SEXP);
326325
extern SEXP _cpp11test_sum_dbl_accumulate_(SEXP);
326+
extern SEXP _cpp11test_sum_dbl_accumulate2_(SEXP);
327+
extern SEXP _cpp11test_sum_dbl_for_(SEXP);
327328
extern SEXP _cpp11test_sum_dbl_for2_(SEXP);
328329
extern SEXP _cpp11test_sum_dbl_for3_(SEXP);
329-
extern SEXP _cpp11test_sum_dbl_for_(SEXP);
330-
extern SEXP _cpp11test_sum_dbl_foreach2_(SEXP);
331330
extern SEXP _cpp11test_sum_dbl_foreach_(SEXP);
331+
extern SEXP _cpp11test_sum_dbl_foreach2_(SEXP);
332332
extern SEXP _cpp11test_sum_int_for_(SEXP);
333333
extern SEXP _cpp11test_upper_bound(SEXP, SEXP);
334334
extern SEXP run_testthat_tests(SEXP);
@@ -364,13 +364,13 @@ static const R_CallMethodDef CallEntries[] = {
364364
{"_cpp11test_rcpp_sum_dbl_foreach_", (DL_FUNC) &_cpp11test_rcpp_sum_dbl_foreach_, 1},
365365
{"_cpp11test_remove_altrep", (DL_FUNC) &_cpp11test_remove_altrep, 1},
366366
{"_cpp11test_row_sums", (DL_FUNC) &_cpp11test_row_sums, 1},
367-
{"_cpp11test_sum_dbl_accumulate2_", (DL_FUNC) &_cpp11test_sum_dbl_accumulate2_, 1},
368367
{"_cpp11test_sum_dbl_accumulate_", (DL_FUNC) &_cpp11test_sum_dbl_accumulate_, 1},
368+
{"_cpp11test_sum_dbl_accumulate2_", (DL_FUNC) &_cpp11test_sum_dbl_accumulate2_, 1},
369+
{"_cpp11test_sum_dbl_for_", (DL_FUNC) &_cpp11test_sum_dbl_for_, 1},
369370
{"_cpp11test_sum_dbl_for2_", (DL_FUNC) &_cpp11test_sum_dbl_for2_, 1},
370371
{"_cpp11test_sum_dbl_for3_", (DL_FUNC) &_cpp11test_sum_dbl_for3_, 1},
371-
{"_cpp11test_sum_dbl_for_", (DL_FUNC) &_cpp11test_sum_dbl_for_, 1},
372-
{"_cpp11test_sum_dbl_foreach2_", (DL_FUNC) &_cpp11test_sum_dbl_foreach2_, 1},
373372
{"_cpp11test_sum_dbl_foreach_", (DL_FUNC) &_cpp11test_sum_dbl_foreach_, 1},
373+
{"_cpp11test_sum_dbl_foreach2_", (DL_FUNC) &_cpp11test_sum_dbl_foreach2_, 1},
374374
{"_cpp11test_sum_int_for_", (DL_FUNC) &_cpp11test_sum_int_for_, 1},
375375
{"_cpp11test_upper_bound", (DL_FUNC) &_cpp11test_upper_bound, 2},
376376
{"run_testthat_tests", (DL_FUNC) &run_testthat_tests, 1},

cpp11test/src/protect.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
// clang-format on
4747

4848
[[cpp11::register]] void protect_many_(int n) {
49+
#ifdef CPP11_BENCH
4950
std::vector<SEXP> res;
5051
for (R_xlen_t i = 0; i < n; ++i) {
5152
res.push_back(PROTECT(Rf_ScalarInteger(n)));
@@ -56,6 +57,7 @@
5657
UNPROTECT(1);
5758
res.pop_back();
5859
}
60+
#endif
5961
}
6062

6163
[[cpp11::register]] void protect_many_cpp11_(int n) {
@@ -99,14 +101,15 @@
99101
}
100102

101103
[[cpp11::register]] void protect_many_rcpp_(int n) {
104+
#ifdef CPP11_BENCH
102105
std::vector<Rcpp::RObject> res;
103106
for (R_xlen_t i = 0; i < n; ++i) {
104-
SEXP x = Rf_ScalarInteger(n);
105-
res.push_back(x);
107+
res.push_back(Rcpp::RObject(Rf_ScalarInteger(n)));
106108
}
107109

108110
for (R_xlen_t i = n - 1; i >= 0; --i) {
109111
SEXP x = res[i];
110112
res.pop_back();
111113
}
114+
#endif
112115
}

cpp11test/src/release.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@
1818
}
1919

2020
[[cpp11::register]] void rcpp_release_(int n) {
21+
#ifdef CPP11_BENCH
2122
std::vector<Rcpp::RObject> x;
2223
int count = 0;
2324
while (count < n) {
24-
x.push_back(Rf_ScalarInteger(count));
25+
x.push_back(Rcpp::RObject(Rf_ScalarInteger(count)));
2526
++count;
2627
}
2728
count = 0;
2829
while (count < n) {
2930
x.pop_back();
3031
++count;
3132
}
33+
#endif
3234
}

inst/include/cpp11/data_frame.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ class data_frame : public cpp11::data_frame {
6565
}
6666

6767
public:
68-
data_frame(const SEXP& data) : cpp11::data_frame(set_data_frame_attributes(data)) {}
69-
data_frame(const SEXP& data, bool is_altrep)
68+
data_frame(const SEXP data) : cpp11::data_frame(set_data_frame_attributes(data)) {}
69+
data_frame(const SEXP data, bool is_altrep)
7070
: cpp11::data_frame(set_data_frame_attributes(data), is_altrep) {}
7171
data_frame(std::initializer_list<list> il)
7272
: cpp11::data_frame(set_data_frame_attributes(writable::list(il))) {}

inst/include/cpp11/doubles.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ inline r_vector<double>::r_vector(std::initializer_list<named_arg> il)
8080
unwind_protect([&] {
8181
protect_ = protect_sexp(data_);
8282
Rf_setAttrib(data_, R_NamesSymbol, Rf_allocVector(STRSXP, capacity_));
83-
SEXP names(Rf_getAttrib(data_, R_NamesSymbol));
83+
sexp names(Rf_getAttrib(data_, R_NamesSymbol));
8484
auto it = il.begin();
8585
for (R_xlen_t i = 0; i < capacity_; ++i, ++it) {
8686
data_p_[i] = doubles(it->value())[0];

inst/include/cpp11/environment.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ class environment {
5050
bool exists(const std::string& name) const { return exists(name.c_str()); }
5151

5252
void remove(SEXP name) {
53+
PROTECT(name);
5354
#ifdef HAS_REMOVE_VAR_FROM_FRAME
5455
R_removeVarFromFrame(name, env_);
5556
#else
5657
auto remove = package("base")["remove"];
5758
remove(name, "envir"_nm = env_);
5859
#endif
60+
UNPROTECT(1);
5961
}
6062

6163
void remove(const char* name) { remove(safe[Rf_install](name)); }

inst/include/cpp11/integers.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ inline r_vector<int>::r_vector(std::initializer_list<named_arg> il)
9696
unwind_protect([&] {
9797
protect_ = protect_sexp(data_);
9898
attr("names") = Rf_allocVector(STRSXP, capacity_);
99-
SEXP names = attr("names");
99+
sexp names(attr("names"));
100100
auto it = il.begin();
101101
for (R_xlen_t i = 0; i < capacity_; ++i, ++it) {
102102
data_p_[i] = integers(it->value())[0];

inst/include/cpp11/logicals.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ inline r_vector<Rboolean>::r_vector(std::initializer_list<named_arg> il)
8484
unwind_protect([&] {
8585
protect_ = protect_sexp(data_);
8686
attr("names") = Rf_allocVector(STRSXP, capacity_);
87-
SEXP names = attr("names");
87+
sexp names(attr("names"));
8888
auto it = il.begin();
8989
for (R_xlen_t i = 0; i < capacity_; ++i, ++it) {
9090
data_p_[i] = logicals(it->value())[0];

inst/include/cpp11/protect.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class unwind_exception : public std::exception {
2222
};
2323

2424
static SEXP preserve(SEXP obj) {
25+
PROTECT(obj);
2526
R_PreserveObject(obj);
27+
UNPROTECT(1);
2628
return obj;
2729
}
2830

inst/include/cpp11/r_string.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class r_string {
3030

3131
bool operator==(const r_string& rhs) const { return data_.data() == rhs.data_.data(); }
3232

33-
bool operator==(const SEXP& rhs) const { return data_.data() == rhs; }
33+
bool operator==(const SEXP rhs) const { return data_.data() == rhs; }
3434

3535
bool operator==(const char* rhs) const {
3636
return static_cast<std::string>(*this) == rhs;

inst/include/cpp11/r_vector.hpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ class r_vector {
5656

5757
r_vector() = default;
5858

59-
r_vector(const SEXP data);
59+
r_vector(SEXP data);
6060

61-
r_vector(const SEXP data, bool is_altrep);
61+
r_vector(SEXP data, bool is_altrep);
6262

6363
#ifdef LONG_VECTOR_SUPPORT
6464
const T operator[](const int pos) const;
@@ -356,15 +356,15 @@ class r_vector : public cpp11::r_vector<T> {
356356
template <typename T>
357357
inline r_vector<T>::r_vector(const SEXP data)
358358
: data_(valid_type(data)),
359-
protect_(protect_sexp(data_)),
359+
protect_(protect_sexp(data)),
360360
is_altrep_(ALTREP(data)),
361361
data_p_(get_p(ALTREP(data), data)),
362362
length_(Rf_xlength(data)) {}
363363

364364
template <typename T>
365365
inline r_vector<T>::r_vector(const SEXP data, bool is_altrep)
366366
: data_(valid_type(data)),
367-
protect_(protect_sexp(data_)),
367+
protect_(protect_sexp(data)),
368368
is_altrep_(is_altrep),
369369
data_p_(get_p(is_altrep, data)),
370370
length_(Rf_xlength(data)) {}
@@ -718,16 +718,18 @@ inline typename r_vector<T>::proxy r_vector<T>::at(size_type pos) const {
718718

719719
template <typename T>
720720
inline typename r_vector<T>::proxy r_vector<T>::operator[](const r_string& name) const {
721-
SEXP names = this->names();
721+
SEXP names = PROTECT(this->names());
722722
R_xlen_t size = Rf_xlength(names);
723723

724724
for (R_xlen_t pos = 0; pos < size; ++pos) {
725725
auto cur = Rf_translateCharUTF8(STRING_ELT(names, pos));
726726
if (name == cur) {
727+
UNPROTECT(1);
727728
return operator[](pos);
728729
}
729730
}
730731

732+
UNPROTECT(1);
731733
throw std::out_of_range("r_vector");
732734
}
733735

@@ -738,16 +740,18 @@ inline typename r_vector<T>::proxy r_vector<T>::at(const r_string& name) const {
738740

739741
template <typename T>
740742
inline typename r_vector<T>::iterator r_vector<T>::find(const r_string& name) const {
741-
SEXP names = this->names();
743+
SEXP names = PROTECT(this->names());
742744
R_xlen_t size = Rf_xlength(names);
743745

744746
for (R_xlen_t pos = 0; pos < size; ++pos) {
745747
auto cur = Rf_translateCharUTF8(STRING_ELT(names, pos));
746748
if (name == cur) {
749+
UNPROTECT(1);
747750
return begin() + pos;
748751
}
749752
}
750753

754+
UNPROTECT(1);
751755
return end();
752756
}
753757

inst/include/cpp11/raws.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ inline r_vector<uint8_t>::r_vector(std::initializer_list<named_arg> il)
9191
unwind_protect([&] {
9292
protect_ = protect_sexp(data_);
9393
attr("names") = Rf_allocVector(STRSXP, capacity_);
94-
SEXP names = attr("names");
94+
sexp names(attr("names"));
9595
auto it = il.begin();
9696
for (R_xlen_t i = 0; i < capacity_; ++i, ++it) {
9797
data_p_[i] = cpp11::raws(it->value())[0];

inst/include/cpp11/strings.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ inline bool operator==(const r_vector<r_string>::proxy& lhs, r_string rhs) {
6464
return static_cast<r_string>(lhs).operator==(static_cast<std::string>(rhs).c_str());
6565
}
6666

67-
inline SEXP alloc_or_copy(const SEXP& data) {
67+
inline SEXP alloc_or_copy(const SEXP data) {
6868
switch (TYPEOF(data)) {
6969
case CHARSXP:
7070
return cpp11::r_vector<r_string>(safe[Rf_allocVector](STRSXP, 1));
@@ -75,7 +75,7 @@ inline SEXP alloc_or_copy(const SEXP& data) {
7575
}
7676
}
7777

78-
inline SEXP alloc_if_charsxp(const SEXP& data) {
78+
inline SEXP alloc_if_charsxp(const SEXP data) {
7979
switch (TYPEOF(data)) {
8080
case CHARSXP:
8181
return cpp11::r_vector<r_string>(safe[Rf_allocVector](STRSXP, 1));
@@ -126,7 +126,7 @@ inline r_vector<r_string>::r_vector(std::initializer_list<named_arg> il)
126126
unwind_protect([&] {
127127
protect_ = protect_sexp(data_);
128128
attr("names") = Rf_allocVector(STRSXP, capacity_);
129-
SEXP names = attr("names");
129+
sexp names(attr("names"));
130130
auto it = il.begin();
131131
for (R_xlen_t i = 0; i < capacity_; ++i, ++it) {
132132
SET_STRING_ELT(data_, i, strings(it->value())[0]);

0 commit comments

Comments
 (0)