clang 17.0.0git
ParsedAttrInfo.h
Go to the documentation of this file.
1//===- ParsedAttrInfo.h - Info needed to parse an attribute -----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the ParsedAttrInfo class, which dictates how to
10// parse an attribute. This class is the one that plugins derive to
11// define a new attribute.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_BASIC_PARSEDATTRINFO_H
16#define LLVM_CLANG_BASIC_PARSEDATTRINFO_H
17
20#include "llvm/ADT/ArrayRef.h"
21#include "llvm/Support/Registry.h"
22#include <climits>
23#include <list>
24
25namespace clang {
26
27class Decl;
28class LangOptions;
29class ParsedAttr;
30class Sema;
31class Stmt;
32class TargetInfo;
33
35 /// Corresponds to the Kind enum.
36 unsigned AttrKind : 16;
37 /// The number of required arguments of this attribute.
38 unsigned NumArgs : 4;
39 /// The number of optional arguments of this attributes.
40 unsigned OptArgs : 4;
41 /// The number of non-fake arguments specified in the attribute definition.
42 unsigned NumArgMembers : 4;
43 /// True if the parsing does not match the semantic content.
44 unsigned HasCustomParsing : 1;
45 // True if this attribute accepts expression parameter pack expansions.
46 unsigned AcceptsExprPack : 1;
47 /// True if this attribute is only available for certain targets.
48 unsigned IsTargetSpecific : 1;
49 /// True if this attribute applies to types.
50 unsigned IsType : 1;
51 /// True if this attribute applies to statements.
52 unsigned IsStmt : 1;
53 /// True if this attribute has any spellings that are known to gcc.
54 unsigned IsKnownToGCC : 1;
55 /// True if this attribute is supported by #pragma clang attribute.
57 /// The syntaxes supported by this attribute and how they're spelled.
58 struct Spelling {
60 const char *NormalizedFullName;
61 };
63 // The names of the known arguments of this attribute.
65
66protected:
72
74 unsigned OptArgs, unsigned NumArgMembers,
75 unsigned HasCustomParsing, unsigned AcceptsExprPack,
76 unsigned IsTargetSpecific, unsigned IsType,
77 unsigned IsStmt, unsigned IsKnownToGCC,
87
88public:
89 virtual ~ParsedAttrInfo() = default;
90
91 /// Check if this attribute has specified spelling.
92 bool hasSpelling(AttributeCommonInfo::Syntax Syntax, StringRef Name) const {
93 return llvm::any_of(Spellings, [&](const Spelling &S) {
94 return (S.Syntax == Syntax && S.NormalizedFullName == Name);
95 });
96 }
97
98 /// Check if this attribute appertains to D, and issue a diagnostic if not.
99 virtual bool diagAppertainsToDecl(Sema &S, const ParsedAttr &Attr,
100 const Decl *D) const {
101 return true;
102 }
103 /// Check if this attribute appertains to St, and issue a diagnostic if not.
104 virtual bool diagAppertainsToStmt(Sema &S, const ParsedAttr &Attr,
105 const Stmt *St) const {
106 return true;
107 }
108 /// Check if the given attribute is mutually exclusive with other attributes
109 /// already applied to the given declaration.
110 virtual bool diagMutualExclusion(Sema &S, const ParsedAttr &A,
111 const Decl *D) const {
112 return true;
113 }
114 /// Check if this attribute is allowed by the language we are compiling.
115 virtual bool acceptsLangOpts(const LangOptions &LO) const { return true; }
116
117 /// Check if this attribute is allowed when compiling for the given target.
118 virtual bool existsInTarget(const TargetInfo &Target) const { return true; }
119 /// Convert the spelling index of Attr to a semantic spelling enum value.
120 virtual unsigned
122 return UINT_MAX;
123 }
124 /// Returns true if the specified parameter index for this attribute in
125 /// Attr.td is an ExprArgument or VariadicExprArgument, or a subclass thereof;
126 /// returns false otherwise.
127 virtual bool isParamExpr(size_t N) const { return false; }
128 /// Populate Rules with the match rules of this attribute.
130 llvm::SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &Rules,
131 const LangOptions &LangOpts) const {}
132
134 /// If this ParsedAttrInfo knows how to handle this ParsedAttr applied to this
135 /// Decl then do so and return either AttributeApplied if it was applied or
136 /// AttributeNotApplied if it wasn't. Otherwise return NotHandled.
138 const ParsedAttr &Attr) const {
139 return NotHandled;
140 }
141
142 static const ParsedAttrInfo &get(const AttributeCommonInfo &A);
144};
145
146typedef llvm::Registry<ParsedAttrInfo> ParsedAttrInfoRegistry;
147
148const std::list<std::unique_ptr<ParsedAttrInfo>> &getAttributePluginInstances();
149
150} // namespace clang
151
152#endif // LLVM_CLANG_BASIC_PARSEDATTRINFO_H
Attr - This represents one attribute.
Definition: Attr.h:40
Syntax
The style used to specify an attribute.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:82
ParsedAttr - Represents a syntactic attribute.
Definition: ParsedAttr.h:123
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:356
Stmt - This represents one statement.
Definition: Stmt.h:72
Exposes information about the current target.
Definition: TargetInfo.h:206
#define UINT_MAX
Definition: limits.h:60
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
const std::list< std::unique_ptr< ParsedAttrInfo > > & getAttributePluginInstances()
llvm::Registry< ParsedAttrInfo > ParsedAttrInfoRegistry
The syntaxes supported by this attribute and how they're spelled.
AttributeCommonInfo::Syntax Syntax
constexpr ParsedAttrInfo(AttributeCommonInfo::Kind AttrKind=AttributeCommonInfo::NoSemaHandlerAttribute)
bool hasSpelling(AttributeCommonInfo::Syntax Syntax, StringRef Name) const
Check if this attribute has specified spelling.
virtual bool acceptsLangOpts(const LangOptions &LO) const
Check if this attribute is allowed by the language we are compiling.
static ArrayRef< const ParsedAttrInfo * > getAllBuiltin()
Definition: ParsedAttr.cpp:138
unsigned IsKnownToGCC
True if this attribute has any spellings that are known to gcc.
ArrayRef< const char * > ArgNames
unsigned HasCustomParsing
True if the parsing does not match the semantic content.
ArrayRef< Spelling > Spellings
unsigned IsType
True if this attribute applies to types.
unsigned IsTargetSpecific
True if this attribute is only available for certain targets.
unsigned IsSupportedByPragmaAttribute
True if this attribute is supported by #pragma clang attribute.
unsigned AttrKind
Corresponds to the Kind enum.
unsigned NumArgs
The number of required arguments of this attribute.
virtual bool diagAppertainsToStmt(Sema &S, const ParsedAttr &Attr, const Stmt *St) const
Check if this attribute appertains to St, and issue a diagnostic if not.
virtual unsigned spellingIndexToSemanticSpelling(const ParsedAttr &Attr) const
Convert the spelling index of Attr to a semantic spelling enum value.
virtual AttrHandling handleDeclAttribute(Sema &S, Decl *D, const ParsedAttr &Attr) const
If this ParsedAttrInfo knows how to handle this ParsedAttr applied to this Decl then do so and return...
static const ParsedAttrInfo & get(const AttributeCommonInfo &A)
Definition: ParsedAttr.cpp:109
constexpr ParsedAttrInfo(AttributeCommonInfo::Kind AttrKind, unsigned NumArgs, unsigned OptArgs, unsigned NumArgMembers, unsigned HasCustomParsing, unsigned AcceptsExprPack, unsigned IsTargetSpecific, unsigned IsType, unsigned IsStmt, unsigned IsKnownToGCC, unsigned IsSupportedByPragmaAttribute, ArrayRef< Spelling > Spellings, ArrayRef< const char * > ArgNames)
virtual bool isParamExpr(size_t N) const
Returns true if the specified parameter index for this attribute in Attr.td is an ExprArgument or Var...
virtual void getPragmaAttributeMatchRules(llvm::SmallVectorImpl< std::pair< attr::SubjectMatchRule, bool > > &Rules, const LangOptions &LangOpts) const
Populate Rules with the match rules of this attribute.
virtual bool existsInTarget(const TargetInfo &Target) const
Check if this attribute is allowed when compiling for the given target.
virtual ~ParsedAttrInfo()=default
virtual bool diagAppertainsToDecl(Sema &S, const ParsedAttr &Attr, const Decl *D) const
Check if this attribute appertains to D, and issue a diagnostic if not.
unsigned OptArgs
The number of optional arguments of this attributes.
virtual bool diagMutualExclusion(Sema &S, const ParsedAttr &A, const Decl *D) const
Check if the given attribute is mutually exclusive with other attributes already applied to the given...
unsigned IsStmt
True if this attribute applies to statements.
unsigned NumArgMembers
The number of non-fake arguments specified in the attribute definition.