query AST for C++ method qualifiers

Hi!

How can I correctly determine the qualifiers used in the declaration of
a 'CXXMethodDecl'?

I found
'CXXMethodDecl::isStatic()'
'CXXMethodDecl::isVirtual()' and
'FunctionDecl::isVirtualAsWritten()'
to work just fine.

For 'const' and 'volatile' I use 'CXXMethodDecl::getThisType()'
and then check if the type 'isLocalConstQualified()' or
'isLocalVolatileQualified()'. Confusingly I *always* get 'false' from
these two method calls, but according to getThisType's source code
comment it's exactly the method I want to use.

Attached are a small test file, the ASTConsumer code I use for testing
and the output I get. Any idea what might be wrong here?

Regards, Jan.

consumer.cpp (1 KB)

output (559 Bytes)

test.cpp (134 Bytes)

The 'this' type is a pointer type; you would want the qualifiers on its pointee type. But what you actually want are the qualifiers on the function type itself.

Also, you should generally not be using 'isLocalX', because it doesn't look through type sugar (like a typedef), which it is possible to declare (but not define) a method with.

John.

John's advice is correct w.r.t the result of getThisType() and the use of the cv-qualification checks. However, there's a simpler way for this specific task: CXXMethodDecl::getTypeQualifiers() gives you a bitmask of the const and volatile qualifiers.

  - Doug

John McCall meinte am 12.09.2010 22:23:

The 'this' type is a pointer type; you would want the qualifiers on
its pointee type. But what you actually want are the qualifiers on the
function type itself.

Thanks to you and Doug, it works now. Actually, as I figured out, I want
not only the qualifiers on the function type, but also the ones on its
return type. It must have slipped my mind that these are separate.

BTW: While trying around with this and Clang's AST printing I noticed
that both 'volatile' and 'const' methods are not handled correctly by it
too - the qualifiers are just omitted. I already tried a simple fix, but
it didn't work.

Regards, Jan.