-
Notifications
You must be signed in to change notification settings - Fork 195
New routine_registration_linter #1669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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" | ||
| ) | ||
| }) | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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") | ||
| ) | ||
Uh oh!
There was an error while loading. Please reload this page.