[cfe-dev] Doubt about Decl::getDeclContext()

Abramo Bagnara abramo.bagnara at gmail.com
Wed May 26 13:43:18 PDT 2010


Il 26/05/2010 21:49, Eli Friedman ha scritto:
> On Wed, May 26, 2010 at 4:55 AM, Abramo Bagnara
> <abramo.bagnara at gmail.com> wrote:
>>
>> I've just noted that unexpectedly (at least for me) getDeclContext()
>> applied to an extern VarDecl returns the FunctionDecl instead of the
>> TranslationUnitDecl:
>>
>> int f() {
>>  extern int a;
>> }
>>
>> Is this expected or a symptom of a bug?
> 
> Expected; even though "a" is a global in the sense of linkage, it
> isn't actually part of the global scope.  It is only visible within
> the function "f".

This means that in this source:

int f() {
  extern int a;
}
int a;

for the two declarations getDeclContext() returns two different results
and that D->getCanonicalDecl()->getDeclContext() could return a
different result from D->getDeclContext().

But once understood that the so called "semantic" context is the
"lookup" context everything might be fine, *but* if I extend your
definition above I don't understand why a block scoped variable has the
full function as its Decl Context, also if it is only visible inside its
block)...

That apart, do we have a way to get the declaration context of the
declared entity i.e. something that is consistent for each redeclaration
and refers to the parent scope of the declared entity?




More information about the cfe-dev mailing list