clang 19.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 LLVM_PREFERRED_TYPE(AttributeCommonInfo::Kind)
38 /// The number of required arguments of this attribute.
39 unsigned NumArgs : 4;
40 /// The number of optional arguments of this attributes.
41 unsigned OptArgs : 4;
42 /// The number of non-fake arguments specified in the attribute definition.
43 unsigned NumArgMembers : 4;
44 /// True if the parsing does not match the semantic content.
45 LLVM_PREFERRED_TYPE(bool)
46 unsigned HasCustomParsing : 1;
47 // True if this attribute accepts expression parameter pack expansions.
48 LLVM_PREFERRED_TYPE(bool)
49 unsigned AcceptsExprPack : 1;
50 /// True if this attribute is only available for certain targets.
51 LLVM_PREFERRED_TYPE(bool)
52 unsigned IsTargetSpecific : 1;
53 /// True if this attribute applies to types.
54 LLVM_PREFERRED_TYPE(bool)
55 unsigned IsType : 1;
56 /// True if this attribute applies to statements.
57 LLVM_PREFERRED_TYPE(bool)
58 unsigned IsStmt : 1;
59 /// True if this attribute has any spellings that are known to gcc.
60 LLVM_PREFERRED_TYPE(bool)
61 unsigned IsKnownToGCC : 1;
62 /// True if this attribute is supported by #pragma clang attribute.
63 LLVM_PREFERRED_TYPE(bool)
65 /// The syntaxes supported by this attribute and how they're spelled.
66 struct Spelling {
68 const char *NormalizedFullName;
69 };
71 // The names of the known arguments of this attribute.
73
74protected:
80
82 unsigned OptArgs, unsigned NumArgMembers,
83 unsigned HasCustomParsing, unsigned AcceptsExprPack,
84 unsigned IsTargetSpecific, unsigned IsType,
85 unsigned IsStmt, unsigned IsKnownToGCC,
95
96public:
97 virtual ~ParsedAttrInfo() = default;
98
99 /// Check if this attribute has specified spelling.
100 bool hasSpelling(AttributeCommonInfo::Syntax Syntax, StringRef Name) const {
101 return llvm::any_of(Spellings, [&](const Spelling &S) {
102 return (S.Syntax == Syntax && S.NormalizedFullName == Name);
103 });
104 }
105
106 /// Check if this attribute appertains to D, and issue a diagnostic if not.
107 virtual bool diagAppertainsToDecl(Sema &S, const ParsedAttr &Attr,
108 const Decl *D) const {
109 return true;
110 }
111 /// Check if this attribute appertains to St, and issue a diagnostic if not.
112 virtual bool diagAppertainsToStmt(Sema &S, const ParsedAttr &Attr,
113 const Stmt *St) const {
114 return true;
115 }
116 /// Check if the given attribute is mutually exclusive with other attributes
117 /// already applied to the given declaration.
118 virtual bool diagMutualExclusion(Sema &S, const ParsedAttr &A,
119 const Decl *D) const {
120 return true;
121 }
122 /// Check if this attribute is allowed by the language we are compiling.
123 virtual bool acceptsLangOpts(const LangOptions &LO) const { return true; }
124
125 /// Check if this attribute is allowed when compiling for the given target.
126 virtual bool existsInTarget(const TargetInfo &Target) const { return true; }
127
128 /// Check if this attribute's spelling is allowed when compiling for the given
129 /// target.
131 const unsigned SpellingListIndex) const {
132 return true;
133 }
134
135 /// Convert the spelling index of Attr to a semantic spelling enum value.
136 virtual unsigned
138 return UINT_MAX;
139 }
140 /// Returns true if the specified parameter index for this attribute in
141 /// Attr.td is an ExprArgument or VariadicExprArgument, or a subclass thereof;
142 /// returns false otherwise.
143 virtual bool isParamExpr(size_t N) const { return false; }
144 /// Populate Rules with the match rules of this attribute.
146 llvm::SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &Rules,
147 const LangOptions &LangOpts) const {}
148
150 /// If this ParsedAttrInfo knows how to handle this ParsedAttr applied to this
151 /// Decl then do so and return either AttributeApplied if it was applied or
152 /// AttributeNotApplied if it wasn't. Otherwise return NotHandled.
154 const ParsedAttr &Attr) const {
155 return NotHandled;
156 }
157
158 static const ParsedAttrInfo &get(const AttributeCommonInfo &A);
160};
161
162typedef llvm::Registry<ParsedAttrInfo> ParsedAttrInfoRegistry;
163
164const std::list<std::unique_ptr<ParsedAttrInfo>> &getAttributePluginInstances();
165
166} // namespace clang
167
168#endif // LLVM_CLANG_BASIC_PARSEDATTRINFO_H
llvm::MachO::Target Target
Definition: MachO.h:44
Attr - This represents one attribute.
Definition: Attr.h:42
Syntax
The style used to specify an attribute.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:85
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:449
ParsedAttr - Represents a syntactic attribute.
Definition: ParsedAttr.h:126
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:426
Stmt - This represents one statement.
Definition: Stmt.h:84
Exposes information about the current target.
Definition: TargetInfo.h:213
#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 ...
The JSON file list parser is used to communicate input to InstallAPI.
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:144
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:115
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 spellingExistsInTarget(const TargetInfo &Target, const unsigned SpellingListIndex) const
Check if this attribute's spelling is allowed when compiling for the given target.
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.