Getting function argument types

蓝咒 提交于 2019-12-14 02:39:02

问题


Suppose I have a call to a function which takes a variable number of arguments in my source code. I want to do some kind of static analysis on this source code to find the type of the arguments being actually passed to the function. For example, if my function call is -

foo(a, b, c)

I want to find the data type of a, b and c and store this information.


回答1:


You pretty well have to do the parse-and-build-a-symbol-table part of compiling the program.

Which means running the preprocessor, and lexing as well.

That's the bad news.

The good news is that you don't have to do much of the hard stuff. No need to build a AST, every part of the code except typedefs; struct, union, and enum definitions; variable-or-function declarations-and-definitions; and analyzing the function call arguments can be a no-op.

On further thought prompted by Chris' comments: You do have to be able to analyze the types of expressions and handle the va-arg promotions, as well.

It is still a smaller project than writing a whole compiler, but should be approached with some thought.




回答2:


If this is in C++, you can hack together some RTTI using typeid etc.

http://en.wikipedia.org/wiki/Run-time_type_information



来源:https://stackoverflow.com/questions/4290437/getting-function-argument-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!