blob: b8116a7e8a613357351e3831293106327eca052b [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
Chandler Carruthedc2c642011-07-02 00:01:44 +00001159static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001160 SmallVector<unsigned, 8> NonNullArgs;
1161 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001162 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001163 uint64_t Idx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00001164 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001165 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001166
1167 // Is the function argument a pointer type?
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001168 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001169 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001170
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001171 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001172 // FIXME: Should also highlight argument in decl.
Aaron Ballmancedaaea2013-12-26 17:07:49 +00001173 S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only)
1174 << Attr.getName() << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001175 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001176 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001177
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001178 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001179 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001180
1181 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1182 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001183 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001184 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1185 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001186 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001187 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001188 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001189 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001190
Ted Kremenek22813f42010-10-21 18:49:36 +00001191 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001192 if (NonNullArgs.empty()) {
1193 // Warn the trivial case only if attribute is not coming from a
1194 // macro instantiation.
1195 if (Attr.getLoc().isFileID())
1196 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001197 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001198 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001199 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001200
Nick Lewyckye1121512013-01-24 01:12:16 +00001201 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001202 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001203 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001204 D->addAttr(::new (S.Context)
1205 NonNullAttr(Attr.getRange(), S.Context, start, size,
1206 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001207}
1208
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001209static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1210 switch (K) {
1211 case OwnershipAttr::Holds: return "'ownership_holds'";
1212 case OwnershipAttr::Takes: return "'ownership_takes'";
1213 case OwnershipAttr::Returns: return "'ownership_returns'";
1214 }
1215 llvm_unreachable("unknown ownership");
1216}
1217
Chandler Carruthedc2c642011-07-02 00:01:44 +00001218static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001219 // This attribute must be applied to a function declaration. The first
1220 // argument to the attribute must be an identifier, the name of the resource,
1221 // for example: malloc. The following arguments must be argument indexes, the
1222 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001223 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001224 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001225 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001226
Aaron Ballman00e99962013-08-31 01:11:41 +00001227 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001228 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001229 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001230 return;
1231 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001232
Richard Smith852e9ce2013-11-27 01:46:48 +00001233 // Figure out our Kind.
1234 OwnershipAttr::OwnershipKind K =
1235 OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0,
1236 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001237
Richard Smith852e9ce2013-11-27 01:46:48 +00001238 // Check arguments.
1239 switch (K) {
1240 case OwnershipAttr::Takes:
1241 case OwnershipAttr::Holds:
1242 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001243 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1244 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001245 return;
1246 }
1247 break;
1248 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001249 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001250 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1251 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001252 return;
1253 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001254 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001255 }
1256
Richard Smith852e9ce2013-11-27 01:46:48 +00001257 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001258
1259 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001260 StringRef ModuleName = Module->getName();
1261 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1262 ModuleName.size() > 4) {
1263 ModuleName = ModuleName.drop_front(2).drop_back(2);
1264 Module = &S.PP.getIdentifierTable().get(ModuleName);
1265 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001266
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001267 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001268 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1269 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001270 uint64_t Idx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00001271 if (!checkFunctionOrMethodArgumentIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001272 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001273
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001274 // Is the function argument a pointer type?
1275 QualType T = getFunctionOrMethodArgType(D, Idx);
1276 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001277 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001278 case OwnershipAttr::Takes:
1279 case OwnershipAttr::Holds:
1280 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1281 Err = 0;
1282 break;
1283 case OwnershipAttr::Returns:
1284 if (!T->isIntegerType())
1285 Err = 1;
1286 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001287 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001288 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001289 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001290 << Ex->getSourceRange();
1291 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001292 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001293
1294 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001295 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001296 i = D->specific_attr_begin<OwnershipAttr>(),
1297 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001298 // FIXME: A returns attribute should conflict with any returns attribute
1299 // with a different index too.
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001300 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1301 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1302 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1303 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1304 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001305 }
1306 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001307 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001308 }
1309
1310 unsigned* start = OwnershipArgs.data();
1311 unsigned size = OwnershipArgs.size();
1312 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001313
Michael Han99315932013-01-24 16:46:58 +00001314 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001315 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001316 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001317}
1318
Chandler Carruthedc2c642011-07-02 00:01:44 +00001319static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001320 // Check the attribute arguments.
1321 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001322 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1323 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001324 return;
1325 }
1326
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001327 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001328
Rafael Espindolac18086a2010-02-23 22:00:30 +00001329 // gcc rejects
1330 // class c {
1331 // static int a __attribute__((weakref ("v2")));
1332 // static int b() __attribute__((weakref ("f3")));
1333 // };
1334 // and ignores the attributes of
1335 // void f(void) {
1336 // static int a __attribute__((weakref ("v2")));
1337 // }
1338 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001339 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001340 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001341 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1342 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001343 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001344 }
1345
1346 // The GCC manual says
1347 //
1348 // At present, a declaration to which `weakref' is attached can only
1349 // be `static'.
1350 //
1351 // It also says
1352 //
1353 // Without a TARGET,
1354 // given as an argument to `weakref' or to `alias', `weakref' is
1355 // equivalent to `weak'.
1356 //
1357 // gcc 4.4.1 will accept
1358 // int a7 __attribute__((weakref));
1359 // as
1360 // int a7 __attribute__((weak));
1361 // This looks like a bug in gcc. We reject that for now. We should revisit
1362 // it if this behaviour is actually used.
1363
Rafael Espindolac18086a2010-02-23 22:00:30 +00001364 // GCC rejects
1365 // static ((alias ("y"), weakref)).
1366 // Should we? How to check that weakref is before or after alias?
1367
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001368 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1369 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1370 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001371 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001372 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001373 // GCC will accept anything as the argument of weakref. Should we
1374 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001375 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1376 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001377
Michael Han99315932013-01-24 16:46:58 +00001378 D->addAttr(::new (S.Context)
1379 WeakRefAttr(Attr.getRange(), S.Context,
1380 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001381}
1382
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001383static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1384 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001385 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001386 return;
1387
Douglas Gregore8bbc122011-09-02 00:18:52 +00001388 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001389 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1390 return;
1391 }
1392
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001393 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001394
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001395 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001396 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001397}
1398
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001399static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001400 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001401 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001402
Michael Han99315932013-01-24 16:46:58 +00001403 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1404 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001405}
1406
1407static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001408 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001409 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001410
Michael Han99315932013-01-24 16:46:58 +00001411 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1412 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001413}
1414
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001415static void handleTLSModelAttr(Sema &S, Decl *D,
1416 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001417 StringRef Model;
1418 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001419 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001420 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001421 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001422
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001423 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001424 if (Model != "global-dynamic" && Model != "local-dynamic"
1425 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001426 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001427 return;
1428 }
1429
Michael Han99315932013-01-24 16:46:58 +00001430 D->addAttr(::new (S.Context)
1431 TLSModelAttr(Attr.getRange(), S.Context, Model,
1432 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001433}
1434
Chandler Carruthedc2c642011-07-02 00:01:44 +00001435static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001436 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001437 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001438 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001439 D->addAttr(::new (S.Context)
1440 MallocAttr(Attr.getRange(), S.Context,
1441 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001442 return;
1443 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001444 }
1445
Ted Kremenek08479ae2009-08-15 00:51:46 +00001446 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001447}
1448
Chandler Carruthedc2c642011-07-02 00:01:44 +00001449static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001450 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001451 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1452 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001453 return;
1454 }
1455
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001456 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1457 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001458}
1459
Chandler Carruthedc2c642011-07-02 00:01:44 +00001460static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001461 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001462
1463 if (S.CheckNoReturnAttr(attr)) return;
1464
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001465 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001466 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001467 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001468 return;
1469 }
1470
Michael Han99315932013-01-24 16:46:58 +00001471 D->addAttr(::new (S.Context)
1472 NoReturnAttr(attr.getRange(), S.Context,
1473 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001474}
1475
1476bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001477 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001478 attr.setInvalid();
1479 return true;
1480 }
1481
1482 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001483}
1484
Chandler Carruthedc2c642011-07-02 00:01:44 +00001485static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1486 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001487
1488 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1489 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001490 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1491 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001492 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1493 && !VD->getType()->isFunctionPointerType())) {
1494 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001495 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001496 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001497 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001498 return;
1499 }
1500 }
1501
Michael Han99315932013-01-24 16:46:58 +00001502 D->addAttr(::new (S.Context)
1503 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1504 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001505}
1506
John Thompsoncdb847ba2010-08-09 21:53:52 +00001507// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001508static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001509/*
1510 Returning a Vector Class in Registers
1511
Eric Christopherbc638a82010-12-01 22:13:54 +00001512 According to the PPU ABI specifications, a class with a single member of
1513 vector type is returned in memory when used as the return value of a function.
1514 This results in inefficient code when implementing vector classes. To return
1515 the value in a single vector register, add the vecreturn attribute to the
1516 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001517
1518 Example:
1519
1520 struct Vector
1521 {
1522 __vector float xyzw;
1523 } __attribute__((vecreturn));
1524
1525 Vector Add(Vector lhs, Vector rhs)
1526 {
1527 Vector result;
1528 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1529 return result; // This will be returned in a register
1530 }
1531*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001532 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1533 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001534 return;
1535 }
1536
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001537 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001538 int count = 0;
1539
1540 if (!isa<CXXRecordDecl>(record)) {
1541 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1542 return;
1543 }
1544
1545 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1546 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1547 return;
1548 }
1549
Eric Christopherbc638a82010-12-01 22:13:54 +00001550 for (RecordDecl::field_iterator iter = record->field_begin();
1551 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001552 if ((count == 1) || !iter->getType()->isVectorType()) {
1553 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1554 return;
1555 }
1556 count++;
1557 }
1558
Michael Han99315932013-01-24 16:46:58 +00001559 D->addAttr(::new (S.Context)
1560 VecReturnAttr(Attr.getRange(), S.Context,
1561 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001562}
1563
Richard Smithe233fbf2013-01-28 22:42:45 +00001564static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1565 const AttributeList &Attr) {
1566 if (isa<ParmVarDecl>(D)) {
1567 // [[carries_dependency]] can only be applied to a parameter if it is a
1568 // parameter of a function declaration or lambda.
1569 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1570 S.Diag(Attr.getLoc(),
1571 diag::err_carries_dependency_param_not_function_decl);
1572 return;
1573 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001574 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001575
1576 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1577 Attr.getRange(), S.Context,
1578 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001579}
1580
Chandler Carruthedc2c642011-07-02 00:01:44 +00001581static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001582 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001583 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001584 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001585 return;
1586 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001587 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001588 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001589 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001590 return;
1591 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001592
Michael Han99315932013-01-24 16:46:58 +00001593 D->addAttr(::new (S.Context)
1594 UsedAttr(Attr.getRange(), S.Context,
1595 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001596}
1597
Chandler Carruthedc2c642011-07-02 00:01:44 +00001598static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001599 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001600 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001601 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1602 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001603 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001604 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001605
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001606 uint32_t priority = ConstructorAttr::DefaultPriority;
1607 if (Attr.getNumArgs() > 0 &&
1608 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1609 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001610
Michael Han99315932013-01-24 16:46:58 +00001611 D->addAttr(::new (S.Context)
1612 ConstructorAttr(Attr.getRange(), S.Context, priority,
1613 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001614}
1615
Chandler Carruthedc2c642011-07-02 00:01:44 +00001616static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001617 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001618 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001619 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1620 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001621 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001622 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001623
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001624 uint32_t priority = ConstructorAttr::DefaultPriority;
1625 if (Attr.getNumArgs() > 0 &&
1626 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1627 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001628
Michael Han99315932013-01-24 16:46:58 +00001629 D->addAttr(::new (S.Context)
1630 DestructorAttr(Attr.getRange(), S.Context, priority,
1631 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001632}
1633
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001634template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001635static void handleAttrWithMessage(Sema &S, Decl *D,
1636 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001637 unsigned NumArgs = Attr.getNumArgs();
1638 if (NumArgs > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001639 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1640 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001641 return;
1642 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001643
1644 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001645 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001646 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001647 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001648
Michael Han99315932013-01-24 16:46:58 +00001649 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1650 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001651}
1652
Ted Kremenek28eace62013-11-23 01:01:34 +00001653static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
1654 const AttributeList &Attr) {
Ted Kremenek28eace62013-11-23 01:01:34 +00001655 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001656 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1657 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001658}
1659
Jordy Rose740b0c22012-05-08 03:27:22 +00001660static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1661 IdentifierInfo *Platform,
1662 VersionTuple Introduced,
1663 VersionTuple Deprecated,
1664 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001665 StringRef PlatformName
1666 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1667 if (PlatformName.empty())
1668 PlatformName = Platform->getName();
1669
1670 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1671 // of these steps are needed).
1672 if (!Introduced.empty() && !Deprecated.empty() &&
1673 !(Introduced <= Deprecated)) {
1674 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1675 << 1 << PlatformName << Deprecated.getAsString()
1676 << 0 << Introduced.getAsString();
1677 return true;
1678 }
1679
1680 if (!Introduced.empty() && !Obsoleted.empty() &&
1681 !(Introduced <= Obsoleted)) {
1682 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1683 << 2 << PlatformName << Obsoleted.getAsString()
1684 << 0 << Introduced.getAsString();
1685 return true;
1686 }
1687
1688 if (!Deprecated.empty() && !Obsoleted.empty() &&
1689 !(Deprecated <= Obsoleted)) {
1690 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1691 << 2 << PlatformName << Obsoleted.getAsString()
1692 << 1 << Deprecated.getAsString();
1693 return true;
1694 }
1695
1696 return false;
1697}
1698
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001699/// \brief Check whether the two versions match.
1700///
1701/// If either version tuple is empty, then they are assumed to match. If
1702/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1703static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1704 bool BeforeIsOkay) {
1705 if (X.empty() || Y.empty())
1706 return true;
1707
1708 if (X == Y)
1709 return true;
1710
1711 if (BeforeIsOkay && X < Y)
1712 return true;
1713
1714 return false;
1715}
1716
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001717AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001718 IdentifierInfo *Platform,
1719 VersionTuple Introduced,
1720 VersionTuple Deprecated,
1721 VersionTuple Obsoleted,
1722 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001723 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001724 bool Override,
1725 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001726 VersionTuple MergedIntroduced = Introduced;
1727 VersionTuple MergedDeprecated = Deprecated;
1728 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001729 bool FoundAny = false;
1730
Rafael Espindolac67f2232012-05-10 02:50:16 +00001731 if (D->hasAttrs()) {
1732 AttrVec &Attrs = D->getAttrs();
1733 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1734 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1735 if (!OldAA) {
1736 ++i;
1737 continue;
1738 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001739
Rafael Espindolac67f2232012-05-10 02:50:16 +00001740 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1741 if (OldPlatform != Platform) {
1742 ++i;
1743 continue;
1744 }
1745
1746 FoundAny = true;
1747 VersionTuple OldIntroduced = OldAA->getIntroduced();
1748 VersionTuple OldDeprecated = OldAA->getDeprecated();
1749 VersionTuple OldObsoleted = OldAA->getObsoleted();
1750 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001751
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001752 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1753 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1754 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1755 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001756 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001757 if (Override) {
1758 int Which = -1;
1759 VersionTuple FirstVersion;
1760 VersionTuple SecondVersion;
1761 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1762 Which = 0;
1763 FirstVersion = OldIntroduced;
1764 SecondVersion = Introduced;
1765 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1766 Which = 1;
1767 FirstVersion = Deprecated;
1768 SecondVersion = OldDeprecated;
1769 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1770 Which = 2;
1771 FirstVersion = Obsoleted;
1772 SecondVersion = OldObsoleted;
1773 }
1774
1775 if (Which == -1) {
1776 Diag(OldAA->getLocation(),
1777 diag::warn_mismatched_availability_override_unavail)
1778 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1779 } else {
1780 Diag(OldAA->getLocation(),
1781 diag::warn_mismatched_availability_override)
1782 << Which
1783 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1784 << FirstVersion.getAsString() << SecondVersion.getAsString();
1785 }
1786 Diag(Range.getBegin(), diag::note_overridden_method);
1787 } else {
1788 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1789 Diag(Range.getBegin(), diag::note_previous_attribute);
1790 }
1791
Rafael Espindolac67f2232012-05-10 02:50:16 +00001792 Attrs.erase(Attrs.begin() + i);
1793 --e;
1794 continue;
1795 }
1796
1797 VersionTuple MergedIntroduced2 = MergedIntroduced;
1798 VersionTuple MergedDeprecated2 = MergedDeprecated;
1799 VersionTuple MergedObsoleted2 = MergedObsoleted;
1800
1801 if (MergedIntroduced2.empty())
1802 MergedIntroduced2 = OldIntroduced;
1803 if (MergedDeprecated2.empty())
1804 MergedDeprecated2 = OldDeprecated;
1805 if (MergedObsoleted2.empty())
1806 MergedObsoleted2 = OldObsoleted;
1807
1808 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1809 MergedIntroduced2, MergedDeprecated2,
1810 MergedObsoleted2)) {
1811 Attrs.erase(Attrs.begin() + i);
1812 --e;
1813 continue;
1814 }
1815
1816 MergedIntroduced = MergedIntroduced2;
1817 MergedDeprecated = MergedDeprecated2;
1818 MergedObsoleted = MergedObsoleted2;
1819 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001820 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001821 }
1822
1823 if (FoundAny &&
1824 MergedIntroduced == Introduced &&
1825 MergedDeprecated == Deprecated &&
1826 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001827 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001828
Ted Kremenekb5445722013-04-06 00:34:27 +00001829 // Only create a new attribute if !Override, but we want to do
1830 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001831 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001832 MergedDeprecated, MergedObsoleted) &&
1833 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001834 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1835 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001836 Obsoleted, IsUnavailable, Message,
1837 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001838 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001839 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001840}
1841
Chandler Carruthedc2c642011-07-02 00:01:44 +00001842static void handleAvailabilityAttr(Sema &S, Decl *D,
1843 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001844 if (!checkAttributeNumArgs(S, Attr, 1))
1845 return;
1846 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001847 unsigned Index = Attr.getAttributeSpellingListIndex();
1848
Aaron Ballman00e99962013-08-31 01:11:41 +00001849 IdentifierInfo *II = Platform->Ident;
1850 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1851 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1852 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001853
Rafael Espindolac231fab2013-01-08 21:30:32 +00001854 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1855 if (!ND) {
1856 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1857 return;
1858 }
1859
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001860 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1861 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1862 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001863 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001864 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001865 if (const StringLiteral *SE =
1866 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001867 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001868
Aaron Ballman00e99962013-08-31 01:11:41 +00001869 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001870 Introduced.Version,
1871 Deprecated.Version,
1872 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001873 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001874 /*Override=*/false,
1875 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001876 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001877 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001878}
1879
John McCalld041a9b2013-02-20 01:54:26 +00001880template <class T>
1881static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1882 typename T::VisibilityType value,
1883 unsigned attrSpellingListIndex) {
1884 T *existingAttr = D->getAttr<T>();
1885 if (existingAttr) {
1886 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1887 if (existingValue == value)
1888 return NULL;
1889 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1890 S.Diag(range.getBegin(), diag::note_previous_attribute);
1891 D->dropAttr<T>();
1892 }
1893 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1894}
1895
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001896VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001897 VisibilityAttr::VisibilityType Vis,
1898 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001899 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1900 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001901}
1902
John McCalld041a9b2013-02-20 01:54:26 +00001903TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1904 TypeVisibilityAttr::VisibilityType Vis,
1905 unsigned AttrSpellingListIndex) {
1906 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1907 AttrSpellingListIndex);
1908}
1909
1910static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1911 bool isTypeVisibility) {
1912 // Visibility attributes don't mean anything on a typedef.
1913 if (isa<TypedefNameDecl>(D)) {
1914 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
1915 << Attr.getName();
1916 return;
1917 }
1918
1919 // 'type_visibility' can only go on a type or namespace.
1920 if (isTypeVisibility &&
1921 !(isa<TagDecl>(D) ||
1922 isa<ObjCInterfaceDecl>(D) ||
1923 isa<NamespaceDecl>(D))) {
1924 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
1925 << Attr.getName() << ExpectedTypeOrNamespace;
1926 return;
1927 }
1928
Benjamin Kramer70370212013-09-09 15:08:57 +00001929 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001930 StringRef TypeStr;
1931 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001932 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001933 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001934
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001935 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00001936 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001937 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00001938 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001939 return;
1940 }
Aaron Ballman682ee422013-09-11 19:47:58 +00001941
1942 // Complain about attempts to use protected visibility on targets
1943 // (like Darwin) that don't support it.
1944 if (type == VisibilityAttr::Protected &&
1945 !S.Context.getTargetInfo().hasProtectedVisibility()) {
1946 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1947 type = VisibilityAttr::Default;
1948 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001949
Michael Han99315932013-01-24 16:46:58 +00001950 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00001951 clang::Attr *newAttr;
1952 if (isTypeVisibility) {
1953 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
1954 (TypeVisibilityAttr::VisibilityType) type,
1955 Index);
1956 } else {
1957 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
1958 }
1959 if (newAttr)
1960 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001961}
1962
Chandler Carruthedc2c642011-07-02 00:01:44 +00001963static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1964 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001965 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00001966 if (!Attr.isArgIdent(0)) {
1967 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1968 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00001969 return;
1970 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001971
Aaron Ballman682ee422013-09-11 19:47:58 +00001972 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1973 ObjCMethodFamilyAttr::FamilyKind F;
1974 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
1975 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
1976 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00001977 return;
1978 }
1979
Aaron Ballman682ee422013-09-11 19:47:58 +00001980 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00001981 !method->getResultType()->isObjCObjectPointerType()) {
1982 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
1983 << method->getResultType();
1984 // Ignore the attribute.
1985 return;
1986 }
1987
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001988 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00001989 S.Context, F,
1990 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00001991}
1992
Chandler Carruthedc2c642011-07-02 00:01:44 +00001993static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00001994 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001995 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00001996 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001997 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1998 return;
1999 }
2000 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002001 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2002 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002003 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002004 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2005 return;
2006 }
2007 }
2008 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002009 // It is okay to include this attribute on properties, e.g.:
2010 //
2011 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2012 //
2013 // In this case it follows tradition and suppresses an error in the above
2014 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002015 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002016 }
Michael Han99315932013-01-24 16:46:58 +00002017 D->addAttr(::new (S.Context)
2018 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2019 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002020}
2021
Chandler Carruthedc2c642011-07-02 00:01:44 +00002022static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002023 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002024 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002025 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002026 return;
2027 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002028
Aaron Ballman00e99962013-08-31 01:11:41 +00002029 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002030 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002031 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2032 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2033 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002034 return;
2035 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002036
Michael Han99315932013-01-24 16:46:58 +00002037 D->addAttr(::new (S.Context)
2038 BlocksAttr(Attr.getRange(), S.Context, type,
2039 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002040}
2041
Chandler Carruthedc2c642011-07-02 00:01:44 +00002042static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002043 // check the attribute arguments.
2044 if (Attr.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00002045 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
2046 << Attr.getName() << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002047 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002048 }
2049
Aaron Ballman18a78382013-11-21 00:28:23 +00002050 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002051 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002052 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002053 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002054 if (E->isTypeDependent() || E->isValueDependent() ||
2055 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002056 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002057 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002058 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002059 return;
2060 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002061
John McCallb46f2872011-09-09 07:56:05 +00002062 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002063 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2064 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002065 return;
2066 }
John McCallb46f2872011-09-09 07:56:05 +00002067
2068 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002069 }
2070
Aaron Ballman18a78382013-11-21 00:28:23 +00002071 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002072 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002073 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002074 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002075 if (E->isTypeDependent() || E->isValueDependent() ||
2076 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002077 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002078 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002079 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002080 return;
2081 }
2082 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002083
John McCallb46f2872011-09-09 07:56:05 +00002084 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002085 // FIXME: This error message could be improved, it would be nice
2086 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002087 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2088 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002089 return;
2090 }
2091 }
2092
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002093 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002094 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002095 if (isa<FunctionNoProtoType>(FT)) {
2096 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2097 return;
2098 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002099
Chris Lattner9363e312009-03-17 23:03:47 +00002100 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002101 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002102 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002103 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002104 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002105 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002106 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002107 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002108 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002109 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2110 if (!BD->isVariadic()) {
2111 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2112 return;
2113 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002114 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002115 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002116 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002117 const FunctionType *FT = Ty->isFunctionPointerType()
2118 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002119 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002120 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002121 int m = Ty->isFunctionPointerType() ? 0 : 1;
2122 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002123 return;
2124 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002125 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002126 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002127 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002128 return;
2129 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002130 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002131 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002132 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002133 return;
2134 }
Michael Han99315932013-01-24 16:46:58 +00002135 D->addAttr(::new (S.Context)
2136 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2137 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002138}
2139
Chandler Carruthedc2c642011-07-02 00:01:44 +00002140static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002141 if (D->getFunctionType() && D->getFunctionType()->getResultType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002142 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2143 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002144 return;
2145 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002146 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2147 if (MD->getResultType()->isVoidType()) {
2148 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2149 << Attr.getName() << 1;
2150 return;
2151 }
2152
Michael Han99315932013-01-24 16:46:58 +00002153 D->addAttr(::new (S.Context)
2154 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2155 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002156}
2157
Chandler Carruthedc2c642011-07-02 00:01:44 +00002158static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002159 // weak_import only applies to variable & function declarations.
2160 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002161 if (!D->canBeWeakImported(isDef)) {
2162 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002163 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2164 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002165 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002166 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002167 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002168 // Nothing to warn about here.
2169 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002170 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002171 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002172
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002173 return;
2174 }
2175
Michael Han99315932013-01-24 16:46:58 +00002176 D->addAttr(::new (S.Context)
2177 WeakImportAttr(Attr.getRange(), S.Context,
2178 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002179}
2180
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002181// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002182template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002183static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002184 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002185 uint32_t WGSize[3];
2186 for (unsigned i = 0; i < 3; ++i)
2187 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002188 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002189
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002190 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2191 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2192 Existing->getYDim() == WGSize[1] &&
2193 Existing->getZDim() == WGSize[2]))
2194 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002195
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002196 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2197 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002198 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002199}
2200
Joey Goulyaba589c2013-03-08 09:42:32 +00002201static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002202 if (!Attr.hasParsedType()) {
2203 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2204 << Attr.getName() << 1;
2205 return;
2206 }
2207
Richard Smithb87c4652013-10-31 21:23:20 +00002208 TypeSourceInfo *ParmTSI = 0;
2209 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2210 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002211
2212 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2213 (ParmType->isBooleanType() ||
2214 !ParmType->isIntegralType(S.getASTContext()))) {
2215 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2216 << ParmType;
2217 return;
2218 }
2219
Aaron Ballmana9e05402013-12-02 22:16:55 +00002220 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002221 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002222 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2223 return;
2224 }
2225 }
2226
2227 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002228 ParmTSI,
2229 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002230}
2231
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002232SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002233 StringRef Name,
2234 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002235 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2236 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002237 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002238 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2239 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002240 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002241 }
Michael Han99315932013-01-24 16:46:58 +00002242 return ::new (Context) SectionAttr(Range, Context, Name,
2243 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002244}
2245
Chandler Carruthedc2c642011-07-02 00:01:44 +00002246static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002247 // Make sure that there is a string literal as the sections's single
2248 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002249 StringRef Str;
2250 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002251 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002252 return;
Mike Stump11289f42009-09-09 15:08:12 +00002253
Chris Lattner30ba6742009-08-10 19:03:04 +00002254 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002255 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002256 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002257 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002258 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002259 return;
2260 }
Mike Stump11289f42009-09-09 15:08:12 +00002261
Michael Han99315932013-01-24 16:46:58 +00002262 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002263 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002264 if (NewAttr)
2265 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002266}
2267
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002268
Chandler Carruthedc2c642011-07-02 00:01:44 +00002269static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002270 VarDecl *VD = cast<VarDecl>(D);
2271 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002272 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002273 return;
2274 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002275
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002276 Expr *E = Attr.getArgAsExpr(0);
2277 SourceLocation Loc = E->getExprLoc();
2278 FunctionDecl *FD = 0;
2279 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002280
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002281 // gcc only allows for simple identifiers. Since we support more than gcc, we
2282 // will warn the user.
2283 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2284 if (DRE->hasQualifier())
2285 S.Diag(Loc, diag::warn_cleanup_ext);
2286 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2287 NI = DRE->getNameInfo();
2288 if (!FD) {
2289 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2290 << NI.getName();
2291 return;
2292 }
2293 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2294 if (ULE->hasExplicitTemplateArgs())
2295 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002296 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2297 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002298 if (!FD) {
2299 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2300 << NI.getName();
2301 if (ULE->getType() == S.Context.OverloadTy)
2302 S.NoteAllOverloadCandidates(ULE);
2303 return;
2304 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002305 } else {
2306 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002307 return;
2308 }
2309
Anders Carlssond277d792009-01-31 01:16:18 +00002310 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002311 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2312 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002313 return;
2314 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002315
Anders Carlsson723f55d2009-02-07 23:16:50 +00002316 // We're currently more strict than GCC about what function types we accept.
2317 // If this ever proves to be a problem it should be easy to fix.
2318 QualType Ty = S.Context.getPointerType(VD->getType());
2319 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002320 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2321 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002322 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2323 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002324 return;
2325 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002326
Michael Han99315932013-01-24 16:46:58 +00002327 D->addAttr(::new (S.Context)
2328 CleanupAttr(Attr.getRange(), S.Context, FD,
2329 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002330}
2331
Mike Stumpd3bb5572009-07-24 19:02:52 +00002332/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002333/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002334static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002335 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002336 uint64_t ArgIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002337 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002338 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002339
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002340 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002341 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002342
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002343 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2344 if (not_nsstring_type &&
2345 !isCFStringType(Ty, S.Context) &&
2346 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002347 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002348 // FIXME: Should highlight the actual expression that has the wrong type.
2349 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002350 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002351 << IdxExpr->getSourceRange();
2352 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002353 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002354 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002355 if (!isNSStringType(Ty, S.Context) &&
2356 !isCFStringType(Ty, S.Context) &&
2357 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002358 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002359 // FIXME: Should highlight the actual expression that has the wrong type.
2360 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002361 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002362 << IdxExpr->getSourceRange();
2363 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002364 }
2365
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002366 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2367 // because that has corrected for the implicit this parameter, and is zero-
2368 // based. The attribute expects what the user wrote explicitly.
2369 llvm::APSInt Val;
2370 IdxExpr->EvaluateAsInt(Val, S.Context);
2371
Michael Han99315932013-01-24 16:46:58 +00002372 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002373 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002374 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002375}
2376
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002377enum FormatAttrKind {
2378 CFStringFormat,
2379 NSStringFormat,
2380 StrftimeFormat,
2381 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002382 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002383 InvalidFormat
2384};
2385
2386/// getFormatAttrKind - Map from format attribute names to supported format
2387/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002388static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002389 return llvm::StringSwitch<FormatAttrKind>(Format)
2390 // Check for formats that get handled specially.
2391 .Case("NSString", NSStringFormat)
2392 .Case("CFString", CFStringFormat)
2393 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002394
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002395 // Otherwise, check for supported formats.
2396 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2397 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2398 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002399
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002400 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2401 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002402}
2403
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002404/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002405/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002406static void handleInitPriorityAttr(Sema &S, Decl *D,
2407 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002408 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002409 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2410 return;
2411 }
2412
Aaron Ballman4a611152013-11-27 16:34:09 +00002413 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002414 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2415 Attr.setInvalid();
2416 return;
2417 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002418 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002419 if (S.Context.getAsArrayType(T))
2420 T = S.Context.getBaseElementType(T);
2421 if (!T->getAs<RecordType>()) {
2422 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2423 Attr.setInvalid();
2424 return;
2425 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002426
2427 Expr *E = Attr.getArgAsExpr(0);
2428 uint32_t prioritynum;
2429 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002430 Attr.setInvalid();
2431 return;
2432 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002433
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002434 if (prioritynum < 101 || prioritynum > 65535) {
2435 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002436 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002437 Attr.setInvalid();
2438 return;
2439 }
Michael Han99315932013-01-24 16:46:58 +00002440 D->addAttr(::new (S.Context)
2441 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2442 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002443}
2444
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002445FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2446 IdentifierInfo *Format, int FormatIdx,
2447 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002448 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002449 // Check whether we already have an equivalent format attribute.
2450 for (specific_attr_iterator<FormatAttr>
2451 i = D->specific_attr_begin<FormatAttr>(),
2452 e = D->specific_attr_end<FormatAttr>();
2453 i != e ; ++i) {
2454 FormatAttr *f = *i;
2455 if (f->getType() == Format &&
2456 f->getFormatIdx() == FormatIdx &&
2457 f->getFirstArg() == FirstArg) {
2458 // If we don't have a valid location for this attribute, adopt the
2459 // location.
2460 if (f->getLocation().isInvalid())
2461 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002462 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002463 }
2464 }
2465
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002466 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2467 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002468}
2469
Mike Stumpd3bb5572009-07-24 19:02:52 +00002470/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002471/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002472static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002473 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002474 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002475 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002476 return;
2477 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002478
Chandler Carruth743682b2010-11-16 08:35:43 +00002479 // In C++ the implicit 'this' function parameter also counts, and they are
2480 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002481 bool HasImplicitThisParam = isInstanceMethod(D);
2482 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002483
Aaron Ballman00e99962013-08-31 01:11:41 +00002484 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2485 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002486
2487 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002488 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002489 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002490 // If we've modified the string name, we need a new identifier for it.
2491 II = &S.Context.Idents.get(Format);
2492 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002493
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002494 // Check for supported formats.
2495 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002496
2497 if (Kind == IgnoredFormat)
2498 return;
2499
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002500 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002501 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002502 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002503 return;
2504 }
2505
2506 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002507 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002508 uint32_t Idx;
2509 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002510 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002511
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002512 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002513 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002514 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002515 return;
2516 }
2517
2518 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002519 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002520
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002521 if (HasImplicitThisParam) {
2522 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002523 S.Diag(Attr.getLoc(),
2524 diag::err_format_attribute_implicit_this_format_string)
2525 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002526 return;
2527 }
2528 ArgIdx--;
2529 }
Mike Stump11289f42009-09-09 15:08:12 +00002530
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002531 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002532 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002533
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002534 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002535 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002536 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2537 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002538 return;
2539 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002540 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002541 // FIXME: do we need to check if the type is NSString*? What are the
2542 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002543 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002544 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002545 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2546 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002547 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002548 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002549 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002550 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002551 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002552 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2553 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002554 return;
2555 }
2556
2557 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002558 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002559 uint32_t FirstArg;
2560 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002561 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002562
2563 // check if the function is variadic if the 3rd argument non-zero
2564 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002565 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002566 ++NumArgs; // +1 for ...
2567 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002568 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002569 return;
2570 }
2571 }
2572
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002573 // strftime requires FirstArg to be 0 because it doesn't read from any
2574 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002575 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002576 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002577 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2578 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002579 return;
2580 }
2581 // if 0 it disables parameter checking (to use with e.g. va_list)
2582 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002583 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002584 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002585 return;
2586 }
2587
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002588 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002589 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002590 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002591 if (NewAttr)
2592 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002593}
2594
Chandler Carruthedc2c642011-07-02 00:01:44 +00002595static void handleTransparentUnionAttr(Sema &S, Decl *D,
2596 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002597 // Try to find the underlying union declaration.
2598 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002599 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002600 if (TD && TD->getUnderlyingType()->isUnionType())
2601 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2602 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002603 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002604
2605 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002606 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002607 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002608 return;
2609 }
2610
John McCallf937c022011-10-07 06:10:15 +00002611 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002612 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002613 diag::warn_transparent_union_attribute_not_definition);
2614 return;
2615 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002616
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002617 RecordDecl::field_iterator Field = RD->field_begin(),
2618 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002619 if (Field == FieldEnd) {
2620 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2621 return;
2622 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002623
David Blaikie40ed2972012-06-06 20:45:41 +00002624 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002625 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002626 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002627 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002628 diag::warn_transparent_union_attribute_floating)
2629 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002630 return;
2631 }
2632
2633 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2634 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2635 for (; Field != FieldEnd; ++Field) {
2636 QualType FieldType = Field->getType();
2637 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2638 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2639 // Warn if we drop the attribute.
2640 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002641 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002642 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002643 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002644 diag::warn_transparent_union_attribute_field_size_align)
2645 << isSize << Field->getDeclName() << FieldBits;
2646 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002647 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002648 diag::note_transparent_union_first_field_size_align)
2649 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002650 return;
2651 }
2652 }
2653
Michael Han99315932013-01-24 16:46:58 +00002654 RD->addAttr(::new (S.Context)
2655 TransparentUnionAttr(Attr.getRange(), S.Context,
2656 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002657}
2658
Chandler Carruthedc2c642011-07-02 00:01:44 +00002659static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002660 // Make sure that there is a string literal as the annotation's single
2661 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002662 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002663 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002664 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002665
2666 // Don't duplicate annotations that are already set.
2667 for (specific_attr_iterator<AnnotateAttr>
2668 i = D->specific_attr_begin<AnnotateAttr>(),
2669 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002670 if ((*i)->getAnnotation() == Str)
2671 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002672 }
Michael Han99315932013-01-24 16:46:58 +00002673
2674 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002675 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002676 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002677}
2678
Chandler Carruthedc2c642011-07-02 00:01:44 +00002679static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002680 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002681 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002682 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2683 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002684 return;
2685 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002686
Richard Smith848e1f12013-02-01 08:12:08 +00002687 if (Attr.getNumArgs() == 0) {
2688 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2689 true, 0, Attr.getAttributeSpellingListIndex()));
2690 return;
2691 }
2692
Aaron Ballman00e99962013-08-31 01:11:41 +00002693 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002694 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2695 S.Diag(Attr.getEllipsisLoc(),
2696 diag::err_pack_expansion_without_parameter_packs);
2697 return;
2698 }
2699
2700 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2701 return;
2702
2703 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2704 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002705}
2706
2707void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002708 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002709 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2710 SourceLocation AttrLoc = AttrRange.getBegin();
2711
Richard Smith1dba27c2013-01-29 09:02:09 +00002712 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002713 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002714 // C++11 [dcl.align]p1:
2715 // An alignment-specifier may be applied to a variable or to a class
2716 // data member, but it shall not be applied to a bit-field, a function
2717 // parameter, the formal parameter of a catch clause, or a variable
2718 // declared with the register storage class specifier. An
2719 // alignment-specifier may also be applied to the declaration of a class
2720 // or enumeration type.
2721 // C11 6.7.5/2:
2722 // An alignment attribute shall not be specified in a declaration of
2723 // a typedef, or a bit-field, or a function, or a parameter, or an
2724 // object declared with the register storage-class specifier.
2725 int DiagKind = -1;
2726 if (isa<ParmVarDecl>(D)) {
2727 DiagKind = 0;
2728 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2729 if (VD->getStorageClass() == SC_Register)
2730 DiagKind = 1;
2731 if (VD->isExceptionVariable())
2732 DiagKind = 2;
2733 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2734 if (FD->isBitField())
2735 DiagKind = 3;
2736 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00002737 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00002738 << (TmpAttr.isC11() ? ExpectedVariableOrField
2739 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002740 return;
2741 }
2742 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002743 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00002744 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002745 return;
2746 }
2747 }
2748
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002749 if (E->isTypeDependent() || E->isValueDependent()) {
2750 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002751 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2752 AA->setPackExpansion(IsPackExpansion);
2753 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002754 return;
2755 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002756
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002757 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002758 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002759 ExprResult ICE
2760 = VerifyIntegerConstantExpression(E, &Alignment,
2761 diag::err_aligned_attribute_argument_not_int,
2762 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002763 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002764 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002765
2766 // C++11 [dcl.align]p2:
2767 // -- if the constant expression evaluates to zero, the alignment
2768 // specifier shall have no effect
2769 // C11 6.7.5p6:
2770 // An alignment specification of zero has no effect.
2771 if (!(TmpAttr.isAlignas() && !Alignment) &&
2772 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002773 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2774 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002775 return;
2776 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002777
Richard Smith848e1f12013-02-01 08:12:08 +00002778 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00002779 // We've already verified it's a power of 2, now let's make sure it's
2780 // 8192 or less.
2781 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00002782 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00002783 << E->getSourceRange();
2784 return;
2785 }
2786 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002787
Richard Smith44c247f2013-02-22 08:32:16 +00002788 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2789 ICE.take(), SpellingListIndex);
2790 AA->setPackExpansion(IsPackExpansion);
2791 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002792}
2793
Michael Hanaf02bbe2013-02-01 01:19:17 +00002794void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002795 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002796 // FIXME: Cache the number on the Attr object if non-dependent?
2797 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002798 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2799 SpellingListIndex);
2800 AA->setPackExpansion(IsPackExpansion);
2801 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002802}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002803
Richard Smith848e1f12013-02-01 08:12:08 +00002804void Sema::CheckAlignasUnderalignment(Decl *D) {
2805 assert(D->hasAttrs() && "no attributes on decl");
2806
2807 QualType Ty;
2808 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2809 Ty = VD->getType();
2810 else
2811 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002812 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002813 return;
2814
2815 // C++11 [dcl.align]p5, C11 6.7.5/4:
2816 // The combined effect of all alignment attributes in a declaration shall
2817 // not specify an alignment that is less strict than the alignment that
2818 // would otherwise be required for the entity being declared.
2819 AlignedAttr *AlignasAttr = 0;
2820 unsigned Align = 0;
2821 for (specific_attr_iterator<AlignedAttr>
2822 I = D->specific_attr_begin<AlignedAttr>(),
2823 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
2824 if (I->isAlignmentDependent())
2825 return;
2826 if (I->isAlignas())
2827 AlignasAttr = *I;
2828 Align = std::max(Align, I->getAlignment(Context));
2829 }
2830
2831 if (AlignasAttr && Align) {
2832 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2833 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2834 if (NaturalAlign > RequestedAlign)
2835 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2836 << Ty << (unsigned)NaturalAlign.getQuantity();
2837 }
2838}
2839
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002840/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002841/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002842///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002843/// Despite what would be logical, the mode attribute is a decl attribute, not a
2844/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2845/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002846static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002847 // This attribute isn't documented, but glibc uses it. It changes
2848 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002849 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002850 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2851 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002852 return;
2853 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002854
Aaron Ballman00e99962013-08-31 01:11:41 +00002855 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2856 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002857
2858 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002859 if (Str.startswith("__") && Str.endswith("__"))
2860 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002861
2862 unsigned DestWidth = 0;
2863 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002864 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002865 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002866 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002867 switch (Str[0]) {
2868 case 'Q': DestWidth = 8; break;
2869 case 'H': DestWidth = 16; break;
2870 case 'S': DestWidth = 32; break;
2871 case 'D': DestWidth = 64; break;
2872 case 'X': DestWidth = 96; break;
2873 case 'T': DestWidth = 128; break;
2874 }
2875 if (Str[1] == 'F') {
2876 IntegerMode = false;
2877 } else if (Str[1] == 'C') {
2878 IntegerMode = false;
2879 ComplexMode = true;
2880 } else if (Str[1] != 'I') {
2881 DestWidth = 0;
2882 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002883 break;
2884 case 4:
2885 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2886 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002887 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002888 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002889 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002890 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002891 break;
2892 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002893 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002894 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002895 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002896 case 11:
2897 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00002898 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002899 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002900 }
2901
2902 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002903 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002904 OldTy = TD->getUnderlyingType();
2905 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2906 OldTy = VD->getType();
2907 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002908 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00002909 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002910 return;
2911 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002912
John McCall9dd450b2009-09-21 23:43:11 +00002913 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002914 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2915 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002916 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002917 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2918 } else if (ComplexMode) {
2919 if (!OldTy->isComplexType())
2920 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2921 } else {
2922 if (!OldTy->isFloatingType())
2923 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2924 }
2925
Mike Stump87c57ac2009-05-16 07:39:55 +00002926 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2927 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002928 // FIXME: Make sure floating-point mappings are accurate
2929 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002930 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00002931 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002932 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002933 }
2934
2935 QualType NewTy;
2936
2937 if (IntegerMode)
2938 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
2939 OldTy->isSignedIntegerType());
2940 else
2941 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
2942
2943 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00002944 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002945 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002946 }
2947
Eli Friedman4735374e2009-03-03 06:41:03 +00002948 if (ComplexMode) {
2949 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002950 }
2951
2952 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002953 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
2954 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
2955 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00002956 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002957
2958 D->addAttr(::new (S.Context)
2959 ModeAttr(Attr.getRange(), S.Context, Name,
2960 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00002961}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002962
Chandler Carruthedc2c642011-07-02 00:01:44 +00002963static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00002964 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2965 if (!VD->hasGlobalStorage())
2966 S.Diag(Attr.getLoc(),
2967 diag::warn_attribute_requires_functions_or_static_globals)
2968 << Attr.getName();
2969 } else if (!isFunctionOrMethod(D)) {
2970 S.Diag(Attr.getLoc(),
2971 diag::warn_attribute_requires_functions_or_static_globals)
2972 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00002973 return;
2974 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002975
Michael Han99315932013-01-24 16:46:58 +00002976 D->addAttr(::new (S.Context)
2977 NoDebugAttr(Attr.getRange(), S.Context,
2978 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00002979}
2980
Chandler Carruthedc2c642011-07-02 00:01:44 +00002981static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00002982 FunctionDecl *FD = cast<FunctionDecl>(D);
2983 if (!FD->getResultType()->isVoidType()) {
2984 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
2985 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
2986 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2987 << FD->getType()
2988 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
2989 "void");
2990 } else {
2991 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2992 << FD->getType();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00002993 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00002994 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002995 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002996
Aaron Ballman3aff6332013-12-02 19:30:36 +00002997 D->addAttr(::new (S.Context)
2998 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002999 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003000}
3001
Chandler Carruthedc2c642011-07-02 00:01:44 +00003002static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003003 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003004 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003005 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003006 return;
3007 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003008
Michael Han99315932013-01-24 16:46:58 +00003009 D->addAttr(::new (S.Context)
3010 GNUInlineAttr(Attr.getRange(), S.Context,
3011 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003012}
3013
Chandler Carruthedc2c642011-07-02 00:01:44 +00003014static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003015 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003016
Aaron Ballman02df2e02012-12-09 17:45:41 +00003017 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003018 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003019 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3020 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003021 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003022 return;
3023
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003024 if (!isa<ObjCMethodDecl>(D)) {
3025 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3026 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003027 return;
3028 }
3029
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003030 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003031 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003032 D->addAttr(::new (S.Context)
3033 FastCallAttr(Attr.getRange(), S.Context,
3034 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003035 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003036 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003037 D->addAttr(::new (S.Context)
3038 StdCallAttr(Attr.getRange(), S.Context,
3039 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003040 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003041 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003042 D->addAttr(::new (S.Context)
3043 ThisCallAttr(Attr.getRange(), S.Context,
3044 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003045 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003046 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003047 D->addAttr(::new (S.Context)
3048 CDeclAttr(Attr.getRange(), S.Context,
3049 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003050 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003051 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003052 D->addAttr(::new (S.Context)
3053 PascalAttr(Attr.getRange(), S.Context,
3054 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003055 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003056 case AttributeList::AT_MSABI:
3057 D->addAttr(::new (S.Context)
3058 MSABIAttr(Attr.getRange(), S.Context,
3059 Attr.getAttributeSpellingListIndex()));
3060 return;
3061 case AttributeList::AT_SysVABI:
3062 D->addAttr(::new (S.Context)
3063 SysVABIAttr(Attr.getRange(), S.Context,
3064 Attr.getAttributeSpellingListIndex()));
3065 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003066 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003067 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003068 switch (CC) {
3069 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003070 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003071 break;
3072 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003073 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003074 break;
3075 default:
3076 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003077 }
3078
Michael Han99315932013-01-24 16:46:58 +00003079 D->addAttr(::new (S.Context)
3080 PcsAttr(Attr.getRange(), S.Context, PCS,
3081 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003082 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003083 }
Derek Schuffa2020962012-10-16 22:30:41 +00003084 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003085 D->addAttr(::new (S.Context)
3086 PnaclCallAttr(Attr.getRange(), S.Context,
3087 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003088 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003089 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003090 D->addAttr(::new (S.Context)
3091 IntelOclBiccAttr(Attr.getRange(), S.Context,
3092 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003093 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003094
Abramo Bagnara50099372010-04-30 13:10:51 +00003095 default:
3096 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003097 }
3098}
3099
Aaron Ballman02df2e02012-12-09 17:45:41 +00003100bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3101 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003102 if (attr.isInvalid())
3103 return true;
3104
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003105 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003106 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003107 attr.setInvalid();
3108 return true;
3109 }
3110
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003111 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003112 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003113 case AttributeList::AT_CDecl: CC = CC_C; break;
3114 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3115 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3116 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3117 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003118 case AttributeList::AT_MSABI:
3119 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3120 CC_X86_64Win64;
3121 break;
3122 case AttributeList::AT_SysVABI:
3123 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3124 CC_C;
3125 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003126 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003127 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003128 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003129 attr.setInvalid();
3130 return true;
3131 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003132 if (StrRef == "aapcs") {
3133 CC = CC_AAPCS;
3134 break;
3135 } else if (StrRef == "aapcs-vfp") {
3136 CC = CC_AAPCS_VFP;
3137 break;
3138 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003139
3140 attr.setInvalid();
3141 Diag(attr.getLoc(), diag::err_invalid_pcs);
3142 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003143 }
Derek Schuffa2020962012-10-16 22:30:41 +00003144 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003145 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003146 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003147 }
3148
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003149 const TargetInfo &TI = Context.getTargetInfo();
3150 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3151 if (A == TargetInfo::CCCR_Warning) {
3152 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003153
3154 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3155 if (FD)
3156 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3157 TargetInfo::CCMT_NonMember;
3158 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003159 }
3160
John McCall3882ace2011-01-05 12:14:39 +00003161 return false;
3162}
3163
John McCall3882ace2011-01-05 12:14:39 +00003164/// Checks a regparm attribute, returning true if it is ill-formed and
3165/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003166bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3167 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003168 return true;
3169
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003170 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003171 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003172 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003173 }
Eli Friedman7044b762009-03-27 21:06:47 +00003174
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003175 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003176 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003177 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003178 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003179 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003180 }
3181
Douglas Gregore8bbc122011-09-02 00:18:52 +00003182 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003183 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003184 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003185 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003186 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003187 }
3188
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003189 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003190 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003191 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003192 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003193 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003194 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003195 }
3196
John McCall3882ace2011-01-05 12:14:39 +00003197 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003198}
3199
Aaron Ballman66039932013-12-19 00:41:31 +00003200static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3201 const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003202 // check the attribute arguments.
3203 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3204 // FIXME: 0 is not okay.
Aaron Ballman05e420a2014-01-02 21:26:14 +00003205 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3206 << Attr.getName() << 2;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003207 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003208 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003209
Aaron Ballman66039932013-12-19 00:41:31 +00003210 uint32_t MaxThreads, MinBlocks = 0;
3211 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3212 return;
3213 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3214 Attr.getArgAsExpr(1),
3215 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003216 return;
3217
3218 D->addAttr(::new (S.Context)
3219 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3220 MaxThreads, MinBlocks,
3221 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003222}
3223
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003224static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3225 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003226 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003227 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003228 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003229 return;
3230 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003231
3232 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003233 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003234
Aaron Ballman00e99962013-08-31 01:11:41 +00003235 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003236
3237 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3238 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3239 << Attr.getName() << ExpectedFunctionOrMethod;
3240 return;
3241 }
3242
3243 uint64_t ArgumentIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003244 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3245 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003246 return;
3247
3248 uint64_t TypeTagIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003249 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3250 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003251 return;
3252
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003253 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003254 if (IsPointer) {
3255 // Ensure that buffer has a pointer type.
3256 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3257 if (!BufferTy->isPointerType()) {
3258 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003259 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003260 }
3261 }
3262
Michael Han99315932013-01-24 16:46:58 +00003263 D->addAttr(::new (S.Context)
3264 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3265 ArgumentIdx, TypeTagIdx, IsPointer,
3266 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003267}
3268
3269static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3270 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003271 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003272 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003273 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003274 return;
3275 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003276
3277 if (!checkAttributeNumArgs(S, Attr, 1))
3278 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003279
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003280 if (!isa<VarDecl>(D)) {
3281 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3282 << Attr.getName() << ExpectedVariable;
3283 return;
3284 }
3285
Aaron Ballman00e99962013-08-31 01:11:41 +00003286 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003287 TypeSourceInfo *MatchingCTypeLoc = 0;
3288 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3289 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003290
Michael Han99315932013-01-24 16:46:58 +00003291 D->addAttr(::new (S.Context)
3292 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003293 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003294 Attr.getLayoutCompatible(),
3295 Attr.getMustBeNull(),
3296 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003297}
3298
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003299//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003300// Checker-specific attribute handlers.
3301//===----------------------------------------------------------------------===//
3302
John McCalled433932011-01-25 03:31:58 +00003303static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003304 return type->isDependentType() ||
3305 type->isObjCObjectPointerType() ||
3306 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003307}
3308static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003309 return type->isDependentType() ||
3310 type->isPointerType() ||
3311 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003312}
3313
Chandler Carruthedc2c642011-07-02 00:01:44 +00003314static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003315 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003316 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003317
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003318 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003319 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3320 cf = false;
3321 } else {
3322 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3323 cf = true;
3324 }
3325
3326 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003327 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003328 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003329 return;
3330 }
3331
3332 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003333 param->addAttr(::new (S.Context)
3334 CFConsumedAttr(Attr.getRange(), S.Context,
3335 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003336 else
Michael Han99315932013-01-24 16:46:58 +00003337 param->addAttr(::new (S.Context)
3338 NSConsumedAttr(Attr.getRange(), S.Context,
3339 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003340}
3341
Chandler Carruthedc2c642011-07-02 00:01:44 +00003342static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3343 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003344
John McCalled433932011-01-25 03:31:58 +00003345 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003346
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003347 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003348 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003349 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003350 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003351 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003352 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3353 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003354 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003355 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003356 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003357 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003358 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003359 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003360 return;
3361 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003362
John McCalled433932011-01-25 03:31:58 +00003363 bool typeOK;
3364 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003365 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003366 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003367 case AttributeList::AT_NSReturnsAutoreleased:
3368 case AttributeList::AT_NSReturnsRetained:
3369 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003370 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3371 cf = false;
3372 break;
3373
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003374 case AttributeList::AT_CFReturnsRetained:
3375 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003376 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3377 cf = true;
3378 break;
3379 }
3380
3381 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003382 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003383 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003384 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003385 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003386
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003387 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003388 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003389 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003390 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003391 D->addAttr(::new (S.Context)
3392 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3393 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003394 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003395 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003396 D->addAttr(::new (S.Context)
3397 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3398 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003399 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003400 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003401 D->addAttr(::new (S.Context)
3402 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3403 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003404 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003405 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003406 D->addAttr(::new (S.Context)
3407 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3408 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003409 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003410 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003411 D->addAttr(::new (S.Context)
3412 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3413 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003414 return;
3415 };
3416}
3417
John McCallcf166702011-07-22 08:53:00 +00003418static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3419 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003420 const int EP_ObjCMethod = 1;
3421 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003422
John McCallcf166702011-07-22 08:53:00 +00003423 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003424 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003425 if (isa<ObjCMethodDecl>(D))
3426 resultType = cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003427 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003428 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003429
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003430 if (!resultType->isReferenceType() &&
3431 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003432 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003433 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003434 << attr.getName()
3435 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003436 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003437
3438 // Drop the attribute.
3439 return;
3440 }
3441
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003442 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003443 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3444 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003445}
3446
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003447static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3448 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003449 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003450
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003451 DeclContext *DC = method->getDeclContext();
3452 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3453 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3454 << attr.getName() << 0;
3455 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3456 return;
3457 }
3458 if (method->getMethodFamily() == OMF_dealloc) {
3459 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3460 << attr.getName() << 1;
3461 return;
3462 }
3463
Michael Han99315932013-01-24 16:46:58 +00003464 method->addAttr(::new (S.Context)
3465 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3466 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003467}
3468
Aaron Ballmanfb763042013-12-02 18:05:46 +00003469static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3470 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003471 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00003472 return;
John McCall32f5fe12011-09-30 05:12:12 +00003473
Aaron Ballmanfb763042013-12-02 18:05:46 +00003474 D->addAttr(::new (S.Context)
3475 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3476 Attr.getAttributeSpellingListIndex()));
3477}
3478
3479static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3480 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003481 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00003482 return;
3483
3484 D->addAttr(::new (S.Context)
3485 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3486 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003487}
3488
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003489static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3490 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003491 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003492
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003493 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003494 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003495 return;
3496 }
3497
3498 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003499 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003500 Attr.getAttributeSpellingListIndex()));
3501}
3502
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003503static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3504 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003505 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003506
3507 if (!Parm) {
3508 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3509 return;
3510 }
3511
3512 D->addAttr(::new (S.Context)
3513 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3514 Attr.getAttributeSpellingListIndex()));
3515}
3516
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003517static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3518 const AttributeList &Attr) {
3519 IdentifierInfo *RelatedClass =
3520 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0;
3521 if (!RelatedClass) {
3522 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3523 return;
3524 }
3525 IdentifierInfo *ClassMethod =
3526 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0;
3527 IdentifierInfo *InstanceMethod =
3528 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0;
3529 D->addAttr(::new (S.Context)
3530 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3531 ClassMethod, InstanceMethod,
3532 Attr.getAttributeSpellingListIndex()));
3533}
3534
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003535static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3536 const AttributeList &Attr) {
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003537 ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003538 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003539 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003540 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3541 Attr.getAttributeSpellingListIndex()));
3542}
3543
Chandler Carruthedc2c642011-07-02 00:01:44 +00003544static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3545 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003546 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003547
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003548 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003549 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003550}
3551
Chandler Carruthedc2c642011-07-02 00:01:44 +00003552static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3553 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003554 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003555 QualType type = vd->getType();
3556
3557 if (!type->isDependentType() &&
3558 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003559 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003560 << type;
3561 return;
3562 }
3563
3564 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3565
3566 // If we have no lifetime yet, check the lifetime we're presumably
3567 // going to infer.
3568 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3569 lifetime = type->getObjCARCImplicitLifetime();
3570
3571 switch (lifetime) {
3572 case Qualifiers::OCL_None:
3573 assert(type->isDependentType() &&
3574 "didn't infer lifetime for non-dependent type?");
3575 break;
3576
3577 case Qualifiers::OCL_Weak: // meaningful
3578 case Qualifiers::OCL_Strong: // meaningful
3579 break;
3580
3581 case Qualifiers::OCL_ExplicitNone:
3582 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003583 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003584 << (lifetime == Qualifiers::OCL_Autoreleasing);
3585 break;
3586 }
3587
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003588 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003589 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3590 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003591}
3592
Francois Picheta83957a2010-12-19 06:50:37 +00003593//===----------------------------------------------------------------------===//
3594// Microsoft specific attribute handlers.
3595//===----------------------------------------------------------------------===//
3596
Chandler Carruthedc2c642011-07-02 00:01:44 +00003597static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003598 if (!S.LangOpts.CPlusPlus) {
3599 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3600 << Attr.getName() << AttributeLangSupport::C;
3601 return;
3602 }
3603
Aaron Ballman60e705e2013-11-24 20:58:02 +00003604 if (!isa<CXXRecordDecl>(D)) {
3605 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3606 << Attr.getName() << ExpectedClass;
3607 return;
3608 }
3609
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003610 StringRef StrRef;
3611 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003612 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003613 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003614
David Majnemer89085342013-08-09 08:56:20 +00003615 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3616 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003617 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3618 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003619
Reid Kleckner140c4a72013-05-17 14:04:52 +00003620 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003621 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003622 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003623 return;
3624 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003625
David Majnemer89085342013-08-09 08:56:20 +00003626 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003627 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003628 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003629 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003630 return;
3631 }
David Majnemer89085342013-08-09 08:56:20 +00003632 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003633 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003634 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003635 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003636 }
Francois Picheta83957a2010-12-19 06:50:37 +00003637
David Majnemer89085342013-08-09 08:56:20 +00003638 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3639 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003640}
3641
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003642static void handleARMInterruptAttr(Sema &S, Decl *D,
3643 const AttributeList &Attr) {
3644 // Check the attribute arguments.
3645 if (Attr.getNumArgs() > 1) {
3646 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3647 << Attr.getName() << 1;
3648 return;
3649 }
3650
3651 StringRef Str;
3652 SourceLocation ArgLoc;
3653
3654 if (Attr.getNumArgs() == 0)
3655 Str = "";
3656 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
3657 return;
3658
3659 ARMInterruptAttr::InterruptType Kind;
3660 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
3661 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
3662 << Attr.getName() << Str << ArgLoc;
3663 return;
3664 }
3665
3666 unsigned Index = Attr.getAttributeSpellingListIndex();
3667 D->addAttr(::new (S.Context)
3668 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
3669}
3670
3671static void handleMSP430InterruptAttr(Sema &S, Decl *D,
3672 const AttributeList &Attr) {
3673 if (!checkAttributeNumArgs(S, Attr, 1))
3674 return;
3675
3676 if (!Attr.isArgExpr(0)) {
3677 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3678 << AANT_ArgumentIntegerConstant;
3679 return;
3680 }
3681
3682 // FIXME: Check for decl - it should be void ()(void).
3683
3684 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3685 llvm::APSInt NumParams(32);
3686 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
3687 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3688 << Attr.getName() << AANT_ArgumentIntegerConstant
3689 << NumParamsExpr->getSourceRange();
3690 return;
3691 }
3692
3693 unsigned Num = NumParams.getLimitedValue(255);
3694 if ((Num & 1) || Num > 30) {
3695 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3696 << Attr.getName() << (int)NumParams.getSExtValue()
3697 << NumParamsExpr->getSourceRange();
3698 return;
3699 }
3700
Aaron Ballman36a53502014-01-16 13:03:14 +00003701 D->addAttr(::new (S.Context)
3702 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
3703 Attr.getAttributeSpellingListIndex()));
3704 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003705}
3706
3707static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3708 // Dispatch the interrupt attribute based on the current target.
3709 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
3710 handleMSP430InterruptAttr(S, D, Attr);
3711 else
3712 handleARMInterruptAttr(S, D, Attr);
3713}
3714
3715static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
3716 const AttributeList& Attr) {
3717 // If we try to apply it to a function pointer, don't warn, but don't
3718 // do anything, either. It doesn't matter anyway, because there's nothing
3719 // special about calling a force_align_arg_pointer function.
3720 ValueDecl *VD = dyn_cast<ValueDecl>(D);
3721 if (VD && VD->getType()->isFunctionPointerType())
3722 return;
3723 // Also don't warn on function pointer typedefs.
3724 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
3725 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
3726 TD->getUnderlyingType()->isFunctionType()))
3727 return;
3728 // Attribute can only be applied to function types.
3729 if (!isa<FunctionDecl>(D)) {
3730 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3731 << Attr.getName() << /* function */0;
3732 return;
3733 }
3734
Aaron Ballman36a53502014-01-16 13:03:14 +00003735 D->addAttr(::new (S.Context)
3736 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
3737 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003738}
3739
3740DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
3741 unsigned AttrSpellingListIndex) {
3742 if (D->hasAttr<DLLExportAttr>()) {
3743 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "dllimport";
3744 return NULL;
3745 }
3746
3747 if (D->hasAttr<DLLImportAttr>())
3748 return NULL;
3749
3750 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3751 if (VD->hasDefinition()) {
3752 // dllimport cannot be applied to definitions.
3753 Diag(D->getLocation(), diag::warn_attribute_invalid_on_definition)
3754 << "dllimport";
3755 return NULL;
3756 }
3757 }
3758
3759 return ::new (Context)DLLImportAttr(Range, Context,
3760 AttrSpellingListIndex);
3761}
3762
3763static void handleDLLImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3764 // Attribute can be applied only to functions or variables.
3765 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3766 if (!FD && !isa<VarDecl>(D)) {
3767 // Apparently Visual C++ thinks it is okay to not emit a warning
3768 // in this case, so only emit a warning when -fms-extensions is not
3769 // specified.
3770 if (!S.getLangOpts().MicrosoftExt)
3771 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3772 << Attr.getName() << 2 /*variable and function*/;
3773 return;
3774 }
3775
3776 // Currently, the dllimport attribute is ignored for inlined functions.
3777 // Warning is emitted.
3778 if (FD && FD->isInlineSpecified()) {
3779 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3780 return;
3781 }
3782
3783 unsigned Index = Attr.getAttributeSpellingListIndex();
3784 DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index);
3785 if (NewAttr)
3786 D->addAttr(NewAttr);
3787}
3788
3789DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
3790 unsigned AttrSpellingListIndex) {
3791 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
3792 Diag(Import->getLocation(), diag::warn_attribute_ignored) << "dllimport";
3793 D->dropAttr<DLLImportAttr>();
3794 }
3795
3796 if (D->hasAttr<DLLExportAttr>())
3797 return NULL;
3798
3799 return ::new (Context)DLLExportAttr(Range, Context,
3800 AttrSpellingListIndex);
3801}
3802
3803static void handleDLLExportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3804 // Currently, the dllexport attribute is ignored for inlined functions, unless
3805 // the -fkeep-inline-functions flag has been used. Warning is emitted;
3806 if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isInlineSpecified()) {
3807 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
3808 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3809 return;
3810 }
3811
3812 unsigned Index = Attr.getAttributeSpellingListIndex();
3813 DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index);
3814 if (NewAttr)
3815 D->addAttr(NewAttr);
3816}
3817
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003818/// Handles semantic checking for features that are common to all attributes,
3819/// such as checking whether a parameter was properly specified, or the correct
3820/// number of arguments were passed, etc.
3821static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3822 const AttributeList &Attr) {
3823 // Several attributes carry different semantics than the parsing requires, so
3824 // those are opted out of the common handling.
3825 //
3826 // We also bail on unknown and ignored attributes because those are handled
3827 // as part of the target-specific handling logic.
3828 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003829 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003830 return false;
3831
Aaron Ballman3aff6332013-12-02 19:30:36 +00003832 // Check whether the attribute requires specific language extensions to be
3833 // enabled.
3834 if (!Attr.diagnoseLangOpts(S))
3835 return true;
3836
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003837 // If there are no optional arguments, then checking for the argument count
3838 // is trivial.
3839 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
3840 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
3841 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003842
3843 // Check whether the attribute appertains to the given subject.
3844 if (!Attr.diagnoseAppertainsTo(S, D))
3845 return true;
3846
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003847 return false;
3848}
3849
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003850//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003851// Top Level Sema Entry Points
3852//===----------------------------------------------------------------------===//
3853
Richard Smithf8a75c32013-08-29 00:47:48 +00003854/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3855/// the attribute applies to decls. If the attribute is a type attribute, just
3856/// silently ignore it if a GNU attribute.
3857static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3858 const AttributeList &Attr,
3859 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003860 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00003861 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003862
Richard Smithf8a75c32013-08-29 00:47:48 +00003863 // Ignore C++11 attributes on declarator chunks: they appertain to the type
3864 // instead.
3865 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
3866 return;
3867
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003868 // Unknown attributes are automatically warned on. Target-specific attributes
3869 // which do not apply to the current target architecture are treated as
3870 // though they were unknown attributes.
3871 if (Attr.getKind() == AttributeList::UnknownAttribute ||
3872 !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) {
3873 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
3874 diag::warn_unhandled_ms_attribute_ignored :
3875 diag::warn_unknown_attribute_ignored) << Attr.getName();
3876 return;
3877 }
3878
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003879 if (handleCommonAttributeFeatures(S, scope, D, Attr))
3880 return;
3881
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003882 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003883 default:
3884 // Type attributes are handled elsewhere; silently move on.
3885 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
3886 break;
3887 case AttributeList::AT_Interrupt:
3888 handleInterruptAttr(S, D, Attr); break;
3889 case AttributeList::AT_X86ForceAlignArgPointer:
3890 handleX86ForceAlignArgPointerAttr(S, D, Attr); break;
3891 case AttributeList::AT_DLLExport:
3892 handleDLLExportAttr(S, D, Attr); break;
3893 case AttributeList::AT_DLLImport:
3894 handleDLLImportAttr(S, D, Attr); break;
3895 case AttributeList::AT_Mips16:
3896 handleSimpleAttribute<Mips16Attr>(S, D, Attr); break;
3897 case AttributeList::AT_NoMips16:
3898 handleSimpleAttribute<NoMips16Attr>(S, D, Attr); break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00003899 case AttributeList::AT_IBAction:
3900 handleSimpleAttribute<IBActionAttr>(S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003901 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
3902 case AttributeList::AT_IBOutletCollection:
3903 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003904 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
3905 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003906 case AttributeList::AT_AlwaysInline:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003907 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003908 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003909 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00003910 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003911 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
3912 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
3913 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00003914 handleDependencyAttr(S, scope, D, Attr);
3915 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003916 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003917 case AttributeList::AT_CUDAConstant:
3918 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003919 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003920 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman3a8e2d92013-11-27 18:53:58 +00003921 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003922 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00003923 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00003924 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003925 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
Nick Lewycky35a6ef42014-01-11 02:50:57 +00003926 case AttributeList::AT_EnableIf: handleEnableIfAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003927 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003928 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003929 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00003930 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003931 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00003932 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003933 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
3934 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
3935 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00003936 case AttributeList::AT_CUDADevice:
3937 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003938 case AttributeList::AT_CUDAHost:
3939 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003940 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
3941 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003942 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00003943 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003944 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003945 case AttributeList::AT_MayAlias:
3946 handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00003947 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003948 case AttributeList::AT_NoCommon:
3949 handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003950 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003951 case AttributeList::AT_Overloadable:
3952 handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00003953 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003954 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
3955 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003956 case AttributeList::AT_Naked:
3957 handleSimpleAttribute<NakedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003958 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00003959 case AttributeList::AT_NoThrow:
3960 handleSimpleAttribute<NoThrowAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003961 case AttributeList::AT_CUDAShared:
3962 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003963 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003964
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003965 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003966 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003967 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003968 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003969
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003970 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00003971 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3972
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003973 case AttributeList::AT_ObjCRequiresSuper:
3974 handleObjCRequiresSuperAttr(S, D, Attr); break;
3975
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003976 case AttributeList::AT_ObjCBridge:
3977 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003978
3979 case AttributeList::AT_ObjCBridgeMutable:
3980 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003981
3982 case AttributeList::AT_ObjCBridgeRelated:
3983 handleObjCBridgeRelatedAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00003984
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003985 case AttributeList::AT_ObjCDesignatedInitializer:
3986 handleObjCDesignatedInitializer(S, D, Attr); break;
3987
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003988 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00003989 handleCFAuditedTransferAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003990 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00003991 handleCFUnknownTransferAttr(S, D, Attr); break;
John McCall32f5fe12011-09-30 05:12:12 +00003992
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003993 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003994 case AttributeList::AT_CFConsumed:
3995 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
3996 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003997 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003998
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003999 case AttributeList::AT_NSReturnsAutoreleased:
4000 case AttributeList::AT_NSReturnsNotRetained:
4001 case AttributeList::AT_CFReturnsNotRetained:
4002 case AttributeList::AT_NSReturnsRetained:
4003 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004004 handleNSReturnsRetainedAttr(S, D, Attr); break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004005 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004006 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004007 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004008 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004009 case AttributeList::AT_VecTypeHint:
4010 handleVecTypeHint(S, D, Attr); break;
4011
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004012 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004013 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004014
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004015 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4016 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4017 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004018 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004019 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004020 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004021 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004022 case AttributeList::AT_ObjCRootClass:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004023 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004024 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek28eace62013-11-23 01:01:34 +00004025 handleObjCSuppresProtocolAttr(S, D, Attr);
4026 break;
4027 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004028 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00004029 case AttributeList::AT_Unused:
4030 handleSimpleAttribute<UnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004031 case AttributeList::AT_ReturnsTwice:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004032 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004033 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004034 case AttributeList::AT_Visibility:
4035 handleVisibilityAttr(S, D, Attr, false);
4036 break;
4037 case AttributeList::AT_TypeVisibility:
4038 handleVisibilityAttr(S, D, Attr, true);
4039 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004040 case AttributeList::AT_WarnUnused:
Aaron Ballman6a42b5a2013-11-27 16:59:17 +00004041 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004042 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004043 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004044 case AttributeList::AT_Weak:
4045 handleSimpleAttribute<WeakAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004046 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4047 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4048 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004049 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004050 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004051 case AttributeList::AT_ObjCException:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004052 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004053 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004054 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004055 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004056 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4057 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4058 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004059 case AttributeList::AT_Const:
4060 handleSimpleAttribute<ConstAttr>(S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004061 case AttributeList::AT_Pure:
4062 handleSimpleAttribute<PureAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004063 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4064 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004065 case AttributeList::AT_NoInline:
4066 handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004067 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004068 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004069 case AttributeList::AT_StdCall:
4070 case AttributeList::AT_CDecl:
4071 case AttributeList::AT_FastCall:
4072 case AttributeList::AT_ThisCall:
4073 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004074 case AttributeList::AT_MSABI:
4075 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004076 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004077 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004078 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004079 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004080 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004081 case AttributeList::AT_OpenCLKernel:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004082 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004083 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman26891332014-01-14 17:41:53 +00004084 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr); break;
John McCall8d32c052012-05-22 21:28:12 +00004085
4086 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004087 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004088 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004089 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004090 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004091 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004092 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004093 case AttributeList::AT_MSInheritance:
4094 handleSimpleAttribute<MSInheritanceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004095 case AttributeList::AT_ForceInline:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004096 handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004097 case AttributeList::AT_SelectAny:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004098 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004099
4100 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004101 case AttributeList::AT_AssertExclusiveLock:
4102 handleAssertExclusiveLockAttr(S, D, Attr);
4103 break;
4104 case AttributeList::AT_AssertSharedLock:
4105 handleAssertSharedLockAttr(S, D, Attr);
4106 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004107 case AttributeList::AT_GuardedVar:
Aaron Ballmane61b8b82013-12-02 15:02:49 +00004108 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004109 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004110 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004111 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004112 case AttributeList::AT_ScopedLockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004113 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004114 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004115 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004116 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004117 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004118 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004119 break;
4120 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004121 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004122 break;
4123 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004124 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004125 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004126 case AttributeList::AT_Lockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004127 handleSimpleAttribute<LockableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004128 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004129 handleGuardedByAttr(S, D, Attr);
4130 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004131 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004132 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004133 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004134 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004135 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004136 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004137 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004138 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004139 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004140 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004141 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004142 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004143 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004144 handleLockReturnedAttr(S, D, Attr);
4145 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004146 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004147 handleLocksExcludedAttr(S, D, Attr);
4148 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004149 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004150 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004151 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004152 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004153 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004154 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004155 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004156 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004157 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004158 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004159 handleUnlockFunAttr(S, D, Attr);
4160 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004161 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004162 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004163 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004164 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004165 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004166 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004167
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004168 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004169 case AttributeList::AT_Consumable:
4170 handleConsumableAttr(S, D, Attr);
4171 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004172 case AttributeList::AT_ConsumableAutoCast:
4173 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr); break;
4174 break;
4175 case AttributeList::AT_ConsumableSetOnRead:
4176 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr); break;
4177 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004178 case AttributeList::AT_CallableWhen:
4179 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004180 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004181 case AttributeList::AT_ParamTypestate:
4182 handleParamTypestateAttr(S, D, Attr);
4183 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004184 case AttributeList::AT_ReturnTypestate:
4185 handleReturnTypestateAttr(S, D, Attr);
4186 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004187 case AttributeList::AT_SetTypestate:
4188 handleSetTypestateAttr(S, D, Attr);
4189 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004190 case AttributeList::AT_TestTypestate:
4191 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004192 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004193
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004194 // Type safety attributes.
4195 case AttributeList::AT_ArgumentWithTypeTag:
4196 handleArgumentWithTypeTagAttr(S, D, Attr);
4197 break;
4198 case AttributeList::AT_TypeTagForDatatype:
4199 handleTypeTagForDatatypeAttr(S, D, Attr);
4200 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004201 }
4202}
4203
4204/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4205/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004206void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004207 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004208 bool IncludeCXX11Attributes) {
4209 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004210 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004211
Joey Gouly2cd9db12013-12-13 16:15:28 +00004212 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004213 // GCC accepts
4214 // static int a9 __attribute__((weakref));
4215 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004216 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00004217 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
4218 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00004219 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004220 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004221 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004222
4223 if (!D->hasAttr<OpenCLKernelAttr>()) {
4224 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004225 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
4226 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004227 D->setInvalidDecl();
4228 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004229 if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
4230 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004231 D->setInvalidDecl();
4232 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004233 if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
4234 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004235 D->setInvalidDecl();
4236 }
4237 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004238}
4239
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004240// Annotation attributes are the only attributes allowed after an access
4241// specifier.
4242bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4243 const AttributeList *AttrList) {
4244 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004245 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004246 handleAnnotateAttr(*this, ASDecl, *l);
4247 } else {
4248 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4249 return true;
4250 }
4251 }
4252
4253 return false;
4254}
4255
John McCall42856de2011-10-01 05:17:03 +00004256/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4257/// contains any decl attributes that we should warn about.
4258static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4259 for ( ; A; A = A->getNext()) {
4260 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004261 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004262 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4263
4264 if (A->getKind() == AttributeList::UnknownAttribute) {
4265 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4266 << A->getName() << A->getRange();
4267 } else {
4268 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4269 << A->getName() << A->getRange();
4270 }
4271 }
4272}
4273
4274/// checkUnusedDeclAttributes - Given a declarator which is not being
4275/// used to build a declaration, complain about any decl attributes
4276/// which might be lying around on it.
4277void Sema::checkUnusedDeclAttributes(Declarator &D) {
4278 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4279 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4280 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4281 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4282}
4283
Ryan Flynn7d470f32009-07-30 03:15:39 +00004284/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004285/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004286NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4287 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004288 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004289 NamedDecl *NewD = 0;
4290 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004291 FunctionDecl *NewFD;
4292 // FIXME: Missing call to CheckFunctionDeclaration().
4293 // FIXME: Mangling?
4294 // FIXME: Is the qualifier info correct?
4295 // FIXME: Is the DeclContext correct?
4296 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4297 Loc, Loc, DeclarationName(II),
4298 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004299 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004300 FD->hasPrototype(),
4301 false/*isConstexprSpecified*/);
4302 NewD = NewFD;
4303
4304 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004305 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004306
4307 // Fake up parameter variables; they are declared as if this were
4308 // a typedef.
4309 QualType FDTy = FD->getType();
4310 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4311 SmallVector<ParmVarDecl*, 16> Params;
4312 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4313 AE = FT->arg_type_end(); AI != AE; ++AI) {
4314 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4315 Param->setScopeInfo(0, Params.size());
4316 Params.push_back(Param);
4317 }
David Blaikie9c70e042011-09-21 18:16:56 +00004318 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004319 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004320 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4321 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004322 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004323 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004324 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004325 if (VD->getQualifier()) {
4326 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004327 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004328 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004329 }
4330 return NewD;
4331}
4332
James Dennett634962f2012-06-14 21:40:34 +00004333/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004334/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004335void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004336 if (W.getUsed()) return; // only do this once
4337 W.setUsed(true);
4338 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4339 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004340 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00004341 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
4342 W.getLocation()));
4343 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00004344 WeakTopLevelDecl.push_back(NewD);
4345 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4346 // to insert Decl at TU scope, sorry.
4347 DeclContext *SavedContext = CurContext;
4348 CurContext = Context.getTranslationUnitDecl();
4349 PushOnScopeChains(NewD, S);
4350 CurContext = SavedContext;
4351 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00004352 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004353 }
4354}
4355
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004356void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4357 // It's valid to "forward-declare" #pragma weak, in which case we
4358 // have to do this.
4359 LoadExternalWeakUndeclaredIdentifiers();
4360 if (!WeakUndeclaredIdentifiers.empty()) {
4361 NamedDecl *ND = NULL;
4362 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4363 if (VD->isExternC())
4364 ND = VD;
4365 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4366 if (FD->isExternC())
4367 ND = FD;
4368 if (ND) {
4369 if (IdentifierInfo *Id = ND->getIdentifier()) {
4370 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4371 = WeakUndeclaredIdentifiers.find(Id);
4372 if (I != WeakUndeclaredIdentifiers.end()) {
4373 WeakInfo W = I->second;
4374 DeclApplyPragmaWeak(S, ND, W);
4375 WeakUndeclaredIdentifiers[Id] = W;
4376 }
4377 }
4378 }
4379 }
4380}
4381
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004382/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4383/// it, apply them to D. This is a bit tricky because PD can have attributes
4384/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004385void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004386 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004387 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004388 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004389
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004390 // Walk the declarator structure, applying decl attributes that were in a type
4391 // position to the decl itself. This handles cases like:
4392 // int *__attr__(x)** D;
4393 // when X is a decl attribute.
4394 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4395 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004396 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004397
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004398 // Finally, apply any attributes on the decl itself.
4399 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004400 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004401}
John McCall28a6aea2009-11-04 02:18:39 +00004402
John McCall31168b02011-06-15 23:02:42 +00004403/// Is the given declaration allowed to use a forbidden type?
4404static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4405 // Private ivars are always okay. Unfortunately, people don't
4406 // always properly make their ivars private, even in system headers.
4407 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004408 // Function declarations in sys headers will be marked unavailable.
4409 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4410 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004411 return false;
4412
4413 // Require it to be declared in a system header.
4414 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4415}
4416
4417/// Handle a delayed forbidden-type diagnostic.
4418static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4419 Decl *decl) {
4420 if (decl && isForbiddenTypeAllowed(S, decl)) {
Aaron Ballman36a53502014-01-16 13:03:14 +00004421 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context,
4422 "this system declaration uses an unsupported type",
4423 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00004424 return;
4425 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004426 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004427 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004428 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004429 // kind of forbidden type messages on unavailable functions.
4430 if (FD->hasAttr<UnavailableAttr>() &&
4431 diag.getForbiddenTypeDiagnostic() ==
4432 diag::err_arc_array_param_no_ownership) {
4433 diag.Triggered = true;
4434 return;
4435 }
4436 }
John McCall31168b02011-06-15 23:02:42 +00004437
4438 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4439 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4440 diag.Triggered = true;
4441}
4442
John McCall2ec85372012-05-07 06:16:41 +00004443void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4444 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004445 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004446 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004447
John McCall2ec85372012-05-07 06:16:41 +00004448 // When delaying diagnostics to run in the context of a parsed
4449 // declaration, we only want to actually emit anything if parsing
4450 // succeeds.
4451 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004452
John McCall2ec85372012-05-07 06:16:41 +00004453 // We emit all the active diagnostics in this pool or any of its
4454 // parents. In general, we'll get one pool for the decl spec
4455 // and a child pool for each declarator; in a decl group like:
4456 // deprecated_typedef foo, *bar, baz();
4457 // only the declarator pops will be passed decls. This is correct;
4458 // we really do need to consider delayed diagnostics from the decl spec
4459 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004460 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004461 do {
John McCall6347b682012-05-07 06:16:58 +00004462 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004463 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4464 // This const_cast is a bit lame. Really, Triggered should be mutable.
4465 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004466 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004467 continue;
4468
John McCallc1465822011-02-14 07:13:47 +00004469 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004470 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004471 case DelayedDiagnostic::Unavailable:
4472 // Don't bother giving deprecation/unavailable diagnostics if
4473 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004474 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004475 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004476 break;
4477
4478 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004479 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004480 break;
John McCall31168b02011-06-15 23:02:42 +00004481
4482 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004483 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004484 break;
John McCall86121512010-01-27 03:50:35 +00004485 }
4486 }
John McCall2ec85372012-05-07 06:16:41 +00004487 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004488}
4489
John McCall6347b682012-05-07 06:16:58 +00004490/// Given a set of delayed diagnostics, re-emit them as if they had
4491/// been delayed in the current context instead of in the given pool.
4492/// Essentially, this just moves them to the current pool.
4493void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4494 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4495 assert(curPool && "re-emitting in undelayed context not supported");
4496 curPool->steal(pool);
4497}
4498
John McCall28a6aea2009-11-04 02:18:39 +00004499static bool isDeclDeprecated(Decl *D) {
4500 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004501 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004502 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004503 // A category implicitly has the availability of the interface.
4504 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4505 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004506 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4507 return false;
4508}
4509
Ted Kremenekb79ee572013-12-18 23:30:06 +00004510static bool isDeclUnavailable(Decl *D) {
4511 do {
4512 if (D->isUnavailable())
4513 return true;
4514 // A category implicitly has the availability of the interface.
4515 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4516 return CatD->getClassInterface()->isUnavailable();
4517 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4518 return false;
4519}
4520
Eli Friedman971bfa12012-08-08 21:52:41 +00004521static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004522DoEmitAvailabilityWarning(Sema &S,
4523 DelayedDiagnostic::DDKind K,
4524 Decl *Ctx,
4525 const NamedDecl *D,
4526 StringRef Message,
4527 SourceLocation Loc,
4528 const ObjCInterfaceDecl *UnknownObjCClass,
4529 const ObjCPropertyDecl *ObjCProperty) {
4530
4531 // Diagnostics for deprecated or unavailable.
4532 unsigned diag, diag_message, diag_fwdclass_message;
4533
4534 // Matches 'diag::note_property_attribute' options.
4535 unsigned property_note_select;
4536
4537 // Matches diag::note_availability_specified_here.
4538 unsigned available_here_select_kind;
4539
4540 // Don't warn if our current context is deprecated or unavailable.
4541 switch (K) {
4542 case DelayedDiagnostic::Deprecation:
4543 if (isDeclDeprecated(Ctx))
4544 return;
4545 diag = diag::warn_deprecated;
4546 diag_message = diag::warn_deprecated_message;
4547 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4548 property_note_select = /* deprecated */ 0;
4549 available_here_select_kind = /* deprecated */ 2;
4550 break;
4551
4552 case DelayedDiagnostic::Unavailable:
4553 if (isDeclUnavailable(Ctx))
4554 return;
4555 diag = diag::err_unavailable;
4556 diag_message = diag::err_unavailable_message;
4557 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4558 property_note_select = /* unavailable */ 1;
4559 available_here_select_kind = /* unavailable */ 0;
4560 break;
4561
4562 default:
4563 llvm_unreachable("Neither a deprecation or unavailable kind");
4564 }
4565
Eli Friedman971bfa12012-08-08 21:52:41 +00004566 DeclarationName Name = D->getDeclName();
4567 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004568 S.Diag(Loc, diag_message) << Name << Message;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004569 if (ObjCProperty)
4570 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4571 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004572 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004573 S.Diag(Loc, diag) << Name;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004574 if (ObjCProperty)
4575 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4576 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004577 } else {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004578 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004579 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4580 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004581
4582 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4583 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004584}
4585
Ted Kremenekb79ee572013-12-18 23:30:06 +00004586void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4587 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004588 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004589 DoEmitAvailabilityWarning(*this,
4590 (DelayedDiagnostic::DDKind) DD.Kind,
4591 Ctx,
4592 DD.getDeprecationDecl(),
4593 DD.getDeprecationMessage(),
4594 DD.Loc,
4595 DD.getUnknownObjCClass(),
4596 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004597}
4598
Ted Kremenekb79ee572013-12-18 23:30:06 +00004599void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4600 NamedDecl *D, StringRef Message,
4601 SourceLocation Loc,
4602 const ObjCInterfaceDecl *UnknownObjCClass,
4603 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004604 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004605 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004606 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4607 UnknownObjCClass,
4608 ObjCProperty,
4609 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004610 return;
4611 }
4612
Ted Kremenekb79ee572013-12-18 23:30:06 +00004613 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4614 DelayedDiagnostic::DDKind K;
4615 switch (AD) {
4616 case AD_Deprecation:
4617 K = DelayedDiagnostic::Deprecation;
4618 break;
4619 case AD_Unavailable:
4620 K = DelayedDiagnostic::Unavailable;
4621 break;
4622 }
4623
4624 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
4625 UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004626}