How to know in what CXXRecordDecl is a Stmt?

Hello,

When I retrieve a Stmt in the AST, how can I know the CXXRecordDel to which that Stmt belongs to? I have been looking over the methods in the Stmt class, but I couldn’t find an appropriate method to this end.

Thanks in advance.

ASTContext (http://clang.llvm.org/doxygen/classclang_1_1ASTContext.html) has a getParents() method. Note that you’ll not always end up in a CXXRecordDecl - sometimes you’ll also end up in a method decl that is defined out-of-line.

Oh, thanks, I didn’t know that method.

Note that you’ll not always end up in a CXXRecordDecl - sometimes you’ll also end up in a method decl that is defined out-of-line.

Ok, so I will go through the ParentVector and, if finding a method decl, I will get the CXXRecordDecl of that method with getParent() in CXXMethodDecl, is this right?

BTW, Isn’t there a getChildren in ASTContext for the contrary purpose? I was wondering the reason.

Thanks.

Oh, thanks, I didn't know that method.

Note that you'll not always end up in a CXXRecordDecl - sometimes you'll
also end up in a method decl that is defined out-of-line.

Ok, so I will go through the ParentVector and, if finding a method decl, I
will get the CXXRecordDecl of that method with getParent() in
CXXMethodDecl, is this right?

Well, as I said, not necessarily - you will definitely find a
CXXMethodDecl, but from there you have to go to its CXXRecordDecl directly,
not via getParents(), as it might be an out-of-line definition.

BTW, Isn't there a getChildren in ASTContext for the contrary purpose? I
was wondering the reason.

No, you get the children directly from the AST nodes.

Well, as I said, not necessarily - you will definitely find a CXXMethodDecl, but from there you have to go to its CXXRecordDecl directly, not via getParents(), as it might be an out-of-line definition.

Ok, that’s much clearer. I hope I can implement it without problems.

No, you get the children directly from the AST nodes.

Sorry if my question is too trivial, but I am not so expert using these libraries. I can find children in Stmt:
http://clang.llvm.org/doxygen/classclang_1_1Stmt.html#a489148e5d234a5a7f00d8b02981bcee5

but I couldn’t find it in Decl. What is the way in this case?

Well, as I said, not necessarily - you will definitely find a
CXXMethodDecl, but from there you have to go to its CXXRecordDecl directly,
not via getParents(), as it might be an out-of-line definition.

Ok, that's much clearer. I hope I can implement it without problems.

No, you get the children directly from the AST nodes.

Sorry if my question is too trivial, but I am not so expert using these
libraries. I can find children in Stmt:

clang: clang::Stmt Class Reference

but I couldn't find it in Decl. What is the way in this case?

You can go from the DeclContext:
http://clang.llvm.org/doxygen/classclang_1_1DeclContext.html
But generally, you'll want to use the AST matchers, or the
RecursiveASTVisitor if you want to traverse the ast...

I’m having a problem and the information is not very enlightening:

/usr/local/include/llvm/ADT/SmallPtrSet.h:190: const PtrTy llvm::SmallPtrSetIterator<const clang::CXXRecordDecl >::operator() const [PtrTy = const clang::CXXRecordDecl *]: Assertion `Bucket < End’ failed.
Stack dump:

Have you ever bumped into this error? The code of the method that gives the error is:

[00192](http://llvm.org/docs/doxygen/html/classllvm_1_1SmallPtrSetIterator.html#a1730fbaa5cd997f7f94cfab313efc001) const PtrTy [operator*](http://llvm.org/docs/doxygen/html/classllvm_1_1SmallPtrSetIterator.html#a1730fbaa5cd997f7f94cfab313efc001)() const {
00193     assert([Bucket](http://llvm.org/docs/doxygen/html/classllvm_1_1SmallPtrSetIteratorImpl.html#ab01497ce0bb39908edbb877194c45b78) < [End](http://llvm.org/docs/doxygen/html/classllvm_1_1SmallPtrSetIteratorImpl.html#a8cdc03b6ca7233dddb05aa44af1f4c1e));
00194     return PtrTraits::getFromVoidPointer(const_cast<void*>(*[Bucket](http://llvm.org/docs/doxygen/html/classllvm_1_1SmallPtrSetIteratorImpl.html#ab01497ce0bb39908edbb877194c45b78)));
00195   }

http://llvm.org/docs/doxygen/html/SmallPtrSet_8h_source.html#l00183

I’m not sure where in the code this error occurs, but anyway, is this the correct way to use the method getParents? This gets all kind of nodes, not only Stmt, right?

clang::ASTContext::ParentVector pv = Context->getParents(*m);

Thanks.

Nope, I’ve never run into that - the idea is usually to gdb it in debug mode to figure out why this triggers.
Or, as I’ve mentioned, use the ASTMatchers with hasAncestor / hasParent.