3

I tried to use clang's ParseAST() and ASTConsumer to extract all c function declarations in a source file, and then output these function declarations into another file.

It's all good unless there is some unknown type name exists. For example,

my_type foo(int x) { /*...*/ }    // "my_type" is just a type identifier whose definition is missed in this translation unit.

When I use getReturnType() in RecursiveASTVisitor::VisitFunctionDecl(), it will return type "int". So my program will output

int foo(int x);

instead of

my_type foo(int x);

I'm curious why "my_type" will be parsed as of type int. When parsing this source code, clang will report an error saying error:

unknown type name 'my_type'

. So I think clang knows it's a type (unknown type is also a type I think).

m.s.
  • 16,063
  • 7
  • 53
  • 88
waiwai444
  • 71
  • 1
  • 3
  • Maybe the parser assumes that `foo` hasn't been declared if the declaration is invalid (because of the unknown return type)? In this case, `foo` is an undeclared function which takes arbitrary arguments (as with an unprototyped function) and returns an `int`. – fuz Jul 08 '15 at 08:50
  • Are you including the definition of `my_type` in the code you are letting clang parse? – Morten Jensen Jul 08 '15 at 08:54
  • @MortenJensen: I don't want to include the definition of my_type. Just want to leave it an unknown type. My point is that, since the parser knows it's indeed a type, although "unknown", wouldn't it be better to leave it as it is? – waiwai444 Jul 08 '15 at 15:17
  • 1
    @waiwai444 I see where you are going, but remember the Clang Parser was probably built for parsing building programs out of valid C in the first place, and for static analysis secondly. My guess is it stems from the C standard: `The default return value from a function is int. In other words, unless explicitly specified the default return value by compiler would be integer value from function.` - So when you're omitting the definition of `my_type` it defaults to `int`. – Morten Jensen Jul 08 '15 at 15:24
  • @MortenJensen: You are right. But I think it's about the semantics, not syntax. But if clang's AST is designed like this and there is no way to change this feature, I have no choice but to find other tools to achieve my goal, maybe using lex and yacc to generate my own AST. Thank you! – waiwai444 Jul 09 '15 at 03:34
  • In fact, all unknown types are replaced by "int", not only the return type. – waiwai444 Jul 09 '15 at 03:36
  • @waiwai444 I can recommend Eli Bendersky's `pycparser` - check out this related question: http://stackoverflow.com/questions/20250702/build-ast-from-c-code/20251060 in which I've given a few recommendations – Morten Jensen Jul 09 '15 at 07:34

0 Answers0