2018-07-13 15:07:47 +00:00
|
|
|
//======- ParsedAttr.cpp --------------------------------------------------===//
|
2007-06-09 03:39:29 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2007-06-09 03:39:29 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2018-07-13 15:07:47 +00:00
|
|
|
// This file defines the ParsedAttr class implementation
|
2007-06-09 03:39:29 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-07-13 15:07:47 +00:00
|
|
|
#include "clang/Sema/ParsedAttr.h"
|
2012-07-04 17:04:04 +00:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2017-04-18 14:33:39 +00:00
|
|
|
#include "clang/Basic/AttrSubjectMatchRules.h"
|
2009-04-11 18:48:18 +00:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2015-07-20 22:57:31 +00:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2014-01-07 11:51:46 +00:00
|
|
|
#include "clang/Sema/SemaInternal.h"
|
2018-02-20 02:16:28 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <utility>
|
|
|
|
|
2007-06-09 03:39:29 +00:00
|
|
|
using namespace clang;
|
|
|
|
|
2013-09-03 18:01:40 +00:00
|
|
|
IdentifierLoc *IdentifierLoc::create(ASTContext &Ctx, SourceLocation Loc,
|
|
|
|
IdentifierInfo *Ident) {
|
|
|
|
IdentifierLoc *Result = new (Ctx) IdentifierLoc;
|
|
|
|
Result->Loc = Loc;
|
|
|
|
Result->Ident = Ident;
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2018-07-13 15:07:47 +00:00
|
|
|
size_t ParsedAttr::allocated_size() const {
|
2011-03-24 11:26:52 +00:00
|
|
|
if (IsAvailability) return AttributeFactory::AvailabilityAllocSize;
|
2012-08-17 00:08:38 +00:00
|
|
|
else if (IsTypeTagForDatatype)
|
|
|
|
return AttributeFactory::TypeTagForDatatypeAllocSize;
|
2013-04-16 07:28:30 +00:00
|
|
|
else if (IsProperty)
|
|
|
|
return AttributeFactory::PropertyAllocSize;
|
2018-06-22 17:34:44 +00:00
|
|
|
else if (HasParsedType)
|
2018-08-09 20:25:12 +00:00
|
|
|
return totalSizeToAlloc<ArgsUnion, detail::AvailabilityData,
|
|
|
|
detail::TypeTagForDatatypeData, ParsedType,
|
|
|
|
detail::PropertyData>(0, 0, 0, 1, 0);
|
|
|
|
return totalSizeToAlloc<ArgsUnion, detail::AvailabilityData,
|
|
|
|
detail::TypeTagForDatatypeData, ParsedType,
|
|
|
|
detail::PropertyData>(NumArgs, 0, 0, 0, 0);
|
2011-03-24 11:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AttributeFactory::AttributeFactory() {
|
|
|
|
// Go ahead and configure all the inline capacity. This is just a memset.
|
|
|
|
FreeLists.resize(InlineFreeListsCapacity);
|
|
|
|
}
|
2018-02-20 02:16:28 +00:00
|
|
|
AttributeFactory::~AttributeFactory() = default;
|
2011-03-24 11:26:52 +00:00
|
|
|
|
|
|
|
static size_t getFreeListIndexForSize(size_t size) {
|
2018-07-13 15:07:47 +00:00
|
|
|
assert(size >= sizeof(ParsedAttr));
|
2011-03-24 11:26:52 +00:00
|
|
|
assert((size % sizeof(void*)) == 0);
|
2018-07-13 15:07:47 +00:00
|
|
|
return ((size - sizeof(ParsedAttr)) / sizeof(void *));
|
2011-03-24 11:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *AttributeFactory::allocate(size_t size) {
|
|
|
|
// Check for a previously reclaimed attribute.
|
|
|
|
size_t index = getFreeListIndexForSize(size);
|
2018-07-12 21:09:05 +00:00
|
|
|
if (index < FreeLists.size() && !FreeLists[index].empty()) {
|
2018-07-13 15:07:47 +00:00
|
|
|
ParsedAttr *attr = FreeLists[index].back();
|
2018-07-12 21:09:05 +00:00
|
|
|
FreeLists[index].pop_back();
|
|
|
|
return attr;
|
2009-02-19 06:25:12 +00:00
|
|
|
}
|
2011-03-24 11:26:52 +00:00
|
|
|
|
|
|
|
// Otherwise, allocate something new.
|
2016-10-20 14:27:22 +00:00
|
|
|
return Alloc.Allocate(size, alignof(AttributeFactory));
|
2011-03-24 11:26:52 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 15:07:47 +00:00
|
|
|
void AttributeFactory::deallocate(ParsedAttr *Attr) {
|
2018-07-12 21:09:05 +00:00
|
|
|
size_t size = Attr->allocated_size();
|
|
|
|
size_t freeListIndex = getFreeListIndexForSize(size);
|
2011-03-24 11:26:52 +00:00
|
|
|
|
2018-07-12 21:09:05 +00:00
|
|
|
// Expand FreeLists to the appropriate size, if required.
|
|
|
|
if (freeListIndex >= FreeLists.size())
|
|
|
|
FreeLists.resize(freeListIndex + 1);
|
2011-03-24 11:26:52 +00:00
|
|
|
|
2018-09-24 12:12:03 +00:00
|
|
|
#ifndef NDEBUG
|
2018-07-12 21:09:05 +00:00
|
|
|
// In debug mode, zero out the attribute to help find memory overwriting.
|
|
|
|
memset(Attr, 0, size);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Add 'Attr' to the appropriate free-list.
|
|
|
|
FreeLists[freeListIndex].push_back(Attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AttributeFactory::reclaimPool(AttributePool &cur) {
|
2018-07-13 15:07:47 +00:00
|
|
|
for (ParsedAttr *AL : cur.Attrs)
|
2018-07-12 21:09:05 +00:00
|
|
|
deallocate(AL);
|
|
|
|
}
|
2011-03-24 11:26:52 +00:00
|
|
|
|
2018-07-12 21:09:05 +00:00
|
|
|
void AttributePool::takePool(AttributePool &pool) {
|
|
|
|
Attrs.insert(Attrs.end(), pool.Attrs.begin(), pool.Attrs.end());
|
|
|
|
pool.Attrs.clear();
|
2007-06-09 03:39:29 +00:00
|
|
|
}
|
2008-02-20 23:14:47 +00:00
|
|
|
|
2024-03-04 09:25:29 -08:00
|
|
|
void AttributePool::takeFrom(ParsedAttributesView &List, AttributePool &Pool) {
|
|
|
|
assert(&Pool != this && "AttributePool can't take attributes from itself");
|
|
|
|
llvm::for_each(List.AttrList, [&Pool](ParsedAttr *A) { Pool.remove(A); });
|
|
|
|
Attrs.insert(Attrs.end(), List.AttrList.begin(), List.AttrList.end());
|
|
|
|
}
|
|
|
|
|
2013-09-09 23:33:17 +00:00
|
|
|
namespace {
|
2018-02-20 02:16:28 +00:00
|
|
|
|
|
|
|
#include "clang/Sema/AttrParsedAttrImpl.inc"
|
|
|
|
|
|
|
|
} // namespace
|
2013-09-09 23:33:17 +00:00
|
|
|
|
2020-02-07 14:21:13 +00:00
|
|
|
const ParsedAttrInfo &ParsedAttrInfo::get(const AttributeCommonInfo &A) {
|
|
|
|
// If we have a ParsedAttrInfo for this ParsedAttr then return that.
|
2022-09-08 14:26:11 -06:00
|
|
|
if ((size_t)A.getParsedKind() < std::size(AttrInfoMap))
|
2020-02-07 14:21:13 +00:00
|
|
|
return *AttrInfoMap[A.getParsedKind()];
|
|
|
|
|
|
|
|
// If this is an ignored attribute then return an appropriate ParsedAttrInfo.
|
2020-03-28 18:12:28 +01:00
|
|
|
static const ParsedAttrInfo IgnoredParsedAttrInfo(
|
2020-02-07 14:21:13 +00:00
|
|
|
AttributeCommonInfo::IgnoredAttribute);
|
|
|
|
if (A.getParsedKind() == AttributeCommonInfo::IgnoredAttribute)
|
|
|
|
return IgnoredParsedAttrInfo;
|
|
|
|
|
2023-02-12 20:44:30 +01:00
|
|
|
// Otherwise this may be an attribute defined by a plugin.
|
2020-02-07 14:21:13 +00:00
|
|
|
|
|
|
|
// Search for a ParsedAttrInfo whose name and syntax match.
|
|
|
|
std::string FullName = A.getNormalizedFullName();
|
|
|
|
AttributeCommonInfo::Syntax SyntaxUsed = A.getSyntax();
|
|
|
|
if (SyntaxUsed == AttributeCommonInfo::AS_ContextSensitiveKeyword)
|
|
|
|
SyntaxUsed = AttributeCommonInfo::AS_Keyword;
|
|
|
|
|
2023-02-12 20:44:30 +01:00
|
|
|
for (auto &Ptr : getAttributePluginInstances())
|
2023-03-09 22:55:38 +01:00
|
|
|
if (Ptr->hasSpelling(SyntaxUsed, FullName))
|
|
|
|
return *Ptr;
|
2020-02-07 14:21:13 +00:00
|
|
|
|
|
|
|
// If we failed to find a match then return a default ParsedAttrInfo.
|
2020-03-28 18:12:28 +01:00
|
|
|
static const ParsedAttrInfo DefaultParsedAttrInfo(
|
2020-02-28 14:51:30 +00:00
|
|
|
AttributeCommonInfo::UnknownAttribute);
|
2020-02-26 16:31:24 +00:00
|
|
|
return DefaultParsedAttrInfo;
|
2013-09-09 23:33:17 +00:00
|
|
|
}
|
|
|
|
|
2021-08-07 17:36:26 +02:00
|
|
|
ArrayRef<const ParsedAttrInfo *> ParsedAttrInfo::getAllBuiltin() {
|
2023-01-06 16:56:23 +01:00
|
|
|
return llvm::ArrayRef(AttrInfoMap);
|
2021-08-07 17:36:26 +02:00
|
|
|
}
|
|
|
|
|
2020-02-07 14:21:13 +00:00
|
|
|
unsigned ParsedAttr::getMinArgs() const { return getInfo().NumArgs; }
|
2013-09-09 23:33:17 +00:00
|
|
|
|
2018-07-13 15:07:47 +00:00
|
|
|
unsigned ParsedAttr::getMaxArgs() const {
|
2020-02-07 14:21:13 +00:00
|
|
|
return getMinArgs() + getInfo().OptArgs;
|
2013-09-09 23:33:17 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 13:27:52 -05:00
|
|
|
unsigned ParsedAttr::getNumArgMembers() const {
|
|
|
|
return getInfo().NumArgMembers;
|
|
|
|
}
|
|
|
|
|
2018-07-13 15:07:47 +00:00
|
|
|
bool ParsedAttr::hasCustomParsing() const {
|
2020-02-07 14:21:13 +00:00
|
|
|
return getInfo().HasCustomParsing;
|
2013-09-09 23:33:17 +00:00
|
|
|
}
|
2013-11-27 13:27:02 +00:00
|
|
|
|
2018-07-13 15:07:47 +00:00
|
|
|
bool ParsedAttr::diagnoseAppertainsTo(Sema &S, const Decl *D) const {
|
2020-02-07 14:21:13 +00:00
|
|
|
return getInfo().diagAppertainsToDecl(S, *this, D);
|
2013-11-27 13:27:02 +00:00
|
|
|
}
|
2013-12-02 19:30:36 +00:00
|
|
|
|
2021-03-19 08:33:27 -04:00
|
|
|
bool ParsedAttr::diagnoseAppertainsTo(Sema &S, const Stmt *St) const {
|
|
|
|
return getInfo().diagAppertainsToStmt(S, *this, St);
|
|
|
|
}
|
|
|
|
|
2021-04-02 16:33:14 -04:00
|
|
|
bool ParsedAttr::diagnoseMutualExclusion(Sema &S, const Decl *D) const {
|
|
|
|
return getInfo().diagMutualExclusion(S, *this, D);
|
|
|
|
}
|
|
|
|
|
2018-07-13 15:07:47 +00:00
|
|
|
bool ParsedAttr::appliesToDecl(const Decl *D,
|
|
|
|
attr::SubjectMatchRule MatchRule) const {
|
2017-04-18 14:33:39 +00:00
|
|
|
return checkAttributeMatchRuleAppliesTo(D, MatchRule);
|
|
|
|
}
|
|
|
|
|
2018-07-13 15:07:47 +00:00
|
|
|
void ParsedAttr::getMatchRules(
|
2017-04-18 14:33:39 +00:00
|
|
|
const LangOptions &LangOpts,
|
|
|
|
SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &MatchRules)
|
|
|
|
const {
|
2020-02-07 14:21:13 +00:00
|
|
|
return getInfo().getPragmaAttributeMatchRules(MatchRules, LangOpts);
|
2017-04-18 14:33:39 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 15:07:47 +00:00
|
|
|
bool ParsedAttr::diagnoseLangOpts(Sema &S) const {
|
2021-08-10 17:56:32 +02:00
|
|
|
if (getInfo().acceptsLangOpts(S.getLangOpts()))
|
|
|
|
return true;
|
|
|
|
S.Diag(getLoc(), diag::warn_attribute_ignored) << *this;
|
|
|
|
return false;
|
2013-12-02 19:30:36 +00:00
|
|
|
}
|
2014-01-09 22:48:32 +00:00
|
|
|
|
2018-07-13 15:07:47 +00:00
|
|
|
bool ParsedAttr::isTargetSpecificAttr() const {
|
2020-02-07 14:21:13 +00:00
|
|
|
return getInfo().IsTargetSpecific;
|
2014-01-09 22:48:32 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 14:21:13 +00:00
|
|
|
bool ParsedAttr::isTypeAttr() const { return getInfo().IsType; }
|
2014-01-09 22:48:32 +00:00
|
|
|
|
2020-02-07 14:21:13 +00:00
|
|
|
bool ParsedAttr::isStmtAttr() const { return getInfo().IsStmt; }
|
2016-03-08 00:32:55 +00:00
|
|
|
|
2018-07-13 15:07:47 +00:00
|
|
|
bool ParsedAttr::existsInTarget(const TargetInfo &Target) const {
|
2023-11-20 11:14:40 -08:00
|
|
|
Kind K = getParsedKind();
|
|
|
|
|
|
|
|
// If the attribute has a target-specific spelling, check that it exists.
|
|
|
|
// Only call this if the attr is not ignored/unknown. For most targets, this
|
|
|
|
// function just returns true.
|
|
|
|
bool HasSpelling = K != IgnoredAttribute && K != UnknownAttribute &&
|
|
|
|
K != NoSemaHandlerAttribute;
|
|
|
|
bool TargetSpecificSpellingExists =
|
|
|
|
!HasSpelling ||
|
|
|
|
getInfo().spellingExistsInTarget(Target, getAttributeSpellingListIndex());
|
|
|
|
|
|
|
|
return getInfo().existsInTarget(Target) && TargetSpecificSpellingExists;
|
2014-01-09 22:48:32 +00:00
|
|
|
}
|
2014-01-20 17:18:35 +00:00
|
|
|
|
2020-02-07 14:21:13 +00:00
|
|
|
bool ParsedAttr::isKnownToGCC() const { return getInfo().IsKnownToGCC; }
|
2014-01-24 21:32:49 +00:00
|
|
|
|
2018-07-13 15:07:47 +00:00
|
|
|
bool ParsedAttr::isSupportedByPragmaAttribute() const {
|
2020-02-07 14:21:13 +00:00
|
|
|
return getInfo().IsSupportedByPragmaAttribute;
|
2017-04-18 14:33:39 +00:00
|
|
|
}
|
|
|
|
|
[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."
(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)
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
2022-06-15 08:07:23 +02:00
|
|
|
bool ParsedAttr::slidesFromDeclToDeclSpecLegacyBehavior() const {
|
[clang] Add Parse and Sema support for RegularKeyword attributes
This patch adds the Parse and Sema support for RegularKeyword attributes,
following on from a previous patch that added Attr.td support.
The patch is quite large. However, nothing outside the tests is
specific to the first RegularKeyword attribute (__arm_streaming).
The patch should therefore be a one-off, up-front cost. Other
attributes just need an entry in Attr.td and the usual Sema support.
The approach taken in the patch is that the keywords can be used with
any language version. If standard attributes were added in language
version Y, the keyword rules for version X<Y are the same as they were
for version Y (to the extent possible). Any extensions beyond Y are
handled in the same way for both keywords and attributes. This ensures
that existing C++11 successors like C++17 are not treated differently
from versions that have yet to be defined.
Some notes on the implementation:
* The patch emits errors rather than warnings for diagnostics that
relate to keywords.
* Where possible, the patch drops “attribute” from diagnostics
relating to keywords.
* One exception to the previous point is that warnings about C++
extensions do still mention attributes. The use there seemed OK
since the diagnostics are noting a change in the production rules.
* If a diagnostic string needs to be different for keywords and
attributes, the patch standardizes on passing the attribute/
name/token followed by 0 for attributes and 1 for keywords.
* Although the patch updates warn_attribute_wrong_decl_type_str,
warn_attribute_wrong_decl_type, and warn_attribute_wrong_decl_type,
only the error forms of these strings are used for keywords.
* I couldn't trigger the warnings in checkUnusedDeclAttributes,
even for existing attributes. An assert on the warnings caused
no failures in the testsuite. I think in practice all standard
attributes would be diagnosed before this.
* The patch drops a call to standardAttributesAllowed in
ParseFunctionDeclarator. This is because MaybeParseCXX11Attributes
checks the same thing itself, where appropriate.
* The new tests are based on c2x-attributes.c and
cxx0x-attributes.cpp. The C++ test also incorporates a version of
cxx11-base-spec-attributes.cpp. The FIXMEs are carried across from
the originals.
Differential Revision: https://reviews.llvm.org/D148702
2023-04-04 14:05:13 +01:00
|
|
|
if (isRegularKeywordAttribute())
|
|
|
|
// The appurtenance rules are applied strictly for all regular keyword
|
|
|
|
// atributes.
|
|
|
|
return false;
|
|
|
|
|
2024-07-18 08:23:41 -04:00
|
|
|
assert(isStandardAttributeSyntax() || isAlignas());
|
[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."
(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)
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
2022-06-15 08:07:23 +02:00
|
|
|
|
|
|
|
// We have historically allowed some type attributes with standard attribute
|
|
|
|
// syntax to slide to the decl-specifier-seq, so we have to keep supporting
|
|
|
|
// it. This property is consciously not defined as a flag in Attr.td because
|
|
|
|
// we don't want new attributes to specify it.
|
|
|
|
//
|
|
|
|
// Note: No new entries should be added to this list. Entries should be
|
|
|
|
// removed from this list after a suitable deprecation period, provided that
|
|
|
|
// there are no compatibility considerations with other compilers. If
|
|
|
|
// possible, we would like this list to go away entirely.
|
|
|
|
switch (getParsedKind()) {
|
|
|
|
case AT_AddressSpace:
|
|
|
|
case AT_OpenCLPrivateAddressSpace:
|
|
|
|
case AT_OpenCLGlobalAddressSpace:
|
|
|
|
case AT_OpenCLGlobalDeviceAddressSpace:
|
|
|
|
case AT_OpenCLGlobalHostAddressSpace:
|
|
|
|
case AT_OpenCLLocalAddressSpace:
|
|
|
|
case AT_OpenCLConstantAddressSpace:
|
|
|
|
case AT_OpenCLGenericAddressSpace:
|
|
|
|
case AT_NeonPolyVectorType:
|
|
|
|
case AT_NeonVectorType:
|
|
|
|
case AT_ArmMveStrictPolymorphism:
|
|
|
|
case AT_BTFTypeTag:
|
|
|
|
case AT_ObjCGC:
|
|
|
|
case AT_MatrixType:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 13:27:52 -05:00
|
|
|
bool ParsedAttr::acceptsExprPack() const { return getInfo().AcceptsExprPack; }
|
|
|
|
|
2018-07-13 15:07:47 +00:00
|
|
|
unsigned ParsedAttr::getSemanticSpelling() const {
|
2020-02-07 14:21:13 +00:00
|
|
|
return getInfo().spellingIndexToSemanticSpelling(*this);
|
2014-01-24 21:32:49 +00:00
|
|
|
}
|
2014-07-31 16:37:04 +00:00
|
|
|
|
2018-07-13 15:07:47 +00:00
|
|
|
bool ParsedAttr::hasVariadicArg() const {
|
2014-07-31 16:37:04 +00:00
|
|
|
// If the attribute has the maximum number of optional arguments, we will
|
|
|
|
// claim that as being variadic. If we someday get an attribute that
|
|
|
|
// legitimately bumps up against that maximum, we can use another bit to track
|
|
|
|
// whether it's truly variadic or not.
|
2020-02-07 14:21:13 +00:00
|
|
|
return getInfo().OptArgs == 15;
|
2014-07-31 16:37:04 +00:00
|
|
|
}
|
2021-03-09 14:52:59 -05:00
|
|
|
|
2022-02-08 13:27:52 -05:00
|
|
|
bool ParsedAttr::isParamExpr(size_t N) const {
|
|
|
|
return getInfo().isParamExpr(N);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParsedAttr::handleAttrWithDelayedArgs(Sema &S, Decl *D) const {
|
|
|
|
::handleAttrWithDelayedArgs(S, D, *this);
|
|
|
|
}
|
|
|
|
|
2021-03-09 14:52:59 -05:00
|
|
|
static unsigned getNumAttributeArgs(const ParsedAttr &AL) {
|
|
|
|
// FIXME: Include the type in the argument list.
|
|
|
|
return AL.getNumArgs() + AL.hasParsedType();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Compare>
|
|
|
|
static bool checkAttributeNumArgsImpl(Sema &S, const ParsedAttr &AL,
|
|
|
|
unsigned Num, unsigned Diag,
|
|
|
|
Compare Comp) {
|
|
|
|
if (Comp(getNumAttributeArgs(AL), Num)) {
|
|
|
|
S.Diag(AL.getLoc(), Diag) << AL << Num;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ParsedAttr::checkExactlyNumArgs(Sema &S, unsigned Num) const {
|
|
|
|
return checkAttributeNumArgsImpl(S, *this, Num,
|
|
|
|
diag::err_attribute_wrong_number_arguments,
|
|
|
|
std::not_equal_to<unsigned>());
|
|
|
|
}
|
|
|
|
bool ParsedAttr::checkAtLeastNumArgs(Sema &S, unsigned Num) const {
|
|
|
|
return checkAttributeNumArgsImpl(S, *this, Num,
|
|
|
|
diag::err_attribute_too_few_arguments,
|
|
|
|
std::less<unsigned>());
|
|
|
|
}
|
|
|
|
bool ParsedAttr::checkAtMostNumArgs(Sema &S, unsigned Num) const {
|
|
|
|
return checkAttributeNumArgsImpl(S, *this, Num,
|
|
|
|
diag::err_attribute_too_many_arguments,
|
|
|
|
std::greater<unsigned>());
|
|
|
|
}
|
[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."
(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)
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
2022-06-15 08:07:23 +02:00
|
|
|
|
|
|
|
void clang::takeAndConcatenateAttrs(ParsedAttributes &First,
|
2025-03-19 15:16:38 +01:00
|
|
|
ParsedAttributes &&Second) {
|
|
|
|
|
|
|
|
First.takeAllAtEndFrom(Second);
|
|
|
|
|
|
|
|
if (!First.Range.getBegin().isValid())
|
|
|
|
First.Range.setBegin(Second.Range.getBegin());
|
|
|
|
|
[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."
(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)
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
2022-06-15 08:07:23 +02:00
|
|
|
if (Second.Range.getEnd().isValid())
|
2025-03-19 15:16:38 +01:00
|
|
|
First.Range.setEnd(Second.Range.getEnd());
|
[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."
(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)
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
2022-06-15 08:07:23 +02:00
|
|
|
}
|