Skip to content

Commit df33639

Browse files
authored
Avoid -Wcast-function-type warnings (#1502)
* add -Wcast-function-type * use intermediate cast to avoid warnings * update Changelog
1 parent 8d5f0f5 commit df33639

9 files changed

Lines changed: 35 additions & 17 deletions

File tree

.github/workflows/werror.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88

99
env:
1010
_R_CHECK_FORCE_SUGGESTS_: "false"
11-
RCPP_CXXFLAGS: "-Werror"
11+
RCPP_CXXFLAGS: "-Werror -Wcast-function-type"
1212

1313
jobs:
1414
ci:

ChangeLog

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
2026-09-03 Iñaki Ucar <iucar@fedoraproject.org>
2+
3+
* .github/workflows/werror.yaml: Add -Wcast-function-type
4+
5+
* inst/include/Rcpp/routines.h: New function pointer RCPP_FUNC typedef used
6+
as an intermediate cast target to avoid gcc's -Wcast-function-type warnings
7+
* inst/include/Rcpp/Module.h: Idem
8+
* src/rcpp_init.cpp: Idem
9+
10+
* src/attributes.cpp: Generate RCPP_FUNC-routed casts in interface code
11+
* inst/examples/ConvolveBenchmarks/overhead_1.cpp: Update generated code
12+
* inst/tinytest/testRcppInterfaceExporter/src/RcppExports.cpp: Idem
13+
* inst/tinytest/testRcppInterfaceExporter/inst/include/testRcppInterfaceExporter_RcppExports.h: Idem
14+
115
2026-09-01 Iñaki Ucar <iucar@fedoraproject.org>
216

317
* .github/workflows/werror.yaml: Add new CI file with -Werror enabled

inst/examples/ConvolveBenchmarks/overhead_1.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ SEXP overhead_cpp(SEXP a, SEXP b) {
1212
extern "C" void R_init_overhead_1(DllInfo *info){
1313

1414
R_CallMethodDef callMethods[] = {
15-
{"overhead_cpp", (DL_FUNC) &overhead_cpp, 2},
15+
{"overhead_cpp", (DL_FUNC) (RCPP_FUNC) &overhead_cpp, 2},
1616
{NULL, NULL, 0}
1717
};
1818

inst/include/Rcpp/Module.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ namespace Rcpp {
113113
inline int nargs() { return sizeof...(T); }
114114
inline bool is_void() { return std::is_void<RESULT_TYPE>::value; }
115115
inline void signature(std::string& s, const char* name) { Rcpp::signature<RESULT_TYPE, T...>(s, name); }
116-
inline DL_FUNC get_function_ptr() { return (DL_FUNC)ptr_fun; }
116+
inline DL_FUNC get_function_ptr() { return (DL_FUNC)(RCPP_FUNC)ptr_fun; }
117117

118118
private:
119119
RESULT_TYPE (*ptr_fun)(T...);

inst/include/Rcpp/routines.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525

2626
#include <Rcpp/iostream/Rstreambuf.h>
2727

28+
// Necessary to cast a function pointer to a seemingly incompatible function
29+
// pointer type while avoiding gcc's -Wcast-function-type warnings.
30+
typedef void (*RCPP_FUNC)(void);
31+
2832
#if defined(COMPILING_RCPP)
2933

3034
// the idea is that this file should be generated automatically by Rcpp::register
@@ -79,7 +83,7 @@ SEXP rcpp_get_current_error();
7983

8084
namespace Rcpp {
8185

82-
#define GET_CALLABLE(__FUN__) (Fun) R_GetCCallable( "Rcpp", __FUN__ )
86+
#define GET_CALLABLE(__FUN__) (Fun) (RCPP_FUNC) R_GetCCallable( "Rcpp", __FUN__ )
8387

8488
inline attribute_hidden const char* type2name(SEXP x){
8589
typedef const char* (*Fun)(SEXP);

inst/tinytest/testRcppInterfaceExporter/inst/include/testRcppInterfaceExporter_RcppExports.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace testRcppInterfaceExporter {
1515
Rcpp::Function require = Rcpp::Environment::base_env()["require"];
1616
require("testRcppInterfaceExporter", Rcpp::Named("quietly") = true);
1717
typedef int(*Ptr_validate)(const char*);
18-
static Ptr_validate p_validate = (Ptr_validate)
18+
static Ptr_validate p_validate = (Ptr_validate) (RCPP_FUNC)
1919
R_GetCCallable("testRcppInterfaceExporter", "_testRcppInterfaceExporter_RcppExport_validate");
2020
if (!p_validate(sig)) {
2121
throw Rcpp::function_not_exported(
@@ -29,7 +29,7 @@ namespace testRcppInterfaceExporter {
2929
static Ptr_test_cpp_interface p_test_cpp_interface = NULL;
3030
if (p_test_cpp_interface == NULL) {
3131
validateSignature("SEXP(*test_cpp_interface)(SEXP,bool)");
32-
p_test_cpp_interface = (Ptr_test_cpp_interface)R_GetCCallable("testRcppInterfaceExporter", "_testRcppInterfaceExporter_test_cpp_interface");
32+
p_test_cpp_interface = (Ptr_test_cpp_interface) (RCPP_FUNC)R_GetCCallable("testRcppInterfaceExporter", "_testRcppInterfaceExporter_test_cpp_interface");
3333
}
3434
RObject rcpp_result_gen;
3535
{

inst/tinytest/testRcppInterfaceExporter/src/RcppExports.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ static int _testRcppInterfaceExporter_RcppExport_validate(const char* sig) {
6060

6161
// registerCCallable (register entry points for exported C++ functions)
6262
RcppExport SEXP _testRcppInterfaceExporter_RcppExport_registerCCallable() {
63-
R_RegisterCCallable("testRcppInterfaceExporter", "_testRcppInterfaceExporter_test_cpp_interface", (DL_FUNC)_testRcppInterfaceExporter_test_cpp_interface_try);
64-
R_RegisterCCallable("testRcppInterfaceExporter", "_testRcppInterfaceExporter_RcppExport_validate", (DL_FUNC)_testRcppInterfaceExporter_RcppExport_validate);
63+
R_RegisterCCallable("testRcppInterfaceExporter", "_testRcppInterfaceExporter_test_cpp_interface", (DL_FUNC) (RCPP_FUNC) _testRcppInterfaceExporter_test_cpp_interface_try);
64+
R_RegisterCCallable("testRcppInterfaceExporter", "_testRcppInterfaceExporter_RcppExport_validate", (DL_FUNC) (RCPP_FUNC) _testRcppInterfaceExporter_RcppExport_validate);
6565
return R_NilValue;
6666
}
6767

6868
static const R_CallMethodDef CallEntries[] = {
69-
{"_testRcppInterfaceExporter_test_cpp_interface", (DL_FUNC) &_testRcppInterfaceExporter_test_cpp_interface, 2},
70-
{"_testRcppInterfaceExporter_RcppExport_registerCCallable", (DL_FUNC) &_testRcppInterfaceExporter_RcppExport_registerCCallable, 0},
69+
{"_testRcppInterfaceExporter_test_cpp_interface", (DL_FUNC) (RCPP_FUNC) &_testRcppInterfaceExporter_test_cpp_interface, 2},
70+
{"_testRcppInterfaceExporter_RcppExport_registerCCallable", (DL_FUNC) (RCPP_FUNC) &_testRcppInterfaceExporter_RcppExport_registerCCallable, 0},
7171
{NULL, NULL, 0}
7272
};
7373

src/attributes.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,7 +2103,7 @@ namespace attributes {
21032103
ostr() << "static const R_CallMethodDef CallEntries[] = {" << std::endl;
21042104
for (std::size_t i=0;i<routineNames.size(); i++) {
21052105
ostr() << " {\"" << routineNames[i] << "\", " <<
2106-
"(DL_FUNC) &" << routineNames[i] << ", " <<
2106+
"(DL_FUNC) (RCPP_FUNC) &" << routineNames[i] << ", " <<
21072107
routineArgs[i] << "}," << std::endl;
21082108
}
21092109
if (callEntries.size() > 0) {
@@ -2148,7 +2148,7 @@ namespace attributes {
21482148
std::string indentStr(indent, ' ');
21492149
ostr << indentStr << "R_RegisterCCallable(\"" << package() << "\", "
21502150
<< "\"" << packageCppPrefix() << "_" << exportedName << "\", "
2151-
<< "(DL_FUNC)" << packageCppPrefix() << "_" << name << ");";
2151+
<< "(DL_FUNC) (RCPP_FUNC) " << packageCppPrefix() << "_" << name << ");";
21522152
return ostr.str(); // #nocov end
21532153
}
21542154

@@ -2221,7 +2221,7 @@ namespace attributes {
22212221

22222222
std::string ptrName = "p_" + validate;
22232223
ostr() << " static " << fnType << " " << ptrName << " = "
2224-
<< "(" << fnType << ")" << std::endl
2224+
<< "(" << fnType << ") (RCPP_FUNC)" << std::endl
22252225
<< " "
22262226
<< getCCallable(exportValidationFunctionRegisteredName())
22272227
<< ";" << std::endl;
@@ -2279,7 +2279,7 @@ namespace attributes {
22792279
<< "(\"" << function.signature() << "\");"
22802280
<< std::endl;
22812281
ostr() << " " << ptrName << " = "
2282-
<< "(" << fnType << ")"
2282+
<< "(" << fnType << ") (RCPP_FUNC)"
22832283
<< getCCallable(packageCppPrefix() + "_" + function.name()) << ";"
22842284
<< std::endl;
22852285
ostr() << " }" << std::endl;

src/rcpp_init.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
#include "internal.h"
2626

2727
// borrowed from Matrix
28-
#define CALLDEF(name, n) {#name, (DL_FUNC) &name, n}
29-
#define EXTDEF(name) {#name, (DL_FUNC) &name, -1}
28+
#define CALLDEF(name, n) {#name, (DL_FUNC) (RCPP_FUNC) &name, n}
29+
#define EXTDEF(name) {#name, (DL_FUNC) (RCPP_FUNC) &name, -1}
3030

3131
static R_CallMethodDef callEntries[] = {
3232
CALLDEF(Class__name,1),
@@ -89,7 +89,7 @@ void registerFunctions(){
8989
using namespace Rcpp;
9090
using namespace Rcpp::internal;
9191

92-
#define RCPP_REGISTER(__FUN__) R_RegisterCCallable( "Rcpp", #__FUN__ , (DL_FUNC)__FUN__ );
92+
#define RCPP_REGISTER(__FUN__) R_RegisterCCallable( "Rcpp", #__FUN__ , (DL_FUNC) (RCPP_FUNC) __FUN__ );
9393
RCPP_REGISTER(rcpp_get_stack_trace)
9494
RCPP_REGISTER(rcpp_set_stack_trace)
9595
RCPP_REGISTER(type2name)

0 commit comments

Comments
 (0)