1

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.

X-Istence
  • 16,324
  • 6
  • 57
  • 74
i0exception
  • 372
  • 4
  • 12
  • FWIW if your function is a `printf`- or `scanf`-like variant, and if you're using gcc, you can use [`format` function attribute](http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#index-g_t_0040code_007bformat_007d-function-attribute-2417) with your function, and the compiler will automatically type-check your arguments to all calls of that function. – Adam Rosenfield Jun 23 '11 at 02:46

2 Answers2

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.

dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
0

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

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

Hank
  • 547
  • 3
  • 4
  • 1
    Resisted my knee-jerk "downvote everyone who mentions C++ in a C question" long enough to check that the language was added as an afterthought rather than part of the question. – Chris Lutz Jun 23 '11 at 02:41