matching all functions in Rust (poor man's tracing) #2364
-
|
I would like to do some sort of quick and dirty tracing in a project, by that I would like to print the function's name in every function. The following command fine for many function signatures but leaves out. (
ast-grep -p 'fn $FUNC($$$ARGS) $$$RT { $$$BODY }'
-r '
fn $FUNC($$$ARGS) $$$RT {
eprintln!("called: $FUNC");
$$$BODY
}'
-l rs
-U
)I tried the pattern Questions
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
you can use ast-grep playground to debug the pattern https://ast-grep.github.io/playground.html#eyJtb2RlIjoiUGF0Y2giLCJsYW5nIjoicnVzdCIsInF1ZXJ5IjoiZm4gJEZVTkMoJCQkQVJHUykgeyAkJCRCT0RZIH0iLCJyZXdyaXRlIjoiIiwic3RyaWN0bmVzcyI6InNtYXJ0Iiwic2VsZWN0b3IiOiIiLCJjb25maWciOiJydWxlOlxuICBwYXR0ZXJuOiAkRlVOQygpXG50cmFuc2Zvcm06XG4gIFNXQVBQRUQ6XG4gICAgcmVwbGFjZTpcbiAgICAgIHNvdXJjZTogJEZVTkNcbiAgICAgIHJlcGxhY2U6ICdeKGdldHxzZXQpKC4rKSQnXG4gICAgICBieTogJyQyJDEnXG5maXg6ICRTV0FQUEVEIiwic291cmNlIjoicHViKGNyYXRlKSBhc3luYyBmbiBteV9mbigpIHt9In0=
the a better way is to use ast-grep scan --inline-rules '
id: find-fn
language: rust
rule:
kind: function_item' |
Beta Was this translation helpful? Give feedback.

you can use ast-grep playground to debug the pattern https://ast-grep.github.io/playground.html#eyJtb2RlIjoiUGF0Y2giLCJsYW5nIjoicnVzdCIsInF1ZXJ5IjoiZm4gJEZVTkMoJCQkQVJHUykgeyAkJCRCT0RZIH0iLCJyZXdyaXRlIjoiIiwic3RyaWN0bmVzcyI6InNtYXJ0Iiwic2VsZWN0b3IiOiIiLCJjb25maWciOiJydWxlOlxuICBwYXR0ZXJuOiAkRlVOQygpXG50cmFuc2Zvcm06XG4gIFNXQVBQRUQ6XG4gICAgcmVwbGFjZTpcbiAgICAgIHNvdXJjZTogJEZVTkNcbiAgICAgIHJlcGxhY2U6ICdeKGdldHxzZXQpKC4rKSQnXG4gICAgICBieTogJyQyJDEnXG5maXg6ICRTV0FQUEVEIiwic291cmNlIjoicHViKGNyYXRlKSBhc3luYyBmbiBteV9mbigpIHt9In0=
the
pubmodifier is inside the function declaration so it does not make the pattern match.a better way is to use
--inline-rules