[cfe-dev] Getting the FieldDecl from a CXXDependentScopeMemberExpr

Adrien Chauve adrien.chauve at gmail.com
Thu Jun 23 10:10:23 PDT 2011


On Thu, Jun 23, 2011 at 17:24, Douglas Gregor <dgregor at apple.com> wrote:
>
> On Jun 22, 2011, at 11:49 PM, Adrien Chauve wrote:
>
>> On Wed, Jun 22, 2011 at 20:26, Douglas Gregor <dgregor at apple.com> wrote:
>>>
>>> On Jun 22, 2011, at 10:05 AM, Adrien Chauve wrote:
>>>
>>>> Thanks a lot!
>>>>
>>>> If I understand well, the AST is of little help for dependent names
>>>> inside template classes.
>>>
>>> Correct. The language is of little help here, either, since it's a fundamental part of the C++ template model.
>>>
>>>> Is there any tool to deal with it? Without instanciation, can we do
>>>> anything with class templates?
>>>
>>>
>>> Not really. You could guess by looking into the primary templates, but the answers you get will be wrong if any specializations come along, and you may not even be able to get enough type information to determine which primary template to look in.
>>>
>>>        - Doug
>>>
>>
>> Thanks!
>>
>> By any chance, would it be possible to try and instantiate a primary
>> template with a list of dumb template parameter types (say ints), skip
>> it if it fails, and if there is no specialization and the instatiation
>> succeeds, draw some conclusions on the primary template?
>
>
> There's no infrastructure for doing this, but it should be possible. However, I don't think you'd want to instantiate with ints… rather, you'd want to instantiate with some dummy type that doesn't imply any specific behavior.
>
> However, it depends on your goals. It might just be easier to inspect the contents of the primary template directly, since there's a full AST there anyway.
>
>        - Doug


My goals are very simple for now: I would like to have a tool to
rename all fields contained in the source code of a given project
(then of course classes, functions, etc...) according to the naming
conventions chosen for the project.

For instance, I would like to rename:

    template<typename T>
    struct Foo
    {
        std::pair<T,T> bar;

        void set(T x, T y)
        {
            bar.first = x;
            bar.second = y;
        }
    };

into:

    template<typename T>
    struct Foo
    {
        std::pair<T,T> m_bar;  // bar -> m_bar

        void set(T x, T y)
        {
            m_bar.first = x;   // bar -> m_bar but not first -> m_first
            m_bar.second = y;
        }
    };


I use VisitCXXDependentScopeMemberExpr but from that I get only the
name of the member and its location.
As std::pair<T,T> is a dependent name, I can't get access to its
primary class template, whose location is not inside the project root
directory so, of course, I don't want to rename its fields "first" and
"second", but I do want to rename "bar".

Is there a way to get this kind of information using the AST of the
primary template Foo without instantiating it?

Another piece of information I would need is to know if a given
CXXDependentScopeMemberExpr refers to a field or a method. This
information lies in the AST since if the parent expression is a
CallExpr it's a method, otherwise it's a field. But I'm not sure there
is a simple way to get the parent of a given expr (I only saw
iterators on children expr), otherwise I suppose you have to hack the
traversal of the AST and store some information on the traversed
nodes.


Thanks,
Adrien




More information about the cfe-dev mailing list