This is an archive of the discontinued LLVM Phabricator instance.

Don't consider `LinkageSpec` when calculating DeclContext `Encloses`
ClosedPublic

Authored by erichkeane on Nov 11 2021, 1:11 PM.

Details

Summary

We don't properly handle lookup through using declarations when there is
a linkage spec in the common chain. This is because CppLookupName and
CppNamespaceLookup end up skipping LinkageSpec's (correctly, as they
are not lookup scopes), but the UnqualUsingDirectiveSet does not.

I discovered that when we are calculating the CommonAncestor for a
using-directive, we were coming up with the LinkageSpec, instead of
the LinkageSpec's parent. Then, when we use
UnqualUsingDirectiveSet::getNamespacesFor a scope, we don't end up
finding any that were in the LinkageSpec (again, since CppLookupName
skips linkage specs), so those don't end up participating in the lookup.

The function UnqualUsingDirectiveSet::addUsingDirective calculates
this common ancestor via a loop through the the DeclSpec::Encloses
function.

Changing this Encloses function to believe that a LinkageSpec
Enclosesnothing ends up fixing the problem without breaking any other tests,
so I opted to do that. A less aggressive patch could perhaps change only
the addUsingDirective, but my examination of all uses of Encloses
showedthat it seems to be used exclusively in lookup, which makes me think
this is correct everywhere.

Diff Detail

Event Timeline

erichkeane requested review of this revision.Nov 11 2021, 1:11 PM
erichkeane created this revision.
rjmccall accepted this revision.Nov 18 2021, 8:48 AM

We don't properly handle lookup through using declarations when there is a linkage spec in the common chain.

Pedantic note: you mean using *directives*.

At some point, we should probably reconsider whether extern "C" ought to be a DeclContext at all, as opposed to ultimately just setting a bit on the decls and providing some way of recovering the source structure. We don't make individual local scopes different DCs, and those probably have more right to be one than LSD.

Anyway, this patch seems acceptable.

clang/lib/AST/DeclBase.cpp
1215

!isa<LinkageSpecDecl>(DC), please

This revision is now accepted and ready to land.Nov 18 2021, 8:48 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 29 2021, 6:24 AM