-
Notifications
You must be signed in to change notification settings - Fork 15.5k
[AST] Support structural equivalence checking of attributes on Decls #168769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ahatanak
wants to merge
3
commits into
llvm:main
Choose a base branch
from
ahatanak:structural-equivalence-attr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -93,6 +93,7 @@ | |
| #include "llvm/Support/ErrorHandling.h" | ||
| #include <cassert> | ||
| #include <optional> | ||
| #include <set> | ||
| #include <utility> | ||
|
|
||
| using namespace clang; | ||
|
|
@@ -451,6 +452,119 @@ class StmtComparer { | |
| }; | ||
| } // namespace | ||
|
|
||
| namespace { | ||
| enum class AttrComparisonKind { Equal, NotEqual }; | ||
|
|
||
| /// Represents the result of comparing the attribute sets on two decls. If the | ||
| /// sets are incompatible, A1/A2 point to the offending attributes. | ||
| struct AttrComparisonResult { | ||
| AttrComparisonKind Kind = AttrComparisonKind::Equal; | ||
| const Attr *A1 = nullptr, *A2 = nullptr; | ||
| }; | ||
| } // namespace | ||
|
|
||
| static AttrComparisonResult | ||
| areAvailabilityAttrsEqual(const AvailabilityAttr *A1, | ||
| const AvailabilityAttr *A2) { | ||
| if (A1->getPlatform() == A2->getPlatform() && | ||
| A1->getIntroduced() == A2->getIntroduced() && | ||
| A1->getDeprecated() == A2->getDeprecated() && | ||
| A1->getObsoleted() == A2->getObsoleted() && | ||
| A1->getUnavailable() == A2->getUnavailable() && | ||
| A1->getMessage() == A2->getMessage() && | ||
| A1->getReplacement() == A2->getReplacement() && | ||
| A1->getStrict() == A2->getStrict() && | ||
| A1->getPriority() == A2->getPriority() && | ||
| A1->getEnvironment() == A2->getEnvironment()) | ||
| return {AttrComparisonKind::Equal}; | ||
| return {AttrComparisonKind::NotEqual, A1, A2}; | ||
| } | ||
|
|
||
| static AttrComparisonResult | ||
| areEnumExtensibilityAttrsEqual(const EnumExtensibilityAttr *A1, | ||
| const EnumExtensibilityAttr *A2) { | ||
| if (A1->getExtensibility() == A2->getExtensibility()) | ||
| return {AttrComparisonKind::Equal}; | ||
| return {AttrComparisonKind::NotEqual, A1, A2}; | ||
| } | ||
|
|
||
| static AttrComparisonResult areAttrsEqual(const Attr *A1, const Attr *A2) { | ||
| auto Kind1 = A1->getKind(), Kind2 = A2->getKind(); | ||
| if (Kind1 != Kind2) | ||
| return {AttrComparisonKind::NotEqual, A1, A2}; | ||
|
|
||
| switch (Kind1) { | ||
| case attr::Availability: | ||
| return areAvailabilityAttrsEqual(cast<AvailabilityAttr>(A1), | ||
| cast<AvailabilityAttr>(A2)); | ||
| case attr::EnumExtensibility: | ||
| return areEnumExtensibilityAttrsEqual(cast<EnumExtensibilityAttr>(A1), | ||
| cast<EnumExtensibilityAttr>(A2)); | ||
| case attr::Unused: | ||
| return {AttrComparisonKind::Equal}; | ||
| default: | ||
| llvm_unreachable("unexpected attr kind"); | ||
| } | ||
| } | ||
|
|
||
| static bool compareAttrKind(const Attr *A1, const Attr *A2) { | ||
| return A1->getKind() < A2->getKind(); | ||
| } | ||
|
|
||
| namespace { | ||
| using AttrSet = std::multiset<const Attr *, decltype(&compareAttrKind)>; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would a |
||
| } | ||
|
|
||
| /// Collects all supported, non-inherited attributes from the given decl. | ||
| /// If the decl doesn't contain any unsupported attributes, returns a nullptr, | ||
| /// otherwise returns the first unsupported attribute. | ||
| static const Attr *collectComparableAttrs(const Decl *D, AttrSet &Attrs) { | ||
| for (const Attr *A : D->attrs()) { | ||
| switch (A->getKind()) { | ||
| case attr::Availability: | ||
| case attr::EnumExtensibility: | ||
| case attr::Unused: | ||
| if (!A->isInherited()) | ||
| Attrs.insert(A); | ||
| break; | ||
| default: | ||
| return A; // unsupported attribute | ||
| } | ||
| } | ||
|
|
||
| return nullptr; | ||
| } | ||
|
|
||
| /// Determines whether D1 and D2 have compatible sets of attributes for the | ||
| /// purposes of structural equivalence checking. | ||
| static AttrComparisonResult areDeclAttrsEquivalent(const Decl *D1, | ||
| const Decl *D2) { | ||
| if (D1->isImplicit() || D2->isImplicit()) | ||
| return {AttrComparisonKind::Equal}; | ||
|
|
||
| AttrSet A1(&compareAttrKind), A2(&compareAttrKind); | ||
|
|
||
| const Attr *UnsupportedAttr1 = collectComparableAttrs(D1, A1); | ||
| const Attr *UnsupportedAttr2 = collectComparableAttrs(D2, A2); | ||
|
|
||
| if (UnsupportedAttr1 || UnsupportedAttr2) | ||
| return {AttrComparisonKind::NotEqual, UnsupportedAttr1, UnsupportedAttr2}; | ||
|
|
||
| auto I1 = A1.begin(), E1 = A1.end(), I2 = A2.begin(), E2 = A2.end(); | ||
| for (; I1 != E1 && I2 != E2; ++I1, ++I2) { | ||
| AttrComparisonResult R = areAttrsEqual(*I1, *I2); | ||
| if (R.Kind != AttrComparisonKind::Equal) | ||
| return R; | ||
| } | ||
|
|
||
| if (I1 != E1) | ||
| return {AttrComparisonKind::NotEqual, *I1}; | ||
| if (I2 != E2) | ||
| return {AttrComparisonKind::NotEqual, nullptr, *I2}; | ||
|
|
||
| return {AttrComparisonKind::Equal}; | ||
| } | ||
|
|
||
| static bool | ||
| CheckStructurallyEquivalentAttributes(StructuralEquivalenceContext &Context, | ||
| const Decl *D1, const Decl *D2, | ||
|
|
@@ -465,21 +579,17 @@ CheckStructurallyEquivalentAttributes(StructuralEquivalenceContext &Context, | |
| // the same semantic attribute, differences in attribute arguments, order | ||
| // in which attributes are applied, how to merge attributes if the types are | ||
| // structurally equivalent, etc. | ||
| const Attr *D1Attr = nullptr, *D2Attr = nullptr; | ||
| if (D1->hasAttrs()) | ||
| D1Attr = *D1->getAttrs().begin(); | ||
| if (D2->hasAttrs()) | ||
| D2Attr = *D2->getAttrs().begin(); | ||
| if ((D1Attr || D2Attr) && !D1->isImplicit() && !D2->isImplicit()) { | ||
| AttrComparisonResult R = areDeclAttrsEquivalent(D1, D2); | ||
| if (R.Kind != AttrComparisonKind::Equal) { | ||
| const auto *DiagnoseDecl = cast<TypeDecl>(PrimaryDecl ? PrimaryDecl : D2); | ||
| Context.Diag2(DiagnoseDecl->getLocation(), | ||
| diag::warn_odr_tag_type_with_attributes) | ||
| << Context.ToCtx.getTypeDeclType(DiagnoseDecl) | ||
| << (PrimaryDecl != nullptr); | ||
| if (D1Attr) | ||
| Context.Diag1(D1Attr->getLoc(), diag::note_odr_attr_here) << D1Attr; | ||
| if (D2Attr) | ||
| Context.Diag1(D2Attr->getLoc(), diag::note_odr_attr_here) << D2Attr; | ||
| if (R.A1) | ||
| Context.Diag1(R.A1->getLoc(), diag::note_odr_attr_here) << R.A1; | ||
| if (R.A2) | ||
| Context.Diag1(R.A2->getLoc(), diag::note_odr_attr_here) << R.A2; | ||
| } | ||
|
|
||
| // The above diagnostic is a warning which defaults to an error. If treated | ||
|
|
@@ -1791,12 +1901,6 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, | |
| } | ||
| } | ||
|
|
||
| // In C23 mode, check for structural equivalence of attributes on the record | ||
| // itself. FIXME: Should this happen in C++ as well? | ||
| if (Context.LangOpts.C23 && | ||
| !CheckStructurallyEquivalentAttributes(Context, D1, D2)) | ||
| return false; | ||
|
|
||
| // If the records occur in different context (namespace), these should be | ||
| // different. This is specially important if the definition of one or both | ||
| // records is missing. In C23, different contexts do not make for a different | ||
|
|
@@ -1838,6 +1942,12 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, | |
| if (!D1 || !D2) | ||
| return !Context.LangOpts.C23; | ||
|
|
||
| // In C23 mode, check for structural equivalence of attributes on the record | ||
| // itself. FIXME: Should this happen in C++ as well? | ||
| if (Context.LangOpts.C23 && | ||
| !CheckStructurallyEquivalentAttributes(Context, D1, D2)) | ||
| return false; | ||
|
|
||
| // If any of the records has external storage and we do a minimal check (or | ||
| // AST import) we assume they are equivalent. (If we didn't have this | ||
| // assumption then `RecordDecl::LoadFieldsFromExternalStorage` could trigger | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to require the spellings to be the same? e.g.,
[[maybe_unused]]vs__attribute__((unused))I think in general we have to because we have attributes with multiple spellings but the same kinds. e.g.,