How to get the namespace name from a clang::CXXRecordDecl?

I’m using an AST matcher to get some clang::CXXRecordDecls, and now I want to get the namespace of that record, but I searched all the apis and cannot find one.

P.S. I dumped the record and found that there’s no information about namespaces, does that mean I have to handle finding namespaces as well? If that’s the case, an AST matcher seems not very helpful since I have to go through the source code anyway.

such information is recorded in the DeclContext chain, reachable via Decl::getDeclContext

Hi Stephan, I’m sorry I tried some combinations of APIs in getDeclContext, which are related to namespace, but failed, can you show me some examples or related documents?

Take for example the definition of Decl::isInAnonymousNamespace in clang/lib/AST/DeclBase.cpp. That should give you an idea.

It works, thanks so much!