This is an archive of the discontinued LLVM Phabricator instance.

WIP: [clang][modules] Do not report declarations without linkage as ambiguous
Needs ReviewPublic

Authored by jansvoboda11 on Mar 11 2022, 7:54 AM.

Details

Reviewers
Bigcheese
vsapsai
Summary

When finalizing the result of name lookup that encountered ambiguity, we check equivalence for declarations with internal linkage. If the declarations are equivalent, we just produce a warning instead of ambiguous result.

In Sema::isEquivalentInternalLinkageDeclaration that implements the check, we have a special case for constants of anonymous enums. However, it only kicks in for C++ anonymous enums (since they have NoLinkage, meaning they are not externally visible).

In (Objective)C, constants of anonymous enums are VisibleNoLinkage, meaning they _are_ externally visible, so the special case doesn't apply for them.

This patch renames the function to isEquivalentNonExternalLinkageDeclaration (its documentation already says it handles declarations with "internal/no linkage") and makes it so that even VisibleNoLinkage declarations are not marked as ambiguous. This is achieved by using hasExternalFormalLinkage instead of isExternallyVisible in one of the equivalence checks. Later on, we don't even emit the warning for such declarations, since they are considered the same entity.

Diff Detail

Event Timeline

jansvoboda11 created this revision.Mar 11 2022, 7:54 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 11 2022, 7:54 AM
jansvoboda11 requested review of this revision.Mar 11 2022, 7:54 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 11 2022, 7:54 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

My concern with this approach is I'm not sure if isEquivalentInternalLinkageDeclaration is a desired long-term solution for anonymous C++ enums. If it is, we can piggy-back on it, otherwise hitching our wagon to this train might be undesirable.

Also I need to check the patch again because NonExternal != Internal is not easy for my comprehension.

clang/test/Modules/ambiguous-anonymous-enum-lookup.m.cpp
10

For enums it is nice to test having a constant that references another constant in the same enum. E.g., kAnonymousEnumAnotherValue = kAnonymousEnumValue + 1.

39

Should we test also non-equivalent enums or is it tested somewhere else?

jansvoboda11 marked 2 inline comments as done.May 6 2022, 5:13 AM

My concern with this approach is I'm not sure if isEquivalentInternalLinkageDeclaration is a desired long-term solution for anonymous C++ enums.

Is there anything suggesting it's not?

clang/test/Modules/ambiguous-anonymous-enum-lookup.m.cpp
39

I believe that's tested in redefinition-c-tagtypes.m.

jansvoboda11 marked an inline comment as done.

Refer to enum constant while defining another