Clang’s AST parser treats unknown type name as int

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).


Source: c++

Leave a Reply