[PATCH] D124919: [clang] [WIP] Reject non-declaration C++11 attributes on declarations.
Martin Böhme via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed May 4 06:02:54 PDT 2022
mboehme created this revision.
Herald added a reviewer: aaron.ballman.
Herald added a project: All.
mboehme requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
For backwards compatiblity, we only emit a warning 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 a warning 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
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." (https://eel.is/c++draft/dcl.dcl#dcl.meaning.general-1)
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)
Things that remain to be done:
- Emit warnings when a legacy type attribute occurs on a declaration. (This is not hard to do but will likely require updating a large number of tests, so I'd first like to get confirmation that the general direction of this change makes sense.)
- Move additional tests added to annotate-type.cpp to https://reviews.llvm.org/D111548 instead.
- Various other TODOs in the code
Depends On D111548 <https://reviews.llvm.org/D111548>
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D124919
Files:
clang/include/clang/Parse/Parser.h
clang/include/clang/Parse/RAIIObjectsForParser.h
clang/include/clang/Sema/ParsedAttr.h
clang/include/clang/Sema/Sema.h
clang/lib/Parse/ParseDecl.cpp
clang/lib/Parse/ParseDeclCXX.cpp
clang/lib/Parse/ParseExprCXX.cpp
clang/lib/Parse/ParseStmt.cpp
clang/lib/Parse/ParseTemplate.cpp
clang/lib/Parse/Parser.cpp
clang/lib/Sema/ParsedAttr.cpp
clang/lib/Sema/SemaDeclAttr.cpp
clang/lib/Sema/SemaType.cpp
clang/test/Sema/annotate-type.c
clang/test/SemaCXX/annotate-type.cpp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D124919.426978.patch
Type: text/x-patch
Size: 27478 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220504/14d26744/attachment-0001.bin>
More information about the cfe-commits
mailing list