This is an archive of the discontinued LLVM Phabricator instance.

[clang] [WIP] Reject non-declaration C++11 attributes on declarations.
AbandonedPublic

Authored by mboehme on May 4 2022, 6:02 AM.

Details

Reviewers
aaron.ballman
Summary

DRAFT -- do not review.

For backwards compatiblity, we emit only a warning instead of an error if the
attribute is one of the existing type attributes that we have historically
allowed to "slide" to the DeclSpec just as if it had been specified in GNU
syntax. (We will call these "legacy type attributes" below.)

The two high-level changes that achieve this are:

  • We move any C++11 attributes (except legacy type attributes) that occur in the attribute-specifier-seq at the beginning of a simple-declaration (and other similar declarations) to Declarator::Attrs. This means that we treat them the same as if they had been placed after the declarator-id. (Justification below.)
  • We make ProcessDeclAttribute emit an error if it sees any non-declaration attributes in C++11 syntax, except in the following cases:
    • If it is being called for attributes on a DeclSpec or DeclaratorChunk
    • If the attribute is a legacy type attribute (in which case we only emit a warning)

The standard justifies treating attributes at the beginning of a
simple-declaration and attributes after a declarator-id the same. Here are some
relevant parts of the standard:

  • The attribute-specifier-seq at the beginning of a simple-declaration "appertains to each of the entities declared by the declarators of the init-declarator-list" (https://eel.is/c++draft/dcl.dcl#dcl.pre-3)

The standard contains similar wording to that for a simple-declaration in other
similar types of declarations, for example:

The new behavior is tested both on the newly added type attribute
annotate_type, for which we emit errors, and for the legacy type attribute
address_space (chosen somewhat randomly from the various legacy type
attributes), for which we emit warnings.

Depends On D111548

Diff Detail

Event Timeline

mboehme created this revision.May 4 2022, 6:02 AM
Herald added a project: Restricted Project. · View Herald Transcript
mboehme requested review of this revision.May 4 2022, 6:02 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 4 2022, 6:02 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
mboehme updated this revision to Diff 426991.May 4 2022, 6:50 AM

Reuploaded because base revision changed.

mboehme updated this revision to Diff 428016.May 9 2022, 2:38 AM
mboehme edited the summary of this revision. (Show Details)
  • Added warnings for "legacy" type attributes
  • Added documentation
  • Fixed TODOs in the code
mboehme updated this revision to Diff 428318.May 10 2022, 2:20 AM

Simply code by replacing ExtractDefiniteDeclAttrs() function with SlideAttrsToDeclSpec(). This essentially inverts the logic: Instead of moving those attributes to a second list that should definitely remain on the declaration, we identify the attributes that should "slide" and move them directly to the DeclSpec. This obviates the need for an additional ParsedAttributes list and hence simplifies the callsites.

mboehme abandoned this revision.May 25 2022, 7:24 AM

It appears that we're converging on the approach in https://reviews.llvm.org/D126061, so I'm abandoning this change.