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

Abramo Bagnara abramo.bagnara at gmail.com
Sat May 29 15:18:27 PDT 2010


Il 26/05/2010 23:36, John McCall ha scritto:
> On May 26, 2010, at 12:49 PM, Eli Friedman wrote:
>> 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".
> 
> I agree with Abramo that the semantic DC here should be the translation unit.
> There's probably a non-trivial amount of code assuming otherwise, but that
> code can be fixed.

I've attached a candidate incomplete patch for your review and comments.

The semantic DC of an extern local variable I've assumed is either the
translation unit or the first linkage specification ancestor or the
first namespace ancestor. In this last case if there is a C linkage
specification as ancestor of the namespace this node is used instead of
the namespace.

Probably I'd have preferred to have the TranslationUnit as semantic DC
for extern C VarDecl (so to have always the same semantic DC for every
declaration of the same variable) and to never have linkage specificaton
as semantic DC, but this would break implementation of
VarDecl::isExternC() that is based on semantic DC and I've not seen an
alternative way to implement it.

The patch passes all tests, but:

Failing Tests (3):
    Clang :: CodeGenCXX/mangle.cpp
    Clang :: Index/usrs.m
    Clang :: Sema/vla.c

The failure in CodeGenCXX/mangle.cpp is related to differences between C
and C++ about this kind of code (illegal in C while legal in C++)

int f() {
  extern int i;
  return i;
}

static int i;

I've failed to understand the failure in Index/usrs.m probably because
my relative ignorance about Objective C

The failure in Sema/vla.c is due to modified diagnostic

error: 'error' diagnostics expected but not seen:
  Line 22: variable length array declaration can not have 'extern' linkage
  Line 38: variable length array declaration can not have 'extern' linkage
  Line 40: variably modified type declaration can not have 'extern' linkage
error: 'error' diagnostics seen but not expected:
  Line 22: variable length array declaration not allowed at file scope
  Line 38: variable length array declaration not allowed at file scope
  Line 40: variably modified type declaration not allowed at file scope

but I think to prefer the new one to the old.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: LocalExternDeclContext.patch
Type: text/x-patch
Size: 4732 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20100530/34629105/attachment.bin>


More information about the cfe-dev mailing list