[cfe-dev] Sema && redefinitions

Douglas Gregor dgregor at apple.com
Fri Dec 5 07:27:32 PST 2008


Hi Anton,

On Dec 5, 2008, at 4:45 AM, Anton Korobeynikov wrote:

> Hi, Douglas
>
>> Couldn't you just scan the getPreviousDeclaration() chain backward,  
>> to
>> find the first declaration of this function that contains the
>> dllimport attribute? You can complain about that declaration, then
>> remove the attribute from all of the declarations to silence any
>> further warnings.
> At this point (when we already saw the body) all attributes are  
> already
> merged thus we cannot distinguish the following two cases:
> 1. Definition originally contained attribute applied
> 2. Attribute was on prev. declaration

Attributes are only merged from earlier declarations to later  
declarations. So, for example, if we had:

void foo(); // #1
void __attribute__((dllimport)) foo(); // #2

void foo() { // #3
}

#1 doesn't have the dllimport attribute. #2 adds the dllimport  
attribute, but only to declaration #2. #2 has a getPreviousDeclaration 
() link back to #1.

At #3, we merge the dllimport from #2 into #3, so #3 has the dllimport  
attribute. #3's getPreviousDeclaration() link back to #2.

So, from the point where you hit the definition, you can walk the  
getPreviousDeclaration() chain back until you find the declaration  
that either has no previous declaration or whose previous declaration  
does not have the attribute.

>> If you've processed the whole translation unit, you could look up the
>> most recent declaration of 'foo'
> How?

Hrm. Actually, this is tougher than expected. I guess you could look  
through the declarations in the translation unit and then perform name  
lookup (e.g., via Sema::LookupDecl) on the names of each to get the  
most recent declaration. Still, it's messy; I hope that the scheme  
described above works out for you, because I think it's far simpler.

   - Doug



More information about the cfe-dev mailing list