Skip to content

Commit

Permalink
Fix missing-braces issue for Rcomplex (#135)
Browse files Browse the repository at this point in the history
* Fix missing-braces issue for Rcomplex

* inline function instead

* 4.3.0, not 4.4.0

* inline, also account for R_LEGACY_RCOMPLEX

* whitespace

* Build object component-wise instead of in-line

* No pointers, just copy

* Update code comment in src/period.cpp

Co-authored-by: Michael Chirico <chiricom@google.com>

---------

Co-authored-by: Dirk Eddelbuettel <edd@debian.org>
  • Loading branch information
MichaelChirico and eddelbuettel authored Oct 31, 2024
1 parent 48d48a7 commit 07f677a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2024-10-30 Michael Chirico <chiricom@google.com>

* src/period.cpp: Create `Rcomplex` objects in a more robust
way that appeases `Wmissing-braces` compiler warnings on `clang`.

2024-09-16 Dirk Eddelbuettel <edd@debian.org>

* DESCRIPTION (Version, Date): Release 0.3.10
Expand Down
27 changes: 19 additions & 8 deletions src/period.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
#include "nanotime/pseudovector.hpp"
#include "nanotime/utilities.hpp"

// From R>=4.3.0 the typedef of Rcomplex changed. Some modern strict
// compilers (e.g. 2024 clang -Wmissing-braces) get tripped up by
// 'Rcomplex{re, im}' vs. 'Rcomplex{{re, im}}', the latter being technically
// more correct for the new typedef, though for many compilers this will
// "just work". See #134 and the comments in R_Ext/Complex.h.
inline Rcomplex makeComplex(double re, double im) {
Rcomplex ret;
ret.r = re;
ret.i = im;
return ret;
}

using namespace nanotime;

Expand Down Expand Up @@ -185,7 +196,7 @@ Rcpp::ComplexVector period_from_string_impl(Rcpp::CharacterVector str) {
for (R_xlen_t i=0; i<str.size(); ++i) {
period prd(Rcpp::as<std::string>(str[i]));
period_union pu = { { prd.getMonths(), prd.getDays(), prd.getDuration().count() } };
res[i] = Rcomplex{pu.dbl2.d1, pu.dbl2.d2 };
res[i] = makeComplex(pu.dbl2.d1, pu.dbl2.d2 );
}
if (str.hasAttribute("names")) {
res.names() = str.names();
Expand All @@ -206,7 +217,7 @@ Rcpp::ComplexVector period_from_parts_impl(Rcpp::IntegerVector months_v, Rcpp::I
for (R_xlen_t i=0; i<res.size(); ++i) {
const auto dur_i = *reinterpret_cast<const int64_t*>(&dur[i]);
period_union pu = { { months[i], days[i], dur_i } };
res[i] = Rcomplex{pu.dbl2.d1, pu.dbl2.d2 };
res[i] = makeComplex(pu.dbl2.d1, pu.dbl2.d2 );
}
}
return assignS4("nanoperiod", res);
Expand Down Expand Up @@ -249,11 +260,11 @@ Rcpp::ComplexVector period_from_integer64_impl(Rcpp::NumericVector i64) {
auto elt = *reinterpret_cast<std::int64_t*>(&i64[i]);
if (elt == NA_INTEGER64) {
period_union pu = { { NA_INTEGER, NA_INTEGER, NA_INTEGER64 } };
res[i] = Rcomplex{pu.dbl2.d1, pu.dbl2.d2 };
res[i] = makeComplex(pu.dbl2.d1, pu.dbl2.d2 );
}
else {
period_union pu = { { 0, 0, elt } };
res[i] = Rcomplex{pu.dbl2.d1, pu.dbl2.d2 };
res[i] = makeComplex(pu.dbl2.d1, pu.dbl2.d2 );
}
}
if (i64.hasAttribute("names")) {
Expand All @@ -269,11 +280,11 @@ Rcpp::ComplexVector period_from_integer_impl(Rcpp::IntegerVector iint) {
for (R_xlen_t i=0; i<iint.size(); ++i) {
if (iint[i] == NA_INTEGER) {
period_union pu = { { NA_INTEGER, NA_INTEGER, NA_INTEGER64 } };
res[i] = Rcomplex{pu.dbl2.d1, pu.dbl2.d2 };
res[i] = makeComplex(pu.dbl2.d1, pu.dbl2.d2 );
}
else {
period_union pu = { { 0, 0, static_cast<std::int64_t>(iint[i]) } };
res[i] = Rcomplex{pu.dbl2.d1, pu.dbl2.d2 };
res[i] = makeComplex(pu.dbl2.d1, pu.dbl2.d2 );
}
}
if (iint.hasAttribute("names")) {
Expand All @@ -289,11 +300,11 @@ Rcpp::ComplexVector period_from_double_impl(Rcpp::NumericVector dbl) {
for (R_xlen_t i=0; i<dbl.size(); ++i) {
if (ISNA(dbl[i])) {
period_union pu = { { NA_INTEGER, NA_INTEGER, NA_INTEGER64 } };
res[i] = Rcomplex{pu.dbl2.d1, pu.dbl2.d2 };
res[i] = makeComplex(pu.dbl2.d1, pu.dbl2.d2 );
}
else {
period_union pu = { { 0, 0, static_cast<std::int64_t>(dbl[i]) } };
res[i] = Rcomplex{pu.dbl2.d1, pu.dbl2.d2 };
res[i] = makeComplex(pu.dbl2.d1, pu.dbl2.d2 );
}
}
if (dbl.hasAttribute("names")) {
Expand Down

0 comments on commit 07f677a

Please sign in to comment.