i0exception
i0exception

Reputation: 392

Getting function argument types

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.

Upvotes: 1

Views: 421

Answers (2)

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.

Upvotes: 1

Hank
Hank

Reputation: 547

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

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

Upvotes: 0

Related Questions