blob: 06290672214400d2217d15c58243e8bca872924e [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"
Rafael Espindoladb77c4a2013-02-26 19:13:56 +000021#include "clang/AST/Mangle.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000022#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000023#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000024#include "clang/Basic/TargetInfo.h"
Benjamin Kramer6ee15622013-09-13 15:35:43 +000025#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000026#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000027#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000028#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000029#include "clang/Sema/Scope.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000030#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000031using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000032using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000033
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000034namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000035 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000036 C,
37 Cpp,
38 ObjC
39 };
40}
41
Chris Lattner58418ff2008-06-29 00:16:31 +000042//===----------------------------------------------------------------------===//
43// Helper functions
44//===----------------------------------------------------------------------===//
45
Ted Kremenek527042b2009-08-14 20:49:40 +000046/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000047/// type (function or function-typed variable) or an Objective-C
48/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000049static bool isFunctionOrMethod(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000050 return (D->getFunctionType() != NULL) || isa<ObjCMethodDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000051}
52
John McCall3882ace2011-01-05 12:14:39 +000053/// Return true if the given decl has a declarator that should have
54/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000055static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000056 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000057 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
58 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +000059}
60
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000061/// hasFunctionProto - Return true if the given decl has a argument
62/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000063/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000064static bool hasFunctionProto(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000065 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000066 return isa<FunctionProtoType>(FnTy);
Aaron Ballman12b9f652014-01-16 13:55:42 +000067 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000068}
69
70/// getFunctionOrMethodNumArgs - Return number of function or method
71/// arguments. It is an error to call this on a K&R function (use
72/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +000073static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000074 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000075 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000076 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000077 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000078 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000079}
80
Chandler Carruthff4c4f02011-07-01 23:49:12 +000081static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000082 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000083 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
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->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +000086
Chandler Carruthff4c4f02011-07-01 23:49:12 +000087 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000088}
89
Chandler Carruthff4c4f02011-07-01 23:49:12 +000090static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000091 if (const FunctionType *FnTy = D->getFunctionType())
Fariborz Jahanianf1c25022009-05-20 17:41:43 +000092 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000093 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +000094}
95
Chandler Carruthff4c4f02011-07-01 23:49:12 +000096static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000097 if (const FunctionType *FnTy = D->getFunctionType()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +000098 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000099 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000100 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000101 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000102 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000103 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000104 }
105}
106
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000107static bool isInstanceMethod(const Decl *D) {
108 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000109 return MethodDecl->isInstance();
110 return false;
111}
112
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000113static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000114 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000115 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000116 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000117
John McCall96fa4842010-05-17 21:00:27 +0000118 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
119 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000120 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000121
John McCall96fa4842010-05-17 21:00:27 +0000122 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000123
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000124 // FIXME: Should we walk the chain of classes?
125 return ClsName == &Ctx.Idents.get("NSString") ||
126 ClsName == &Ctx.Idents.get("NSMutableString");
127}
128
Daniel Dunbar980c6692008-09-26 03:32:58 +0000129static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000130 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000131 if (!PT)
132 return false;
133
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000134 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000135 if (!RT)
136 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000137
Daniel Dunbar980c6692008-09-26 03:32:58 +0000138 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000139 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000140 return false;
141
142 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
143}
144
Richard Smithb87c4652013-10-31 21:23:20 +0000145static unsigned getNumAttributeArgs(const AttributeList &Attr) {
146 // FIXME: Include the type in the argument list.
147 return Attr.getNumArgs() + Attr.hasParsedType();
148}
149
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000150/// \brief Check if the attribute has exactly as many args as Num. May
151/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000152static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000153 unsigned Num) {
154 if (getNumAttributeArgs(Attr) != Num) {
Aaron Ballmanb7243382013-07-23 19:30:11 +0000155 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
156 << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000157 return false;
158 }
159
160 return true;
161}
162
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000163/// \brief Check if the attribute has at least as many args as Num. May
164/// output an error.
165static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000166 unsigned Num) {
167 if (getNumAttributeArgs(Attr) < Num) {
Aaron Ballman05e420a2014-01-02 21:26:14 +0000168 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments)
169 << Attr.getName() << Num;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000170 return false;
171 }
172
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000173 return true;
174}
175
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000176/// \brief If Expr is a valid integer constant, get the value of the integer
177/// expression and return success or failure. May output an error.
178static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
179 const Expr *Expr, uint32_t &Val,
180 unsigned Idx = UINT_MAX) {
181 llvm::APSInt I(32);
182 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
183 !Expr->isIntegerConstantExpr(I, S.Context)) {
184 if (Idx != UINT_MAX)
185 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
186 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
187 << Expr->getSourceRange();
188 else
189 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
190 << Attr.getName() << AANT_ArgumentIntegerConstant
191 << Expr->getSourceRange();
192 return false;
193 }
194 Val = (uint32_t)I.getZExtValue();
195 return true;
196}
197
Aaron Ballmanfb763042013-12-02 18:05:46 +0000198/// \brief Diagnose mutually exclusive attributes when present on a given
199/// declaration. Returns true if diagnosed.
200template <typename AttrTy>
201static bool checkAttrMutualExclusion(Sema &S, Decl *D,
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000202 const AttributeList &Attr) {
203 if (AttrTy *A = D->getAttr<AttrTy>()) {
Aaron Ballmanfb763042013-12-02 18:05:46 +0000204 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000205 << Attr.getName() << A;
Aaron Ballmanfb763042013-12-02 18:05:46 +0000206 return true;
207 }
208 return false;
209}
210
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000211/// \brief Check if IdxExpr is a valid argument index for a function or
212/// instance method D. May output an error.
213///
214/// \returns true if IdxExpr is a valid index.
215static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000216 const AttributeList &Attr,
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000217 unsigned AttrArgNum,
218 const Expr *IdxExpr,
219 uint64_t &Idx)
220{
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000221 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000222
223 // In C++ the implicit 'this' function parameter also counts.
224 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000225 bool HP = hasFunctionProto(D);
226 bool HasImplicitThisParam = isInstanceMethod(D);
227 bool IV = HP && isFunctionOrMethodVariadic(D);
228 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
229 HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000230
231 llvm::APSInt IdxInt;
232 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
233 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000234 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
235 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
236 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000237 return false;
238 }
239
240 Idx = IdxInt.getLimitedValue();
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000241 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000242 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
243 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000244 return false;
245 }
246 Idx--; // Convert to zero-based.
247 if (HasImplicitThisParam) {
248 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000249 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000250 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000251 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000252 return false;
253 }
254 --Idx;
255 }
256
257 return true;
258}
259
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000260/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
261/// If not emit an error and return false. If the argument is an identifier it
262/// will emit an error with a fixit hint and treat it as if it was a string
263/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000264bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
265 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000266 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000267 // Look for identifiers. If we have one emit a hint to fix it to a literal.
268 if (Attr.isArgIdent(ArgNum)) {
269 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000270 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000271 << Attr.getName() << AANT_ArgumentString
272 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000273 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000274 Str = Loc->Ident->getName();
275 if (ArgLocation)
276 *ArgLocation = Loc->Loc;
277 return true;
278 }
279
280 // Now check for an actual string literal.
281 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
282 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
283 if (ArgLocation)
284 *ArgLocation = ArgExpr->getLocStart();
285
286 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000287 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000288 << Attr.getName() << AANT_ArgumentString;
289 return false;
290 }
291
292 Str = Literal->getString();
293 return true;
294}
295
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000296/// \brief Applies the given attribute to the Decl without performing any
297/// additional semantic checking.
298template <typename AttrType>
299static void handleSimpleAttribute(Sema &S, Decl *D,
300 const AttributeList &Attr) {
301 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
302 Attr.getAttributeSpellingListIndex()));
303}
304
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000305/// \brief Check if the passed-in expression is of type int or bool.
306static bool isIntOrBool(Expr *Exp) {
307 QualType QT = Exp->getType();
308 return QT->isBooleanType() || QT->isIntegerType();
309}
310
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000311
312// Check to see if the type is a smart pointer of some kind. We assume
313// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000314static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
315 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
316 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000317 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000318 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000319
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000320 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
321 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000322 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000323 return false;
324
325 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000326}
327
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000328/// \brief Check if passed in Decl is a pointer type.
329/// Note that this function may produce an error message.
330/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000331static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
332 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000333 const ValueDecl *vd = cast<ValueDecl>(D);
334 QualType QT = vd->getType();
335 if (QT->isAnyPointerType())
336 return true;
337
338 if (const RecordType *RT = QT->getAs<RecordType>()) {
339 // If it's an incomplete type, it could be a smart pointer; skip it.
340 // (We don't want to force template instantiation if we can avoid it,
341 // since that would alter the order in which templates are instantiated.)
342 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000343 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000344
Aaron Ballman553e6812013-12-26 14:54:11 +0000345 if (threadSafetyCheckIsSmartPointer(S, RT))
346 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000347 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000348
349 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000350 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000351 return false;
352}
353
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000354/// \brief Checks that the passed in QualType either is of RecordType or points
355/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000356static const RecordType *getRecordType(QualType QT) {
357 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000358 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000359
360 // Now check if we point to record type.
361 if (const PointerType *PT = QT->getAs<PointerType>())
362 return PT->getPointeeType()->getAs<RecordType>();
363
364 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000365}
366
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000367
Jordy Rose740b0c22012-05-08 03:27:22 +0000368static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
369 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000370 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
Aaron Ballman9ead1242013-12-19 02:39:40 +0000371 return RT->getDecl()->hasAttr<LockableAttr>();
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000372}
373
374
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000375/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000376/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000377static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
378 QualType Ty) {
379 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000380
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000381 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000382 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000383 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
Aaron Ballman1da282a2014-01-02 23:15:58 +0000384 << Attr.getName() << Ty;
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000385 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000386 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000387
Michael Hana9171bc2012-08-03 17:40:43 +0000388 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000389 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000390 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000391
392 // Allow smart pointers to be used as lockable objects.
393 // FIXME -- Check the type that the smart pointer points to.
394 if (threadSafetyCheckIsSmartPointer(S, RT))
395 return;
396
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000397 // Check if the type is lockable.
398 RecordDecl *RD = RT->getDecl();
Aaron Ballman9ead1242013-12-19 02:39:40 +0000399 if (RD->hasAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000400 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000401
402 // Else check if any base classes are lockable.
403 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
404 CXXBasePaths BPaths(false, false);
405 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
406 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000407 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000408
409 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
Aaron Ballman1da282a2014-01-02 23:15:58 +0000410 << Attr.getName() << Ty;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000411}
412
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000413/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000414/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000415/// \param Sidx The attribute argument index to start checking with.
416/// \param ParamIdxOk Whether an argument can be indexing into a function
417/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000418static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000419 const AttributeList &Attr,
420 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000421 int Sidx = 0,
422 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000423 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000424 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000425
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000426 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000427 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000428 Args.push_back(ArgExp);
429 continue;
430 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000431
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000432 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000433 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000434 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000435 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000436 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000437 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000438 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000439 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000440
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000441 // We allow constant strings to be used as a placeholder for expressions
442 // that are not valid C++ syntax, but warn that they are ignored.
443 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
444 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000445 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000446 continue;
447 }
448
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000449 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000450
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000451 // A pointer to member expression of the form &MyClass::mu is treated
452 // specially -- we need to look at the type of the member.
453 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
454 if (UOp->getOpcode() == UO_AddrOf)
455 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
456 if (DRE->getDecl()->isCXXInstanceMember())
457 ArgTy = DRE->getDecl()->getType();
458
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000459 // First see if we can just cast to record type, or point to record type.
460 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000461
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000462 // Now check if we index into a record type function param.
463 if(!RT && ParamIdxOk) {
464 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000465 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
466 if(FD && IL) {
467 unsigned int NumParams = FD->getNumParams();
468 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000469 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
470 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
471 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000472 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
473 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000474 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000475 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000476 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000477 }
478 }
479
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000480 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000481
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000482 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000483 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000484}
485
Chris Lattner58418ff2008-06-29 00:16:31 +0000486//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000487// Attribute Implementations
488//===----------------------------------------------------------------------===//
489
Daniel Dunbar032db472008-07-31 22:40:48 +0000490// FIXME: All this manual attribute parsing code is gross. At the
491// least add some helper functions to check most argument patterns (#
492// and types of args).
493
Michael Hana9171bc2012-08-03 17:40:43 +0000494static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000495 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000496 if (!threadSafetyCheckIsPointer(S, D, Attr))
497 return;
498
Michael Han99315932013-01-24 16:46:58 +0000499 D->addAttr(::new (S.Context)
500 PtGuardedVarAttr(Attr.getRange(), S.Context,
501 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000502}
503
Michael Hana9171bc2012-08-03 17:40:43 +0000504static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
505 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000506 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000507 SmallVector<Expr*, 1> Args;
508 // check that all arguments are lockable objects
509 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
510 unsigned Size = Args.size();
511 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000512 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000513
Michael Han3be3b442012-07-23 18:48:41 +0000514 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000515
Michael Han3be3b442012-07-23 18:48:41 +0000516 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000517}
518
Michael Han3be3b442012-07-23 18:48:41 +0000519static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
520 Expr *Arg = 0;
521 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
522 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000523
Aaron Ballman36a53502014-01-16 13:03:14 +0000524 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg,
525 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000526}
527
Michael Hana9171bc2012-08-03 17:40:43 +0000528static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000529 const AttributeList &Attr) {
530 Expr *Arg = 0;
531 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
532 return;
533
534 if (!threadSafetyCheckIsPointer(S, D, Attr))
535 return;
536
537 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +0000538 S.Context, Arg,
539 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000540}
541
Michael Hana9171bc2012-08-03 17:40:43 +0000542static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
543 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000544 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000545 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000546 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000547
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000548 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000549 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000550 if (!QT->isDependentType()) {
551 const RecordType *RT = getRecordType(QT);
Aaron Ballman9ead1242013-12-19 02:39:40 +0000552 if (!RT || !RT->getDecl()->hasAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000553 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000554 << Attr.getName();
555 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000556 }
557 }
558
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000559 // Check that all arguments are lockable objects.
560 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000561 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000562 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000563
Michael Han3be3b442012-07-23 18:48:41 +0000564 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000565}
566
Michael Hana9171bc2012-08-03 17:40:43 +0000567static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000568 const AttributeList &Attr) {
569 SmallVector<Expr*, 1> Args;
570 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
571 return;
572
573 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000574 D->addAttr(::new (S.Context)
575 AcquiredAfterAttr(Attr.getRange(), S.Context,
576 StartArg, Args.size(),
577 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000578}
579
Michael Hana9171bc2012-08-03 17:40:43 +0000580static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000581 const AttributeList &Attr) {
582 SmallVector<Expr*, 1> Args;
583 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
584 return;
585
586 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000587 D->addAttr(::new (S.Context)
588 AcquiredBeforeAttr(Attr.getRange(), S.Context,
589 StartArg, Args.size(),
590 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000591}
592
Michael Hana9171bc2012-08-03 17:40:43 +0000593static bool checkLockFunAttrCommon(Sema &S, Decl *D,
594 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000595 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000596 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000597 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000598 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000599
Michael Han3be3b442012-07-23 18:48:41 +0000600 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000601}
602
Michael Hana9171bc2012-08-03 17:40:43 +0000603static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000604 const AttributeList &Attr) {
605 SmallVector<Expr*, 1> Args;
606 if (!checkLockFunAttrCommon(S, D, Attr, Args))
607 return;
608
609 unsigned Size = Args.size();
610 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000611 D->addAttr(::new (S.Context)
612 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
613 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000614}
615
Michael Hana9171bc2012-08-03 17:40:43 +0000616static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000617 const AttributeList &Attr) {
618 SmallVector<Expr*, 1> Args;
619 if (!checkLockFunAttrCommon(S, D, Attr, Args))
620 return;
621
622 unsigned Size = Args.size();
623 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000624 D->addAttr(::new (S.Context)
625 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
626 StartArg, Size,
627 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000628}
629
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000630static void handleAssertSharedLockAttr(Sema &S, Decl *D,
631 const AttributeList &Attr) {
632 SmallVector<Expr*, 1> Args;
633 if (!checkLockFunAttrCommon(S, D, Attr, Args))
634 return;
635
636 unsigned Size = Args.size();
637 Expr **StartArg = Size == 0 ? 0 : &Args[0];
638 D->addAttr(::new (S.Context)
639 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
640 Attr.getAttributeSpellingListIndex()));
641}
642
643static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
644 const AttributeList &Attr) {
645 SmallVector<Expr*, 1> Args;
646 if (!checkLockFunAttrCommon(S, D, Attr, Args))
647 return;
648
649 unsigned Size = Args.size();
650 Expr **StartArg = Size == 0 ? 0 : &Args[0];
651 D->addAttr(::new (S.Context)
652 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
653 StartArg, Size,
654 Attr.getAttributeSpellingListIndex()));
655}
656
657
Michael Hana9171bc2012-08-03 17:40:43 +0000658static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
659 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000660 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000661 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000662 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000663
Aaron Ballman00e99962013-08-31 01:11:41 +0000664 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000665 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000666 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000667 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000668 }
669
670 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000671 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000672
Michael Han3be3b442012-07-23 18:48:41 +0000673 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000674}
675
Michael Hana9171bc2012-08-03 17:40:43 +0000676static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000677 const AttributeList &Attr) {
678 SmallVector<Expr*, 2> Args;
679 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
680 return;
681
Michael Han99315932013-01-24 16:46:58 +0000682 D->addAttr(::new (S.Context)
683 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000684 Attr.getArgAsExpr(0),
685 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000686 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000687}
688
Michael Hana9171bc2012-08-03 17:40:43 +0000689static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000690 const AttributeList &Attr) {
691 SmallVector<Expr*, 2> Args;
692 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
693 return;
694
Michael Han99315932013-01-24 16:46:58 +0000695 D->addAttr(::new (S.Context)
696 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000697 Attr.getArgAsExpr(0),
698 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000699 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000700}
701
Michael Hana9171bc2012-08-03 17:40:43 +0000702static bool checkLocksRequiredCommon(Sema &S, Decl *D,
703 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000704 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000705 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000706 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000707
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000708 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000709 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000710 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000711 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000712
Michael Han3be3b442012-07-23 18:48:41 +0000713 return true;
714}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000715
Michael Hana9171bc2012-08-03 17:40:43 +0000716static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000717 const AttributeList &Attr) {
718 SmallVector<Expr*, 1> Args;
719 if (!checkLocksRequiredCommon(S, D, Attr, Args))
720 return;
721
722 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000723 D->addAttr(::new (S.Context)
724 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
725 StartArg, Args.size(),
726 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000727}
728
Michael Hana9171bc2012-08-03 17:40:43 +0000729static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000730 const AttributeList &Attr) {
731 SmallVector<Expr*, 1> Args;
732 if (!checkLocksRequiredCommon(S, D, Attr, Args))
733 return;
734
735 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000736 D->addAttr(::new (S.Context)
737 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
738 StartArg, Args.size(),
739 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000740}
741
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000742static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000743 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000744 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000745 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000746 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000747 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000748 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000749 Expr **StartArg = Size == 0 ? 0 : &Args[0];
750
Michael Han99315932013-01-24 16:46:58 +0000751 D->addAttr(::new (S.Context)
752 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
753 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000754}
755
756static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000757 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000758 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000759 SmallVector<Expr*, 1> Args;
760 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
761 unsigned Size = Args.size();
762 if (Size == 0)
763 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000764
Michael Han99315932013-01-24 16:46:58 +0000765 D->addAttr(::new (S.Context)
766 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
767 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000768}
769
770static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000771 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000772 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000773 return;
774
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000775 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000776 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000777 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000778 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000779 if (Size == 0)
780 return;
781 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000782
Michael Han99315932013-01-24 16:46:58 +0000783 D->addAttr(::new (S.Context)
784 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
785 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000786}
787
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000788static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
789 Expr *Cond = Attr.getArgAsExpr(0);
790 if (!Cond->isTypeDependent()) {
791 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
792 if (Converted.isInvalid())
793 return;
794 Cond = Converted.take();
795 }
796
797 StringRef Msg;
798 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
799 return;
800
801 SmallVector<PartialDiagnosticAt, 8> Diags;
802 if (!Cond->isValueDependent() &&
803 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
804 Diags)) {
805 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
806 for (int I = 0, N = Diags.size(); I != N; ++I)
807 S.Diag(Diags[I].first, Diags[I].second);
808 return;
809 }
810
811 D->addAttr(::new (S.Context)
812 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
813 Attr.getAttributeSpellingListIndex()));
814}
815
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000816static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000817 ConsumableAttr::ConsumedState DefaultState;
818
819 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000820 IdentifierLoc *IL = Attr.getArgAsIdent(0);
821 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
822 DefaultState)) {
823 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
824 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000825 return;
826 }
David Blaikie16f76d22013-09-06 01:28:43 +0000827 } else {
828 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
829 << Attr.getName() << AANT_ArgumentIdentifier;
830 return;
831 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000832
833 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000834 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000835 Attr.getAttributeSpellingListIndex()));
836}
837
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000838
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000839static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
840 const AttributeList &Attr) {
841 ASTContext &CurrContext = S.getASTContext();
842 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
843
844 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
845 if (!RD->hasAttr<ConsumableAttr>()) {
846 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
847 RD->getNameAsString();
848
849 return false;
850 }
851 }
852
853 return true;
854}
855
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000856
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000857static void handleCallableWhenAttr(Sema &S, Decl *D,
858 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000859 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
860 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000861
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000862 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
863 return;
864
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000865 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
866 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
867 CallableWhenAttr::ConsumedState CallableState;
868
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000869 StringRef StateString;
870 SourceLocation Loc;
871 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
872 return;
873
874 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000875 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000876 S.Diag(Loc, diag::warn_attribute_type_not_supported)
877 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000878 return;
879 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000880
881 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000882 }
883
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000884 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000885 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
886 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000887}
888
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000889
DeLesley Hutchins69391772013-10-17 23:23:53 +0000890static void handleParamTypestateAttr(Sema &S, Decl *D,
891 const AttributeList &Attr) {
892 if (!checkAttributeNumArgs(S, Attr, 1)) return;
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000893
DeLesley Hutchins69391772013-10-17 23:23:53 +0000894 ParamTypestateAttr::ConsumedState ParamState;
895
896 if (Attr.isArgIdent(0)) {
897 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
898 StringRef StateString = Ident->Ident->getName();
899
900 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
901 ParamState)) {
902 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
903 << Attr.getName() << StateString;
904 return;
905 }
906 } else {
907 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
908 Attr.getName() << AANT_ArgumentIdentifier;
909 return;
910 }
911
912 // FIXME: This check is currently being done in the analysis. It can be
913 // enabled here only after the parser propagates attributes at
914 // template specialization definition, not declaration.
915 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
916 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
917 //
918 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
919 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
920 // ReturnType.getAsString();
921 // return;
922 //}
923
924 D->addAttr(::new (S.Context)
925 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
926 Attr.getAttributeSpellingListIndex()));
927}
928
929
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000930static void handleReturnTypestateAttr(Sema &S, Decl *D,
931 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000932 if (!checkAttributeNumArgs(S, Attr, 1)) return;
933
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000934 ReturnTypestateAttr::ConsumedState ReturnState;
935
936 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000937 IdentifierLoc *IL = Attr.getArgAsIdent(0);
938 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
939 ReturnState)) {
940 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
941 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000942 return;
943 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000944 } else {
945 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
946 Attr.getName() << AANT_ArgumentIdentifier;
947 return;
948 }
949
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000950 // FIXME: This check is currently being done in the analysis. It can be
951 // enabled here only after the parser propagates attributes at
952 // template specialization definition, not declaration.
953 //QualType ReturnType;
954 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000955 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
956 // ReturnType = Param->getType();
957 //
958 //} else if (const CXXConstructorDecl *Constructor =
959 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000960 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
961 //
962 //} else {
963 //
964 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
965 //}
966 //
967 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
968 //
969 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
970 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
971 // ReturnType.getAsString();
972 // return;
973 //}
974
975 D->addAttr(::new (S.Context)
976 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
977 Attr.getAttributeSpellingListIndex()));
978}
979
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000980
981static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000982 if (!checkAttributeNumArgs(S, Attr, 1))
983 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000984
985 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
986 return;
987
988 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000989 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000990 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
991 StringRef Param = Ident->Ident->getName();
992 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
993 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
994 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000995 return;
996 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000997 } else {
998 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
999 Attr.getName() << AANT_ArgumentIdentifier;
1000 return;
1001 }
1002
1003 D->addAttr(::new (S.Context)
1004 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1005 Attr.getAttributeSpellingListIndex()));
1006}
1007
Chris Wailes9385f9f2013-10-29 20:28:41 +00001008static void handleTestTypestateAttr(Sema &S, Decl *D,
1009 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001010 if (!checkAttributeNumArgs(S, Attr, 1))
1011 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001012
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001013 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1014 return;
1015
Chris Wailes9385f9f2013-10-29 20:28:41 +00001016 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001017 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001018 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1019 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001020 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001021 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1022 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001023 return;
1024 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001025 } else {
1026 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1027 Attr.getName() << AANT_ArgumentIdentifier;
1028 return;
1029 }
1030
1031 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001032 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001033 Attr.getAttributeSpellingListIndex()));
1034}
1035
Chandler Carruthedc2c642011-07-02 00:01:44 +00001036static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1037 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001038 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001039 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001040}
1041
Chandler Carruthedc2c642011-07-02 00:01:44 +00001042static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001043 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Aaron Ballman36a53502014-01-16 13:03:14 +00001044 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context,
1045 Attr.getAttributeSpellingListIndex()));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001046 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001047 // If the alignment is less than or equal to 8 bits, the packed attribute
1048 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001049 if (!FD->getType()->isDependentType() &&
1050 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001051 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001052 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001053 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001054 else
Michael Han99315932013-01-24 16:46:58 +00001055 FD->addAttr(::new (S.Context)
1056 PackedAttr(Attr.getRange(), S.Context,
1057 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001058 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001059 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001060}
1061
Ted Kremenek7fd17232011-09-29 07:02:25 +00001062static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1063 // The IBOutlet/IBOutletCollection attributes only apply to instance
1064 // variables or properties of Objective-C classes. The outlet must also
1065 // have an object reference type.
1066 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1067 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001068 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001069 << Attr.getName() << VD->getType() << 0;
1070 return false;
1071 }
1072 }
1073 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1074 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001075 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001076 << Attr.getName() << PD->getType() << 1;
1077 return false;
1078 }
1079 }
1080 else {
1081 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1082 return false;
1083 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001084
Ted Kremenek7fd17232011-09-29 07:02:25 +00001085 return true;
1086}
1087
Chandler Carruthedc2c642011-07-02 00:01:44 +00001088static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001089 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001090 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001091
Michael Han99315932013-01-24 16:46:58 +00001092 D->addAttr(::new (S.Context)
1093 IBOutletAttr(Attr.getRange(), S.Context,
1094 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001095}
1096
Chandler Carruthedc2c642011-07-02 00:01:44 +00001097static void handleIBOutletCollection(Sema &S, Decl *D,
1098 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001099
1100 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001101 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001102 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1103 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001104 return;
1105 }
1106
Ted Kremenek7fd17232011-09-29 07:02:25 +00001107 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001108 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001109
Richard Smithb1f9a282013-10-31 01:56:18 +00001110 ParsedType PT;
1111
1112 if (Attr.hasParsedType())
1113 PT = Attr.getTypeArg();
1114 else {
1115 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1116 S.getScopeForContext(D->getDeclContext()->getParent()));
1117 if (!PT) {
1118 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1119 return;
1120 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001121 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001122
Richard Smithb87c4652013-10-31 21:23:20 +00001123 TypeSourceInfo *QTLoc = 0;
1124 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1125 if (!QTLoc)
1126 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001127
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001128 // Diagnose use of non-object type in iboutletcollection attribute.
1129 // FIXME. Gnu attribute extension ignores use of builtin types in
1130 // attributes. So, __attribute__((iboutletcollection(char))) will be
1131 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001132 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001133 S.Diag(Attr.getLoc(),
1134 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1135 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001136 return;
1137 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001138
Michael Han99315932013-01-24 16:46:58 +00001139 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001140 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001141 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001142}
1143
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001144static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001145 if (const RecordType *UT = T->getAsUnionType())
1146 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1147 RecordDecl *UD = UT->getDecl();
1148 for (RecordDecl::field_iterator it = UD->field_begin(),
1149 itend = UD->field_end(); it != itend; ++it) {
1150 QualType QT = it->getType();
1151 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1152 T = QT;
1153 return;
1154 }
1155 }
1156 }
1157}
1158
Ted Kremenek9aedc152014-01-17 06:24:56 +00001159static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
1160 SourceRange R) {
1161 T = T.getNonReferenceType();
1162 possibleTransparentUnionPointerType(T);
1163
1164 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1165 S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only)
1166 << Attr.getName() << R;
1167 return false;
1168 }
1169 return true;
1170}
1171
1172static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1173 const AttributeList &Attr) {
1174 // Is the argument a pointer type?
1175 if (!attrNonNullArgCheck(S, D->getType(), Attr, D->getSourceRange()))
1176 return;
1177
1178 if (Attr.getNumArgs() > 0) {
1179 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1180 << D->getSourceRange();
1181 return;
1182 }
1183
1184 D->addAttr(::new (S.Context)
1185 NonNullAttr(Attr.getRange(), S.Context, 0, 0,
1186 Attr.getAttributeSpellingListIndex()));
1187}
1188
Chandler Carruthedc2c642011-07-02 00:01:44 +00001189static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001190 SmallVector<unsigned, 8> NonNullArgs;
1191 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001192 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001193 uint64_t Idx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00001194 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001195 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001196
1197 // Is the function argument a pointer type?
Ted Kremenek9aedc152014-01-17 06:24:56 +00001198 // FIXME: Should also highlight argument in decl in the diagnostic.
1199 if (!attrNonNullArgCheck(S, getFunctionOrMethodArgType(D, Idx),
1200 Attr, Ex->getSourceRange()))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001201 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001202
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001203 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001204 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001205
1206 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1207 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001208 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001209 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1210 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001211 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001212 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001213 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001214 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001215
Ted Kremenek22813f42010-10-21 18:49:36 +00001216 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001217 if (NonNullArgs.empty()) {
1218 // Warn the trivial case only if attribute is not coming from a
1219 // macro instantiation.
1220 if (Attr.getLoc().isFileID())
1221 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001222 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001223 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001224 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001225
Nick Lewyckye1121512013-01-24 01:12:16 +00001226 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001227 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001228 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001229 D->addAttr(::new (S.Context)
1230 NonNullAttr(Attr.getRange(), S.Context, start, size,
1231 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001232}
1233
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001234static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1235 switch (K) {
1236 case OwnershipAttr::Holds: return "'ownership_holds'";
1237 case OwnershipAttr::Takes: return "'ownership_takes'";
1238 case OwnershipAttr::Returns: return "'ownership_returns'";
1239 }
1240 llvm_unreachable("unknown ownership");
1241}
1242
Chandler Carruthedc2c642011-07-02 00:01:44 +00001243static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001244 // This attribute must be applied to a function declaration. The first
1245 // argument to the attribute must be an identifier, the name of the resource,
1246 // for example: malloc. The following arguments must be argument indexes, the
1247 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001248 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001249 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001250 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001251
Aaron Ballman00e99962013-08-31 01:11:41 +00001252 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001253 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001254 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001255 return;
1256 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001257
Richard Smith852e9ce2013-11-27 01:46:48 +00001258 // Figure out our Kind.
1259 OwnershipAttr::OwnershipKind K =
1260 OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0,
1261 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001262
Richard Smith852e9ce2013-11-27 01:46:48 +00001263 // Check arguments.
1264 switch (K) {
1265 case OwnershipAttr::Takes:
1266 case OwnershipAttr::Holds:
1267 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001268 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1269 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001270 return;
1271 }
1272 break;
1273 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001274 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001275 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1276 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001277 return;
1278 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001279 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001280 }
1281
Richard Smith852e9ce2013-11-27 01:46:48 +00001282 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001283
1284 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001285 StringRef ModuleName = Module->getName();
1286 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1287 ModuleName.size() > 4) {
1288 ModuleName = ModuleName.drop_front(2).drop_back(2);
1289 Module = &S.PP.getIdentifierTable().get(ModuleName);
1290 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001291
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001292 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001293 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1294 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001295 uint64_t Idx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00001296 if (!checkFunctionOrMethodArgumentIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001297 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001298
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001299 // Is the function argument a pointer type?
1300 QualType T = getFunctionOrMethodArgType(D, Idx);
1301 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001302 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001303 case OwnershipAttr::Takes:
1304 case OwnershipAttr::Holds:
1305 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1306 Err = 0;
1307 break;
1308 case OwnershipAttr::Returns:
1309 if (!T->isIntegerType())
1310 Err = 1;
1311 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001312 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001313 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001314 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001315 << Ex->getSourceRange();
1316 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001317 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001318
1319 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001320 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001321 i = D->specific_attr_begin<OwnershipAttr>(),
1322 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001323 // FIXME: A returns attribute should conflict with any returns attribute
1324 // with a different index too.
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001325 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1326 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1327 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1328 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1329 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001330 }
1331 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001332 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001333 }
1334
1335 unsigned* start = OwnershipArgs.data();
1336 unsigned size = OwnershipArgs.size();
1337 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001338
Michael Han99315932013-01-24 16:46:58 +00001339 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001340 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001341 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001342}
1343
Chandler Carruthedc2c642011-07-02 00:01:44 +00001344static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001345 // Check the attribute arguments.
1346 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001347 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1348 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001349 return;
1350 }
1351
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001352 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001353
Rafael Espindolac18086a2010-02-23 22:00:30 +00001354 // gcc rejects
1355 // class c {
1356 // static int a __attribute__((weakref ("v2")));
1357 // static int b() __attribute__((weakref ("f3")));
1358 // };
1359 // and ignores the attributes of
1360 // void f(void) {
1361 // static int a __attribute__((weakref ("v2")));
1362 // }
1363 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001364 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001365 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001366 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1367 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001368 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001369 }
1370
1371 // The GCC manual says
1372 //
1373 // At present, a declaration to which `weakref' is attached can only
1374 // be `static'.
1375 //
1376 // It also says
1377 //
1378 // Without a TARGET,
1379 // given as an argument to `weakref' or to `alias', `weakref' is
1380 // equivalent to `weak'.
1381 //
1382 // gcc 4.4.1 will accept
1383 // int a7 __attribute__((weakref));
1384 // as
1385 // int a7 __attribute__((weak));
1386 // This looks like a bug in gcc. We reject that for now. We should revisit
1387 // it if this behaviour is actually used.
1388
Rafael Espindolac18086a2010-02-23 22:00:30 +00001389 // GCC rejects
1390 // static ((alias ("y"), weakref)).
1391 // Should we? How to check that weakref is before or after alias?
1392
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001393 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1394 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1395 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001396 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001397 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001398 // GCC will accept anything as the argument of weakref. Should we
1399 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001400 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1401 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001402
Michael Han99315932013-01-24 16:46:58 +00001403 D->addAttr(::new (S.Context)
1404 WeakRefAttr(Attr.getRange(), S.Context,
1405 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001406}
1407
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001408static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1409 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001410 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001411 return;
1412
Douglas Gregore8bbc122011-09-02 00:18:52 +00001413 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001414 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1415 return;
1416 }
1417
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001418 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001419
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001420 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001421 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001422}
1423
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001424static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001425 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001426 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001427
Michael Han99315932013-01-24 16:46:58 +00001428 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1429 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001430}
1431
1432static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001433 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001434 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001435
Michael Han99315932013-01-24 16:46:58 +00001436 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1437 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001438}
1439
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001440static void handleTLSModelAttr(Sema &S, Decl *D,
1441 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001442 StringRef Model;
1443 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001444 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001445 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001446 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001447
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001448 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001449 if (Model != "global-dynamic" && Model != "local-dynamic"
1450 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001451 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001452 return;
1453 }
1454
Michael Han99315932013-01-24 16:46:58 +00001455 D->addAttr(::new (S.Context)
1456 TLSModelAttr(Attr.getRange(), S.Context, Model,
1457 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001458}
1459
Chandler Carruthedc2c642011-07-02 00:01:44 +00001460static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001461 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001462 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001463 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001464 D->addAttr(::new (S.Context)
1465 MallocAttr(Attr.getRange(), S.Context,
1466 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001467 return;
1468 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001469 }
1470
Ted Kremenek08479ae2009-08-15 00:51:46 +00001471 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001472}
1473
Chandler Carruthedc2c642011-07-02 00:01:44 +00001474static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001475 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001476 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1477 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001478 return;
1479 }
1480
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001481 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1482 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001483}
1484
Chandler Carruthedc2c642011-07-02 00:01:44 +00001485static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001486 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001487
1488 if (S.CheckNoReturnAttr(attr)) return;
1489
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001490 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001491 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001492 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001493 return;
1494 }
1495
Michael Han99315932013-01-24 16:46:58 +00001496 D->addAttr(::new (S.Context)
1497 NoReturnAttr(attr.getRange(), S.Context,
1498 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001499}
1500
1501bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001502 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001503 attr.setInvalid();
1504 return true;
1505 }
1506
1507 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001508}
1509
Chandler Carruthedc2c642011-07-02 00:01:44 +00001510static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1511 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001512
1513 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1514 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001515 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1516 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001517 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1518 && !VD->getType()->isFunctionPointerType())) {
1519 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001520 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001521 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001522 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001523 return;
1524 }
1525 }
1526
Michael Han99315932013-01-24 16:46:58 +00001527 D->addAttr(::new (S.Context)
1528 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1529 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001530}
1531
John Thompsoncdb847ba2010-08-09 21:53:52 +00001532// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001533static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001534/*
1535 Returning a Vector Class in Registers
1536
Eric Christopherbc638a82010-12-01 22:13:54 +00001537 According to the PPU ABI specifications, a class with a single member of
1538 vector type is returned in memory when used as the return value of a function.
1539 This results in inefficient code when implementing vector classes. To return
1540 the value in a single vector register, add the vecreturn attribute to the
1541 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001542
1543 Example:
1544
1545 struct Vector
1546 {
1547 __vector float xyzw;
1548 } __attribute__((vecreturn));
1549
1550 Vector Add(Vector lhs, Vector rhs)
1551 {
1552 Vector result;
1553 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1554 return result; // This will be returned in a register
1555 }
1556*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001557 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1558 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001559 return;
1560 }
1561
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001562 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001563 int count = 0;
1564
1565 if (!isa<CXXRecordDecl>(record)) {
1566 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1567 return;
1568 }
1569
1570 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1571 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1572 return;
1573 }
1574
Eric Christopherbc638a82010-12-01 22:13:54 +00001575 for (RecordDecl::field_iterator iter = record->field_begin();
1576 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001577 if ((count == 1) || !iter->getType()->isVectorType()) {
1578 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1579 return;
1580 }
1581 count++;
1582 }
1583
Michael Han99315932013-01-24 16:46:58 +00001584 D->addAttr(::new (S.Context)
1585 VecReturnAttr(Attr.getRange(), S.Context,
1586 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001587}
1588
Richard Smithe233fbf2013-01-28 22:42:45 +00001589static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1590 const AttributeList &Attr) {
1591 if (isa<ParmVarDecl>(D)) {
1592 // [[carries_dependency]] can only be applied to a parameter if it is a
1593 // parameter of a function declaration or lambda.
1594 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1595 S.Diag(Attr.getLoc(),
1596 diag::err_carries_dependency_param_not_function_decl);
1597 return;
1598 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001599 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001600
1601 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1602 Attr.getRange(), S.Context,
1603 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001604}
1605
Chandler Carruthedc2c642011-07-02 00:01:44 +00001606static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001607 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001608 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001609 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001610 return;
1611 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001612 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001613 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001614 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001615 return;
1616 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001617
Michael Han99315932013-01-24 16:46:58 +00001618 D->addAttr(::new (S.Context)
1619 UsedAttr(Attr.getRange(), S.Context,
1620 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001621}
1622
Chandler Carruthedc2c642011-07-02 00:01:44 +00001623static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001624 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001625 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001626 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1627 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001628 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001629 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001630
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001631 uint32_t priority = ConstructorAttr::DefaultPriority;
1632 if (Attr.getNumArgs() > 0 &&
1633 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1634 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001635
Michael Han99315932013-01-24 16:46:58 +00001636 D->addAttr(::new (S.Context)
1637 ConstructorAttr(Attr.getRange(), S.Context, priority,
1638 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001639}
1640
Chandler Carruthedc2c642011-07-02 00:01:44 +00001641static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001642 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001643 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001644 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1645 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001646 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001647 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001648
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001649 uint32_t priority = ConstructorAttr::DefaultPriority;
1650 if (Attr.getNumArgs() > 0 &&
1651 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1652 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001653
Michael Han99315932013-01-24 16:46:58 +00001654 D->addAttr(::new (S.Context)
1655 DestructorAttr(Attr.getRange(), S.Context, priority,
1656 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001657}
1658
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001659template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001660static void handleAttrWithMessage(Sema &S, Decl *D,
1661 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001662 unsigned NumArgs = Attr.getNumArgs();
1663 if (NumArgs > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001664 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1665 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001666 return;
1667 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001668
1669 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001670 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001671 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001672 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001673
Michael Han99315932013-01-24 16:46:58 +00001674 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1675 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001676}
1677
Ted Kremenek28eace62013-11-23 01:01:34 +00001678static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
1679 const AttributeList &Attr) {
Ted Kremenek28eace62013-11-23 01:01:34 +00001680 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001681 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1682 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001683}
1684
Jordy Rose740b0c22012-05-08 03:27:22 +00001685static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1686 IdentifierInfo *Platform,
1687 VersionTuple Introduced,
1688 VersionTuple Deprecated,
1689 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001690 StringRef PlatformName
1691 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1692 if (PlatformName.empty())
1693 PlatformName = Platform->getName();
1694
1695 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1696 // of these steps are needed).
1697 if (!Introduced.empty() && !Deprecated.empty() &&
1698 !(Introduced <= Deprecated)) {
1699 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1700 << 1 << PlatformName << Deprecated.getAsString()
1701 << 0 << Introduced.getAsString();
1702 return true;
1703 }
1704
1705 if (!Introduced.empty() && !Obsoleted.empty() &&
1706 !(Introduced <= Obsoleted)) {
1707 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1708 << 2 << PlatformName << Obsoleted.getAsString()
1709 << 0 << Introduced.getAsString();
1710 return true;
1711 }
1712
1713 if (!Deprecated.empty() && !Obsoleted.empty() &&
1714 !(Deprecated <= Obsoleted)) {
1715 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1716 << 2 << PlatformName << Obsoleted.getAsString()
1717 << 1 << Deprecated.getAsString();
1718 return true;
1719 }
1720
1721 return false;
1722}
1723
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001724/// \brief Check whether the two versions match.
1725///
1726/// If either version tuple is empty, then they are assumed to match. If
1727/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1728static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1729 bool BeforeIsOkay) {
1730 if (X.empty() || Y.empty())
1731 return true;
1732
1733 if (X == Y)
1734 return true;
1735
1736 if (BeforeIsOkay && X < Y)
1737 return true;
1738
1739 return false;
1740}
1741
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001742AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001743 IdentifierInfo *Platform,
1744 VersionTuple Introduced,
1745 VersionTuple Deprecated,
1746 VersionTuple Obsoleted,
1747 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001748 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001749 bool Override,
1750 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001751 VersionTuple MergedIntroduced = Introduced;
1752 VersionTuple MergedDeprecated = Deprecated;
1753 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001754 bool FoundAny = false;
1755
Rafael Espindolac67f2232012-05-10 02:50:16 +00001756 if (D->hasAttrs()) {
1757 AttrVec &Attrs = D->getAttrs();
1758 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1759 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1760 if (!OldAA) {
1761 ++i;
1762 continue;
1763 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001764
Rafael Espindolac67f2232012-05-10 02:50:16 +00001765 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1766 if (OldPlatform != Platform) {
1767 ++i;
1768 continue;
1769 }
1770
1771 FoundAny = true;
1772 VersionTuple OldIntroduced = OldAA->getIntroduced();
1773 VersionTuple OldDeprecated = OldAA->getDeprecated();
1774 VersionTuple OldObsoleted = OldAA->getObsoleted();
1775 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001776
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001777 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1778 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1779 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1780 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001781 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001782 if (Override) {
1783 int Which = -1;
1784 VersionTuple FirstVersion;
1785 VersionTuple SecondVersion;
1786 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1787 Which = 0;
1788 FirstVersion = OldIntroduced;
1789 SecondVersion = Introduced;
1790 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1791 Which = 1;
1792 FirstVersion = Deprecated;
1793 SecondVersion = OldDeprecated;
1794 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1795 Which = 2;
1796 FirstVersion = Obsoleted;
1797 SecondVersion = OldObsoleted;
1798 }
1799
1800 if (Which == -1) {
1801 Diag(OldAA->getLocation(),
1802 diag::warn_mismatched_availability_override_unavail)
1803 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1804 } else {
1805 Diag(OldAA->getLocation(),
1806 diag::warn_mismatched_availability_override)
1807 << Which
1808 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1809 << FirstVersion.getAsString() << SecondVersion.getAsString();
1810 }
1811 Diag(Range.getBegin(), diag::note_overridden_method);
1812 } else {
1813 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1814 Diag(Range.getBegin(), diag::note_previous_attribute);
1815 }
1816
Rafael Espindolac67f2232012-05-10 02:50:16 +00001817 Attrs.erase(Attrs.begin() + i);
1818 --e;
1819 continue;
1820 }
1821
1822 VersionTuple MergedIntroduced2 = MergedIntroduced;
1823 VersionTuple MergedDeprecated2 = MergedDeprecated;
1824 VersionTuple MergedObsoleted2 = MergedObsoleted;
1825
1826 if (MergedIntroduced2.empty())
1827 MergedIntroduced2 = OldIntroduced;
1828 if (MergedDeprecated2.empty())
1829 MergedDeprecated2 = OldDeprecated;
1830 if (MergedObsoleted2.empty())
1831 MergedObsoleted2 = OldObsoleted;
1832
1833 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1834 MergedIntroduced2, MergedDeprecated2,
1835 MergedObsoleted2)) {
1836 Attrs.erase(Attrs.begin() + i);
1837 --e;
1838 continue;
1839 }
1840
1841 MergedIntroduced = MergedIntroduced2;
1842 MergedDeprecated = MergedDeprecated2;
1843 MergedObsoleted = MergedObsoleted2;
1844 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001845 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001846 }
1847
1848 if (FoundAny &&
1849 MergedIntroduced == Introduced &&
1850 MergedDeprecated == Deprecated &&
1851 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001852 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001853
Ted Kremenekb5445722013-04-06 00:34:27 +00001854 // Only create a new attribute if !Override, but we want to do
1855 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001856 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001857 MergedDeprecated, MergedObsoleted) &&
1858 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001859 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1860 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001861 Obsoleted, IsUnavailable, Message,
1862 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001863 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001864 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001865}
1866
Chandler Carruthedc2c642011-07-02 00:01:44 +00001867static void handleAvailabilityAttr(Sema &S, Decl *D,
1868 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001869 if (!checkAttributeNumArgs(S, Attr, 1))
1870 return;
1871 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001872 unsigned Index = Attr.getAttributeSpellingListIndex();
1873
Aaron Ballman00e99962013-08-31 01:11:41 +00001874 IdentifierInfo *II = Platform->Ident;
1875 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1876 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1877 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001878
Rafael Espindolac231fab2013-01-08 21:30:32 +00001879 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1880 if (!ND) {
1881 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1882 return;
1883 }
1884
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001885 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1886 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1887 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001888 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001889 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001890 if (const StringLiteral *SE =
1891 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001892 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001893
Aaron Ballman00e99962013-08-31 01:11:41 +00001894 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001895 Introduced.Version,
1896 Deprecated.Version,
1897 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001898 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001899 /*Override=*/false,
1900 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001901 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001902 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001903}
1904
John McCalld041a9b2013-02-20 01:54:26 +00001905template <class T>
1906static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1907 typename T::VisibilityType value,
1908 unsigned attrSpellingListIndex) {
1909 T *existingAttr = D->getAttr<T>();
1910 if (existingAttr) {
1911 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1912 if (existingValue == value)
1913 return NULL;
1914 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1915 S.Diag(range.getBegin(), diag::note_previous_attribute);
1916 D->dropAttr<T>();
1917 }
1918 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1919}
1920
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001921VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001922 VisibilityAttr::VisibilityType Vis,
1923 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001924 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1925 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001926}
1927
John McCalld041a9b2013-02-20 01:54:26 +00001928TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1929 TypeVisibilityAttr::VisibilityType Vis,
1930 unsigned AttrSpellingListIndex) {
1931 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1932 AttrSpellingListIndex);
1933}
1934
1935static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1936 bool isTypeVisibility) {
1937 // Visibility attributes don't mean anything on a typedef.
1938 if (isa<TypedefNameDecl>(D)) {
1939 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
1940 << Attr.getName();
1941 return;
1942 }
1943
1944 // 'type_visibility' can only go on a type or namespace.
1945 if (isTypeVisibility &&
1946 !(isa<TagDecl>(D) ||
1947 isa<ObjCInterfaceDecl>(D) ||
1948 isa<NamespaceDecl>(D))) {
1949 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
1950 << Attr.getName() << ExpectedTypeOrNamespace;
1951 return;
1952 }
1953
Benjamin Kramer70370212013-09-09 15:08:57 +00001954 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001955 StringRef TypeStr;
1956 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001957 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001958 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001959
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001960 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00001961 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001962 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00001963 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001964 return;
1965 }
Aaron Ballman682ee422013-09-11 19:47:58 +00001966
1967 // Complain about attempts to use protected visibility on targets
1968 // (like Darwin) that don't support it.
1969 if (type == VisibilityAttr::Protected &&
1970 !S.Context.getTargetInfo().hasProtectedVisibility()) {
1971 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1972 type = VisibilityAttr::Default;
1973 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001974
Michael Han99315932013-01-24 16:46:58 +00001975 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00001976 clang::Attr *newAttr;
1977 if (isTypeVisibility) {
1978 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
1979 (TypeVisibilityAttr::VisibilityType) type,
1980 Index);
1981 } else {
1982 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
1983 }
1984 if (newAttr)
1985 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001986}
1987
Chandler Carruthedc2c642011-07-02 00:01:44 +00001988static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1989 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001990 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00001991 if (!Attr.isArgIdent(0)) {
1992 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1993 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00001994 return;
1995 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001996
Aaron Ballman682ee422013-09-11 19:47:58 +00001997 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1998 ObjCMethodFamilyAttr::FamilyKind F;
1999 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2000 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2001 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002002 return;
2003 }
2004
Aaron Ballman682ee422013-09-11 19:47:58 +00002005 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002006 !method->getResultType()->isObjCObjectPointerType()) {
2007 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2008 << method->getResultType();
2009 // Ignore the attribute.
2010 return;
2011 }
2012
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002013 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00002014 S.Context, F,
2015 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002016}
2017
Chandler Carruthedc2c642011-07-02 00:01:44 +00002018static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002019 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002020 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002021 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002022 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2023 return;
2024 }
2025 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002026 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2027 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002028 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002029 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2030 return;
2031 }
2032 }
2033 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002034 // It is okay to include this attribute on properties, e.g.:
2035 //
2036 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2037 //
2038 // In this case it follows tradition and suppresses an error in the above
2039 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002040 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002041 }
Michael Han99315932013-01-24 16:46:58 +00002042 D->addAttr(::new (S.Context)
2043 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2044 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002045}
2046
Chandler Carruthedc2c642011-07-02 00:01:44 +00002047static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002048 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002049 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002050 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002051 return;
2052 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002053
Aaron Ballman00e99962013-08-31 01:11:41 +00002054 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002055 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002056 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2057 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2058 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002059 return;
2060 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002061
Michael Han99315932013-01-24 16:46:58 +00002062 D->addAttr(::new (S.Context)
2063 BlocksAttr(Attr.getRange(), S.Context, type,
2064 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002065}
2066
Chandler Carruthedc2c642011-07-02 00:01:44 +00002067static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002068 // check the attribute arguments.
2069 if (Attr.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00002070 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
2071 << Attr.getName() << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002072 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002073 }
2074
Aaron Ballman18a78382013-11-21 00:28:23 +00002075 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002076 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002077 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002078 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002079 if (E->isTypeDependent() || E->isValueDependent() ||
2080 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002081 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002082 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002083 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002084 return;
2085 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002086
John McCallb46f2872011-09-09 07:56:05 +00002087 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002088 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2089 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002090 return;
2091 }
John McCallb46f2872011-09-09 07:56:05 +00002092
2093 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002094 }
2095
Aaron Ballman18a78382013-11-21 00:28:23 +00002096 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002097 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002098 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002099 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002100 if (E->isTypeDependent() || E->isValueDependent() ||
2101 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002102 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002103 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002104 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002105 return;
2106 }
2107 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002108
John McCallb46f2872011-09-09 07:56:05 +00002109 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002110 // FIXME: This error message could be improved, it would be nice
2111 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002112 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2113 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002114 return;
2115 }
2116 }
2117
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002118 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002119 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002120 if (isa<FunctionNoProtoType>(FT)) {
2121 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2122 return;
2123 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002124
Chris Lattner9363e312009-03-17 23:03:47 +00002125 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002126 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002127 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002128 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002129 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002130 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002131 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002132 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002133 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002134 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2135 if (!BD->isVariadic()) {
2136 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2137 return;
2138 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002139 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002140 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002141 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002142 const FunctionType *FT = Ty->isFunctionPointerType()
2143 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002144 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002145 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002146 int m = Ty->isFunctionPointerType() ? 0 : 1;
2147 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002148 return;
2149 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002150 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002151 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002152 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002153 return;
2154 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002155 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002156 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002157 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002158 return;
2159 }
Michael Han99315932013-01-24 16:46:58 +00002160 D->addAttr(::new (S.Context)
2161 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2162 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002163}
2164
Chandler Carruthedc2c642011-07-02 00:01:44 +00002165static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002166 if (D->getFunctionType() && D->getFunctionType()->getResultType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002167 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2168 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002169 return;
2170 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002171 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2172 if (MD->getResultType()->isVoidType()) {
2173 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2174 << Attr.getName() << 1;
2175 return;
2176 }
2177
Michael Han99315932013-01-24 16:46:58 +00002178 D->addAttr(::new (S.Context)
2179 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2180 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002181}
2182
Chandler Carruthedc2c642011-07-02 00:01:44 +00002183static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002184 // weak_import only applies to variable & function declarations.
2185 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002186 if (!D->canBeWeakImported(isDef)) {
2187 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002188 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2189 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002190 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002191 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002192 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002193 // Nothing to warn about here.
2194 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002195 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002196 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002197
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002198 return;
2199 }
2200
Michael Han99315932013-01-24 16:46:58 +00002201 D->addAttr(::new (S.Context)
2202 WeakImportAttr(Attr.getRange(), S.Context,
2203 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002204}
2205
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002206// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002207template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002208static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002209 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002210 uint32_t WGSize[3];
2211 for (unsigned i = 0; i < 3; ++i)
2212 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002213 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002214
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002215 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2216 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2217 Existing->getYDim() == WGSize[1] &&
2218 Existing->getZDim() == WGSize[2]))
2219 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002220
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002221 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2222 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002223 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002224}
2225
Joey Goulyaba589c2013-03-08 09:42:32 +00002226static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002227 if (!Attr.hasParsedType()) {
2228 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2229 << Attr.getName() << 1;
2230 return;
2231 }
2232
Richard Smithb87c4652013-10-31 21:23:20 +00002233 TypeSourceInfo *ParmTSI = 0;
2234 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2235 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002236
2237 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2238 (ParmType->isBooleanType() ||
2239 !ParmType->isIntegralType(S.getASTContext()))) {
2240 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2241 << ParmType;
2242 return;
2243 }
2244
Aaron Ballmana9e05402013-12-02 22:16:55 +00002245 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002246 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002247 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2248 return;
2249 }
2250 }
2251
2252 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002253 ParmTSI,
2254 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002255}
2256
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002257SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002258 StringRef Name,
2259 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002260 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2261 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002262 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002263 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2264 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002265 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002266 }
Michael Han99315932013-01-24 16:46:58 +00002267 return ::new (Context) SectionAttr(Range, Context, Name,
2268 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002269}
2270
Chandler Carruthedc2c642011-07-02 00:01:44 +00002271static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002272 // Make sure that there is a string literal as the sections's single
2273 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002274 StringRef Str;
2275 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002276 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002277 return;
Mike Stump11289f42009-09-09 15:08:12 +00002278
Chris Lattner30ba6742009-08-10 19:03:04 +00002279 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002280 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002281 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002282 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002283 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002284 return;
2285 }
Mike Stump11289f42009-09-09 15:08:12 +00002286
Michael Han99315932013-01-24 16:46:58 +00002287 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002288 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002289 if (NewAttr)
2290 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002291}
2292
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002293
Chandler Carruthedc2c642011-07-02 00:01:44 +00002294static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002295 VarDecl *VD = cast<VarDecl>(D);
2296 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002297 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002298 return;
2299 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002300
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002301 Expr *E = Attr.getArgAsExpr(0);
2302 SourceLocation Loc = E->getExprLoc();
2303 FunctionDecl *FD = 0;
2304 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002305
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002306 // gcc only allows for simple identifiers. Since we support more than gcc, we
2307 // will warn the user.
2308 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2309 if (DRE->hasQualifier())
2310 S.Diag(Loc, diag::warn_cleanup_ext);
2311 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2312 NI = DRE->getNameInfo();
2313 if (!FD) {
2314 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2315 << NI.getName();
2316 return;
2317 }
2318 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2319 if (ULE->hasExplicitTemplateArgs())
2320 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002321 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2322 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002323 if (!FD) {
2324 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2325 << NI.getName();
2326 if (ULE->getType() == S.Context.OverloadTy)
2327 S.NoteAllOverloadCandidates(ULE);
2328 return;
2329 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002330 } else {
2331 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002332 return;
2333 }
2334
Anders Carlssond277d792009-01-31 01:16:18 +00002335 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002336 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2337 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002338 return;
2339 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002340
Anders Carlsson723f55d2009-02-07 23:16:50 +00002341 // We're currently more strict than GCC about what function types we accept.
2342 // If this ever proves to be a problem it should be easy to fix.
2343 QualType Ty = S.Context.getPointerType(VD->getType());
2344 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002345 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2346 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002347 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2348 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002349 return;
2350 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002351
Michael Han99315932013-01-24 16:46:58 +00002352 D->addAttr(::new (S.Context)
2353 CleanupAttr(Attr.getRange(), S.Context, FD,
2354 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002355}
2356
Mike Stumpd3bb5572009-07-24 19:02:52 +00002357/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002358/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002359static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002360 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002361 uint64_t ArgIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002362 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002363 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002364
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002365 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002366 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002367
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002368 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2369 if (not_nsstring_type &&
2370 !isCFStringType(Ty, S.Context) &&
2371 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002372 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002373 // FIXME: Should highlight the actual expression that has the wrong type.
2374 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002375 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002376 << IdxExpr->getSourceRange();
2377 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002378 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002379 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002380 if (!isNSStringType(Ty, S.Context) &&
2381 !isCFStringType(Ty, S.Context) &&
2382 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002383 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002384 // FIXME: Should highlight the actual expression that has the wrong type.
2385 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002386 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002387 << IdxExpr->getSourceRange();
2388 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002389 }
2390
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002391 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2392 // because that has corrected for the implicit this parameter, and is zero-
2393 // based. The attribute expects what the user wrote explicitly.
2394 llvm::APSInt Val;
2395 IdxExpr->EvaluateAsInt(Val, S.Context);
2396
Michael Han99315932013-01-24 16:46:58 +00002397 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002398 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002399 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002400}
2401
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002402enum FormatAttrKind {
2403 CFStringFormat,
2404 NSStringFormat,
2405 StrftimeFormat,
2406 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002407 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002408 InvalidFormat
2409};
2410
2411/// getFormatAttrKind - Map from format attribute names to supported format
2412/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002413static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002414 return llvm::StringSwitch<FormatAttrKind>(Format)
2415 // Check for formats that get handled specially.
2416 .Case("NSString", NSStringFormat)
2417 .Case("CFString", CFStringFormat)
2418 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002419
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002420 // Otherwise, check for supported formats.
2421 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2422 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2423 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002424
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002425 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2426 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002427}
2428
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002429/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002430/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002431static void handleInitPriorityAttr(Sema &S, Decl *D,
2432 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002433 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002434 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2435 return;
2436 }
2437
Aaron Ballman4a611152013-11-27 16:34:09 +00002438 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002439 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2440 Attr.setInvalid();
2441 return;
2442 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002443 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002444 if (S.Context.getAsArrayType(T))
2445 T = S.Context.getBaseElementType(T);
2446 if (!T->getAs<RecordType>()) {
2447 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2448 Attr.setInvalid();
2449 return;
2450 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002451
2452 Expr *E = Attr.getArgAsExpr(0);
2453 uint32_t prioritynum;
2454 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002455 Attr.setInvalid();
2456 return;
2457 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002458
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002459 if (prioritynum < 101 || prioritynum > 65535) {
2460 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002461 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002462 Attr.setInvalid();
2463 return;
2464 }
Michael Han99315932013-01-24 16:46:58 +00002465 D->addAttr(::new (S.Context)
2466 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2467 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002468}
2469
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002470FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2471 IdentifierInfo *Format, int FormatIdx,
2472 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002473 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002474 // Check whether we already have an equivalent format attribute.
2475 for (specific_attr_iterator<FormatAttr>
2476 i = D->specific_attr_begin<FormatAttr>(),
2477 e = D->specific_attr_end<FormatAttr>();
2478 i != e ; ++i) {
2479 FormatAttr *f = *i;
2480 if (f->getType() == Format &&
2481 f->getFormatIdx() == FormatIdx &&
2482 f->getFirstArg() == FirstArg) {
2483 // If we don't have a valid location for this attribute, adopt the
2484 // location.
2485 if (f->getLocation().isInvalid())
2486 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002487 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002488 }
2489 }
2490
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002491 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2492 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002493}
2494
Mike Stumpd3bb5572009-07-24 19:02:52 +00002495/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002496/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002497static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002498 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002499 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002500 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002501 return;
2502 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002503
Chandler Carruth743682b2010-11-16 08:35:43 +00002504 // In C++ the implicit 'this' function parameter also counts, and they are
2505 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002506 bool HasImplicitThisParam = isInstanceMethod(D);
2507 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002508
Aaron Ballman00e99962013-08-31 01:11:41 +00002509 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2510 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002511
2512 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002513 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002514 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002515 // If we've modified the string name, we need a new identifier for it.
2516 II = &S.Context.Idents.get(Format);
2517 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002518
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002519 // Check for supported formats.
2520 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002521
2522 if (Kind == IgnoredFormat)
2523 return;
2524
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002525 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002526 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002527 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002528 return;
2529 }
2530
2531 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002532 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002533 uint32_t Idx;
2534 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002535 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002536
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002537 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002538 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002539 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002540 return;
2541 }
2542
2543 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002544 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002545
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002546 if (HasImplicitThisParam) {
2547 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002548 S.Diag(Attr.getLoc(),
2549 diag::err_format_attribute_implicit_this_format_string)
2550 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002551 return;
2552 }
2553 ArgIdx--;
2554 }
Mike Stump11289f42009-09-09 15:08:12 +00002555
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002556 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002557 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002558
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002559 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002560 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002561 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2562 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002563 return;
2564 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002565 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002566 // FIXME: do we need to check if the type is NSString*? What are the
2567 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002568 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002569 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002570 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2571 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002572 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002573 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002574 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002575 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002576 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002577 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2578 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002579 return;
2580 }
2581
2582 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002583 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002584 uint32_t FirstArg;
2585 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002586 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002587
2588 // check if the function is variadic if the 3rd argument non-zero
2589 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002590 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002591 ++NumArgs; // +1 for ...
2592 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002593 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002594 return;
2595 }
2596 }
2597
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002598 // strftime requires FirstArg to be 0 because it doesn't read from any
2599 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002600 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002601 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002602 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2603 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002604 return;
2605 }
2606 // if 0 it disables parameter checking (to use with e.g. va_list)
2607 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002608 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002609 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002610 return;
2611 }
2612
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002613 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002614 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002615 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002616 if (NewAttr)
2617 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002618}
2619
Chandler Carruthedc2c642011-07-02 00:01:44 +00002620static void handleTransparentUnionAttr(Sema &S, Decl *D,
2621 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002622 // Try to find the underlying union declaration.
2623 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002624 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002625 if (TD && TD->getUnderlyingType()->isUnionType())
2626 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2627 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002628 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002629
2630 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002631 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002632 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002633 return;
2634 }
2635
John McCallf937c022011-10-07 06:10:15 +00002636 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002637 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002638 diag::warn_transparent_union_attribute_not_definition);
2639 return;
2640 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002641
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002642 RecordDecl::field_iterator Field = RD->field_begin(),
2643 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002644 if (Field == FieldEnd) {
2645 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2646 return;
2647 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002648
David Blaikie40ed2972012-06-06 20:45:41 +00002649 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002650 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002651 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002652 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002653 diag::warn_transparent_union_attribute_floating)
2654 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002655 return;
2656 }
2657
2658 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2659 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2660 for (; Field != FieldEnd; ++Field) {
2661 QualType FieldType = Field->getType();
2662 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2663 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2664 // Warn if we drop the attribute.
2665 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002666 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002667 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002668 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002669 diag::warn_transparent_union_attribute_field_size_align)
2670 << isSize << Field->getDeclName() << FieldBits;
2671 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002672 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002673 diag::note_transparent_union_first_field_size_align)
2674 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002675 return;
2676 }
2677 }
2678
Michael Han99315932013-01-24 16:46:58 +00002679 RD->addAttr(::new (S.Context)
2680 TransparentUnionAttr(Attr.getRange(), S.Context,
2681 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002682}
2683
Chandler Carruthedc2c642011-07-02 00:01:44 +00002684static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002685 // Make sure that there is a string literal as the annotation's single
2686 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002687 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002688 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002689 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002690
2691 // Don't duplicate annotations that are already set.
2692 for (specific_attr_iterator<AnnotateAttr>
2693 i = D->specific_attr_begin<AnnotateAttr>(),
2694 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002695 if ((*i)->getAnnotation() == Str)
2696 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002697 }
Michael Han99315932013-01-24 16:46:58 +00002698
2699 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002700 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002701 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002702}
2703
Chandler Carruthedc2c642011-07-02 00:01:44 +00002704static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002705 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002706 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002707 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2708 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002709 return;
2710 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002711
Richard Smith848e1f12013-02-01 08:12:08 +00002712 if (Attr.getNumArgs() == 0) {
2713 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2714 true, 0, Attr.getAttributeSpellingListIndex()));
2715 return;
2716 }
2717
Aaron Ballman00e99962013-08-31 01:11:41 +00002718 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002719 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2720 S.Diag(Attr.getEllipsisLoc(),
2721 diag::err_pack_expansion_without_parameter_packs);
2722 return;
2723 }
2724
2725 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2726 return;
2727
2728 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2729 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002730}
2731
2732void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002733 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002734 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2735 SourceLocation AttrLoc = AttrRange.getBegin();
2736
Richard Smith1dba27c2013-01-29 09:02:09 +00002737 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002738 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002739 // C++11 [dcl.align]p1:
2740 // An alignment-specifier may be applied to a variable or to a class
2741 // data member, but it shall not be applied to a bit-field, a function
2742 // parameter, the formal parameter of a catch clause, or a variable
2743 // declared with the register storage class specifier. An
2744 // alignment-specifier may also be applied to the declaration of a class
2745 // or enumeration type.
2746 // C11 6.7.5/2:
2747 // An alignment attribute shall not be specified in a declaration of
2748 // a typedef, or a bit-field, or a function, or a parameter, or an
2749 // object declared with the register storage-class specifier.
2750 int DiagKind = -1;
2751 if (isa<ParmVarDecl>(D)) {
2752 DiagKind = 0;
2753 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2754 if (VD->getStorageClass() == SC_Register)
2755 DiagKind = 1;
2756 if (VD->isExceptionVariable())
2757 DiagKind = 2;
2758 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2759 if (FD->isBitField())
2760 DiagKind = 3;
2761 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00002762 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00002763 << (TmpAttr.isC11() ? ExpectedVariableOrField
2764 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002765 return;
2766 }
2767 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002768 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00002769 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002770 return;
2771 }
2772 }
2773
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002774 if (E->isTypeDependent() || E->isValueDependent()) {
2775 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002776 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2777 AA->setPackExpansion(IsPackExpansion);
2778 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002779 return;
2780 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002781
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002782 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002783 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002784 ExprResult ICE
2785 = VerifyIntegerConstantExpression(E, &Alignment,
2786 diag::err_aligned_attribute_argument_not_int,
2787 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002788 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002789 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002790
2791 // C++11 [dcl.align]p2:
2792 // -- if the constant expression evaluates to zero, the alignment
2793 // specifier shall have no effect
2794 // C11 6.7.5p6:
2795 // An alignment specification of zero has no effect.
2796 if (!(TmpAttr.isAlignas() && !Alignment) &&
2797 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002798 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2799 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002800 return;
2801 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002802
Richard Smith848e1f12013-02-01 08:12:08 +00002803 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00002804 // We've already verified it's a power of 2, now let's make sure it's
2805 // 8192 or less.
2806 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00002807 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00002808 << E->getSourceRange();
2809 return;
2810 }
2811 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002812
Richard Smith44c247f2013-02-22 08:32:16 +00002813 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2814 ICE.take(), SpellingListIndex);
2815 AA->setPackExpansion(IsPackExpansion);
2816 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002817}
2818
Michael Hanaf02bbe2013-02-01 01:19:17 +00002819void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002820 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002821 // FIXME: Cache the number on the Attr object if non-dependent?
2822 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002823 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2824 SpellingListIndex);
2825 AA->setPackExpansion(IsPackExpansion);
2826 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002827}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002828
Richard Smith848e1f12013-02-01 08:12:08 +00002829void Sema::CheckAlignasUnderalignment(Decl *D) {
2830 assert(D->hasAttrs() && "no attributes on decl");
2831
2832 QualType Ty;
2833 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2834 Ty = VD->getType();
2835 else
2836 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002837 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002838 return;
2839
2840 // C++11 [dcl.align]p5, C11 6.7.5/4:
2841 // The combined effect of all alignment attributes in a declaration shall
2842 // not specify an alignment that is less strict than the alignment that
2843 // would otherwise be required for the entity being declared.
2844 AlignedAttr *AlignasAttr = 0;
2845 unsigned Align = 0;
2846 for (specific_attr_iterator<AlignedAttr>
2847 I = D->specific_attr_begin<AlignedAttr>(),
2848 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
2849 if (I->isAlignmentDependent())
2850 return;
2851 if (I->isAlignas())
2852 AlignasAttr = *I;
2853 Align = std::max(Align, I->getAlignment(Context));
2854 }
2855
2856 if (AlignasAttr && Align) {
2857 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2858 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2859 if (NaturalAlign > RequestedAlign)
2860 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2861 << Ty << (unsigned)NaturalAlign.getQuantity();
2862 }
2863}
2864
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002865/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002866/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002867///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002868/// Despite what would be logical, the mode attribute is a decl attribute, not a
2869/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2870/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002871static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002872 // This attribute isn't documented, but glibc uses it. It changes
2873 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002874 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002875 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2876 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002877 return;
2878 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002879
Aaron Ballman00e99962013-08-31 01:11:41 +00002880 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2881 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002882
2883 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002884 if (Str.startswith("__") && Str.endswith("__"))
2885 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002886
2887 unsigned DestWidth = 0;
2888 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002889 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002890 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002891 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002892 switch (Str[0]) {
2893 case 'Q': DestWidth = 8; break;
2894 case 'H': DestWidth = 16; break;
2895 case 'S': DestWidth = 32; break;
2896 case 'D': DestWidth = 64; break;
2897 case 'X': DestWidth = 96; break;
2898 case 'T': DestWidth = 128; break;
2899 }
2900 if (Str[1] == 'F') {
2901 IntegerMode = false;
2902 } else if (Str[1] == 'C') {
2903 IntegerMode = false;
2904 ComplexMode = true;
2905 } else if (Str[1] != 'I') {
2906 DestWidth = 0;
2907 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002908 break;
2909 case 4:
2910 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2911 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002912 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002913 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002914 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002915 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002916 break;
2917 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002918 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002919 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002920 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002921 case 11:
2922 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00002923 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002924 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002925 }
2926
2927 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002928 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002929 OldTy = TD->getUnderlyingType();
2930 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2931 OldTy = VD->getType();
2932 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002933 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00002934 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002935 return;
2936 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002937
John McCall9dd450b2009-09-21 23:43:11 +00002938 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002939 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2940 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002941 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002942 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2943 } else if (ComplexMode) {
2944 if (!OldTy->isComplexType())
2945 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2946 } else {
2947 if (!OldTy->isFloatingType())
2948 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2949 }
2950
Mike Stump87c57ac2009-05-16 07:39:55 +00002951 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2952 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002953 // FIXME: Make sure floating-point mappings are accurate
2954 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002955 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00002956 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002957 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002958 }
2959
2960 QualType NewTy;
2961
2962 if (IntegerMode)
2963 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
2964 OldTy->isSignedIntegerType());
2965 else
2966 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
2967
2968 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00002969 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002970 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002971 }
2972
Eli Friedman4735374e2009-03-03 06:41:03 +00002973 if (ComplexMode) {
2974 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002975 }
2976
2977 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002978 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
2979 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
2980 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00002981 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002982
2983 D->addAttr(::new (S.Context)
2984 ModeAttr(Attr.getRange(), S.Context, Name,
2985 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00002986}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002987
Chandler Carruthedc2c642011-07-02 00:01:44 +00002988static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00002989 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2990 if (!VD->hasGlobalStorage())
2991 S.Diag(Attr.getLoc(),
2992 diag::warn_attribute_requires_functions_or_static_globals)
2993 << Attr.getName();
2994 } else if (!isFunctionOrMethod(D)) {
2995 S.Diag(Attr.getLoc(),
2996 diag::warn_attribute_requires_functions_or_static_globals)
2997 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00002998 return;
2999 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003000
Michael Han99315932013-01-24 16:46:58 +00003001 D->addAttr(::new (S.Context)
3002 NoDebugAttr(Attr.getRange(), S.Context,
3003 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003004}
3005
Chandler Carruthedc2c642011-07-02 00:01:44 +00003006static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003007 FunctionDecl *FD = cast<FunctionDecl>(D);
3008 if (!FD->getResultType()->isVoidType()) {
3009 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
3010 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
3011 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3012 << FD->getType()
3013 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
3014 "void");
3015 } else {
3016 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3017 << FD->getType();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003018 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003019 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003020 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003021
Aaron Ballman3aff6332013-12-02 19:30:36 +00003022 D->addAttr(::new (S.Context)
3023 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003024 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003025}
3026
Chandler Carruthedc2c642011-07-02 00:01:44 +00003027static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003028 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003029 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003030 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003031 return;
3032 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003033
Michael Han99315932013-01-24 16:46:58 +00003034 D->addAttr(::new (S.Context)
3035 GNUInlineAttr(Attr.getRange(), S.Context,
3036 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003037}
3038
Chandler Carruthedc2c642011-07-02 00:01:44 +00003039static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003040 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003041
Aaron Ballman02df2e02012-12-09 17:45:41 +00003042 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003043 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003044 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3045 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003046 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003047 return;
3048
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003049 if (!isa<ObjCMethodDecl>(D)) {
3050 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3051 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003052 return;
3053 }
3054
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003055 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003056 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003057 D->addAttr(::new (S.Context)
3058 FastCallAttr(Attr.getRange(), S.Context,
3059 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003060 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003061 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003062 D->addAttr(::new (S.Context)
3063 StdCallAttr(Attr.getRange(), S.Context,
3064 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003065 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003066 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003067 D->addAttr(::new (S.Context)
3068 ThisCallAttr(Attr.getRange(), S.Context,
3069 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003070 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003071 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003072 D->addAttr(::new (S.Context)
3073 CDeclAttr(Attr.getRange(), S.Context,
3074 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003075 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003076 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003077 D->addAttr(::new (S.Context)
3078 PascalAttr(Attr.getRange(), S.Context,
3079 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003080 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003081 case AttributeList::AT_MSABI:
3082 D->addAttr(::new (S.Context)
3083 MSABIAttr(Attr.getRange(), S.Context,
3084 Attr.getAttributeSpellingListIndex()));
3085 return;
3086 case AttributeList::AT_SysVABI:
3087 D->addAttr(::new (S.Context)
3088 SysVABIAttr(Attr.getRange(), S.Context,
3089 Attr.getAttributeSpellingListIndex()));
3090 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003091 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003092 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003093 switch (CC) {
3094 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003095 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003096 break;
3097 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003098 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003099 break;
3100 default:
3101 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003102 }
3103
Michael Han99315932013-01-24 16:46:58 +00003104 D->addAttr(::new (S.Context)
3105 PcsAttr(Attr.getRange(), S.Context, PCS,
3106 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003107 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003108 }
Derek Schuffa2020962012-10-16 22:30:41 +00003109 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003110 D->addAttr(::new (S.Context)
3111 PnaclCallAttr(Attr.getRange(), S.Context,
3112 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003113 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003114 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003115 D->addAttr(::new (S.Context)
3116 IntelOclBiccAttr(Attr.getRange(), S.Context,
3117 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003118 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003119
Abramo Bagnara50099372010-04-30 13:10:51 +00003120 default:
3121 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003122 }
3123}
3124
Aaron Ballman02df2e02012-12-09 17:45:41 +00003125bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3126 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003127 if (attr.isInvalid())
3128 return true;
3129
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003130 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003131 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003132 attr.setInvalid();
3133 return true;
3134 }
3135
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003136 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003137 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003138 case AttributeList::AT_CDecl: CC = CC_C; break;
3139 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3140 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3141 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3142 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003143 case AttributeList::AT_MSABI:
3144 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3145 CC_X86_64Win64;
3146 break;
3147 case AttributeList::AT_SysVABI:
3148 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3149 CC_C;
3150 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003151 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003152 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003153 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003154 attr.setInvalid();
3155 return true;
3156 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003157 if (StrRef == "aapcs") {
3158 CC = CC_AAPCS;
3159 break;
3160 } else if (StrRef == "aapcs-vfp") {
3161 CC = CC_AAPCS_VFP;
3162 break;
3163 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003164
3165 attr.setInvalid();
3166 Diag(attr.getLoc(), diag::err_invalid_pcs);
3167 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003168 }
Derek Schuffa2020962012-10-16 22:30:41 +00003169 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003170 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003171 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003172 }
3173
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003174 const TargetInfo &TI = Context.getTargetInfo();
3175 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3176 if (A == TargetInfo::CCCR_Warning) {
3177 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003178
3179 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3180 if (FD)
3181 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3182 TargetInfo::CCMT_NonMember;
3183 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003184 }
3185
John McCall3882ace2011-01-05 12:14:39 +00003186 return false;
3187}
3188
John McCall3882ace2011-01-05 12:14:39 +00003189/// Checks a regparm attribute, returning true if it is ill-formed and
3190/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003191bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3192 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003193 return true;
3194
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003195 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003196 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003197 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003198 }
Eli Friedman7044b762009-03-27 21:06:47 +00003199
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003200 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003201 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003202 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003203 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003204 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003205 }
3206
Douglas Gregore8bbc122011-09-02 00:18:52 +00003207 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003208 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003209 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003210 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003211 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003212 }
3213
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003214 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003215 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003216 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003217 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003218 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003219 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003220 }
3221
John McCall3882ace2011-01-05 12:14:39 +00003222 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003223}
3224
Aaron Ballman66039932013-12-19 00:41:31 +00003225static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3226 const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003227 // check the attribute arguments.
3228 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3229 // FIXME: 0 is not okay.
Aaron Ballman05e420a2014-01-02 21:26:14 +00003230 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3231 << Attr.getName() << 2;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003232 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003233 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003234
Aaron Ballman66039932013-12-19 00:41:31 +00003235 uint32_t MaxThreads, MinBlocks = 0;
3236 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3237 return;
3238 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3239 Attr.getArgAsExpr(1),
3240 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003241 return;
3242
3243 D->addAttr(::new (S.Context)
3244 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3245 MaxThreads, MinBlocks,
3246 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003247}
3248
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003249static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3250 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003251 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003252 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003253 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003254 return;
3255 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003256
3257 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003258 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003259
Aaron Ballman00e99962013-08-31 01:11:41 +00003260 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003261
3262 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3263 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3264 << Attr.getName() << ExpectedFunctionOrMethod;
3265 return;
3266 }
3267
3268 uint64_t ArgumentIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003269 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3270 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003271 return;
3272
3273 uint64_t TypeTagIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003274 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3275 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003276 return;
3277
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003278 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003279 if (IsPointer) {
3280 // Ensure that buffer has a pointer type.
3281 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3282 if (!BufferTy->isPointerType()) {
3283 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003284 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003285 }
3286 }
3287
Michael Han99315932013-01-24 16:46:58 +00003288 D->addAttr(::new (S.Context)
3289 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3290 ArgumentIdx, TypeTagIdx, IsPointer,
3291 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003292}
3293
3294static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3295 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003296 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003297 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003298 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003299 return;
3300 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003301
3302 if (!checkAttributeNumArgs(S, Attr, 1))
3303 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003304
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003305 if (!isa<VarDecl>(D)) {
3306 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3307 << Attr.getName() << ExpectedVariable;
3308 return;
3309 }
3310
Aaron Ballman00e99962013-08-31 01:11:41 +00003311 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003312 TypeSourceInfo *MatchingCTypeLoc = 0;
3313 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3314 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003315
Michael Han99315932013-01-24 16:46:58 +00003316 D->addAttr(::new (S.Context)
3317 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003318 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003319 Attr.getLayoutCompatible(),
3320 Attr.getMustBeNull(),
3321 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003322}
3323
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003324//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003325// Checker-specific attribute handlers.
3326//===----------------------------------------------------------------------===//
3327
John McCalled433932011-01-25 03:31:58 +00003328static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003329 return type->isDependentType() ||
3330 type->isObjCObjectPointerType() ||
3331 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003332}
3333static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003334 return type->isDependentType() ||
3335 type->isPointerType() ||
3336 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003337}
3338
Chandler Carruthedc2c642011-07-02 00:01:44 +00003339static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003340 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003341 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003342
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003343 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003344 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3345 cf = false;
3346 } else {
3347 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3348 cf = true;
3349 }
3350
3351 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003352 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003353 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003354 return;
3355 }
3356
3357 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003358 param->addAttr(::new (S.Context)
3359 CFConsumedAttr(Attr.getRange(), S.Context,
3360 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003361 else
Michael Han99315932013-01-24 16:46:58 +00003362 param->addAttr(::new (S.Context)
3363 NSConsumedAttr(Attr.getRange(), S.Context,
3364 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003365}
3366
Chandler Carruthedc2c642011-07-02 00:01:44 +00003367static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3368 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003369
John McCalled433932011-01-25 03:31:58 +00003370 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003371
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003372 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003373 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003374 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003375 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003376 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003377 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3378 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003379 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003380 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003381 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003382 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003383 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003384 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003385 return;
3386 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003387
John McCalled433932011-01-25 03:31:58 +00003388 bool typeOK;
3389 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003390 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003391 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003392 case AttributeList::AT_NSReturnsAutoreleased:
3393 case AttributeList::AT_NSReturnsRetained:
3394 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003395 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3396 cf = false;
3397 break;
3398
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003399 case AttributeList::AT_CFReturnsRetained:
3400 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003401 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3402 cf = true;
3403 break;
3404 }
3405
3406 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003407 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003408 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003409 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003410 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003411
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003412 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003413 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003414 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003415 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003416 D->addAttr(::new (S.Context)
3417 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3418 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003419 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003420 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003421 D->addAttr(::new (S.Context)
3422 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3423 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003424 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003425 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003426 D->addAttr(::new (S.Context)
3427 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3428 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003429 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003430 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003431 D->addAttr(::new (S.Context)
3432 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3433 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003434 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003435 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003436 D->addAttr(::new (S.Context)
3437 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3438 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003439 return;
3440 };
3441}
3442
John McCallcf166702011-07-22 08:53:00 +00003443static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3444 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003445 const int EP_ObjCMethod = 1;
3446 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003447
John McCallcf166702011-07-22 08:53:00 +00003448 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003449 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003450 if (isa<ObjCMethodDecl>(D))
3451 resultType = cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003452 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003453 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003454
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003455 if (!resultType->isReferenceType() &&
3456 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003457 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003458 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003459 << attr.getName()
3460 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003461 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003462
3463 // Drop the attribute.
3464 return;
3465 }
3466
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003467 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003468 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3469 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003470}
3471
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003472static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3473 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003474 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003475
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003476 DeclContext *DC = method->getDeclContext();
3477 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3478 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3479 << attr.getName() << 0;
3480 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3481 return;
3482 }
3483 if (method->getMethodFamily() == OMF_dealloc) {
3484 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3485 << attr.getName() << 1;
3486 return;
3487 }
3488
Michael Han99315932013-01-24 16:46:58 +00003489 method->addAttr(::new (S.Context)
3490 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3491 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003492}
3493
Aaron Ballmanfb763042013-12-02 18:05:46 +00003494static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3495 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003496 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00003497 return;
John McCall32f5fe12011-09-30 05:12:12 +00003498
Aaron Ballmanfb763042013-12-02 18:05:46 +00003499 D->addAttr(::new (S.Context)
3500 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3501 Attr.getAttributeSpellingListIndex()));
3502}
3503
3504static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3505 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003506 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00003507 return;
3508
3509 D->addAttr(::new (S.Context)
3510 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3511 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003512}
3513
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003514static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3515 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003516 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003517
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003518 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003519 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003520 return;
3521 }
3522
3523 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003524 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003525 Attr.getAttributeSpellingListIndex()));
3526}
3527
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003528static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3529 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003530 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003531
3532 if (!Parm) {
3533 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3534 return;
3535 }
3536
3537 D->addAttr(::new (S.Context)
3538 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3539 Attr.getAttributeSpellingListIndex()));
3540}
3541
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003542static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3543 const AttributeList &Attr) {
3544 IdentifierInfo *RelatedClass =
3545 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0;
3546 if (!RelatedClass) {
3547 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3548 return;
3549 }
3550 IdentifierInfo *ClassMethod =
3551 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0;
3552 IdentifierInfo *InstanceMethod =
3553 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0;
3554 D->addAttr(::new (S.Context)
3555 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3556 ClassMethod, InstanceMethod,
3557 Attr.getAttributeSpellingListIndex()));
3558}
3559
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003560static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3561 const AttributeList &Attr) {
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003562 ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003563 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003564 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003565 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3566 Attr.getAttributeSpellingListIndex()));
3567}
3568
Chandler Carruthedc2c642011-07-02 00:01:44 +00003569static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3570 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003571 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003572
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003573 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003574 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003575}
3576
Chandler Carruthedc2c642011-07-02 00:01:44 +00003577static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3578 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003579 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003580 QualType type = vd->getType();
3581
3582 if (!type->isDependentType() &&
3583 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003584 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003585 << type;
3586 return;
3587 }
3588
3589 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3590
3591 // If we have no lifetime yet, check the lifetime we're presumably
3592 // going to infer.
3593 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3594 lifetime = type->getObjCARCImplicitLifetime();
3595
3596 switch (lifetime) {
3597 case Qualifiers::OCL_None:
3598 assert(type->isDependentType() &&
3599 "didn't infer lifetime for non-dependent type?");
3600 break;
3601
3602 case Qualifiers::OCL_Weak: // meaningful
3603 case Qualifiers::OCL_Strong: // meaningful
3604 break;
3605
3606 case Qualifiers::OCL_ExplicitNone:
3607 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003608 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003609 << (lifetime == Qualifiers::OCL_Autoreleasing);
3610 break;
3611 }
3612
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003613 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003614 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3615 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003616}
3617
Francois Picheta83957a2010-12-19 06:50:37 +00003618//===----------------------------------------------------------------------===//
3619// Microsoft specific attribute handlers.
3620//===----------------------------------------------------------------------===//
3621
Chandler Carruthedc2c642011-07-02 00:01:44 +00003622static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003623 if (!S.LangOpts.CPlusPlus) {
3624 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3625 << Attr.getName() << AttributeLangSupport::C;
3626 return;
3627 }
3628
Aaron Ballman60e705e2013-11-24 20:58:02 +00003629 if (!isa<CXXRecordDecl>(D)) {
3630 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3631 << Attr.getName() << ExpectedClass;
3632 return;
3633 }
3634
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003635 StringRef StrRef;
3636 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003637 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003638 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003639
David Majnemer89085342013-08-09 08:56:20 +00003640 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3641 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003642 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3643 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003644
Reid Kleckner140c4a72013-05-17 14:04:52 +00003645 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003646 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003647 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003648 return;
3649 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003650
David Majnemer89085342013-08-09 08:56:20 +00003651 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003652 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003653 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003654 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003655 return;
3656 }
David Majnemer89085342013-08-09 08:56:20 +00003657 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003658 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003659 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003660 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003661 }
Francois Picheta83957a2010-12-19 06:50:37 +00003662
David Majnemer89085342013-08-09 08:56:20 +00003663 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3664 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003665}
3666
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003667static void handleARMInterruptAttr(Sema &S, Decl *D,
3668 const AttributeList &Attr) {
3669 // Check the attribute arguments.
3670 if (Attr.getNumArgs() > 1) {
3671 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3672 << Attr.getName() << 1;
3673 return;
3674 }
3675
3676 StringRef Str;
3677 SourceLocation ArgLoc;
3678
3679 if (Attr.getNumArgs() == 0)
3680 Str = "";
3681 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
3682 return;
3683
3684 ARMInterruptAttr::InterruptType Kind;
3685 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
3686 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
3687 << Attr.getName() << Str << ArgLoc;
3688 return;
3689 }
3690
3691 unsigned Index = Attr.getAttributeSpellingListIndex();
3692 D->addAttr(::new (S.Context)
3693 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
3694}
3695
3696static void handleMSP430InterruptAttr(Sema &S, Decl *D,
3697 const AttributeList &Attr) {
3698 if (!checkAttributeNumArgs(S, Attr, 1))
3699 return;
3700
3701 if (!Attr.isArgExpr(0)) {
3702 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3703 << AANT_ArgumentIntegerConstant;
3704 return;
3705 }
3706
3707 // FIXME: Check for decl - it should be void ()(void).
3708
3709 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3710 llvm::APSInt NumParams(32);
3711 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
3712 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3713 << Attr.getName() << AANT_ArgumentIntegerConstant
3714 << NumParamsExpr->getSourceRange();
3715 return;
3716 }
3717
3718 unsigned Num = NumParams.getLimitedValue(255);
3719 if ((Num & 1) || Num > 30) {
3720 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3721 << Attr.getName() << (int)NumParams.getSExtValue()
3722 << NumParamsExpr->getSourceRange();
3723 return;
3724 }
3725
Aaron Ballman36a53502014-01-16 13:03:14 +00003726 D->addAttr(::new (S.Context)
3727 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
3728 Attr.getAttributeSpellingListIndex()));
3729 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003730}
3731
3732static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3733 // Dispatch the interrupt attribute based on the current target.
3734 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
3735 handleMSP430InterruptAttr(S, D, Attr);
3736 else
3737 handleARMInterruptAttr(S, D, Attr);
3738}
3739
3740static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
3741 const AttributeList& Attr) {
3742 // If we try to apply it to a function pointer, don't warn, but don't
3743 // do anything, either. It doesn't matter anyway, because there's nothing
3744 // special about calling a force_align_arg_pointer function.
3745 ValueDecl *VD = dyn_cast<ValueDecl>(D);
3746 if (VD && VD->getType()->isFunctionPointerType())
3747 return;
3748 // Also don't warn on function pointer typedefs.
3749 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
3750 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
3751 TD->getUnderlyingType()->isFunctionType()))
3752 return;
3753 // Attribute can only be applied to function types.
3754 if (!isa<FunctionDecl>(D)) {
3755 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3756 << Attr.getName() << /* function */0;
3757 return;
3758 }
3759
Aaron Ballman36a53502014-01-16 13:03:14 +00003760 D->addAttr(::new (S.Context)
3761 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
3762 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003763}
3764
3765DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
3766 unsigned AttrSpellingListIndex) {
3767 if (D->hasAttr<DLLExportAttr>()) {
3768 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "dllimport";
3769 return NULL;
3770 }
3771
3772 if (D->hasAttr<DLLImportAttr>())
3773 return NULL;
3774
3775 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3776 if (VD->hasDefinition()) {
3777 // dllimport cannot be applied to definitions.
3778 Diag(D->getLocation(), diag::warn_attribute_invalid_on_definition)
3779 << "dllimport";
3780 return NULL;
3781 }
3782 }
3783
3784 return ::new (Context)DLLImportAttr(Range, Context,
3785 AttrSpellingListIndex);
3786}
3787
3788static void handleDLLImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3789 // Attribute can be applied only to functions or variables.
3790 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3791 if (!FD && !isa<VarDecl>(D)) {
3792 // Apparently Visual C++ thinks it is okay to not emit a warning
3793 // in this case, so only emit a warning when -fms-extensions is not
3794 // specified.
3795 if (!S.getLangOpts().MicrosoftExt)
3796 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3797 << Attr.getName() << 2 /*variable and function*/;
3798 return;
3799 }
3800
3801 // Currently, the dllimport attribute is ignored for inlined functions.
3802 // Warning is emitted.
3803 if (FD && FD->isInlineSpecified()) {
3804 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3805 return;
3806 }
3807
3808 unsigned Index = Attr.getAttributeSpellingListIndex();
3809 DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index);
3810 if (NewAttr)
3811 D->addAttr(NewAttr);
3812}
3813
3814DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
3815 unsigned AttrSpellingListIndex) {
3816 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
3817 Diag(Import->getLocation(), diag::warn_attribute_ignored) << "dllimport";
3818 D->dropAttr<DLLImportAttr>();
3819 }
3820
3821 if (D->hasAttr<DLLExportAttr>())
3822 return NULL;
3823
3824 return ::new (Context)DLLExportAttr(Range, Context,
3825 AttrSpellingListIndex);
3826}
3827
3828static void handleDLLExportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3829 // Currently, the dllexport attribute is ignored for inlined functions, unless
3830 // the -fkeep-inline-functions flag has been used. Warning is emitted;
3831 if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isInlineSpecified()) {
3832 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
3833 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3834 return;
3835 }
3836
3837 unsigned Index = Attr.getAttributeSpellingListIndex();
3838 DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index);
3839 if (NewAttr)
3840 D->addAttr(NewAttr);
3841}
3842
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003843/// Handles semantic checking for features that are common to all attributes,
3844/// such as checking whether a parameter was properly specified, or the correct
3845/// number of arguments were passed, etc.
3846static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3847 const AttributeList &Attr) {
3848 // Several attributes carry different semantics than the parsing requires, so
3849 // those are opted out of the common handling.
3850 //
3851 // We also bail on unknown and ignored attributes because those are handled
3852 // as part of the target-specific handling logic.
3853 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003854 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003855 return false;
3856
Aaron Ballman3aff6332013-12-02 19:30:36 +00003857 // Check whether the attribute requires specific language extensions to be
3858 // enabled.
3859 if (!Attr.diagnoseLangOpts(S))
3860 return true;
3861
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003862 // If there are no optional arguments, then checking for the argument count
3863 // is trivial.
3864 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
3865 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
3866 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003867
3868 // Check whether the attribute appertains to the given subject.
3869 if (!Attr.diagnoseAppertainsTo(S, D))
3870 return true;
3871
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003872 return false;
3873}
3874
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003875//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003876// Top Level Sema Entry Points
3877//===----------------------------------------------------------------------===//
3878
Richard Smithf8a75c32013-08-29 00:47:48 +00003879/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3880/// the attribute applies to decls. If the attribute is a type attribute, just
3881/// silently ignore it if a GNU attribute.
3882static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3883 const AttributeList &Attr,
3884 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003885 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00003886 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003887
Richard Smithf8a75c32013-08-29 00:47:48 +00003888 // Ignore C++11 attributes on declarator chunks: they appertain to the type
3889 // instead.
3890 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
3891 return;
3892
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003893 // Unknown attributes are automatically warned on. Target-specific attributes
3894 // which do not apply to the current target architecture are treated as
3895 // though they were unknown attributes.
3896 if (Attr.getKind() == AttributeList::UnknownAttribute ||
3897 !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) {
3898 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
3899 diag::warn_unhandled_ms_attribute_ignored :
3900 diag::warn_unknown_attribute_ignored) << Attr.getName();
3901 return;
3902 }
3903
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003904 if (handleCommonAttributeFeatures(S, scope, D, Attr))
3905 return;
3906
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003907 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003908 default:
3909 // Type attributes are handled elsewhere; silently move on.
3910 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
3911 break;
3912 case AttributeList::AT_Interrupt:
3913 handleInterruptAttr(S, D, Attr); break;
3914 case AttributeList::AT_X86ForceAlignArgPointer:
3915 handleX86ForceAlignArgPointerAttr(S, D, Attr); break;
3916 case AttributeList::AT_DLLExport:
3917 handleDLLExportAttr(S, D, Attr); break;
3918 case AttributeList::AT_DLLImport:
3919 handleDLLImportAttr(S, D, Attr); break;
3920 case AttributeList::AT_Mips16:
3921 handleSimpleAttribute<Mips16Attr>(S, D, Attr); break;
3922 case AttributeList::AT_NoMips16:
3923 handleSimpleAttribute<NoMips16Attr>(S, D, Attr); break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00003924 case AttributeList::AT_IBAction:
3925 handleSimpleAttribute<IBActionAttr>(S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003926 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
3927 case AttributeList::AT_IBOutletCollection:
3928 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003929 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
3930 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003931 case AttributeList::AT_AlwaysInline:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003932 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003933 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003934 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00003935 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003936 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
3937 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
3938 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00003939 handleDependencyAttr(S, scope, D, Attr);
3940 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003941 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003942 case AttributeList::AT_CUDAConstant:
3943 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003944 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003945 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman3a8e2d92013-11-27 18:53:58 +00003946 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003947 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00003948 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00003949 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003950 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
Nick Lewycky35a6ef42014-01-11 02:50:57 +00003951 case AttributeList::AT_EnableIf: handleEnableIfAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003952 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003953 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003954 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00003955 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003956 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00003957 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003958 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
3959 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
3960 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00003961 case AttributeList::AT_CUDADevice:
3962 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003963 case AttributeList::AT_CUDAHost:
3964 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003965 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
3966 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003967 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00003968 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003969 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003970 case AttributeList::AT_MayAlias:
3971 handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00003972 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003973 case AttributeList::AT_NoCommon:
3974 handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00003975 case AttributeList::AT_NonNull:
3976 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
3977 handleNonNullAttrParameter(S, PVD, Attr);
3978 else
3979 handleNonNullAttr(S, D, Attr);
3980 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003981 case AttributeList::AT_Overloadable:
3982 handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00003983 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003984 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
3985 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003986 case AttributeList::AT_Naked:
3987 handleSimpleAttribute<NakedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003988 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00003989 case AttributeList::AT_NoThrow:
3990 handleSimpleAttribute<NoThrowAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003991 case AttributeList::AT_CUDAShared:
3992 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003993 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003994
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003995 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003996 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003997 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003998 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003999
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004000 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004001 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4002
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004003 case AttributeList::AT_ObjCRequiresSuper:
4004 handleObjCRequiresSuperAttr(S, D, Attr); break;
4005
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004006 case AttributeList::AT_ObjCBridge:
4007 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004008
4009 case AttributeList::AT_ObjCBridgeMutable:
4010 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004011
4012 case AttributeList::AT_ObjCBridgeRelated:
4013 handleObjCBridgeRelatedAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00004014
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004015 case AttributeList::AT_ObjCDesignatedInitializer:
4016 handleObjCDesignatedInitializer(S, D, Attr); break;
4017
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004018 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00004019 handleCFAuditedTransferAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004020 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00004021 handleCFUnknownTransferAttr(S, D, Attr); break;
John McCall32f5fe12011-09-30 05:12:12 +00004022
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004023 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004024 case AttributeList::AT_CFConsumed:
4025 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4026 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004027 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004028
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004029 case AttributeList::AT_NSReturnsAutoreleased:
4030 case AttributeList::AT_NSReturnsNotRetained:
4031 case AttributeList::AT_CFReturnsNotRetained:
4032 case AttributeList::AT_NSReturnsRetained:
4033 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004034 handleNSReturnsRetainedAttr(S, D, Attr); break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004035 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004036 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004037 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004038 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004039 case AttributeList::AT_VecTypeHint:
4040 handleVecTypeHint(S, D, Attr); break;
4041
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004042 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004043 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004044
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004045 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4046 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4047 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004048 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004049 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004050 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004051 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004052 case AttributeList::AT_ObjCRootClass:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004053 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004054 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek28eace62013-11-23 01:01:34 +00004055 handleObjCSuppresProtocolAttr(S, D, Attr);
4056 break;
4057 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004058 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00004059 case AttributeList::AT_Unused:
4060 handleSimpleAttribute<UnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004061 case AttributeList::AT_ReturnsTwice:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004062 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004063 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004064 case AttributeList::AT_Visibility:
4065 handleVisibilityAttr(S, D, Attr, false);
4066 break;
4067 case AttributeList::AT_TypeVisibility:
4068 handleVisibilityAttr(S, D, Attr, true);
4069 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004070 case AttributeList::AT_WarnUnused:
Aaron Ballman6a42b5a2013-11-27 16:59:17 +00004071 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004072 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004073 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004074 case AttributeList::AT_Weak:
4075 handleSimpleAttribute<WeakAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004076 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4077 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4078 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004079 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004080 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004081 case AttributeList::AT_ObjCException:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004082 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004083 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004084 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004085 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004086 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4087 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4088 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004089 case AttributeList::AT_Const:
4090 handleSimpleAttribute<ConstAttr>(S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004091 case AttributeList::AT_Pure:
4092 handleSimpleAttribute<PureAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004093 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4094 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004095 case AttributeList::AT_NoInline:
4096 handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004097 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004098 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004099 case AttributeList::AT_StdCall:
4100 case AttributeList::AT_CDecl:
4101 case AttributeList::AT_FastCall:
4102 case AttributeList::AT_ThisCall:
4103 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004104 case AttributeList::AT_MSABI:
4105 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004106 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004107 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004108 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004109 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004110 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004111 case AttributeList::AT_OpenCLKernel:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004112 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004113 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman26891332014-01-14 17:41:53 +00004114 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr); break;
John McCall8d32c052012-05-22 21:28:12 +00004115
4116 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004117 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004118 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004119 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004120 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004121 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004122 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004123 case AttributeList::AT_MSInheritance:
4124 handleSimpleAttribute<MSInheritanceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004125 case AttributeList::AT_ForceInline:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004126 handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004127 case AttributeList::AT_SelectAny:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004128 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004129
4130 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004131 case AttributeList::AT_AssertExclusiveLock:
4132 handleAssertExclusiveLockAttr(S, D, Attr);
4133 break;
4134 case AttributeList::AT_AssertSharedLock:
4135 handleAssertSharedLockAttr(S, D, Attr);
4136 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004137 case AttributeList::AT_GuardedVar:
Aaron Ballmane61b8b82013-12-02 15:02:49 +00004138 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004139 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004140 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004141 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004142 case AttributeList::AT_ScopedLockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004143 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004144 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004145 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004146 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004147 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004148 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004149 break;
4150 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004151 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004152 break;
4153 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004154 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004155 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004156 case AttributeList::AT_Lockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004157 handleSimpleAttribute<LockableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004158 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004159 handleGuardedByAttr(S, D, Attr);
4160 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004161 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004162 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004163 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004164 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004165 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004166 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004167 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004168 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004169 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004170 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004171 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004172 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004173 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004174 handleLockReturnedAttr(S, D, Attr);
4175 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004176 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004177 handleLocksExcludedAttr(S, D, Attr);
4178 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004179 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004180 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004181 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004182 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004183 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004184 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004185 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004186 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004187 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004188 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004189 handleUnlockFunAttr(S, D, Attr);
4190 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004191 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004192 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004193 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004194 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004195 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004196 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004197
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004198 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004199 case AttributeList::AT_Consumable:
4200 handleConsumableAttr(S, D, Attr);
4201 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004202 case AttributeList::AT_ConsumableAutoCast:
4203 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr); break;
4204 break;
4205 case AttributeList::AT_ConsumableSetOnRead:
4206 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr); break;
4207 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004208 case AttributeList::AT_CallableWhen:
4209 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004210 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004211 case AttributeList::AT_ParamTypestate:
4212 handleParamTypestateAttr(S, D, Attr);
4213 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004214 case AttributeList::AT_ReturnTypestate:
4215 handleReturnTypestateAttr(S, D, Attr);
4216 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004217 case AttributeList::AT_SetTypestate:
4218 handleSetTypestateAttr(S, D, Attr);
4219 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004220 case AttributeList::AT_TestTypestate:
4221 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004222 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004223
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004224 // Type safety attributes.
4225 case AttributeList::AT_ArgumentWithTypeTag:
4226 handleArgumentWithTypeTagAttr(S, D, Attr);
4227 break;
4228 case AttributeList::AT_TypeTagForDatatype:
4229 handleTypeTagForDatatypeAttr(S, D, Attr);
4230 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004231 }
4232}
4233
4234/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4235/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004236void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004237 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004238 bool IncludeCXX11Attributes) {
4239 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004240 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004241
Joey Gouly2cd9db12013-12-13 16:15:28 +00004242 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004243 // GCC accepts
4244 // static int a9 __attribute__((weakref));
4245 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004246 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00004247 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
4248 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00004249 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004250 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004251 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004252
4253 if (!D->hasAttr<OpenCLKernelAttr>()) {
4254 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004255 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
4256 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004257 D->setInvalidDecl();
4258 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004259 if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
4260 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004261 D->setInvalidDecl();
4262 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004263 if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
4264 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004265 D->setInvalidDecl();
4266 }
4267 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004268}
4269
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004270// Annotation attributes are the only attributes allowed after an access
4271// specifier.
4272bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4273 const AttributeList *AttrList) {
4274 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004275 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004276 handleAnnotateAttr(*this, ASDecl, *l);
4277 } else {
4278 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4279 return true;
4280 }
4281 }
4282
4283 return false;
4284}
4285
John McCall42856de2011-10-01 05:17:03 +00004286/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4287/// contains any decl attributes that we should warn about.
4288static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4289 for ( ; A; A = A->getNext()) {
4290 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004291 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004292 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4293
4294 if (A->getKind() == AttributeList::UnknownAttribute) {
4295 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4296 << A->getName() << A->getRange();
4297 } else {
4298 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4299 << A->getName() << A->getRange();
4300 }
4301 }
4302}
4303
4304/// checkUnusedDeclAttributes - Given a declarator which is not being
4305/// used to build a declaration, complain about any decl attributes
4306/// which might be lying around on it.
4307void Sema::checkUnusedDeclAttributes(Declarator &D) {
4308 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4309 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4310 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4311 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4312}
4313
Ryan Flynn7d470f32009-07-30 03:15:39 +00004314/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004315/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004316NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4317 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004318 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004319 NamedDecl *NewD = 0;
4320 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004321 FunctionDecl *NewFD;
4322 // FIXME: Missing call to CheckFunctionDeclaration().
4323 // FIXME: Mangling?
4324 // FIXME: Is the qualifier info correct?
4325 // FIXME: Is the DeclContext correct?
4326 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4327 Loc, Loc, DeclarationName(II),
4328 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004329 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004330 FD->hasPrototype(),
4331 false/*isConstexprSpecified*/);
4332 NewD = NewFD;
4333
4334 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004335 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004336
4337 // Fake up parameter variables; they are declared as if this were
4338 // a typedef.
4339 QualType FDTy = FD->getType();
4340 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4341 SmallVector<ParmVarDecl*, 16> Params;
4342 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4343 AE = FT->arg_type_end(); AI != AE; ++AI) {
4344 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4345 Param->setScopeInfo(0, Params.size());
4346 Params.push_back(Param);
4347 }
David Blaikie9c70e042011-09-21 18:16:56 +00004348 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004349 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004350 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4351 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004352 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004353 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004354 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004355 if (VD->getQualifier()) {
4356 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004357 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004358 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004359 }
4360 return NewD;
4361}
4362
James Dennett634962f2012-06-14 21:40:34 +00004363/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004364/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004365void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004366 if (W.getUsed()) return; // only do this once
4367 W.setUsed(true);
4368 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4369 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004370 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00004371 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
4372 W.getLocation()));
4373 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00004374 WeakTopLevelDecl.push_back(NewD);
4375 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4376 // to insert Decl at TU scope, sorry.
4377 DeclContext *SavedContext = CurContext;
4378 CurContext = Context.getTranslationUnitDecl();
4379 PushOnScopeChains(NewD, S);
4380 CurContext = SavedContext;
4381 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00004382 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004383 }
4384}
4385
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004386void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4387 // It's valid to "forward-declare" #pragma weak, in which case we
4388 // have to do this.
4389 LoadExternalWeakUndeclaredIdentifiers();
4390 if (!WeakUndeclaredIdentifiers.empty()) {
4391 NamedDecl *ND = NULL;
4392 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4393 if (VD->isExternC())
4394 ND = VD;
4395 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4396 if (FD->isExternC())
4397 ND = FD;
4398 if (ND) {
4399 if (IdentifierInfo *Id = ND->getIdentifier()) {
4400 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4401 = WeakUndeclaredIdentifiers.find(Id);
4402 if (I != WeakUndeclaredIdentifiers.end()) {
4403 WeakInfo W = I->second;
4404 DeclApplyPragmaWeak(S, ND, W);
4405 WeakUndeclaredIdentifiers[Id] = W;
4406 }
4407 }
4408 }
4409 }
4410}
4411
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004412/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4413/// it, apply them to D. This is a bit tricky because PD can have attributes
4414/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004415void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004416 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004417 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004418 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004419
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004420 // Walk the declarator structure, applying decl attributes that were in a type
4421 // position to the decl itself. This handles cases like:
4422 // int *__attr__(x)** D;
4423 // when X is a decl attribute.
4424 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4425 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004426 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004427
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004428 // Finally, apply any attributes on the decl itself.
4429 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004430 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004431}
John McCall28a6aea2009-11-04 02:18:39 +00004432
John McCall31168b02011-06-15 23:02:42 +00004433/// Is the given declaration allowed to use a forbidden type?
4434static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4435 // Private ivars are always okay. Unfortunately, people don't
4436 // always properly make their ivars private, even in system headers.
4437 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004438 // Function declarations in sys headers will be marked unavailable.
4439 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4440 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004441 return false;
4442
4443 // Require it to be declared in a system header.
4444 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4445}
4446
4447/// Handle a delayed forbidden-type diagnostic.
4448static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4449 Decl *decl) {
4450 if (decl && isForbiddenTypeAllowed(S, decl)) {
Aaron Ballman36a53502014-01-16 13:03:14 +00004451 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context,
4452 "this system declaration uses an unsupported type",
4453 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00004454 return;
4455 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004456 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004457 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004458 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004459 // kind of forbidden type messages on unavailable functions.
4460 if (FD->hasAttr<UnavailableAttr>() &&
4461 diag.getForbiddenTypeDiagnostic() ==
4462 diag::err_arc_array_param_no_ownership) {
4463 diag.Triggered = true;
4464 return;
4465 }
4466 }
John McCall31168b02011-06-15 23:02:42 +00004467
4468 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4469 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4470 diag.Triggered = true;
4471}
4472
John McCall2ec85372012-05-07 06:16:41 +00004473void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4474 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004475 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004476 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004477
John McCall2ec85372012-05-07 06:16:41 +00004478 // When delaying diagnostics to run in the context of a parsed
4479 // declaration, we only want to actually emit anything if parsing
4480 // succeeds.
4481 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004482
John McCall2ec85372012-05-07 06:16:41 +00004483 // We emit all the active diagnostics in this pool or any of its
4484 // parents. In general, we'll get one pool for the decl spec
4485 // and a child pool for each declarator; in a decl group like:
4486 // deprecated_typedef foo, *bar, baz();
4487 // only the declarator pops will be passed decls. This is correct;
4488 // we really do need to consider delayed diagnostics from the decl spec
4489 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004490 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004491 do {
John McCall6347b682012-05-07 06:16:58 +00004492 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004493 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4494 // This const_cast is a bit lame. Really, Triggered should be mutable.
4495 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004496 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004497 continue;
4498
John McCallc1465822011-02-14 07:13:47 +00004499 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004500 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004501 case DelayedDiagnostic::Unavailable:
4502 // Don't bother giving deprecation/unavailable diagnostics if
4503 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004504 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004505 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004506 break;
4507
4508 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004509 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004510 break;
John McCall31168b02011-06-15 23:02:42 +00004511
4512 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004513 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004514 break;
John McCall86121512010-01-27 03:50:35 +00004515 }
4516 }
John McCall2ec85372012-05-07 06:16:41 +00004517 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004518}
4519
John McCall6347b682012-05-07 06:16:58 +00004520/// Given a set of delayed diagnostics, re-emit them as if they had
4521/// been delayed in the current context instead of in the given pool.
4522/// Essentially, this just moves them to the current pool.
4523void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4524 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4525 assert(curPool && "re-emitting in undelayed context not supported");
4526 curPool->steal(pool);
4527}
4528
John McCall28a6aea2009-11-04 02:18:39 +00004529static bool isDeclDeprecated(Decl *D) {
4530 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004531 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004532 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004533 // A category implicitly has the availability of the interface.
4534 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4535 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004536 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4537 return false;
4538}
4539
Ted Kremenekb79ee572013-12-18 23:30:06 +00004540static bool isDeclUnavailable(Decl *D) {
4541 do {
4542 if (D->isUnavailable())
4543 return true;
4544 // A category implicitly has the availability of the interface.
4545 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4546 return CatD->getClassInterface()->isUnavailable();
4547 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4548 return false;
4549}
4550
Eli Friedman971bfa12012-08-08 21:52:41 +00004551static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004552DoEmitAvailabilityWarning(Sema &S,
4553 DelayedDiagnostic::DDKind K,
4554 Decl *Ctx,
4555 const NamedDecl *D,
4556 StringRef Message,
4557 SourceLocation Loc,
4558 const ObjCInterfaceDecl *UnknownObjCClass,
4559 const ObjCPropertyDecl *ObjCProperty) {
4560
4561 // Diagnostics for deprecated or unavailable.
4562 unsigned diag, diag_message, diag_fwdclass_message;
4563
4564 // Matches 'diag::note_property_attribute' options.
4565 unsigned property_note_select;
4566
4567 // Matches diag::note_availability_specified_here.
4568 unsigned available_here_select_kind;
4569
4570 // Don't warn if our current context is deprecated or unavailable.
4571 switch (K) {
4572 case DelayedDiagnostic::Deprecation:
4573 if (isDeclDeprecated(Ctx))
4574 return;
4575 diag = diag::warn_deprecated;
4576 diag_message = diag::warn_deprecated_message;
4577 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4578 property_note_select = /* deprecated */ 0;
4579 available_here_select_kind = /* deprecated */ 2;
4580 break;
4581
4582 case DelayedDiagnostic::Unavailable:
4583 if (isDeclUnavailable(Ctx))
4584 return;
4585 diag = diag::err_unavailable;
4586 diag_message = diag::err_unavailable_message;
4587 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4588 property_note_select = /* unavailable */ 1;
4589 available_here_select_kind = /* unavailable */ 0;
4590 break;
4591
4592 default:
4593 llvm_unreachable("Neither a deprecation or unavailable kind");
4594 }
4595
Eli Friedman971bfa12012-08-08 21:52:41 +00004596 DeclarationName Name = D->getDeclName();
4597 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004598 S.Diag(Loc, diag_message) << Name << Message;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004599 if (ObjCProperty)
4600 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4601 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004602 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004603 S.Diag(Loc, diag) << Name;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004604 if (ObjCProperty)
4605 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4606 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004607 } else {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004608 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004609 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4610 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004611
4612 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4613 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004614}
4615
Ted Kremenekb79ee572013-12-18 23:30:06 +00004616void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4617 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004618 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004619 DoEmitAvailabilityWarning(*this,
4620 (DelayedDiagnostic::DDKind) DD.Kind,
4621 Ctx,
4622 DD.getDeprecationDecl(),
4623 DD.getDeprecationMessage(),
4624 DD.Loc,
4625 DD.getUnknownObjCClass(),
4626 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004627}
4628
Ted Kremenekb79ee572013-12-18 23:30:06 +00004629void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4630 NamedDecl *D, StringRef Message,
4631 SourceLocation Loc,
4632 const ObjCInterfaceDecl *UnknownObjCClass,
4633 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004634 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004635 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004636 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4637 UnknownObjCClass,
4638 ObjCProperty,
4639 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004640 return;
4641 }
4642
Ted Kremenekb79ee572013-12-18 23:30:06 +00004643 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4644 DelayedDiagnostic::DDKind K;
4645 switch (AD) {
4646 case AD_Deprecation:
4647 K = DelayedDiagnostic::Deprecation;
4648 break;
4649 case AD_Unavailable:
4650 K = DelayedDiagnostic::Unavailable;
4651 break;
4652 }
4653
4654 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
4655 UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004656}