Type Information for C++ classes

Hi,
   I am trying to find the type of a VarDecl. I am currently using the
VarDecl->getOriginalType(). This function does not work for C++ classes.
Consider the code below:

class Foo
{
    int a;
};

int f( Foo foo);

When I try to find the type for "foo" in the function decleration, it
returns "int", while the actual type is Foo. If Foo is a structure, this
works perfectly fine but not for C++ classes. Can some one please explain
where the type information for C++ classes is stored?

Hi,
   I am trying to find the type of a VarDecl. I am currently using the
VarDecl->getOriginalType(). This function does not work for C++ classes.
Consider the code below:

Does getType() not work for you for some reason?

Cheers,
/Manuel

+cfe-dev

My purpose is to overload the << operator for all classes if they don’t already have one. The basic goal is to make all classes serializable over some stream (ideally a network stream). Is that possible with clang? If yes, How do I find all the member variables and functions of a class or struct?

Also, Is it better to use libclang for doing this. My complete purpose is to refactor source code to find all functions, their arguments, return statements, all class members and methods. Can I find all this using libclang? I’ve heard that clang is unstable while libclang is stable. Which one should I use? I am currently using clang’s internal API.

Also, Is it better to use libclang for doing this. My complete purpose is
to refactor source code to find all functions, their arguments, return
statements, all class members and methods. Can I find all this using
libclang? I've heard that clang is unstable while libclang is stable. Which
one should I use? I am currently using clang's internal API.

http://clang.llvm.org/docs/Tooling.html

I don't have too much experience with using libclang for something like
this; what you want to do is definitely possible with LibTooling. And while
the internal clang AST interface is going to change more often than
libclang, in practice it is stable enough that you usually don't care
unless you want to ship a product (like an IDE) to end users. I'd try to
decide more on how much power you need, or what language you'd like to
write your integration in.

One good way is to try to start with libclang, and if you see that you're
hitting a missing feature, switch to using libtooling.

Cheers,
/Manuel

I am comfortable with clang’s internal API at the time, although the abstraction provided in libclang seems much easier to work with. The clang’s internal API is much more self-explanatory than libclang. Can you please give me some hints as for how to find the members and methods of a class. I can visit all classes using VisitCXXRecordDecl but there doesn’t seem to be any way to find it’s members. Is there a way to recursively walk all the children this CXXRecordDecl and figure out which ones are methods and members? Also, I am struggling on how to use the ASTMatcher you mentioned earlier. I have a returnstmt using the VisitStmt method of my visitor. This will give me a return statement. How do I find which function it belongs to? Can you give a small example of how to do it? I am very grateful for your help.

Is this what you’re looking for?
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/tool-template/ToolTemplate.cpp?revision=162624&view=markup

I am analyzing an AST with clang and I have a code which uses “FILE” type. Clang reports FILE type as int rather than FILE. I need to distinguish between FILE and int because both of them behave differently with streams. Is there a way I can distinguish between these two in clang?

Presumably, you’ll see a TypedefType for FILE, whose underlying type is ‘int’.

  • Doug

Unfortunately not. I am just seeing an int. That is because the header files which contain type information for FILE namely <stdio.h> is not found by the program. Is there a way to include default C++ paths using the CompilerInstance? I want to add these paths in a re-writing program.

Unfortunately not. I am just seeing an int. That is because the header files which contain type information for FILE namely <stdio.h> is not found by the program.

Oh! Clang is just falling back to ‘int’ as part of it’s recovery. You aren’t likely to get good results.

Is there a way to include default C++ paths using the CompilerInstance? I want to add these paths in a re-writing program.

If you use the tooling infrastructure (http://clang.llvm.org/docs/LibTooling.html), it handles default C++ paths for you. It’s far, far better than trying to construct a CompilerInstance yourself.

  • Doug