blob: 5b85bf0da2c9fd988da8deba8c4e4600b1f70c54 [file] [log] [blame]
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000015#include "clang/AST/ASTContext.h"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000016#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000020#include "clang/AST/Expr.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000021#include "clang/AST/ExprCXX.h"
Rafael Espindoladb77c4a2013-02-26 19:13:56 +000022#include "clang/AST/Mangle.h"
Alex Denisovfde64952015-06-26 05:28:36 +000023#include "clang/AST/ASTMutationListener.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000024#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000025#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000026#include "clang/Basic/TargetInfo.h"
Benjamin Kramer6ee15622013-09-13 15:35:43 +000027#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000028#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000029#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000030#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000031#include "clang/Sema/Scope.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000032#include "llvm/ADT/StringExtras.h"
Hal Finkelee90a222014-09-26 05:04:30 +000033#include "llvm/Support/MathExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000034using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000035using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000036
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000037namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000038 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000039 C,
40 Cpp,
41 ObjC
42 };
43}
44
Chris Lattner58418ff2008-06-29 00:16:31 +000045//===----------------------------------------------------------------------===//
46// Helper functions
47//===----------------------------------------------------------------------===//
48
Ted Kremenek527042b2009-08-14 20:49:40 +000049/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000050/// type (function or function-typed variable) or an Objective-C
51/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000052static bool isFunctionOrMethod(const Decl *D) {
Craig Topperc3ec1492014-05-26 06:22:03 +000053 return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000054}
David Majnemer06864812015-04-07 06:01:53 +000055/// \brief Return true if the given decl has function type (function or
56/// function-typed variable) or an Objective-C method or a block.
57static bool isFunctionOrMethodOrBlock(const Decl *D) {
58 return isFunctionOrMethod(D) || isa<BlockDecl>(D);
59}
Fariborz Jahanian4447e172009-05-15 23:15:03 +000060
John McCall3882ace2011-01-05 12:14:39 +000061/// Return true if the given decl has a declarator that should have
62/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000063static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000064 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000065 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
66 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +000067}
68
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000069/// hasFunctionProto - Return true if the given decl has a argument
70/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000071/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000072static bool hasFunctionProto(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000073 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000074 return isa<FunctionProtoType>(FnTy);
Aaron Ballman12b9f652014-01-16 13:55:42 +000075 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000076}
77
Alp Toker601b22c2014-01-21 23:35:24 +000078/// getFunctionOrMethodNumParams - Return number of function or method
79/// parameters. It is an error to call this on a K&R function (use
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000080/// hasFunctionProto first).
Alp Toker601b22c2014-01-21 23:35:24 +000081static unsigned getFunctionOrMethodNumParams(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000082 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000083 return cast<FunctionProtoType>(FnTy)->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000084 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000085 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000086 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000087}
88
Alp Toker601b22c2014-01-21 23:35:24 +000089static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000090 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000091 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +000092 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000093 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +000094
Alp Toker03376dc2014-07-07 09:02:20 +000095 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000096}
97
Aaron Ballman4bfa0de2014-08-01 12:58:11 +000098static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
99 if (const auto *FD = dyn_cast<FunctionDecl>(D))
100 return FD->getParamDecl(Idx)->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000101 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000102 return MD->parameters()[Idx]->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000103 if (const auto *BD = dyn_cast<BlockDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000104 return BD->getParamDecl(Idx)->getSourceRange();
105 return SourceRange();
106}
107
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000108static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000109 if (const FunctionType *FnTy = D->getFunctionType())
Aaron Ballman6288d062014-07-11 16:31:29 +0000110 return cast<FunctionType>(FnTy)->getReturnType();
Alp Toker314cc812014-01-25 16:55:45 +0000111 return cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000112}
113
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000114static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) {
115 if (const auto *FD = dyn_cast<FunctionDecl>(D))
116 return FD->getReturnTypeSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000117 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000118 return MD->getReturnTypeSourceRange();
119 return SourceRange();
120}
121
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000122static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000123 if (const FunctionType *FnTy = D->getFunctionType()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000124 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000125 return proto->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000126 }
Aaron Ballmane13d0092014-08-01 17:02:34 +0000127 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
128 return BD->isVariadic();
129
130 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000131}
132
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000133static bool isInstanceMethod(const Decl *D) {
134 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000135 return MethodDecl->isInstance();
136 return false;
137}
138
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000139static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000140 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000141 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000142 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000143
John McCall96fa4842010-05-17 21:00:27 +0000144 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
145 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000146 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000147
John McCall96fa4842010-05-17 21:00:27 +0000148 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000149
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000150 // FIXME: Should we walk the chain of classes?
151 return ClsName == &Ctx.Idents.get("NSString") ||
152 ClsName == &Ctx.Idents.get("NSMutableString");
153}
154
Daniel Dunbar980c6692008-09-26 03:32:58 +0000155static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000156 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000157 if (!PT)
158 return false;
159
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000160 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000161 if (!RT)
162 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000163
Daniel Dunbar980c6692008-09-26 03:32:58 +0000164 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000165 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000166 return false;
167
168 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
169}
170
Richard Smithb87c4652013-10-31 21:23:20 +0000171static unsigned getNumAttributeArgs(const AttributeList &Attr) {
172 // FIXME: Include the type in the argument list.
173 return Attr.getNumArgs() + Attr.hasParsedType();
174}
175
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000176template <typename Compare>
177static bool checkAttributeNumArgsImpl(Sema &S, const AttributeList &Attr,
178 unsigned Num, unsigned Diag,
179 Compare Comp) {
180 if (Comp(getNumAttributeArgs(Attr), Num)) {
181 S.Diag(Attr.getLoc(), Diag) << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000182 return false;
183 }
184
185 return true;
186}
187
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000188/// \brief Check if the attribute has exactly as many args as Num. May
189/// output an error.
190static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
191 unsigned Num) {
192 return checkAttributeNumArgsImpl(S, Attr, Num,
193 diag::err_attribute_wrong_number_arguments,
194 std::not_equal_to<unsigned>());
195}
196
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000197/// \brief Check if the attribute has at least as many args as Num. May
198/// output an error.
199static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000200 unsigned Num) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000201 return checkAttributeNumArgsImpl(S, Attr, Num,
202 diag::err_attribute_too_few_arguments,
203 std::less<unsigned>());
204}
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000205
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000206/// \brief Check if the attribute has at most as many args as Num. May
207/// output an error.
208static bool checkAttributeAtMostNumArgs(Sema &S, const AttributeList &Attr,
209 unsigned Num) {
210 return checkAttributeNumArgsImpl(S, Attr, Num,
211 diag::err_attribute_too_many_arguments,
212 std::greater<unsigned>());
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000213}
214
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000215/// \brief If Expr is a valid integer constant, get the value of the integer
216/// expression and return success or failure. May output an error.
217static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
218 const Expr *Expr, uint32_t &Val,
219 unsigned Idx = UINT_MAX) {
220 llvm::APSInt I(32);
221 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
222 !Expr->isIntegerConstantExpr(I, S.Context)) {
223 if (Idx != UINT_MAX)
224 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
225 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
226 << Expr->getSourceRange();
227 else
228 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
229 << Attr.getName() << AANT_ArgumentIntegerConstant
230 << Expr->getSourceRange();
231 return false;
232 }
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000233
234 if (!I.isIntN(32)) {
Aaron Ballman31f42312014-07-24 14:51:23 +0000235 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
236 << I.toString(10, false) << 32 << /* Unsigned */ 1;
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000237 return false;
238 }
239
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000240 Val = (uint32_t)I.getZExtValue();
241 return true;
242}
243
Aaron Ballmanfb763042013-12-02 18:05:46 +0000244/// \brief Diagnose mutually exclusive attributes when present on a given
245/// declaration. Returns true if diagnosed.
246template <typename AttrTy>
247static bool checkAttrMutualExclusion(Sema &S, Decl *D,
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000248 const AttributeList &Attr) {
249 if (AttrTy *A = D->getAttr<AttrTy>()) {
Aaron Ballmanfb763042013-12-02 18:05:46 +0000250 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000251 << Attr.getName() << A;
Aaron Ballmanfb763042013-12-02 18:05:46 +0000252 return true;
253 }
254 return false;
255}
256
Alp Toker601b22c2014-01-21 23:35:24 +0000257/// \brief Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000258/// instance method D. May output an error.
259///
260/// \returns true if IdxExpr is a valid index.
Alp Toker601b22c2014-01-21 23:35:24 +0000261static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D,
262 const AttributeList &Attr,
263 unsigned AttrArgNum,
264 const Expr *IdxExpr,
265 uint64_t &Idx) {
David Majnemer06864812015-04-07 06:01:53 +0000266 assert(isFunctionOrMethodOrBlock(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000267
268 // In C++ the implicit 'this' function parameter also counts.
269 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000270 bool HP = hasFunctionProto(D);
271 bool HasImplicitThisParam = isInstanceMethod(D);
272 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000273 unsigned NumParams =
274 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000275
276 llvm::APSInt IdxInt;
277 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
278 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000279 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
280 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
281 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000282 return false;
283 }
284
285 Idx = IdxInt.getLimitedValue();
Alp Toker601b22c2014-01-21 23:35:24 +0000286 if (Idx < 1 || (!IV && Idx > NumParams)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000287 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
288 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000289 return false;
290 }
291 Idx--; // Convert to zero-based.
292 if (HasImplicitThisParam) {
293 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000294 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000295 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000296 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000297 return false;
298 }
299 --Idx;
300 }
301
302 return true;
303}
304
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000305/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
306/// If not emit an error and return false. If the argument is an identifier it
307/// will emit an error with a fixit hint and treat it as if it was a string
308/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000309bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
310 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000311 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000312 // Look for identifiers. If we have one emit a hint to fix it to a literal.
313 if (Attr.isArgIdent(ArgNum)) {
314 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000315 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000316 << Attr.getName() << AANT_ArgumentString
317 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000318 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000319 Str = Loc->Ident->getName();
320 if (ArgLocation)
321 *ArgLocation = Loc->Loc;
322 return true;
323 }
324
325 // Now check for an actual string literal.
326 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
327 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
328 if (ArgLocation)
329 *ArgLocation = ArgExpr->getLocStart();
330
331 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000332 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000333 << Attr.getName() << AANT_ArgumentString;
334 return false;
335 }
336
337 Str = Literal->getString();
338 return true;
339}
340
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000341/// \brief Applies the given attribute to the Decl without performing any
342/// additional semantic checking.
343template <typename AttrType>
344static void handleSimpleAttribute(Sema &S, Decl *D,
345 const AttributeList &Attr) {
346 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
347 Attr.getAttributeSpellingListIndex()));
348}
349
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000350/// \brief Check if the passed-in expression is of type int or bool.
351static bool isIntOrBool(Expr *Exp) {
352 QualType QT = Exp->getType();
353 return QT->isBooleanType() || QT->isIntegerType();
354}
355
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000356
357// Check to see if the type is a smart pointer of some kind. We assume
358// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000359static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
Richard Smithcf4bdde2015-02-21 02:45:19 +0000360 DeclContextLookupResult Res1 = RT->getDecl()->lookup(
361 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000362 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000363 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000364
Richard Smithcf4bdde2015-02-21 02:45:19 +0000365 DeclContextLookupResult Res2 = RT->getDecl()->lookup(
366 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000367 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000368 return false;
369
370 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000371}
372
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000373/// \brief Check if passed in Decl is a pointer type.
374/// Note that this function may produce an error message.
375/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000376static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
377 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000378 const ValueDecl *vd = cast<ValueDecl>(D);
379 QualType QT = vd->getType();
380 if (QT->isAnyPointerType())
381 return true;
382
383 if (const RecordType *RT = QT->getAs<RecordType>()) {
384 // If it's an incomplete type, it could be a smart pointer; skip it.
385 // (We don't want to force template instantiation if we can avoid it,
386 // since that would alter the order in which templates are instantiated.)
387 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000388 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000389
Aaron Ballman553e6812013-12-26 14:54:11 +0000390 if (threadSafetyCheckIsSmartPointer(S, RT))
391 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000392 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000393
394 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000395 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000396 return false;
397}
398
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000399/// \brief Checks that the passed in QualType either is of RecordType or points
400/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000401static const RecordType *getRecordType(QualType QT) {
402 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000403 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000404
405 // Now check if we point to record type.
406 if (const PointerType *PT = QT->getAs<PointerType>())
407 return PT->getPointeeType()->getAs<RecordType>();
408
Craig Topperc3ec1492014-05-26 06:22:03 +0000409 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000410}
411
Aaron Ballman76050722014-04-04 15:13:57 +0000412static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000413 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000414
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000415 if (!RT)
416 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000417
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000418 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000419 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000420 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000421
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000422 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000423 // FIXME -- Check the type that the smart pointer points to.
424 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000425 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000426
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000427 // Check if the record itself has a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000428 RecordDecl *RD = RT->getDecl();
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000429 if (RD->hasAttr<CapabilityAttr>())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000430 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000431
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000432 // Else check if any base classes have a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000433 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
434 CXXBasePaths BPaths(false, false);
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000435 if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &) {
436 const auto *Type = BS->getType()->getAs<RecordType>();
437 return Type->getDecl()->hasAttr<CapabilityAttr>();
438 }, BPaths))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000439 return true;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000440 }
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000441 return false;
442}
443
Aaron Ballman76050722014-04-04 15:13:57 +0000444static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000445 const auto *TD = Ty->getAs<TypedefType>();
446 if (!TD)
447 return false;
448
449 TypedefNameDecl *TN = TD->getDecl();
450 if (!TN)
451 return false;
452
453 return TN->hasAttr<CapabilityAttr>();
454}
455
Aaron Ballman76050722014-04-04 15:13:57 +0000456static bool typeHasCapability(Sema &S, QualType Ty) {
457 if (checkTypedefTypeForCapability(Ty))
458 return true;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000459
Aaron Ballman76050722014-04-04 15:13:57 +0000460 if (checkRecordTypeForCapability(S, Ty))
461 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000462
Aaron Ballman76050722014-04-04 15:13:57 +0000463 return false;
464}
465
466static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
467 // Capability expressions are simple expressions involving the boolean logic
468 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
469 // a DeclRefExpr is found, its type should be checked to determine whether it
470 // is a capability or not.
471
472 if (const auto *E = dyn_cast<DeclRefExpr>(Ex))
473 return typeHasCapability(S, E->getType());
474 else if (const auto *E = dyn_cast<CastExpr>(Ex))
475 return isCapabilityExpr(S, E->getSubExpr());
476 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
477 return isCapabilityExpr(S, E->getSubExpr());
478 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
479 if (E->getOpcode() == UO_LNot)
480 return isCapabilityExpr(S, E->getSubExpr());
481 return false;
482 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
483 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
484 return isCapabilityExpr(S, E->getLHS()) &&
485 isCapabilityExpr(S, E->getRHS());
486 return false;
487 }
488
489 return false;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000490}
491
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000492/// \brief Checks that all attribute arguments, starting from Sidx, resolve to
493/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000494/// \param Sidx The attribute argument index to start checking with.
495/// \param ParamIdxOk Whether an argument can be indexing into a function
496/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000497static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
498 const AttributeList &Attr,
499 SmallVectorImpl<Expr *> &Args,
500 int Sidx = 0,
501 bool ParamIdxOk = false) {
502 for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000503 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000504
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000505 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000506 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000507 Args.push_back(ArgExp);
508 continue;
509 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000510
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000511 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000512 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000513 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000514 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000515 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000516 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000517 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000518 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000519
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000520 // We allow constant strings to be used as a placeholder for expressions
521 // that are not valid C++ syntax, but warn that they are ignored.
522 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
523 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000524 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000525 continue;
526 }
527
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000528 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000529
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000530 // A pointer to member expression of the form &MyClass::mu is treated
531 // specially -- we need to look at the type of the member.
532 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
533 if (UOp->getOpcode() == UO_AddrOf)
534 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
535 if (DRE->getDecl()->isCXXInstanceMember())
536 ArgTy = DRE->getDecl()->getType();
537
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000538 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000539 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000540
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000541 // Now check if we index into a record type function param.
542 if(!RT && ParamIdxOk) {
543 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000544 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
545 if(FD && IL) {
546 unsigned int NumParams = FD->getNumParams();
547 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000548 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
549 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
550 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000551 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
552 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000553 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000554 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000555 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000556 }
557 }
558
Aaron Ballman76050722014-04-04 15:13:57 +0000559 // If the type does not have a capability, see if the components of the
560 // expression have capabilities. This allows for writing C code where the
561 // capability may be on the type, and the expression is a capability
562 // boolean logic expression. Eg) requires_capability(A || B && !C)
563 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
564 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
565 << Attr.getName() << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000566
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000567 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000568 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000569}
570
Chris Lattner58418ff2008-06-29 00:16:31 +0000571//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000572// Attribute Implementations
573//===----------------------------------------------------------------------===//
574
Michael Hana9171bc2012-08-03 17:40:43 +0000575static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000576 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000577 if (!threadSafetyCheckIsPointer(S, D, Attr))
578 return;
579
Michael Han99315932013-01-24 16:46:58 +0000580 D->addAttr(::new (S.Context)
581 PtGuardedVarAttr(Attr.getRange(), S.Context,
582 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000583}
584
Michael Hana9171bc2012-08-03 17:40:43 +0000585static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
586 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000587 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000588 SmallVector<Expr*, 1> Args;
589 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000590 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000591 unsigned Size = Args.size();
592 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000593 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000594
Michael Han3be3b442012-07-23 18:48:41 +0000595 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000596
Michael Han3be3b442012-07-23 18:48:41 +0000597 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000598}
599
Michael Han3be3b442012-07-23 18:48:41 +0000600static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000601 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000602 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
603 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000604
Aaron Ballman36a53502014-01-16 13:03:14 +0000605 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg,
606 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000607}
608
Michael Hana9171bc2012-08-03 17:40:43 +0000609static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000610 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000611 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000612 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
613 return;
614
615 if (!threadSafetyCheckIsPointer(S, D, Attr))
616 return;
617
618 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +0000619 S.Context, Arg,
620 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000621}
622
Michael Hana9171bc2012-08-03 17:40:43 +0000623static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
624 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000625 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000626 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000627 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000628
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000629 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000630 QualType QT = cast<ValueDecl>(D)->getType();
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000631 if (!QT->isDependentType() && !typeHasCapability(S, QT)) {
632 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
633 << Attr.getName();
634 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000635 }
636
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000637 // Check that all arguments are lockable objects.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000638 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000639 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000640 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000641
Michael Han3be3b442012-07-23 18:48:41 +0000642 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000643}
644
Michael Hana9171bc2012-08-03 17:40:43 +0000645static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000646 const AttributeList &Attr) {
647 SmallVector<Expr*, 1> Args;
648 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
649 return;
650
651 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000652 D->addAttr(::new (S.Context)
653 AcquiredAfterAttr(Attr.getRange(), S.Context,
654 StartArg, Args.size(),
655 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000656}
657
Michael Hana9171bc2012-08-03 17:40:43 +0000658static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000659 const AttributeList &Attr) {
660 SmallVector<Expr*, 1> Args;
661 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
662 return;
663
664 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000665 D->addAttr(::new (S.Context)
666 AcquiredBeforeAttr(Attr.getRange(), S.Context,
667 StartArg, Args.size(),
668 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000669}
670
Michael Hana9171bc2012-08-03 17:40:43 +0000671static bool checkLockFunAttrCommon(Sema &S, Decl *D,
672 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000673 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000674 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000675 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000676 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000677
Michael Han3be3b442012-07-23 18:48:41 +0000678 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000679}
680
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000681static void handleAssertSharedLockAttr(Sema &S, Decl *D,
682 const AttributeList &Attr) {
683 SmallVector<Expr*, 1> Args;
684 if (!checkLockFunAttrCommon(S, D, Attr, Args))
685 return;
686
687 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000688 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000689 D->addAttr(::new (S.Context)
690 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
691 Attr.getAttributeSpellingListIndex()));
692}
693
694static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
695 const AttributeList &Attr) {
696 SmallVector<Expr*, 1> Args;
697 if (!checkLockFunAttrCommon(S, D, Attr, Args))
698 return;
699
700 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000701 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000702 D->addAttr(::new (S.Context)
703 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
704 StartArg, Size,
705 Attr.getAttributeSpellingListIndex()));
706}
707
708
Michael Hana9171bc2012-08-03 17:40:43 +0000709static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
710 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000711 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000712 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000713 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000714
Aaron Ballman00e99962013-08-31 01:11:41 +0000715 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000716 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000717 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000718 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000719 }
720
721 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000722 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000723
Michael Han3be3b442012-07-23 18:48:41 +0000724 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000725}
726
Michael Hana9171bc2012-08-03 17:40:43 +0000727static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000728 const AttributeList &Attr) {
729 SmallVector<Expr*, 2> Args;
730 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
731 return;
732
Michael Han99315932013-01-24 16:46:58 +0000733 D->addAttr(::new (S.Context)
734 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000735 Attr.getArgAsExpr(0),
736 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000737 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000738}
739
Michael Hana9171bc2012-08-03 17:40:43 +0000740static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000741 const AttributeList &Attr) {
742 SmallVector<Expr*, 2> Args;
743 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
744 return;
745
Nico Weber462fd1e2015-01-07 23:50:05 +0000746 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(
747 Attr.getRange(), S.Context, Attr.getArgAsExpr(0), Args.data(),
748 Args.size(), Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000749}
750
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000751static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000752 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000753 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000754 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000755 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000756 unsigned Size = Args.size();
757 if (Size == 0)
758 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000759
Michael Han99315932013-01-24 16:46:58 +0000760 D->addAttr(::new (S.Context)
761 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
762 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000763}
764
765static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000766 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000767 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000768 return;
769
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000770 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000771 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000772 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000773 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000774 if (Size == 0)
775 return;
776 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000777
Michael Han99315932013-01-24 16:46:58 +0000778 D->addAttr(::new (S.Context)
779 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
780 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000781}
782
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000783static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
784 Expr *Cond = Attr.getArgAsExpr(0);
785 if (!Cond->isTypeDependent()) {
786 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
787 if (Converted.isInvalid())
788 return;
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000789 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000790 }
791
792 StringRef Msg;
793 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
794 return;
795
796 SmallVector<PartialDiagnosticAt, 8> Diags;
797 if (!Cond->isValueDependent() &&
798 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
799 Diags)) {
800 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
801 for (int I = 0, N = Diags.size(); I != N; ++I)
802 S.Diag(Diags[I].first, Diags[I].second);
803 return;
804 }
805
806 D->addAttr(::new (S.Context)
807 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
808 Attr.getAttributeSpellingListIndex()));
809}
810
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000811static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000812 ConsumableAttr::ConsumedState DefaultState;
813
814 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000815 IdentifierLoc *IL = Attr.getArgAsIdent(0);
816 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
817 DefaultState)) {
818 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
819 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000820 return;
821 }
David Blaikie16f76d22013-09-06 01:28:43 +0000822 } else {
823 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
824 << Attr.getName() << AANT_ArgumentIdentifier;
825 return;
826 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000827
828 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000829 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000830 Attr.getAttributeSpellingListIndex()));
831}
832
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000833
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000834static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
835 const AttributeList &Attr) {
836 ASTContext &CurrContext = S.getASTContext();
837 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
838
839 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
840 if (!RD->hasAttr<ConsumableAttr>()) {
841 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
842 RD->getNameAsString();
843
844 return false;
845 }
846 }
847
848 return true;
849}
850
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000851
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000852static void handleCallableWhenAttr(Sema &S, Decl *D,
853 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000854 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
855 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000856
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000857 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
858 return;
859
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000860 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
861 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
862 CallableWhenAttr::ConsumedState CallableState;
863
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000864 StringRef StateString;
865 SourceLocation Loc;
Aaron Ballman55ef1512014-12-19 16:42:04 +0000866 if (Attr.isArgIdent(ArgIndex)) {
867 IdentifierLoc *Ident = Attr.getArgAsIdent(ArgIndex);
868 StateString = Ident->Ident->getName();
869 Loc = Ident->Loc;
870 } else {
871 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
872 return;
873 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000874
875 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000876 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000877 S.Diag(Loc, diag::warn_attribute_type_not_supported)
878 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000879 return;
880 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000881
882 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000883 }
884
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000885 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000886 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
887 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000888}
889
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000890
DeLesley Hutchins69391772013-10-17 23:23:53 +0000891static void handleParamTypestateAttr(Sema &S, Decl *D,
892 const AttributeList &Attr) {
DeLesley Hutchins69391772013-10-17 23:23:53 +0000893 ParamTypestateAttr::ConsumedState ParamState;
894
895 if (Attr.isArgIdent(0)) {
896 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
897 StringRef StateString = Ident->Ident->getName();
898
899 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
900 ParamState)) {
901 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
902 << Attr.getName() << StateString;
903 return;
904 }
905 } else {
906 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
907 Attr.getName() << AANT_ArgumentIdentifier;
908 return;
909 }
910
911 // FIXME: This check is currently being done in the analysis. It can be
912 // enabled here only after the parser propagates attributes at
913 // template specialization definition, not declaration.
914 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
915 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
916 //
917 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
918 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
919 // ReturnType.getAsString();
920 // return;
921 //}
922
923 D->addAttr(::new (S.Context)
924 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
925 Attr.getAttributeSpellingListIndex()));
926}
927
928
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000929static void handleReturnTypestateAttr(Sema &S, Decl *D,
930 const AttributeList &Attr) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000931 ReturnTypestateAttr::ConsumedState ReturnState;
932
933 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000934 IdentifierLoc *IL = Attr.getArgAsIdent(0);
935 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
936 ReturnState)) {
937 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
938 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000939 return;
940 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000941 } else {
942 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
943 Attr.getName() << AANT_ArgumentIdentifier;
944 return;
945 }
946
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000947 // FIXME: This check is currently being done in the analysis. It can be
948 // enabled here only after the parser propagates attributes at
949 // template specialization definition, not declaration.
950 //QualType ReturnType;
951 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000952 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
953 // ReturnType = Param->getType();
954 //
955 //} else if (const CXXConstructorDecl *Constructor =
956 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000957 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
958 //
959 //} else {
960 //
961 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
962 //}
963 //
964 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
965 //
966 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
967 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
968 // ReturnType.getAsString();
969 // return;
970 //}
971
972 D->addAttr(::new (S.Context)
973 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
974 Attr.getAttributeSpellingListIndex()));
975}
976
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000977
978static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000979 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
980 return;
981
982 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000983 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000984 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
985 StringRef Param = Ident->Ident->getName();
986 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
987 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
988 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000989 return;
990 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000991 } else {
992 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
993 Attr.getName() << AANT_ArgumentIdentifier;
994 return;
995 }
996
997 D->addAttr(::new (S.Context)
998 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
999 Attr.getAttributeSpellingListIndex()));
1000}
1001
Chris Wailes9385f9f2013-10-29 20:28:41 +00001002static void handleTestTypestateAttr(Sema &S, Decl *D,
1003 const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001004 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1005 return;
1006
Chris Wailes9385f9f2013-10-29 20:28:41 +00001007 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001008 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001009 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1010 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001011 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001012 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1013 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001014 return;
1015 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001016 } else {
1017 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1018 Attr.getName() << AANT_ArgumentIdentifier;
1019 return;
1020 }
1021
1022 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001023 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001024 Attr.getAttributeSpellingListIndex()));
1025}
1026
Chandler Carruthedc2c642011-07-02 00:01:44 +00001027static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1028 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001029 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001030 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001031}
1032
Chandler Carruthedc2c642011-07-02 00:01:44 +00001033static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001034 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Aaron Ballman36a53502014-01-16 13:03:14 +00001035 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context,
1036 Attr.getAttributeSpellingListIndex()));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001037 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001038 // If the alignment is less than or equal to 8 bits, the packed attribute
1039 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001040 if (!FD->getType()->isDependentType() &&
1041 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001042 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001043 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001044 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001045 else
Michael Han99315932013-01-24 16:46:58 +00001046 FD->addAttr(::new (S.Context)
1047 PackedAttr(Attr.getRange(), S.Context,
1048 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001049 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001050 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001051}
1052
Ted Kremenek7fd17232011-09-29 07:02:25 +00001053static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1054 // The IBOutlet/IBOutletCollection attributes only apply to instance
1055 // variables or properties of Objective-C classes. The outlet must also
1056 // have an object reference type.
1057 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1058 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001059 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001060 << Attr.getName() << VD->getType() << 0;
1061 return false;
1062 }
1063 }
1064 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1065 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001066 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001067 << Attr.getName() << PD->getType() << 1;
1068 return false;
1069 }
1070 }
1071 else {
1072 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1073 return false;
1074 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001075
Ted Kremenek7fd17232011-09-29 07:02:25 +00001076 return true;
1077}
1078
Chandler Carruthedc2c642011-07-02 00:01:44 +00001079static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001080 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001081 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001082
Michael Han99315932013-01-24 16:46:58 +00001083 D->addAttr(::new (S.Context)
1084 IBOutletAttr(Attr.getRange(), S.Context,
1085 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001086}
1087
Chandler Carruthedc2c642011-07-02 00:01:44 +00001088static void handleIBOutletCollection(Sema &S, Decl *D,
1089 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001090
1091 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001092 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001093 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1094 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001095 return;
1096 }
1097
Ted Kremenek7fd17232011-09-29 07:02:25 +00001098 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001099 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001100
Richard Smithb1f9a282013-10-31 01:56:18 +00001101 ParsedType PT;
1102
1103 if (Attr.hasParsedType())
1104 PT = Attr.getTypeArg();
1105 else {
1106 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1107 S.getScopeForContext(D->getDeclContext()->getParent()));
1108 if (!PT) {
1109 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1110 return;
1111 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001112 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001113
Craig Topperc3ec1492014-05-26 06:22:03 +00001114 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001115 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1116 if (!QTLoc)
1117 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001118
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001119 // Diagnose use of non-object type in iboutletcollection attribute.
1120 // FIXME. Gnu attribute extension ignores use of builtin types in
1121 // attributes. So, __attribute__((iboutletcollection(char))) will be
1122 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001123 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001124 S.Diag(Attr.getLoc(),
1125 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1126 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001127 return;
1128 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001129
Michael Han99315932013-01-24 16:46:58 +00001130 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001131 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001132 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001133}
1134
Hal Finkelee90a222014-09-26 05:04:30 +00001135bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1136 if (RefOkay) {
1137 if (T->isReferenceType())
1138 return true;
1139 } else {
1140 T = T.getNonReferenceType();
1141 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001142
Hal Finkelee90a222014-09-26 05:04:30 +00001143 // The nonnull attribute, and other similar attributes, can be applied to a
1144 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001145 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001146 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1147 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001148 for (const auto *I : UD->fields()) {
1149 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001150 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1151 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001152 }
1153 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001154 }
1155
1156 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001157}
1158
Ted Kremenek9aedc152014-01-17 06:24:56 +00001159static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001160 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001161 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001162 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001163 if (!S.isValidPointerAttrType(T)) {
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001164 S.Diag(Attr.getLoc(), isReturnValue
1165 ? diag::warn_attribute_return_pointers_only
1166 : diag::warn_attribute_pointers_only)
Hal Finkelee90a222014-09-26 05:04:30 +00001167 << Attr.getName() << AttrParmRange << TypeRange;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001168 return false;
1169 }
1170 return true;
1171}
1172
Chandler Carruthedc2c642011-07-02 00:01:44 +00001173static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001174 SmallVector<unsigned, 8> NonNullArgs;
Richard Smith588bd9b2014-08-27 04:59:42 +00001175 for (unsigned I = 0; I < Attr.getNumArgs(); ++I) {
1176 Expr *Ex = Attr.getArgAsExpr(I);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001177 uint64_t Idx;
Richard Smith588bd9b2014-08-27 04:59:42 +00001178 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001179 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001180
1181 // Is the function argument a pointer type?
Richard Smith588bd9b2014-08-27 04:59:42 +00001182 if (Idx < getFunctionOrMethodNumParams(D) &&
1183 !attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001184 Ex->getSourceRange(),
1185 getFunctionOrMethodParamRange(D, Idx)))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001186 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001187
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001188 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001189 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001190
1191 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001192 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1193 // check if the attribute came from a macro expansion or a template
1194 // instantiation.
1195 if (NonNullArgs.empty() && Attr.getLoc().isFileID() &&
1196 S.ActiveTemplateInstantiations.empty()) {
1197 bool AnyPointers = isFunctionOrMethodVariadic(D);
1198 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1199 I != E && !AnyPointers; ++I) {
1200 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001201 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001202 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001203 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001204
Richard Smith588bd9b2014-08-27 04:59:42 +00001205 if (!AnyPointers)
1206 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001207 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001208
Richard Smith588bd9b2014-08-27 04:59:42 +00001209 unsigned *Start = NonNullArgs.data();
1210 unsigned Size = NonNullArgs.size();
1211 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001212 D->addAttr(::new (S.Context)
Richard Smith588bd9b2014-08-27 04:59:42 +00001213 NonNullAttr(Attr.getRange(), S.Context, Start, Size,
Michael Han99315932013-01-24 16:46:58 +00001214 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001215}
1216
Jordan Rosec9399072014-02-11 17:27:59 +00001217static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1218 const AttributeList &Attr) {
1219 if (Attr.getNumArgs() > 0) {
1220 if (D->getFunctionType()) {
1221 handleNonNullAttr(S, D, Attr);
1222 } else {
1223 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1224 << D->getSourceRange();
1225 }
1226 return;
1227 }
1228
1229 // Is the argument a pointer type?
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001230 if (!attrNonNullArgCheck(S, D->getType(), Attr, SourceRange(),
1231 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001232 return;
1233
1234 D->addAttr(::new (S.Context)
Craig Topperc3ec1492014-05-26 06:22:03 +00001235 NonNullAttr(Attr.getRange(), S.Context, nullptr, 0,
Jordan Rosec9399072014-02-11 17:27:59 +00001236 Attr.getAttributeSpellingListIndex()));
1237}
1238
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001239static void handleReturnsNonNullAttr(Sema &S, Decl *D,
1240 const AttributeList &Attr) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001241 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001242 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1243 if (!attrNonNullArgCheck(S, ResultType, Attr, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001244 /* isReturnValue */ true))
1245 return;
1246
1247 D->addAttr(::new (S.Context)
1248 ReturnsNonNullAttr(Attr.getRange(), S.Context,
1249 Attr.getAttributeSpellingListIndex()));
1250}
1251
Hal Finkelee90a222014-09-26 05:04:30 +00001252static void handleAssumeAlignedAttr(Sema &S, Decl *D,
1253 const AttributeList &Attr) {
1254 Expr *E = Attr.getArgAsExpr(0),
1255 *OE = Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr;
1256 S.AddAssumeAlignedAttr(Attr.getRange(), D, E, OE,
1257 Attr.getAttributeSpellingListIndex());
1258}
1259
1260void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
1261 Expr *OE, unsigned SpellingListIndex) {
1262 QualType ResultType = getFunctionOrMethodResultType(D);
1263 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1264
1265 AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex);
1266 SourceLocation AttrLoc = AttrRange.getBegin();
1267
1268 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1269 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1270 << &TmpAttr << AttrRange << SR;
1271 return;
1272 }
1273
1274 if (!E->isValueDependent()) {
1275 llvm::APSInt I(64);
1276 if (!E->isIntegerConstantExpr(I, Context)) {
1277 if (OE)
1278 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1279 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1280 << E->getSourceRange();
1281 else
1282 Diag(AttrLoc, diag::err_attribute_argument_type)
1283 << &TmpAttr << AANT_ArgumentIntegerConstant
1284 << E->getSourceRange();
1285 return;
1286 }
1287
1288 if (!I.isPowerOf2()) {
1289 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1290 << E->getSourceRange();
1291 return;
1292 }
1293 }
1294
1295 if (OE) {
1296 if (!OE->isValueDependent()) {
1297 llvm::APSInt I(64);
1298 if (!OE->isIntegerConstantExpr(I, Context)) {
1299 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1300 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1301 << OE->getSourceRange();
1302 return;
1303 }
1304 }
1305 }
1306
1307 D->addAttr(::new (Context)
1308 AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex));
1309}
1310
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001311/// Normalize the attribute, __foo__ becomes foo.
1312/// Returns true if normalization was applied.
1313static bool normalizeName(StringRef &AttrName) {
Aaron Ballman62692362015-10-09 13:53:24 +00001314 if (AttrName.size() > 4 && AttrName.startswith("__") &&
1315 AttrName.endswith("__")) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001316 AttrName = AttrName.drop_front(2).drop_back(2);
1317 return true;
1318 }
1319 return false;
1320}
1321
Chandler Carruthedc2c642011-07-02 00:01:44 +00001322static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001323 // This attribute must be applied to a function declaration. The first
1324 // argument to the attribute must be an identifier, the name of the resource,
1325 // for example: malloc. The following arguments must be argument indexes, the
1326 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001327 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001328 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001329 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001330
Aaron Ballman00e99962013-08-31 01:11:41 +00001331 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001332 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001333 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001334 return;
1335 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001336
Richard Smith852e9ce2013-11-27 01:46:48 +00001337 // Figure out our Kind.
1338 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001339 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001340 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001341
Richard Smith852e9ce2013-11-27 01:46:48 +00001342 // Check arguments.
1343 switch (K) {
1344 case OwnershipAttr::Takes:
1345 case OwnershipAttr::Holds:
1346 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001347 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1348 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001349 return;
1350 }
1351 break;
1352 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001353 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001354 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1355 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001356 return;
1357 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001358 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001359 }
1360
Richard Smith852e9ce2013-11-27 01:46:48 +00001361 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001362
Richard Smith852e9ce2013-11-27 01:46:48 +00001363 StringRef ModuleName = Module->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001364 if (normalizeName(ModuleName)) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001365 Module = &S.PP.getIdentifierTable().get(ModuleName);
1366 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001367
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001368 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001369 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1370 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001371 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001372 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001373 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001374
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001375 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001376 QualType T = getFunctionOrMethodParamType(D, Idx);
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001377 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001378 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001379 case OwnershipAttr::Takes:
1380 case OwnershipAttr::Holds:
1381 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1382 Err = 0;
1383 break;
1384 case OwnershipAttr::Returns:
1385 if (!T->isIntegerType())
1386 Err = 1;
1387 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001388 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001389 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001390 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001391 << Ex->getSourceRange();
1392 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001393 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001394
1395 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001396 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001397 // Cannot have two ownership attributes of different kinds for the same
1398 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001399 if (I->getOwnKind() != K && I->args_end() !=
1400 std::find(I->args_begin(), I->args_end(), Idx)) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001401 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001402 << AL.getName() << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001403 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001404 } else if (K == OwnershipAttr::Returns &&
1405 I->getOwnKind() == OwnershipAttr::Returns) {
1406 // A returns attribute conflicts with any other returns attribute using
1407 // a different index. Note, diagnostic reporting is 1-based, but stored
1408 // argument indexes are 0-based.
1409 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1410 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
1411 << *(I->args_begin()) + 1;
1412 if (I->args_size())
1413 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
1414 << (unsigned)Idx + 1 << Ex->getSourceRange();
1415 return;
1416 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001417 }
1418 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001419 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001420 }
1421
1422 unsigned* start = OwnershipArgs.data();
1423 unsigned size = OwnershipArgs.size();
1424 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001425
Michael Han99315932013-01-24 16:46:58 +00001426 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001427 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001428 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001429}
1430
Chandler Carruthedc2c642011-07-02 00:01:44 +00001431static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001432 // Check the attribute arguments.
1433 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001434 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1435 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001436 return;
1437 }
1438
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001439 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001440
Rafael Espindolac18086a2010-02-23 22:00:30 +00001441 // gcc rejects
1442 // class c {
1443 // static int a __attribute__((weakref ("v2")));
1444 // static int b() __attribute__((weakref ("f3")));
1445 // };
1446 // and ignores the attributes of
1447 // void f(void) {
1448 // static int a __attribute__((weakref ("v2")));
1449 // }
1450 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001451 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001452 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001453 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1454 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001455 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001456 }
1457
1458 // The GCC manual says
1459 //
1460 // At present, a declaration to which `weakref' is attached can only
1461 // be `static'.
1462 //
1463 // It also says
1464 //
1465 // Without a TARGET,
1466 // given as an argument to `weakref' or to `alias', `weakref' is
1467 // equivalent to `weak'.
1468 //
1469 // gcc 4.4.1 will accept
1470 // int a7 __attribute__((weakref));
1471 // as
1472 // int a7 __attribute__((weak));
1473 // This looks like a bug in gcc. We reject that for now. We should revisit
1474 // it if this behaviour is actually used.
1475
Rafael Espindolac18086a2010-02-23 22:00:30 +00001476 // GCC rejects
1477 // static ((alias ("y"), weakref)).
1478 // Should we? How to check that weakref is before or after alias?
1479
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001480 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1481 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1482 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001483 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001484 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001485 // GCC will accept anything as the argument of weakref. Should we
1486 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001487 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1488 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001489
Michael Han99315932013-01-24 16:46:58 +00001490 D->addAttr(::new (S.Context)
1491 WeakRefAttr(Attr.getRange(), S.Context,
1492 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001493}
1494
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001495static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1496 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001497 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001498 return;
1499
Douglas Gregore8bbc122011-09-02 00:18:52 +00001500 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001501 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1502 return;
1503 }
1504
David Majnemer2dc81462015-01-19 09:00:28 +00001505 // Aliases should be on declarations, not definitions.
1506 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1507 if (FD->isThisDeclarationADefinition()) {
1508 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD;
1509 return;
1510 }
1511 } else {
1512 const auto *VD = cast<VarDecl>(D);
1513 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
1514 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << VD;
1515 return;
1516 }
1517 }
1518
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001519 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001520
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001521 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001522 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001523}
1524
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001525static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001526 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001527 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001528
Michael Han99315932013-01-24 16:46:58 +00001529 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1530 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001531}
1532
1533static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001534 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001535 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001536
Michael Han99315932013-01-24 16:46:58 +00001537 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1538 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001539}
1540
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001541static void handleTLSModelAttr(Sema &S, Decl *D,
1542 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001543 StringRef Model;
1544 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001545 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001546 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001547 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001548
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001549 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001550 if (Model != "global-dynamic" && Model != "local-dynamic"
1551 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001552 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001553 return;
1554 }
1555
Michael Han99315932013-01-24 16:46:58 +00001556 D->addAttr(::new (S.Context)
1557 TLSModelAttr(Attr.getRange(), S.Context, Model,
1558 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001559}
1560
David Majnemer631a90b2015-02-04 07:23:21 +00001561static void handleRestrictAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1562 QualType ResultType = getFunctionOrMethodResultType(D);
1563 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
1564 D->addAttr(::new (S.Context) RestrictAttr(
1565 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1566 return;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001567 }
1568
David Majnemer631a90b2015-02-04 07:23:21 +00001569 S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only)
1570 << Attr.getName() << getFunctionOrMethodResultSourceRange(D);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001571}
1572
Chandler Carruthedc2c642011-07-02 00:01:44 +00001573static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001574 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001575 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1576 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001577 return;
1578 }
1579
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001580 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1581 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001582}
1583
Chandler Carruthedc2c642011-07-02 00:01:44 +00001584static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001585 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001586
1587 if (S.CheckNoReturnAttr(attr)) return;
1588
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001589 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001590 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001591 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001592 return;
1593 }
1594
Michael Han99315932013-01-24 16:46:58 +00001595 D->addAttr(::new (S.Context)
1596 NoReturnAttr(attr.getRange(), S.Context,
1597 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001598}
1599
1600bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001601 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001602 attr.setInvalid();
1603 return true;
1604 }
1605
1606 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001607}
1608
Chandler Carruthedc2c642011-07-02 00:01:44 +00001609static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1610 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001611
1612 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1613 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00001614 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001615 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00001616 if (!VD || (!VD->getType()->isBlockPointerType() &&
1617 !VD->getType()->isFunctionPointerType())) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001618 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001619 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001620 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001621 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001622 return;
1623 }
1624 }
1625
Michael Han99315932013-01-24 16:46:58 +00001626 D->addAttr(::new (S.Context)
1627 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1628 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001629}
1630
John Thompsoncdb847ba2010-08-09 21:53:52 +00001631// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001632static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001633/*
1634 Returning a Vector Class in Registers
1635
Eric Christopherbc638a82010-12-01 22:13:54 +00001636 According to the PPU ABI specifications, a class with a single member of
1637 vector type is returned in memory when used as the return value of a function.
1638 This results in inefficient code when implementing vector classes. To return
1639 the value in a single vector register, add the vecreturn attribute to the
1640 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001641
1642 Example:
1643
1644 struct Vector
1645 {
1646 __vector float xyzw;
1647 } __attribute__((vecreturn));
1648
1649 Vector Add(Vector lhs, Vector rhs)
1650 {
1651 Vector result;
1652 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1653 return result; // This will be returned in a register
1654 }
1655*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001656 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1657 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001658 return;
1659 }
1660
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001661 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001662 int count = 0;
1663
1664 if (!isa<CXXRecordDecl>(record)) {
1665 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1666 return;
1667 }
1668
1669 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1670 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1671 return;
1672 }
1673
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001674 for (const auto *I : record->fields()) {
1675 if ((count == 1) || !I->getType()->isVectorType()) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001676 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1677 return;
1678 }
1679 count++;
1680 }
1681
Michael Han99315932013-01-24 16:46:58 +00001682 D->addAttr(::new (S.Context)
1683 VecReturnAttr(Attr.getRange(), S.Context,
1684 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001685}
1686
Richard Smithe233fbf2013-01-28 22:42:45 +00001687static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1688 const AttributeList &Attr) {
1689 if (isa<ParmVarDecl>(D)) {
1690 // [[carries_dependency]] can only be applied to a parameter if it is a
1691 // parameter of a function declaration or lambda.
1692 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1693 S.Diag(Attr.getLoc(),
1694 diag::err_carries_dependency_param_not_function_decl);
1695 return;
1696 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001697 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001698
1699 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1700 Attr.getRange(), S.Context,
1701 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001702}
1703
Akira Hatanakac8667622015-11-06 23:56:15 +00001704static void handleNotTailCalledAttr(Sema &S, Decl *D,
1705 const AttributeList &Attr) {
1706 if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr))
1707 return;
1708
1709 D->addAttr(::new (S.Context) NotTailCalledAttr(
1710 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1711}
1712
Chandler Carruthedc2c642011-07-02 00:01:44 +00001713static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001714 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001715 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001716 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001717 return;
1718 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001719 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001720 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001721 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001722 return;
1723 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001724
Michael Han99315932013-01-24 16:46:58 +00001725 D->addAttr(::new (S.Context)
1726 UsedAttr(Attr.getRange(), S.Context,
1727 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001728}
1729
Chandler Carruthedc2c642011-07-02 00:01:44 +00001730static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001731 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001732 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001733 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1734 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001735
Michael Han99315932013-01-24 16:46:58 +00001736 D->addAttr(::new (S.Context)
1737 ConstructorAttr(Attr.getRange(), S.Context, priority,
1738 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001739}
1740
Chandler Carruthedc2c642011-07-02 00:01:44 +00001741static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001742 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001743 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001744 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1745 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001746
Michael Han99315932013-01-24 16:46:58 +00001747 D->addAttr(::new (S.Context)
1748 DestructorAttr(Attr.getRange(), S.Context, priority,
1749 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001750}
1751
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001752template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001753static void handleAttrWithMessage(Sema &S, Decl *D,
1754 const AttributeList &Attr) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001755 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001756 StringRef Str;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001757 if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001758 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001759
Michael Han99315932013-01-24 16:46:58 +00001760 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1761 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001762}
1763
Ted Kremenek438f8db2014-02-22 01:06:05 +00001764static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Ted Kremenek28eace62013-11-23 01:01:34 +00001765 const AttributeList &Attr) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00001766 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Ted Kremenek27cfe102014-02-21 22:49:04 +00001767 S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition)
1768 << Attr.getName() << Attr.getRange();
1769 return;
1770 }
1771
Ted Kremenek28eace62013-11-23 01:01:34 +00001772 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001773 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1774 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001775}
1776
Jordy Rose740b0c22012-05-08 03:27:22 +00001777static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1778 IdentifierInfo *Platform,
1779 VersionTuple Introduced,
1780 VersionTuple Deprecated,
1781 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001782 StringRef PlatformName
1783 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1784 if (PlatformName.empty())
1785 PlatformName = Platform->getName();
1786
1787 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1788 // of these steps are needed).
1789 if (!Introduced.empty() && !Deprecated.empty() &&
1790 !(Introduced <= Deprecated)) {
1791 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1792 << 1 << PlatformName << Deprecated.getAsString()
1793 << 0 << Introduced.getAsString();
1794 return true;
1795 }
1796
1797 if (!Introduced.empty() && !Obsoleted.empty() &&
1798 !(Introduced <= Obsoleted)) {
1799 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1800 << 2 << PlatformName << Obsoleted.getAsString()
1801 << 0 << Introduced.getAsString();
1802 return true;
1803 }
1804
1805 if (!Deprecated.empty() && !Obsoleted.empty() &&
1806 !(Deprecated <= Obsoleted)) {
1807 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1808 << 2 << PlatformName << Obsoleted.getAsString()
1809 << 1 << Deprecated.getAsString();
1810 return true;
1811 }
1812
1813 return false;
1814}
1815
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001816/// \brief Check whether the two versions match.
1817///
1818/// If either version tuple is empty, then they are assumed to match. If
1819/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1820static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1821 bool BeforeIsOkay) {
1822 if (X.empty() || Y.empty())
1823 return true;
1824
1825 if (X == Y)
1826 return true;
1827
1828 if (BeforeIsOkay && X < Y)
1829 return true;
1830
1831 return false;
1832}
1833
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001834AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001835 IdentifierInfo *Platform,
1836 VersionTuple Introduced,
1837 VersionTuple Deprecated,
1838 VersionTuple Obsoleted,
1839 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001840 StringRef Message,
Douglas Gregord2a713e2015-09-30 21:27:42 +00001841 AvailabilityMergeKind AMK,
Michael Han99315932013-01-24 16:46:58 +00001842 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001843 VersionTuple MergedIntroduced = Introduced;
1844 VersionTuple MergedDeprecated = Deprecated;
1845 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001846 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001847 bool OverrideOrImpl = false;
1848 switch (AMK) {
1849 case AMK_None:
1850 case AMK_Redeclaration:
1851 OverrideOrImpl = false;
1852 break;
1853
1854 case AMK_Override:
1855 case AMK_ProtocolImplementation:
1856 OverrideOrImpl = true;
1857 break;
1858 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001859
Rafael Espindolac67f2232012-05-10 02:50:16 +00001860 if (D->hasAttrs()) {
1861 AttrVec &Attrs = D->getAttrs();
1862 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1863 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1864 if (!OldAA) {
1865 ++i;
1866 continue;
1867 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001868
Rafael Espindolac67f2232012-05-10 02:50:16 +00001869 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1870 if (OldPlatform != Platform) {
1871 ++i;
1872 continue;
1873 }
1874
Tim Northover7a73cc72015-10-30 16:30:49 +00001875 // If there is an existing availability attribute for this platform that
1876 // is explicit and the new one is implicit use the explicit one and
1877 // discard the new implicit attribute.
1878 if (OldAA->getRange().isValid() && Range.isInvalid()) {
1879 return nullptr;
1880 }
1881
1882 // If there is an existing attribute for this platform that is implicit
1883 // and the new attribute is explicit then erase the old one and
1884 // continue processing the attributes.
1885 if (Range.isValid() && OldAA->getRange().isInvalid()) {
1886 Attrs.erase(Attrs.begin() + i);
1887 --e;
1888 continue;
1889 }
1890
Rafael Espindolac67f2232012-05-10 02:50:16 +00001891 FoundAny = true;
1892 VersionTuple OldIntroduced = OldAA->getIntroduced();
1893 VersionTuple OldDeprecated = OldAA->getDeprecated();
1894 VersionTuple OldObsoleted = OldAA->getObsoleted();
1895 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001896
Douglas Gregord2a713e2015-09-30 21:27:42 +00001897 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
1898 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
1899 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001900 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00001901 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
1902 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001903 int Which = -1;
1904 VersionTuple FirstVersion;
1905 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001906 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001907 Which = 0;
1908 FirstVersion = OldIntroduced;
1909 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001910 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001911 Which = 1;
1912 FirstVersion = Deprecated;
1913 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001914 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001915 Which = 2;
1916 FirstVersion = Obsoleted;
1917 SecondVersion = OldObsoleted;
1918 }
1919
1920 if (Which == -1) {
1921 Diag(OldAA->getLocation(),
1922 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00001923 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1924 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001925 } else {
1926 Diag(OldAA->getLocation(),
1927 diag::warn_mismatched_availability_override)
1928 << Which
1929 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00001930 << FirstVersion.getAsString() << SecondVersion.getAsString()
1931 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001932 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00001933 if (AMK == AMK_Override)
1934 Diag(Range.getBegin(), diag::note_overridden_method);
1935 else
1936 Diag(Range.getBegin(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001937 } else {
1938 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1939 Diag(Range.getBegin(), diag::note_previous_attribute);
1940 }
1941
Rafael Espindolac67f2232012-05-10 02:50:16 +00001942 Attrs.erase(Attrs.begin() + i);
1943 --e;
1944 continue;
1945 }
1946
1947 VersionTuple MergedIntroduced2 = MergedIntroduced;
1948 VersionTuple MergedDeprecated2 = MergedDeprecated;
1949 VersionTuple MergedObsoleted2 = MergedObsoleted;
1950
1951 if (MergedIntroduced2.empty())
1952 MergedIntroduced2 = OldIntroduced;
1953 if (MergedDeprecated2.empty())
1954 MergedDeprecated2 = OldDeprecated;
1955 if (MergedObsoleted2.empty())
1956 MergedObsoleted2 = OldObsoleted;
1957
1958 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1959 MergedIntroduced2, MergedDeprecated2,
1960 MergedObsoleted2)) {
1961 Attrs.erase(Attrs.begin() + i);
1962 --e;
1963 continue;
1964 }
1965
1966 MergedIntroduced = MergedIntroduced2;
1967 MergedDeprecated = MergedDeprecated2;
1968 MergedObsoleted = MergedObsoleted2;
1969 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001970 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001971 }
1972
1973 if (FoundAny &&
1974 MergedIntroduced == Introduced &&
1975 MergedDeprecated == Deprecated &&
1976 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00001977 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001978
Douglas Gregord2a713e2015-09-30 21:27:42 +00001979 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00001980 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001981 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001982 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00001983 !OverrideOrImpl) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001984 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1985 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001986 Obsoleted, IsUnavailable, Message,
1987 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001988 }
Craig Topperc3ec1492014-05-26 06:22:03 +00001989 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001990}
1991
Chandler Carruthedc2c642011-07-02 00:01:44 +00001992static void handleAvailabilityAttr(Sema &S, Decl *D,
1993 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001994 if (!checkAttributeNumArgs(S, Attr, 1))
1995 return;
1996 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001997 unsigned Index = Attr.getAttributeSpellingListIndex();
1998
Aaron Ballman00e99962013-08-31 01:11:41 +00001999 IdentifierInfo *II = Platform->Ident;
2000 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2001 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2002 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002003
Rafael Espindolac231fab2013-01-08 21:30:32 +00002004 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2005 if (!ND) {
2006 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2007 return;
2008 }
2009
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002010 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2011 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2012 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002013 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002014 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00002015 if (const StringLiteral *SE =
2016 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002017 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002018
Aaron Ballman00e99962013-08-31 01:11:41 +00002019 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002020 Introduced.Version,
2021 Deprecated.Version,
2022 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002023 IsUnavailable, Str,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002024 Sema::AMK_None,
Michael Han99315932013-01-24 16:46:58 +00002025 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002026 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002027 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002028
2029 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2030 // matches before the start of the watchOS platform.
2031 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2032 IdentifierInfo *NewII = nullptr;
2033 if (II->getName() == "ios")
2034 NewII = &S.Context.Idents.get("watchos");
2035 else if (II->getName() == "ios_app_extension")
2036 NewII = &S.Context.Idents.get("watchos_app_extension");
2037
2038 if (NewII) {
2039 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2040 if (Version.empty())
2041 return Version;
2042 auto Major = Version.getMajor();
2043 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2044 if (NewMajor >= 2) {
2045 if (Version.getMinor().hasValue()) {
2046 if (Version.getSubminor().hasValue())
2047 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2048 Version.getSubminor().getValue());
2049 else
2050 return VersionTuple(NewMajor, Version.getMinor().getValue());
2051 }
2052 }
2053
2054 return VersionTuple(2, 0);
2055 };
2056
2057 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2058 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2059 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2060
2061 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
2062 SourceRange(),
2063 NewII,
2064 NewIntroduced,
2065 NewDeprecated,
2066 NewObsoleted,
2067 IsUnavailable, Str,
2068 Sema::AMK_None,
2069 Index);
2070 if (NewAttr)
2071 D->addAttr(NewAttr);
2072 }
2073 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2074 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2075 // matches before the start of the tvOS platform.
2076 IdentifierInfo *NewII = nullptr;
2077 if (II->getName() == "ios")
2078 NewII = &S.Context.Idents.get("tvos");
2079 else if (II->getName() == "ios_app_extension")
2080 NewII = &S.Context.Idents.get("tvos_app_extension");
2081
2082 if (NewII) {
2083 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
2084 SourceRange(),
2085 NewII,
2086 Introduced.Version,
2087 Deprecated.Version,
2088 Obsoleted.Version,
2089 IsUnavailable, Str,
2090 Sema::AMK_None,
2091 Index);
2092 if (NewAttr)
2093 D->addAttr(NewAttr);
2094 }
2095 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002096}
2097
John McCalld041a9b2013-02-20 01:54:26 +00002098template <class T>
2099static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2100 typename T::VisibilityType value,
2101 unsigned attrSpellingListIndex) {
2102 T *existingAttr = D->getAttr<T>();
2103 if (existingAttr) {
2104 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2105 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002106 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002107 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2108 S.Diag(range.getBegin(), diag::note_previous_attribute);
2109 D->dropAttr<T>();
2110 }
2111 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2112}
2113
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002114VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002115 VisibilityAttr::VisibilityType Vis,
2116 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002117 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2118 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002119}
2120
John McCalld041a9b2013-02-20 01:54:26 +00002121TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2122 TypeVisibilityAttr::VisibilityType Vis,
2123 unsigned AttrSpellingListIndex) {
2124 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2125 AttrSpellingListIndex);
2126}
2127
2128static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2129 bool isTypeVisibility) {
2130 // Visibility attributes don't mean anything on a typedef.
2131 if (isa<TypedefNameDecl>(D)) {
2132 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2133 << Attr.getName();
2134 return;
2135 }
2136
2137 // 'type_visibility' can only go on a type or namespace.
2138 if (isTypeVisibility &&
2139 !(isa<TagDecl>(D) ||
2140 isa<ObjCInterfaceDecl>(D) ||
2141 isa<NamespaceDecl>(D))) {
2142 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2143 << Attr.getName() << ExpectedTypeOrNamespace;
2144 return;
2145 }
2146
Benjamin Kramer70370212013-09-09 15:08:57 +00002147 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002148 StringRef TypeStr;
2149 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002150 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002151 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002152
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002153 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002154 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002155 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002156 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002157 return;
2158 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002159
2160 // Complain about attempts to use protected visibility on targets
2161 // (like Darwin) that don't support it.
2162 if (type == VisibilityAttr::Protected &&
2163 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2164 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2165 type = VisibilityAttr::Default;
2166 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002167
Michael Han99315932013-01-24 16:46:58 +00002168 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002169 clang::Attr *newAttr;
2170 if (isTypeVisibility) {
2171 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2172 (TypeVisibilityAttr::VisibilityType) type,
2173 Index);
2174 } else {
2175 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2176 }
2177 if (newAttr)
2178 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002179}
2180
Chandler Carruthedc2c642011-07-02 00:01:44 +00002181static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2182 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002183 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002184 if (!Attr.isArgIdent(0)) {
2185 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2186 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002187 return;
2188 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002189
Aaron Ballman682ee422013-09-11 19:47:58 +00002190 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2191 ObjCMethodFamilyAttr::FamilyKind F;
2192 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2193 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2194 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002195 return;
2196 }
2197
Alp Toker314cc812014-01-25 16:55:45 +00002198 if (F == ObjCMethodFamilyAttr::OMF_init &&
2199 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00002200 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00002201 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002202 // Ignore the attribute.
2203 return;
2204 }
2205
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002206 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00002207 S.Context, F,
2208 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002209}
2210
Chandler Carruthedc2c642011-07-02 00:01:44 +00002211static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002212 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002213 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002214 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002215 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2216 return;
2217 }
2218 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002219 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2220 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002221 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002222 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2223 return;
2224 }
2225 }
2226 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002227 // It is okay to include this attribute on properties, e.g.:
2228 //
2229 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2230 //
2231 // In this case it follows tradition and suppresses an error in the above
2232 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002233 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002234 }
Michael Han99315932013-01-24 16:46:58 +00002235 D->addAttr(::new (S.Context)
2236 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2237 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002238}
2239
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002240static void handleObjCIndependentClass(Sema &S, Decl *D, const AttributeList &Attr) {
2241 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
2242 QualType T = TD->getUnderlyingType();
2243 if (!T->isObjCObjectPointerType()) {
2244 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2245 return;
2246 }
2247 } else {
2248 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2249 return;
2250 }
2251 D->addAttr(::new (S.Context)
2252 ObjCIndependentClassAttr(Attr.getRange(), S.Context,
2253 Attr.getAttributeSpellingListIndex()));
2254}
2255
Chandler Carruthedc2c642011-07-02 00:01:44 +00002256static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002257 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002258 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002259 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002260 return;
2261 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002262
Aaron Ballman00e99962013-08-31 01:11:41 +00002263 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002264 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002265 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2266 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2267 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002268 return;
2269 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002270
Michael Han99315932013-01-24 16:46:58 +00002271 D->addAttr(::new (S.Context)
2272 BlocksAttr(Attr.getRange(), S.Context, type,
2273 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002274}
2275
Chandler Carruthedc2c642011-07-02 00:01:44 +00002276static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002277 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002278 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002279 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002280 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002281 if (E->isTypeDependent() || E->isValueDependent() ||
2282 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002283 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002284 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002285 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002286 return;
2287 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002288
John McCallb46f2872011-09-09 07:56:05 +00002289 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002290 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2291 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002292 return;
2293 }
John McCallb46f2872011-09-09 07:56:05 +00002294
2295 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002296 }
2297
Aaron Ballman18a78382013-11-21 00:28:23 +00002298 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002299 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002300 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002301 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002302 if (E->isTypeDependent() || E->isValueDependent() ||
2303 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002304 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002305 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002306 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002307 return;
2308 }
2309 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002310
John McCallb46f2872011-09-09 07:56:05 +00002311 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002312 // FIXME: This error message could be improved, it would be nice
2313 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002314 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2315 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002316 return;
2317 }
2318 }
2319
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002320 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002321 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002322 if (isa<FunctionNoProtoType>(FT)) {
2323 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2324 return;
2325 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002326
Chris Lattner9363e312009-03-17 23:03:47 +00002327 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002328 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002329 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002330 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002331 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002332 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002333 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002334 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002335 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002336 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2337 if (!BD->isVariadic()) {
2338 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2339 return;
2340 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002341 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002342 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002343 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002344 const FunctionType *FT = Ty->isFunctionPointerType()
2345 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002346 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002347 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002348 int m = Ty->isFunctionPointerType() ? 0 : 1;
2349 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002350 return;
2351 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002352 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002353 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002354 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002355 return;
2356 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002357 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002358 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002359 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002360 return;
2361 }
Michael Han99315932013-01-24 16:46:58 +00002362 D->addAttr(::new (S.Context)
2363 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2364 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002365}
2366
Chandler Carruthedc2c642011-07-02 00:01:44 +00002367static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002368 if (D->getFunctionType() &&
2369 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002370 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2371 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002372 return;
2373 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002374 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002375 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002376 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2377 << Attr.getName() << 1;
2378 return;
2379 }
2380
Michael Han99315932013-01-24 16:46:58 +00002381 D->addAttr(::new (S.Context)
2382 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2383 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002384}
2385
Chandler Carruthedc2c642011-07-02 00:01:44 +00002386static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002387 // weak_import only applies to variable & function declarations.
2388 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002389 if (!D->canBeWeakImported(isDef)) {
2390 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002391 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2392 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002393 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002394 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002395 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002396 // Nothing to warn about here.
2397 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002398 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002399 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002400
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002401 return;
2402 }
2403
Michael Han99315932013-01-24 16:46:58 +00002404 D->addAttr(::new (S.Context)
2405 WeakImportAttr(Attr.getRange(), S.Context,
2406 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002407}
2408
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002409// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002410template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002411static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002412 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002413 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002414 for (unsigned i = 0; i < 3; ++i) {
2415 const Expr *E = Attr.getArgAsExpr(i);
2416 if (!checkUInt32Argument(S, Attr, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002417 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002418 if (WGSize[i] == 0) {
2419 S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero)
2420 << Attr.getName() << E->getSourceRange();
2421 return;
2422 }
2423 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002424
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002425 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2426 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2427 Existing->getYDim() == WGSize[1] &&
2428 Existing->getZDim() == WGSize[2]))
2429 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002430
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002431 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2432 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002433 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002434}
2435
Joey Goulyaba589c2013-03-08 09:42:32 +00002436static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002437 if (!Attr.hasParsedType()) {
2438 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2439 << Attr.getName() << 1;
2440 return;
2441 }
2442
Craig Topperc3ec1492014-05-26 06:22:03 +00002443 TypeSourceInfo *ParmTSI = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00002444 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2445 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002446
2447 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2448 (ParmType->isBooleanType() ||
2449 !ParmType->isIntegralType(S.getASTContext()))) {
2450 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2451 << ParmType;
2452 return;
2453 }
2454
Aaron Ballmana9e05402013-12-02 22:16:55 +00002455 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002456 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002457 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2458 return;
2459 }
2460 }
2461
2462 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002463 ParmTSI,
2464 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002465}
2466
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002467SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002468 StringRef Name,
2469 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002470 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2471 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002472 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002473 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2474 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002475 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002476 }
Michael Han99315932013-01-24 16:46:58 +00002477 return ::new (Context) SectionAttr(Range, Context, Name,
2478 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002479}
2480
Reid Kleckner2a133222015-03-04 23:39:17 +00002481bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2482 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2483 if (!Error.empty()) {
2484 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error;
2485 return false;
2486 }
2487 return true;
2488}
2489
Chandler Carruthedc2c642011-07-02 00:01:44 +00002490static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002491 // Make sure that there is a string literal as the sections's single
2492 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002493 StringRef Str;
2494 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002495 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002496 return;
Mike Stump11289f42009-09-09 15:08:12 +00002497
Reid Kleckner2a133222015-03-04 23:39:17 +00002498 if (!S.checkSectionName(LiteralLoc, Str))
2499 return;
2500
Chris Lattner30ba6742009-08-10 19:03:04 +00002501 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002502 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002503 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002504 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002505 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002506 return;
2507 }
Mike Stump11289f42009-09-09 15:08:12 +00002508
Michael Han99315932013-01-24 16:46:58 +00002509 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002510 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002511 if (NewAttr)
2512 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002513}
2514
Eric Christopher789a7ad2015-06-12 01:36:05 +00002515// Check for things we'd like to warn about, no errors or validation for now.
2516// TODO: Validation should use a backend target library that specifies
2517// the allowable subtarget features and cpus. We could use something like a
2518// TargetCodeGenInfo hook here to do validation.
2519void Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
2520 for (auto Str : {"tune=", "fpmath="})
2521 if (AttrStr.find(Str) != StringRef::npos)
2522 Diag(LiteralLoc, diag::warn_unsupported_target_attribute) << Str;
2523}
2524
Eric Christopher11acf732015-06-12 01:35:52 +00002525static void handleTargetAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eric Christopher11acf732015-06-12 01:35:52 +00002526 StringRef Str;
2527 SourceLocation LiteralLoc;
2528 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
2529 return;
Eric Christopher789a7ad2015-06-12 01:36:05 +00002530 S.checkTargetAttr(LiteralLoc, Str);
Eric Christopher11acf732015-06-12 01:35:52 +00002531 unsigned Index = Attr.getAttributeSpellingListIndex();
2532 TargetAttr *NewAttr =
2533 ::new (S.Context) TargetAttr(Attr.getRange(), S.Context, Str, Index);
2534 D->addAttr(NewAttr);
2535}
2536
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002537
Chandler Carruthedc2c642011-07-02 00:01:44 +00002538static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002539 VarDecl *VD = cast<VarDecl>(D);
2540 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002541 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002542 return;
2543 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002544
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002545 Expr *E = Attr.getArgAsExpr(0);
2546 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00002547 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002548 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002549
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002550 // gcc only allows for simple identifiers. Since we support more than gcc, we
2551 // will warn the user.
2552 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2553 if (DRE->hasQualifier())
2554 S.Diag(Loc, diag::warn_cleanup_ext);
2555 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2556 NI = DRE->getNameInfo();
2557 if (!FD) {
2558 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2559 << NI.getName();
2560 return;
2561 }
2562 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2563 if (ULE->hasExplicitTemplateArgs())
2564 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002565 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2566 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002567 if (!FD) {
2568 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2569 << NI.getName();
2570 if (ULE->getType() == S.Context.OverloadTy)
2571 S.NoteAllOverloadCandidates(ULE);
2572 return;
2573 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002574 } else {
2575 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002576 return;
2577 }
2578
Anders Carlssond277d792009-01-31 01:16:18 +00002579 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002580 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2581 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002582 return;
2583 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002584
Anders Carlsson723f55d2009-02-07 23:16:50 +00002585 // We're currently more strict than GCC about what function types we accept.
2586 // If this ever proves to be a problem it should be easy to fix.
2587 QualType Ty = S.Context.getPointerType(VD->getType());
2588 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002589 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2590 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002591 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2592 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002593 return;
2594 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002595
Michael Han99315932013-01-24 16:46:58 +00002596 D->addAttr(::new (S.Context)
2597 CleanupAttr(Attr.getRange(), S.Context, FD,
2598 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002599}
2600
Mike Stumpd3bb5572009-07-24 19:02:52 +00002601/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002602/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002603static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002604 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002605 uint64_t Idx;
2606 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002607 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002608
Eric Christopherb64963e2015-08-13 21:34:35 +00002609 // Make sure the format string is really a string.
Alp Toker601b22c2014-01-21 23:35:24 +00002610 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002611
Eric Christopherb64963e2015-08-13 21:34:35 +00002612 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
2613 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002614 !isCFStringType(Ty, S.Context) &&
2615 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002616 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002617 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00002618 << "a string type" << IdxExpr->getSourceRange()
2619 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002620 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002621 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002622 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002623 if (!isNSStringType(Ty, S.Context) &&
2624 !isCFStringType(Ty, S.Context) &&
2625 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002626 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002627 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00002628 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00002629 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002630 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002631 }
2632
Alp Toker601b22c2014-01-21 23:35:24 +00002633 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002634 // because that has corrected for the implicit this parameter, and is zero-
2635 // based. The attribute expects what the user wrote explicitly.
2636 llvm::APSInt Val;
2637 IdxExpr->EvaluateAsInt(Val, S.Context);
2638
Michael Han99315932013-01-24 16:46:58 +00002639 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002640 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002641 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002642}
2643
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002644enum FormatAttrKind {
2645 CFStringFormat,
2646 NSStringFormat,
2647 StrftimeFormat,
2648 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002649 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002650 InvalidFormat
2651};
2652
2653/// getFormatAttrKind - Map from format attribute names to supported format
2654/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002655static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002656 return llvm::StringSwitch<FormatAttrKind>(Format)
2657 // Check for formats that get handled specially.
2658 .Case("NSString", NSStringFormat)
2659 .Case("CFString", CFStringFormat)
2660 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002661
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002662 // Otherwise, check for supported formats.
2663 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2664 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2665 .Case("kprintf", SupportedFormat) // OpenBSD.
Dimitry Andric6b5ed342015-02-19 22:32:33 +00002666 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
Fariborz Jahanianf8dce0f2015-02-21 00:45:58 +00002667 .Case("os_trace", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002668
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002669 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2670 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002671}
2672
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002673/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002674/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002675static void handleInitPriorityAttr(Sema &S, Decl *D,
2676 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002677 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002678 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2679 return;
2680 }
2681
Aaron Ballman4a611152013-11-27 16:34:09 +00002682 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002683 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2684 Attr.setInvalid();
2685 return;
2686 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002687 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002688 if (S.Context.getAsArrayType(T))
2689 T = S.Context.getBaseElementType(T);
2690 if (!T->getAs<RecordType>()) {
2691 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2692 Attr.setInvalid();
2693 return;
2694 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002695
2696 Expr *E = Attr.getArgAsExpr(0);
2697 uint32_t prioritynum;
2698 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002699 Attr.setInvalid();
2700 return;
2701 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002702
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002703 if (prioritynum < 101 || prioritynum > 65535) {
2704 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002705 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002706 Attr.setInvalid();
2707 return;
2708 }
Michael Han99315932013-01-24 16:46:58 +00002709 D->addAttr(::new (S.Context)
2710 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2711 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002712}
2713
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002714FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2715 IdentifierInfo *Format, int FormatIdx,
2716 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002717 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002718 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002719 for (auto *F : D->specific_attrs<FormatAttr>()) {
2720 if (F->getType() == Format &&
2721 F->getFormatIdx() == FormatIdx &&
2722 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002723 // If we don't have a valid location for this attribute, adopt the
2724 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002725 if (F->getLocation().isInvalid())
2726 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00002727 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00002728 }
2729 }
2730
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002731 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2732 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002733}
2734
Mike Stumpd3bb5572009-07-24 19:02:52 +00002735/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002736/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002737static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002738 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002739 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002740 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002741 return;
2742 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002743
Chandler Carruth743682b2010-11-16 08:35:43 +00002744 // In C++ the implicit 'this' function parameter also counts, and they are
2745 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002746 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002747 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002748
Aaron Ballman00e99962013-08-31 01:11:41 +00002749 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2750 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002751
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00002752 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002753 // If we've modified the string name, we need a new identifier for it.
2754 II = &S.Context.Idents.get(Format);
2755 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002756
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002757 // Check for supported formats.
2758 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002759
2760 if (Kind == IgnoredFormat)
2761 return;
2762
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002763 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002764 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002765 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002766 return;
2767 }
2768
2769 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002770 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002771 uint32_t Idx;
2772 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002773 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002774
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002775 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002776 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002777 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002778 return;
2779 }
2780
2781 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002782 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002783
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002784 if (HasImplicitThisParam) {
2785 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002786 S.Diag(Attr.getLoc(),
2787 diag::err_format_attribute_implicit_this_format_string)
2788 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002789 return;
2790 }
2791 ArgIdx--;
2792 }
Mike Stump11289f42009-09-09 15:08:12 +00002793
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002794 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002795 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002796
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002797 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002798 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002799 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002800 << "a CFString" << IdxExpr->getSourceRange()
2801 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00002802 return;
2803 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002804 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002805 // FIXME: do we need to check if the type is NSString*? What are the
2806 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002807 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002808 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002809 << "an NSString" << IdxExpr->getSourceRange()
2810 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002811 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002812 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002813 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002814 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002815 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002816 << "a string type" << IdxExpr->getSourceRange()
2817 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002818 return;
2819 }
2820
2821 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002822 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002823 uint32_t FirstArg;
2824 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002825 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002826
2827 // check if the function is variadic if the 3rd argument non-zero
2828 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002829 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002830 ++NumArgs; // +1 for ...
2831 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002832 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002833 return;
2834 }
2835 }
2836
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002837 // strftime requires FirstArg to be 0 because it doesn't read from any
2838 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002839 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002840 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002841 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2842 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002843 return;
2844 }
2845 // if 0 it disables parameter checking (to use with e.g. va_list)
2846 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002847 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002848 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002849 return;
2850 }
2851
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002852 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002853 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002854 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002855 if (NewAttr)
2856 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002857}
2858
Chandler Carruthedc2c642011-07-02 00:01:44 +00002859static void handleTransparentUnionAttr(Sema &S, Decl *D,
2860 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002861 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00002862 RecordDecl *RD = nullptr;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002863 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002864 if (TD && TD->getUnderlyingType()->isUnionType())
2865 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2866 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002867 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002868
2869 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002870 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002871 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002872 return;
2873 }
2874
John McCallf937c022011-10-07 06:10:15 +00002875 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002876 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002877 diag::warn_transparent_union_attribute_not_definition);
2878 return;
2879 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002880
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002881 RecordDecl::field_iterator Field = RD->field_begin(),
2882 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002883 if (Field == FieldEnd) {
2884 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2885 return;
2886 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002887
David Blaikie40ed2972012-06-06 20:45:41 +00002888 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002889 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002890 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002891 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002892 diag::warn_transparent_union_attribute_floating)
2893 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002894 return;
2895 }
2896
2897 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2898 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2899 for (; Field != FieldEnd; ++Field) {
2900 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002901 // FIXME: this isn't fully correct; we also need to test whether the
2902 // members of the union would all have the same calling convention as the
2903 // first member of the union. Checking just the size and alignment isn't
2904 // sufficient (consider structs passed on the stack instead of in registers
2905 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002906 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002907 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002908 // Warn if we drop the attribute.
2909 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002910 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002911 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002912 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002913 diag::warn_transparent_union_attribute_field_size_align)
2914 << isSize << Field->getDeclName() << FieldBits;
2915 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002916 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002917 diag::note_transparent_union_first_field_size_align)
2918 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002919 return;
2920 }
2921 }
2922
Michael Han99315932013-01-24 16:46:58 +00002923 RD->addAttr(::new (S.Context)
2924 TransparentUnionAttr(Attr.getRange(), S.Context,
2925 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002926}
2927
Chandler Carruthedc2c642011-07-02 00:01:44 +00002928static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002929 // Make sure that there is a string literal as the annotation's single
2930 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002931 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002932 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002933 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002934
2935 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002936 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
2937 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002938 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002939 }
Michael Han99315932013-01-24 16:46:58 +00002940
2941 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002942 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002943 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002944}
2945
Hal Finkel1b0d24e2014-10-02 21:21:25 +00002946static void handleAlignValueAttr(Sema &S, Decl *D,
2947 const AttributeList &Attr) {
2948 S.AddAlignValueAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
2949 Attr.getAttributeSpellingListIndex());
2950}
2951
2952void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
2953 unsigned SpellingListIndex) {
2954 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
2955 SourceLocation AttrLoc = AttrRange.getBegin();
2956
2957 QualType T;
2958 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
2959 T = TD->getUnderlyingType();
2960 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2961 T = VD->getType();
2962 else
2963 llvm_unreachable("Unknown decl type for align_value");
2964
2965 if (!T->isDependentType() && !T->isAnyPointerType() &&
2966 !T->isReferenceType() && !T->isMemberPointerType()) {
2967 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
2968 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
2969 return;
2970 }
2971
2972 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00002973 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00002974 ExprResult ICE
2975 = VerifyIntegerConstantExpression(E, &Alignment,
2976 diag::err_align_value_attribute_argument_not_int,
2977 /*AllowFold*/ false);
2978 if (ICE.isInvalid())
2979 return;
2980
2981 if (!Alignment.isPowerOf2()) {
2982 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
2983 << E->getSourceRange();
2984 return;
2985 }
2986
2987 D->addAttr(::new (Context)
2988 AlignValueAttr(AttrRange, Context, ICE.get(),
2989 SpellingListIndex));
2990 return;
2991 }
2992
2993 // Save dependent expressions in the AST to be instantiated.
2994 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
2995 return;
2996}
2997
Chandler Carruthedc2c642011-07-02 00:01:44 +00002998static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002999 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003000 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003001 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3002 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003003 return;
3004 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003005
Richard Smith848e1f12013-02-01 08:12:08 +00003006 if (Attr.getNumArgs() == 0) {
3007 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
Craig Topperc3ec1492014-05-26 06:22:03 +00003008 true, nullptr, Attr.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00003009 return;
3010 }
3011
Aaron Ballman00e99962013-08-31 01:11:41 +00003012 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00003013 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3014 S.Diag(Attr.getEllipsisLoc(),
3015 diag::err_pack_expansion_without_parameter_packs);
3016 return;
3017 }
3018
3019 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3020 return;
3021
David Majnemer26a1e0e2015-04-07 02:37:09 +00003022 if (E->isValueDependent()) {
3023 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3024 if (!TND->getUnderlyingType()->isDependentType()) {
3025 S.Diag(Attr.getLoc(), diag::err_alignment_dependent_typedef_name)
3026 << E->getSourceRange();
3027 return;
3028 }
3029 }
3030 }
3031
Richard Smith44c247f2013-02-22 08:32:16 +00003032 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3033 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003034}
3035
3036void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003037 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003038 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3039 SourceLocation AttrLoc = AttrRange.getBegin();
3040
Richard Smith1dba27c2013-01-29 09:02:09 +00003041 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003042 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003043 // C++11 [dcl.align]p1:
3044 // An alignment-specifier may be applied to a variable or to a class
3045 // data member, but it shall not be applied to a bit-field, a function
3046 // parameter, the formal parameter of a catch clause, or a variable
3047 // declared with the register storage class specifier. An
3048 // alignment-specifier may also be applied to the declaration of a class
3049 // or enumeration type.
3050 // C11 6.7.5/2:
3051 // An alignment attribute shall not be specified in a declaration of
3052 // a typedef, or a bit-field, or a function, or a parameter, or an
3053 // object declared with the register storage-class specifier.
3054 int DiagKind = -1;
3055 if (isa<ParmVarDecl>(D)) {
3056 DiagKind = 0;
3057 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3058 if (VD->getStorageClass() == SC_Register)
3059 DiagKind = 1;
3060 if (VD->isExceptionVariable())
3061 DiagKind = 2;
3062 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3063 if (FD->isBitField())
3064 DiagKind = 3;
3065 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003066 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003067 << (TmpAttr.isC11() ? ExpectedVariableOrField
3068 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003069 return;
3070 }
3071 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003072 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003073 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003074 return;
3075 }
3076 }
3077
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003078 if (E->isTypeDependent() || E->isValueDependent()) {
3079 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003080 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3081 AA->setPackExpansion(IsPackExpansion);
3082 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003083 return;
3084 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003085
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003086 // FIXME: Cache the number on the Attr object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003087 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003088 ExprResult ICE
3089 = VerifyIntegerConstantExpression(E, &Alignment,
3090 diag::err_aligned_attribute_argument_not_int,
3091 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003092 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003093 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003094
David Majnemer0be6bd02015-07-26 09:02:21 +00003095 uint64_t AlignVal = Alignment.getZExtValue();
3096
Richard Smith848e1f12013-02-01 08:12:08 +00003097 // C++11 [dcl.align]p2:
3098 // -- if the constant expression evaluates to zero, the alignment
3099 // specifier shall have no effect
3100 // C11 6.7.5p6:
3101 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003102 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003103 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003104 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3105 << E->getSourceRange();
3106 return;
3107 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003108 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003109
David Majnemerabecae72014-02-12 20:36:10 +00003110 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003111 unsigned MaxValidAlignment =
3112 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3113 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003114 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003115 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3116 << E->getSourceRange();
3117 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003118 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003119
David Majnemer0be6bd02015-07-26 09:02:21 +00003120 if (Context.getTargetInfo().isTLSSupported()) {
3121 unsigned MaxTLSAlign =
3122 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3123 .getQuantity();
3124 auto *VD = dyn_cast<VarDecl>(D);
3125 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3126 VD->getTLSKind() != VarDecl::TLS_None) {
3127 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3128 << (unsigned)AlignVal << VD << MaxTLSAlign;
3129 return;
3130 }
3131 }
3132
Richard Smith44c247f2013-02-22 08:32:16 +00003133 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003134 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00003135 AA->setPackExpansion(IsPackExpansion);
3136 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003137}
3138
Michael Hanaf02bbe2013-02-01 01:19:17 +00003139void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003140 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003141 // FIXME: Cache the number on the Attr object if non-dependent?
3142 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003143 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3144 SpellingListIndex);
3145 AA->setPackExpansion(IsPackExpansion);
3146 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003147}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003148
Richard Smith848e1f12013-02-01 08:12:08 +00003149void Sema::CheckAlignasUnderalignment(Decl *D) {
3150 assert(D->hasAttrs() && "no attributes on decl");
3151
David Majnemer475b25e2015-01-21 10:54:38 +00003152 QualType UnderlyingTy, DiagTy;
3153 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
3154 UnderlyingTy = DiagTy = VD->getType();
3155 } else {
3156 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
3157 if (EnumDecl *ED = dyn_cast<EnumDecl>(D))
3158 UnderlyingTy = ED->getIntegerType();
3159 }
3160 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003161 return;
3162
3163 // C++11 [dcl.align]p5, C11 6.7.5/4:
3164 // The combined effect of all alignment attributes in a declaration shall
3165 // not specify an alignment that is less strict than the alignment that
3166 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003167 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003168 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003169 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003170 if (I->isAlignmentDependent())
3171 return;
3172 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003173 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003174 Align = std::max(Align, I->getAlignment(Context));
3175 }
3176
3177 if (AlignasAttr && Align) {
3178 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003179 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003180 if (NaturalAlign > RequestedAlign)
3181 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003182 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003183 }
3184}
3185
David Majnemer2c4e00a2014-01-29 22:07:36 +00003186bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003187 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003188 MSInheritanceAttr::Spelling SemanticSpelling) {
3189 assert(RD->hasDefinition() && "RD has no definition!");
3190
David Majnemer98c9ee22014-02-07 00:43:07 +00003191 // We may not have seen base specifiers or any virtual methods yet. We will
3192 // have to wait until the record is defined to catch any mismatches.
3193 if (!RD->getDefinition()->isCompleteDefinition())
3194 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003195
David Majnemer98c9ee22014-02-07 00:43:07 +00003196 // The unspecified model never matches what a definition could need.
3197 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3198 return false;
3199
David Majnemer4bb09802014-02-10 19:50:15 +00003200 if (BestCase) {
3201 if (RD->calculateInheritanceModel() == SemanticSpelling)
3202 return false;
3203 } else {
3204 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3205 return false;
3206 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003207
3208 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3209 << 0 /*definition*/;
3210 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3211 << RD->getNameAsString();
3212 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003213}
3214
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003215/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003216/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003217///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003218/// Despite what would be logical, the mode attribute is a decl attribute, not a
3219/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3220/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003221static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003222 // This attribute isn't documented, but glibc uses it. It changes
3223 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00003224 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00003225 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3226 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003227 return;
3228 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003229
Aaron Ballman00e99962013-08-31 01:11:41 +00003230 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3231 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003232
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00003233 normalizeName(Str);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003234
3235 unsigned DestWidth = 0;
3236 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003237 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003238 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003239 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003240 switch (Str[0]) {
3241 case 'Q': DestWidth = 8; break;
3242 case 'H': DestWidth = 16; break;
3243 case 'S': DestWidth = 32; break;
3244 case 'D': DestWidth = 64; break;
3245 case 'X': DestWidth = 96; break;
3246 case 'T': DestWidth = 128; break;
3247 }
3248 if (Str[1] == 'F') {
3249 IntegerMode = false;
3250 } else if (Str[1] == 'C') {
3251 IntegerMode = false;
3252 ComplexMode = true;
3253 } else if (Str[1] != 'I') {
3254 DestWidth = 0;
3255 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003256 break;
3257 case 4:
3258 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3259 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003260 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003261 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003262 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003263 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003264 break;
3265 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003266 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003267 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003268 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003269 case 11:
3270 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003271 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003272 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003273 }
3274
3275 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003276 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003277 OldTy = TD->getUnderlyingType();
3278 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3279 OldTy = VD->getType();
3280 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003281 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00003282 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003283 return;
3284 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003285
Alexey Bataev326057d2015-06-19 07:46:21 +00003286 // Base type can also be a vector type (see PR17453).
3287 // Distinguish between base type and base element type.
3288 QualType OldElemTy = OldTy;
3289 if (const VectorType *VT = OldTy->getAs<VectorType>())
3290 OldElemTy = VT->getElementType();
3291
3292 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003293 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3294 else if (IntegerMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003295 if (!OldElemTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003296 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3297 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003298 if (!OldElemTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003299 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3300 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00003301 if (!OldElemTy->isFloatingType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003302 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3303 }
3304
Mike Stump87c57ac2009-05-16 07:39:55 +00003305 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3306 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003307 // FIXME: Make sure floating-point mappings are accurate
3308 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003309 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00003310 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003311 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003312 }
3313
Alexey Bataev326057d2015-06-19 07:46:21 +00003314 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003315
3316 if (IntegerMode)
Alexey Bataev326057d2015-06-19 07:46:21 +00003317 NewElemTy = S.Context.getIntTypeForBitwidth(
3318 DestWidth, OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003319 else
Alexey Bataev326057d2015-06-19 07:46:21 +00003320 NewElemTy = S.Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003321
Alexey Bataev326057d2015-06-19 07:46:21 +00003322 if (NewElemTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00003323 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003324 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003325 }
3326
Eli Friedman4735374e2009-03-03 06:41:03 +00003327 if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003328 NewElemTy = S.Context.getComplexType(NewElemTy);
3329 }
3330
3331 QualType NewTy = NewElemTy;
3332 if (const VectorType *OldVT = OldTy->getAs<VectorType>()) {
3333 // Complex machine mode does not support base vector types.
3334 if (ComplexMode) {
3335 S.Diag(Attr.getLoc(), diag::err_complex_mode_vector_type);
3336 return;
3337 }
3338 unsigned NumElements = S.Context.getTypeSize(OldElemTy) *
3339 OldVT->getNumElements() /
3340 S.Context.getTypeSize(NewElemTy);
3341 NewTy =
3342 S.Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
3343 }
3344
3345 if (NewTy.isNull()) {
3346 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3347 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003348 }
3349
3350 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003351 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3352 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3353 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003354 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003355
3356 D->addAttr(::new (S.Context)
3357 ModeAttr(Attr.getRange(), S.Context, Name,
3358 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003359}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003360
Chandler Carruthedc2c642011-07-02 00:01:44 +00003361static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003362 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3363 if (!VD->hasGlobalStorage())
3364 S.Diag(Attr.getLoc(),
3365 diag::warn_attribute_requires_functions_or_static_globals)
3366 << Attr.getName();
3367 } else if (!isFunctionOrMethod(D)) {
3368 S.Diag(Attr.getLoc(),
3369 diag::warn_attribute_requires_functions_or_static_globals)
3370 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003371 return;
3372 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003373
Michael Han99315932013-01-24 16:46:58 +00003374 D->addAttr(::new (S.Context)
3375 NoDebugAttr(Attr.getRange(), S.Context,
3376 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003377}
3378
Paul Robinson30e41fb2014-12-15 18:57:28 +00003379AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00003380 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00003381 unsigned AttrSpellingListIndex) {
3382 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003383 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00003384 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3385 return nullptr;
3386 }
3387
3388 if (D->hasAttr<AlwaysInlineAttr>())
3389 return nullptr;
3390
3391 return ::new (Context) AlwaysInlineAttr(Range, Context,
3392 AttrSpellingListIndex);
3393}
3394
3395MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
3396 unsigned AttrSpellingListIndex) {
3397 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
3398 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
3399 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3400 return nullptr;
3401 }
3402
3403 if (D->hasAttr<MinSizeAttr>())
3404 return nullptr;
3405
3406 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
3407}
3408
3409OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
3410 unsigned AttrSpellingListIndex) {
3411 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
3412 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
3413 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3414 D->dropAttr<AlwaysInlineAttr>();
3415 }
3416 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
3417 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
3418 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3419 D->dropAttr<MinSizeAttr>();
3420 }
3421
3422 if (D->hasAttr<OptimizeNoneAttr>())
3423 return nullptr;
3424
3425 return ::new (Context) OptimizeNoneAttr(Range, Context,
3426 AttrSpellingListIndex);
3427}
3428
Paul Robinsonf0674352014-03-31 22:29:15 +00003429static void handleAlwaysInlineAttr(Sema &S, Decl *D,
3430 const AttributeList &Attr) {
Akira Hatanakac8667622015-11-06 23:56:15 +00003431 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, Attr))
3432 return;
3433
Paul Robinson080b1f32015-01-13 18:34:56 +00003434 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
3435 D, Attr.getRange(), Attr.getName(),
3436 Attr.getAttributeSpellingListIndex()))
3437 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00003438}
3439
Paul Robinson080b1f32015-01-13 18:34:56 +00003440static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3441 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
3442 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3443 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00003444}
3445
Paul Robinsonf0674352014-03-31 22:29:15 +00003446static void handleOptimizeNoneAttr(Sema &S, Decl *D,
3447 const AttributeList &Attr) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003448 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
3449 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3450 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00003451}
3452
Chandler Carruthedc2c642011-07-02 00:01:44 +00003453static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003454 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003455 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00003456 SourceRange RTRange = FD->getReturnTypeSourceRange();
3457 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00003458 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00003459 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
3460 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00003461 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003462 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003463
Aaron Ballman3aff6332013-12-02 19:30:36 +00003464 D->addAttr(::new (S.Context)
3465 CUDAGlobalAttr(Attr.getRange(), S.Context,
Eli Bendersky83860042014-09-02 22:00:06 +00003466 Attr.getAttributeSpellingListIndex()));
Artem Belevichc3fa25d2015-09-22 17:22:51 +00003467
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003468}
3469
Chandler Carruthedc2c642011-07-02 00:01:44 +00003470static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003471 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003472 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003473 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003474 return;
3475 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003476
Michael Han99315932013-01-24 16:46:58 +00003477 D->addAttr(::new (S.Context)
3478 GNUInlineAttr(Attr.getRange(), S.Context,
3479 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003480}
3481
Chandler Carruthedc2c642011-07-02 00:01:44 +00003482static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003483 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003484
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003485 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003486 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3487 CallingConv CC;
Richard Smitheec7cb12015-05-28 23:38:53 +00003488 if (S.CheckCallingConvAttr(Attr, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00003489 return;
3490
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003491 if (!isa<ObjCMethodDecl>(D)) {
3492 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3493 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003494 return;
3495 }
3496
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003497 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003498 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003499 D->addAttr(::new (S.Context)
3500 FastCallAttr(Attr.getRange(), S.Context,
3501 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003502 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003503 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003504 D->addAttr(::new (S.Context)
3505 StdCallAttr(Attr.getRange(), S.Context,
3506 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003507 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003508 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003509 D->addAttr(::new (S.Context)
3510 ThisCallAttr(Attr.getRange(), S.Context,
3511 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003512 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003513 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003514 D->addAttr(::new (S.Context)
3515 CDeclAttr(Attr.getRange(), S.Context,
3516 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003517 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003518 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003519 D->addAttr(::new (S.Context)
3520 PascalAttr(Attr.getRange(), S.Context,
3521 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003522 return;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003523 case AttributeList::AT_VectorCall:
3524 D->addAttr(::new (S.Context)
3525 VectorCallAttr(Attr.getRange(), S.Context,
3526 Attr.getAttributeSpellingListIndex()));
3527 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003528 case AttributeList::AT_MSABI:
3529 D->addAttr(::new (S.Context)
3530 MSABIAttr(Attr.getRange(), S.Context,
3531 Attr.getAttributeSpellingListIndex()));
3532 return;
3533 case AttributeList::AT_SysVABI:
3534 D->addAttr(::new (S.Context)
3535 SysVABIAttr(Attr.getRange(), S.Context,
3536 Attr.getAttributeSpellingListIndex()));
3537 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003538 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003539 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003540 switch (CC) {
3541 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003542 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003543 break;
3544 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003545 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003546 break;
3547 default:
3548 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003549 }
3550
Michael Han99315932013-01-24 16:46:58 +00003551 D->addAttr(::new (S.Context)
3552 PcsAttr(Attr.getRange(), S.Context, PCS,
3553 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003554 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003555 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003556 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003557 D->addAttr(::new (S.Context)
3558 IntelOclBiccAttr(Attr.getRange(), S.Context,
3559 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003560 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003561
Abramo Bagnara50099372010-04-30 13:10:51 +00003562 default:
3563 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003564 }
3565}
3566
Aaron Ballman02df2e02012-12-09 17:45:41 +00003567bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3568 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003569 if (attr.isInvalid())
3570 return true;
3571
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003572 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003573 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003574 attr.setInvalid();
3575 return true;
3576 }
3577
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003578 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003579 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003580 case AttributeList::AT_CDecl: CC = CC_C; break;
3581 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3582 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3583 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3584 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003585 case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003586 case AttributeList::AT_MSABI:
3587 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3588 CC_X86_64Win64;
3589 break;
3590 case AttributeList::AT_SysVABI:
3591 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3592 CC_C;
3593 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003594 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003595 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003596 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003597 attr.setInvalid();
3598 return true;
3599 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003600 if (StrRef == "aapcs") {
3601 CC = CC_AAPCS;
3602 break;
3603 } else if (StrRef == "aapcs-vfp") {
3604 CC = CC_AAPCS_VFP;
3605 break;
3606 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003607
3608 attr.setInvalid();
3609 Diag(attr.getLoc(), diag::err_invalid_pcs);
3610 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003611 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003612 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003613 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003614 }
3615
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003616 const TargetInfo &TI = Context.getTargetInfo();
3617 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003618 if (A != TargetInfo::CCCR_OK) {
3619 if (A == TargetInfo::CCCR_Warning)
3620 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003621
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003622 // This convention is not valid for the target. Use the default function or
3623 // method calling convention.
Aaron Ballman02df2e02012-12-09 17:45:41 +00003624 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3625 if (FD)
3626 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3627 TargetInfo::CCMT_NonMember;
3628 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003629 }
3630
John McCall3882ace2011-01-05 12:14:39 +00003631 return false;
3632}
3633
John McCall3882ace2011-01-05 12:14:39 +00003634/// Checks a regparm attribute, returning true if it is ill-formed and
3635/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003636bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3637 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003638 return true;
3639
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003640 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003641 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003642 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003643 }
Eli Friedman7044b762009-03-27 21:06:47 +00003644
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003645 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003646 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003647 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003648 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003649 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003650 }
3651
Douglas Gregore8bbc122011-09-02 00:18:52 +00003652 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003653 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003654 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003655 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003656 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003657 }
3658
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003659 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003660 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003661 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003662 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003663 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003664 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003665 }
3666
John McCall3882ace2011-01-05 12:14:39 +00003667 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003668}
3669
Artem Belevich7093e402015-04-21 22:55:54 +00003670// Checks whether an argument of launch_bounds attribute is acceptable
3671// May output an error.
3672static bool checkLaunchBoundsArgument(Sema &S, Expr *E,
3673 const CUDALaunchBoundsAttr &Attr,
3674 const unsigned Idx) {
3675
3676 if (S.DiagnoseUnexpandedParameterPack(E))
3677 return false;
3678
3679 // Accept template arguments for now as they depend on something else.
3680 // We'll get to check them when they eventually get instantiated.
3681 if (E->isValueDependent())
3682 return true;
3683
3684 llvm::APSInt I(64);
3685 if (!E->isIntegerConstantExpr(I, S.Context)) {
3686 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
3687 << &Attr << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
3688 return false;
3689 }
3690 // Make sure we can fit it in 32 bits.
3691 if (!I.isIntN(32)) {
3692 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
3693 << 32 << /* Unsigned */ 1;
3694 return false;
3695 }
3696 if (I < 0)
3697 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
3698 << &Attr << Idx << E->getSourceRange();
3699
3700 return true;
3701}
3702
3703void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
3704 Expr *MinBlocks, unsigned SpellingListIndex) {
3705 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
3706 SpellingListIndex);
3707
3708 if (!checkLaunchBoundsArgument(*this, MaxThreads, TmpAttr, 0))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003709 return;
3710
Artem Belevich7093e402015-04-21 22:55:54 +00003711 if (MinBlocks && !checkLaunchBoundsArgument(*this, MinBlocks, TmpAttr, 1))
3712 return;
3713
3714 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
3715 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
3716}
3717
3718static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3719 const AttributeList &Attr) {
3720 if (!checkAttributeAtLeastNumArgs(S, Attr, 1) ||
3721 !checkAttributeAtMostNumArgs(S, Attr, 2))
3722 return;
3723
3724 S.AddLaunchBoundsAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
3725 Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr,
3726 Attr.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00003727}
3728
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003729static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3730 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003731 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003732 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003733 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003734 return;
3735 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003736
3737 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003738 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003739
Aaron Ballman00e99962013-08-31 01:11:41 +00003740 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003741
3742 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3743 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3744 << Attr.getName() << ExpectedFunctionOrMethod;
3745 return;
3746 }
3747
3748 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003749 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3750 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003751 return;
3752
3753 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003754 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3755 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003756 return;
3757
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003758 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003759 if (IsPointer) {
3760 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003761 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003762 if (!BufferTy->isPointerType()) {
3763 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003764 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003765 }
3766 }
3767
Michael Han99315932013-01-24 16:46:58 +00003768 D->addAttr(::new (S.Context)
3769 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3770 ArgumentIdx, TypeTagIdx, IsPointer,
3771 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003772}
3773
3774static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3775 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003776 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003777 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003778 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003779 return;
3780 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003781
3782 if (!checkAttributeNumArgs(S, Attr, 1))
3783 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003784
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003785 if (!isa<VarDecl>(D)) {
3786 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3787 << Attr.getName() << ExpectedVariable;
3788 return;
3789 }
3790
Aaron Ballman00e99962013-08-31 01:11:41 +00003791 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00003792 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00003793 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3794 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003795
Michael Han99315932013-01-24 16:46:58 +00003796 D->addAttr(::new (S.Context)
3797 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003798 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003799 Attr.getLayoutCompatible(),
3800 Attr.getMustBeNull(),
3801 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003802}
3803
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003804//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003805// Checker-specific attribute handlers.
3806//===----------------------------------------------------------------------===//
3807
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003808static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) {
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003809 return type->isDependentType() ||
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003810 type->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003811}
3812
John McCalled433932011-01-25 03:31:58 +00003813static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003814 return type->isDependentType() ||
3815 type->isObjCObjectPointerType() ||
3816 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003817}
3818static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003819 return type->isDependentType() ||
3820 type->isPointerType() ||
3821 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003822}
3823
Chandler Carruthedc2c642011-07-02 00:01:44 +00003824static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003825 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003826 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003827
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003828 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003829 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3830 cf = false;
3831 } else {
3832 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3833 cf = true;
3834 }
3835
3836 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003837 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003838 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003839 return;
3840 }
3841
3842 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003843 param->addAttr(::new (S.Context)
3844 CFConsumedAttr(Attr.getRange(), S.Context,
3845 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003846 else
Michael Han99315932013-01-24 16:46:58 +00003847 param->addAttr(::new (S.Context)
3848 NSConsumedAttr(Attr.getRange(), S.Context,
3849 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003850}
3851
Chandler Carruthedc2c642011-07-02 00:01:44 +00003852static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3853 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003854
John McCalled433932011-01-25 03:31:58 +00003855 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003856
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003857 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003858 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003859 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003860 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003861 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003862 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3863 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003864 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003865 returnType = FD->getReturnType();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00003866 else if (auto *Param = dyn_cast<ParmVarDecl>(D)) {
3867 returnType = Param->getType()->getPointeeType();
3868 if (returnType.isNull()) {
3869 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
3870 << Attr.getName() << /*pointer-to-CF*/2
3871 << Attr.getRange();
3872 return;
3873 }
3874 } else {
3875 AttributeDeclKind ExpectedDeclKind;
3876 switch (Attr.getKind()) {
3877 default: llvm_unreachable("invalid ownership attribute");
3878 case AttributeList::AT_NSReturnsRetained:
3879 case AttributeList::AT_NSReturnsAutoreleased:
3880 case AttributeList::AT_NSReturnsNotRetained:
3881 ExpectedDeclKind = ExpectedFunctionOrMethod;
3882 break;
3883
3884 case AttributeList::AT_CFReturnsRetained:
3885 case AttributeList::AT_CFReturnsNotRetained:
3886 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
3887 break;
3888 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003889 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00003890 << Attr.getRange() << Attr.getName() << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003891 return;
3892 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003893
John McCalled433932011-01-25 03:31:58 +00003894 bool typeOK;
3895 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003896 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003897 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003898 case AttributeList::AT_NSReturnsRetained:
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003899 typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType);
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003900 cf = false;
3901 break;
3902
3903 case AttributeList::AT_NSReturnsAutoreleased:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003904 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003905 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3906 cf = false;
3907 break;
3908
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003909 case AttributeList::AT_CFReturnsRetained:
3910 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003911 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3912 cf = true;
3913 break;
3914 }
3915
3916 if (!typeOK) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00003917 if (isa<ParmVarDecl>(D)) {
3918 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
3919 << Attr.getName() << /*pointer-to-CF*/2
3920 << Attr.getRange();
3921 } else {
3922 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
3923 enum : unsigned {
3924 Function,
3925 Method,
3926 Property
3927 } SubjectKind = Function;
3928 if (isa<ObjCMethodDecl>(D))
3929 SubjectKind = Method;
3930 else if (isa<ObjCPropertyDecl>(D))
3931 SubjectKind = Property;
3932 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3933 << Attr.getName() << SubjectKind << cf
3934 << Attr.getRange();
3935 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003936 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003937 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003938
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003939 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003940 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003941 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003942 case AttributeList::AT_NSReturnsAutoreleased:
Nico Weber462fd1e2015-01-07 23:50:05 +00003943 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(
3944 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003945 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003946 case AttributeList::AT_CFReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003947 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(
3948 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003949 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003950 case AttributeList::AT_NSReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003951 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(
3952 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003953 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003954 case AttributeList::AT_CFReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003955 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(
3956 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003957 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003958 case AttributeList::AT_NSReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003959 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(
3960 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003961 return;
3962 };
3963}
3964
John McCallcf166702011-07-22 08:53:00 +00003965static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3966 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003967 const int EP_ObjCMethod = 1;
3968 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003969
John McCallcf166702011-07-22 08:53:00 +00003970 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003971 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003972 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003973 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003974 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003975 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003976
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003977 if (!resultType->isReferenceType() &&
3978 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003979 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003980 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003981 << attr.getName()
3982 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003983 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003984
3985 // Drop the attribute.
3986 return;
3987 }
3988
Nico Weber462fd1e2015-01-07 23:50:05 +00003989 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
3990 attr.getRange(), S.Context, attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003991}
3992
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003993static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3994 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003995 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003996
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003997 DeclContext *DC = method->getDeclContext();
3998 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3999 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4000 << attr.getName() << 0;
4001 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4002 return;
4003 }
4004 if (method->getMethodFamily() == OMF_dealloc) {
4005 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4006 << attr.getName() << 1;
4007 return;
4008 }
4009
Michael Han99315932013-01-24 16:46:58 +00004010 method->addAttr(::new (S.Context)
4011 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4012 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004013}
4014
Aaron Ballmanfb763042013-12-02 18:05:46 +00004015static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
4016 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00004017 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00004018 return;
John McCall32f5fe12011-09-30 05:12:12 +00004019
Aaron Ballmanfb763042013-12-02 18:05:46 +00004020 D->addAttr(::new (S.Context)
4021 CFAuditedTransferAttr(Attr.getRange(), S.Context,
4022 Attr.getAttributeSpellingListIndex()));
4023}
4024
4025static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
4026 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00004027 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00004028 return;
4029
4030 D->addAttr(::new (S.Context)
4031 CFUnknownTransferAttr(Attr.getRange(), S.Context,
4032 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004033}
4034
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004035static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
4036 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004037 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004038
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004039 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004040 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004041 return;
4042 }
John McCall28592582015-02-01 22:34:06 +00004043
4044 // Typedefs only allow objc_bridge(id) and have some additional checking.
4045 if (auto TD = dyn_cast<TypedefNameDecl>(D)) {
4046 if (!Parm->Ident->isStr("id")) {
4047 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_id)
4048 << Attr.getName();
4049 return;
4050 }
4051
4052 // Only allow 'cv void *'.
4053 QualType T = TD->getUnderlyingType();
4054 if (!T->isVoidPointerType()) {
4055 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
4056 return;
4057 }
4058 }
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004059
4060 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004061 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004062 Attr.getAttributeSpellingListIndex()));
4063}
4064
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004065static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
4066 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004067 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
4068
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004069 if (!Parm) {
4070 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4071 return;
4072 }
4073
4074 D->addAttr(::new (S.Context)
4075 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
4076 Attr.getAttributeSpellingListIndex()));
4077}
4078
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004079static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
4080 const AttributeList &Attr) {
4081 IdentifierInfo *RelatedClass =
Craig Topperc3ec1492014-05-26 06:22:03 +00004082 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004083 if (!RelatedClass) {
4084 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4085 return;
4086 }
4087 IdentifierInfo *ClassMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00004088 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004089 IdentifierInfo *InstanceMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00004090 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004091 D->addAttr(::new (S.Context)
4092 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
4093 ClassMethod, InstanceMethod,
4094 Attr.getAttributeSpellingListIndex()));
4095}
4096
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004097static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
4098 const AttributeList &Attr) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004099 ObjCInterfaceDecl *IFace;
Nico Weber462fd1e2015-01-07 23:50:05 +00004100 if (ObjCCategoryDecl *CatDecl =
4101 dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004102 IFace = CatDecl->getClassInterface();
4103 else
4104 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00004105
4106 if (!IFace)
4107 return;
4108
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00004109 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00004110 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004111 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
4112 Attr.getAttributeSpellingListIndex()));
4113}
4114
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004115static void handleObjCRuntimeName(Sema &S, Decl *D,
4116 const AttributeList &Attr) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004117 StringRef MetaDataName;
4118 if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName))
4119 return;
4120 D->addAttr(::new (S.Context)
4121 ObjCRuntimeNameAttr(Attr.getRange(), S.Context,
4122 MetaDataName,
4123 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004124}
4125
Alex Denisovfde64952015-06-26 05:28:36 +00004126// when a user wants to use objc_boxable with a union or struct
4127// but she doesn't have access to the declaration (legacy/third-party code)
4128// then she can 'enable' this feature via trick with a typedef
4129// e.g.:
4130// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
4131static void handleObjCBoxable(Sema &S, Decl *D, const AttributeList &Attr) {
4132 bool notify = false;
4133
4134 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4135 if (RD && RD->getDefinition()) {
4136 RD = RD->getDefinition();
4137 notify = true;
4138 }
4139
4140 if (RD) {
4141 ObjCBoxableAttr *BoxableAttr = ::new (S.Context)
4142 ObjCBoxableAttr(Attr.getRange(), S.Context,
4143 Attr.getAttributeSpellingListIndex());
4144 RD->addAttr(BoxableAttr);
4145 if (notify) {
4146 // we need to notify ASTReader/ASTWriter about
4147 // modification of existing declaration
4148 if (ASTMutationListener *L = S.getASTMutationListener())
4149 L->AddedAttributeToRecord(BoxableAttr, RD);
4150 }
4151 }
4152}
4153
Chandler Carruthedc2c642011-07-02 00:01:44 +00004154static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4155 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004156 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004157
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004158 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004159 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004160}
4161
Chandler Carruthedc2c642011-07-02 00:01:44 +00004162static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4163 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004164 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004165 QualType type = vd->getType();
4166
4167 if (!type->isDependentType() &&
4168 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004169 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004170 << type;
4171 return;
4172 }
4173
4174 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4175
4176 // If we have no lifetime yet, check the lifetime we're presumably
4177 // going to infer.
4178 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4179 lifetime = type->getObjCARCImplicitLifetime();
4180
4181 switch (lifetime) {
4182 case Qualifiers::OCL_None:
4183 assert(type->isDependentType() &&
4184 "didn't infer lifetime for non-dependent type?");
4185 break;
4186
4187 case Qualifiers::OCL_Weak: // meaningful
4188 case Qualifiers::OCL_Strong: // meaningful
4189 break;
4190
4191 case Qualifiers::OCL_ExplicitNone:
4192 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004193 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004194 << (lifetime == Qualifiers::OCL_Autoreleasing);
4195 break;
4196 }
4197
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004198 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004199 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4200 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004201}
4202
Francois Picheta83957a2010-12-19 06:50:37 +00004203//===----------------------------------------------------------------------===//
4204// Microsoft specific attribute handlers.
4205//===----------------------------------------------------------------------===//
4206
Chandler Carruthedc2c642011-07-02 00:01:44 +00004207static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00004208 if (!S.LangOpts.CPlusPlus) {
4209 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4210 << Attr.getName() << AttributeLangSupport::C;
4211 return;
4212 }
4213
Aaron Ballman60e705e2013-11-24 20:58:02 +00004214 if (!isa<CXXRecordDecl>(D)) {
4215 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4216 << Attr.getName() << ExpectedClass;
4217 return;
4218 }
4219
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004220 StringRef StrRef;
4221 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00004222 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00004223 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004224
David Majnemer89085342013-08-09 08:56:20 +00004225 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4226 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00004227 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4228 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00004229
Reid Kleckner140c4a72013-05-17 14:04:52 +00004230 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00004231 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004232 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004233 return;
4234 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004235
David Majnemer89085342013-08-09 08:56:20 +00004236 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004237 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00004238 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004239 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00004240 return;
4241 }
David Majnemer89085342013-08-09 08:56:20 +00004242 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004243 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004244 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004245 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004246 }
Francois Picheta83957a2010-12-19 06:50:37 +00004247
David Majnemer89085342013-08-09 08:56:20 +00004248 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4249 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004250}
4251
David Majnemer2c4e00a2014-01-29 22:07:36 +00004252static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4253 if (!S.LangOpts.CPlusPlus) {
4254 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4255 << Attr.getName() << AttributeLangSupport::C;
4256 return;
4257 }
4258 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00004259 D, Attr.getRange(), /*BestCase=*/true,
4260 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00004261 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
4262 if (IA)
4263 D->addAttr(IA);
4264}
4265
Reid Kleckner7d6d2702014-05-01 03:16:47 +00004266static void handleDeclspecThreadAttr(Sema &S, Decl *D,
4267 const AttributeList &Attr) {
4268 VarDecl *VD = cast<VarDecl>(D);
4269 if (!S.Context.getTargetInfo().isTLSSupported()) {
4270 S.Diag(Attr.getLoc(), diag::err_thread_unsupported);
4271 return;
4272 }
4273 if (VD->getTSCSpec() != TSCS_unspecified) {
4274 S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable);
4275 return;
4276 }
4277 if (VD->hasLocalStorage()) {
4278 S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
4279 return;
4280 }
4281 VD->addAttr(::new (S.Context) ThreadAttr(
4282 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
4283}
4284
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004285static void handleARMInterruptAttr(Sema &S, Decl *D,
4286 const AttributeList &Attr) {
4287 // Check the attribute arguments.
4288 if (Attr.getNumArgs() > 1) {
4289 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
4290 << Attr.getName() << 1;
4291 return;
4292 }
4293
4294 StringRef Str;
4295 SourceLocation ArgLoc;
4296
4297 if (Attr.getNumArgs() == 0)
4298 Str = "";
4299 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
4300 return;
4301
4302 ARMInterruptAttr::InterruptType Kind;
4303 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
4304 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
4305 << Attr.getName() << Str << ArgLoc;
4306 return;
4307 }
4308
4309 unsigned Index = Attr.getAttributeSpellingListIndex();
4310 D->addAttr(::new (S.Context)
4311 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
4312}
4313
4314static void handleMSP430InterruptAttr(Sema &S, Decl *D,
4315 const AttributeList &Attr) {
4316 if (!checkAttributeNumArgs(S, Attr, 1))
4317 return;
4318
4319 if (!Attr.isArgExpr(0)) {
4320 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
4321 << AANT_ArgumentIntegerConstant;
4322 return;
4323 }
4324
4325 // FIXME: Check for decl - it should be void ()(void).
4326
4327 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4328 llvm::APSInt NumParams(32);
4329 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
4330 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4331 << Attr.getName() << AANT_ArgumentIntegerConstant
4332 << NumParamsExpr->getSourceRange();
4333 return;
4334 }
4335
4336 unsigned Num = NumParams.getLimitedValue(255);
4337 if ((Num & 1) || Num > 30) {
4338 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
4339 << Attr.getName() << (int)NumParams.getSExtValue()
4340 << NumParamsExpr->getSourceRange();
4341 return;
4342 }
4343
Aaron Ballman36a53502014-01-16 13:03:14 +00004344 D->addAttr(::new (S.Context)
4345 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
4346 Attr.getAttributeSpellingListIndex()));
4347 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004348}
4349
4350static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4351 // Dispatch the interrupt attribute based on the current target.
4352 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
4353 handleMSP430InterruptAttr(S, D, Attr);
4354 else
4355 handleARMInterruptAttr(S, D, Attr);
4356}
4357
Matt Arsenault43fae6c2014-12-04 20:38:18 +00004358static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D,
4359 const AttributeList &Attr) {
4360 uint32_t NumRegs;
4361 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4362 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4363 return;
4364
4365 D->addAttr(::new (S.Context)
4366 AMDGPUNumVGPRAttr(Attr.getLoc(), S.Context,
4367 NumRegs,
4368 Attr.getAttributeSpellingListIndex()));
4369}
4370
4371static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D,
4372 const AttributeList &Attr) {
4373 uint32_t NumRegs;
4374 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4375 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4376 return;
4377
4378 D->addAttr(::new (S.Context)
4379 AMDGPUNumSGPRAttr(Attr.getLoc(), S.Context,
4380 NumRegs,
4381 Attr.getAttributeSpellingListIndex()));
4382}
4383
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004384static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
4385 const AttributeList& Attr) {
4386 // If we try to apply it to a function pointer, don't warn, but don't
4387 // do anything, either. It doesn't matter anyway, because there's nothing
4388 // special about calling a force_align_arg_pointer function.
4389 ValueDecl *VD = dyn_cast<ValueDecl>(D);
4390 if (VD && VD->getType()->isFunctionPointerType())
4391 return;
4392 // Also don't warn on function pointer typedefs.
4393 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
4394 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
4395 TD->getUnderlyingType()->isFunctionType()))
4396 return;
4397 // Attribute can only be applied to function types.
4398 if (!isa<FunctionDecl>(D)) {
4399 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4400 << Attr.getName() << /* function */0;
4401 return;
4402 }
4403
Aaron Ballman36a53502014-01-16 13:03:14 +00004404 D->addAttr(::new (S.Context)
4405 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
4406 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004407}
4408
4409DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
4410 unsigned AttrSpellingListIndex) {
4411 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004412 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00004413 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004414 }
4415
4416 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004417 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004418
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004419 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004420}
4421
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004422DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
4423 unsigned AttrSpellingListIndex) {
4424 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004425 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004426 D->dropAttr<DLLImportAttr>();
4427 }
4428
4429 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004430 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004431
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004432 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004433}
4434
Hans Wennborge82f19c2014-06-24 23:57:05 +00004435static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00004436 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
4437 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4438 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
4439 << A.getName();
4440 return;
4441 }
4442
Hans Wennborg606bd6d2014-11-03 14:24:45 +00004443 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4444 if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport &&
4445 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4446 // MinGW doesn't allow dllimport on inline functions.
4447 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
4448 << A.getName();
4449 return;
4450 }
4451 }
4452
Hans Wennborg5869ec42015-09-15 21:05:30 +00004453 if (auto *MD = dyn_cast<CXXMethodDecl>(D)) {
4454 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
4455 MD->getParent()->isLambda()) {
4456 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A.getName();
4457 return;
4458 }
4459 }
4460
Hans Wennborge82f19c2014-06-24 23:57:05 +00004461 unsigned Index = A.getAttributeSpellingListIndex();
4462 Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport
4463 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
4464 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004465 if (NewAttr)
4466 D->addAttr(NewAttr);
4467}
4468
David Majnemer2c4e00a2014-01-29 22:07:36 +00004469MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00004470Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00004471 unsigned AttrSpellingListIndex,
4472 MSInheritanceAttr::Spelling SemanticSpelling) {
4473 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
4474 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00004475 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004476 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
4477 << 1 /*previous declaration*/;
4478 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
4479 D->dropAttr<MSInheritanceAttr>();
4480 }
4481
4482 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
4483 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00004484 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
4485 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004486 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004487 }
4488 } else {
4489 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
4490 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4491 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004492 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004493 }
4494 if (RD->getDescribedClassTemplate()) {
4495 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4496 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004497 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004498 }
4499 }
4500
4501 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00004502 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00004503}
4504
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004505static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4506 // The capability attributes take a single string parameter for the name of
4507 // the capability they represent. The lockable attribute does not take any
4508 // parameters. However, semantically, both attributes represent the same
4509 // concept, and so they use the same semantic attribute. Eventually, the
4510 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00004511 //
Alp Toker958027b2014-07-14 19:42:55 +00004512 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00004513 // literal will be considered a "mutex."
4514 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004515 SourceLocation LiteralLoc;
4516 if (Attr.getKind() == AttributeList::AT_Capability &&
4517 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
4518 return;
4519
Aaron Ballman6c810072014-03-05 21:47:13 +00004520 // Currently, there are only two names allowed for a capability: role and
4521 // mutex (case insensitive). Diagnose other capability names.
4522 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
4523 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
4524
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004525 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
4526 Attr.getAttributeSpellingListIndex()));
4527}
4528
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004529static void handleAssertCapabilityAttr(Sema &S, Decl *D,
4530 const AttributeList &Attr) {
4531 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
4532 Attr.getArgAsExpr(0),
4533 Attr.getAttributeSpellingListIndex()));
4534}
4535
4536static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
4537 const AttributeList &Attr) {
4538 SmallVector<Expr*, 1> Args;
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004539 if (!checkLockFunAttrCommon(S, D, Attr, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004540 return;
4541
4542 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
4543 S.Context,
4544 Args.data(), Args.size(),
4545 Attr.getAttributeSpellingListIndex()));
4546}
4547
4548static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
4549 const AttributeList &Attr) {
4550 SmallVector<Expr*, 2> Args;
4551 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
4552 return;
4553
4554 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
4555 S.Context,
4556 Attr.getArgAsExpr(0),
4557 Args.data(),
4558 Args.size(),
4559 Attr.getAttributeSpellingListIndex()));
4560}
4561
4562static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
4563 const AttributeList &Attr) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004564 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004565 SmallVector<Expr *, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004566 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004567
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004568 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
4569 Attr.getRange(), S.Context, Args.data(), Args.size(),
4570 Attr.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004571}
4572
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004573static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
4574 const AttributeList &Attr) {
4575 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4576 return;
4577
4578 // check that all arguments are lockable objects
4579 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004580 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004581 if (Args.empty())
4582 return;
4583
4584 RequiresCapabilityAttr *RCA = ::new (S.Context)
4585 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
4586 Args.size(), Attr.getAttributeSpellingListIndex());
4587
4588 D->addAttr(RCA);
4589}
4590
Aaron Ballman43f40102014-11-14 22:34:56 +00004591static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4592 if (auto *NSD = dyn_cast<NamespaceDecl>(D)) {
4593 if (NSD->isAnonymousNamespace()) {
4594 S.Diag(Attr.getLoc(), diag::warn_deprecated_anonymous_namespace);
4595 // Do not want to attach the attribute to the namespace because that will
4596 // cause confusing diagnostic reports for uses of declarations within the
4597 // namespace.
4598 return;
4599 }
4600 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00004601
4602 if (!S.getLangOpts().CPlusPlus14)
4603 if (Attr.isCXX11Attribute() &&
4604 !(Attr.hasScope() && Attr.getScopeName()->isStr("gnu")))
Saleem Abdulrasoolb47d6062015-02-18 04:33:26 +00004605 S.Diag(Attr.getLoc(), diag::ext_deprecated_attr_is_a_cxx14_extension);
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00004606
Aaron Ballman43f40102014-11-14 22:34:56 +00004607 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
4608}
4609
Peter Collingbourne915df992015-05-15 18:33:32 +00004610static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4611 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4612 return;
4613
4614 std::vector<std::string> Sanitizers;
4615
4616 for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) {
4617 StringRef SanitizerName;
4618 SourceLocation LiteralLoc;
4619
4620 if (!S.checkStringLiteralArgumentAttr(Attr, I, SanitizerName, &LiteralLoc))
4621 return;
4622
4623 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0)
4624 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
4625
4626 Sanitizers.push_back(SanitizerName);
4627 }
4628
4629 D->addAttr(::new (S.Context) NoSanitizeAttr(
4630 Attr.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
4631 Attr.getAttributeSpellingListIndex()));
4632}
4633
4634static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
4635 const AttributeList &Attr) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00004636 StringRef AttrName = Attr.getName()->getName();
4637 normalizeName(AttrName);
Peter Collingbourne915df992015-05-15 18:33:32 +00004638 std::string SanitizerName =
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00004639 llvm::StringSwitch<std::string>(AttrName)
Peter Collingbourne915df992015-05-15 18:33:32 +00004640 .Case("no_address_safety_analysis", "address")
4641 .Case("no_sanitize_address", "address")
4642 .Case("no_sanitize_thread", "thread")
Peter Collingbourne94410942015-05-15 20:11:18 +00004643 .Case("no_sanitize_memory", "memory");
Peter Collingbourne915df992015-05-15 18:33:32 +00004644 D->addAttr(::new (S.Context)
4645 NoSanitizeAttr(Attr.getRange(), S.Context, &SanitizerName, 1,
4646 Attr.getAttributeSpellingListIndex()));
4647}
4648
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004649/// Handles semantic checking for features that are common to all attributes,
4650/// such as checking whether a parameter was properly specified, or the correct
4651/// number of arguments were passed, etc.
4652static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4653 const AttributeList &Attr) {
4654 // Several attributes carry different semantics than the parsing requires, so
4655 // those are opted out of the common handling.
4656 //
4657 // We also bail on unknown and ignored attributes because those are handled
4658 // as part of the target-specific handling logic.
4659 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004660 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004661 return false;
4662
Aaron Ballman3aff6332013-12-02 19:30:36 +00004663 // Check whether the attribute requires specific language extensions to be
4664 // enabled.
4665 if (!Attr.diagnoseLangOpts(S))
4666 return true;
4667
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00004668 if (Attr.getMinArgs() == Attr.getMaxArgs()) {
4669 // If there are no optional arguments, then checking for the argument count
4670 // is trivial.
4671 if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4672 return true;
4673 } else {
4674 // There are optional arguments, so checking is slightly more involved.
4675 if (Attr.getMinArgs() &&
4676 !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs()))
4677 return true;
4678 else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() &&
4679 !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs()))
4680 return true;
4681 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004682
4683 // Check whether the attribute appertains to the given subject.
4684 if (!Attr.diagnoseAppertainsTo(S, D))
4685 return true;
4686
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004687 return false;
4688}
4689
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004690//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004691// Top Level Sema Entry Points
4692//===----------------------------------------------------------------------===//
4693
Richard Smithf8a75c32013-08-29 00:47:48 +00004694/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4695/// the attribute applies to decls. If the attribute is a type attribute, just
4696/// silently ignore it if a GNU attribute.
4697static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4698 const AttributeList &Attr,
4699 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004700 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00004701 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004702
Richard Smithf8a75c32013-08-29 00:47:48 +00004703 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4704 // instead.
4705 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4706 return;
4707
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004708 // Unknown attributes are automatically warned on. Target-specific attributes
4709 // which do not apply to the current target architecture are treated as
4710 // though they were unknown attributes.
4711 if (Attr.getKind() == AttributeList::UnknownAttribute ||
Bob Wilson7c730832015-07-20 22:57:31 +00004712 !Attr.existsInTarget(S.Context.getTargetInfo())) {
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004713 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute()
4714 ? diag::warn_unhandled_ms_attribute_ignored
4715 : diag::warn_unknown_attribute_ignored)
4716 << Attr.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004717 return;
4718 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004719
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004720 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4721 return;
4722
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004723 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004724 default:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004725 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004726 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004727 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004728 case AttributeList::AT_Interrupt:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004729 handleInterruptAttr(S, D, Attr);
4730 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004731 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004732 handleX86ForceAlignArgPointerAttr(S, D, Attr);
4733 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004734 case AttributeList::AT_DLLExport:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004735 case AttributeList::AT_DLLImport:
Hans Wennborge82f19c2014-06-24 23:57:05 +00004736 handleDLLAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004737 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004738 case AttributeList::AT_Mips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004739 handleSimpleAttribute<Mips16Attr>(S, D, Attr);
4740 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004741 case AttributeList::AT_NoMips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004742 handleSimpleAttribute<NoMips16Attr>(S, D, Attr);
4743 break;
Matt Arsenault43fae6c2014-12-04 20:38:18 +00004744 case AttributeList::AT_AMDGPUNumVGPR:
4745 handleAMDGPUNumVGPRAttr(S, D, Attr);
4746 break;
4747 case AttributeList::AT_AMDGPUNumSGPR:
4748 handleAMDGPUNumSGPRAttr(S, D, Attr);
4749 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00004750 case AttributeList::AT_IBAction:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004751 handleSimpleAttribute<IBActionAttr>(S, D, Attr);
4752 break;
4753 case AttributeList::AT_IBOutlet:
4754 handleIBOutlet(S, D, Attr);
4755 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004756 case AttributeList::AT_IBOutletCollection:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004757 handleIBOutletCollection(S, D, Attr);
4758 break;
4759 case AttributeList::AT_Alias:
4760 handleAliasAttr(S, D, Attr);
4761 break;
4762 case AttributeList::AT_Aligned:
4763 handleAlignedAttr(S, D, Attr);
4764 break;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00004765 case AttributeList::AT_AlignValue:
4766 handleAlignValueAttr(S, D, Attr);
4767 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004768 case AttributeList::AT_AlwaysInline:
Paul Robinsonf0674352014-03-31 22:29:15 +00004769 handleAlwaysInlineAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004770 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004771 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004772 handleAnalyzerNoReturnAttr(S, D, Attr);
4773 break;
4774 case AttributeList::AT_TLSModel:
4775 handleTLSModelAttr(S, D, Attr);
4776 break;
4777 case AttributeList::AT_Annotate:
4778 handleAnnotateAttr(S, D, Attr);
4779 break;
4780 case AttributeList::AT_Availability:
4781 handleAvailabilityAttr(S, D, Attr);
4782 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004783 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004784 handleDependencyAttr(S, scope, D, Attr);
4785 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004786 case AttributeList::AT_Common:
4787 handleCommonAttr(S, D, Attr);
4788 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004789 case AttributeList::AT_CUDAConstant:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004790 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr);
4791 break;
4792 case AttributeList::AT_Constructor:
4793 handleConstructorAttr(S, D, Attr);
4794 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004795 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004796 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr);
4797 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004798 case AttributeList::AT_Deprecated:
Aaron Ballman43f40102014-11-14 22:34:56 +00004799 handleDeprecatedAttr(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004800 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004801 case AttributeList::AT_Destructor:
4802 handleDestructorAttr(S, D, Attr);
4803 break;
4804 case AttributeList::AT_EnableIf:
4805 handleEnableIfAttr(S, D, Attr);
4806 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004807 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004808 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004809 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004810 case AttributeList::AT_MinSize:
Paul Robinsonaae2fba2014-12-10 23:34:36 +00004811 handleMinSizeAttr(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004812 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00004813 case AttributeList::AT_OptimizeNone:
4814 handleOptimizeNoneAttr(S, D, Attr);
4815 break;
Alexis Hunt724f14e2014-11-28 00:53:20 +00004816 case AttributeList::AT_FlagEnum:
4817 handleSimpleAttribute<FlagEnumAttr>(S, D, Attr);
4818 break;
Peter Collingbourne41af7c22014-05-20 17:12:51 +00004819 case AttributeList::AT_Flatten:
4820 handleSimpleAttribute<FlattenAttr>(S, D, Attr);
4821 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004822 case AttributeList::AT_Format:
4823 handleFormatAttr(S, D, Attr);
4824 break;
4825 case AttributeList::AT_FormatArg:
4826 handleFormatArgAttr(S, D, Attr);
4827 break;
4828 case AttributeList::AT_CUDAGlobal:
4829 handleGlobalAttr(S, D, Attr);
4830 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00004831 case AttributeList::AT_CUDADevice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004832 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr);
4833 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004834 case AttributeList::AT_CUDAHost:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004835 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr);
4836 break;
4837 case AttributeList::AT_GNUInline:
4838 handleGNUInlineAttr(S, D, Attr);
4839 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004840 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004841 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004842 break;
David Majnemer631a90b2015-02-04 07:23:21 +00004843 case AttributeList::AT_Restrict:
4844 handleRestrictAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004845 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004846 case AttributeList::AT_MayAlias:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004847 handleSimpleAttribute<MayAliasAttr>(S, D, Attr);
4848 break;
4849 case AttributeList::AT_Mode:
4850 handleModeAttr(S, D, Attr);
4851 break;
David Majnemer1bf0f8e2015-07-20 22:51:52 +00004852 case AttributeList::AT_NoAlias:
4853 handleSimpleAttribute<NoAliasAttr>(S, D, Attr);
4854 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004855 case AttributeList::AT_NoCommon:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004856 handleSimpleAttribute<NoCommonAttr>(S, D, Attr);
4857 break;
Peter Collingbourneb4728c12014-05-19 22:14:34 +00004858 case AttributeList::AT_NoSplitStack:
4859 handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr);
4860 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00004861 case AttributeList::AT_NonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004862 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
4863 handleNonNullAttrParameter(S, PVD, Attr);
4864 else
4865 handleNonNullAttr(S, D, Attr);
4866 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00004867 case AttributeList::AT_ReturnsNonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004868 handleReturnsNonNullAttr(S, D, Attr);
4869 break;
Hal Finkelee90a222014-09-26 05:04:30 +00004870 case AttributeList::AT_AssumeAligned:
4871 handleAssumeAlignedAttr(S, D, Attr);
4872 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004873 case AttributeList::AT_Overloadable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004874 handleSimpleAttribute<OverloadableAttr>(S, D, Attr);
4875 break;
4876 case AttributeList::AT_Ownership:
4877 handleOwnershipAttr(S, D, Attr);
4878 break;
4879 case AttributeList::AT_Cold:
4880 handleColdAttr(S, D, Attr);
4881 break;
4882 case AttributeList::AT_Hot:
4883 handleHotAttr(S, D, Attr);
4884 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004885 case AttributeList::AT_Naked:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004886 handleSimpleAttribute<NakedAttr>(S, D, Attr);
4887 break;
4888 case AttributeList::AT_NoReturn:
4889 handleNoReturnAttr(S, D, Attr);
4890 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004891 case AttributeList::AT_NoThrow:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004892 handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
4893 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004894 case AttributeList::AT_CUDAShared:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004895 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr);
4896 break;
4897 case AttributeList::AT_VecReturn:
4898 handleVecReturnAttr(S, D, Attr);
4899 break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004900
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004901 case AttributeList::AT_ObjCOwnership:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004902 handleObjCOwnershipAttr(S, D, Attr);
4903 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004904 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004905 handleObjCPreciseLifetimeAttr(S, D, Attr);
4906 break;
John McCall31168b02011-06-15 23:02:42 +00004907
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004908 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004909 handleObjCReturnsInnerPointerAttr(S, D, Attr);
4910 break;
John McCallcf166702011-07-22 08:53:00 +00004911
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004912 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004913 handleObjCRequiresSuperAttr(S, D, Attr);
4914 break;
4915
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004916 case AttributeList::AT_ObjCBridge:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004917 handleObjCBridgeAttr(S, scope, D, Attr);
4918 break;
4919
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004920 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004921 handleObjCBridgeMutableAttr(S, scope, D, Attr);
4922 break;
4923
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004924 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004925 handleObjCBridgeRelatedAttr(S, scope, D, Attr);
4926 break;
John McCallf1e8b342011-09-29 07:17:38 +00004927
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004928 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004929 handleObjCDesignatedInitializer(S, D, Attr);
4930 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004931
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004932 case AttributeList::AT_ObjCRuntimeName:
4933 handleObjCRuntimeName(S, D, Attr);
4934 break;
Alex Denisovfde64952015-06-26 05:28:36 +00004935
4936 case AttributeList::AT_ObjCBoxable:
4937 handleObjCBoxable(S, D, Attr);
4938 break;
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004939
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004940 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004941 handleCFAuditedTransferAttr(S, D, Attr);
4942 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004943 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004944 handleCFUnknownTransferAttr(S, D, Attr);
4945 break;
John McCall32f5fe12011-09-30 05:12:12 +00004946
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004947 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004948 case AttributeList::AT_NSConsumed:
4949 handleNSConsumedAttr(S, D, Attr);
4950 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004951 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004952 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr);
4953 break;
John McCalled433932011-01-25 03:31:58 +00004954
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004955 case AttributeList::AT_NSReturnsAutoreleased:
4956 case AttributeList::AT_NSReturnsNotRetained:
4957 case AttributeList::AT_CFReturnsNotRetained:
4958 case AttributeList::AT_NSReturnsRetained:
4959 case AttributeList::AT_CFReturnsRetained:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004960 handleNSReturnsRetainedAttr(S, D, Attr);
4961 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004962 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004963 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr);
4964 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004965 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004966 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr);
4967 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004968 case AttributeList::AT_VecTypeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004969 handleVecTypeHint(S, D, Attr);
4970 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004971
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004972 case AttributeList::AT_InitPriority:
4973 handleInitPriorityAttr(S, D, Attr);
4974 break;
4975
4976 case AttributeList::AT_Packed:
4977 handlePackedAttr(S, D, Attr);
4978 break;
4979 case AttributeList::AT_Section:
4980 handleSectionAttr(S, D, Attr);
4981 break;
Eric Christopher11acf732015-06-12 01:35:52 +00004982 case AttributeList::AT_Target:
4983 handleTargetAttr(S, D, Attr);
4984 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004985 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004986 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004987 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004988 case AttributeList::AT_ArcWeakrefUnavailable:
4989 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr);
4990 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004991 case AttributeList::AT_ObjCRootClass:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004992 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr);
4993 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004994 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek438f8db2014-02-22 01:06:05 +00004995 handleObjCSuppresProtocolAttr(S, D, Attr);
Ted Kremenek28eace62013-11-23 01:01:34 +00004996 break;
4997 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004998 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr);
4999 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00005000 case AttributeList::AT_Unused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005001 handleSimpleAttribute<UnusedAttr>(S, D, Attr);
5002 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005003 case AttributeList::AT_ReturnsTwice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005004 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr);
5005 break;
Akira Hatanakac8667622015-11-06 23:56:15 +00005006 case AttributeList::AT_NotTailCalled:
5007 handleNotTailCalledAttr(S, D, Attr);
5008 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005009 case AttributeList::AT_Used:
5010 handleUsedAttr(S, D, Attr);
5011 break;
John McCalld041a9b2013-02-20 01:54:26 +00005012 case AttributeList::AT_Visibility:
5013 handleVisibilityAttr(S, D, Attr, false);
5014 break;
5015 case AttributeList::AT_TypeVisibility:
5016 handleVisibilityAttr(S, D, Attr, true);
5017 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00005018 case AttributeList::AT_WarnUnused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005019 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr);
5020 break;
5021 case AttributeList::AT_WarnUnusedResult:
5022 handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00005023 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00005024 case AttributeList::AT_Weak:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005025 handleSimpleAttribute<WeakAttr>(S, D, Attr);
5026 break;
5027 case AttributeList::AT_WeakRef:
5028 handleWeakRefAttr(S, D, Attr);
5029 break;
5030 case AttributeList::AT_WeakImport:
5031 handleWeakImportAttr(S, D, Attr);
5032 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005033 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005034 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005035 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005036 case AttributeList::AT_ObjCException:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005037 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr);
5038 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005039 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005040 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00005041 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005042 case AttributeList::AT_ObjCNSObject:
5043 handleObjCNSObject(S, D, Attr);
5044 break;
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00005045 case AttributeList::AT_ObjCIndependentClass:
5046 handleObjCIndependentClass(S, D, Attr);
5047 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005048 case AttributeList::AT_Blocks:
5049 handleBlocksAttr(S, D, Attr);
5050 break;
5051 case AttributeList::AT_Sentinel:
5052 handleSentinelAttr(S, D, Attr);
5053 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00005054 case AttributeList::AT_Const:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005055 handleSimpleAttribute<ConstAttr>(S, D, Attr);
5056 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005057 case AttributeList::AT_Pure:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005058 handleSimpleAttribute<PureAttr>(S, D, Attr);
5059 break;
5060 case AttributeList::AT_Cleanup:
5061 handleCleanupAttr(S, D, Attr);
5062 break;
5063 case AttributeList::AT_NoDebug:
5064 handleNoDebugAttr(S, D, Attr);
5065 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00005066 case AttributeList::AT_NoDuplicate:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005067 handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
5068 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005069 case AttributeList::AT_NoInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005070 handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
5071 break;
5072 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
5073 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr);
5074 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005075 case AttributeList::AT_StdCall:
5076 case AttributeList::AT_CDecl:
5077 case AttributeList::AT_FastCall:
5078 case AttributeList::AT_ThisCall:
5079 case AttributeList::AT_Pascal:
Reid Klecknerd7857f02014-10-24 17:42:17 +00005080 case AttributeList::AT_VectorCall:
Charles Davisb5a214e2013-08-30 04:39:01 +00005081 case AttributeList::AT_MSABI:
5082 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005083 case AttributeList::AT_Pcs:
Guy Benyeif0a014b2012-12-25 08:53:55 +00005084 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005085 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00005086 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005087 case AttributeList::AT_OpenCLKernel:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005088 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr);
5089 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00005090 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005091 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr);
5092 break;
John McCall8d32c052012-05-22 21:28:12 +00005093
5094 // Microsoft attributes:
David Majnemer8ab003a2015-02-02 19:30:52 +00005095 case AttributeList::AT_MSNoVTable:
5096 handleSimpleAttribute<MSNoVTableAttr>(S, D, Attr);
David Majnemer129f4172015-02-02 10:22:20 +00005097 break;
David Majnemer8ab003a2015-02-02 19:30:52 +00005098 case AttributeList::AT_MSStruct:
5099 handleSimpleAttribute<MSStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00005100 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005101 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005102 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00005103 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00005104 case AttributeList::AT_MSInheritance:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005105 handleMSInheritanceAttr(S, D, Attr);
5106 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00005107 case AttributeList::AT_SelectAny:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005108 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr);
5109 break;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005110 case AttributeList::AT_Thread:
5111 handleDeclspecThreadAttr(S, D, Attr);
5112 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005113
5114 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00005115 case AttributeList::AT_AssertExclusiveLock:
5116 handleAssertExclusiveLockAttr(S, D, Attr);
5117 break;
5118 case AttributeList::AT_AssertSharedLock:
5119 handleAssertSharedLockAttr(S, D, Attr);
5120 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005121 case AttributeList::AT_GuardedVar:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005122 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr);
5123 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005124 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00005125 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005126 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005127 case AttributeList::AT_ScopedLockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005128 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr);
5129 break;
Peter Collingbourne915df992015-05-15 18:33:32 +00005130 case AttributeList::AT_NoSanitize:
5131 handleNoSanitizeAttr(S, D, Attr);
5132 break;
5133 case AttributeList::AT_NoSanitizeSpecific:
5134 handleNoSanitizeSpecificAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00005135 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005136 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005137 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00005138 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005139 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005140 handleGuardedByAttr(S, D, Attr);
5141 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005142 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00005143 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005144 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005145 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005146 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005147 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005148 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005149 handleLockReturnedAttr(S, D, Attr);
5150 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005151 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005152 handleLocksExcludedAttr(S, D, Attr);
5153 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005154 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005155 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005156 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005157 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00005158 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005159 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005160 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00005161 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005162 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005163
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005164 // Capability analysis attributes.
5165 case AttributeList::AT_Capability:
5166 case AttributeList::AT_Lockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005167 handleCapabilityAttr(S, D, Attr);
5168 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005169 case AttributeList::AT_RequiresCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005170 handleRequiresCapabilityAttr(S, D, Attr);
5171 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005172
5173 case AttributeList::AT_AssertCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005174 handleAssertCapabilityAttr(S, D, Attr);
5175 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005176 case AttributeList::AT_AcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005177 handleAcquireCapabilityAttr(S, D, Attr);
5178 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005179 case AttributeList::AT_ReleaseCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005180 handleReleaseCapabilityAttr(S, D, Attr);
5181 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005182 case AttributeList::AT_TryAcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005183 handleTryAcquireCapabilityAttr(S, D, Attr);
5184 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005185
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00005186 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00005187 case AttributeList::AT_Consumable:
5188 handleConsumableAttr(S, D, Attr);
5189 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00005190 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005191 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr);
5192 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00005193 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005194 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr);
5195 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00005196 case AttributeList::AT_CallableWhen:
5197 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00005198 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00005199 case AttributeList::AT_ParamTypestate:
5200 handleParamTypestateAttr(S, D, Attr);
5201 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00005202 case AttributeList::AT_ReturnTypestate:
5203 handleReturnTypestateAttr(S, D, Attr);
5204 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00005205 case AttributeList::AT_SetTypestate:
5206 handleSetTypestateAttr(S, D, Attr);
5207 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00005208 case AttributeList::AT_TestTypestate:
5209 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00005210 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00005211
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00005212 // Type safety attributes.
5213 case AttributeList::AT_ArgumentWithTypeTag:
5214 handleArgumentWithTypeTagAttr(S, D, Attr);
5215 break;
5216 case AttributeList::AT_TypeTagForDatatype:
5217 handleTypeTagForDatatypeAttr(S, D, Attr);
5218 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005219 }
5220}
5221
5222/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5223/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00005224void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00005225 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00005226 bool IncludeCXX11Attributes) {
5227 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00005228 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00005229
Joey Gouly2cd9db12013-12-13 16:15:28 +00005230 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00005231 // GCC accepts
5232 // static int a9 __attribute__((weakref));
5233 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00005234 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00005235 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
5236 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00005237 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00005238 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005239 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00005240
Aaron Ballmanbe243a72014-12-04 22:45:31 +00005241 // FIXME: We should be able to handle this in TableGen as well. It would be
5242 // good to have a way to specify "these attributes must appear as a group",
5243 // for these. Additionally, it would be good to have a way to specify "these
5244 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00005245 if (!D->hasAttr<OpenCLKernelAttr>()) {
5246 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00005247 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00005248 // FIXME: This emits a different error message than
5249 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00005250 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005251 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00005252 } else if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00005253 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005254 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00005255 } else if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00005256 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005257 D->setInvalidDecl();
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00005258 } else if (Attr *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
5259 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5260 << A << ExpectedKernelFunction;
5261 D->setInvalidDecl();
5262 } else if (Attr *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
5263 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5264 << A << ExpectedKernelFunction;
5265 D->setInvalidDecl();
Joey Gouly2cd9db12013-12-13 16:15:28 +00005266 }
5267 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005268}
5269
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005270// Annotation attributes are the only attributes allowed after an access
5271// specifier.
5272bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5273 const AttributeList *AttrList) {
5274 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005275 if (l->getKind() == AttributeList::AT_Annotate) {
David Majnemer706f3152014-12-14 01:05:01 +00005276 ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005277 } else {
5278 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5279 return true;
5280 }
5281 }
5282
5283 return false;
5284}
5285
John McCall42856de2011-10-01 05:17:03 +00005286/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5287/// contains any decl attributes that we should warn about.
5288static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5289 for ( ; A; A = A->getNext()) {
5290 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00005291 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00005292 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5293
5294 if (A->getKind() == AttributeList::UnknownAttribute) {
5295 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5296 << A->getName() << A->getRange();
5297 } else {
5298 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5299 << A->getName() << A->getRange();
5300 }
5301 }
5302}
5303
5304/// checkUnusedDeclAttributes - Given a declarator which is not being
5305/// used to build a declaration, complain about any decl attributes
5306/// which might be lying around on it.
5307void Sema::checkUnusedDeclAttributes(Declarator &D) {
5308 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5309 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5310 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5311 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5312}
5313
Ryan Flynn7d470f32009-07-30 03:15:39 +00005314/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00005315/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00005316NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5317 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00005318 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00005319 NamedDecl *NewD = nullptr;
Ryan Flynn7d470f32009-07-30 03:15:39 +00005320 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00005321 FunctionDecl *NewFD;
5322 // FIXME: Missing call to CheckFunctionDeclaration().
5323 // FIXME: Mangling?
5324 // FIXME: Is the qualifier info correct?
5325 // FIXME: Is the DeclContext correct?
5326 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5327 Loc, Loc, DeclarationName(II),
5328 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005329 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00005330 FD->hasPrototype(),
5331 false/*isConstexprSpecified*/);
5332 NewD = NewFD;
5333
5334 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00005335 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00005336
5337 // Fake up parameter variables; they are declared as if this were
5338 // a typedef.
5339 QualType FDTy = FD->getType();
5340 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5341 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00005342 for (const auto &AI : FT->param_types()) {
5343 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00005344 Param->setScopeInfo(0, Params.size());
5345 Params.push_back(Param);
5346 }
David Blaikie9c70e042011-09-21 18:16:56 +00005347 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005348 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005349 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5350 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005351 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005352 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005353 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00005354 if (VD->getQualifier()) {
5355 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005356 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005357 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005358 }
5359 return NewD;
5360}
5361
James Dennett634962f2012-06-14 21:40:34 +00005362/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005363/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005364void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005365 if (W.getUsed()) return; // only do this once
5366 W.setUsed(true);
5367 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5368 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005369 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00005370 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
5371 W.getLocation()));
5372 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00005373 WeakTopLevelDecl.push_back(NewD);
5374 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5375 // to insert Decl at TU scope, sorry.
5376 DeclContext *SavedContext = CurContext;
5377 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00005378 NewD->setDeclContext(CurContext);
5379 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00005380 PushOnScopeChains(NewD, S);
5381 CurContext = SavedContext;
5382 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00005383 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005384 }
5385}
5386
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005387void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5388 // It's valid to "forward-declare" #pragma weak, in which case we
5389 // have to do this.
5390 LoadExternalWeakUndeclaredIdentifiers();
5391 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005392 NamedDecl *ND = nullptr;
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005393 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5394 if (VD->isExternC())
5395 ND = VD;
5396 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5397 if (FD->isExternC())
5398 ND = FD;
5399 if (ND) {
5400 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00005401 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005402 if (I != WeakUndeclaredIdentifiers.end()) {
5403 WeakInfo W = I->second;
5404 DeclApplyPragmaWeak(S, ND, W);
5405 WeakUndeclaredIdentifiers[Id] = W;
5406 }
5407 }
5408 }
5409 }
5410}
5411
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005412/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5413/// it, apply them to D. This is a bit tricky because PD can have attributes
5414/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00005415void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005416 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005417 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00005418 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005419
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005420 // Walk the declarator structure, applying decl attributes that were in a type
5421 // position to the decl itself. This handles cases like:
5422 // int *__attr__(x)** D;
5423 // when X is a decl attribute.
5424 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5425 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00005426 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005427
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005428 // Finally, apply any attributes on the decl itself.
5429 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00005430 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005431}
John McCall28a6aea2009-11-04 02:18:39 +00005432
John McCall31168b02011-06-15 23:02:42 +00005433/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00005434/// If so, it'll still be annotated with an attribute that makes it
5435/// illegal to actually use.
5436static bool isForbiddenTypeAllowed(Sema &S, Decl *decl,
5437 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00005438 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00005439 // Private ivars are always okay. Unfortunately, people don't
5440 // always properly make their ivars private, even in system headers.
5441 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005442 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5443 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005444 return false;
5445
John McCallc6af8c62015-10-28 05:03:19 +00005446 // Silently accept unsupported uses of __weak in both user and system
5447 // declarations when it's been disabled, for ease of integration with
5448 // -fno-objc-arc files. We do have to take some care against attempts
5449 // to define such things; for now, we've only done that for ivars
5450 // and properties.
5451 if ((isa<ObjCIvarDecl>(decl) || isa<ObjCPropertyDecl>(decl))) {
5452 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
5453 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
5454 reason = UnavailableAttr::IR_ForbiddenWeak;
5455 return true;
5456 }
John McCallb61e14e2015-10-27 04:54:50 +00005457 }
5458
John McCallc6af8c62015-10-28 05:03:19 +00005459 // Allow all sorts of things in system headers.
5460 if (S.Context.getSourceManager().isInSystemHeader(decl->getLocation())) {
5461 // Currently, all the failures dealt with this way are due to ARC
5462 // restrictions.
5463 reason = UnavailableAttr::IR_ARCForbiddenType;
5464 return true;
John McCallb61e14e2015-10-27 04:54:50 +00005465 }
5466
5467 return false;
John McCall31168b02011-06-15 23:02:42 +00005468}
5469
5470/// Handle a delayed forbidden-type diagnostic.
5471static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5472 Decl *decl) {
John McCallc6af8c62015-10-28 05:03:19 +00005473 auto reason = UnavailableAttr::IR_None;
5474 if (decl && isForbiddenTypeAllowed(S, decl, diag, reason)) {
5475 assert(reason && "didn't set reason?");
5476 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", reason,
5477 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00005478 return;
5479 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005480 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005481 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005482 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005483 // kind of forbidden type messages on unavailable functions.
5484 if (FD->hasAttr<UnavailableAttr>() &&
5485 diag.getForbiddenTypeDiagnostic() ==
5486 diag::err_arc_array_param_no_ownership) {
5487 diag.Triggered = true;
5488 return;
5489 }
5490 }
John McCall31168b02011-06-15 23:02:42 +00005491
5492 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5493 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5494 diag.Triggered = true;
5495}
5496
Aaron Ballmanfb237522014-10-15 15:37:51 +00005497
5498static bool isDeclDeprecated(Decl *D) {
5499 do {
5500 if (D->isDeprecated())
5501 return true;
5502 // A category implicitly has the availability of the interface.
5503 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005504 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
5505 return Interface->isDeprecated();
Aaron Ballmanfb237522014-10-15 15:37:51 +00005506 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5507 return false;
5508}
5509
5510static bool isDeclUnavailable(Decl *D) {
5511 do {
5512 if (D->isUnavailable())
5513 return true;
5514 // A category implicitly has the availability of the interface.
5515 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005516 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
5517 return Interface->isUnavailable();
Aaron Ballmanfb237522014-10-15 15:37:51 +00005518 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5519 return false;
5520}
5521
Nico Weber0055a192015-03-19 19:18:22 +00005522static void DoEmitAvailabilityWarning(Sema &S, Sema::AvailabilityDiagnostic K,
Aaron Ballmanfb237522014-10-15 15:37:51 +00005523 Decl *Ctx, const NamedDecl *D,
5524 StringRef Message, SourceLocation Loc,
5525 const ObjCInterfaceDecl *UnknownObjCClass,
5526 const ObjCPropertyDecl *ObjCProperty,
5527 bool ObjCPropertyAccess) {
5528 // Diagnostics for deprecated or unavailable.
5529 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00005530 unsigned diag_available_here = diag::note_availability_specified_here;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005531
5532 // Matches 'diag::note_property_attribute' options.
5533 unsigned property_note_select;
5534
5535 // Matches diag::note_availability_specified_here.
5536 unsigned available_here_select_kind;
5537
5538 // Don't warn if our current context is deprecated or unavailable.
5539 switch (K) {
Nico Weber0055a192015-03-19 19:18:22 +00005540 case Sema::AD_Deprecation:
Jordan Rosed17c03e2015-04-30 17:20:35 +00005541 if (isDeclDeprecated(Ctx) || isDeclUnavailable(Ctx))
Aaron Ballmanfb237522014-10-15 15:37:51 +00005542 return;
5543 diag = !ObjCPropertyAccess ? diag::warn_deprecated
5544 : diag::warn_property_method_deprecated;
5545 diag_message = diag::warn_deprecated_message;
5546 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
5547 property_note_select = /* deprecated */ 0;
5548 available_here_select_kind = /* deprecated */ 2;
5549 break;
5550
Nico Weber0055a192015-03-19 19:18:22 +00005551 case Sema::AD_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00005552 if (isDeclUnavailable(Ctx))
5553 return;
5554 diag = !ObjCPropertyAccess ? diag::err_unavailable
5555 : diag::err_property_method_unavailable;
5556 diag_message = diag::err_unavailable_message;
5557 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
5558 property_note_select = /* unavailable */ 1;
5559 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00005560
John McCallc6af8c62015-10-28 05:03:19 +00005561 if (auto attr = D->getAttr<UnavailableAttr>()) {
5562 if (attr->isImplicit() && attr->getImplicitReason()) {
5563 // Most of these failures are due to extra restrictions in ARC;
5564 // reflect that in the primary diagnostic when applicable.
5565 auto flagARCError = [&] {
5566 if (S.getLangOpts().ObjCAutoRefCount &&
5567 S.getSourceManager().isInSystemHeader(D->getLocation()))
5568 diag = diag::err_unavailable_in_arc;
5569 };
5570
5571 switch (attr->getImplicitReason()) {
5572 case UnavailableAttr::IR_None: break;
5573
5574 case UnavailableAttr::IR_ARCForbiddenType:
5575 flagARCError();
5576 diag_available_here = diag::note_arc_forbidden_type;
5577 break;
5578
5579 case UnavailableAttr::IR_ForbiddenWeak:
5580 if (S.getLangOpts().ObjCWeakRuntime)
5581 diag_available_here = diag::note_arc_weak_disabled;
5582 else
5583 diag_available_here = diag::note_arc_weak_no_runtime;
5584 break;
5585
5586 case UnavailableAttr::IR_ARCForbiddenConversion:
5587 flagARCError();
5588 diag_available_here = diag::note_performs_forbidden_arc_conversion;
5589 break;
5590
5591 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
5592 flagARCError();
5593 diag_available_here = diag::note_arc_init_returns_unrelated;
5594 break;
5595
5596 case UnavailableAttr::IR_ARCFieldWithOwnership:
5597 flagARCError();
5598 diag_available_here = diag::note_arc_field_with_ownership;
5599 break;
5600 }
5601 }
John McCallb61e14e2015-10-27 04:54:50 +00005602 }
5603
Aaron Ballmanfb237522014-10-15 15:37:51 +00005604 break;
5605
Nico Weber0055a192015-03-19 19:18:22 +00005606 case Sema::AD_Partial:
5607 diag = diag::warn_partial_availability;
5608 diag_message = diag::warn_partial_message;
5609 diag_fwdclass_message = diag::warn_partial_fwdclass_message;
5610 property_note_select = /* partial */ 2;
5611 available_here_select_kind = /* partial */ 3;
5612 break;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005613 }
5614
Aaron Ballmanfb237522014-10-15 15:37:51 +00005615 if (!Message.empty()) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005616 S.Diag(Loc, diag_message) << D << Message;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005617 if (ObjCProperty)
5618 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5619 << ObjCProperty->getDeclName() << property_note_select;
5620 } else if (!UnknownObjCClass) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005621 S.Diag(Loc, diag) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005622 if (ObjCProperty)
5623 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5624 << ObjCProperty->getDeclName() << property_note_select;
5625 } else {
Aaron Ballman43f40102014-11-14 22:34:56 +00005626 S.Diag(Loc, diag_fwdclass_message) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005627 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5628 }
5629
John McCallb61e14e2015-10-27 04:54:50 +00005630 S.Diag(D->getLocation(), diag_available_here)
Aaron Ballmanfb237522014-10-15 15:37:51 +00005631 << D << available_here_select_kind;
Nico Weber0055a192015-03-19 19:18:22 +00005632 if (K == Sema::AD_Partial)
5633 S.Diag(Loc, diag::note_partial_availability_silence) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005634}
5635
5636static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
5637 Decl *Ctx) {
Nico Weber0055a192015-03-19 19:18:22 +00005638 assert(DD.Kind == DelayedDiagnostic::Deprecation ||
5639 DD.Kind == DelayedDiagnostic::Unavailable);
5640 Sema::AvailabilityDiagnostic AD = DD.Kind == DelayedDiagnostic::Deprecation
5641 ? Sema::AD_Deprecation
5642 : Sema::AD_Unavailable;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005643 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00005644 DoEmitAvailabilityWarning(
5645 S, AD, Ctx, DD.getDeprecationDecl(), DD.getDeprecationMessage(), DD.Loc,
5646 DD.getUnknownObjCClass(), DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00005647}
5648
John McCall2ec85372012-05-07 06:16:41 +00005649void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5650 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005651 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005652 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005653
John McCall2ec85372012-05-07 06:16:41 +00005654 // When delaying diagnostics to run in the context of a parsed
5655 // declaration, we only want to actually emit anything if parsing
5656 // succeeds.
5657 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005658
John McCall2ec85372012-05-07 06:16:41 +00005659 // We emit all the active diagnostics in this pool or any of its
5660 // parents. In general, we'll get one pool for the decl spec
5661 // and a child pool for each declarator; in a decl group like:
5662 // deprecated_typedef foo, *bar, baz();
5663 // only the declarator pops will be passed decls. This is correct;
5664 // we really do need to consider delayed diagnostics from the decl spec
5665 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005666 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005667 do {
John McCall6347b682012-05-07 06:16:58 +00005668 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005669 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5670 // This const_cast is a bit lame. Really, Triggered should be mutable.
5671 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005672 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005673 continue;
5674
John McCallc1465822011-02-14 07:13:47 +00005675 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005676 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00005677 case DelayedDiagnostic::Unavailable:
5678 // Don't bother giving deprecation/unavailable diagnostics if
5679 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00005680 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00005681 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00005682 break;
5683
5684 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005685 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005686 break;
John McCall31168b02011-06-15 23:02:42 +00005687
5688 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005689 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005690 break;
John McCall86121512010-01-27 03:50:35 +00005691 }
5692 }
John McCall2ec85372012-05-07 06:16:41 +00005693 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005694}
5695
John McCall6347b682012-05-07 06:16:58 +00005696/// Given a set of delayed diagnostics, re-emit them as if they had
5697/// been delayed in the current context instead of in the given pool.
5698/// Essentially, this just moves them to the current pool.
5699void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5700 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5701 assert(curPool && "re-emitting in undelayed context not supported");
5702 curPool->steal(pool);
5703}
5704
Ted Kremenekb79ee572013-12-18 23:30:06 +00005705void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
5706 NamedDecl *D, StringRef Message,
5707 SourceLocation Loc,
5708 const ObjCInterfaceDecl *UnknownObjCClass,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00005709 const ObjCPropertyDecl *ObjCProperty,
5710 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00005711 // Delay if we're currently parsing a declaration.
Nico Weber0055a192015-03-19 19:18:22 +00005712 if (DelayedDiagnostics.shouldDelayDiagnostics() && AD != AD_Partial) {
Nico Weber462fd1e2015-01-07 23:50:05 +00005713 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(
5714 AD, Loc, D, UnknownObjCClass, ObjCProperty, Message,
5715 ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00005716 return;
5717 }
5718
Ted Kremenekb79ee572013-12-18 23:30:06 +00005719 Decl *Ctx = cast<Decl>(getCurLexicalContext());
Nico Weber0055a192015-03-19 19:18:22 +00005720 DoEmitAvailabilityWarning(*this, AD, Ctx, D, Message, Loc, UnknownObjCClass,
5721 ObjCProperty, ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00005722}