[all-commits] [llvm/llvm-project] 8c7b64: [clang] Reject non-declaration C++11 attributes on...

martinboehme via All-commits all-commits at lists.llvm.org
Wed Jun 15 02:58:42 PDT 2022

  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 8c7b64b5ae2a09027c38db969a04fc9ddd0cd6bb
  Author: Martin Boehme <mboehme at google.com>
  Date:   2022-06-15 (Wed, 15 Jun 2022)

  Changed paths:
    M clang/include/clang/Basic/AttributeCommonInfo.h
    M clang/include/clang/Basic/DiagnosticCommonKinds.td
    M clang/include/clang/Basic/DiagnosticSemaKinds.td
    M clang/include/clang/Parse/Parser.h
    M clang/include/clang/Parse/RAIIObjectsForParser.h
    M clang/include/clang/Sema/DeclSpec.h
    M clang/include/clang/Sema/ParsedAttr.h
    M clang/include/clang/Sema/Sema.h
    M clang/lib/Parse/ParseDecl.cpp
    M clang/lib/Parse/ParseDeclCXX.cpp
    M clang/lib/Parse/ParseExpr.cpp
    M clang/lib/Parse/ParseExprCXX.cpp
    M clang/lib/Parse/ParseObjc.cpp
    M clang/lib/Parse/ParseOpenMP.cpp
    M clang/lib/Parse/ParseStmt.cpp
    M clang/lib/Parse/ParseTemplate.cpp
    M clang/lib/Parse/Parser.cpp
    M clang/lib/Sema/ParsedAttr.cpp
    M clang/lib/Sema/SemaDecl.cpp
    M clang/lib/Sema/SemaDeclAttr.cpp
    M clang/lib/Sema/SemaDeclCXX.cpp
    M clang/lib/Sema/SemaDeclObjC.cpp
    M clang/lib/Sema/SemaType.cpp
    M clang/test/AST/language_address_space_attribute.cpp
    M clang/test/CodeGen/attr-btf_type_tag-func.c
    M clang/test/CodeGen/attr-btf_type_tag-var.c
    M clang/test/Frontend/noderef.c
    M clang/test/Parser/MicrosoftExtensions.cpp
    M clang/test/Parser/asm.c
    M clang/test/Parser/asm.cpp
    M clang/test/Parser/attributes.c
    M clang/test/Parser/c2x-attributes.c
    M clang/test/Parser/cxx0x-attributes.cpp
    M clang/test/Sema/annotate-type.c
    M clang/test/Sema/attr-declspec-ignored.c
    M clang/test/Sema/attr-regparm.c
    M clang/test/Sema/matrix-type-builtins.c
    M clang/test/Sema/neon-vector-types.c
    M clang/test/Sema/overload-arm-mve.c
    M clang/test/Sema/vector-gcc-compat.c
    A clang/test/SemaCXX/address-space-placement.cpp
    M clang/test/SemaCXX/annotate-type.cpp
    M clang/test/SemaObjC/attr-objc-gc.m
    M clang/test/SemaOpenCL/address-spaces.cl
    M clang/utils/TableGen/ClangAttrEmitter.cpp

  Log Message:
  [clang] Reject non-declaration C++11 attributes on declarations

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 high-level changes that achieve this are:

- We introduce a new field `Declarator::DeclarationAttrs` (with appropriate
  accessors) to store C++11 attributes occurring in the attribute-specifier-seq
  at the beginning of a simple-declaration (and other similar declarations).
  Previously, these attributes were placed on the `DeclSpec`, which made it
  impossible to reconstruct later on whether the attributes had in fact been
  placed on the decl-specifier-seq or ahead of the declaration.

- In the parser, we propgate declaration attributes and decl-specifier-seq
  attributes separately until we can place them in
  `Declarator::DeclarationAttrs` or `DeclSpec::Attrs`, respectively.

- In `ProcessDeclAttributes()`, in addition to processing declarator attributes,
  we now also process the attributes from `Declarator::DeclarationAttrs` (except
  if they are legacy type attributes).

- In `ConvertDeclSpecToType()`, in addition to processing `DeclSpec` attributes,
  we also process any legacy type attributes that occur in
  `Declarator::DeclarationAttrs` (and emit a warning).

- 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)

- "In the declaration for an entity, attributes appertaining to that entity can
  appear at the start of the declaration and after the declarator-id for that
  declaration." (https://eel.is/c++draft/dcl.dcl#dcl.pre-note-2)

- "The optional attribute-specifier-seq following a declarator-id appertains to
  the entity that is declared."

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

- "The optional attribute-specifier-seq in a parameter-declaration appertains to
  the parameter." (https://eel.is/c++draft/dcl.fct#3)

- "The optional attribute-specifier-seq in an exception-declaration appertains
  to the parameter of the catch clause" (https://eel.is/c++draft/except.pre#1)

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

Reviewed By: aaron.ballman, rsmith

Differential Revision: https://reviews.llvm.org/D126061

More information about the All-commits mailing list