-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New routine_registration_linter (#1669)
- Loading branch information
1 parent
444a2fb
commit cee077a
Showing
11 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#' Identify unregistered native routines | ||
#' | ||
#' It is preferable to register routines for efficiency and safety. | ||
#' | ||
#' @examples | ||
#' # will produce lints | ||
#' lint( | ||
#' text = '.Call("cpp_routine", PACKAGE = "mypkg")', | ||
#' linters = routine_registration_linter() | ||
#' ) | ||
#' | ||
#' lint( | ||
#' text = '.Fortran("f_routine", PACKAGE = "mypkg")', | ||
#' linters = routine_registration_linter() | ||
#' ) | ||
#' | ||
#' # okay | ||
#' lint( | ||
#' text = ".Call(cpp_routine)", | ||
#' linters = routine_registration_linter() | ||
#' ) | ||
#' | ||
#' lint( | ||
#' text = ".Fortran(f_routine)", | ||
#' linters = routine_registration_linter() | ||
#' ) | ||
#' | ||
#' @evalRd rd_tags("routine_registration_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. \cr | ||
#' https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Registering-native-routines | ||
#' | ||
#' @export | ||
routine_registration_linter <- function() { | ||
native_routine_callers <- c(".C", ".Call", ".Fortran", ".External") | ||
xpath <- glue::glue(" | ||
//SYMBOL_FUNCTION_CALL[ {xp_text_in_table(native_routine_callers)} ] | ||
/parent::expr | ||
/following-sibling::expr[1]/STR_CONST | ||
/parent::expr | ||
") | ||
|
||
Linter(function(source_expression) { | ||
if (!is_lint_level(source_expression, "expression")) { | ||
return(list()) | ||
} | ||
|
||
xml <- source_expression$xml_parsed_content | ||
|
||
bad_expr <- xml2::xml_find_all(xml, xpath) | ||
|
||
xml_nodes_to_lints( | ||
bad_expr, | ||
source_expression = source_expression, | ||
lint_message = "Register your native code routines with useDynLib and R_registerRoutines().", | ||
type = "warning" | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
skip_if_not_installed("patrick") | ||
patrick::with_parameters_test_that( | ||
"lints correctly", | ||
{ | ||
linter <- routine_registration_linter() | ||
expect_lint(sprintf("%s(ROUTINE, 1)", caller), NULL, linter) | ||
expect_lint( | ||
sprintf("%s('ROUTINE', PACKAGE = 'foo')", caller), | ||
"Register your native code routines with useDynLib", | ||
linter | ||
) | ||
}, | ||
.test_name = c(".C", ".Call", ".External", ".Fortran"), | ||
caller = c(".C", ".Call", ".External", ".Fortran") | ||
) |