blob: 3395381b6a3650c21bcdc2e074b6af9c42d74dfa [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);
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000435 if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &P,
436 void *) {
437 return BS->getType()->getAs<RecordType>()
438 ->getDecl()->hasAttr<CapabilityAttr>();
Craig Topperc3ec1492014-05-26 06:22:03 +0000439 }, nullptr, BPaths))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000440 return true;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000441 }
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000442 return false;
443}
444
Aaron Ballman76050722014-04-04 15:13:57 +0000445static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000446 const auto *TD = Ty->getAs<TypedefType>();
447 if (!TD)
448 return false;
449
450 TypedefNameDecl *TN = TD->getDecl();
451 if (!TN)
452 return false;
453
454 return TN->hasAttr<CapabilityAttr>();
455}
456
Aaron Ballman76050722014-04-04 15:13:57 +0000457static bool typeHasCapability(Sema &S, QualType Ty) {
458 if (checkTypedefTypeForCapability(Ty))
459 return true;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000460
Aaron Ballman76050722014-04-04 15:13:57 +0000461 if (checkRecordTypeForCapability(S, Ty))
462 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000463
Aaron Ballman76050722014-04-04 15:13:57 +0000464 return false;
465}
466
467static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
468 // Capability expressions are simple expressions involving the boolean logic
469 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
470 // a DeclRefExpr is found, its type should be checked to determine whether it
471 // is a capability or not.
472
473 if (const auto *E = dyn_cast<DeclRefExpr>(Ex))
474 return typeHasCapability(S, E->getType());
475 else if (const auto *E = dyn_cast<CastExpr>(Ex))
476 return isCapabilityExpr(S, E->getSubExpr());
477 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
478 return isCapabilityExpr(S, E->getSubExpr());
479 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
480 if (E->getOpcode() == UO_LNot)
481 return isCapabilityExpr(S, E->getSubExpr());
482 return false;
483 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
484 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
485 return isCapabilityExpr(S, E->getLHS()) &&
486 isCapabilityExpr(S, E->getRHS());
487 return false;
488 }
489
490 return false;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000491}
492
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000493/// \brief Checks that all attribute arguments, starting from Sidx, resolve to
494/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000495/// \param Sidx The attribute argument index to start checking with.
496/// \param ParamIdxOk Whether an argument can be indexing into a function
497/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000498static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
499 const AttributeList &Attr,
500 SmallVectorImpl<Expr *> &Args,
501 int Sidx = 0,
502 bool ParamIdxOk = false) {
503 for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000504 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000505
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000506 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000507 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000508 Args.push_back(ArgExp);
509 continue;
510 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000511
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000512 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000513 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000514 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000515 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000516 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000517 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000518 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000519 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000520
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000521 // We allow constant strings to be used as a placeholder for expressions
522 // that are not valid C++ syntax, but warn that they are ignored.
523 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
524 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000525 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000526 continue;
527 }
528
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000529 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000530
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000531 // A pointer to member expression of the form &MyClass::mu is treated
532 // specially -- we need to look at the type of the member.
533 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
534 if (UOp->getOpcode() == UO_AddrOf)
535 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
536 if (DRE->getDecl()->isCXXInstanceMember())
537 ArgTy = DRE->getDecl()->getType();
538
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000539 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000540 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000541
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000542 // Now check if we index into a record type function param.
543 if(!RT && ParamIdxOk) {
544 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000545 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
546 if(FD && IL) {
547 unsigned int NumParams = FD->getNumParams();
548 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000549 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
550 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
551 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000552 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
553 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000554 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000555 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000556 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000557 }
558 }
559
Aaron Ballman76050722014-04-04 15:13:57 +0000560 // If the type does not have a capability, see if the components of the
561 // expression have capabilities. This allows for writing C code where the
562 // capability may be on the type, and the expression is a capability
563 // boolean logic expression. Eg) requires_capability(A || B && !C)
564 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
565 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
566 << Attr.getName() << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000567
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000568 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000569 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000570}
571
Chris Lattner58418ff2008-06-29 00:16:31 +0000572//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000573// Attribute Implementations
574//===----------------------------------------------------------------------===//
575
Michael Hana9171bc2012-08-03 17:40:43 +0000576static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000577 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000578 if (!threadSafetyCheckIsPointer(S, D, Attr))
579 return;
580
Michael Han99315932013-01-24 16:46:58 +0000581 D->addAttr(::new (S.Context)
582 PtGuardedVarAttr(Attr.getRange(), S.Context,
583 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000584}
585
Michael Hana9171bc2012-08-03 17:40:43 +0000586static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
587 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000588 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000589 SmallVector<Expr*, 1> Args;
590 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000591 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000592 unsigned Size = Args.size();
593 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000594 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000595
Michael Han3be3b442012-07-23 18:48:41 +0000596 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000597
Michael Han3be3b442012-07-23 18:48:41 +0000598 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000599}
600
Michael Han3be3b442012-07-23 18:48:41 +0000601static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000602 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000603 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
604 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000605
Aaron Ballman36a53502014-01-16 13:03:14 +0000606 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg,
607 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000608}
609
Michael Hana9171bc2012-08-03 17:40:43 +0000610static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000611 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000612 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000613 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
614 return;
615
616 if (!threadSafetyCheckIsPointer(S, D, Attr))
617 return;
618
619 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +0000620 S.Context, Arg,
621 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000622}
623
Michael Hana9171bc2012-08-03 17:40:43 +0000624static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
625 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000626 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000627 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000628 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000629
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000630 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000631 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000632 if (!QT->isDependentType()) {
633 const RecordType *RT = getRecordType(QT);
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000634 if (!RT || !RT->getDecl()->hasAttr<CapabilityAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000635 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000636 << Attr.getName();
637 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000638 }
639 }
640
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000641 // Check that all arguments are lockable objects.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000642 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000643 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000644 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000645
Michael Han3be3b442012-07-23 18:48:41 +0000646 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000647}
648
Michael Hana9171bc2012-08-03 17:40:43 +0000649static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000650 const AttributeList &Attr) {
651 SmallVector<Expr*, 1> Args;
652 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
653 return;
654
655 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000656 D->addAttr(::new (S.Context)
657 AcquiredAfterAttr(Attr.getRange(), S.Context,
658 StartArg, Args.size(),
659 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000660}
661
Michael Hana9171bc2012-08-03 17:40:43 +0000662static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000663 const AttributeList &Attr) {
664 SmallVector<Expr*, 1> Args;
665 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
666 return;
667
668 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000669 D->addAttr(::new (S.Context)
670 AcquiredBeforeAttr(Attr.getRange(), S.Context,
671 StartArg, Args.size(),
672 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000673}
674
Michael Hana9171bc2012-08-03 17:40:43 +0000675static bool checkLockFunAttrCommon(Sema &S, Decl *D,
676 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000677 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000678 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000679 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000680 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000681
Michael Han3be3b442012-07-23 18:48:41 +0000682 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000683}
684
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000685static void handleAssertSharedLockAttr(Sema &S, Decl *D,
686 const AttributeList &Attr) {
687 SmallVector<Expr*, 1> Args;
688 if (!checkLockFunAttrCommon(S, D, Attr, Args))
689 return;
690
691 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000692 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000693 D->addAttr(::new (S.Context)
694 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
695 Attr.getAttributeSpellingListIndex()));
696}
697
698static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
699 const AttributeList &Attr) {
700 SmallVector<Expr*, 1> Args;
701 if (!checkLockFunAttrCommon(S, D, Attr, Args))
702 return;
703
704 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000705 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000706 D->addAttr(::new (S.Context)
707 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
708 StartArg, Size,
709 Attr.getAttributeSpellingListIndex()));
710}
711
712
Michael Hana9171bc2012-08-03 17:40:43 +0000713static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
714 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000715 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000716 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000717 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000718
Aaron Ballman00e99962013-08-31 01:11:41 +0000719 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000720 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000721 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000722 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000723 }
724
725 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000726 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000727
Michael Han3be3b442012-07-23 18:48:41 +0000728 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000729}
730
Michael Hana9171bc2012-08-03 17:40:43 +0000731static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000732 const AttributeList &Attr) {
733 SmallVector<Expr*, 2> Args;
734 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
735 return;
736
Michael Han99315932013-01-24 16:46:58 +0000737 D->addAttr(::new (S.Context)
738 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000739 Attr.getArgAsExpr(0),
740 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000741 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000742}
743
Michael Hana9171bc2012-08-03 17:40:43 +0000744static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000745 const AttributeList &Attr) {
746 SmallVector<Expr*, 2> Args;
747 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
748 return;
749
Nico Weber462fd1e2015-01-07 23:50:05 +0000750 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(
751 Attr.getRange(), S.Context, Attr.getArgAsExpr(0), Args.data(),
752 Args.size(), Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000753}
754
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000755static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000756 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000757 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000758 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000759 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000760 unsigned Size = Args.size();
761 if (Size == 0)
762 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000763
Michael Han99315932013-01-24 16:46:58 +0000764 D->addAttr(::new (S.Context)
765 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
766 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000767}
768
769static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000770 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000771 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000772 return;
773
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000774 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000775 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000776 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000777 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000778 if (Size == 0)
779 return;
780 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000781
Michael Han99315932013-01-24 16:46:58 +0000782 D->addAttr(::new (S.Context)
783 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
784 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000785}
786
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000787static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
788 Expr *Cond = Attr.getArgAsExpr(0);
789 if (!Cond->isTypeDependent()) {
790 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
791 if (Converted.isInvalid())
792 return;
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000793 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000794 }
795
796 StringRef Msg;
797 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
798 return;
799
800 SmallVector<PartialDiagnosticAt, 8> Diags;
801 if (!Cond->isValueDependent() &&
802 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
803 Diags)) {
804 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
805 for (int I = 0, N = Diags.size(); I != N; ++I)
806 S.Diag(Diags[I].first, Diags[I].second);
807 return;
808 }
809
810 D->addAttr(::new (S.Context)
811 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
812 Attr.getAttributeSpellingListIndex()));
813}
814
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000815static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000816 ConsumableAttr::ConsumedState DefaultState;
817
818 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000819 IdentifierLoc *IL = Attr.getArgAsIdent(0);
820 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
821 DefaultState)) {
822 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
823 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000824 return;
825 }
David Blaikie16f76d22013-09-06 01:28:43 +0000826 } else {
827 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
828 << Attr.getName() << AANT_ArgumentIdentifier;
829 return;
830 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000831
832 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000833 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000834 Attr.getAttributeSpellingListIndex()));
835}
836
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000837
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000838static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
839 const AttributeList &Attr) {
840 ASTContext &CurrContext = S.getASTContext();
841 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
842
843 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
844 if (!RD->hasAttr<ConsumableAttr>()) {
845 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
846 RD->getNameAsString();
847
848 return false;
849 }
850 }
851
852 return true;
853}
854
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000855
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000856static void handleCallableWhenAttr(Sema &S, Decl *D,
857 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000858 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
859 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000860
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000861 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
862 return;
863
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000864 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
865 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
866 CallableWhenAttr::ConsumedState CallableState;
867
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000868 StringRef StateString;
869 SourceLocation Loc;
Aaron Ballman55ef1512014-12-19 16:42:04 +0000870 if (Attr.isArgIdent(ArgIndex)) {
871 IdentifierLoc *Ident = Attr.getArgAsIdent(ArgIndex);
872 StateString = Ident->Ident->getName();
873 Loc = Ident->Loc;
874 } else {
875 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
876 return;
877 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000878
879 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000880 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000881 S.Diag(Loc, diag::warn_attribute_type_not_supported)
882 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000883 return;
884 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000885
886 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000887 }
888
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000889 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000890 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
891 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000892}
893
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000894
DeLesley Hutchins69391772013-10-17 23:23:53 +0000895static void handleParamTypestateAttr(Sema &S, Decl *D,
896 const AttributeList &Attr) {
DeLesley Hutchins69391772013-10-17 23:23:53 +0000897 ParamTypestateAttr::ConsumedState ParamState;
898
899 if (Attr.isArgIdent(0)) {
900 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
901 StringRef StateString = Ident->Ident->getName();
902
903 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
904 ParamState)) {
905 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
906 << Attr.getName() << StateString;
907 return;
908 }
909 } else {
910 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
911 Attr.getName() << AANT_ArgumentIdentifier;
912 return;
913 }
914
915 // FIXME: This check is currently being done in the analysis. It can be
916 // enabled here only after the parser propagates attributes at
917 // template specialization definition, not declaration.
918 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
919 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
920 //
921 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
922 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
923 // ReturnType.getAsString();
924 // return;
925 //}
926
927 D->addAttr(::new (S.Context)
928 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
929 Attr.getAttributeSpellingListIndex()));
930}
931
932
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000933static void handleReturnTypestateAttr(Sema &S, Decl *D,
934 const AttributeList &Attr) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000935 ReturnTypestateAttr::ConsumedState ReturnState;
936
937 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000938 IdentifierLoc *IL = Attr.getArgAsIdent(0);
939 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
940 ReturnState)) {
941 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
942 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000943 return;
944 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000945 } else {
946 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
947 Attr.getName() << AANT_ArgumentIdentifier;
948 return;
949 }
950
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000951 // FIXME: This check is currently being done in the analysis. It can be
952 // enabled here only after the parser propagates attributes at
953 // template specialization definition, not declaration.
954 //QualType ReturnType;
955 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000956 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
957 // ReturnType = Param->getType();
958 //
959 //} else if (const CXXConstructorDecl *Constructor =
960 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000961 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
962 //
963 //} else {
964 //
965 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
966 //}
967 //
968 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
969 //
970 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
971 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
972 // ReturnType.getAsString();
973 // return;
974 //}
975
976 D->addAttr(::new (S.Context)
977 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
978 Attr.getAttributeSpellingListIndex()));
979}
980
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000981
982static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000983 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
984 return;
985
986 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000987 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000988 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
989 StringRef Param = Ident->Ident->getName();
990 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
991 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
992 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000993 return;
994 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000995 } else {
996 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
997 Attr.getName() << AANT_ArgumentIdentifier;
998 return;
999 }
1000
1001 D->addAttr(::new (S.Context)
1002 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1003 Attr.getAttributeSpellingListIndex()));
1004}
1005
Chris Wailes9385f9f2013-10-29 20:28:41 +00001006static void handleTestTypestateAttr(Sema &S, Decl *D,
1007 const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001008 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1009 return;
1010
Chris Wailes9385f9f2013-10-29 20:28:41 +00001011 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001012 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001013 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1014 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001015 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001016 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1017 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001018 return;
1019 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001020 } else {
1021 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1022 Attr.getName() << AANT_ArgumentIdentifier;
1023 return;
1024 }
1025
1026 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001027 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001028 Attr.getAttributeSpellingListIndex()));
1029}
1030
Chandler Carruthedc2c642011-07-02 00:01:44 +00001031static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1032 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001033 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001034 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001035}
1036
Chandler Carruthedc2c642011-07-02 00:01:44 +00001037static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001038 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Aaron Ballman36a53502014-01-16 13:03:14 +00001039 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context,
1040 Attr.getAttributeSpellingListIndex()));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001041 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001042 // If the alignment is less than or equal to 8 bits, the packed attribute
1043 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001044 if (!FD->getType()->isDependentType() &&
1045 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001046 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001047 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001048 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001049 else
Michael Han99315932013-01-24 16:46:58 +00001050 FD->addAttr(::new (S.Context)
1051 PackedAttr(Attr.getRange(), S.Context,
1052 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001053 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001054 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001055}
1056
Ted Kremenek7fd17232011-09-29 07:02:25 +00001057static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1058 // The IBOutlet/IBOutletCollection attributes only apply to instance
1059 // variables or properties of Objective-C classes. The outlet must also
1060 // have an object reference type.
1061 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1062 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001063 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001064 << Attr.getName() << VD->getType() << 0;
1065 return false;
1066 }
1067 }
1068 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1069 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001070 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001071 << Attr.getName() << PD->getType() << 1;
1072 return false;
1073 }
1074 }
1075 else {
1076 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1077 return false;
1078 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001079
Ted Kremenek7fd17232011-09-29 07:02:25 +00001080 return true;
1081}
1082
Chandler Carruthedc2c642011-07-02 00:01:44 +00001083static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001084 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001085 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001086
Michael Han99315932013-01-24 16:46:58 +00001087 D->addAttr(::new (S.Context)
1088 IBOutletAttr(Attr.getRange(), S.Context,
1089 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001090}
1091
Chandler Carruthedc2c642011-07-02 00:01:44 +00001092static void handleIBOutletCollection(Sema &S, Decl *D,
1093 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001094
1095 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001096 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001097 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1098 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001099 return;
1100 }
1101
Ted Kremenek7fd17232011-09-29 07:02:25 +00001102 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001103 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001104
Richard Smithb1f9a282013-10-31 01:56:18 +00001105 ParsedType PT;
1106
1107 if (Attr.hasParsedType())
1108 PT = Attr.getTypeArg();
1109 else {
1110 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1111 S.getScopeForContext(D->getDeclContext()->getParent()));
1112 if (!PT) {
1113 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1114 return;
1115 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001116 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001117
Craig Topperc3ec1492014-05-26 06:22:03 +00001118 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001119 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1120 if (!QTLoc)
1121 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001122
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001123 // Diagnose use of non-object type in iboutletcollection attribute.
1124 // FIXME. Gnu attribute extension ignores use of builtin types in
1125 // attributes. So, __attribute__((iboutletcollection(char))) will be
1126 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001127 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001128 S.Diag(Attr.getLoc(),
1129 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1130 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001131 return;
1132 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001133
Michael Han99315932013-01-24 16:46:58 +00001134 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001135 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001136 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001137}
1138
Hal Finkelee90a222014-09-26 05:04:30 +00001139bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1140 if (RefOkay) {
1141 if (T->isReferenceType())
1142 return true;
1143 } else {
1144 T = T.getNonReferenceType();
1145 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001146
Hal Finkelee90a222014-09-26 05:04:30 +00001147 // The nonnull attribute, and other similar attributes, can be applied to a
1148 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001149 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001150 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1151 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001152 for (const auto *I : UD->fields()) {
1153 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001154 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1155 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001156 }
1157 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001158 }
1159
1160 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001161}
1162
Ted Kremenek9aedc152014-01-17 06:24:56 +00001163static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001164 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001165 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001166 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001167 if (!S.isValidPointerAttrType(T)) {
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001168 S.Diag(Attr.getLoc(), isReturnValue
1169 ? diag::warn_attribute_return_pointers_only
1170 : diag::warn_attribute_pointers_only)
Hal Finkelee90a222014-09-26 05:04:30 +00001171 << Attr.getName() << AttrParmRange << TypeRange;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001172 return false;
1173 }
1174 return true;
1175}
1176
Chandler Carruthedc2c642011-07-02 00:01:44 +00001177static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001178 SmallVector<unsigned, 8> NonNullArgs;
Richard Smith588bd9b2014-08-27 04:59:42 +00001179 for (unsigned I = 0; I < Attr.getNumArgs(); ++I) {
1180 Expr *Ex = Attr.getArgAsExpr(I);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001181 uint64_t Idx;
Richard Smith588bd9b2014-08-27 04:59:42 +00001182 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001183 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001184
1185 // Is the function argument a pointer type?
Richard Smith588bd9b2014-08-27 04:59:42 +00001186 if (Idx < getFunctionOrMethodNumParams(D) &&
1187 !attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001188 Ex->getSourceRange(),
1189 getFunctionOrMethodParamRange(D, Idx)))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001190 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001191
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001192 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001193 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001194
1195 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001196 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1197 // check if the attribute came from a macro expansion or a template
1198 // instantiation.
1199 if (NonNullArgs.empty() && Attr.getLoc().isFileID() &&
1200 S.ActiveTemplateInstantiations.empty()) {
1201 bool AnyPointers = isFunctionOrMethodVariadic(D);
1202 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1203 I != E && !AnyPointers; ++I) {
1204 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001205 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001206 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001207 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001208
Richard Smith588bd9b2014-08-27 04:59:42 +00001209 if (!AnyPointers)
1210 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001211 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001212
Richard Smith588bd9b2014-08-27 04:59:42 +00001213 unsigned *Start = NonNullArgs.data();
1214 unsigned Size = NonNullArgs.size();
1215 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001216 D->addAttr(::new (S.Context)
Richard Smith588bd9b2014-08-27 04:59:42 +00001217 NonNullAttr(Attr.getRange(), S.Context, Start, Size,
Michael Han99315932013-01-24 16:46:58 +00001218 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001219}
1220
Jordan Rosec9399072014-02-11 17:27:59 +00001221static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1222 const AttributeList &Attr) {
1223 if (Attr.getNumArgs() > 0) {
1224 if (D->getFunctionType()) {
1225 handleNonNullAttr(S, D, Attr);
1226 } else {
1227 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1228 << D->getSourceRange();
1229 }
1230 return;
1231 }
1232
1233 // Is the argument a pointer type?
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001234 if (!attrNonNullArgCheck(S, D->getType(), Attr, SourceRange(),
1235 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001236 return;
1237
1238 D->addAttr(::new (S.Context)
Craig Topperc3ec1492014-05-26 06:22:03 +00001239 NonNullAttr(Attr.getRange(), S.Context, nullptr, 0,
Jordan Rosec9399072014-02-11 17:27:59 +00001240 Attr.getAttributeSpellingListIndex()));
1241}
1242
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001243static void handleReturnsNonNullAttr(Sema &S, Decl *D,
1244 const AttributeList &Attr) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001245 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001246 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1247 if (!attrNonNullArgCheck(S, ResultType, Attr, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001248 /* isReturnValue */ true))
1249 return;
1250
1251 D->addAttr(::new (S.Context)
1252 ReturnsNonNullAttr(Attr.getRange(), S.Context,
1253 Attr.getAttributeSpellingListIndex()));
1254}
1255
Hal Finkelee90a222014-09-26 05:04:30 +00001256static void handleAssumeAlignedAttr(Sema &S, Decl *D,
1257 const AttributeList &Attr) {
1258 Expr *E = Attr.getArgAsExpr(0),
1259 *OE = Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr;
1260 S.AddAssumeAlignedAttr(Attr.getRange(), D, E, OE,
1261 Attr.getAttributeSpellingListIndex());
1262}
1263
1264void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
1265 Expr *OE, unsigned SpellingListIndex) {
1266 QualType ResultType = getFunctionOrMethodResultType(D);
1267 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1268
1269 AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex);
1270 SourceLocation AttrLoc = AttrRange.getBegin();
1271
1272 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1273 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1274 << &TmpAttr << AttrRange << SR;
1275 return;
1276 }
1277
1278 if (!E->isValueDependent()) {
1279 llvm::APSInt I(64);
1280 if (!E->isIntegerConstantExpr(I, Context)) {
1281 if (OE)
1282 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1283 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1284 << E->getSourceRange();
1285 else
1286 Diag(AttrLoc, diag::err_attribute_argument_type)
1287 << &TmpAttr << AANT_ArgumentIntegerConstant
1288 << E->getSourceRange();
1289 return;
1290 }
1291
1292 if (!I.isPowerOf2()) {
1293 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1294 << E->getSourceRange();
1295 return;
1296 }
1297 }
1298
1299 if (OE) {
1300 if (!OE->isValueDependent()) {
1301 llvm::APSInt I(64);
1302 if (!OE->isIntegerConstantExpr(I, Context)) {
1303 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1304 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1305 << OE->getSourceRange();
1306 return;
1307 }
1308 }
1309 }
1310
1311 D->addAttr(::new (Context)
1312 AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex));
1313}
1314
Chandler Carruthedc2c642011-07-02 00:01:44 +00001315static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001316 // This attribute must be applied to a function declaration. The first
1317 // argument to the attribute must be an identifier, the name of the resource,
1318 // for example: malloc. The following arguments must be argument indexes, the
1319 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001320 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001321 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001322 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001323
Aaron Ballman00e99962013-08-31 01:11:41 +00001324 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001325 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001326 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001327 return;
1328 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001329
Richard Smith852e9ce2013-11-27 01:46:48 +00001330 // Figure out our Kind.
1331 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001332 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001333 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001334
Richard Smith852e9ce2013-11-27 01:46:48 +00001335 // Check arguments.
1336 switch (K) {
1337 case OwnershipAttr::Takes:
1338 case OwnershipAttr::Holds:
1339 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001340 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1341 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001342 return;
1343 }
1344 break;
1345 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001346 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001347 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1348 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001349 return;
1350 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001351 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001352 }
1353
Richard Smith852e9ce2013-11-27 01:46:48 +00001354 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001355
1356 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001357 StringRef ModuleName = Module->getName();
1358 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1359 ModuleName.size() > 4) {
1360 ModuleName = ModuleName.drop_front(2).drop_back(2);
1361 Module = &S.PP.getIdentifierTable().get(ModuleName);
1362 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001363
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001364 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001365 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1366 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001367 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001368 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001369 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001370
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001371 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001372 QualType T = getFunctionOrMethodParamType(D, Idx);
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001373 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001374 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001375 case OwnershipAttr::Takes:
1376 case OwnershipAttr::Holds:
1377 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1378 Err = 0;
1379 break;
1380 case OwnershipAttr::Returns:
1381 if (!T->isIntegerType())
1382 Err = 1;
1383 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001384 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001385 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001386 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001387 << Ex->getSourceRange();
1388 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001389 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001390
1391 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001392 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001393 // Cannot have two ownership attributes of different kinds for the same
1394 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001395 if (I->getOwnKind() != K && I->args_end() !=
1396 std::find(I->args_begin(), I->args_end(), Idx)) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001397 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001398 << AL.getName() << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001399 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001400 } else if (K == OwnershipAttr::Returns &&
1401 I->getOwnKind() == OwnershipAttr::Returns) {
1402 // A returns attribute conflicts with any other returns attribute using
1403 // a different index. Note, diagnostic reporting is 1-based, but stored
1404 // argument indexes are 0-based.
1405 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1406 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
1407 << *(I->args_begin()) + 1;
1408 if (I->args_size())
1409 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
1410 << (unsigned)Idx + 1 << Ex->getSourceRange();
1411 return;
1412 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001413 }
1414 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001415 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001416 }
1417
1418 unsigned* start = OwnershipArgs.data();
1419 unsigned size = OwnershipArgs.size();
1420 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001421
Michael Han99315932013-01-24 16:46:58 +00001422 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001423 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001424 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001425}
1426
Chandler Carruthedc2c642011-07-02 00:01:44 +00001427static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001428 // Check the attribute arguments.
1429 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001430 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1431 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001432 return;
1433 }
1434
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001435 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001436
Rafael Espindolac18086a2010-02-23 22:00:30 +00001437 // gcc rejects
1438 // class c {
1439 // static int a __attribute__((weakref ("v2")));
1440 // static int b() __attribute__((weakref ("f3")));
1441 // };
1442 // and ignores the attributes of
1443 // void f(void) {
1444 // static int a __attribute__((weakref ("v2")));
1445 // }
1446 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001447 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001448 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001449 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1450 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001451 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001452 }
1453
1454 // The GCC manual says
1455 //
1456 // At present, a declaration to which `weakref' is attached can only
1457 // be `static'.
1458 //
1459 // It also says
1460 //
1461 // Without a TARGET,
1462 // given as an argument to `weakref' or to `alias', `weakref' is
1463 // equivalent to `weak'.
1464 //
1465 // gcc 4.4.1 will accept
1466 // int a7 __attribute__((weakref));
1467 // as
1468 // int a7 __attribute__((weak));
1469 // This looks like a bug in gcc. We reject that for now. We should revisit
1470 // it if this behaviour is actually used.
1471
Rafael Espindolac18086a2010-02-23 22:00:30 +00001472 // GCC rejects
1473 // static ((alias ("y"), weakref)).
1474 // Should we? How to check that weakref is before or after alias?
1475
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001476 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1477 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1478 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001479 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001480 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001481 // GCC will accept anything as the argument of weakref. Should we
1482 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001483 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1484 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001485
Michael Han99315932013-01-24 16:46:58 +00001486 D->addAttr(::new (S.Context)
1487 WeakRefAttr(Attr.getRange(), S.Context,
1488 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001489}
1490
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001491static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1492 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001493 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001494 return;
1495
Douglas Gregore8bbc122011-09-02 00:18:52 +00001496 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001497 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1498 return;
1499 }
1500
David Majnemer2dc81462015-01-19 09:00:28 +00001501 // Aliases should be on declarations, not definitions.
1502 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1503 if (FD->isThisDeclarationADefinition()) {
1504 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD;
1505 return;
1506 }
1507 } else {
1508 const auto *VD = cast<VarDecl>(D);
1509 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
1510 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << VD;
1511 return;
1512 }
1513 }
1514
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001515 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001516
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001517 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001518 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001519}
1520
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001521static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001522 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001523 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001524
Michael Han99315932013-01-24 16:46:58 +00001525 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1526 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001527}
1528
1529static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001530 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001531 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001532
Michael Han99315932013-01-24 16:46:58 +00001533 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1534 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001535}
1536
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001537static void handleTLSModelAttr(Sema &S, Decl *D,
1538 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001539 StringRef Model;
1540 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001541 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001542 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001543 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001544
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001545 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001546 if (Model != "global-dynamic" && Model != "local-dynamic"
1547 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001548 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001549 return;
1550 }
1551
Michael Han99315932013-01-24 16:46:58 +00001552 D->addAttr(::new (S.Context)
1553 TLSModelAttr(Attr.getRange(), S.Context, Model,
1554 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001555}
1556
David Majnemer631a90b2015-02-04 07:23:21 +00001557static void handleRestrictAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1558 QualType ResultType = getFunctionOrMethodResultType(D);
1559 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
1560 D->addAttr(::new (S.Context) RestrictAttr(
1561 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1562 return;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001563 }
1564
David Majnemer631a90b2015-02-04 07:23:21 +00001565 S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only)
1566 << Attr.getName() << getFunctionOrMethodResultSourceRange(D);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001567}
1568
Chandler Carruthedc2c642011-07-02 00:01:44 +00001569static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001570 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001571 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1572 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001573 return;
1574 }
1575
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001576 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1577 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001578}
1579
Chandler Carruthedc2c642011-07-02 00:01:44 +00001580static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001581 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001582
1583 if (S.CheckNoReturnAttr(attr)) return;
1584
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001585 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001586 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001587 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001588 return;
1589 }
1590
Michael Han99315932013-01-24 16:46:58 +00001591 D->addAttr(::new (S.Context)
1592 NoReturnAttr(attr.getRange(), S.Context,
1593 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001594}
1595
1596bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001597 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001598 attr.setInvalid();
1599 return true;
1600 }
1601
1602 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001603}
1604
Chandler Carruthedc2c642011-07-02 00:01:44 +00001605static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1606 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001607
1608 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1609 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00001610 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001611 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00001612 if (!VD || (!VD->getType()->isBlockPointerType() &&
1613 !VD->getType()->isFunctionPointerType())) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001614 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001615 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001616 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001617 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001618 return;
1619 }
1620 }
1621
Michael Han99315932013-01-24 16:46:58 +00001622 D->addAttr(::new (S.Context)
1623 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1624 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001625}
1626
John Thompsoncdb847ba2010-08-09 21:53:52 +00001627// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001628static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001629/*
1630 Returning a Vector Class in Registers
1631
Eric Christopherbc638a82010-12-01 22:13:54 +00001632 According to the PPU ABI specifications, a class with a single member of
1633 vector type is returned in memory when used as the return value of a function.
1634 This results in inefficient code when implementing vector classes. To return
1635 the value in a single vector register, add the vecreturn attribute to the
1636 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001637
1638 Example:
1639
1640 struct Vector
1641 {
1642 __vector float xyzw;
1643 } __attribute__((vecreturn));
1644
1645 Vector Add(Vector lhs, Vector rhs)
1646 {
1647 Vector result;
1648 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1649 return result; // This will be returned in a register
1650 }
1651*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001652 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1653 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001654 return;
1655 }
1656
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001657 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001658 int count = 0;
1659
1660 if (!isa<CXXRecordDecl>(record)) {
1661 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1662 return;
1663 }
1664
1665 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1666 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1667 return;
1668 }
1669
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001670 for (const auto *I : record->fields()) {
1671 if ((count == 1) || !I->getType()->isVectorType()) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001672 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1673 return;
1674 }
1675 count++;
1676 }
1677
Michael Han99315932013-01-24 16:46:58 +00001678 D->addAttr(::new (S.Context)
1679 VecReturnAttr(Attr.getRange(), S.Context,
1680 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001681}
1682
Richard Smithe233fbf2013-01-28 22:42:45 +00001683static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1684 const AttributeList &Attr) {
1685 if (isa<ParmVarDecl>(D)) {
1686 // [[carries_dependency]] can only be applied to a parameter if it is a
1687 // parameter of a function declaration or lambda.
1688 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1689 S.Diag(Attr.getLoc(),
1690 diag::err_carries_dependency_param_not_function_decl);
1691 return;
1692 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001693 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001694
1695 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1696 Attr.getRange(), S.Context,
1697 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001698}
1699
Chandler Carruthedc2c642011-07-02 00:01:44 +00001700static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001701 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001702 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001703 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001704 return;
1705 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001706 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001707 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001708 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001709 return;
1710 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001711
Michael Han99315932013-01-24 16:46:58 +00001712 D->addAttr(::new (S.Context)
1713 UsedAttr(Attr.getRange(), S.Context,
1714 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001715}
1716
Chandler Carruthedc2c642011-07-02 00:01:44 +00001717static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001718 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001719 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001720 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1721 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001722
Michael Han99315932013-01-24 16:46:58 +00001723 D->addAttr(::new (S.Context)
1724 ConstructorAttr(Attr.getRange(), S.Context, priority,
1725 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001726}
1727
Chandler Carruthedc2c642011-07-02 00:01:44 +00001728static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001729 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001730 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001731 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1732 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001733
Michael Han99315932013-01-24 16:46:58 +00001734 D->addAttr(::new (S.Context)
1735 DestructorAttr(Attr.getRange(), S.Context, priority,
1736 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001737}
1738
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001739template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001740static void handleAttrWithMessage(Sema &S, Decl *D,
1741 const AttributeList &Attr) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001742 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001743 StringRef Str;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001744 if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001745 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001746
Michael Han99315932013-01-24 16:46:58 +00001747 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1748 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001749}
1750
Ted Kremenek438f8db2014-02-22 01:06:05 +00001751static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Ted Kremenek28eace62013-11-23 01:01:34 +00001752 const AttributeList &Attr) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00001753 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Ted Kremenek27cfe102014-02-21 22:49:04 +00001754 S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition)
1755 << Attr.getName() << Attr.getRange();
1756 return;
1757 }
1758
Ted Kremenek28eace62013-11-23 01:01:34 +00001759 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001760 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1761 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001762}
1763
Jordy Rose740b0c22012-05-08 03:27:22 +00001764static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1765 IdentifierInfo *Platform,
1766 VersionTuple Introduced,
1767 VersionTuple Deprecated,
1768 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001769 StringRef PlatformName
1770 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1771 if (PlatformName.empty())
1772 PlatformName = Platform->getName();
1773
1774 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1775 // of these steps are needed).
1776 if (!Introduced.empty() && !Deprecated.empty() &&
1777 !(Introduced <= Deprecated)) {
1778 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1779 << 1 << PlatformName << Deprecated.getAsString()
1780 << 0 << Introduced.getAsString();
1781 return true;
1782 }
1783
1784 if (!Introduced.empty() && !Obsoleted.empty() &&
1785 !(Introduced <= Obsoleted)) {
1786 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1787 << 2 << PlatformName << Obsoleted.getAsString()
1788 << 0 << Introduced.getAsString();
1789 return true;
1790 }
1791
1792 if (!Deprecated.empty() && !Obsoleted.empty() &&
1793 !(Deprecated <= Obsoleted)) {
1794 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1795 << 2 << PlatformName << Obsoleted.getAsString()
1796 << 1 << Deprecated.getAsString();
1797 return true;
1798 }
1799
1800 return false;
1801}
1802
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001803/// \brief Check whether the two versions match.
1804///
1805/// If either version tuple is empty, then they are assumed to match. If
1806/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1807static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1808 bool BeforeIsOkay) {
1809 if (X.empty() || Y.empty())
1810 return true;
1811
1812 if (X == Y)
1813 return true;
1814
1815 if (BeforeIsOkay && X < Y)
1816 return true;
1817
1818 return false;
1819}
1820
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001821AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001822 IdentifierInfo *Platform,
1823 VersionTuple Introduced,
1824 VersionTuple Deprecated,
1825 VersionTuple Obsoleted,
1826 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001827 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001828 bool Override,
1829 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001830 VersionTuple MergedIntroduced = Introduced;
1831 VersionTuple MergedDeprecated = Deprecated;
1832 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001833 bool FoundAny = false;
1834
Rafael Espindolac67f2232012-05-10 02:50:16 +00001835 if (D->hasAttrs()) {
1836 AttrVec &Attrs = D->getAttrs();
1837 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1838 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1839 if (!OldAA) {
1840 ++i;
1841 continue;
1842 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001843
Rafael Espindolac67f2232012-05-10 02:50:16 +00001844 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1845 if (OldPlatform != Platform) {
1846 ++i;
1847 continue;
1848 }
1849
1850 FoundAny = true;
1851 VersionTuple OldIntroduced = OldAA->getIntroduced();
1852 VersionTuple OldDeprecated = OldAA->getDeprecated();
1853 VersionTuple OldObsoleted = OldAA->getObsoleted();
1854 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001855
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001856 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1857 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1858 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1859 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001860 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001861 if (Override) {
1862 int Which = -1;
1863 VersionTuple FirstVersion;
1864 VersionTuple SecondVersion;
1865 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1866 Which = 0;
1867 FirstVersion = OldIntroduced;
1868 SecondVersion = Introduced;
1869 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1870 Which = 1;
1871 FirstVersion = Deprecated;
1872 SecondVersion = OldDeprecated;
1873 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1874 Which = 2;
1875 FirstVersion = Obsoleted;
1876 SecondVersion = OldObsoleted;
1877 }
1878
1879 if (Which == -1) {
1880 Diag(OldAA->getLocation(),
1881 diag::warn_mismatched_availability_override_unavail)
1882 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1883 } else {
1884 Diag(OldAA->getLocation(),
1885 diag::warn_mismatched_availability_override)
1886 << Which
1887 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1888 << FirstVersion.getAsString() << SecondVersion.getAsString();
1889 }
1890 Diag(Range.getBegin(), diag::note_overridden_method);
1891 } else {
1892 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1893 Diag(Range.getBegin(), diag::note_previous_attribute);
1894 }
1895
Rafael Espindolac67f2232012-05-10 02:50:16 +00001896 Attrs.erase(Attrs.begin() + i);
1897 --e;
1898 continue;
1899 }
1900
1901 VersionTuple MergedIntroduced2 = MergedIntroduced;
1902 VersionTuple MergedDeprecated2 = MergedDeprecated;
1903 VersionTuple MergedObsoleted2 = MergedObsoleted;
1904
1905 if (MergedIntroduced2.empty())
1906 MergedIntroduced2 = OldIntroduced;
1907 if (MergedDeprecated2.empty())
1908 MergedDeprecated2 = OldDeprecated;
1909 if (MergedObsoleted2.empty())
1910 MergedObsoleted2 = OldObsoleted;
1911
1912 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1913 MergedIntroduced2, MergedDeprecated2,
1914 MergedObsoleted2)) {
1915 Attrs.erase(Attrs.begin() + i);
1916 --e;
1917 continue;
1918 }
1919
1920 MergedIntroduced = MergedIntroduced2;
1921 MergedDeprecated = MergedDeprecated2;
1922 MergedObsoleted = MergedObsoleted2;
1923 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001924 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001925 }
1926
1927 if (FoundAny &&
1928 MergedIntroduced == Introduced &&
1929 MergedDeprecated == Deprecated &&
1930 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00001931 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001932
Ted Kremenekb5445722013-04-06 00:34:27 +00001933 // Only create a new attribute if !Override, but we want to do
1934 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001935 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001936 MergedDeprecated, MergedObsoleted) &&
1937 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001938 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1939 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001940 Obsoleted, IsUnavailable, Message,
1941 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001942 }
Craig Topperc3ec1492014-05-26 06:22:03 +00001943 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001944}
1945
Chandler Carruthedc2c642011-07-02 00:01:44 +00001946static void handleAvailabilityAttr(Sema &S, Decl *D,
1947 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001948 if (!checkAttributeNumArgs(S, Attr, 1))
1949 return;
1950 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001951 unsigned Index = Attr.getAttributeSpellingListIndex();
1952
Aaron Ballman00e99962013-08-31 01:11:41 +00001953 IdentifierInfo *II = Platform->Ident;
1954 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1955 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1956 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001957
Rafael Espindolac231fab2013-01-08 21:30:32 +00001958 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1959 if (!ND) {
1960 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1961 return;
1962 }
1963
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001964 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1965 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1966 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001967 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001968 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001969 if (const StringLiteral *SE =
1970 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001971 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001972
Aaron Ballman00e99962013-08-31 01:11:41 +00001973 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001974 Introduced.Version,
1975 Deprecated.Version,
1976 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001977 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001978 /*Override=*/false,
1979 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001980 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001981 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001982}
1983
John McCalld041a9b2013-02-20 01:54:26 +00001984template <class T>
1985static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1986 typename T::VisibilityType value,
1987 unsigned attrSpellingListIndex) {
1988 T *existingAttr = D->getAttr<T>();
1989 if (existingAttr) {
1990 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1991 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00001992 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00001993 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1994 S.Diag(range.getBegin(), diag::note_previous_attribute);
1995 D->dropAttr<T>();
1996 }
1997 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1998}
1999
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002000VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002001 VisibilityAttr::VisibilityType Vis,
2002 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002003 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2004 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002005}
2006
John McCalld041a9b2013-02-20 01:54:26 +00002007TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2008 TypeVisibilityAttr::VisibilityType Vis,
2009 unsigned AttrSpellingListIndex) {
2010 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2011 AttrSpellingListIndex);
2012}
2013
2014static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2015 bool isTypeVisibility) {
2016 // Visibility attributes don't mean anything on a typedef.
2017 if (isa<TypedefNameDecl>(D)) {
2018 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2019 << Attr.getName();
2020 return;
2021 }
2022
2023 // 'type_visibility' can only go on a type or namespace.
2024 if (isTypeVisibility &&
2025 !(isa<TagDecl>(D) ||
2026 isa<ObjCInterfaceDecl>(D) ||
2027 isa<NamespaceDecl>(D))) {
2028 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2029 << Attr.getName() << ExpectedTypeOrNamespace;
2030 return;
2031 }
2032
Benjamin Kramer70370212013-09-09 15:08:57 +00002033 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002034 StringRef TypeStr;
2035 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002036 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002037 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002038
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002039 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002040 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002041 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002042 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002043 return;
2044 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002045
2046 // Complain about attempts to use protected visibility on targets
2047 // (like Darwin) that don't support it.
2048 if (type == VisibilityAttr::Protected &&
2049 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2050 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2051 type = VisibilityAttr::Default;
2052 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002053
Michael Han99315932013-01-24 16:46:58 +00002054 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002055 clang::Attr *newAttr;
2056 if (isTypeVisibility) {
2057 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2058 (TypeVisibilityAttr::VisibilityType) type,
2059 Index);
2060 } else {
2061 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2062 }
2063 if (newAttr)
2064 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002065}
2066
Chandler Carruthedc2c642011-07-02 00:01:44 +00002067static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2068 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002069 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002070 if (!Attr.isArgIdent(0)) {
2071 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2072 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002073 return;
2074 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002075
Aaron Ballman682ee422013-09-11 19:47:58 +00002076 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2077 ObjCMethodFamilyAttr::FamilyKind F;
2078 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2079 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2080 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002081 return;
2082 }
2083
Alp Toker314cc812014-01-25 16:55:45 +00002084 if (F == ObjCMethodFamilyAttr::OMF_init &&
2085 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00002086 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00002087 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002088 // Ignore the attribute.
2089 return;
2090 }
2091
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002092 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00002093 S.Context, F,
2094 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002095}
2096
Chandler Carruthedc2c642011-07-02 00:01:44 +00002097static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002098 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002099 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002100 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002101 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2102 return;
2103 }
2104 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002105 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2106 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002107 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002108 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2109 return;
2110 }
2111 }
2112 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002113 // It is okay to include this attribute on properties, e.g.:
2114 //
2115 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2116 //
2117 // In this case it follows tradition and suppresses an error in the above
2118 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002119 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002120 }
Michael Han99315932013-01-24 16:46:58 +00002121 D->addAttr(::new (S.Context)
2122 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2123 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002124}
2125
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002126static void handleObjCIndependentClass(Sema &S, Decl *D, const AttributeList &Attr) {
2127 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
2128 QualType T = TD->getUnderlyingType();
2129 if (!T->isObjCObjectPointerType()) {
2130 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2131 return;
2132 }
2133 } else {
2134 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2135 return;
2136 }
2137 D->addAttr(::new (S.Context)
2138 ObjCIndependentClassAttr(Attr.getRange(), S.Context,
2139 Attr.getAttributeSpellingListIndex()));
2140}
2141
Chandler Carruthedc2c642011-07-02 00:01:44 +00002142static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002143 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002144 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002145 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002146 return;
2147 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002148
Aaron Ballman00e99962013-08-31 01:11:41 +00002149 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002150 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002151 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2152 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2153 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002154 return;
2155 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002156
Michael Han99315932013-01-24 16:46:58 +00002157 D->addAttr(::new (S.Context)
2158 BlocksAttr(Attr.getRange(), S.Context, type,
2159 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002160}
2161
Chandler Carruthedc2c642011-07-02 00:01:44 +00002162static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002163 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002164 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002165 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002166 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002167 if (E->isTypeDependent() || E->isValueDependent() ||
2168 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002169 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002170 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002171 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002172 return;
2173 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002174
John McCallb46f2872011-09-09 07:56:05 +00002175 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002176 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2177 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002178 return;
2179 }
John McCallb46f2872011-09-09 07:56:05 +00002180
2181 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002182 }
2183
Aaron Ballman18a78382013-11-21 00:28:23 +00002184 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002185 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002186 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002187 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002188 if (E->isTypeDependent() || E->isValueDependent() ||
2189 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002190 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002191 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002192 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002193 return;
2194 }
2195 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002196
John McCallb46f2872011-09-09 07:56:05 +00002197 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002198 // FIXME: This error message could be improved, it would be nice
2199 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002200 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2201 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002202 return;
2203 }
2204 }
2205
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002206 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002207 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002208 if (isa<FunctionNoProtoType>(FT)) {
2209 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2210 return;
2211 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002212
Chris Lattner9363e312009-03-17 23:03:47 +00002213 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002214 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002215 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002216 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002217 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002218 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002219 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002220 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002221 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002222 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2223 if (!BD->isVariadic()) {
2224 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2225 return;
2226 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002227 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002228 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002229 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002230 const FunctionType *FT = Ty->isFunctionPointerType()
2231 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002232 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002233 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002234 int m = Ty->isFunctionPointerType() ? 0 : 1;
2235 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002236 return;
2237 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002238 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002239 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002240 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002241 return;
2242 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002243 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002244 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002245 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002246 return;
2247 }
Michael Han99315932013-01-24 16:46:58 +00002248 D->addAttr(::new (S.Context)
2249 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2250 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002251}
2252
Chandler Carruthedc2c642011-07-02 00:01:44 +00002253static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002254 if (D->getFunctionType() &&
2255 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002256 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2257 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002258 return;
2259 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002260 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002261 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002262 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2263 << Attr.getName() << 1;
2264 return;
2265 }
2266
Michael Han99315932013-01-24 16:46:58 +00002267 D->addAttr(::new (S.Context)
2268 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2269 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002270}
2271
Chandler Carruthedc2c642011-07-02 00:01:44 +00002272static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002273 // weak_import only applies to variable & function declarations.
2274 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002275 if (!D->canBeWeakImported(isDef)) {
2276 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002277 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2278 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002279 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002280 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002281 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002282 // Nothing to warn about here.
2283 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002284 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002285 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002286
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002287 return;
2288 }
2289
Michael Han99315932013-01-24 16:46:58 +00002290 D->addAttr(::new (S.Context)
2291 WeakImportAttr(Attr.getRange(), S.Context,
2292 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002293}
2294
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002295// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002296template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002297static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002298 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002299 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002300 for (unsigned i = 0; i < 3; ++i) {
2301 const Expr *E = Attr.getArgAsExpr(i);
2302 if (!checkUInt32Argument(S, Attr, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002303 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002304 if (WGSize[i] == 0) {
2305 S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero)
2306 << Attr.getName() << E->getSourceRange();
2307 return;
2308 }
2309 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002310
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002311 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2312 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2313 Existing->getYDim() == WGSize[1] &&
2314 Existing->getZDim() == WGSize[2]))
2315 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002316
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002317 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2318 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002319 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002320}
2321
Joey Goulyaba589c2013-03-08 09:42:32 +00002322static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002323 if (!Attr.hasParsedType()) {
2324 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2325 << Attr.getName() << 1;
2326 return;
2327 }
2328
Craig Topperc3ec1492014-05-26 06:22:03 +00002329 TypeSourceInfo *ParmTSI = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00002330 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2331 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002332
2333 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2334 (ParmType->isBooleanType() ||
2335 !ParmType->isIntegralType(S.getASTContext()))) {
2336 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2337 << ParmType;
2338 return;
2339 }
2340
Aaron Ballmana9e05402013-12-02 22:16:55 +00002341 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002342 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002343 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2344 return;
2345 }
2346 }
2347
2348 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002349 ParmTSI,
2350 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002351}
2352
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002353SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002354 StringRef Name,
2355 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002356 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2357 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002358 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002359 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2360 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002361 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002362 }
Michael Han99315932013-01-24 16:46:58 +00002363 return ::new (Context) SectionAttr(Range, Context, Name,
2364 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002365}
2366
Reid Kleckner2a133222015-03-04 23:39:17 +00002367bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2368 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2369 if (!Error.empty()) {
2370 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error;
2371 return false;
2372 }
2373 return true;
2374}
2375
Chandler Carruthedc2c642011-07-02 00:01:44 +00002376static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002377 // Make sure that there is a string literal as the sections's single
2378 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002379 StringRef Str;
2380 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002381 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002382 return;
Mike Stump11289f42009-09-09 15:08:12 +00002383
Reid Kleckner2a133222015-03-04 23:39:17 +00002384 if (!S.checkSectionName(LiteralLoc, Str))
2385 return;
2386
Chris Lattner30ba6742009-08-10 19:03:04 +00002387 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002388 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002389 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002390 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002391 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002392 return;
2393 }
Mike Stump11289f42009-09-09 15:08:12 +00002394
Michael Han99315932013-01-24 16:46:58 +00002395 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002396 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002397 if (NewAttr)
2398 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002399}
2400
Eric Christopher789a7ad2015-06-12 01:36:05 +00002401// Check for things we'd like to warn about, no errors or validation for now.
2402// TODO: Validation should use a backend target library that specifies
2403// the allowable subtarget features and cpus. We could use something like a
2404// TargetCodeGenInfo hook here to do validation.
2405void Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
2406 for (auto Str : {"tune=", "fpmath="})
2407 if (AttrStr.find(Str) != StringRef::npos)
2408 Diag(LiteralLoc, diag::warn_unsupported_target_attribute) << Str;
2409}
2410
Eric Christopher11acf732015-06-12 01:35:52 +00002411static void handleTargetAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eric Christopher11acf732015-06-12 01:35:52 +00002412 StringRef Str;
2413 SourceLocation LiteralLoc;
2414 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
2415 return;
Eric Christopher789a7ad2015-06-12 01:36:05 +00002416 S.checkTargetAttr(LiteralLoc, Str);
Eric Christopher11acf732015-06-12 01:35:52 +00002417 unsigned Index = Attr.getAttributeSpellingListIndex();
2418 TargetAttr *NewAttr =
2419 ::new (S.Context) TargetAttr(Attr.getRange(), S.Context, Str, Index);
2420 D->addAttr(NewAttr);
2421}
2422
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002423
Chandler Carruthedc2c642011-07-02 00:01:44 +00002424static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002425 VarDecl *VD = cast<VarDecl>(D);
2426 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002427 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002428 return;
2429 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002430
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002431 Expr *E = Attr.getArgAsExpr(0);
2432 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00002433 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002434 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002435
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002436 // gcc only allows for simple identifiers. Since we support more than gcc, we
2437 // will warn the user.
2438 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2439 if (DRE->hasQualifier())
2440 S.Diag(Loc, diag::warn_cleanup_ext);
2441 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2442 NI = DRE->getNameInfo();
2443 if (!FD) {
2444 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2445 << NI.getName();
2446 return;
2447 }
2448 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2449 if (ULE->hasExplicitTemplateArgs())
2450 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002451 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2452 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002453 if (!FD) {
2454 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2455 << NI.getName();
2456 if (ULE->getType() == S.Context.OverloadTy)
2457 S.NoteAllOverloadCandidates(ULE);
2458 return;
2459 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002460 } else {
2461 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002462 return;
2463 }
2464
Anders Carlssond277d792009-01-31 01:16:18 +00002465 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002466 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2467 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002468 return;
2469 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002470
Anders Carlsson723f55d2009-02-07 23:16:50 +00002471 // We're currently more strict than GCC about what function types we accept.
2472 // If this ever proves to be a problem it should be easy to fix.
2473 QualType Ty = S.Context.getPointerType(VD->getType());
2474 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002475 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2476 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002477 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2478 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002479 return;
2480 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002481
Michael Han99315932013-01-24 16:46:58 +00002482 D->addAttr(::new (S.Context)
2483 CleanupAttr(Attr.getRange(), S.Context, FD,
2484 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002485}
2486
Mike Stumpd3bb5572009-07-24 19:02:52 +00002487/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002488/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002489static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002490 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002491 uint64_t Idx;
2492 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002493 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002494
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002495 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002496 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002497
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002498 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2499 if (not_nsstring_type &&
2500 !isCFStringType(Ty, S.Context) &&
2501 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002502 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002503 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00002504 << (not_nsstring_type ? "a string type" : "an NSString")
2505 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002506 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002507 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002508 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002509 if (!isNSStringType(Ty, S.Context) &&
2510 !isCFStringType(Ty, S.Context) &&
2511 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002512 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002513 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00002514 << (not_nsstring_type ? "string type" : "NSString")
2515 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002516 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002517 }
2518
Alp Toker601b22c2014-01-21 23:35:24 +00002519 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002520 // because that has corrected for the implicit this parameter, and is zero-
2521 // based. The attribute expects what the user wrote explicitly.
2522 llvm::APSInt Val;
2523 IdxExpr->EvaluateAsInt(Val, S.Context);
2524
Michael Han99315932013-01-24 16:46:58 +00002525 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002526 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002527 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002528}
2529
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002530enum FormatAttrKind {
2531 CFStringFormat,
2532 NSStringFormat,
2533 StrftimeFormat,
2534 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002535 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002536 InvalidFormat
2537};
2538
2539/// getFormatAttrKind - Map from format attribute names to supported format
2540/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002541static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002542 return llvm::StringSwitch<FormatAttrKind>(Format)
2543 // Check for formats that get handled specially.
2544 .Case("NSString", NSStringFormat)
2545 .Case("CFString", CFStringFormat)
2546 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002547
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002548 // Otherwise, check for supported formats.
2549 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2550 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2551 .Case("kprintf", SupportedFormat) // OpenBSD.
Dimitry Andric6b5ed342015-02-19 22:32:33 +00002552 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
Fariborz Jahanianf8dce0f2015-02-21 00:45:58 +00002553 .Case("os_trace", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002554
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002555 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2556 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002557}
2558
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002559/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002560/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002561static void handleInitPriorityAttr(Sema &S, Decl *D,
2562 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002563 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002564 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2565 return;
2566 }
2567
Aaron Ballman4a611152013-11-27 16:34:09 +00002568 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002569 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2570 Attr.setInvalid();
2571 return;
2572 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002573 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002574 if (S.Context.getAsArrayType(T))
2575 T = S.Context.getBaseElementType(T);
2576 if (!T->getAs<RecordType>()) {
2577 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2578 Attr.setInvalid();
2579 return;
2580 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002581
2582 Expr *E = Attr.getArgAsExpr(0);
2583 uint32_t prioritynum;
2584 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002585 Attr.setInvalid();
2586 return;
2587 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002588
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002589 if (prioritynum < 101 || prioritynum > 65535) {
2590 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002591 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002592 Attr.setInvalid();
2593 return;
2594 }
Michael Han99315932013-01-24 16:46:58 +00002595 D->addAttr(::new (S.Context)
2596 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2597 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002598}
2599
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002600FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2601 IdentifierInfo *Format, int FormatIdx,
2602 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002603 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002604 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002605 for (auto *F : D->specific_attrs<FormatAttr>()) {
2606 if (F->getType() == Format &&
2607 F->getFormatIdx() == FormatIdx &&
2608 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002609 // If we don't have a valid location for this attribute, adopt the
2610 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002611 if (F->getLocation().isInvalid())
2612 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00002613 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00002614 }
2615 }
2616
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002617 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2618 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002619}
2620
Mike Stumpd3bb5572009-07-24 19:02:52 +00002621/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002622/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002623static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002624 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002625 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002626 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002627 return;
2628 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002629
Chandler Carruth743682b2010-11-16 08:35:43 +00002630 // In C++ the implicit 'this' function parameter also counts, and they are
2631 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002632 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002633 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002634
Aaron Ballman00e99962013-08-31 01:11:41 +00002635 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2636 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002637
2638 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002639 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002640 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002641 // If we've modified the string name, we need a new identifier for it.
2642 II = &S.Context.Idents.get(Format);
2643 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002644
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002645 // Check for supported formats.
2646 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002647
2648 if (Kind == IgnoredFormat)
2649 return;
2650
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002651 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002652 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002653 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002654 return;
2655 }
2656
2657 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002658 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002659 uint32_t Idx;
2660 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002661 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002662
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002663 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002664 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002665 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002666 return;
2667 }
2668
2669 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002670 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002671
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002672 if (HasImplicitThisParam) {
2673 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002674 S.Diag(Attr.getLoc(),
2675 diag::err_format_attribute_implicit_this_format_string)
2676 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002677 return;
2678 }
2679 ArgIdx--;
2680 }
Mike Stump11289f42009-09-09 15:08:12 +00002681
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002682 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002683 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002684
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002685 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002686 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002687 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002688 << "a CFString" << IdxExpr->getSourceRange()
2689 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00002690 return;
2691 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002692 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002693 // FIXME: do we need to check if the type is NSString*? What are the
2694 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002695 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002696 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002697 << "an NSString" << IdxExpr->getSourceRange()
2698 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002699 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002700 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002701 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002702 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002703 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002704 << "a string type" << IdxExpr->getSourceRange()
2705 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002706 return;
2707 }
2708
2709 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002710 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002711 uint32_t FirstArg;
2712 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002713 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002714
2715 // check if the function is variadic if the 3rd argument non-zero
2716 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002717 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002718 ++NumArgs; // +1 for ...
2719 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002720 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002721 return;
2722 }
2723 }
2724
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002725 // strftime requires FirstArg to be 0 because it doesn't read from any
2726 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002727 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002728 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002729 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2730 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002731 return;
2732 }
2733 // if 0 it disables parameter checking (to use with e.g. va_list)
2734 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002735 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002736 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002737 return;
2738 }
2739
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002740 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002741 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002742 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002743 if (NewAttr)
2744 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002745}
2746
Chandler Carruthedc2c642011-07-02 00:01:44 +00002747static void handleTransparentUnionAttr(Sema &S, Decl *D,
2748 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002749 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00002750 RecordDecl *RD = nullptr;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002751 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002752 if (TD && TD->getUnderlyingType()->isUnionType())
2753 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2754 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002755 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002756
2757 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002758 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002759 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002760 return;
2761 }
2762
John McCallf937c022011-10-07 06:10:15 +00002763 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002764 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002765 diag::warn_transparent_union_attribute_not_definition);
2766 return;
2767 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002768
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002769 RecordDecl::field_iterator Field = RD->field_begin(),
2770 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002771 if (Field == FieldEnd) {
2772 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2773 return;
2774 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002775
David Blaikie40ed2972012-06-06 20:45:41 +00002776 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002777 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002778 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002779 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002780 diag::warn_transparent_union_attribute_floating)
2781 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002782 return;
2783 }
2784
2785 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2786 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2787 for (; Field != FieldEnd; ++Field) {
2788 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002789 // FIXME: this isn't fully correct; we also need to test whether the
2790 // members of the union would all have the same calling convention as the
2791 // first member of the union. Checking just the size and alignment isn't
2792 // sufficient (consider structs passed on the stack instead of in registers
2793 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002794 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002795 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002796 // Warn if we drop the attribute.
2797 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002798 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002799 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002800 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002801 diag::warn_transparent_union_attribute_field_size_align)
2802 << isSize << Field->getDeclName() << FieldBits;
2803 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002804 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002805 diag::note_transparent_union_first_field_size_align)
2806 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002807 return;
2808 }
2809 }
2810
Michael Han99315932013-01-24 16:46:58 +00002811 RD->addAttr(::new (S.Context)
2812 TransparentUnionAttr(Attr.getRange(), S.Context,
2813 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002814}
2815
Chandler Carruthedc2c642011-07-02 00:01:44 +00002816static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002817 // Make sure that there is a string literal as the annotation's single
2818 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002819 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002820 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002821 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002822
2823 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002824 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
2825 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002826 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002827 }
Michael Han99315932013-01-24 16:46:58 +00002828
2829 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002830 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002831 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002832}
2833
Hal Finkel1b0d24e2014-10-02 21:21:25 +00002834static void handleAlignValueAttr(Sema &S, Decl *D,
2835 const AttributeList &Attr) {
2836 S.AddAlignValueAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
2837 Attr.getAttributeSpellingListIndex());
2838}
2839
2840void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
2841 unsigned SpellingListIndex) {
2842 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
2843 SourceLocation AttrLoc = AttrRange.getBegin();
2844
2845 QualType T;
2846 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
2847 T = TD->getUnderlyingType();
2848 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2849 T = VD->getType();
2850 else
2851 llvm_unreachable("Unknown decl type for align_value");
2852
2853 if (!T->isDependentType() && !T->isAnyPointerType() &&
2854 !T->isReferenceType() && !T->isMemberPointerType()) {
2855 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
2856 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
2857 return;
2858 }
2859
2860 if (!E->isValueDependent()) {
2861 llvm::APSInt Alignment(32);
2862 ExprResult ICE
2863 = VerifyIntegerConstantExpression(E, &Alignment,
2864 diag::err_align_value_attribute_argument_not_int,
2865 /*AllowFold*/ false);
2866 if (ICE.isInvalid())
2867 return;
2868
2869 if (!Alignment.isPowerOf2()) {
2870 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
2871 << E->getSourceRange();
2872 return;
2873 }
2874
2875 D->addAttr(::new (Context)
2876 AlignValueAttr(AttrRange, Context, ICE.get(),
2877 SpellingListIndex));
2878 return;
2879 }
2880
2881 // Save dependent expressions in the AST to be instantiated.
2882 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
2883 return;
2884}
2885
Chandler Carruthedc2c642011-07-02 00:01:44 +00002886static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002887 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002888 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002889 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2890 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002891 return;
2892 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002893
Richard Smith848e1f12013-02-01 08:12:08 +00002894 if (Attr.getNumArgs() == 0) {
2895 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
Craig Topperc3ec1492014-05-26 06:22:03 +00002896 true, nullptr, Attr.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00002897 return;
2898 }
2899
Aaron Ballman00e99962013-08-31 01:11:41 +00002900 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002901 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2902 S.Diag(Attr.getEllipsisLoc(),
2903 diag::err_pack_expansion_without_parameter_packs);
2904 return;
2905 }
2906
2907 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2908 return;
2909
David Majnemer26a1e0e2015-04-07 02:37:09 +00002910 if (E->isValueDependent()) {
2911 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
2912 if (!TND->getUnderlyingType()->isDependentType()) {
2913 S.Diag(Attr.getLoc(), diag::err_alignment_dependent_typedef_name)
2914 << E->getSourceRange();
2915 return;
2916 }
2917 }
2918 }
2919
Richard Smith44c247f2013-02-22 08:32:16 +00002920 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2921 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002922}
2923
2924void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002925 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002926 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2927 SourceLocation AttrLoc = AttrRange.getBegin();
2928
Richard Smith1dba27c2013-01-29 09:02:09 +00002929 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002930 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002931 // C++11 [dcl.align]p1:
2932 // An alignment-specifier may be applied to a variable or to a class
2933 // data member, but it shall not be applied to a bit-field, a function
2934 // parameter, the formal parameter of a catch clause, or a variable
2935 // declared with the register storage class specifier. An
2936 // alignment-specifier may also be applied to the declaration of a class
2937 // or enumeration type.
2938 // C11 6.7.5/2:
2939 // An alignment attribute shall not be specified in a declaration of
2940 // a typedef, or a bit-field, or a function, or a parameter, or an
2941 // object declared with the register storage-class specifier.
2942 int DiagKind = -1;
2943 if (isa<ParmVarDecl>(D)) {
2944 DiagKind = 0;
2945 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2946 if (VD->getStorageClass() == SC_Register)
2947 DiagKind = 1;
2948 if (VD->isExceptionVariable())
2949 DiagKind = 2;
2950 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2951 if (FD->isBitField())
2952 DiagKind = 3;
2953 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00002954 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00002955 << (TmpAttr.isC11() ? ExpectedVariableOrField
2956 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002957 return;
2958 }
2959 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002960 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00002961 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002962 return;
2963 }
2964 }
2965
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002966 if (E->isTypeDependent() || E->isValueDependent()) {
2967 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002968 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2969 AA->setPackExpansion(IsPackExpansion);
2970 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002971 return;
2972 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002973
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002974 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002975 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002976 ExprResult ICE
2977 = VerifyIntegerConstantExpression(E, &Alignment,
2978 diag::err_aligned_attribute_argument_not_int,
2979 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002980 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002981 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002982
2983 // C++11 [dcl.align]p2:
2984 // -- if the constant expression evaluates to zero, the alignment
2985 // specifier shall have no effect
2986 // C11 6.7.5p6:
2987 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00002988 if (!(TmpAttr.isAlignas() && !Alignment)) {
2989 if(!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
2990 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
2991 << E->getSourceRange();
2992 return;
2993 }
2994 if (Context.getTargetInfo().isTLSSupported()) {
2995 if (unsigned MaxAlign = Context.getTargetInfo().getMaxTLSAlign()) {
2996 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2997 if (VD->getTLSKind()) {
2998 CharUnits MaxAlignChars = Context.toCharUnitsFromBits(MaxAlign);
2999 if (Alignment.getSExtValue() > MaxAlignChars.getQuantity()) {
3000 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3001 << (unsigned)Alignment.getZExtValue() << VD
3002 << (unsigned)MaxAlignChars.getQuantity();
3003 return;
3004 }
3005 }
3006 }
3007 }
3008 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003009 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003010
David Majnemerabecae72014-02-12 20:36:10 +00003011 // Alignment calculations can wrap around if it's greater than 2**28.
3012 unsigned MaxValidAlignment = TmpAttr.isDeclspec() ? 8192 : 268435456;
3013 if (Alignment.getZExtValue() > MaxValidAlignment) {
3014 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3015 << E->getSourceRange();
3016 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003017 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003018
Richard Smith44c247f2013-02-22 08:32:16 +00003019 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003020 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00003021 AA->setPackExpansion(IsPackExpansion);
3022 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003023}
3024
Michael Hanaf02bbe2013-02-01 01:19:17 +00003025void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003026 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003027 // FIXME: Cache the number on the Attr object if non-dependent?
3028 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003029 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3030 SpellingListIndex);
3031 AA->setPackExpansion(IsPackExpansion);
3032 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003033}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003034
Richard Smith848e1f12013-02-01 08:12:08 +00003035void Sema::CheckAlignasUnderalignment(Decl *D) {
3036 assert(D->hasAttrs() && "no attributes on decl");
3037
David Majnemer475b25e2015-01-21 10:54:38 +00003038 QualType UnderlyingTy, DiagTy;
3039 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
3040 UnderlyingTy = DiagTy = VD->getType();
3041 } else {
3042 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
3043 if (EnumDecl *ED = dyn_cast<EnumDecl>(D))
3044 UnderlyingTy = ED->getIntegerType();
3045 }
3046 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003047 return;
3048
3049 // C++11 [dcl.align]p5, C11 6.7.5/4:
3050 // The combined effect of all alignment attributes in a declaration shall
3051 // not specify an alignment that is less strict than the alignment that
3052 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003053 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003054 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003055 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003056 if (I->isAlignmentDependent())
3057 return;
3058 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003059 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003060 Align = std::max(Align, I->getAlignment(Context));
3061 }
3062
3063 if (AlignasAttr && Align) {
3064 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003065 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003066 if (NaturalAlign > RequestedAlign)
3067 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003068 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003069 }
3070}
3071
David Majnemer2c4e00a2014-01-29 22:07:36 +00003072bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003073 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003074 MSInheritanceAttr::Spelling SemanticSpelling) {
3075 assert(RD->hasDefinition() && "RD has no definition!");
3076
David Majnemer98c9ee22014-02-07 00:43:07 +00003077 // We may not have seen base specifiers or any virtual methods yet. We will
3078 // have to wait until the record is defined to catch any mismatches.
3079 if (!RD->getDefinition()->isCompleteDefinition())
3080 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003081
David Majnemer98c9ee22014-02-07 00:43:07 +00003082 // The unspecified model never matches what a definition could need.
3083 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3084 return false;
3085
David Majnemer4bb09802014-02-10 19:50:15 +00003086 if (BestCase) {
3087 if (RD->calculateInheritanceModel() == SemanticSpelling)
3088 return false;
3089 } else {
3090 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3091 return false;
3092 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003093
3094 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3095 << 0 /*definition*/;
3096 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3097 << RD->getNameAsString();
3098 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003099}
3100
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003101/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003102/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003103///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003104/// Despite what would be logical, the mode attribute is a decl attribute, not a
3105/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3106/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003107static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003108 // This attribute isn't documented, but glibc uses it. It changes
3109 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00003110 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00003111 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3112 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003113 return;
3114 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003115
Aaron Ballman00e99962013-08-31 01:11:41 +00003116 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3117 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003118
3119 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003120 if (Str.startswith("__") && Str.endswith("__"))
3121 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003122
3123 unsigned DestWidth = 0;
3124 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003125 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003126 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003127 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003128 switch (Str[0]) {
3129 case 'Q': DestWidth = 8; break;
3130 case 'H': DestWidth = 16; break;
3131 case 'S': DestWidth = 32; break;
3132 case 'D': DestWidth = 64; break;
3133 case 'X': DestWidth = 96; break;
3134 case 'T': DestWidth = 128; break;
3135 }
3136 if (Str[1] == 'F') {
3137 IntegerMode = false;
3138 } else if (Str[1] == 'C') {
3139 IntegerMode = false;
3140 ComplexMode = true;
3141 } else if (Str[1] != 'I') {
3142 DestWidth = 0;
3143 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003144 break;
3145 case 4:
3146 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3147 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003148 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003149 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003150 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003151 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003152 break;
3153 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003154 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003155 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003156 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003157 case 11:
3158 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003159 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003160 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003161 }
3162
3163 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003164 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003165 OldTy = TD->getUnderlyingType();
3166 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3167 OldTy = VD->getType();
3168 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003169 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00003170 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003171 return;
3172 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003173
Alexey Bataev326057d2015-06-19 07:46:21 +00003174 // Base type can also be a vector type (see PR17453).
3175 // Distinguish between base type and base element type.
3176 QualType OldElemTy = OldTy;
3177 if (const VectorType *VT = OldTy->getAs<VectorType>())
3178 OldElemTy = VT->getElementType();
3179
3180 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003181 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3182 else if (IntegerMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003183 if (!OldElemTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003184 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3185 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003186 if (!OldElemTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003187 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3188 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00003189 if (!OldElemTy->isFloatingType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003190 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3191 }
3192
Mike Stump87c57ac2009-05-16 07:39:55 +00003193 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3194 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003195 // FIXME: Make sure floating-point mappings are accurate
3196 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003197 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00003198 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003199 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003200 }
3201
Alexey Bataev326057d2015-06-19 07:46:21 +00003202 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003203
3204 if (IntegerMode)
Alexey Bataev326057d2015-06-19 07:46:21 +00003205 NewElemTy = S.Context.getIntTypeForBitwidth(
3206 DestWidth, OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003207 else
Alexey Bataev326057d2015-06-19 07:46:21 +00003208 NewElemTy = S.Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003209
Alexey Bataev326057d2015-06-19 07:46:21 +00003210 if (NewElemTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00003211 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003212 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003213 }
3214
Eli Friedman4735374e2009-03-03 06:41:03 +00003215 if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003216 NewElemTy = S.Context.getComplexType(NewElemTy);
3217 }
3218
3219 QualType NewTy = NewElemTy;
3220 if (const VectorType *OldVT = OldTy->getAs<VectorType>()) {
3221 // Complex machine mode does not support base vector types.
3222 if (ComplexMode) {
3223 S.Diag(Attr.getLoc(), diag::err_complex_mode_vector_type);
3224 return;
3225 }
3226 unsigned NumElements = S.Context.getTypeSize(OldElemTy) *
3227 OldVT->getNumElements() /
3228 S.Context.getTypeSize(NewElemTy);
3229 NewTy =
3230 S.Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
3231 }
3232
3233 if (NewTy.isNull()) {
3234 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3235 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003236 }
3237
3238 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003239 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3240 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3241 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003242 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003243
3244 D->addAttr(::new (S.Context)
3245 ModeAttr(Attr.getRange(), S.Context, Name,
3246 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003247}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003248
Chandler Carruthedc2c642011-07-02 00:01:44 +00003249static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003250 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3251 if (!VD->hasGlobalStorage())
3252 S.Diag(Attr.getLoc(),
3253 diag::warn_attribute_requires_functions_or_static_globals)
3254 << Attr.getName();
3255 } else if (!isFunctionOrMethod(D)) {
3256 S.Diag(Attr.getLoc(),
3257 diag::warn_attribute_requires_functions_or_static_globals)
3258 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003259 return;
3260 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003261
Michael Han99315932013-01-24 16:46:58 +00003262 D->addAttr(::new (S.Context)
3263 NoDebugAttr(Attr.getRange(), S.Context,
3264 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003265}
3266
Paul Robinson30e41fb2014-12-15 18:57:28 +00003267AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00003268 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00003269 unsigned AttrSpellingListIndex) {
3270 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003271 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00003272 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3273 return nullptr;
3274 }
3275
3276 if (D->hasAttr<AlwaysInlineAttr>())
3277 return nullptr;
3278
3279 return ::new (Context) AlwaysInlineAttr(Range, Context,
3280 AttrSpellingListIndex);
3281}
3282
3283MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
3284 unsigned AttrSpellingListIndex) {
3285 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
3286 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
3287 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3288 return nullptr;
3289 }
3290
3291 if (D->hasAttr<MinSizeAttr>())
3292 return nullptr;
3293
3294 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
3295}
3296
3297OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
3298 unsigned AttrSpellingListIndex) {
3299 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
3300 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
3301 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3302 D->dropAttr<AlwaysInlineAttr>();
3303 }
3304 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
3305 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
3306 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3307 D->dropAttr<MinSizeAttr>();
3308 }
3309
3310 if (D->hasAttr<OptimizeNoneAttr>())
3311 return nullptr;
3312
3313 return ::new (Context) OptimizeNoneAttr(Range, Context,
3314 AttrSpellingListIndex);
3315}
3316
Paul Robinsonf0674352014-03-31 22:29:15 +00003317static void handleAlwaysInlineAttr(Sema &S, Decl *D,
3318 const AttributeList &Attr) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003319 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
3320 D, Attr.getRange(), Attr.getName(),
3321 Attr.getAttributeSpellingListIndex()))
3322 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00003323}
3324
Paul Robinson080b1f32015-01-13 18:34:56 +00003325static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3326 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
3327 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3328 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00003329}
3330
Paul Robinsonf0674352014-03-31 22:29:15 +00003331static void handleOptimizeNoneAttr(Sema &S, Decl *D,
3332 const AttributeList &Attr) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003333 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
3334 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3335 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00003336}
3337
Chandler Carruthedc2c642011-07-02 00:01:44 +00003338static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003339 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003340 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00003341 SourceRange RTRange = FD->getReturnTypeSourceRange();
3342 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00003343 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00003344 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
3345 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00003346 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003347 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003348
Aaron Ballman3aff6332013-12-02 19:30:36 +00003349 D->addAttr(::new (S.Context)
3350 CUDAGlobalAttr(Attr.getRange(), S.Context,
Eli Bendersky83860042014-09-02 22:00:06 +00003351 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003352}
3353
Chandler Carruthedc2c642011-07-02 00:01:44 +00003354static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003355 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003356 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003357 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003358 return;
3359 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003360
Michael Han99315932013-01-24 16:46:58 +00003361 D->addAttr(::new (S.Context)
3362 GNUInlineAttr(Attr.getRange(), S.Context,
3363 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003364}
3365
Chandler Carruthedc2c642011-07-02 00:01:44 +00003366static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003367 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003368
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003369 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003370 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3371 CallingConv CC;
Richard Smitheec7cb12015-05-28 23:38:53 +00003372 if (S.CheckCallingConvAttr(Attr, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00003373 return;
3374
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003375 if (!isa<ObjCMethodDecl>(D)) {
3376 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3377 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003378 return;
3379 }
3380
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003381 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003382 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003383 D->addAttr(::new (S.Context)
3384 FastCallAttr(Attr.getRange(), S.Context,
3385 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003386 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003387 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003388 D->addAttr(::new (S.Context)
3389 StdCallAttr(Attr.getRange(), S.Context,
3390 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003391 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003392 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003393 D->addAttr(::new (S.Context)
3394 ThisCallAttr(Attr.getRange(), S.Context,
3395 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003396 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003397 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003398 D->addAttr(::new (S.Context)
3399 CDeclAttr(Attr.getRange(), S.Context,
3400 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003401 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003402 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003403 D->addAttr(::new (S.Context)
3404 PascalAttr(Attr.getRange(), S.Context,
3405 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003406 return;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003407 case AttributeList::AT_VectorCall:
3408 D->addAttr(::new (S.Context)
3409 VectorCallAttr(Attr.getRange(), S.Context,
3410 Attr.getAttributeSpellingListIndex()));
3411 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003412 case AttributeList::AT_MSABI:
3413 D->addAttr(::new (S.Context)
3414 MSABIAttr(Attr.getRange(), S.Context,
3415 Attr.getAttributeSpellingListIndex()));
3416 return;
3417 case AttributeList::AT_SysVABI:
3418 D->addAttr(::new (S.Context)
3419 SysVABIAttr(Attr.getRange(), S.Context,
3420 Attr.getAttributeSpellingListIndex()));
3421 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003422 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003423 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003424 switch (CC) {
3425 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003426 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003427 break;
3428 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003429 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003430 break;
3431 default:
3432 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003433 }
3434
Michael Han99315932013-01-24 16:46:58 +00003435 D->addAttr(::new (S.Context)
3436 PcsAttr(Attr.getRange(), S.Context, PCS,
3437 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003438 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003439 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003440 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003441 D->addAttr(::new (S.Context)
3442 IntelOclBiccAttr(Attr.getRange(), S.Context,
3443 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003444 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003445
Abramo Bagnara50099372010-04-30 13:10:51 +00003446 default:
3447 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003448 }
3449}
3450
Aaron Ballman02df2e02012-12-09 17:45:41 +00003451bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3452 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003453 if (attr.isInvalid())
3454 return true;
3455
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003456 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003457 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003458 attr.setInvalid();
3459 return true;
3460 }
3461
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003462 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003463 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003464 case AttributeList::AT_CDecl: CC = CC_C; break;
3465 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3466 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3467 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3468 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003469 case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003470 case AttributeList::AT_MSABI:
3471 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3472 CC_X86_64Win64;
3473 break;
3474 case AttributeList::AT_SysVABI:
3475 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3476 CC_C;
3477 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003478 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003479 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003480 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003481 attr.setInvalid();
3482 return true;
3483 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003484 if (StrRef == "aapcs") {
3485 CC = CC_AAPCS;
3486 break;
3487 } else if (StrRef == "aapcs-vfp") {
3488 CC = CC_AAPCS_VFP;
3489 break;
3490 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003491
3492 attr.setInvalid();
3493 Diag(attr.getLoc(), diag::err_invalid_pcs);
3494 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003495 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003496 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003497 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003498 }
3499
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003500 const TargetInfo &TI = Context.getTargetInfo();
3501 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003502 if (A != TargetInfo::CCCR_OK) {
3503 if (A == TargetInfo::CCCR_Warning)
3504 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003505
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003506 // This convention is not valid for the target. Use the default function or
3507 // method calling convention.
Aaron Ballman02df2e02012-12-09 17:45:41 +00003508 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3509 if (FD)
3510 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3511 TargetInfo::CCMT_NonMember;
3512 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003513 }
3514
John McCall3882ace2011-01-05 12:14:39 +00003515 return false;
3516}
3517
John McCall3882ace2011-01-05 12:14:39 +00003518/// Checks a regparm attribute, returning true if it is ill-formed and
3519/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003520bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3521 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003522 return true;
3523
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003524 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003525 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003526 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003527 }
Eli Friedman7044b762009-03-27 21:06:47 +00003528
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003529 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003530 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003531 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003532 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003533 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003534 }
3535
Douglas Gregore8bbc122011-09-02 00:18:52 +00003536 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003537 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003538 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003539 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003540 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003541 }
3542
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003543 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003544 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003545 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003546 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003547 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003548 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003549 }
3550
John McCall3882ace2011-01-05 12:14:39 +00003551 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003552}
3553
Artem Belevich7093e402015-04-21 22:55:54 +00003554// Checks whether an argument of launch_bounds attribute is acceptable
3555// May output an error.
3556static bool checkLaunchBoundsArgument(Sema &S, Expr *E,
3557 const CUDALaunchBoundsAttr &Attr,
3558 const unsigned Idx) {
3559
3560 if (S.DiagnoseUnexpandedParameterPack(E))
3561 return false;
3562
3563 // Accept template arguments for now as they depend on something else.
3564 // We'll get to check them when they eventually get instantiated.
3565 if (E->isValueDependent())
3566 return true;
3567
3568 llvm::APSInt I(64);
3569 if (!E->isIntegerConstantExpr(I, S.Context)) {
3570 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
3571 << &Attr << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
3572 return false;
3573 }
3574 // Make sure we can fit it in 32 bits.
3575 if (!I.isIntN(32)) {
3576 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
3577 << 32 << /* Unsigned */ 1;
3578 return false;
3579 }
3580 if (I < 0)
3581 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
3582 << &Attr << Idx << E->getSourceRange();
3583
3584 return true;
3585}
3586
3587void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
3588 Expr *MinBlocks, unsigned SpellingListIndex) {
3589 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
3590 SpellingListIndex);
3591
3592 if (!checkLaunchBoundsArgument(*this, MaxThreads, TmpAttr, 0))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003593 return;
3594
Artem Belevich7093e402015-04-21 22:55:54 +00003595 if (MinBlocks && !checkLaunchBoundsArgument(*this, MinBlocks, TmpAttr, 1))
3596 return;
3597
3598 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
3599 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
3600}
3601
3602static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3603 const AttributeList &Attr) {
3604 if (!checkAttributeAtLeastNumArgs(S, Attr, 1) ||
3605 !checkAttributeAtMostNumArgs(S, Attr, 2))
3606 return;
3607
3608 S.AddLaunchBoundsAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
3609 Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr,
3610 Attr.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00003611}
3612
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003613static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3614 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003615 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003616 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003617 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003618 return;
3619 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003620
3621 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003622 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003623
Aaron Ballman00e99962013-08-31 01:11:41 +00003624 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003625
3626 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3627 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3628 << Attr.getName() << ExpectedFunctionOrMethod;
3629 return;
3630 }
3631
3632 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003633 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3634 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003635 return;
3636
3637 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003638 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3639 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003640 return;
3641
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003642 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003643 if (IsPointer) {
3644 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003645 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003646 if (!BufferTy->isPointerType()) {
3647 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003648 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003649 }
3650 }
3651
Michael Han99315932013-01-24 16:46:58 +00003652 D->addAttr(::new (S.Context)
3653 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3654 ArgumentIdx, TypeTagIdx, IsPointer,
3655 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003656}
3657
3658static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3659 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003660 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003661 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003662 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003663 return;
3664 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003665
3666 if (!checkAttributeNumArgs(S, Attr, 1))
3667 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003668
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003669 if (!isa<VarDecl>(D)) {
3670 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3671 << Attr.getName() << ExpectedVariable;
3672 return;
3673 }
3674
Aaron Ballman00e99962013-08-31 01:11:41 +00003675 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00003676 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00003677 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3678 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003679
Michael Han99315932013-01-24 16:46:58 +00003680 D->addAttr(::new (S.Context)
3681 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003682 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003683 Attr.getLayoutCompatible(),
3684 Attr.getMustBeNull(),
3685 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003686}
3687
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003688//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003689// Checker-specific attribute handlers.
3690//===----------------------------------------------------------------------===//
3691
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003692static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) {
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003693 return type->isDependentType() ||
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003694 type->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003695}
3696
John McCalled433932011-01-25 03:31:58 +00003697static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003698 return type->isDependentType() ||
3699 type->isObjCObjectPointerType() ||
3700 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003701}
3702static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003703 return type->isDependentType() ||
3704 type->isPointerType() ||
3705 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003706}
3707
Chandler Carruthedc2c642011-07-02 00:01:44 +00003708static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003709 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003710 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003711
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003712 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003713 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3714 cf = false;
3715 } else {
3716 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3717 cf = true;
3718 }
3719
3720 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003721 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003722 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003723 return;
3724 }
3725
3726 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003727 param->addAttr(::new (S.Context)
3728 CFConsumedAttr(Attr.getRange(), S.Context,
3729 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003730 else
Michael Han99315932013-01-24 16:46:58 +00003731 param->addAttr(::new (S.Context)
3732 NSConsumedAttr(Attr.getRange(), S.Context,
3733 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003734}
3735
Chandler Carruthedc2c642011-07-02 00:01:44 +00003736static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3737 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003738
John McCalled433932011-01-25 03:31:58 +00003739 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003740
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003741 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003742 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003743 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003744 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003745 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003746 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3747 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003748 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003749 returnType = FD->getReturnType();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00003750 else if (auto *Param = dyn_cast<ParmVarDecl>(D)) {
3751 returnType = Param->getType()->getPointeeType();
3752 if (returnType.isNull()) {
3753 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
3754 << Attr.getName() << /*pointer-to-CF*/2
3755 << Attr.getRange();
3756 return;
3757 }
3758 } else {
3759 AttributeDeclKind ExpectedDeclKind;
3760 switch (Attr.getKind()) {
3761 default: llvm_unreachable("invalid ownership attribute");
3762 case AttributeList::AT_NSReturnsRetained:
3763 case AttributeList::AT_NSReturnsAutoreleased:
3764 case AttributeList::AT_NSReturnsNotRetained:
3765 ExpectedDeclKind = ExpectedFunctionOrMethod;
3766 break;
3767
3768 case AttributeList::AT_CFReturnsRetained:
3769 case AttributeList::AT_CFReturnsNotRetained:
3770 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
3771 break;
3772 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003773 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00003774 << Attr.getRange() << Attr.getName() << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003775 return;
3776 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003777
John McCalled433932011-01-25 03:31:58 +00003778 bool typeOK;
3779 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003780 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003781 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003782 case AttributeList::AT_NSReturnsRetained:
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003783 typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType);
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003784 cf = false;
3785 break;
3786
3787 case AttributeList::AT_NSReturnsAutoreleased:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003788 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003789 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3790 cf = false;
3791 break;
3792
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003793 case AttributeList::AT_CFReturnsRetained:
3794 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003795 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3796 cf = true;
3797 break;
3798 }
3799
3800 if (!typeOK) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00003801 if (isa<ParmVarDecl>(D)) {
3802 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
3803 << Attr.getName() << /*pointer-to-CF*/2
3804 << Attr.getRange();
3805 } else {
3806 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
3807 enum : unsigned {
3808 Function,
3809 Method,
3810 Property
3811 } SubjectKind = Function;
3812 if (isa<ObjCMethodDecl>(D))
3813 SubjectKind = Method;
3814 else if (isa<ObjCPropertyDecl>(D))
3815 SubjectKind = Property;
3816 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3817 << Attr.getName() << SubjectKind << cf
3818 << Attr.getRange();
3819 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003820 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003821 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003822
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003823 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003824 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003825 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003826 case AttributeList::AT_NSReturnsAutoreleased:
Nico Weber462fd1e2015-01-07 23:50:05 +00003827 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(
3828 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003829 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003830 case AttributeList::AT_CFReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003831 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(
3832 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003833 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003834 case AttributeList::AT_NSReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003835 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(
3836 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003837 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003838 case AttributeList::AT_CFReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003839 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(
3840 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003841 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003842 case AttributeList::AT_NSReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003843 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(
3844 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003845 return;
3846 };
3847}
3848
John McCallcf166702011-07-22 08:53:00 +00003849static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3850 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003851 const int EP_ObjCMethod = 1;
3852 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003853
John McCallcf166702011-07-22 08:53:00 +00003854 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003855 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003856 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003857 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003858 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003859 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003860
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003861 if (!resultType->isReferenceType() &&
3862 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003863 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003864 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003865 << attr.getName()
3866 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003867 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003868
3869 // Drop the attribute.
3870 return;
3871 }
3872
Nico Weber462fd1e2015-01-07 23:50:05 +00003873 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
3874 attr.getRange(), S.Context, attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003875}
3876
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003877static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3878 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003879 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003880
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003881 DeclContext *DC = method->getDeclContext();
3882 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3883 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3884 << attr.getName() << 0;
3885 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3886 return;
3887 }
3888 if (method->getMethodFamily() == OMF_dealloc) {
3889 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3890 << attr.getName() << 1;
3891 return;
3892 }
3893
Michael Han99315932013-01-24 16:46:58 +00003894 method->addAttr(::new (S.Context)
3895 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3896 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003897}
3898
Aaron Ballmanfb763042013-12-02 18:05:46 +00003899static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3900 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003901 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00003902 return;
John McCall32f5fe12011-09-30 05:12:12 +00003903
Aaron Ballmanfb763042013-12-02 18:05:46 +00003904 D->addAttr(::new (S.Context)
3905 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3906 Attr.getAttributeSpellingListIndex()));
3907}
3908
3909static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3910 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003911 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00003912 return;
3913
3914 D->addAttr(::new (S.Context)
3915 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3916 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003917}
3918
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003919static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3920 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00003921 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003922
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003923 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003924 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003925 return;
3926 }
John McCall28592582015-02-01 22:34:06 +00003927
3928 // Typedefs only allow objc_bridge(id) and have some additional checking.
3929 if (auto TD = dyn_cast<TypedefNameDecl>(D)) {
3930 if (!Parm->Ident->isStr("id")) {
3931 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_id)
3932 << Attr.getName();
3933 return;
3934 }
3935
3936 // Only allow 'cv void *'.
3937 QualType T = TD->getUnderlyingType();
3938 if (!T->isVoidPointerType()) {
3939 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
3940 return;
3941 }
3942 }
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003943
3944 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003945 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003946 Attr.getAttributeSpellingListIndex()));
3947}
3948
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003949static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3950 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00003951 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
3952
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003953 if (!Parm) {
3954 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3955 return;
3956 }
3957
3958 D->addAttr(::new (S.Context)
3959 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3960 Attr.getAttributeSpellingListIndex()));
3961}
3962
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003963static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3964 const AttributeList &Attr) {
3965 IdentifierInfo *RelatedClass =
Craig Topperc3ec1492014-05-26 06:22:03 +00003966 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003967 if (!RelatedClass) {
3968 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3969 return;
3970 }
3971 IdentifierInfo *ClassMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00003972 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003973 IdentifierInfo *InstanceMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00003974 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003975 D->addAttr(::new (S.Context)
3976 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3977 ClassMethod, InstanceMethod,
3978 Attr.getAttributeSpellingListIndex()));
3979}
3980
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003981static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3982 const AttributeList &Attr) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00003983 ObjCInterfaceDecl *IFace;
Nico Weber462fd1e2015-01-07 23:50:05 +00003984 if (ObjCCategoryDecl *CatDecl =
3985 dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00003986 IFace = CatDecl->getClassInterface();
3987 else
3988 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00003989
3990 if (!IFace)
3991 return;
3992
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003993 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003994 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003995 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3996 Attr.getAttributeSpellingListIndex()));
3997}
3998
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00003999static void handleObjCRuntimeName(Sema &S, Decl *D,
4000 const AttributeList &Attr) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004001 StringRef MetaDataName;
4002 if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName))
4003 return;
4004 D->addAttr(::new (S.Context)
4005 ObjCRuntimeNameAttr(Attr.getRange(), S.Context,
4006 MetaDataName,
4007 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004008}
4009
Alex Denisovfde64952015-06-26 05:28:36 +00004010// when a user wants to use objc_boxable with a union or struct
4011// but she doesn't have access to the declaration (legacy/third-party code)
4012// then she can 'enable' this feature via trick with a typedef
4013// e.g.:
4014// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
4015static void handleObjCBoxable(Sema &S, Decl *D, const AttributeList &Attr) {
4016 bool notify = false;
4017
4018 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4019 if (RD && RD->getDefinition()) {
4020 RD = RD->getDefinition();
4021 notify = true;
4022 }
4023
4024 if (RD) {
4025 ObjCBoxableAttr *BoxableAttr = ::new (S.Context)
4026 ObjCBoxableAttr(Attr.getRange(), S.Context,
4027 Attr.getAttributeSpellingListIndex());
4028 RD->addAttr(BoxableAttr);
4029 if (notify) {
4030 // we need to notify ASTReader/ASTWriter about
4031 // modification of existing declaration
4032 if (ASTMutationListener *L = S.getASTMutationListener())
4033 L->AddedAttributeToRecord(BoxableAttr, RD);
4034 }
4035 }
4036}
4037
Chandler Carruthedc2c642011-07-02 00:01:44 +00004038static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4039 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004040 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004041
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004042 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004043 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004044}
4045
Chandler Carruthedc2c642011-07-02 00:01:44 +00004046static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4047 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004048 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004049 QualType type = vd->getType();
4050
4051 if (!type->isDependentType() &&
4052 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004053 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004054 << type;
4055 return;
4056 }
4057
4058 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4059
4060 // If we have no lifetime yet, check the lifetime we're presumably
4061 // going to infer.
4062 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4063 lifetime = type->getObjCARCImplicitLifetime();
4064
4065 switch (lifetime) {
4066 case Qualifiers::OCL_None:
4067 assert(type->isDependentType() &&
4068 "didn't infer lifetime for non-dependent type?");
4069 break;
4070
4071 case Qualifiers::OCL_Weak: // meaningful
4072 case Qualifiers::OCL_Strong: // meaningful
4073 break;
4074
4075 case Qualifiers::OCL_ExplicitNone:
4076 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004077 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004078 << (lifetime == Qualifiers::OCL_Autoreleasing);
4079 break;
4080 }
4081
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004082 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004083 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4084 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004085}
4086
Francois Picheta83957a2010-12-19 06:50:37 +00004087//===----------------------------------------------------------------------===//
4088// Microsoft specific attribute handlers.
4089//===----------------------------------------------------------------------===//
4090
Chandler Carruthedc2c642011-07-02 00:01:44 +00004091static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00004092 if (!S.LangOpts.CPlusPlus) {
4093 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4094 << Attr.getName() << AttributeLangSupport::C;
4095 return;
4096 }
4097
Aaron Ballman60e705e2013-11-24 20:58:02 +00004098 if (!isa<CXXRecordDecl>(D)) {
4099 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4100 << Attr.getName() << ExpectedClass;
4101 return;
4102 }
4103
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004104 StringRef StrRef;
4105 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00004106 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00004107 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004108
David Majnemer89085342013-08-09 08:56:20 +00004109 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4110 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00004111 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4112 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00004113
Reid Kleckner140c4a72013-05-17 14:04:52 +00004114 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00004115 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004116 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004117 return;
4118 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004119
David Majnemer89085342013-08-09 08:56:20 +00004120 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004121 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00004122 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004123 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00004124 return;
4125 }
David Majnemer89085342013-08-09 08:56:20 +00004126 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004127 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004128 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004129 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004130 }
Francois Picheta83957a2010-12-19 06:50:37 +00004131
David Majnemer89085342013-08-09 08:56:20 +00004132 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4133 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004134}
4135
David Majnemer2c4e00a2014-01-29 22:07:36 +00004136static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4137 if (!S.LangOpts.CPlusPlus) {
4138 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4139 << Attr.getName() << AttributeLangSupport::C;
4140 return;
4141 }
4142 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00004143 D, Attr.getRange(), /*BestCase=*/true,
4144 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00004145 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
4146 if (IA)
4147 D->addAttr(IA);
4148}
4149
Reid Kleckner7d6d2702014-05-01 03:16:47 +00004150static void handleDeclspecThreadAttr(Sema &S, Decl *D,
4151 const AttributeList &Attr) {
4152 VarDecl *VD = cast<VarDecl>(D);
4153 if (!S.Context.getTargetInfo().isTLSSupported()) {
4154 S.Diag(Attr.getLoc(), diag::err_thread_unsupported);
4155 return;
4156 }
4157 if (VD->getTSCSpec() != TSCS_unspecified) {
4158 S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable);
4159 return;
4160 }
4161 if (VD->hasLocalStorage()) {
4162 S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
4163 return;
4164 }
4165 VD->addAttr(::new (S.Context) ThreadAttr(
4166 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
4167}
4168
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004169static void handleARMInterruptAttr(Sema &S, Decl *D,
4170 const AttributeList &Attr) {
4171 // Check the attribute arguments.
4172 if (Attr.getNumArgs() > 1) {
4173 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
4174 << Attr.getName() << 1;
4175 return;
4176 }
4177
4178 StringRef Str;
4179 SourceLocation ArgLoc;
4180
4181 if (Attr.getNumArgs() == 0)
4182 Str = "";
4183 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
4184 return;
4185
4186 ARMInterruptAttr::InterruptType Kind;
4187 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
4188 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
4189 << Attr.getName() << Str << ArgLoc;
4190 return;
4191 }
4192
4193 unsigned Index = Attr.getAttributeSpellingListIndex();
4194 D->addAttr(::new (S.Context)
4195 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
4196}
4197
4198static void handleMSP430InterruptAttr(Sema &S, Decl *D,
4199 const AttributeList &Attr) {
4200 if (!checkAttributeNumArgs(S, Attr, 1))
4201 return;
4202
4203 if (!Attr.isArgExpr(0)) {
4204 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
4205 << AANT_ArgumentIntegerConstant;
4206 return;
4207 }
4208
4209 // FIXME: Check for decl - it should be void ()(void).
4210
4211 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4212 llvm::APSInt NumParams(32);
4213 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
4214 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4215 << Attr.getName() << AANT_ArgumentIntegerConstant
4216 << NumParamsExpr->getSourceRange();
4217 return;
4218 }
4219
4220 unsigned Num = NumParams.getLimitedValue(255);
4221 if ((Num & 1) || Num > 30) {
4222 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
4223 << Attr.getName() << (int)NumParams.getSExtValue()
4224 << NumParamsExpr->getSourceRange();
4225 return;
4226 }
4227
Aaron Ballman36a53502014-01-16 13:03:14 +00004228 D->addAttr(::new (S.Context)
4229 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
4230 Attr.getAttributeSpellingListIndex()));
4231 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004232}
4233
4234static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4235 // Dispatch the interrupt attribute based on the current target.
4236 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
4237 handleMSP430InterruptAttr(S, D, Attr);
4238 else
4239 handleARMInterruptAttr(S, D, Attr);
4240}
4241
Matt Arsenault43fae6c2014-12-04 20:38:18 +00004242static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D,
4243 const AttributeList &Attr) {
4244 uint32_t NumRegs;
4245 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4246 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4247 return;
4248
4249 D->addAttr(::new (S.Context)
4250 AMDGPUNumVGPRAttr(Attr.getLoc(), S.Context,
4251 NumRegs,
4252 Attr.getAttributeSpellingListIndex()));
4253}
4254
4255static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D,
4256 const AttributeList &Attr) {
4257 uint32_t NumRegs;
4258 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4259 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4260 return;
4261
4262 D->addAttr(::new (S.Context)
4263 AMDGPUNumSGPRAttr(Attr.getLoc(), S.Context,
4264 NumRegs,
4265 Attr.getAttributeSpellingListIndex()));
4266}
4267
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004268static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
4269 const AttributeList& Attr) {
4270 // If we try to apply it to a function pointer, don't warn, but don't
4271 // do anything, either. It doesn't matter anyway, because there's nothing
4272 // special about calling a force_align_arg_pointer function.
4273 ValueDecl *VD = dyn_cast<ValueDecl>(D);
4274 if (VD && VD->getType()->isFunctionPointerType())
4275 return;
4276 // Also don't warn on function pointer typedefs.
4277 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
4278 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
4279 TD->getUnderlyingType()->isFunctionType()))
4280 return;
4281 // Attribute can only be applied to function types.
4282 if (!isa<FunctionDecl>(D)) {
4283 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4284 << Attr.getName() << /* function */0;
4285 return;
4286 }
4287
Aaron Ballman36a53502014-01-16 13:03:14 +00004288 D->addAttr(::new (S.Context)
4289 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
4290 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004291}
4292
4293DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
4294 unsigned AttrSpellingListIndex) {
4295 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004296 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00004297 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004298 }
4299
4300 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004301 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004302
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004303 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004304}
4305
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004306DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
4307 unsigned AttrSpellingListIndex) {
4308 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004309 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004310 D->dropAttr<DLLImportAttr>();
4311 }
4312
4313 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004314 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004315
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004316 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004317}
4318
Hans Wennborge82f19c2014-06-24 23:57:05 +00004319static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00004320 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
4321 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4322 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
4323 << A.getName();
4324 return;
4325 }
4326
Hans Wennborg606bd6d2014-11-03 14:24:45 +00004327 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4328 if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport &&
4329 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4330 // MinGW doesn't allow dllimport on inline functions.
4331 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
4332 << A.getName();
4333 return;
4334 }
4335 }
4336
Hans Wennborge82f19c2014-06-24 23:57:05 +00004337 unsigned Index = A.getAttributeSpellingListIndex();
4338 Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport
4339 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
4340 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004341 if (NewAttr)
4342 D->addAttr(NewAttr);
4343}
4344
David Majnemer2c4e00a2014-01-29 22:07:36 +00004345MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00004346Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00004347 unsigned AttrSpellingListIndex,
4348 MSInheritanceAttr::Spelling SemanticSpelling) {
4349 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
4350 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00004351 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004352 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
4353 << 1 /*previous declaration*/;
4354 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
4355 D->dropAttr<MSInheritanceAttr>();
4356 }
4357
4358 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
4359 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00004360 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
4361 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004362 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004363 }
4364 } else {
4365 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
4366 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4367 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004368 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004369 }
4370 if (RD->getDescribedClassTemplate()) {
4371 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4372 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004373 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004374 }
4375 }
4376
4377 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00004378 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00004379}
4380
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004381static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4382 // The capability attributes take a single string parameter for the name of
4383 // the capability they represent. The lockable attribute does not take any
4384 // parameters. However, semantically, both attributes represent the same
4385 // concept, and so they use the same semantic attribute. Eventually, the
4386 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00004387 //
Alp Toker958027b2014-07-14 19:42:55 +00004388 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00004389 // literal will be considered a "mutex."
4390 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004391 SourceLocation LiteralLoc;
4392 if (Attr.getKind() == AttributeList::AT_Capability &&
4393 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
4394 return;
4395
Aaron Ballman6c810072014-03-05 21:47:13 +00004396 // Currently, there are only two names allowed for a capability: role and
4397 // mutex (case insensitive). Diagnose other capability names.
4398 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
4399 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
4400
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004401 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
4402 Attr.getAttributeSpellingListIndex()));
4403}
4404
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004405static void handleAssertCapabilityAttr(Sema &S, Decl *D,
4406 const AttributeList &Attr) {
4407 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
4408 Attr.getArgAsExpr(0),
4409 Attr.getAttributeSpellingListIndex()));
4410}
4411
4412static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
4413 const AttributeList &Attr) {
4414 SmallVector<Expr*, 1> Args;
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004415 if (!checkLockFunAttrCommon(S, D, Attr, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004416 return;
4417
4418 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
4419 S.Context,
4420 Args.data(), Args.size(),
4421 Attr.getAttributeSpellingListIndex()));
4422}
4423
4424static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
4425 const AttributeList &Attr) {
4426 SmallVector<Expr*, 2> Args;
4427 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
4428 return;
4429
4430 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
4431 S.Context,
4432 Attr.getArgAsExpr(0),
4433 Args.data(),
4434 Args.size(),
4435 Attr.getAttributeSpellingListIndex()));
4436}
4437
4438static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
4439 const AttributeList &Attr) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004440 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004441 SmallVector<Expr *, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004442 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004443
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004444 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
4445 Attr.getRange(), S.Context, Args.data(), Args.size(),
4446 Attr.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004447}
4448
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004449static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
4450 const AttributeList &Attr) {
4451 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4452 return;
4453
4454 // check that all arguments are lockable objects
4455 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004456 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004457 if (Args.empty())
4458 return;
4459
4460 RequiresCapabilityAttr *RCA = ::new (S.Context)
4461 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
4462 Args.size(), Attr.getAttributeSpellingListIndex());
4463
4464 D->addAttr(RCA);
4465}
4466
Aaron Ballman43f40102014-11-14 22:34:56 +00004467static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4468 if (auto *NSD = dyn_cast<NamespaceDecl>(D)) {
4469 if (NSD->isAnonymousNamespace()) {
4470 S.Diag(Attr.getLoc(), diag::warn_deprecated_anonymous_namespace);
4471 // Do not want to attach the attribute to the namespace because that will
4472 // cause confusing diagnostic reports for uses of declarations within the
4473 // namespace.
4474 return;
4475 }
4476 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00004477
4478 if (!S.getLangOpts().CPlusPlus14)
4479 if (Attr.isCXX11Attribute() &&
4480 !(Attr.hasScope() && Attr.getScopeName()->isStr("gnu")))
Saleem Abdulrasoolb47d6062015-02-18 04:33:26 +00004481 S.Diag(Attr.getLoc(), diag::ext_deprecated_attr_is_a_cxx14_extension);
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00004482
Aaron Ballman43f40102014-11-14 22:34:56 +00004483 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
4484}
4485
Peter Collingbourne915df992015-05-15 18:33:32 +00004486static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4487 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4488 return;
4489
4490 std::vector<std::string> Sanitizers;
4491
4492 for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) {
4493 StringRef SanitizerName;
4494 SourceLocation LiteralLoc;
4495
4496 if (!S.checkStringLiteralArgumentAttr(Attr, I, SanitizerName, &LiteralLoc))
4497 return;
4498
4499 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0)
4500 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
4501
4502 Sanitizers.push_back(SanitizerName);
4503 }
4504
4505 D->addAttr(::new (S.Context) NoSanitizeAttr(
4506 Attr.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
4507 Attr.getAttributeSpellingListIndex()));
4508}
4509
4510static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
4511 const AttributeList &Attr) {
4512 std::string SanitizerName =
Peter Collingbourne94410942015-05-15 20:11:18 +00004513 llvm::StringSwitch<std::string>(Attr.getName()->getName())
Peter Collingbourne915df992015-05-15 18:33:32 +00004514 .Case("no_address_safety_analysis", "address")
4515 .Case("no_sanitize_address", "address")
4516 .Case("no_sanitize_thread", "thread")
Peter Collingbourne94410942015-05-15 20:11:18 +00004517 .Case("no_sanitize_memory", "memory");
Peter Collingbourne915df992015-05-15 18:33:32 +00004518 D->addAttr(::new (S.Context)
4519 NoSanitizeAttr(Attr.getRange(), S.Context, &SanitizerName, 1,
4520 Attr.getAttributeSpellingListIndex()));
4521}
4522
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004523/// Handles semantic checking for features that are common to all attributes,
4524/// such as checking whether a parameter was properly specified, or the correct
4525/// number of arguments were passed, etc.
4526static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4527 const AttributeList &Attr) {
4528 // Several attributes carry different semantics than the parsing requires, so
4529 // those are opted out of the common handling.
4530 //
4531 // We also bail on unknown and ignored attributes because those are handled
4532 // as part of the target-specific handling logic.
4533 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004534 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004535 return false;
4536
Aaron Ballman3aff6332013-12-02 19:30:36 +00004537 // Check whether the attribute requires specific language extensions to be
4538 // enabled.
4539 if (!Attr.diagnoseLangOpts(S))
4540 return true;
4541
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00004542 if (Attr.getMinArgs() == Attr.getMaxArgs()) {
4543 // If there are no optional arguments, then checking for the argument count
4544 // is trivial.
4545 if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4546 return true;
4547 } else {
4548 // There are optional arguments, so checking is slightly more involved.
4549 if (Attr.getMinArgs() &&
4550 !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs()))
4551 return true;
4552 else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() &&
4553 !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs()))
4554 return true;
4555 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004556
4557 // Check whether the attribute appertains to the given subject.
4558 if (!Attr.diagnoseAppertainsTo(S, D))
4559 return true;
4560
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004561 return false;
4562}
4563
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004564//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004565// Top Level Sema Entry Points
4566//===----------------------------------------------------------------------===//
4567
Richard Smithf8a75c32013-08-29 00:47:48 +00004568/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4569/// the attribute applies to decls. If the attribute is a type attribute, just
4570/// silently ignore it if a GNU attribute.
4571static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4572 const AttributeList &Attr,
4573 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004574 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00004575 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004576
Richard Smithf8a75c32013-08-29 00:47:48 +00004577 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4578 // instead.
4579 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4580 return;
4581
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004582 // Unknown attributes are automatically warned on. Target-specific attributes
4583 // which do not apply to the current target architecture are treated as
4584 // though they were unknown attributes.
4585 if (Attr.getKind() == AttributeList::UnknownAttribute ||
Bob Wilson7c730832015-07-20 22:57:31 +00004586 !Attr.existsInTarget(S.Context.getTargetInfo())) {
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004587 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute()
4588 ? diag::warn_unhandled_ms_attribute_ignored
4589 : diag::warn_unknown_attribute_ignored)
4590 << Attr.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004591 return;
4592 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004593
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004594 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4595 return;
4596
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004597 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004598 default:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004599 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004600 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004601 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004602 case AttributeList::AT_Interrupt:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004603 handleInterruptAttr(S, D, Attr);
4604 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004605 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004606 handleX86ForceAlignArgPointerAttr(S, D, Attr);
4607 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004608 case AttributeList::AT_DLLExport:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004609 case AttributeList::AT_DLLImport:
Hans Wennborge82f19c2014-06-24 23:57:05 +00004610 handleDLLAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004611 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004612 case AttributeList::AT_Mips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004613 handleSimpleAttribute<Mips16Attr>(S, D, Attr);
4614 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004615 case AttributeList::AT_NoMips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004616 handleSimpleAttribute<NoMips16Attr>(S, D, Attr);
4617 break;
Matt Arsenault43fae6c2014-12-04 20:38:18 +00004618 case AttributeList::AT_AMDGPUNumVGPR:
4619 handleAMDGPUNumVGPRAttr(S, D, Attr);
4620 break;
4621 case AttributeList::AT_AMDGPUNumSGPR:
4622 handleAMDGPUNumSGPRAttr(S, D, Attr);
4623 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00004624 case AttributeList::AT_IBAction:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004625 handleSimpleAttribute<IBActionAttr>(S, D, Attr);
4626 break;
4627 case AttributeList::AT_IBOutlet:
4628 handleIBOutlet(S, D, Attr);
4629 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004630 case AttributeList::AT_IBOutletCollection:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004631 handleIBOutletCollection(S, D, Attr);
4632 break;
4633 case AttributeList::AT_Alias:
4634 handleAliasAttr(S, D, Attr);
4635 break;
4636 case AttributeList::AT_Aligned:
4637 handleAlignedAttr(S, D, Attr);
4638 break;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00004639 case AttributeList::AT_AlignValue:
4640 handleAlignValueAttr(S, D, Attr);
4641 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004642 case AttributeList::AT_AlwaysInline:
Paul Robinsonf0674352014-03-31 22:29:15 +00004643 handleAlwaysInlineAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004644 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004645 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004646 handleAnalyzerNoReturnAttr(S, D, Attr);
4647 break;
4648 case AttributeList::AT_TLSModel:
4649 handleTLSModelAttr(S, D, Attr);
4650 break;
4651 case AttributeList::AT_Annotate:
4652 handleAnnotateAttr(S, D, Attr);
4653 break;
4654 case AttributeList::AT_Availability:
4655 handleAvailabilityAttr(S, D, Attr);
4656 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004657 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004658 handleDependencyAttr(S, scope, D, Attr);
4659 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004660 case AttributeList::AT_Common:
4661 handleCommonAttr(S, D, Attr);
4662 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004663 case AttributeList::AT_CUDAConstant:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004664 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr);
4665 break;
4666 case AttributeList::AT_Constructor:
4667 handleConstructorAttr(S, D, Attr);
4668 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004669 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004670 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr);
4671 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004672 case AttributeList::AT_Deprecated:
Aaron Ballman43f40102014-11-14 22:34:56 +00004673 handleDeprecatedAttr(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004674 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004675 case AttributeList::AT_Destructor:
4676 handleDestructorAttr(S, D, Attr);
4677 break;
4678 case AttributeList::AT_EnableIf:
4679 handleEnableIfAttr(S, D, Attr);
4680 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004681 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004682 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004683 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004684 case AttributeList::AT_MinSize:
Paul Robinsonaae2fba2014-12-10 23:34:36 +00004685 handleMinSizeAttr(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004686 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00004687 case AttributeList::AT_OptimizeNone:
4688 handleOptimizeNoneAttr(S, D, Attr);
4689 break;
Alexis Hunt724f14e2014-11-28 00:53:20 +00004690 case AttributeList::AT_FlagEnum:
4691 handleSimpleAttribute<FlagEnumAttr>(S, D, Attr);
4692 break;
Peter Collingbourne41af7c22014-05-20 17:12:51 +00004693 case AttributeList::AT_Flatten:
4694 handleSimpleAttribute<FlattenAttr>(S, D, Attr);
4695 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004696 case AttributeList::AT_Format:
4697 handleFormatAttr(S, D, Attr);
4698 break;
4699 case AttributeList::AT_FormatArg:
4700 handleFormatArgAttr(S, D, Attr);
4701 break;
4702 case AttributeList::AT_CUDAGlobal:
4703 handleGlobalAttr(S, D, Attr);
4704 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00004705 case AttributeList::AT_CUDADevice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004706 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr);
4707 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004708 case AttributeList::AT_CUDAHost:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004709 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr);
4710 break;
4711 case AttributeList::AT_GNUInline:
4712 handleGNUInlineAttr(S, D, Attr);
4713 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004714 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004715 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004716 break;
David Majnemer631a90b2015-02-04 07:23:21 +00004717 case AttributeList::AT_Restrict:
4718 handleRestrictAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004719 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004720 case AttributeList::AT_MayAlias:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004721 handleSimpleAttribute<MayAliasAttr>(S, D, Attr);
4722 break;
4723 case AttributeList::AT_Mode:
4724 handleModeAttr(S, D, Attr);
4725 break;
David Majnemer1bf0f8e2015-07-20 22:51:52 +00004726 case AttributeList::AT_NoAlias:
4727 handleSimpleAttribute<NoAliasAttr>(S, D, Attr);
4728 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004729 case AttributeList::AT_NoCommon:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004730 handleSimpleAttribute<NoCommonAttr>(S, D, Attr);
4731 break;
Peter Collingbourneb4728c12014-05-19 22:14:34 +00004732 case AttributeList::AT_NoSplitStack:
4733 handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr);
4734 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00004735 case AttributeList::AT_NonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004736 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
4737 handleNonNullAttrParameter(S, PVD, Attr);
4738 else
4739 handleNonNullAttr(S, D, Attr);
4740 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00004741 case AttributeList::AT_ReturnsNonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004742 handleReturnsNonNullAttr(S, D, Attr);
4743 break;
Hal Finkelee90a222014-09-26 05:04:30 +00004744 case AttributeList::AT_AssumeAligned:
4745 handleAssumeAlignedAttr(S, D, Attr);
4746 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004747 case AttributeList::AT_Overloadable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004748 handleSimpleAttribute<OverloadableAttr>(S, D, Attr);
4749 break;
4750 case AttributeList::AT_Ownership:
4751 handleOwnershipAttr(S, D, Attr);
4752 break;
4753 case AttributeList::AT_Cold:
4754 handleColdAttr(S, D, Attr);
4755 break;
4756 case AttributeList::AT_Hot:
4757 handleHotAttr(S, D, Attr);
4758 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004759 case AttributeList::AT_Naked:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004760 handleSimpleAttribute<NakedAttr>(S, D, Attr);
4761 break;
4762 case AttributeList::AT_NoReturn:
4763 handleNoReturnAttr(S, D, Attr);
4764 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004765 case AttributeList::AT_NoThrow:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004766 handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
4767 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004768 case AttributeList::AT_CUDAShared:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004769 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr);
4770 break;
4771 case AttributeList::AT_VecReturn:
4772 handleVecReturnAttr(S, D, Attr);
4773 break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004774
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004775 case AttributeList::AT_ObjCOwnership:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004776 handleObjCOwnershipAttr(S, D, Attr);
4777 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004778 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004779 handleObjCPreciseLifetimeAttr(S, D, Attr);
4780 break;
John McCall31168b02011-06-15 23:02:42 +00004781
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004782 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004783 handleObjCReturnsInnerPointerAttr(S, D, Attr);
4784 break;
John McCallcf166702011-07-22 08:53:00 +00004785
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004786 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004787 handleObjCRequiresSuperAttr(S, D, Attr);
4788 break;
4789
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004790 case AttributeList::AT_ObjCBridge:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004791 handleObjCBridgeAttr(S, scope, D, Attr);
4792 break;
4793
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004794 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004795 handleObjCBridgeMutableAttr(S, scope, D, Attr);
4796 break;
4797
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004798 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004799 handleObjCBridgeRelatedAttr(S, scope, D, Attr);
4800 break;
John McCallf1e8b342011-09-29 07:17:38 +00004801
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004802 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004803 handleObjCDesignatedInitializer(S, D, Attr);
4804 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004805
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004806 case AttributeList::AT_ObjCRuntimeName:
4807 handleObjCRuntimeName(S, D, Attr);
4808 break;
Alex Denisovfde64952015-06-26 05:28:36 +00004809
4810 case AttributeList::AT_ObjCBoxable:
4811 handleObjCBoxable(S, D, Attr);
4812 break;
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004813
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004814 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004815 handleCFAuditedTransferAttr(S, D, Attr);
4816 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004817 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004818 handleCFUnknownTransferAttr(S, D, Attr);
4819 break;
John McCall32f5fe12011-09-30 05:12:12 +00004820
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004821 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004822 case AttributeList::AT_NSConsumed:
4823 handleNSConsumedAttr(S, D, Attr);
4824 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004825 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004826 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr);
4827 break;
John McCalled433932011-01-25 03:31:58 +00004828
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004829 case AttributeList::AT_NSReturnsAutoreleased:
4830 case AttributeList::AT_NSReturnsNotRetained:
4831 case AttributeList::AT_CFReturnsNotRetained:
4832 case AttributeList::AT_NSReturnsRetained:
4833 case AttributeList::AT_CFReturnsRetained:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004834 handleNSReturnsRetainedAttr(S, D, Attr);
4835 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004836 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004837 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr);
4838 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004839 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004840 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr);
4841 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004842 case AttributeList::AT_VecTypeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004843 handleVecTypeHint(S, D, Attr);
4844 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004845
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004846 case AttributeList::AT_InitPriority:
4847 handleInitPriorityAttr(S, D, Attr);
4848 break;
4849
4850 case AttributeList::AT_Packed:
4851 handlePackedAttr(S, D, Attr);
4852 break;
4853 case AttributeList::AT_Section:
4854 handleSectionAttr(S, D, Attr);
4855 break;
Eric Christopher11acf732015-06-12 01:35:52 +00004856 case AttributeList::AT_Target:
4857 handleTargetAttr(S, D, Attr);
4858 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004859 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004860 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004861 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004862 case AttributeList::AT_ArcWeakrefUnavailable:
4863 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr);
4864 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004865 case AttributeList::AT_ObjCRootClass:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004866 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr);
4867 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004868 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek438f8db2014-02-22 01:06:05 +00004869 handleObjCSuppresProtocolAttr(S, D, Attr);
Ted Kremenek28eace62013-11-23 01:01:34 +00004870 break;
4871 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004872 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr);
4873 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00004874 case AttributeList::AT_Unused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004875 handleSimpleAttribute<UnusedAttr>(S, D, Attr);
4876 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004877 case AttributeList::AT_ReturnsTwice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004878 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr);
4879 break;
4880 case AttributeList::AT_Used:
4881 handleUsedAttr(S, D, Attr);
4882 break;
John McCalld041a9b2013-02-20 01:54:26 +00004883 case AttributeList::AT_Visibility:
4884 handleVisibilityAttr(S, D, Attr, false);
4885 break;
4886 case AttributeList::AT_TypeVisibility:
4887 handleVisibilityAttr(S, D, Attr, true);
4888 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004889 case AttributeList::AT_WarnUnused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004890 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr);
4891 break;
4892 case AttributeList::AT_WarnUnusedResult:
4893 handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004894 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004895 case AttributeList::AT_Weak:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004896 handleSimpleAttribute<WeakAttr>(S, D, Attr);
4897 break;
4898 case AttributeList::AT_WeakRef:
4899 handleWeakRefAttr(S, D, Attr);
4900 break;
4901 case AttributeList::AT_WeakImport:
4902 handleWeakImportAttr(S, D, Attr);
4903 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004904 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004905 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004906 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004907 case AttributeList::AT_ObjCException:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004908 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr);
4909 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004910 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004911 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004912 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004913 case AttributeList::AT_ObjCNSObject:
4914 handleObjCNSObject(S, D, Attr);
4915 break;
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00004916 case AttributeList::AT_ObjCIndependentClass:
4917 handleObjCIndependentClass(S, D, Attr);
4918 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004919 case AttributeList::AT_Blocks:
4920 handleBlocksAttr(S, D, Attr);
4921 break;
4922 case AttributeList::AT_Sentinel:
4923 handleSentinelAttr(S, D, Attr);
4924 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004925 case AttributeList::AT_Const:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004926 handleSimpleAttribute<ConstAttr>(S, D, Attr);
4927 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004928 case AttributeList::AT_Pure:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004929 handleSimpleAttribute<PureAttr>(S, D, Attr);
4930 break;
4931 case AttributeList::AT_Cleanup:
4932 handleCleanupAttr(S, D, Attr);
4933 break;
4934 case AttributeList::AT_NoDebug:
4935 handleNoDebugAttr(S, D, Attr);
4936 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00004937 case AttributeList::AT_NoDuplicate:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004938 handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
4939 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004940 case AttributeList::AT_NoInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004941 handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
4942 break;
4943 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
4944 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr);
4945 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004946 case AttributeList::AT_StdCall:
4947 case AttributeList::AT_CDecl:
4948 case AttributeList::AT_FastCall:
4949 case AttributeList::AT_ThisCall:
4950 case AttributeList::AT_Pascal:
Reid Klecknerd7857f02014-10-24 17:42:17 +00004951 case AttributeList::AT_VectorCall:
Charles Davisb5a214e2013-08-30 04:39:01 +00004952 case AttributeList::AT_MSABI:
4953 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004954 case AttributeList::AT_Pcs:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004955 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004956 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004957 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004958 case AttributeList::AT_OpenCLKernel:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004959 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr);
4960 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004961 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004962 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr);
4963 break;
John McCall8d32c052012-05-22 21:28:12 +00004964
4965 // Microsoft attributes:
David Majnemer8ab003a2015-02-02 19:30:52 +00004966 case AttributeList::AT_MSNoVTable:
4967 handleSimpleAttribute<MSNoVTableAttr>(S, D, Attr);
David Majnemer129f4172015-02-02 10:22:20 +00004968 break;
David Majnemer8ab003a2015-02-02 19:30:52 +00004969 case AttributeList::AT_MSStruct:
4970 handleSimpleAttribute<MSStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004971 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004972 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004973 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004974 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004975 case AttributeList::AT_MSInheritance:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004976 handleMSInheritanceAttr(S, D, Attr);
4977 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004978 case AttributeList::AT_SelectAny:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004979 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr);
4980 break;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00004981 case AttributeList::AT_Thread:
4982 handleDeclspecThreadAttr(S, D, Attr);
4983 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004984
4985 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004986 case AttributeList::AT_AssertExclusiveLock:
4987 handleAssertExclusiveLockAttr(S, D, Attr);
4988 break;
4989 case AttributeList::AT_AssertSharedLock:
4990 handleAssertSharedLockAttr(S, D, Attr);
4991 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004992 case AttributeList::AT_GuardedVar:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004993 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr);
4994 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004995 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004996 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004997 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004998 case AttributeList::AT_ScopedLockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004999 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr);
5000 break;
Peter Collingbourne915df992015-05-15 18:33:32 +00005001 case AttributeList::AT_NoSanitize:
5002 handleNoSanitizeAttr(S, D, Attr);
5003 break;
5004 case AttributeList::AT_NoSanitizeSpecific:
5005 handleNoSanitizeSpecificAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00005006 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005007 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005008 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00005009 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005010 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005011 handleGuardedByAttr(S, D, Attr);
5012 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005013 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00005014 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005015 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005016 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005017 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005018 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005019 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005020 handleLockReturnedAttr(S, D, Attr);
5021 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005022 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005023 handleLocksExcludedAttr(S, D, Attr);
5024 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005025 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005026 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005027 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005028 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00005029 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005030 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005031 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00005032 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005033 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005034
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005035 // Capability analysis attributes.
5036 case AttributeList::AT_Capability:
5037 case AttributeList::AT_Lockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005038 handleCapabilityAttr(S, D, Attr);
5039 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005040 case AttributeList::AT_RequiresCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005041 handleRequiresCapabilityAttr(S, D, Attr);
5042 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005043
5044 case AttributeList::AT_AssertCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005045 handleAssertCapabilityAttr(S, D, Attr);
5046 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005047 case AttributeList::AT_AcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005048 handleAcquireCapabilityAttr(S, D, Attr);
5049 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005050 case AttributeList::AT_ReleaseCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005051 handleReleaseCapabilityAttr(S, D, Attr);
5052 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005053 case AttributeList::AT_TryAcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005054 handleTryAcquireCapabilityAttr(S, D, Attr);
5055 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005056
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00005057 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00005058 case AttributeList::AT_Consumable:
5059 handleConsumableAttr(S, D, Attr);
5060 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00005061 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005062 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr);
5063 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00005064 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005065 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr);
5066 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00005067 case AttributeList::AT_CallableWhen:
5068 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00005069 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00005070 case AttributeList::AT_ParamTypestate:
5071 handleParamTypestateAttr(S, D, Attr);
5072 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00005073 case AttributeList::AT_ReturnTypestate:
5074 handleReturnTypestateAttr(S, D, Attr);
5075 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00005076 case AttributeList::AT_SetTypestate:
5077 handleSetTypestateAttr(S, D, Attr);
5078 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00005079 case AttributeList::AT_TestTypestate:
5080 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00005081 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00005082
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00005083 // Type safety attributes.
5084 case AttributeList::AT_ArgumentWithTypeTag:
5085 handleArgumentWithTypeTagAttr(S, D, Attr);
5086 break;
5087 case AttributeList::AT_TypeTagForDatatype:
5088 handleTypeTagForDatatypeAttr(S, D, Attr);
5089 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005090 }
5091}
5092
5093/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5094/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00005095void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00005096 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00005097 bool IncludeCXX11Attributes) {
5098 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00005099 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00005100
Joey Gouly2cd9db12013-12-13 16:15:28 +00005101 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00005102 // GCC accepts
5103 // static int a9 __attribute__((weakref));
5104 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00005105 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00005106 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
5107 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00005108 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00005109 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005110 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00005111
Aaron Ballmanbe243a72014-12-04 22:45:31 +00005112 // FIXME: We should be able to handle this in TableGen as well. It would be
5113 // good to have a way to specify "these attributes must appear as a group",
5114 // for these. Additionally, it would be good to have a way to specify "these
5115 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00005116 if (!D->hasAttr<OpenCLKernelAttr>()) {
5117 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00005118 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00005119 // FIXME: This emits a different error message than
5120 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00005121 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005122 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00005123 } else if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00005124 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005125 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00005126 } else if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00005127 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005128 D->setInvalidDecl();
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00005129 } else if (Attr *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
5130 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5131 << A << ExpectedKernelFunction;
5132 D->setInvalidDecl();
5133 } else if (Attr *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
5134 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5135 << A << ExpectedKernelFunction;
5136 D->setInvalidDecl();
Joey Gouly2cd9db12013-12-13 16:15:28 +00005137 }
5138 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005139}
5140
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005141// Annotation attributes are the only attributes allowed after an access
5142// specifier.
5143bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5144 const AttributeList *AttrList) {
5145 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005146 if (l->getKind() == AttributeList::AT_Annotate) {
David Majnemer706f3152014-12-14 01:05:01 +00005147 ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005148 } else {
5149 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5150 return true;
5151 }
5152 }
5153
5154 return false;
5155}
5156
John McCall42856de2011-10-01 05:17:03 +00005157/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5158/// contains any decl attributes that we should warn about.
5159static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5160 for ( ; A; A = A->getNext()) {
5161 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00005162 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00005163 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5164
5165 if (A->getKind() == AttributeList::UnknownAttribute) {
5166 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5167 << A->getName() << A->getRange();
5168 } else {
5169 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5170 << A->getName() << A->getRange();
5171 }
5172 }
5173}
5174
5175/// checkUnusedDeclAttributes - Given a declarator which is not being
5176/// used to build a declaration, complain about any decl attributes
5177/// which might be lying around on it.
5178void Sema::checkUnusedDeclAttributes(Declarator &D) {
5179 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5180 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5181 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5182 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5183}
5184
Ryan Flynn7d470f32009-07-30 03:15:39 +00005185/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00005186/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00005187NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5188 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00005189 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00005190 NamedDecl *NewD = nullptr;
Ryan Flynn7d470f32009-07-30 03:15:39 +00005191 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00005192 FunctionDecl *NewFD;
5193 // FIXME: Missing call to CheckFunctionDeclaration().
5194 // FIXME: Mangling?
5195 // FIXME: Is the qualifier info correct?
5196 // FIXME: Is the DeclContext correct?
5197 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5198 Loc, Loc, DeclarationName(II),
5199 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005200 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00005201 FD->hasPrototype(),
5202 false/*isConstexprSpecified*/);
5203 NewD = NewFD;
5204
5205 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00005206 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00005207
5208 // Fake up parameter variables; they are declared as if this were
5209 // a typedef.
5210 QualType FDTy = FD->getType();
5211 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5212 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00005213 for (const auto &AI : FT->param_types()) {
5214 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00005215 Param->setScopeInfo(0, Params.size());
5216 Params.push_back(Param);
5217 }
David Blaikie9c70e042011-09-21 18:16:56 +00005218 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005219 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005220 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5221 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005222 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005223 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005224 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00005225 if (VD->getQualifier()) {
5226 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005227 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005228 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005229 }
5230 return NewD;
5231}
5232
James Dennett634962f2012-06-14 21:40:34 +00005233/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005234/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005235void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005236 if (W.getUsed()) return; // only do this once
5237 W.setUsed(true);
5238 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5239 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005240 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00005241 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
5242 W.getLocation()));
5243 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00005244 WeakTopLevelDecl.push_back(NewD);
5245 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5246 // to insert Decl at TU scope, sorry.
5247 DeclContext *SavedContext = CurContext;
5248 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00005249 NewD->setDeclContext(CurContext);
5250 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00005251 PushOnScopeChains(NewD, S);
5252 CurContext = SavedContext;
5253 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00005254 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005255 }
5256}
5257
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005258void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5259 // It's valid to "forward-declare" #pragma weak, in which case we
5260 // have to do this.
5261 LoadExternalWeakUndeclaredIdentifiers();
5262 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005263 NamedDecl *ND = nullptr;
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005264 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5265 if (VD->isExternC())
5266 ND = VD;
5267 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5268 if (FD->isExternC())
5269 ND = FD;
5270 if (ND) {
5271 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00005272 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005273 if (I != WeakUndeclaredIdentifiers.end()) {
5274 WeakInfo W = I->second;
5275 DeclApplyPragmaWeak(S, ND, W);
5276 WeakUndeclaredIdentifiers[Id] = W;
5277 }
5278 }
5279 }
5280 }
5281}
5282
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005283/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5284/// it, apply them to D. This is a bit tricky because PD can have attributes
5285/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00005286void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005287 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005288 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00005289 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005290
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005291 // Walk the declarator structure, applying decl attributes that were in a type
5292 // position to the decl itself. This handles cases like:
5293 // int *__attr__(x)** D;
5294 // when X is a decl attribute.
5295 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5296 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00005297 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005298
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005299 // Finally, apply any attributes on the decl itself.
5300 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00005301 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005302}
John McCall28a6aea2009-11-04 02:18:39 +00005303
John McCall31168b02011-06-15 23:02:42 +00005304/// Is the given declaration allowed to use a forbidden type?
5305static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5306 // Private ivars are always okay. Unfortunately, people don't
5307 // always properly make their ivars private, even in system headers.
5308 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005309 // Function declarations in sys headers will be marked unavailable.
5310 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5311 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005312 return false;
5313
5314 // Require it to be declared in a system header.
5315 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5316}
5317
5318/// Handle a delayed forbidden-type diagnostic.
5319static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5320 Decl *decl) {
5321 if (decl && isForbiddenTypeAllowed(S, decl)) {
Aaron Ballman36a53502014-01-16 13:03:14 +00005322 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context,
5323 "this system declaration uses an unsupported type",
5324 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00005325 return;
5326 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005327 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005328 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005329 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005330 // kind of forbidden type messages on unavailable functions.
5331 if (FD->hasAttr<UnavailableAttr>() &&
5332 diag.getForbiddenTypeDiagnostic() ==
5333 diag::err_arc_array_param_no_ownership) {
5334 diag.Triggered = true;
5335 return;
5336 }
5337 }
John McCall31168b02011-06-15 23:02:42 +00005338
5339 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5340 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5341 diag.Triggered = true;
5342}
5343
Aaron Ballmanfb237522014-10-15 15:37:51 +00005344
5345static bool isDeclDeprecated(Decl *D) {
5346 do {
5347 if (D->isDeprecated())
5348 return true;
5349 // A category implicitly has the availability of the interface.
5350 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005351 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
5352 return Interface->isDeprecated();
Aaron Ballmanfb237522014-10-15 15:37:51 +00005353 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5354 return false;
5355}
5356
5357static bool isDeclUnavailable(Decl *D) {
5358 do {
5359 if (D->isUnavailable())
5360 return true;
5361 // A category implicitly has the availability of the interface.
5362 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005363 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
5364 return Interface->isUnavailable();
Aaron Ballmanfb237522014-10-15 15:37:51 +00005365 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5366 return false;
5367}
5368
Nico Weber0055a192015-03-19 19:18:22 +00005369static void DoEmitAvailabilityWarning(Sema &S, Sema::AvailabilityDiagnostic K,
Aaron Ballmanfb237522014-10-15 15:37:51 +00005370 Decl *Ctx, const NamedDecl *D,
5371 StringRef Message, SourceLocation Loc,
5372 const ObjCInterfaceDecl *UnknownObjCClass,
5373 const ObjCPropertyDecl *ObjCProperty,
5374 bool ObjCPropertyAccess) {
5375 // Diagnostics for deprecated or unavailable.
5376 unsigned diag, diag_message, diag_fwdclass_message;
5377
5378 // Matches 'diag::note_property_attribute' options.
5379 unsigned property_note_select;
5380
5381 // Matches diag::note_availability_specified_here.
5382 unsigned available_here_select_kind;
5383
5384 // Don't warn if our current context is deprecated or unavailable.
5385 switch (K) {
Nico Weber0055a192015-03-19 19:18:22 +00005386 case Sema::AD_Deprecation:
Jordan Rosed17c03e2015-04-30 17:20:35 +00005387 if (isDeclDeprecated(Ctx) || isDeclUnavailable(Ctx))
Aaron Ballmanfb237522014-10-15 15:37:51 +00005388 return;
5389 diag = !ObjCPropertyAccess ? diag::warn_deprecated
5390 : diag::warn_property_method_deprecated;
5391 diag_message = diag::warn_deprecated_message;
5392 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
5393 property_note_select = /* deprecated */ 0;
5394 available_here_select_kind = /* deprecated */ 2;
5395 break;
5396
Nico Weber0055a192015-03-19 19:18:22 +00005397 case Sema::AD_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00005398 if (isDeclUnavailable(Ctx))
5399 return;
5400 diag = !ObjCPropertyAccess ? diag::err_unavailable
5401 : diag::err_property_method_unavailable;
5402 diag_message = diag::err_unavailable_message;
5403 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
5404 property_note_select = /* unavailable */ 1;
5405 available_here_select_kind = /* unavailable */ 0;
5406 break;
5407
Nico Weber0055a192015-03-19 19:18:22 +00005408 case Sema::AD_Partial:
5409 diag = diag::warn_partial_availability;
5410 diag_message = diag::warn_partial_message;
5411 diag_fwdclass_message = diag::warn_partial_fwdclass_message;
5412 property_note_select = /* partial */ 2;
5413 available_here_select_kind = /* partial */ 3;
5414 break;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005415 }
5416
Aaron Ballmanfb237522014-10-15 15:37:51 +00005417 if (!Message.empty()) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005418 S.Diag(Loc, diag_message) << D << Message;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005419 if (ObjCProperty)
5420 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5421 << ObjCProperty->getDeclName() << property_note_select;
5422 } else if (!UnknownObjCClass) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005423 S.Diag(Loc, diag) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005424 if (ObjCProperty)
5425 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5426 << ObjCProperty->getDeclName() << property_note_select;
5427 } else {
Aaron Ballman43f40102014-11-14 22:34:56 +00005428 S.Diag(Loc, diag_fwdclass_message) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005429 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5430 }
5431
5432 S.Diag(D->getLocation(), diag::note_availability_specified_here)
5433 << D << available_here_select_kind;
Nico Weber0055a192015-03-19 19:18:22 +00005434 if (K == Sema::AD_Partial)
5435 S.Diag(Loc, diag::note_partial_availability_silence) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005436}
5437
5438static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
5439 Decl *Ctx) {
Nico Weber0055a192015-03-19 19:18:22 +00005440 assert(DD.Kind == DelayedDiagnostic::Deprecation ||
5441 DD.Kind == DelayedDiagnostic::Unavailable);
5442 Sema::AvailabilityDiagnostic AD = DD.Kind == DelayedDiagnostic::Deprecation
5443 ? Sema::AD_Deprecation
5444 : Sema::AD_Unavailable;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005445 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00005446 DoEmitAvailabilityWarning(
5447 S, AD, Ctx, DD.getDeprecationDecl(), DD.getDeprecationMessage(), DD.Loc,
5448 DD.getUnknownObjCClass(), DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00005449}
5450
John McCall2ec85372012-05-07 06:16:41 +00005451void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5452 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005453 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005454 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005455
John McCall2ec85372012-05-07 06:16:41 +00005456 // When delaying diagnostics to run in the context of a parsed
5457 // declaration, we only want to actually emit anything if parsing
5458 // succeeds.
5459 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005460
John McCall2ec85372012-05-07 06:16:41 +00005461 // We emit all the active diagnostics in this pool or any of its
5462 // parents. In general, we'll get one pool for the decl spec
5463 // and a child pool for each declarator; in a decl group like:
5464 // deprecated_typedef foo, *bar, baz();
5465 // only the declarator pops will be passed decls. This is correct;
5466 // we really do need to consider delayed diagnostics from the decl spec
5467 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005468 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005469 do {
John McCall6347b682012-05-07 06:16:58 +00005470 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005471 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5472 // This const_cast is a bit lame. Really, Triggered should be mutable.
5473 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005474 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005475 continue;
5476
John McCallc1465822011-02-14 07:13:47 +00005477 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005478 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00005479 case DelayedDiagnostic::Unavailable:
5480 // Don't bother giving deprecation/unavailable diagnostics if
5481 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00005482 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00005483 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00005484 break;
5485
5486 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005487 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005488 break;
John McCall31168b02011-06-15 23:02:42 +00005489
5490 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005491 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005492 break;
John McCall86121512010-01-27 03:50:35 +00005493 }
5494 }
John McCall2ec85372012-05-07 06:16:41 +00005495 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005496}
5497
John McCall6347b682012-05-07 06:16:58 +00005498/// Given a set of delayed diagnostics, re-emit them as if they had
5499/// been delayed in the current context instead of in the given pool.
5500/// Essentially, this just moves them to the current pool.
5501void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5502 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5503 assert(curPool && "re-emitting in undelayed context not supported");
5504 curPool->steal(pool);
5505}
5506
Ted Kremenekb79ee572013-12-18 23:30:06 +00005507void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
5508 NamedDecl *D, StringRef Message,
5509 SourceLocation Loc,
5510 const ObjCInterfaceDecl *UnknownObjCClass,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00005511 const ObjCPropertyDecl *ObjCProperty,
5512 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00005513 // Delay if we're currently parsing a declaration.
Nico Weber0055a192015-03-19 19:18:22 +00005514 if (DelayedDiagnostics.shouldDelayDiagnostics() && AD != AD_Partial) {
Nico Weber462fd1e2015-01-07 23:50:05 +00005515 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(
5516 AD, Loc, D, UnknownObjCClass, ObjCProperty, Message,
5517 ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00005518 return;
5519 }
5520
Ted Kremenekb79ee572013-12-18 23:30:06 +00005521 Decl *Ctx = cast<Decl>(getCurLexicalContext());
Nico Weber0055a192015-03-19 19:18:22 +00005522 DoEmitAvailabilityWarning(*this, AD, Ctx, D, Message, Loc, UnknownObjCClass,
5523 ObjCProperty, ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00005524}