How to get typedef name?

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

How to get typedef name?

Kristof Beyls via cfe-dev
How can I get the underlying typedef name for the parameters of
a FunctionDecl?  

The following gives the resolved typedef name, but I want the typedef
name.


func->parameters()[i]->getType().getAsString();


Thanks,
Kenth


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to get typedef name?

Kristof Beyls via cfe-dev
Hi Kenth,

On Thu, Oct 31, 2019 at 03:41:22PM +0000, Kenth Eriksson via cfe-dev <[hidden email]> wrote:
> How can I get the underlying typedef name for the parameters of
> a FunctionDecl?  
>
> The following gives the resolved typedef name, but I want the typedef
> name.
>
> func->parameters()[i]->getType().getAsString();

Care to share a short, self-contained example?

I just tried this on a function that takes an std::string (which is
typically a typedef) and I got std::string by default.

If you want to get the "desugared" type, then you would need to dyn_cast
to clang::TypedefType, then desugar().getAsString().

Regards,

Miklos

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

signature.asc (201 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: How to get typedef name?

Kristof Beyls via cfe-dev
In reply to this post by Kristof Beyls via cfe-dev
On Thu, 2019-11-03 at 12:53, Miklos Vajna wrote:

> How can I get the underlying typedef name for the parameters of
> a FunctionDecl?  
>
> The following gives the resolved typedef name, but I want the typedef
> name.
>
>
> func->parameters()[i]->getType().getAsString();
>
>
> Care to share a short, self-contained example?

typedef int my_int_t;

int my_func(my_int_t i);

I would like to get the the typedef name for parameter 0 in the
function decl. I don't want to desugar it, only get the typedef name.





_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to get typedef name?

Kristof Beyls via cfe-dev
Hi Kenth,

On Tue, Nov 05, 2019 at 09:28:42AM +0000, Kenth Eriksson via cfe-dev <[hidden email]> wrote:
> > Care to share a short, self-contained example?
>
> typedef int my_int_t;
>
> int my_func(my_int_t i);

And what code do you use to parse this?

> I would like to get the the typedef name for parameter 0 in the
> function decl. I don't want to desugar it, only get the typedef name.

That's what you get by default if you match for e.g. functionDecl() and
have a callback like this:

----
class Callback : public clang::ast_matchers::MatchFinder::MatchCallback
{
  public:
    Callback() {}

    void
    run(const clang::ast_matchers::MatchFinder::MatchResult& result) override
    {
        const auto decl = result.Nodes.getNodeAs<clang::FunctionDecl>("decl");
        if (!decl)
        {
            return;
        }

        std::cerr << decl->parameters()[0]->getType().getAsString() << std::endl;
    }
};
----

This prints "my_int_t" for me with your above 2 liner input.

Regards,

Miklos

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

signature.asc (201 bytes) Download Attachment