Skip to content

Commit d18a0bc

Browse files
authored
Merge pull request #528 from coatless/autogeneration-warning
Auto generation Warnings & Invalid C++ Identifiers (Closes #526 and #387)
2 parents 1fb8865 + 068899b commit d18a0bc

File tree

7 files changed

+55
-35
lines changed

7 files changed

+55
-35
lines changed

ChangeLog

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
2016-08-02 James J Balamuta <[email protected]>
2+
3+
* R/Rcpp.package.skeleton.R: Added autogeneration warning
4+
* src/attributes.cpp: Fixed invalid C++ Identifiers and modified
5+
export header to warn against hand edits
6+
* vignettes/Rcpp-package.Rnw: Updated code generation snippets
7+
* man/Rcpp.package.skeleton.Rd: Add warning on hand edits to the
8+
autogenerated RcppExports files.
9+
* man/compileAttributes.Rd: Idem
10+
111
2016-08-01 Nathan Russell <[email protected]>
212

313
* inst/include/Rcpp/vector/Vector.h: Added decreasing option for Vector

R/Rcpp.package.skeleton.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ Rcpp.package.skeleton <- function(name = "anRpackage", list = character(),
144144
message(" >> added example src file using Rcpp attributes")
145145
compileAttributes(root)
146146
message(" >> compiled Rcpp attributes")
147+
message(" >> do NOT modify by hand either RcppExports.cpp or ",
148+
"RcppExports.R")
147149
} else {
148150
header <- readLines(file.path(skeleton, "rcpp_hello_world.h"))
149151
header <- gsub("@PKG@", name, header, fixed = TRUE)

inst/NEWS.Rd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
\item The \code{NORET} macro is now defined if it was not already defined
1111
by R (Kevin fixing issue \ghit{512}).
1212
\item Environment functions get() & find() now accept a Symbol
13-
(James Joseph Balamuta in \ghpr{513} addressing issue \ghit{326}).
13+
(James Balamuta in \ghpr{513} addressing issue \ghit{326}).
1414
\item Several uses of \code{Rf_eval} were replaced by the preferred
1515
\code{Rcpp::Rcpp_eval} (Qiang in PR \ghpr{523} closing \ghit{498}).
16+
\item Improved Autogeneration Warning for RcppExports
17+
(James Balamuta in \ghpr{528} addressing issue \ghit{526}).
18+
\item Fixed invalid C++ prefix identifiers in auto-generated code
19+
(James Balamuta in \ghpr{528} addressing issue \ghit{387}).
1620
}
1721
\item Changes in Rcpp unit tests
1822
\itemize{

man/Rcpp.package.skeleton.Rd

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,11 @@ Rcpp.package.skeleton(name = "anRpackage", list = character(),
6565
If the \code{attributes} argument is \code{TRUE}, then rather than generate
6666
the example files as described above, a single \samp{rcpp_hello_world.cpp}
6767
file is created in the \samp{src} directory and it's attributes are
68-
compiled using the \code{\link{compileAttributes}} function, so files
69-
\samp{RcppExports.R} and \samp{RcppExports.cpp} are generated as well.
68+
compiled using the \code{\link{compileAttributes}} function. This leads to
69+
the files \samp{RcppExports.R} and \samp{RcppExports.cpp} being generated.
70+
They are automatically regenerated from \emph{scratch} each time
71+
\code{\link{compileAttributes}} is called. Therefore, one should
72+
\strong{not} modify by hand either of the \samp{RcppExports} files.
7073
7174
If the \code{module} argument is \code{TRUE}, a sample Rcpp module will
7275
be generated as well.

man/compileAttributes.Rd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ compileAttributes(pkgdir = ".", verbose = getOption("verbose"))
2121
\details{
2222
The source files in the package directory given by \code{pkgdir} are scanned for attributes and code is generated as required based on the attributes.
2323

24-
For C++ functions adorned with the \code{Rcpp::export} attribute, the C++ and R source code required to bind to the function from R is generated and added (respectively) to \code{src/RcppExports.cpp} or \code{R/RcppExports.R}.
24+
For C++ functions adorned with the \code{Rcpp::export} attribute, the C++ and R source code required to bind to the function from R is generated and added (respectively) to \code{src/RcppExports.cpp} or \code{R/RcppExports.R}. Both of these files are automatically generated from \emph{scratch} each time \code{compiledAttributes} is run.
2525

2626
In order to access the declarations for custom \code{Rcpp::as} and \code{Rcpp::wrap} handlers the \code{compileAttributes} function will also call any \link[inline:plugins]{inline plugins} available for packages listed in the \code{LinkingTo} field of the \code{DESCRIPTION} file.
2727
}

src/attributes.cpp

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,8 +1772,9 @@ namespace attributes {
17721772

17731773
// write header/preamble
17741774
std::ostringstream headerStream;
1775-
headerStream << commentPrefix_ << " This file was generated by "
1776-
<< "Rcpp::compileAttributes" << std::endl;
1775+
headerStream << commentPrefix_ << " Generated by using "
1776+
<< "Rcpp::compileAttributes()"
1777+
<< " -> do not edit by hand" << std::endl;
17771778
headerStream << commentPrefix_ << " Generator token: "
17781779
<< generatorToken() << std::endl << std::endl;
17791780
if (!preamble.empty())
@@ -2045,11 +2046,11 @@ namespace attributes {
20452046
<< getCCallable(packageCpp() + "_" + function.name()) << ";"
20462047
<< std::endl;
20472048
ostr() << " }" << std::endl;
2048-
ostr() << " RObject __result;" << std::endl;
2049+
ostr() << " RObject rcpp_result_gen;" << std::endl;
20492050
ostr() << " {" << std::endl;
20502051
if (it->rng())
2051-
ostr() << " RNGScope __rngScope;" << std::endl;
2052-
ostr() << " __result = " << ptrName << "(";
2052+
ostr() << " RNGScope RCPP_rngScope_gen;" << std::endl;
2053+
ostr() << " rcpp_result_gen = " << ptrName << "(";
20532054

20542055
const std::vector<Argument>& args = function.arguments();
20552056
for (std::size_t i = 0; i<args.size(); i++) {
@@ -2061,18 +2062,18 @@ namespace attributes {
20612062
ostr() << ");" << std::endl;
20622063
ostr() << " }" << std::endl;
20632064

2064-
ostr() << " if (__result.inherits(\"interrupted-error\"))"
2065+
ostr() << " if (rcpp_result_gen.inherits(\"interrupted-error\"))"
20652066
<< std::endl
20662067
<< " throw Rcpp::internal::InterruptedException();"
20672068
<< std::endl;
2068-
ostr() << " if (__result.inherits(\"try-error\"))"
2069+
ostr() << " if (rcpp_result_gen.inherits(\"try-error\"))"
20692070
<< std::endl
20702071
<< " throw Rcpp::exception(as<std::string>("
2071-
<< "__result).c_str());"
2072+
<< "rcpp_result_gen).c_str());"
20722073
<< std::endl;
20732074
if (!function.type().isVoid()) {
20742075
ostr() << " return Rcpp::as<" << function.type() << " >"
2075-
<< "(__result);" << std::endl;
2076+
<< "(rcpp_result_gen);" << std::endl;
20762077
}
20772078

20782079
ostr() << " }" << std::endl << std::endl;
@@ -2148,7 +2149,7 @@ namespace attributes {
21482149
}
21492150

21502151
std::string CppExportsIncludeGenerator::getHeaderGuard() const {
2151-
return "__" + packageCpp() + "_RcppExports_h__";
2152+
return "_RCPP_" + packageCpp() + "_RCPPEXPORTS_H_GEN_";
21522153
}
21532154

21542155
CppPackageIncludeGenerator::CppPackageIncludeGenerator(
@@ -2196,7 +2197,7 @@ namespace attributes {
21962197
}
21972198

21982199
std::string CppPackageIncludeGenerator::getHeaderGuard() const {
2199-
return "__" + packageCpp() + "_h__";
2200+
return "_RCPP_" + packageCpp() + "_H_GEN_";
22002201
}
22012202

22022203
RExportsGenerator::RExportsGenerator(const std::string& packageDir,
@@ -2582,9 +2583,9 @@ namespace attributes {
25822583
ostr << args << ") {" << std::endl;
25832584
ostr << "BEGIN_RCPP" << std::endl;
25842585
if (!function.type().isVoid())
2585-
ostr << " Rcpp::RObject __result;" << std::endl;
2586+
ostr << " Rcpp::RObject rcpp_result_gen;" << std::endl;
25862587
if (!cppInterface && attribute.rng())
2587-
ostr << " Rcpp::RNGScope __rngScope;" << std::endl;
2588+
ostr << " Rcpp::RNGScope rcpp_rngScope_gen;" << std::endl;
25882589
for (size_t i = 0; i<arguments.size(); i++) {
25892590
const Argument& argument = arguments[i];
25902591

@@ -2595,7 +2596,7 @@ namespace attributes {
25952596

25962597
ostr << " ";
25972598
if (!function.type().isVoid())
2598-
ostr << "__result = Rcpp::wrap(";
2599+
ostr << "rcpp_result_gen = Rcpp::wrap(";
25992600
ostr << function.name() << "(";
26002601
for (size_t i = 0; i<arguments.size(); i++) {
26012602
const Argument& argument = arguments[i];
@@ -2609,7 +2610,7 @@ namespace attributes {
26092610

26102611
if (!function.type().isVoid())
26112612
{
2612-
ostr << " return __result;" << std::endl;
2613+
ostr << " return rcpp_result_gen;" << std::endl;
26132614
}
26142615
else
26152616
{
@@ -2623,11 +2624,11 @@ namespace attributes {
26232624
if (cppInterface) {
26242625
ostr << "RcppExport SEXP " << funcName << "(" << args << ") {"
26252626
<< std::endl;
2626-
ostr << " SEXP __result;" << std::endl;
2627+
ostr << " SEXP rcpp_result_gen;" << std::endl;
26272628
ostr << " {" << std::endl;
26282629
if (attribute.rng())
2629-
ostr << " Rcpp::RNGScope __rngScope;" << std::endl;
2630-
ostr << " __result = PROTECT(" << funcName
2630+
ostr << " Rcpp::RNGScope rcpp_rngScope_gen;" << std::endl;
2631+
ostr << " rcpp_result_gen = PROTECT(" << funcName
26312632
<< kTrySuffix << "(";
26322633
for (size_t i = 0; i<arguments.size(); i++) {
26332634
const Argument& argument = arguments[i];
@@ -2637,21 +2638,21 @@ namespace attributes {
26372638
}
26382639
ostr << "));" << std::endl;
26392640
ostr << " }" << std::endl;
2640-
ostr << " Rboolean __isInterrupt = Rf_inherits(__result, \"interrupted-error\");"
2641+
ostr << " Rboolean rcpp_isError_gen = Rf_inherits(rcpp_result_gen, \"interrupted-error\");"
26412642
<< std::endl
2642-
<< " if (__isInterrupt) {" << std::endl
2643+
<< " if (rcpp_isError_gen) {" << std::endl
26432644
<< " UNPROTECT(1);" << std::endl
26442645
<< " Rf_onintr();" << std::endl
26452646
<< " }" << std::endl
2646-
<< " Rboolean __isError = Rf_inherits(__result, \"try-error\");"
2647+
<< " Rboolean rcpp_isError_gen = Rf_inherits(rcpp_result_gen, \"try-error\");"
26472648
<< std::endl
2648-
<< " if (__isError) {" << std::endl
2649-
<< " SEXP __msgSEXP = Rf_asChar(__result);" << std::endl
2649+
<< " if (rcpp_isError_gen) {" << std::endl
2650+
<< " SEXP rcpp_msgSEXP_gen = Rf_asChar(rcpp_result_gen);" << std::endl
26502651
<< " UNPROTECT(1);" << std::endl
2651-
<< " Rf_error(CHAR(__msgSEXP));" << std::endl
2652+
<< " Rf_error(CHAR(rcpp_msgSEXP_gen));" << std::endl
26522653
<< " }" << std::endl
26532654
<< " UNPROTECT(1);" << std::endl
2654-
<< " return __result;" << std::endl
2655+
<< " return rcpp_result_gen;" << std::endl
26552656
<< "}" << std::endl;
26562657
}
26572658
}

vignettes/Rcpp-package.Rnw

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ e.g. handle the \texttt{List} class.
173173
on the package, which generates the \texttt{RcppExports.cpp} file:
174174

175175
<<lang=cpp>>=
176-
// This file was generated by Rcpp::compileAttributes
176+
// Generated by using Rcpp::compileAttributes() -> do not edit by hand
177177
// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
178178
179179
#include <Rcpp.h>
@@ -184,10 +184,10 @@ using namespace Rcpp;
184184
List rcpp_hello_world();
185185
RcppExport SEXP mypackage_rcpp_hello_world() {
186186
BEGIN_RCPP
187-
Rcpp::RObject __result;
188-
Rcpp::RNGScope __rngScope;
189-
__result = Rcpp::wrap(rcpp_hello_world());
190-
return __result;
187+
Rcpp::RObject rcpp_result_gen;
188+
Rcpp::RNGScope rcpp_rngScope_gen;
189+
rcpp_result_gen = Rcpp::wrap(rcpp_hello_world());
190+
return rcpp_result_gen;
191191
END_RCPP
192192
}
193193
@
@@ -205,7 +205,7 @@ The \rdoc{Rcpp}{compileAttributes} also generates \proglang{R} code
205205
that uses the \proglang{C++} function.
206206

207207
<<lang=cpp>>=
208-
# This file was generated by Rcpp::compileAttributes
208+
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
209209
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
210210
211211
rcpp_hello_world <- function() {

0 commit comments

Comments
 (0)