query AST for C++ method qualifiers

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

query AST for C++ method qualifiers

Jan Bierbaum-2
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.

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

consumer.cpp (1K) Download Attachment
output (599 bytes) Download Attachment
test.cpp (143 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: query AST for C++ method qualifiers

John McCall

On Sep 12, 2010, at 8:21 AM, Jan Bierbaum wrote:

> 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?

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.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: query AST for C++ method qualifiers

Douglas Gregor
In reply to this post by Jan Bierbaum-2

On Sep 12, 2010, at 8:21 AM, Jan Bierbaum wrote:

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


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
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: query AST for C++ method qualifiers

Jan Bierbaum-2
In reply to this post by John McCall
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.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev