This is an archive of the discontinued LLVM Phabricator instance.

Add a priority field to availability attributes to prioritize explicit attributes from declaration over attributes from '#pragma clang attribute'
ClosedPublic

Authored by arphaman on Jan 17 2019, 6:38 PM.

Details

Summary

We have an issue when using #pragma clang attribute with availability attributes:

  • The explicit attribute that's specified next to the declaration is not guaranteed to be preferred over the attribute specified in the pragma.

This patch fixes this by introducing a priority field to the availability attribute to control how they're merged. Attributes with higher priority are applied over attributes with lower priority for the same platform. The implicitly inferred attributes are given the lower priority. This ensures that:

  1. explicit attributes are preferred over all other attributes.
  2. implicitly inferred attributes that are inferred from an explicit attribute are discarded if there's an explicit attribute or an attribute specified using a #pragma for the same platform.
  3. implicitly inferred attributes that are inferred from an attribute in the #pragma are not used if there's an explicit, explicit #pragma, or an implicit attribute inferred from an explicit attribute for the declaration.

This is the resulting ranking:

platform availability > platform availability from pragma > inferred availability > inferred availability from pragma

rdar://46390243

Diff Detail

Repository
rL LLVM

Event Timeline

arphaman created this revision.Jan 17 2019, 6:38 PM
arphaman updated this revision to Diff 182432.Jan 17 2019, 6:47 PM

Reverse the priority numbering direction as suggested by @dexonsmith .

Now the lower priority is the most important, and the less important priorities have a higher numerical value.

I think the documentation for the attribute should be updated to explain this new behavior, otherwise this will be a very inexplicable feature to users.

One question I have is: will this feature also be needed by other attributes? This seems like a somewhat general problem between explicit attributes, implicit attributes, and attributes added via the pragma. It might not be worth generalizing yet, but I'm curious to know whether we'll need this for things like visibility and type_visibility as well.

include/clang/Sema/Sema.h
2467 ↗(On Diff #182432)

We should have a comment somewhere (perhaps here) that makes it clear that the priorities we calculate and store on the semantic attribute object are not expected to match values in this enumeration, but instead be treated as a plain integer value. This enumeration is just giving a name to some of the addends used to calculate that value.

This seems pretty reasonable to me. I agree that a more general mechanism to override #pca (/implicit) attributes would be pretty useful, but I guess there is no need to jump the gun on that.

include/clang/Sema/Sema.h
2471 ↗(On Diff #182432)

Maybe add = 0 (and =1 and =2) explicitly to indicate that their relative values matter here.

lib/Sema/SemaDeclAttr.cpp
2479 ↗(On Diff #182432)

Use AP_Explicit here instead of 0?

2483 ↗(On Diff #182432)

... And the avoid adding it back on here?

arphaman updated this revision to Diff 182608.Jan 18 2019, 2:24 PM
arphaman marked 3 inline comments as done.
  • Add documentation and expand a comment as suggested by Aaron.
  • Address Erik's comments.

I think the documentation for the attribute should be updated to explain this new behavior, otherwise this will be a very inexplicable feature to users.

Done

One question I have is: will this feature also be needed by other attributes? This seems like a somewhat general problem between explicit attributes, implicit attributes, and attributes added via the pragma. It might not be worth generalizing yet, but I'm curious to know whether we'll need this for things like visibility and type_visibility as well.

Possibly. We haven't come across a use case that requires that yet, but we would be definitely willing to work on a more generalized solution in the future if such a need arises.

erik.pilkington accepted this revision.Jan 18 2019, 2:56 PM

LGTM, thanks!

This revision is now accepted and ready to land.Jan 18 2019, 2:56 PM
aaron.ballman accepted this revision.Jan 21 2019, 7:37 AM

LGTM with a suggested edit for the docs (which look great, btw!).

include/clang/Basic/AttrDocs.td
1252 ↗(On Diff #182608)

tvOS whose -> tvOS, whose

1253 ↗(On Diff #182608)

attribute the logic -> attribute, the logic

arphaman marked 2 inline comments as done.Jan 24 2019, 11:14 AM
This revision was automatically updated to reflect the committed changes.