Sergejs Belajevs | 17 Aug 2012 15:23
Picon

Visiting anonymous unions

Hi,

I am writing a source-to-source transformation tool and I need a way
to check if function contains at least one anonymous union inside. My
current way is the following:

class AnonymousUnionChecker : public
clang::RecursiveASTVisitor<AnonymousUnionChecker>
{
     bool foundAnonUnion;

public:
    AnonymousUnionChecker() {}

    bool VisitRecordType(RecordType *T)
    {
        RecordDecl *D = T->getDecl();
        if (D->isUnion() && !D->getIdentifier())
        {
            foundAnonUnion = true;
            return false;
        }
        return true;
    }

    bool CanFindInside(clang::Stmt* stmt)  // for functions' bodies
    {
        foundAnonUnion = false;
        TraverseStmt(stmt);
        return foundAnonUnion;
(Continue reading)

Philip Craig | 17 Aug 2012 15:46
Picon

Re: Visiting anonymous unions

On Fri, Aug 17, 2012 at 11:23 PM, Sergejs Belajevs
<sergejs.belajevs@...> wrote:
> but it doesn't work (VisitRecordType isn't get called) for code like:
>
> void foo()
> {
>   union { int a; };
>   a = 5;
> }

Is that valid code? clang gives errors with the default options.
Sergejs Belajevs | 17 Aug 2012 15:51
Picon

Re: Visiting anonymous unions

Yes, it is valid C++ code. Clang gives errors if you try to process it
with .c extension, with .cpp all is OK on my machine.

On Fri, Aug 17, 2012 at 4:46 PM, Philip Craig <philipjcraig@...> wrote:
> On Fri, Aug 17, 2012 at 11:23 PM, Sergejs Belajevs
> <sergejs.belajevs@...> wrote:
>> but it doesn't work (VisitRecordType isn't get called) for code like:
>>
>> void foo()
>> {
>>   union { int a; };
>>   a = 5;
>> }
>
> Is that valid code? clang gives errors with the default options.
John McCall | 17 Aug 2012 21:52
Picon
Favicon

Re: Visiting anonymous unions

On Aug 17, 2012, at 6:23 AM, Sergejs Belajevs wrote:
> I am writing a source-to-source transformation tool and I need a way
> to check if function contains at least one anonymous union inside.

You should see a DeclStmt that declares a single VarDecl whose type
is a RecordType whose RecordDecl has the isAnonymousStructOrUnion()
bit set.

John.
Manuel Klimek | 17 Aug 2012 22:28
Picon
Favicon

Re: Visiting anonymous unions

On Fri, Aug 17, 2012 at 9:52 PM, John McCall <rjmccall@...> wrote:
> On Aug 17, 2012, at 6:23 AM, Sergejs Belajevs wrote:
>> I am writing a source-to-source transformation tool and I need a way
>> to check if function contains at least one anonymous union inside.
>
> You should see a DeclStmt that declares a single VarDecl whose type
> is a RecordType whose RecordDecl has the isAnonymousStructOrUnion()
> bit set.

I have played around with that a bit - I have not found a RecordDecl
within the FunctionDecl (but I have found a VarDecl) - if you say that
should be there, that might be a problem with RAV...

Cheers,
/Manuel

>
> John.
> _______________________________________________
> cfe-dev mailing list
> cfe-dev@...
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Philip Craig | 18 Aug 2012 02:18
Picon

Re: Visiting anonymous unions

On Sat, Aug 18, 2012 at 6:28 AM, Manuel Klimek <klimek@...> wrote:
> On Fri, Aug 17, 2012 at 9:52 PM, John McCall <rjmccall@...> wrote:
>> On Aug 17, 2012, at 6:23 AM, Sergejs Belajevs wrote:
>>> I am writing a source-to-source transformation tool and I need a way
>>> to check if function contains at least one anonymous union inside.
>>
>> You should see a DeclStmt that declares a single VarDecl whose type
>> is a RecordType whose RecordDecl has the isAnonymousStructOrUnion()
>> bit set.
>
> I have played around with that a bit - I have not found a RecordDecl
> within the FunctionDecl (but I have found a VarDecl) - if you say that
> should be there, that might be a problem with RAV...

I think John meant that the RecordDecl is only referenced via the
RecordType of the VarDecl. RAV currently doesn't traverse into the
RecordType though (same as in PR13618).
Sergejs Belajevs | 20 Aug 2012 11:05
Picon

Re: Visiting anonymous unions

Overriding shouldVisitImplicitCode() does the trick, thanks! However,
it seems that this RAV method appears in trunk and not in clang 3.1,
so I'll wait for 3.2 to come out and meanwhile use my token-by-token
solution.

Thanks,
Sergejs

On Sat, Aug 18, 2012 at 3:18 AM, Philip Craig <philipjcraig@...> wrote:
> On Sat, Aug 18, 2012 at 6:28 AM, Manuel Klimek <klimek@...> wrote:
>> On Fri, Aug 17, 2012 at 9:52 PM, John McCall <rjmccall@...> wrote:
>>> On Aug 17, 2012, at 6:23 AM, Sergejs Belajevs wrote:
>>>> I am writing a source-to-source transformation tool and I need a way
>>>> to check if function contains at least one anonymous union inside.
>>>
>>> You should see a DeclStmt that declares a single VarDecl whose type
>>> is a RecordType whose RecordDecl has the isAnonymousStructOrUnion()
>>> bit set.
>>
>> I have played around with that a bit - I have not found a RecordDecl
>> within the FunctionDecl (but I have found a VarDecl) - if you say that
>> should be there, that might be a problem with RAV...
>
> I think John meant that the RecordDecl is only referenced via the
> RecordType of the VarDecl. RAV currently doesn't traverse into the
> RecordType though (same as in PR13618).
> _______________________________________________
> cfe-dev mailing list
> cfe-dev@...
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
(Continue reading)

Philip Craig | 18 Aug 2012 02:15
Picon

Re: Visiting anonymous unions

On Sat, Aug 18, 2012 at 5:52 AM, John McCall <rjmccall@...> wrote:
> On Aug 17, 2012, at 6:23 AM, Sergejs Belajevs wrote:
>> I am writing a source-to-source transformation tool and I need a way
>> to check if function contains at least one anonymous union inside.
>
> You should see a DeclStmt that declares a single VarDecl whose type
> is a RecordType whose RecordDecl has the isAnonymousStructOrUnion()
> bit set.

The VarDecl is flagged as implicit, so to see it in the visitor you
need to override shouldVisitImplicitCode() to return true.

Gmane