简体   繁体   English

正则表达式,用于获取C类型定义

[英]Regex for greping C type definition

I have a big bunch of source files and I want to grep through it to find the definition of a specific user-defined type dev_if_type_t . 我有大量的源文件,我想通过它查找特定的用户定义类型dev_if_type_t的定义。 All I know about it so far it's that some functions in the code I'm examining use it as a return value. 到目前为止,我所知道的只是我正在检查的代码中的某些函数将其用作返回值。

Right now I'm using the following: 现在,我正在使用以下内容:

typedef.*dev_if_type_t|(define|typedef|enum|struct)\s*dev_if_type_t

but it returns no results. 但它不会返回任何结果。 Is there another method of C type definition I'm neglecting to mention? 我有没有提到C类型定义的另一种方法?

The grep line itself, in the code base's top directory: grep行本身,位于代码库的顶层目录中:

grep -rn "typedef.*dev_if_type_t\|\(define\|typedef\|enum\|struct\)\s*dev_if_type_t" *

There could be much more variants of the definition like: 该定义可能有更多变体,例如:

typedef struct {
    /* some code */
} dev_if_type_t;

Some code could also look like this: 一些代码也可能像这样:

#define \
dev_if_type_t int

struct
dev_if_type_t
{
    /* some code */
};

You'll never know. 你永远不会知道。
I would suggest you try it just with grepping dev_if_type_t and using the context option -C <num> of grep to find the definition by yourself. 我建议您尝试使用dev_if_type_t并使用grep的上下文选项-C <num> dev_if_type_t查找定义。

When using expressions including | 使用包括|在内的表达式时 don't forget to use egrep (deprecated) or the proper command grep -E ... . 不要忘记使用egrep (不建议使用)或正确的命令grep -E ...

Note that \\| 注意\\| and \\( has a different meaning. Use | and ( for your purpose. \\(具有不同的含义。使用|(用于您的目的。

So the correct pattern should be: 因此正确的模式应该是:

grep -Ern "typedef.*dev_if_type_t|(define|typedef|enum|struct)\s*dev_if_type_t" *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM