Skip to content
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

analyze: search for C/C++ function(s) containing a pattern #1543

Closed
christoffer-nylen opened this issue Mar 7, 2022 · 1 comment
Closed

Comments

@christoffer-nylen
Copy link
Collaborator

christoffer-nylen commented Mar 7, 2022

The user would like to find C/C++ function(s) where a specific pattern is found; for example to study how a variable, literal or code comment is used in a project. This can be helpful when doing code review, traceability analysis, data-flow analysis or control-flow analysis.

Here's a similar stack overflow discussion about this topic.

Example:

A user would like to get a list of the function(s) in foo.c that contains the keyword tmp:

int func1(int x, int y)
{
  return x + y;
}
int func2(int x, int y, int z)
{
  int tmp = x + y;
  tmp *= z;
  return tmp;
}

Suggestions

Approach 1: grep -B

$ grep -nHi -B10 tmp
foo.c-1-int func1(int x, int y)
foo.c-2-{
foo.c-3-  return x + y;
foo.c-4-}
foo.c-5-int func2(int x, int y, int z)
foo.c-6-{
foo.c:7:  int tmp = x + y;
foo.c:8:  tmp *= z;
foo.c:9:  return tmp;

Cons:

  • generates much noise
  • -B might require large numbers for large functions

Approach 2: git grep -p

$ git grep -nHi -p tmp
foo.c=5=int func2(int x, int y, int z)
foo.c:7:  int tmp = x + y;
foo.c:8:  tmp *= z;
foo.c:9:  return tmp;

Pros:

  • Much less noise than grep -B

Cons:

  • Also displays the pattern match, which might not always be desirable.

Approach 3: new dextool plugin/feature

Example:

$ dextool analyze -h
-p, --show-function shows the line that contains the function name of a `file:row`

$ grep -nHi -e 'tmp' foo.c | dextool analyze --show-function --from-stdin
foo.c:5: int func2(int x, int y, int z)

$ dextool analyze --show-function foo.c:7
foo.c:1: int func1(int x, int y)
@christoffer-nylen christoffer-nylen changed the title analyze: grep function name analyze: list C/C++ function(s) where a pattern is found Mar 7, 2022
@christoffer-nylen christoffer-nylen changed the title analyze: list C/C++ function(s) where a pattern is found analyze: list C/C++ function(s) containing a specified pattern Mar 7, 2022
@christoffer-nylen christoffer-nylen changed the title analyze: list C/C++ function(s) containing a specified pattern analyze: search for C/C++ function(s) containing a pattern Mar 7, 2022
@christoffer-nylen
Copy link
Collaborator Author

(use git grep -p)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant