question on -ast dump/print

Hi there,
I'm new to clang, so please bet gentle.

On revision 57719, debug build

I'm a bit confused on the -ast-dump/print output. It seems to be not
printing global declarations. I don't know if this is a "feature" or
"bug" or perhaps me not understanding what should be in the AST. I
read the bug reports but didn't see anything relevant. Oddly
emit-html correctly prints out everything.

here's some examples:

"""
int i = 0;
"""

gets turned into

"""
$ clang -ast-print junk.c
typedef char *__builtin_va_list;
Read top-level variable decl: 'i'
"""

which smells wrong... locals seems to be ok

But...

"""
void f() {
  int i = 0;
}
"""

becomes

"""
typedef char *__builtin_va_list;
void f() {
  int i = 0;
}
"""

However, enums are also strange:

Something like
{{{
int f(void) {
  enum color { RED, GREEN, BLUE};
  enum color x = RED;
  switch (x) {
  case RED: return 1;
  case GREEN: return 2;
  case BLUE: return 3;
  }
}
"""

gets turned into:

"""
typedef char *__builtin_va_list;

int f() {
  enum color;
  enum color x = RED;
  switch ((x)) {
  case RED:
    return 1;
  case GREEN:
    return 2;
  case BLUE:
    return 3;
  }
}

which zapped away all the enum declarations.

""""

thoughts?

--nickg

Unfortunately, AST dumping and printing need a fair amount of work. We really just need someone to take this on as a project. I literally have a sticky note on my monitor which says “printing needs love”, but its been there for some time.

I think the main thing is to move the printing logic which is in Driver/ASTConsumers.cpp and merge it with the StmtPrinting stuff. This way they can reuse the same pretty printing infrastructure. Once that work is done it will be much easier to just add things on a case by case basis.

For now, the unfortunate truth is that -ast-dump and -ast-print don’t work very well, when something appears incorrect it is more often than not a problem in the printing rather than a problem in the AST representation.

  • Daniel

Thanks for the quick reply!

Ok, I'll take a look at those files. This might more than I can chew
on right now, but who knows, it just might be fun.

With a solid text-based AST, a lot of interesting things can be done.
A lot of easy static checkers could be made, among other things (if
anything just for proof-of-concept).

Should that stickie on your desk be turned into a bug report, or would
that just muck up the database.

thanks,

--nickg

There is no hard answer here, but I tend to prefer bugzilla entries for minor bugs in existing solid features... not "this feature doesn't work at all".

-Chris