Skip to content

Commit c655b6e

Browse files
authored
Merge pull request #1067 from RcppCore/bugfix/short-file-name
define safe variant of short_file_name
2 parents c9cf6aa + cce9e61 commit c655b6e

File tree

5 files changed

+53
-23
lines changed

5 files changed

+53
-23
lines changed

ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2020-04-10 Kevin Ushey <[email protected]>
2+
3+
* inst/include/Rcpp/macros/macros.h: Safer definition of short_file_name
4+
* inst/include/Rcpp/String.h: Idem
5+
* inst/include/Rcpp/macros/debug.h: Idem
6+
* src/api.cpp: Idem
7+
18
2020-04-10 Dirk Eddelbuettel <[email protected]>
29

310
* inst/tinytest/test_exceptions.R: Skip parts of file if on Solaris

inst/include/Rcpp/String.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929

3030
#if RCPP_STRING_DEBUG_LEVEL > 0
3131
#define RCPP_STRING_DEBUG_FORMAT "%40s:%4d "
32-
#define RCPP_STRING_DEBUG(MSG) Rprintf(RCPP_STRING_DEBUG_FORMAT "%s\n" , short_file_name(__FILE__), __LINE__, MSG);
33-
#define RCPP_STRING_DEBUG_1(fmt, MSG) Rprintf(RCPP_STRING_DEBUG_FORMAT fmt "\n" , short_file_name(__FILE__), __LINE__, MSG);
34-
#define RCPP_STRING_DEBUG_2(fmt, M1, M2) Rprintf(RCPP_STRING_DEBUG_FORMAT fmt "\n" , short_file_name(__FILE__), __LINE__, M1, M2);
35-
#define RCPP_STRING_DEBUG_3(fmt, M1, M2, M3) Rprintf(RCPP_STRING_DEBUG_FORMAT fmt "\n" , short_file_name(__FILE__), __LINE__, M1, M2, M3);
32+
#define RCPP_STRING_DEBUG(MSG) Rprintf(RCPP_STRING_DEBUG_FORMAT "%s\n" , ::Rcpp::debug::short_file_name(__FILE__).c_str(), __LINE__, MSG);
33+
#define RCPP_STRING_DEBUG_1(fmt, MSG) Rprintf(RCPP_STRING_DEBUG_FORMAT fmt "\n" , ::Rcpp::debug::short_file_name(__FILE__).c_str(), __LINE__, MSG);
34+
#define RCPP_STRING_DEBUG_2(fmt, M1, M2) Rprintf(RCPP_STRING_DEBUG_FORMAT fmt "\n" , ::Rcpp::debug::short_file_name(__FILE__).c_str(), __LINE__, M1, M2);
35+
#define RCPP_STRING_DEBUG_3(fmt, M1, M2, M3) Rprintf(RCPP_STRING_DEBUG_FORMAT fmt "\n" , ::Rcpp::debug::short_file_name(__FILE__).c_str(), __LINE__, M1, M2, M3);
3636
#else
3737
#define RCPP_STRING_DEBUG(MSG)
3838
#define RCPP_STRING_DEBUG_1(fmt, MSG)

inst/include/Rcpp/macros/debug.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333

3434

3535
#if RCPP_DEBUG_LEVEL > 0
36-
#define RCPP_DEBUG( MSG ) Rprintf( "%40s:%4d %s\n" , short_file_name(__FILE__), __LINE__, MSG ) ;
37-
#define RCPP_DEBUG_1( fmt, MSG ) Rprintf( "%40s:%4d " fmt "\n" , short_file_name(__FILE__), __LINE__, MSG ) ;
38-
#define RCPP_DEBUG_2( fmt, M1, M2 ) Rprintf( "%40s:%4d " fmt "\n" , short_file_name(__FILE__), __LINE__, M1, M2 ) ;
39-
#define RCPP_DEBUG_3( fmt, M1, M2, M3 ) Rprintf( "%40s:%4d " fmt "\n" , short_file_name(__FILE__), __LINE__, M1, M2, M3) ;
40-
#define RCPP_DEBUG_4( fmt, M1, M2, M3, M4 ) Rprintf( "%40s:%4d " fmt "\n" , short_file_name(__FILE__), __LINE__, M1, M2, M3, M4) ;
41-
#define RCPP_DEBUG_5( fmt, M1, M2, M3, M4, M5 ) Rprintf( "%40s:%4d " fmt "\n" , short_file_name(__FILE__), __LINE__, M1, M2, M3, M4, M5) ;
36+
#define RCPP_DEBUG( MSG ) Rprintf( "%40s:%4d %s\n" , ::Rcpp::debug::short_file_name(__FILE__).c_str(), __LINE__, MSG ) ;
37+
#define RCPP_DEBUG_1( fmt, MSG ) Rprintf( "%40s:%4d " fmt "\n" , ::Rcpp::debug::short_file_name(__FILE__).c_str(), __LINE__, MSG ) ;
38+
#define RCPP_DEBUG_2( fmt, M1, M2 ) Rprintf( "%40s:%4d " fmt "\n" , ::Rcpp::debug::short_file_name(__FILE__).c_str(), __LINE__, M1, M2 ) ;
39+
#define RCPP_DEBUG_3( fmt, M1, M2, M3 ) Rprintf( "%40s:%4d " fmt "\n" , ::Rcpp::debug::short_file_name(__FILE__).c_str(), __LINE__, M1, M2, M3) ;
40+
#define RCPP_DEBUG_4( fmt, M1, M2, M3, M4 ) Rprintf( "%40s:%4d " fmt "\n" , ::Rcpp::debug::short_file_name(__FILE__).c_str(), __LINE__, M1, M2, M3, M4) ;
41+
#define RCPP_DEBUG_5( fmt, M1, M2, M3, M4, M5 ) Rprintf( "%40s:%4d " fmt "\n" , ::Rcpp::debug::short_file_name(__FILE__).c_str(), __LINE__, M1, M2, M3, M4, M5) ;
4242
#else
4343
#define RCPP_DEBUG( MSG )
4444
#define RCPP_DEBUG_1( fmt, MSG )
@@ -52,49 +52,49 @@
5252
#define RCPP_DEBUG_MODULE( MSG ) { \
5353
Rcpp::Module * mod__ = getCurrentScope() ; \
5454
if( mod__ ){ \
55-
Rprintf( "[module (%s) <%p> ] %40s:%4d %s\n" , mod__->name.c_str(), mod__, short_file_name(__FILE__), __LINE__, MSG ) ;\
55+
Rprintf( "[module (%s) <%p> ] %40s:%4d %s\n" , mod__->name.c_str(), mod__, ::Rcpp::debug::short_file_name(__FILE__).c_str(), __LINE__, MSG ) ;\
5656
} else { \
57-
Rprintf( "[module () ] %40s:%4d %s\n" , short_file_name(__FILE__), __LINE__, MSG ) ; \
57+
Rprintf( "[module () ] %40s:%4d %s\n" , ::Rcpp::debug::short_file_name(__FILE__).c_str(), __LINE__, MSG ) ; \
5858
} \
5959
}
6060
#define RCPP_DEBUG_MODULE_1( fmt, MSG ) { \
6161
Rcpp::Module * mod__ = getCurrentScope() ; \
6262
if( mod__ ){ \
63-
Rprintf( "[module (%s) <%p> ] %40s:%4d " fmt "\n" , mod__->name.c_str(), mod__, short_file_name(__FILE__), __LINE__, MSG ) ;\
63+
Rprintf( "[module (%s) <%p> ] %40s:%4d " fmt "\n" , mod__->name.c_str(), mod__, ::Rcpp::debug::short_file_name(__FILE__).c_str(), __LINE__, MSG ) ;\
6464
} else { \
65-
Rprintf( "[module () ] %40s:%4d " fmt "\n" , short_file_name(__FILE__), __LINE__, MSG ) ; \
65+
Rprintf( "[module () ] %40s:%4d " fmt "\n" , ::Rcpp::debug::short_file_name(__FILE__).c_str(), __LINE__, MSG ) ; \
6666
} \
6767
}
6868
#define RCPP_DEBUG_MODULE_2( fmt, M1, M2 ) { \
6969
Rcpp::Module * mod__ = getCurrentScope() ; \
7070
if( mod__ ){ \
71-
Rprintf( "[module (%s) <%p> ] %40s:%4d " fmt "\n" , mod__->name.c_str(), mod__, short_file_name(__FILE__), __LINE__, M1, M2 ) ;\
71+
Rprintf( "[module (%s) <%p> ] %40s:%4d " fmt "\n" , mod__->name.c_str(), mod__, ::Rcpp::debug::short_file_name(__FILE__).c_str(), __LINE__, M1, M2 ) ;\
7272
} else { \
73-
Rprintf( "[module () ] %40s:%4d " fmt "\n" , short_file_name(__FILE__), __LINE__, M1, M2 ) ; \
73+
Rprintf( "[module () ] %40s:%4d " fmt "\n" , ::Rcpp::debug::short_file_name(__FILE__).c_str(), __LINE__, M1, M2 ) ; \
7474
} \
7575
}
7676
#define RCPP_DEBUG_MODULE_3( fmt, M1, M2, M3 ) { \
7777
Rcpp::Module * mod__ = getCurrentScope() ; \
7878
if( mod__ ){ \
79-
Rprintf( "[module (%s) <%p> ] %40s:%4d " fmt "\n" , mod__->name.c_str(), mod__, short_file_name(__FILE__), __LINE__, M1, M2, M3 ) ;\
79+
Rprintf( "[module (%s) <%p> ] %40s:%4d " fmt "\n" , mod__->name.c_str(), mod__, ::Rcpp::debug::short_file_name(__FILE__).c_str(), __LINE__, M1, M2, M3 ) ;\
8080
} else { \
81-
Rprintf( "[module () ] %40s:%4d " fmt "\n" , short_file_name(__FILE__), __LINE__, M1, M2, M3 ) ; \
81+
Rprintf( "[module () ] %40s:%4d " fmt "\n" , ::Rcpp::debug::short_file_name(__FILE__).c_str(), __LINE__, M1, M2, M3 ) ; \
8282
} \
8383
}
8484
#define RCPP_DEBUG_MODULE_4( fmt, M1, M2, M3, M4 ) { \
8585
Rcpp::Module * mod__ = getCurrentScope() ; \
8686
if( mod__ ) { \
87-
Rprintf( "[module (%s) <%p> ] %40s:%4d " fmt "\n" , mod__->name.c_str(), mod__, short_file_name(__FILE__), __LINE__, M1, M2, M3, M4 ) ;\
87+
Rprintf( "[module (%s) <%p> ] %40s:%4d " fmt "\n" , mod__->name.c_str(), mod__, ::Rcpp::debug::short_file_name(__FILE__).c_str(), __LINE__, M1, M2, M3, M4 ) ;\
8888
} else { \
89-
Rprintf( "[module () ] %40s:%4d " fmt "\n" , short_file_name(__FILE__), __LINE__, M1, M2, M3, M4 ) ; \
89+
Rprintf( "[module () ] %40s:%4d " fmt "\n" , ::Rcpp::debug::short_file_name(__FILE__).c_str(), __LINE__, M1, M2, M3, M4 ) ; \
9090
} \
9191
}
9292
#define RCPP_DEBUG_MODULE_5( fmt, M1, M2, M3, M4, M5 ) { \
9393
Rcpp::Module * mod__ = getCurrentScope() ; \
9494
if( mod__ ){ \
95-
Rprintf( "[module (%s) <%p> ] %40s:%4d " fmt "\n" , mod__->name.c_str(), mod__, short_file_name(__FILE__), __LINE__, M1, M2, M3, M4, M5 ) ;\
95+
Rprintf( "[module (%s) <%p> ] %40s:%4d " fmt "\n" , mod__->name.c_str(), mod__, ::Rcpp::debug::short_file_name(__FILE__).c_str(), __LINE__, M1, M2, M3, M4, M5 ) ;\
9696
} else { \
97-
Rprintf( "[module () ] %40s:%4d " fmt "\n" , short_file_name(__FILE__), __LINE__, M1, M2, M3, M4, M5 ) ; \
97+
Rprintf( "[module () ] %40s:%4d " fmt "\n" , ::Rcpp::debug::short_file_name(__FILE__).c_str(), __LINE__, M1, M2, M3, M4, M5 ) ; \
9898
} \
9999
}
100100
#else

inst/include/Rcpp/macros/macros.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@
2222
#ifndef Rcpp_macros_macros_h
2323
#define Rcpp_macros_macros_h
2424

25+
#include <string>
26+
27+
namespace Rcpp {
28+
namespace debug {
29+
30+
inline std::string short_file_name(const char* file)
31+
{
32+
std::string f(file);
33+
size_t index = f.find("/include/");
34+
if (index != std::string::npos)
35+
f = f.substr(index + 9);
36+
return f;
37+
}
38+
39+
} // namespace debug
40+
} // namespace Rcpp
41+
2542
#define RCPP_DECORATE(__FUN__) __FUN__##__rcpp__wrapper__
2643
#define RCPP_GET_NAMES(x) Rf_getAttrib(x, R_NamesSymbol)
2744
#define RCPP_GET_CLASS(x) Rf_getAttrib(x, R_ClassSymbol)

src/api.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,15 @@ std::string demangle(const std::string& name) {
126126
#endif
127127
}
128128

129+
// NOTE: remains registered but this routine is now effectively unused by Rcpp;
130+
// we retain it for backwards compatibility with any existing packages which
131+
// (explicitly or implicitly) rely on its existence. See also:
132+
// https://github.com/RcppCore/Rcpp/issues/1066
133+
129134
// [[Rcpp::register]]
130135
const char* short_file_name(const char* file) { // #nocov start
131-
std::string f(file);
136+
static std::string f;
137+
f = file;
132138
size_t index = f.find("/include/");
133139
if (index != std::string::npos) {
134140
f = f.substr(index + 9);

0 commit comments

Comments
 (0)