ctags:获取C函数结束行号
ctags: To get C function end line number
是否也可以通过ctags获取函数结束行号
"ctags -x --c-kinds=f filename.c"
以上命令列出了函数定义的起始行号。想要一种获取函数结束行号的方法。
其他方法:
awk 'NR > first && /^}$/ { print NR; exit }' first=$FIRST_LINE filename.c
这需要正确格式化代码
示例:
filename.c
1 #include<stdio.h>
2 #include<stdlib.h>
3 int main()
4 {
5 const char *name;
6
7 int a=0
8 printf("name");
9 printf("sssss: %s",name);
10
11 return 0;
12 }
13
14 void code()
15 {
16 printf("Code \n");
17 }
18
19 int code2()
20 {
21 printf("code2 \n");
22 return 1
23 }
24
输入:文件名和函数起始行号
Example:
Input: filename.c 3
Output: 12
Input : filename.c 19
Output : 23
有什么better/simple方法可以做到这一点吗?
awk
救援!
不处理注释中的花括号,但应该处理函数中的块,请试一试...
$ awk -v s=3 'NR>=s && /{/ {c++}
NR>=s && /}/ && c && !--c {print NR; exit}' file
查找指定起始行号s后第一个匹配的大括号。
C/C++ Universal-ctags(https://ctags.io) 的解析器有结束:字段。
jet@localhost tmp]$ cat -n foo.c
1 int
2 main( void )
3 {
4
5 }
6
7 int
8 bar (void)
9 {
10
11 }
12
13 struct x {
14 int y;
15 };
16
[jet@localhost tmp]$ ~/var/ctags/ctags --fields=+ne -o - --sort=no foo.c
main foo.c /^main( void )$/;" f line:2 typeref:typename:int end:5
bar foo.c /^bar (void)$/;" f line:8 typeref:typename:int end:11
x foo.c /^struct x {$/;" s line:13 file: end:15
y foo.c /^ int y;$/;" m line:14 struct:x typeref:typename:int file:
是否也可以通过ctags获取函数结束行号
"ctags -x --c-kinds=f filename.c"
以上命令列出了函数定义的起始行号。想要一种获取函数结束行号的方法。
其他方法:
awk 'NR > first && /^}$/ { print NR; exit }' first=$FIRST_LINE filename.c
这需要正确格式化代码
示例: filename.c
1 #include<stdio.h>
2 #include<stdlib.h>
3 int main()
4 {
5 const char *name;
6
7 int a=0
8 printf("name");
9 printf("sssss: %s",name);
10
11 return 0;
12 }
13
14 void code()
15 {
16 printf("Code \n");
17 }
18
19 int code2()
20 {
21 printf("code2 \n");
22 return 1
23 }
24
输入:文件名和函数起始行号
Example:
Input: filename.c 3
Output: 12
Input : filename.c 19
Output : 23
有什么better/simple方法可以做到这一点吗?
awk
救援!
不处理注释中的花括号,但应该处理函数中的块,请试一试...
$ awk -v s=3 'NR>=s && /{/ {c++}
NR>=s && /}/ && c && !--c {print NR; exit}' file
查找指定起始行号s后第一个匹配的大括号。
C/C++ Universal-ctags(https://ctags.io) 的解析器有结束:字段。
jet@localhost tmp]$ cat -n foo.c
1 int
2 main( void )
3 {
4
5 }
6
7 int
8 bar (void)
9 {
10
11 }
12
13 struct x {
14 int y;
15 };
16
[jet@localhost tmp]$ ~/var/ctags/ctags --fields=+ne -o - --sort=no foo.c
main foo.c /^main( void )$/;" f line:2 typeref:typename:int end:5
bar foo.c /^bar (void)$/;" f line:8 typeref:typename:int end:11
x foo.c /^struct x {$/;" s line:13 file: end:15
y foo.c /^ int y;$/;" m line:14 struct:x typeref:typename:int file: