Skip to content

Commit

Permalink
Revert "[Sema] Fix handling of functions that hide classes"
Browse files Browse the repository at this point in the history
This reverts commit d031ff3.
See https://reviews.llvm.org/D154503#4576393 for a reproducer and
details.
  • Loading branch information
kadircet committed Aug 10, 2023
1 parent 3dd5833 commit 7d259b3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 409 deletions.
68 changes: 32 additions & 36 deletions clang/lib/Sema/SemaLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,42 +514,21 @@ void LookupResult::resolveKind() {
const NamedDecl *HasNonFunction = nullptr;

llvm::SmallVector<const NamedDecl *, 4> EquivalentNonFunctions;
llvm::BitVector RemovedDecls(N);

for (unsigned I = 0; I < N; I++) {
unsigned UniqueTagIndex = 0;

unsigned I = 0;
while (I < N) {
const NamedDecl *D = Decls[I]->getUnderlyingDecl();
D = cast<NamedDecl>(D->getCanonicalDecl());

// Ignore an invalid declaration unless it's the only one left.
// Also ignore HLSLBufferDecl which not have name conflict with other Decls.
if ((D->isInvalidDecl() || isa<HLSLBufferDecl>(D)) &&
N - RemovedDecls.count() > 1) {
RemovedDecls.set(I);
if ((D->isInvalidDecl() || isa<HLSLBufferDecl>(D)) && !(I == 0 && N == 1)) {
Decls[I] = Decls[--N];
continue;
}

// C++ [basic.scope.hiding]p2:
// A class name or enumeration name can be hidden by the name of
// an object, function, or enumerator declared in the same
// scope. If a class or enumeration name and an object, function,
// or enumerator are declared in the same scope (in any order)
// with the same name, the class or enumeration name is hidden
// wherever the object, function, or enumerator name is visible.
if (HideTags && isa<TagDecl>(D)) {
bool Hidden = false;
for (auto *OtherDecl : Decls) {
if (canHideTag(OtherDecl) &&
getContextForScopeMatching(OtherDecl)->Equals(
getContextForScopeMatching(Decls[I]))) {
RemovedDecls.set(I);
Hidden = true;
break;
}
}
if (Hidden)
continue;
}

std::optional<unsigned> ExistingI;

// Redeclarations of types via typedef can occur both within a scope
Expand Down Expand Up @@ -582,7 +561,7 @@ void LookupResult::resolveKind() {
if (isPreferredLookupResult(getSema(), getLookupKind(), Decls[I],
Decls[*ExistingI]))
Decls[*ExistingI] = Decls[I];
RemovedDecls.set(I);
Decls[I] = Decls[--N];
continue;
}

Expand All @@ -593,6 +572,7 @@ void LookupResult::resolveKind() {
} else if (isa<TagDecl>(D)) {
if (HasTag)
Ambiguous = true;
UniqueTagIndex = I;
HasTag = true;
} else if (isa<FunctionTemplateDecl>(D)) {
HasFunction = true;
Expand All @@ -608,7 +588,7 @@ void LookupResult::resolveKind() {
if (getSema().isEquivalentInternalLinkageDeclaration(HasNonFunction,
D)) {
EquivalentNonFunctions.push_back(D);
RemovedDecls.set(I);
Decls[I] = Decls[--N];
continue;
}
if (D->isPlaceholderVar(getSema().getLangOpts()) &&
Expand All @@ -620,6 +600,28 @@ void LookupResult::resolveKind() {
}
HasNonFunction = D;
}
I++;
}

// C++ [basic.scope.hiding]p2:
// A class name or enumeration name can be hidden by the name of
// an object, function, or enumerator declared in the same
// scope. If a class or enumeration name and an object, function,
// or enumerator are declared in the same scope (in any order)
// with the same name, the class or enumeration name is hidden
// wherever the object, function, or enumerator name is visible.
// But it's still an error if there are distinct tag types found,
// even if they're not visible. (ref?)
if (N > 1 && HideTags && HasTag && !Ambiguous &&
(HasFunction || HasNonFunction || HasUnresolved)) {
const NamedDecl *OtherDecl = Decls[UniqueTagIndex ? 0 : N - 1];
if (isa<TagDecl>(Decls[UniqueTagIndex]->getUnderlyingDecl()) &&
getContextForScopeMatching(Decls[UniqueTagIndex])->Equals(
getContextForScopeMatching(OtherDecl)) &&
canHideTag(OtherDecl))
Decls[UniqueTagIndex] = Decls[--N];
else
Ambiguous = true;
}

// FIXME: This diagnostic should really be delayed until we're done with
Expand All @@ -628,15 +630,9 @@ void LookupResult::resolveKind() {
getSema().diagnoseEquivalentInternalLinkageDeclarations(
getNameLoc(), HasNonFunction, EquivalentNonFunctions);

// Remove decls by replacing them with decls from the end (which
// means that we need to iterate from the end) and then truncating
// to the new size.
for (int I = RemovedDecls.find_last(); I >= 0; I = RemovedDecls.find_prev(I))
Decls[I] = Decls[--N];
Decls.truncate(N);

if ((HasNonFunction && (HasFunction || HasUnresolved)) ||
(HideTags && HasTag && (HasFunction || HasNonFunction || HasUnresolved)))
if (HasNonFunction && (HasFunction || HasUnresolved))
Ambiguous = true;

if (Ambiguous && ReferenceToPlaceHolderVariable)
Expand Down

0 comments on commit 7d259b3

Please sign in to comment.