clang 20.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"
26#include "clang/Sema/SemaBase.h"
27#include "llvm/Support/Casting.h"
28
29namespace clang {
30
31/// isFuncOrMethodForAttrSubject - Return true if the given decl has function
32/// type (function or function-typed variable) or an Objective-C
33/// method.
34inline bool isFuncOrMethodForAttrSubject(const Decl *D) {
35 return (D->getFunctionType() != nullptr) || llvm::isa<ObjCMethodDecl>(D);
36}
37
38/// Return true if the given decl has function type (function or
39/// function-typed variable) or an Objective-C method or a block.
41 return isFuncOrMethodForAttrSubject(D) || llvm::isa<BlockDecl>(D);
42}
43
44/// Return true if the given decl has a declarator that should have
45/// been processed by Sema::GetTypeForDeclarator.
46inline bool hasDeclarator(const Decl *D) {
47 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
48 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) ||
49 isa<TypedefNameDecl>(D) || isa<ObjCPropertyDecl>(D);
50}
51
52/// hasFunctionProto - Return true if the given decl has a argument
53/// information. This decl should have already passed
54/// isFuncOrMethodForAttrSubject or isFunctionOrMethodOrBlockForAttrSubject.
55inline bool hasFunctionProto(const Decl *D) {
56 if (const FunctionType *FnTy = D->getFunctionType())
57 return isa<FunctionProtoType>(FnTy);
58 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
59}
60
61/// getFunctionOrMethodNumParams - Return number of function or method
62/// parameters. It is an error to call this on a K&R function (use
63/// hasFunctionProto first).
64inline unsigned getFunctionOrMethodNumParams(const Decl *D) {
65 if (const FunctionType *FnTy = D->getFunctionType())
66 return cast<FunctionProtoType>(FnTy)->getNumParams();
67 if (const auto *BD = dyn_cast<BlockDecl>(D))
68 return BD->getNumParams();
69 return cast<ObjCMethodDecl>(D)->param_size();
70}
71
73 unsigned Idx) {
74 if (const auto *FD = dyn_cast<FunctionDecl>(D))
75 return FD->getParamDecl(Idx);
76 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
77 return MD->getParamDecl(Idx);
78 if (const auto *BD = dyn_cast<BlockDecl>(D))
79 return BD->getParamDecl(Idx);
80 return nullptr;
81}
82
83inline QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
84 if (const FunctionType *FnTy = D->getFunctionType())
85 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
86 if (const auto *BD = dyn_cast<BlockDecl>(D))
87 return BD->getParamDecl(Idx)->getType();
88
89 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
90}
91
92inline SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
93 if (auto *PVD = getFunctionOrMethodParam(D, Idx))
94 return PVD->getSourceRange();
95 return SourceRange();
96}
97
99 if (const FunctionType *FnTy = D->getFunctionType())
100 return FnTy->getReturnType();
101 return cast<ObjCMethodDecl>(D)->getReturnType();
102}
103
105 if (const auto *FD = dyn_cast<FunctionDecl>(D))
106 return FD->getReturnTypeSourceRange();
107 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
108 return MD->getReturnTypeSourceRange();
109 return SourceRange();
110}
111
112inline bool isFunctionOrMethodVariadic(const Decl *D) {
113 if (const FunctionType *FnTy = D->getFunctionType())
114 return cast<FunctionProtoType>(FnTy)->isVariadic();
115 if (const auto *BD = dyn_cast<BlockDecl>(D))
116 return BD->isVariadic();
117 return cast<ObjCMethodDecl>(D)->isVariadic();
118}
119
120inline bool isInstanceMethod(const Decl *D) {
121 if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(D))
122 return MethodDecl->isInstance();
123 return false;
124}
125
126/// Diagnose mutually exclusive attributes when present on a given
127/// declaration. Returns true if diagnosed.
128template <typename AttrTy>
130 if (const auto *A = D->getAttr<AttrTy>()) {
131 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
132 << AL << A
133 << (AL.isRegularKeywordAttribute() || A->isRegularKeywordAttribute());
134 S.Diag(A->getLocation(), diag::note_conflicting_attribute);
135 return true;
136 }
137 return false;
138}
139
140template <typename AttrTy>
142 if (const auto *A = D->getAttr<AttrTy>()) {
143 S.Diag(AL.getLocation(), diag::err_attributes_are_not_compatible)
144 << &AL << A
145 << (AL.isRegularKeywordAttribute() || A->isRegularKeywordAttribute());
146 Diag(A->getLocation(), diag::note_conflicting_attribute);
147 return true;
148 }
149 return false;
150}
151
152template <typename... DiagnosticArgs>
153const SemaBase::SemaDiagnosticBuilder &
155 return Bldr;
156}
157
158template <typename T, typename... DiagnosticArgs>
159const SemaBase::SemaDiagnosticBuilder &
161 DiagnosticArgs &&...ExtraArgs) {
162 return appendDiagnostics(Bldr << std::forward<T>(ExtraArg),
163 std::forward<DiagnosticArgs>(ExtraArgs)...);
164}
165
166/// Applies the given attribute to the Decl without performing any
167/// additional semantic checking.
168template <typename AttrType>
170 const AttributeCommonInfo &CI) {
171 D->addAttr(::new (S.getASTContext()) AttrType(S.getASTContext(), CI));
172}
173
174/// Add an attribute @c AttrType to declaration @c D, provided that
175/// @c PassesCheck is true.
176/// Otherwise, emit diagnostic @c DiagID, passing in all parameters
177/// specified in @c ExtraArgs.
178template <typename AttrType, typename... DiagnosticArgs>
180 const AttributeCommonInfo &CI,
181 bool PassesCheck, unsigned DiagID,
182 DiagnosticArgs &&...ExtraArgs) {
183 if (!PassesCheck) {
185 appendDiagnostics(DB, std::forward<DiagnosticArgs>(ExtraArgs)...);
186 return;
187 }
188 handleSimpleAttribute<AttrType>(S, D, CI);
189}
190
191} // namespace clang
192#endif // LLVM_CLANG_SEMA_ATTR_H
const Decl * D
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.
C Language Family Type Representation.
Attr - This represents one attribute.
Definition: Attr.h:42
SourceLocation getLocation() const
Definition: Attr.h:95
SourceLocation getLoc() const
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
T * getAttr() const
Definition: DeclBase.h:580
void addAttr(Attr *A)
Definition: DeclBase.cpp:1013
const FunctionType * getFunctionType(bool BlocksToo=true) const
Looks through the Decl's underlying type to extract a FunctionType when possible.
Definition: DeclBase.cpp:1178
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: DeclBase.h:438
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: Type.h:4308
Represents a parameter to a function.
Definition: Decl.h:1722
ParsedAttr - Represents a syntactic attribute.
Definition: ParsedAttr.h:129
A (possibly-)qualified type.
Definition: Type.h:941
A generic diagnostic builder for errors which may or may not be deferred.
Definition: SemaBase.h:110
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID, bool DeferHint=false)
Emit a diagnostic.
Definition: SemaBase.cpp:60
ASTContext & getASTContext() const
Definition: Sema.h:560
A trivial tuple used to represent a source range.
The JSON file list parser is used to communicate input to InstallAPI.
const SemaBase::SemaDiagnosticBuilder & appendDiagnostics(const SemaBase::SemaDiagnosticBuilder &Bldr)
Definition: Attr.h:154
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:179
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:46
QualType getFunctionOrMethodResultType(const Decl *D)
Definition: Attr.h:98
bool isInstanceMethod(const Decl *D)
Definition: Attr.h:120
const ParmVarDecl * getFunctionOrMethodParam(const Decl *D, unsigned Idx)
Definition: Attr.h:72
bool checkAttrMutualExclusion(SemaBase &S, Decl *D, const ParsedAttr &AL)
Diagnose mutually exclusive attributes when present on a given declaration.
Definition: Attr.h:129
SourceRange getFunctionOrMethodResultSourceRange(const Decl *D)
Definition: Attr.h:104
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:40
QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx)
Definition: Attr.h:83
bool isFunctionOrMethodVariadic(const Decl *D)
Definition: Attr.h:112
bool isFuncOrMethodForAttrSubject(const Decl *D)
isFuncOrMethodForAttrSubject - Return true if the given decl has function type (function or function-...
Definition: Attr.h:34
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:169
const FunctionProtoType * T
bool hasFunctionProto(const Decl *D)
hasFunctionProto - Return true if the given decl has a argument information.
Definition: Attr.h:55
unsigned getFunctionOrMethodNumParams(const Decl *D)
getFunctionOrMethodNumParams - Return number of function or method parameters.
Definition: Attr.h:64
SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx)
Definition: Attr.h:92