eliben / pycparser

:snake: Complete C99 parser in pure Python
Other
3.24k stars 609 forks source link

Parsing `va_arg(foo, type)` #533

Open charmoniumQ opened 6 months ago

charmoniumQ commented 6 months ago

I was hoping to parse the following code.

// Fake stub of stdarg.h more amenable to pycparser
typedef void* va_list;

// The actual code I want to parse
int foo(int f, ...) {
  va_list ap;
  va_start(ap, f);
  va_arg(ap, int); // <--- ParseError: before: int
  va_end(ap);
}

I understand that pycparser can parse function-like syntax forms, so long as none of the arguments are type-names. I was wondering what part of the code causes this behavior (it's not obvious to me from reading the _c_ast.cfg), and would you accept a pull request that loosens this behavior, i.e., foo(int) would be valid (so that va_arg can be supported)?

Some rationale:

eliben commented 6 months ago

While va_arg is in C99, it's a macro so it should be handled by the preprocessor. You can already parse this using the standard provided fake headers (see the README).

In the general case taking types instead of other identifiers can be very tricky in C (which is probably why this is delegated to a macro in the standard).

charmoniumQ commented 6 months ago

The provided macro ignores the type of the argument, so this information is not available in the AST.

eliben commented 6 months ago

I don't object, but it may be tricky to add this parsing rule without introducing more parser conflicts. PRs welcome