function - grep 报告在上下文中包含函数名称

标签 function grep git-diff

git-diff 报告包含更改的直接函数名称,如下所示:

$ git diff
diff --git a/apps/cli/elmo.py b/apps/cli/elmo.py
index ac056e9..1b7c1d0 100644
--- a/myfile.c
+++ b/myfile.c
@@ -100,12 +106,20 @@ int myioctl(unsigned int cmd, int size, int direction, unsigned long arg
                rc = myfunc1(ioargp);
                break;

-       case IOCTL_1:
-               rc = myfunc1(ioargp);
+       case IOCTL_2:
+               rc = myfunc2(arg);
                break;

有没有办法让 grep 报告匹配的函数上下文,类似于 git-diff (即,@@ int myioctl(...) 在上述情况下)?或者是否有一些 grep 的替代工具,其行为类似于 grep 但也会报告函数上下文?

我经常对源代码进行 grep,了解包含匹配行的函数很有帮助,而无需打开文件并查找特定模式或行号。

谢谢!

最佳答案

这并不完全可行,因为我认为您无法使用正则表达式表示所有形式的函数语句。你需要一个语法。

反正

所以,你有文件名

现在您需要找出匹配发生的行号。

grep -n "pattern" MYFILE | sed -r 's/([0-9]*):.*/\1/'

您想知道上面印有哪一行的函数
grep -n "pattern" MYFILE | sed -r 's/([0-9]*):.*/\1/' | while read lineNo
do
    head -n $lineNo MYFILE | tac | egrep -m 1 "[[:alnum:]]+[[:space:]]+[[:alnum:]](.*)[[:space:]]*{"
done

这将为您提供包含发生匹配行的函数的名称(希望如此)。
(我正在使用 egrep 所以我可以使用 '+' 运算符。)

请注意,此模式与函数声明相匹配,如下所示:
<function-type> <function-identifier> ( <things> ) {  

您将需要对其进行扩展,以便它可以表示更多形式的函数语句。

关于function - grep 报告在上下文中包含函数名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12823013/

相关文章:

linux - 有没有办法捕获请求日志文件中没有响应的行?

linux - 根据格式从文件中批量提取行

git - 如何只列出两个分支之间新添加的文件

git - 如何在 Mac OS 中设置 kdiff3?

git - git 可以在 repo 文件和外部(非 repo )文件之间进行区分吗?

function - 在 Shell 脚本中将变量作用域到函数的 POSIX 兼容方式

function - 是否有一些 "scale invariant"替代 softmax 函数?

c++ - 构造函数中的 "No matching function call"

更改指针地址 - 函数

linux - Grep 显示几行直到特定文本