开发者

How to match C function prototypes and variable definitions with a regexp?

开发者 https://www.devze.com 2023-04-12 00:39 出处:网络
This has been asked before but I have a specialised case which I should be able to handle with a regular expression.

This has been asked before but I have a specialised case which I should be able to handle with a regular expression.

I'm trying to read the warning log from Doxygen and the source is in C (so far, I dread to think about C++).

I need to match the functions and variable definitions found in that log and pick up the function and variable names.

More specifically the log has lines like

/home/me/blaa.c:10:Warning: Member a_function(int a, int b) (function) of file blaa.c is not documented

and

/home/me/blaa.h:10:Warning: Member a_variable[SOME_CONST(sizeof(SOME_STRUCT), 64)*ANOTHER_CONST] (variable) of file blaa.h is not documented

With all the variations you can have in C...

Can I match those with just one regexp or should I not even bother? The word in after the "parameter" (I use this loosely to also include the variables) list in parentheses is a set of certain words (function, variable, enum, etc) so if nothing else helps, I could match w开发者_如何学编程ith those but I'd rather not in case there are types that I haven't seen yet in the logs.

My current attempt looks like

'(?P<full_path>.+):\d+:\s+Warning:\s+Member\s+(?P<member_name>.+)([\(\[](\**)\s*\w+([,)])[\)\]))*\s+\((?P<member_type>.+)\) of file\s+(?P<filename>.+)\s+is not documented'

(I use Python's re package.)

But it still fails to catch everything.

EDIT: There's some mistake in there that I have done in the last edit.


You were allowing zero or more matches between <member_name> and <member_type>. Try this instead:

'(?P<full_path>.+):\d+:\s+Warning:\s+Member\s+(?P<member_name>\w+).*\s+\((?P<member_type>\w+)\) of file\s+(?P<filename>.+)\s+is not documented'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号