clang 24.0.0git
Attr.h
Go to the documentation of this file.
1//===----- Attr.h --- Helper functions for attribute handling in Sema -----===//
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 provides helpers for Sema functions that handle attributes.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_SEMA_ATTR_H
14#define LLVM_CLANG_SEMA_ATTR_H
15
16#include "clang/AST/Attr.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclBase.h"
19#include "clang/AST/DeclCXX.h"
20#include "clang/AST/DeclObjC.h"
21#include "clang/AST/Type.h"
22#include "clang/AST/TypeLoc.h"
27#include "clang/Sema/SemaBase.h"
28#include "llvm/Support/Casting.h"
29
30namespace clang {
31
32/// isFuncOrMethodForAttrSubject - Return true if the given decl has function
33/// type (function or function-typed variable) or an Objective-C
34/// method.
35inline bool isFuncOrMethodForAttrSubject(const Decl *D) {
36 return (D->getFunctionType() != nullptr) || llvm::isa<ObjCMethodDecl>(D);
37}
38
39/// Return true if the given decl has function type (function or
40/// function-typed variable) or an Objective-C method or a block.
42 return isFuncOrMethodForAttrSubject(D) || llvm::isa<BlockDecl>(D);
43}
44
45/// Return true if the given decl has a declarator that should have
46/// been processed by Sema::GetTypeForDeclarator.
47inline bool hasDeclarator(const Decl *D) {
48 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
49 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) ||
51}
52
53/// hasFunctionProto - Return true if the given decl has a argument
54/// information. This decl should have already passed
55/// isFuncOrMethodForAttrSubject or isFunctionOrMethodOrBlockForAttrSubject.
56inline bool hasFunctionProto(const Decl *D) {
57 if (const FunctionType *FnTy = D->getFunctionType())
58 return isa<FunctionProtoType>(FnTy);
59 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
60}
61
62/// getFunctionOrMethodNumParams - Return number of function or method
63/// parameters. It is an error to call this on a K&R function (use
64/// hasFunctionProto first).
65inline unsigned getFunctionOrMethodNumParams(const Decl *D) {
66 if (const FunctionType *FnTy = D->getFunctionType())
67 return cast<FunctionProtoType>(FnTy)->getNumParams();
68 if (const auto *BD = dyn_cast<BlockDecl>(D))
69 return BD->getNumParams();
70 return cast<ObjCMethodDecl>(D)->param_size();
71}
72
73/// getFunctionOrMethodParam - Return parameter declaration for the given index
74/// of the passed Decl, which must have a FunctionProtoType. When the Decl is
75/// not a FunctionDecl/ObjCMethodDecl/BlockDecl, do best effort to find
76/// underlying FunctionProtoType using the Decl's TypeSourceInfo.
78 unsigned Idx) {
79 if (const auto *FD = dyn_cast<FunctionDecl>(D))
80 return FD->getParamDecl(Idx);
81 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
82 return MD->getParamDecl(Idx);
83 if (const auto *BD = dyn_cast<BlockDecl>(D))
84 return BD->getParamDecl(Idx);
85
86 // Handle declarations that do not directly own parameters but have an
87 // underlying FunctionProtoType (e.g. function pointers).
88 const TypeSourceInfo *TSI = nullptr;
89 if (const auto *DD = dyn_cast<DeclaratorDecl>(D))
90 TSI = DD->getTypeSourceInfo();
91 else if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
92 TSI = TD->getTypeSourceInfo();
93
94 if (!TSI)
95 return nullptr;
96
98
99 if (auto PTL = TL.getAsAdjusted<PointerTypeLoc>())
100 TL = PTL.getPointeeLoc();
101 else if (auto MPTL = TL.getAsAdjusted<MemberPointerTypeLoc>())
102 TL = MPTL.getPointeeLoc();
103 else if (auto RTL = TL.getAsAdjusted<ReferenceTypeLoc>())
104 TL = RTL.getPointeeLoc();
105 else if (auto BPTL = TL.getAsAdjusted<BlockPointerTypeLoc>())
106 TL = BPTL.getPointeeLoc();
107
108 if (auto FPTL = TL.getAsAdjusted<FunctionProtoTypeLoc>())
109 if (Idx < FPTL.getNumParams())
110 return FPTL.getParam(Idx);
111
112 return nullptr;
113}
114
115inline QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
116 if (const FunctionType *FnTy = D->getFunctionType())
117 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
118 if (const auto *BD = dyn_cast<BlockDecl>(D))
119 return BD->getParamDecl(Idx)->getType();
120
121 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
122}
123
124inline SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
125 if (auto *PVD = getFunctionOrMethodParam(D, Idx))
126 return PVD->getSourceRange();
127 return SourceRange();
128}
129
131 if (const FunctionType *FnTy = D->getFunctionType())
132 return FnTy->getReturnType();
133 return cast<ObjCMethodDecl>(D)->getReturnType();
134}
135
137 if (const auto *FD = dyn_cast<FunctionDecl>(D))
138 return FD->getReturnTypeSourceRange();
139 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
140 return MD->getReturnTypeSourceRange();
141 return SourceRange();
142}
143
144inline bool isFunctionOrMethodVariadic(const Decl *D) {
145 if (const FunctionType *FnTy = D->getFunctionType())
146 return cast<FunctionProtoType>(FnTy)->isVariadic();
147 if (const auto *BD = dyn_cast<BlockDecl>(D))
148 return BD->isVariadic();
149 return cast<ObjCMethodDecl>(D)->isVariadic();
150}
151
152inline bool isInstanceMethod(const Decl *D) {
153 if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(D))
154 return MethodDecl->isInstance();
155 return false;
156}
157
158inline bool hasImplicitObjectParameter(const Decl *D) {
159 if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(D))
160 return MethodDecl->isImplicitObjectMemberFunction();
161 return false;
162}
163
164/// Diagnose mutually exclusive attributes when present on a given
165/// declaration. Returns true if diagnosed.
166template <typename AttrTy>
168 if (const auto *A = D->getAttr<AttrTy>()) {
169 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
170 << AL << A
171 << (AL.isRegularKeywordAttribute() || A->isRegularKeywordAttribute());
172 S.Diag(A->getLocation(), diag::note_conflicting_attribute);
173 return true;
174 }
175 return false;
176}
177
178template <typename AttrTy>
180 if (const auto *A = D->getAttr<AttrTy>()) {
181 S.Diag(AL.getLocation(), diag::err_attributes_are_not_compatible)
182 << &AL << A
183 << (AL.isRegularKeywordAttribute() || A->isRegularKeywordAttribute());
184 Diag(A->getLocation(), diag::note_conflicting_attribute);
185 return true;
186 }
187 return false;
188}
189
190template <typename... DiagnosticArgs>
191const SemaBase::SemaDiagnosticBuilder &
193 return Bldr;
194}
195
196template <typename T, typename... DiagnosticArgs>
197const SemaBase::SemaDiagnosticBuilder &
199 DiagnosticArgs &&...ExtraArgs) {
200 return appendDiagnostics(Bldr << std::forward<T>(ExtraArg),
201 std::forward<DiagnosticArgs>(ExtraArgs)...);
202}
203
204/// Applies the given attribute to the Decl without performing any
205/// additional semantic checking.
206template <typename AttrType>
208 const AttributeCommonInfo &CI) {
209 D->addAttr(::new (S.getASTContext()) AttrType(S.getASTContext(), CI));
210}
211
212/// Add an attribute @c AttrType to declaration @c D, provided that
213/// @c PassesCheck is true.
214/// Otherwise, emit diagnostic @c DiagID, passing in all parameters
215/// specified in @c ExtraArgs.
216template <typename AttrType, typename... DiagnosticArgs>
218 const AttributeCommonInfo &CI,
219 bool PassesCheck, unsigned DiagID,
220 DiagnosticArgs &&...ExtraArgs) {
221 if (!PassesCheck) {
223 appendDiagnostics(DB, std::forward<DiagnosticArgs>(ExtraArgs)...);
224 return;
225 }
227}
228
229} // namespace clang
230#endif // LLVM_CLANG_SEMA_ATTR_H
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
Defines the clang::SourceLocation class and associated facilities.
Defines the clang::TypeLoc interface and its subclasses.
C Language Family Type Representation.
Attr - This represents one attribute.
Definition Attr.h:46
SourceLocation getLocation() const
Definition Attr.h:99
SourceLocation getLoc() const
Wrapper for source info for block pointers.
Definition TypeLoc.h:1557
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
T * getAttr() const
Definition DeclBase.h:581
void addAttr(Attr *A)
const FunctionType * getFunctionType(bool BlocksToo=true) const
Looks through the Decl's underlying type to extract a FunctionType when possible.
SourceLocation getBeginLoc() const LLVM_READONLY
Definition DeclBase.h:439
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition TypeBase.h:4614
Wrapper for source info for member pointers.
Definition TypeLoc.h:1575
Represents a parameter to a function.
Definition Decl.h:1819
ParsedAttr - Represents a syntactic attribute.
Definition ParsedAttr.h:119
Wrapper for source info for pointers.
Definition TypeLoc.h:1544
A (possibly-)qualified type.
Definition TypeBase.h:938
A generic diagnostic builder for errors which may or may not be deferred.
Definition SemaBase.h:111
ASTContext & getASTContext() const
Definition SemaBase.cpp:9
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID)
Emit a diagnostic.
Definition SemaBase.cpp:61
A trivial tuple used to represent a source range.
Base wrapper for a particular "section" of type source info.
Definition TypeLoc.h:59
UnqualTypeLoc getUnqualifiedLoc() const
Skips past any qualifiers, if this is qualified.
Definition TypeLoc.h:349
T getAsAdjusted() const
Convert to the specified TypeLoc type, returning a null TypeLoc if this TypeLoc is not of the desired...
Definition TypeLoc.h:2766
A container of type source information.
Definition TypeBase.h:8472
TypeLoc getTypeLoc() const
Return the TypeLoc wrapper for the type source info.
Definition TypeLoc.h:267
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
const SemaBase::SemaDiagnosticBuilder & appendDiagnostics(const SemaBase::SemaDiagnosticBuilder &Bldr)
Definition Attr.h:192
void handleSimpleAttributeOrDiagnose(SemaBase &S, Decl *D, const AttributeCommonInfo &CI, bool PassesCheck, unsigned DiagID, DiagnosticArgs &&...ExtraArgs)
Add an attribute AttrType to declaration D, provided that PassesCheck is true.
Definition Attr.h:217
bool hasDeclarator(const Decl *D)
Return true if the given decl has a declarator that should have been processed by Sema::GetTypeForDec...
Definition Attr.h:47
QualType getFunctionOrMethodResultType(const Decl *D)
Definition Attr.h:130
bool isInstanceMethod(const Decl *D)
Definition Attr.h:152
const ParmVarDecl * getFunctionOrMethodParam(const Decl *D, unsigned Idx)
getFunctionOrMethodParam - Return parameter declaration for the given index of the passed Decl,...
Definition Attr.h:77
bool checkAttrMutualExclusion(SemaBase &S, Decl *D, const ParsedAttr &AL)
Diagnose mutually exclusive attributes when present on a given declaration.
Definition Attr.h:167
SourceRange getFunctionOrMethodResultSourceRange(const Decl *D)
Definition Attr.h:136
bool isFunctionOrMethodOrBlockForAttrSubject(const Decl *D)
Return true if the given decl has function type (function or function-typed variable) or an Objective...
Definition Attr.h:41
QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx)
Definition Attr.h:115
const FunctionProtoType * T
bool isFunctionOrMethodVariadic(const Decl *D)
Definition Attr.h:144
bool isFuncOrMethodForAttrSubject(const Decl *D)
isFuncOrMethodForAttrSubject - Return true if the given decl has function type (function or function-...
Definition Attr.h:35
void handleSimpleAttribute(SemaBase &S, Decl *D, const AttributeCommonInfo &CI)
Applies the given attribute to the Decl without performing any additional semantic checking.
Definition Attr.h:207
bool hasImplicitObjectParameter(const Decl *D)
Definition Attr.h:158
bool hasFunctionProto(const Decl *D)
hasFunctionProto - Return true if the given decl has a argument information.
Definition Attr.h:56
unsigned getFunctionOrMethodNumParams(const Decl *D)
getFunctionOrMethodNumParams - Return number of function or method parameters.
Definition Attr.h:65
U cast(CodeGen::Address addr)
Definition Address.h:327
SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx)
Definition Attr.h:124