blob: ed8610cec39e1a6cc196aead3fe00ff0520eee28 [file] [log] [blame]
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000015#include "clang/AST/ASTContext.h"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000016#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000020#include "clang/AST/Expr.h"
Rafael Espindoladb77c4a2013-02-26 19:13:56 +000021#include "clang/AST/Mangle.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000022#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000023#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000024#include "clang/Basic/TargetInfo.h"
Benjamin Kramer6ee15622013-09-13 15:35:43 +000025#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000026#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000027#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000028#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000029#include "clang/Sema/Scope.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000030#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000031using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000032using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000033
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000034namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000035 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000036 C,
37 Cpp,
38 ObjC
39 };
40}
41
Chris Lattner58418ff2008-06-29 00:16:31 +000042//===----------------------------------------------------------------------===//
43// Helper functions
44//===----------------------------------------------------------------------===//
45
Ted Kremenek527042b2009-08-14 20:49:40 +000046/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000047/// type (function or function-typed variable) or an Objective-C
48/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000049static bool isFunctionOrMethod(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000050 return (D->getFunctionType() != NULL) || isa<ObjCMethodDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000051}
52
John McCall3882ace2011-01-05 12:14:39 +000053/// Return true if the given decl has a declarator that should have
54/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000055static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000056 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000057 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
58 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +000059}
60
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000061/// hasFunctionProto - Return true if the given decl has a argument
62/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000063/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000064static bool hasFunctionProto(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000065 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000066 return isa<FunctionProtoType>(FnTy);
Aaron Ballman12b9f652014-01-16 13:55:42 +000067 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000068}
69
70/// getFunctionOrMethodNumArgs - Return number of function or method
71/// arguments. It is an error to call this on a K&R function (use
72/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +000073static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000074 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000075 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000076 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000077 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000078 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000079}
80
Chandler Carruthff4c4f02011-07-01 23:49:12 +000081static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000082 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000083 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +000084 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000085 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +000086
Chandler Carruthff4c4f02011-07-01 23:49:12 +000087 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000088}
89
Chandler Carruthff4c4f02011-07-01 23:49:12 +000090static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000091 if (const FunctionType *FnTy = D->getFunctionType())
Fariborz Jahanianf1c25022009-05-20 17:41:43 +000092 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000093 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +000094}
95
Chandler Carruthff4c4f02011-07-01 23:49:12 +000096static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000097 if (const FunctionType *FnTy = D->getFunctionType()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +000098 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000099 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000100 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000101 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000102 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000103 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000104 }
105}
106
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000107static bool isInstanceMethod(const Decl *D) {
108 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000109 return MethodDecl->isInstance();
110 return false;
111}
112
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000113static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000114 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000115 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000116 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000117
John McCall96fa4842010-05-17 21:00:27 +0000118 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
119 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000120 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000121
John McCall96fa4842010-05-17 21:00:27 +0000122 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000123
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000124 // FIXME: Should we walk the chain of classes?
125 return ClsName == &Ctx.Idents.get("NSString") ||
126 ClsName == &Ctx.Idents.get("NSMutableString");
127}
128
Daniel Dunbar980c6692008-09-26 03:32:58 +0000129static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000130 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000131 if (!PT)
132 return false;
133
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000134 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000135 if (!RT)
136 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000137
Daniel Dunbar980c6692008-09-26 03:32:58 +0000138 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000139 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000140 return false;
141
142 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
143}
144
Richard Smithb87c4652013-10-31 21:23:20 +0000145static unsigned getNumAttributeArgs(const AttributeList &Attr) {
146 // FIXME: Include the type in the argument list.
147 return Attr.getNumArgs() + Attr.hasParsedType();
148}
149
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000150/// \brief Check if the attribute has exactly as many args as Num. May
151/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000152static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000153 unsigned Num) {
154 if (getNumAttributeArgs(Attr) != Num) {
Aaron Ballmanb7243382013-07-23 19:30:11 +0000155 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
156 << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000157 return false;
158 }
159
160 return true;
161}
162
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000163/// \brief Check if the attribute has at least as many args as Num. May
164/// output an error.
165static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000166 unsigned Num) {
167 if (getNumAttributeArgs(Attr) < Num) {
Aaron Ballman05e420a2014-01-02 21:26:14 +0000168 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments)
169 << Attr.getName() << Num;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000170 return false;
171 }
172
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000173 return true;
174}
175
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000176/// \brief If Expr is a valid integer constant, get the value of the integer
177/// expression and return success or failure. May output an error.
178static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
179 const Expr *Expr, uint32_t &Val,
180 unsigned Idx = UINT_MAX) {
181 llvm::APSInt I(32);
182 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
183 !Expr->isIntegerConstantExpr(I, S.Context)) {
184 if (Idx != UINT_MAX)
185 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
186 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
187 << Expr->getSourceRange();
188 else
189 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
190 << Attr.getName() << AANT_ArgumentIntegerConstant
191 << Expr->getSourceRange();
192 return false;
193 }
194 Val = (uint32_t)I.getZExtValue();
195 return true;
196}
197
Aaron Ballmanfb763042013-12-02 18:05:46 +0000198/// \brief Diagnose mutually exclusive attributes when present on a given
199/// declaration. Returns true if diagnosed.
200template <typename AttrTy>
201static bool checkAttrMutualExclusion(Sema &S, Decl *D,
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000202 const AttributeList &Attr) {
203 if (AttrTy *A = D->getAttr<AttrTy>()) {
Aaron Ballmanfb763042013-12-02 18:05:46 +0000204 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000205 << Attr.getName() << A;
Aaron Ballmanfb763042013-12-02 18:05:46 +0000206 return true;
207 }
208 return false;
209}
210
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000211/// \brief Check if IdxExpr is a valid argument index for a function or
212/// instance method D. May output an error.
213///
214/// \returns true if IdxExpr is a valid index.
215static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000216 const AttributeList &Attr,
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000217 unsigned AttrArgNum,
218 const Expr *IdxExpr,
219 uint64_t &Idx)
220{
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000221 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000222
223 // In C++ the implicit 'this' function parameter also counts.
224 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000225 bool HP = hasFunctionProto(D);
226 bool HasImplicitThisParam = isInstanceMethod(D);
227 bool IV = HP && isFunctionOrMethodVariadic(D);
228 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
229 HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000230
231 llvm::APSInt IdxInt;
232 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
233 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000234 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
235 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
236 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000237 return false;
238 }
239
240 Idx = IdxInt.getLimitedValue();
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000241 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000242 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
243 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000244 return false;
245 }
246 Idx--; // Convert to zero-based.
247 if (HasImplicitThisParam) {
248 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000249 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000250 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000251 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000252 return false;
253 }
254 --Idx;
255 }
256
257 return true;
258}
259
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000260/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
261/// If not emit an error and return false. If the argument is an identifier it
262/// will emit an error with a fixit hint and treat it as if it was a string
263/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000264bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
265 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000266 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000267 // Look for identifiers. If we have one emit a hint to fix it to a literal.
268 if (Attr.isArgIdent(ArgNum)) {
269 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000270 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000271 << Attr.getName() << AANT_ArgumentString
272 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000273 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000274 Str = Loc->Ident->getName();
275 if (ArgLocation)
276 *ArgLocation = Loc->Loc;
277 return true;
278 }
279
280 // Now check for an actual string literal.
281 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
282 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
283 if (ArgLocation)
284 *ArgLocation = ArgExpr->getLocStart();
285
286 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000287 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000288 << Attr.getName() << AANT_ArgumentString;
289 return false;
290 }
291
292 Str = Literal->getString();
293 return true;
294}
295
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000296/// \brief Applies the given attribute to the Decl without performing any
297/// additional semantic checking.
298template <typename AttrType>
299static void handleSimpleAttribute(Sema &S, Decl *D,
300 const AttributeList &Attr) {
301 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
302 Attr.getAttributeSpellingListIndex()));
303}
304
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000305/// \brief Check if the passed-in expression is of type int or bool.
306static bool isIntOrBool(Expr *Exp) {
307 QualType QT = Exp->getType();
308 return QT->isBooleanType() || QT->isIntegerType();
309}
310
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000311
312// Check to see if the type is a smart pointer of some kind. We assume
313// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000314static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
315 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
316 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000317 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000318 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000319
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000320 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
321 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000322 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000323 return false;
324
325 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000326}
327
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000328/// \brief Check if passed in Decl is a pointer type.
329/// Note that this function may produce an error message.
330/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000331static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
332 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000333 const ValueDecl *vd = cast<ValueDecl>(D);
334 QualType QT = vd->getType();
335 if (QT->isAnyPointerType())
336 return true;
337
338 if (const RecordType *RT = QT->getAs<RecordType>()) {
339 // If it's an incomplete type, it could be a smart pointer; skip it.
340 // (We don't want to force template instantiation if we can avoid it,
341 // since that would alter the order in which templates are instantiated.)
342 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000343 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000344
Aaron Ballman553e6812013-12-26 14:54:11 +0000345 if (threadSafetyCheckIsSmartPointer(S, RT))
346 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000347 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000348
349 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000350 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000351 return false;
352}
353
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000354/// \brief Checks that the passed in QualType either is of RecordType or points
355/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000356static const RecordType *getRecordType(QualType QT) {
357 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000358 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000359
360 // Now check if we point to record type.
361 if (const PointerType *PT = QT->getAs<PointerType>())
362 return PT->getPointeeType()->getAs<RecordType>();
363
364 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000365}
366
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000367
Jordy Rose740b0c22012-05-08 03:27:22 +0000368static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
369 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000370 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
Aaron Ballman9ead1242013-12-19 02:39:40 +0000371 return RT->getDecl()->hasAttr<LockableAttr>();
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000372}
373
374
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000375/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000376/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000377static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
378 QualType Ty) {
379 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000380
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000381 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000382 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000383 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
Aaron Ballman1da282a2014-01-02 23:15:58 +0000384 << Attr.getName() << Ty;
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000385 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000386 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000387
Michael Hana9171bc2012-08-03 17:40:43 +0000388 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000389 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000390 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000391
392 // Allow smart pointers to be used as lockable objects.
393 // FIXME -- Check the type that the smart pointer points to.
394 if (threadSafetyCheckIsSmartPointer(S, RT))
395 return;
396
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000397 // Check if the type is lockable.
398 RecordDecl *RD = RT->getDecl();
Aaron Ballman9ead1242013-12-19 02:39:40 +0000399 if (RD->hasAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000400 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000401
402 // Else check if any base classes are lockable.
403 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
404 CXXBasePaths BPaths(false, false);
405 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
406 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000407 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000408
409 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
Aaron Ballman1da282a2014-01-02 23:15:58 +0000410 << Attr.getName() << Ty;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000411}
412
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000413/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000414/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000415/// \param Sidx The attribute argument index to start checking with.
416/// \param ParamIdxOk Whether an argument can be indexing into a function
417/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000418static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000419 const AttributeList &Attr,
420 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000421 int Sidx = 0,
422 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000423 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000424 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000425
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000426 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000427 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000428 Args.push_back(ArgExp);
429 continue;
430 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000431
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000432 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000433 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000434 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000435 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000436 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000437 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000438 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000439 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000440
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000441 // We allow constant strings to be used as a placeholder for expressions
442 // that are not valid C++ syntax, but warn that they are ignored.
443 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
444 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000445 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000446 continue;
447 }
448
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000449 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000450
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000451 // A pointer to member expression of the form &MyClass::mu is treated
452 // specially -- we need to look at the type of the member.
453 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
454 if (UOp->getOpcode() == UO_AddrOf)
455 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
456 if (DRE->getDecl()->isCXXInstanceMember())
457 ArgTy = DRE->getDecl()->getType();
458
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000459 // First see if we can just cast to record type, or point to record type.
460 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000461
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000462 // Now check if we index into a record type function param.
463 if(!RT && ParamIdxOk) {
464 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000465 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
466 if(FD && IL) {
467 unsigned int NumParams = FD->getNumParams();
468 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000469 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
470 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
471 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000472 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
473 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000474 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000475 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000476 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000477 }
478 }
479
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000480 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000481
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000482 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000483 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000484}
485
Chris Lattner58418ff2008-06-29 00:16:31 +0000486//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000487// Attribute Implementations
488//===----------------------------------------------------------------------===//
489
Daniel Dunbar032db472008-07-31 22:40:48 +0000490// FIXME: All this manual attribute parsing code is gross. At the
491// least add some helper functions to check most argument patterns (#
492// and types of args).
493
Michael Hana9171bc2012-08-03 17:40:43 +0000494static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000495 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000496 if (!threadSafetyCheckIsPointer(S, D, Attr))
497 return;
498
Michael Han99315932013-01-24 16:46:58 +0000499 D->addAttr(::new (S.Context)
500 PtGuardedVarAttr(Attr.getRange(), S.Context,
501 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000502}
503
Michael Hana9171bc2012-08-03 17:40:43 +0000504static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
505 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000506 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000507 SmallVector<Expr*, 1> Args;
508 // check that all arguments are lockable objects
509 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
510 unsigned Size = Args.size();
511 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000512 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000513
Michael Han3be3b442012-07-23 18:48:41 +0000514 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000515
Michael Han3be3b442012-07-23 18:48:41 +0000516 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000517}
518
Michael Han3be3b442012-07-23 18:48:41 +0000519static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
520 Expr *Arg = 0;
521 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
522 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000523
Aaron Ballman36a53502014-01-16 13:03:14 +0000524 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg,
525 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000526}
527
Michael Hana9171bc2012-08-03 17:40:43 +0000528static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000529 const AttributeList &Attr) {
530 Expr *Arg = 0;
531 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
532 return;
533
534 if (!threadSafetyCheckIsPointer(S, D, Attr))
535 return;
536
537 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +0000538 S.Context, Arg,
539 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000540}
541
Michael Hana9171bc2012-08-03 17:40:43 +0000542static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
543 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000544 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000545 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000546 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000547
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000548 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000549 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000550 if (!QT->isDependentType()) {
551 const RecordType *RT = getRecordType(QT);
Aaron Ballman9ead1242013-12-19 02:39:40 +0000552 if (!RT || !RT->getDecl()->hasAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000553 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000554 << Attr.getName();
555 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000556 }
557 }
558
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000559 // Check that all arguments are lockable objects.
560 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000561 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000562 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000563
Michael Han3be3b442012-07-23 18:48:41 +0000564 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000565}
566
Michael Hana9171bc2012-08-03 17:40:43 +0000567static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000568 const AttributeList &Attr) {
569 SmallVector<Expr*, 1> Args;
570 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
571 return;
572
573 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000574 D->addAttr(::new (S.Context)
575 AcquiredAfterAttr(Attr.getRange(), S.Context,
576 StartArg, Args.size(),
577 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000578}
579
Michael Hana9171bc2012-08-03 17:40:43 +0000580static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000581 const AttributeList &Attr) {
582 SmallVector<Expr*, 1> Args;
583 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
584 return;
585
586 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000587 D->addAttr(::new (S.Context)
588 AcquiredBeforeAttr(Attr.getRange(), S.Context,
589 StartArg, Args.size(),
590 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000591}
592
Michael Hana9171bc2012-08-03 17:40:43 +0000593static bool checkLockFunAttrCommon(Sema &S, Decl *D,
594 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000595 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000596 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000597 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000598 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000599
Michael Han3be3b442012-07-23 18:48:41 +0000600 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000601}
602
Michael Hana9171bc2012-08-03 17:40:43 +0000603static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000604 const AttributeList &Attr) {
605 SmallVector<Expr*, 1> Args;
606 if (!checkLockFunAttrCommon(S, D, Attr, Args))
607 return;
608
609 unsigned Size = Args.size();
610 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000611 D->addAttr(::new (S.Context)
612 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
613 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000614}
615
Michael Hana9171bc2012-08-03 17:40:43 +0000616static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000617 const AttributeList &Attr) {
618 SmallVector<Expr*, 1> Args;
619 if (!checkLockFunAttrCommon(S, D, Attr, Args))
620 return;
621
622 unsigned Size = Args.size();
623 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000624 D->addAttr(::new (S.Context)
625 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
626 StartArg, Size,
627 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000628}
629
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000630static void handleAssertSharedLockAttr(Sema &S, Decl *D,
631 const AttributeList &Attr) {
632 SmallVector<Expr*, 1> Args;
633 if (!checkLockFunAttrCommon(S, D, Attr, Args))
634 return;
635
636 unsigned Size = Args.size();
637 Expr **StartArg = Size == 0 ? 0 : &Args[0];
638 D->addAttr(::new (S.Context)
639 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
640 Attr.getAttributeSpellingListIndex()));
641}
642
643static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
644 const AttributeList &Attr) {
645 SmallVector<Expr*, 1> Args;
646 if (!checkLockFunAttrCommon(S, D, Attr, Args))
647 return;
648
649 unsigned Size = Args.size();
650 Expr **StartArg = Size == 0 ? 0 : &Args[0];
651 D->addAttr(::new (S.Context)
652 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
653 StartArg, Size,
654 Attr.getAttributeSpellingListIndex()));
655}
656
657
Michael Hana9171bc2012-08-03 17:40:43 +0000658static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
659 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000660 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000661 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000662 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000663
Aaron Ballman00e99962013-08-31 01:11:41 +0000664 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000665 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000666 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000667 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000668 }
669
670 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000671 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000672
Michael Han3be3b442012-07-23 18:48:41 +0000673 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000674}
675
Michael Hana9171bc2012-08-03 17:40:43 +0000676static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000677 const AttributeList &Attr) {
678 SmallVector<Expr*, 2> Args;
679 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
680 return;
681
Michael Han99315932013-01-24 16:46:58 +0000682 D->addAttr(::new (S.Context)
683 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000684 Attr.getArgAsExpr(0),
685 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000686 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000687}
688
Michael Hana9171bc2012-08-03 17:40:43 +0000689static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000690 const AttributeList &Attr) {
691 SmallVector<Expr*, 2> Args;
692 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
693 return;
694
Michael Han99315932013-01-24 16:46:58 +0000695 D->addAttr(::new (S.Context)
696 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000697 Attr.getArgAsExpr(0),
698 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000699 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000700}
701
Michael Hana9171bc2012-08-03 17:40:43 +0000702static bool checkLocksRequiredCommon(Sema &S, Decl *D,
703 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000704 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000705 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000706 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000707
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000708 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000709 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000710 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000711 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000712
Michael Han3be3b442012-07-23 18:48:41 +0000713 return true;
714}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000715
Michael Hana9171bc2012-08-03 17:40:43 +0000716static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000717 const AttributeList &Attr) {
718 SmallVector<Expr*, 1> Args;
719 if (!checkLocksRequiredCommon(S, D, Attr, Args))
720 return;
721
722 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000723 D->addAttr(::new (S.Context)
724 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
725 StartArg, Args.size(),
726 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000727}
728
Michael Hana9171bc2012-08-03 17:40:43 +0000729static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000730 const AttributeList &Attr) {
731 SmallVector<Expr*, 1> Args;
732 if (!checkLocksRequiredCommon(S, D, Attr, Args))
733 return;
734
735 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000736 D->addAttr(::new (S.Context)
737 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
738 StartArg, Args.size(),
739 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000740}
741
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000742static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000743 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000744 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000745 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000746 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000747 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000748 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000749 Expr **StartArg = Size == 0 ? 0 : &Args[0];
750
Michael Han99315932013-01-24 16:46:58 +0000751 D->addAttr(::new (S.Context)
752 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
753 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000754}
755
756static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000757 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000758 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000759 SmallVector<Expr*, 1> Args;
760 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
761 unsigned Size = Args.size();
762 if (Size == 0)
763 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000764
Michael Han99315932013-01-24 16:46:58 +0000765 D->addAttr(::new (S.Context)
766 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
767 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000768}
769
770static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000771 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000772 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000773 return;
774
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000775 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000776 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000777 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000778 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000779 if (Size == 0)
780 return;
781 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000782
Michael Han99315932013-01-24 16:46:58 +0000783 D->addAttr(::new (S.Context)
784 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
785 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000786}
787
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000788static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
789 Expr *Cond = Attr.getArgAsExpr(0);
790 if (!Cond->isTypeDependent()) {
791 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
792 if (Converted.isInvalid())
793 return;
794 Cond = Converted.take();
795 }
796
797 StringRef Msg;
798 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
799 return;
800
801 SmallVector<PartialDiagnosticAt, 8> Diags;
802 if (!Cond->isValueDependent() &&
803 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
804 Diags)) {
805 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
806 for (int I = 0, N = Diags.size(); I != N; ++I)
807 S.Diag(Diags[I].first, Diags[I].second);
808 return;
809 }
810
811 D->addAttr(::new (S.Context)
812 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
813 Attr.getAttributeSpellingListIndex()));
814}
815
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000816static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000817 ConsumableAttr::ConsumedState DefaultState;
818
819 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000820 IdentifierLoc *IL = Attr.getArgAsIdent(0);
821 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
822 DefaultState)) {
823 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
824 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000825 return;
826 }
David Blaikie16f76d22013-09-06 01:28:43 +0000827 } else {
828 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
829 << Attr.getName() << AANT_ArgumentIdentifier;
830 return;
831 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000832
833 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000834 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000835 Attr.getAttributeSpellingListIndex()));
836}
837
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000838
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000839static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
840 const AttributeList &Attr) {
841 ASTContext &CurrContext = S.getASTContext();
842 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
843
844 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
845 if (!RD->hasAttr<ConsumableAttr>()) {
846 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
847 RD->getNameAsString();
848
849 return false;
850 }
851 }
852
853 return true;
854}
855
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000856
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000857static void handleCallableWhenAttr(Sema &S, Decl *D,
858 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000859 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
860 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000861
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000862 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
863 return;
864
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000865 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
866 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
867 CallableWhenAttr::ConsumedState CallableState;
868
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000869 StringRef StateString;
870 SourceLocation Loc;
871 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
872 return;
873
874 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000875 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000876 S.Diag(Loc, diag::warn_attribute_type_not_supported)
877 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000878 return;
879 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000880
881 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000882 }
883
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000884 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000885 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
886 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000887}
888
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000889
DeLesley Hutchins69391772013-10-17 23:23:53 +0000890static void handleParamTypestateAttr(Sema &S, Decl *D,
891 const AttributeList &Attr) {
892 if (!checkAttributeNumArgs(S, Attr, 1)) return;
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000893
DeLesley Hutchins69391772013-10-17 23:23:53 +0000894 ParamTypestateAttr::ConsumedState ParamState;
895
896 if (Attr.isArgIdent(0)) {
897 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
898 StringRef StateString = Ident->Ident->getName();
899
900 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
901 ParamState)) {
902 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
903 << Attr.getName() << StateString;
904 return;
905 }
906 } else {
907 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
908 Attr.getName() << AANT_ArgumentIdentifier;
909 return;
910 }
911
912 // FIXME: This check is currently being done in the analysis. It can be
913 // enabled here only after the parser propagates attributes at
914 // template specialization definition, not declaration.
915 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
916 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
917 //
918 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
919 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
920 // ReturnType.getAsString();
921 // return;
922 //}
923
924 D->addAttr(::new (S.Context)
925 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
926 Attr.getAttributeSpellingListIndex()));
927}
928
929
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000930static void handleReturnTypestateAttr(Sema &S, Decl *D,
931 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000932 if (!checkAttributeNumArgs(S, Attr, 1)) return;
933
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000934 ReturnTypestateAttr::ConsumedState ReturnState;
935
936 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000937 IdentifierLoc *IL = Attr.getArgAsIdent(0);
938 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
939 ReturnState)) {
940 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
941 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000942 return;
943 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000944 } else {
945 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
946 Attr.getName() << AANT_ArgumentIdentifier;
947 return;
948 }
949
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000950 // FIXME: This check is currently being done in the analysis. It can be
951 // enabled here only after the parser propagates attributes at
952 // template specialization definition, not declaration.
953 //QualType ReturnType;
954 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000955 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
956 // ReturnType = Param->getType();
957 //
958 //} else if (const CXXConstructorDecl *Constructor =
959 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000960 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
961 //
962 //} else {
963 //
964 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
965 //}
966 //
967 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
968 //
969 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
970 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
971 // ReturnType.getAsString();
972 // return;
973 //}
974
975 D->addAttr(::new (S.Context)
976 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
977 Attr.getAttributeSpellingListIndex()));
978}
979
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000980
981static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000982 if (!checkAttributeNumArgs(S, Attr, 1))
983 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000984
985 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
986 return;
987
988 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000989 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000990 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
991 StringRef Param = Ident->Ident->getName();
992 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
993 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
994 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000995 return;
996 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000997 } else {
998 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
999 Attr.getName() << AANT_ArgumentIdentifier;
1000 return;
1001 }
1002
1003 D->addAttr(::new (S.Context)
1004 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1005 Attr.getAttributeSpellingListIndex()));
1006}
1007
Chris Wailes9385f9f2013-10-29 20:28:41 +00001008static void handleTestTypestateAttr(Sema &S, Decl *D,
1009 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001010 if (!checkAttributeNumArgs(S, Attr, 1))
1011 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001012
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001013 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1014 return;
1015
Chris Wailes9385f9f2013-10-29 20:28:41 +00001016 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001017 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001018 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1019 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001020 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001021 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1022 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001023 return;
1024 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001025 } else {
1026 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1027 Attr.getName() << AANT_ArgumentIdentifier;
1028 return;
1029 }
1030
1031 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001032 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001033 Attr.getAttributeSpellingListIndex()));
1034}
1035
Chandler Carruthedc2c642011-07-02 00:01:44 +00001036static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1037 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001038 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001039 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001040}
1041
Chandler Carruthedc2c642011-07-02 00:01:44 +00001042static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001043 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Aaron Ballman36a53502014-01-16 13:03:14 +00001044 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context,
1045 Attr.getAttributeSpellingListIndex()));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001046 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001047 // If the alignment is less than or equal to 8 bits, the packed attribute
1048 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001049 if (!FD->getType()->isDependentType() &&
1050 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001051 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001052 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001053 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001054 else
Michael Han99315932013-01-24 16:46:58 +00001055 FD->addAttr(::new (S.Context)
1056 PackedAttr(Attr.getRange(), S.Context,
1057 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001058 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001059 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001060}
1061
Ted Kremenek7fd17232011-09-29 07:02:25 +00001062static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1063 // The IBOutlet/IBOutletCollection attributes only apply to instance
1064 // variables or properties of Objective-C classes. The outlet must also
1065 // have an object reference type.
1066 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1067 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001068 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001069 << Attr.getName() << VD->getType() << 0;
1070 return false;
1071 }
1072 }
1073 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1074 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001075 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001076 << Attr.getName() << PD->getType() << 1;
1077 return false;
1078 }
1079 }
1080 else {
1081 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1082 return false;
1083 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001084
Ted Kremenek7fd17232011-09-29 07:02:25 +00001085 return true;
1086}
1087
Chandler Carruthedc2c642011-07-02 00:01:44 +00001088static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001089 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001090 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001091
Michael Han99315932013-01-24 16:46:58 +00001092 D->addAttr(::new (S.Context)
1093 IBOutletAttr(Attr.getRange(), S.Context,
1094 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001095}
1096
Chandler Carruthedc2c642011-07-02 00:01:44 +00001097static void handleIBOutletCollection(Sema &S, Decl *D,
1098 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001099
1100 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001101 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001102 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1103 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001104 return;
1105 }
1106
Ted Kremenek7fd17232011-09-29 07:02:25 +00001107 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001108 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001109
Richard Smithb1f9a282013-10-31 01:56:18 +00001110 ParsedType PT;
1111
1112 if (Attr.hasParsedType())
1113 PT = Attr.getTypeArg();
1114 else {
1115 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1116 S.getScopeForContext(D->getDeclContext()->getParent()));
1117 if (!PT) {
1118 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1119 return;
1120 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001121 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001122
Richard Smithb87c4652013-10-31 21:23:20 +00001123 TypeSourceInfo *QTLoc = 0;
1124 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1125 if (!QTLoc)
1126 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001127
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001128 // Diagnose use of non-object type in iboutletcollection attribute.
1129 // FIXME. Gnu attribute extension ignores use of builtin types in
1130 // attributes. So, __attribute__((iboutletcollection(char))) will be
1131 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001132 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001133 S.Diag(Attr.getLoc(),
1134 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1135 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001136 return;
1137 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001138
Michael Han99315932013-01-24 16:46:58 +00001139 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001140 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001141 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001142}
1143
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001144static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001145 if (const RecordType *UT = T->getAsUnionType())
1146 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1147 RecordDecl *UD = UT->getDecl();
1148 for (RecordDecl::field_iterator it = UD->field_begin(),
1149 itend = UD->field_end(); it != itend; ++it) {
1150 QualType QT = it->getType();
1151 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1152 T = QT;
1153 return;
1154 }
1155 }
1156 }
1157}
1158
Ted Kremenek9aedc152014-01-17 06:24:56 +00001159static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001160 SourceRange R, bool isReturnValue = false) {
Ted Kremenek9aedc152014-01-17 06:24:56 +00001161 T = T.getNonReferenceType();
1162 possibleTransparentUnionPointerType(T);
1163
1164 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001165 S.Diag(Attr.getLoc(),
1166 isReturnValue ? diag::warn_attribute_return_pointers_only
1167 : diag::warn_attribute_pointers_only)
Ted Kremenek9aedc152014-01-17 06:24:56 +00001168 << Attr.getName() << R;
1169 return false;
1170 }
1171 return true;
1172}
1173
1174static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1175 const AttributeList &Attr) {
1176 // Is the argument a pointer type?
1177 if (!attrNonNullArgCheck(S, D->getType(), Attr, D->getSourceRange()))
1178 return;
1179
1180 if (Attr.getNumArgs() > 0) {
1181 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1182 << D->getSourceRange();
1183 return;
1184 }
1185
1186 D->addAttr(::new (S.Context)
1187 NonNullAttr(Attr.getRange(), S.Context, 0, 0,
1188 Attr.getAttributeSpellingListIndex()));
1189}
1190
Chandler Carruthedc2c642011-07-02 00:01:44 +00001191static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001192 SmallVector<unsigned, 8> NonNullArgs;
1193 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001194 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001195 uint64_t Idx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00001196 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001197 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001198
1199 // Is the function argument a pointer type?
Ted Kremenek9aedc152014-01-17 06:24:56 +00001200 // FIXME: Should also highlight argument in decl in the diagnostic.
1201 if (!attrNonNullArgCheck(S, getFunctionOrMethodArgType(D, Idx),
1202 Attr, Ex->getSourceRange()))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001203 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001204
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001205 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001206 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001207
1208 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1209 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001210 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001211 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1212 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001213 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001214 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001215 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001216 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001217
Ted Kremenek22813f42010-10-21 18:49:36 +00001218 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001219 if (NonNullArgs.empty()) {
1220 // Warn the trivial case only if attribute is not coming from a
1221 // macro instantiation.
1222 if (Attr.getLoc().isFileID())
1223 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001224 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001225 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001226 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001227
Nick Lewyckye1121512013-01-24 01:12:16 +00001228 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001229 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001230 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001231 D->addAttr(::new (S.Context)
1232 NonNullAttr(Attr.getRange(), S.Context, start, size,
1233 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001234}
1235
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001236static void handleReturnsNonNullAttr(Sema &S, Decl *D,
1237 const AttributeList &Attr) {
1238 QualType ResultType;
1239 if (const FunctionType *Ty = D->getFunctionType())
1240 ResultType = Ty->getResultType();
1241 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1242 ResultType = MD->getResultType();
1243
1244 if (!attrNonNullArgCheck(S, ResultType, Attr, Attr.getRange(),
1245 /* isReturnValue */ true))
1246 return;
1247
1248 D->addAttr(::new (S.Context)
1249 ReturnsNonNullAttr(Attr.getRange(), S.Context,
1250 Attr.getAttributeSpellingListIndex()));
1251}
1252
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001253static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1254 switch (K) {
1255 case OwnershipAttr::Holds: return "'ownership_holds'";
1256 case OwnershipAttr::Takes: return "'ownership_takes'";
1257 case OwnershipAttr::Returns: return "'ownership_returns'";
1258 }
1259 llvm_unreachable("unknown ownership");
1260}
1261
Chandler Carruthedc2c642011-07-02 00:01:44 +00001262static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001263 // This attribute must be applied to a function declaration. The first
1264 // argument to the attribute must be an identifier, the name of the resource,
1265 // for example: malloc. The following arguments must be argument indexes, the
1266 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001267 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001268 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001269 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001270
Aaron Ballman00e99962013-08-31 01:11:41 +00001271 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001272 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001273 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001274 return;
1275 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001276
Richard Smith852e9ce2013-11-27 01:46:48 +00001277 // Figure out our Kind.
1278 OwnershipAttr::OwnershipKind K =
1279 OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0,
1280 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001281
Richard Smith852e9ce2013-11-27 01:46:48 +00001282 // Check arguments.
1283 switch (K) {
1284 case OwnershipAttr::Takes:
1285 case OwnershipAttr::Holds:
1286 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001287 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1288 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001289 return;
1290 }
1291 break;
1292 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001293 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001294 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1295 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001296 return;
1297 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001298 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001299 }
1300
Richard Smith852e9ce2013-11-27 01:46:48 +00001301 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001302
1303 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001304 StringRef ModuleName = Module->getName();
1305 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1306 ModuleName.size() > 4) {
1307 ModuleName = ModuleName.drop_front(2).drop_back(2);
1308 Module = &S.PP.getIdentifierTable().get(ModuleName);
1309 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001310
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001311 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001312 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1313 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001314 uint64_t Idx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00001315 if (!checkFunctionOrMethodArgumentIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001316 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001317
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001318 // Is the function argument a pointer type?
1319 QualType T = getFunctionOrMethodArgType(D, Idx);
1320 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001321 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001322 case OwnershipAttr::Takes:
1323 case OwnershipAttr::Holds:
1324 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1325 Err = 0;
1326 break;
1327 case OwnershipAttr::Returns:
1328 if (!T->isIntegerType())
1329 Err = 1;
1330 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001331 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001332 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001333 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001334 << Ex->getSourceRange();
1335 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001336 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001337
1338 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001339 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001340 i = D->specific_attr_begin<OwnershipAttr>(),
1341 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001342 // FIXME: A returns attribute should conflict with any returns attribute
1343 // with a different index too.
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001344 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1345 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1346 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1347 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1348 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001349 }
1350 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001351 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001352 }
1353
1354 unsigned* start = OwnershipArgs.data();
1355 unsigned size = OwnershipArgs.size();
1356 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001357
Michael Han99315932013-01-24 16:46:58 +00001358 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001359 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001360 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001361}
1362
Chandler Carruthedc2c642011-07-02 00:01:44 +00001363static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001364 // Check the attribute arguments.
1365 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001366 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1367 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001368 return;
1369 }
1370
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001371 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001372
Rafael Espindolac18086a2010-02-23 22:00:30 +00001373 // gcc rejects
1374 // class c {
1375 // static int a __attribute__((weakref ("v2")));
1376 // static int b() __attribute__((weakref ("f3")));
1377 // };
1378 // and ignores the attributes of
1379 // void f(void) {
1380 // static int a __attribute__((weakref ("v2")));
1381 // }
1382 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001383 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001384 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001385 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1386 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001387 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001388 }
1389
1390 // The GCC manual says
1391 //
1392 // At present, a declaration to which `weakref' is attached can only
1393 // be `static'.
1394 //
1395 // It also says
1396 //
1397 // Without a TARGET,
1398 // given as an argument to `weakref' or to `alias', `weakref' is
1399 // equivalent to `weak'.
1400 //
1401 // gcc 4.4.1 will accept
1402 // int a7 __attribute__((weakref));
1403 // as
1404 // int a7 __attribute__((weak));
1405 // This looks like a bug in gcc. We reject that for now. We should revisit
1406 // it if this behaviour is actually used.
1407
Rafael Espindolac18086a2010-02-23 22:00:30 +00001408 // GCC rejects
1409 // static ((alias ("y"), weakref)).
1410 // Should we? How to check that weakref is before or after alias?
1411
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001412 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1413 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1414 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001415 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001416 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001417 // GCC will accept anything as the argument of weakref. Should we
1418 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001419 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1420 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001421
Michael Han99315932013-01-24 16:46:58 +00001422 D->addAttr(::new (S.Context)
1423 WeakRefAttr(Attr.getRange(), S.Context,
1424 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001425}
1426
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001427static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1428 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001429 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001430 return;
1431
Douglas Gregore8bbc122011-09-02 00:18:52 +00001432 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001433 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1434 return;
1435 }
1436
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001437 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001438
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001439 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001440 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001441}
1442
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001443static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001444 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001445 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001446
Michael Han99315932013-01-24 16:46:58 +00001447 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1448 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001449}
1450
1451static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001452 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001453 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001454
Michael Han99315932013-01-24 16:46:58 +00001455 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1456 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001457}
1458
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001459static void handleTLSModelAttr(Sema &S, Decl *D,
1460 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001461 StringRef Model;
1462 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001463 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001464 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001465 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001466
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001467 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001468 if (Model != "global-dynamic" && Model != "local-dynamic"
1469 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001470 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001471 return;
1472 }
1473
Michael Han99315932013-01-24 16:46:58 +00001474 D->addAttr(::new (S.Context)
1475 TLSModelAttr(Attr.getRange(), S.Context, Model,
1476 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001477}
1478
Chandler Carruthedc2c642011-07-02 00:01:44 +00001479static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001480 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001481 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001482 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001483 D->addAttr(::new (S.Context)
1484 MallocAttr(Attr.getRange(), S.Context,
1485 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001486 return;
1487 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001488 }
1489
Ted Kremenek08479ae2009-08-15 00:51:46 +00001490 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001491}
1492
Chandler Carruthedc2c642011-07-02 00:01:44 +00001493static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001494 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001495 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1496 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001497 return;
1498 }
1499
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001500 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1501 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001502}
1503
Chandler Carruthedc2c642011-07-02 00:01:44 +00001504static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001505 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001506
1507 if (S.CheckNoReturnAttr(attr)) return;
1508
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001509 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001510 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001511 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001512 return;
1513 }
1514
Michael Han99315932013-01-24 16:46:58 +00001515 D->addAttr(::new (S.Context)
1516 NoReturnAttr(attr.getRange(), S.Context,
1517 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001518}
1519
1520bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001521 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001522 attr.setInvalid();
1523 return true;
1524 }
1525
1526 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001527}
1528
Chandler Carruthedc2c642011-07-02 00:01:44 +00001529static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1530 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001531
1532 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1533 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001534 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1535 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001536 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1537 && !VD->getType()->isFunctionPointerType())) {
1538 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001539 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001540 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001541 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001542 return;
1543 }
1544 }
1545
Michael Han99315932013-01-24 16:46:58 +00001546 D->addAttr(::new (S.Context)
1547 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1548 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001549}
1550
John Thompsoncdb847ba2010-08-09 21:53:52 +00001551// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001552static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001553/*
1554 Returning a Vector Class in Registers
1555
Eric Christopherbc638a82010-12-01 22:13:54 +00001556 According to the PPU ABI specifications, a class with a single member of
1557 vector type is returned in memory when used as the return value of a function.
1558 This results in inefficient code when implementing vector classes. To return
1559 the value in a single vector register, add the vecreturn attribute to the
1560 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001561
1562 Example:
1563
1564 struct Vector
1565 {
1566 __vector float xyzw;
1567 } __attribute__((vecreturn));
1568
1569 Vector Add(Vector lhs, Vector rhs)
1570 {
1571 Vector result;
1572 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1573 return result; // This will be returned in a register
1574 }
1575*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001576 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1577 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001578 return;
1579 }
1580
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001581 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001582 int count = 0;
1583
1584 if (!isa<CXXRecordDecl>(record)) {
1585 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1586 return;
1587 }
1588
1589 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1590 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1591 return;
1592 }
1593
Eric Christopherbc638a82010-12-01 22:13:54 +00001594 for (RecordDecl::field_iterator iter = record->field_begin();
1595 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001596 if ((count == 1) || !iter->getType()->isVectorType()) {
1597 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1598 return;
1599 }
1600 count++;
1601 }
1602
Michael Han99315932013-01-24 16:46:58 +00001603 D->addAttr(::new (S.Context)
1604 VecReturnAttr(Attr.getRange(), S.Context,
1605 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001606}
1607
Richard Smithe233fbf2013-01-28 22:42:45 +00001608static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1609 const AttributeList &Attr) {
1610 if (isa<ParmVarDecl>(D)) {
1611 // [[carries_dependency]] can only be applied to a parameter if it is a
1612 // parameter of a function declaration or lambda.
1613 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1614 S.Diag(Attr.getLoc(),
1615 diag::err_carries_dependency_param_not_function_decl);
1616 return;
1617 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001618 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001619
1620 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1621 Attr.getRange(), S.Context,
1622 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001623}
1624
Chandler Carruthedc2c642011-07-02 00:01:44 +00001625static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001626 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001627 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001628 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001629 return;
1630 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001631 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001632 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001633 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001634 return;
1635 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001636
Michael Han99315932013-01-24 16:46:58 +00001637 D->addAttr(::new (S.Context)
1638 UsedAttr(Attr.getRange(), S.Context,
1639 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001640}
1641
Chandler Carruthedc2c642011-07-02 00:01:44 +00001642static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001643 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001644 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001645 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1646 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001647 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001648 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001649
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001650 uint32_t priority = ConstructorAttr::DefaultPriority;
1651 if (Attr.getNumArgs() > 0 &&
1652 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1653 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001654
Michael Han99315932013-01-24 16:46:58 +00001655 D->addAttr(::new (S.Context)
1656 ConstructorAttr(Attr.getRange(), S.Context, priority,
1657 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001658}
1659
Chandler Carruthedc2c642011-07-02 00:01:44 +00001660static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001661 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001662 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001663 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1664 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001665 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001666 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001667
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001668 uint32_t priority = ConstructorAttr::DefaultPriority;
1669 if (Attr.getNumArgs() > 0 &&
1670 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1671 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001672
Michael Han99315932013-01-24 16:46:58 +00001673 D->addAttr(::new (S.Context)
1674 DestructorAttr(Attr.getRange(), S.Context, priority,
1675 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001676}
1677
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001678template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001679static void handleAttrWithMessage(Sema &S, Decl *D,
1680 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001681 unsigned NumArgs = Attr.getNumArgs();
1682 if (NumArgs > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001683 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1684 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001685 return;
1686 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001687
1688 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001689 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001690 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001691 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001692
Michael Han99315932013-01-24 16:46:58 +00001693 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1694 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001695}
1696
Ted Kremenek28eace62013-11-23 01:01:34 +00001697static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
1698 const AttributeList &Attr) {
Ted Kremenek28eace62013-11-23 01:01:34 +00001699 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001700 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1701 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001702}
1703
Jordy Rose740b0c22012-05-08 03:27:22 +00001704static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1705 IdentifierInfo *Platform,
1706 VersionTuple Introduced,
1707 VersionTuple Deprecated,
1708 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001709 StringRef PlatformName
1710 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1711 if (PlatformName.empty())
1712 PlatformName = Platform->getName();
1713
1714 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1715 // of these steps are needed).
1716 if (!Introduced.empty() && !Deprecated.empty() &&
1717 !(Introduced <= Deprecated)) {
1718 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1719 << 1 << PlatformName << Deprecated.getAsString()
1720 << 0 << Introduced.getAsString();
1721 return true;
1722 }
1723
1724 if (!Introduced.empty() && !Obsoleted.empty() &&
1725 !(Introduced <= Obsoleted)) {
1726 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1727 << 2 << PlatformName << Obsoleted.getAsString()
1728 << 0 << Introduced.getAsString();
1729 return true;
1730 }
1731
1732 if (!Deprecated.empty() && !Obsoleted.empty() &&
1733 !(Deprecated <= Obsoleted)) {
1734 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1735 << 2 << PlatformName << Obsoleted.getAsString()
1736 << 1 << Deprecated.getAsString();
1737 return true;
1738 }
1739
1740 return false;
1741}
1742
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001743/// \brief Check whether the two versions match.
1744///
1745/// If either version tuple is empty, then they are assumed to match. If
1746/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1747static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1748 bool BeforeIsOkay) {
1749 if (X.empty() || Y.empty())
1750 return true;
1751
1752 if (X == Y)
1753 return true;
1754
1755 if (BeforeIsOkay && X < Y)
1756 return true;
1757
1758 return false;
1759}
1760
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001761AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001762 IdentifierInfo *Platform,
1763 VersionTuple Introduced,
1764 VersionTuple Deprecated,
1765 VersionTuple Obsoleted,
1766 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001767 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001768 bool Override,
1769 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001770 VersionTuple MergedIntroduced = Introduced;
1771 VersionTuple MergedDeprecated = Deprecated;
1772 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001773 bool FoundAny = false;
1774
Rafael Espindolac67f2232012-05-10 02:50:16 +00001775 if (D->hasAttrs()) {
1776 AttrVec &Attrs = D->getAttrs();
1777 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1778 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1779 if (!OldAA) {
1780 ++i;
1781 continue;
1782 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001783
Rafael Espindolac67f2232012-05-10 02:50:16 +00001784 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1785 if (OldPlatform != Platform) {
1786 ++i;
1787 continue;
1788 }
1789
1790 FoundAny = true;
1791 VersionTuple OldIntroduced = OldAA->getIntroduced();
1792 VersionTuple OldDeprecated = OldAA->getDeprecated();
1793 VersionTuple OldObsoleted = OldAA->getObsoleted();
1794 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001795
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001796 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1797 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1798 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1799 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001800 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001801 if (Override) {
1802 int Which = -1;
1803 VersionTuple FirstVersion;
1804 VersionTuple SecondVersion;
1805 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1806 Which = 0;
1807 FirstVersion = OldIntroduced;
1808 SecondVersion = Introduced;
1809 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1810 Which = 1;
1811 FirstVersion = Deprecated;
1812 SecondVersion = OldDeprecated;
1813 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1814 Which = 2;
1815 FirstVersion = Obsoleted;
1816 SecondVersion = OldObsoleted;
1817 }
1818
1819 if (Which == -1) {
1820 Diag(OldAA->getLocation(),
1821 diag::warn_mismatched_availability_override_unavail)
1822 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1823 } else {
1824 Diag(OldAA->getLocation(),
1825 diag::warn_mismatched_availability_override)
1826 << Which
1827 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1828 << FirstVersion.getAsString() << SecondVersion.getAsString();
1829 }
1830 Diag(Range.getBegin(), diag::note_overridden_method);
1831 } else {
1832 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1833 Diag(Range.getBegin(), diag::note_previous_attribute);
1834 }
1835
Rafael Espindolac67f2232012-05-10 02:50:16 +00001836 Attrs.erase(Attrs.begin() + i);
1837 --e;
1838 continue;
1839 }
1840
1841 VersionTuple MergedIntroduced2 = MergedIntroduced;
1842 VersionTuple MergedDeprecated2 = MergedDeprecated;
1843 VersionTuple MergedObsoleted2 = MergedObsoleted;
1844
1845 if (MergedIntroduced2.empty())
1846 MergedIntroduced2 = OldIntroduced;
1847 if (MergedDeprecated2.empty())
1848 MergedDeprecated2 = OldDeprecated;
1849 if (MergedObsoleted2.empty())
1850 MergedObsoleted2 = OldObsoleted;
1851
1852 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1853 MergedIntroduced2, MergedDeprecated2,
1854 MergedObsoleted2)) {
1855 Attrs.erase(Attrs.begin() + i);
1856 --e;
1857 continue;
1858 }
1859
1860 MergedIntroduced = MergedIntroduced2;
1861 MergedDeprecated = MergedDeprecated2;
1862 MergedObsoleted = MergedObsoleted2;
1863 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001864 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001865 }
1866
1867 if (FoundAny &&
1868 MergedIntroduced == Introduced &&
1869 MergedDeprecated == Deprecated &&
1870 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001871 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001872
Ted Kremenekb5445722013-04-06 00:34:27 +00001873 // Only create a new attribute if !Override, but we want to do
1874 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001875 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001876 MergedDeprecated, MergedObsoleted) &&
1877 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001878 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1879 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001880 Obsoleted, IsUnavailable, Message,
1881 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001882 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001883 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001884}
1885
Chandler Carruthedc2c642011-07-02 00:01:44 +00001886static void handleAvailabilityAttr(Sema &S, Decl *D,
1887 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001888 if (!checkAttributeNumArgs(S, Attr, 1))
1889 return;
1890 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001891 unsigned Index = Attr.getAttributeSpellingListIndex();
1892
Aaron Ballman00e99962013-08-31 01:11:41 +00001893 IdentifierInfo *II = Platform->Ident;
1894 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1895 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1896 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001897
Rafael Espindolac231fab2013-01-08 21:30:32 +00001898 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1899 if (!ND) {
1900 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1901 return;
1902 }
1903
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001904 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1905 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1906 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001907 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001908 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001909 if (const StringLiteral *SE =
1910 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001911 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001912
Aaron Ballman00e99962013-08-31 01:11:41 +00001913 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001914 Introduced.Version,
1915 Deprecated.Version,
1916 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001917 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001918 /*Override=*/false,
1919 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001920 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001921 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001922}
1923
John McCalld041a9b2013-02-20 01:54:26 +00001924template <class T>
1925static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1926 typename T::VisibilityType value,
1927 unsigned attrSpellingListIndex) {
1928 T *existingAttr = D->getAttr<T>();
1929 if (existingAttr) {
1930 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1931 if (existingValue == value)
1932 return NULL;
1933 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1934 S.Diag(range.getBegin(), diag::note_previous_attribute);
1935 D->dropAttr<T>();
1936 }
1937 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1938}
1939
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001940VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001941 VisibilityAttr::VisibilityType Vis,
1942 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001943 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1944 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001945}
1946
John McCalld041a9b2013-02-20 01:54:26 +00001947TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1948 TypeVisibilityAttr::VisibilityType Vis,
1949 unsigned AttrSpellingListIndex) {
1950 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1951 AttrSpellingListIndex);
1952}
1953
1954static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1955 bool isTypeVisibility) {
1956 // Visibility attributes don't mean anything on a typedef.
1957 if (isa<TypedefNameDecl>(D)) {
1958 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
1959 << Attr.getName();
1960 return;
1961 }
1962
1963 // 'type_visibility' can only go on a type or namespace.
1964 if (isTypeVisibility &&
1965 !(isa<TagDecl>(D) ||
1966 isa<ObjCInterfaceDecl>(D) ||
1967 isa<NamespaceDecl>(D))) {
1968 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
1969 << Attr.getName() << ExpectedTypeOrNamespace;
1970 return;
1971 }
1972
Benjamin Kramer70370212013-09-09 15:08:57 +00001973 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001974 StringRef TypeStr;
1975 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001976 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001977 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001978
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001979 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00001980 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001981 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00001982 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001983 return;
1984 }
Aaron Ballman682ee422013-09-11 19:47:58 +00001985
1986 // Complain about attempts to use protected visibility on targets
1987 // (like Darwin) that don't support it.
1988 if (type == VisibilityAttr::Protected &&
1989 !S.Context.getTargetInfo().hasProtectedVisibility()) {
1990 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1991 type = VisibilityAttr::Default;
1992 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001993
Michael Han99315932013-01-24 16:46:58 +00001994 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00001995 clang::Attr *newAttr;
1996 if (isTypeVisibility) {
1997 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
1998 (TypeVisibilityAttr::VisibilityType) type,
1999 Index);
2000 } else {
2001 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2002 }
2003 if (newAttr)
2004 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002005}
2006
Chandler Carruthedc2c642011-07-02 00:01:44 +00002007static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2008 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002009 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002010 if (!Attr.isArgIdent(0)) {
2011 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2012 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002013 return;
2014 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002015
Aaron Ballman682ee422013-09-11 19:47:58 +00002016 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2017 ObjCMethodFamilyAttr::FamilyKind F;
2018 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2019 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2020 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002021 return;
2022 }
2023
Aaron Ballman682ee422013-09-11 19:47:58 +00002024 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002025 !method->getResultType()->isObjCObjectPointerType()) {
2026 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2027 << method->getResultType();
2028 // Ignore the attribute.
2029 return;
2030 }
2031
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002032 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00002033 S.Context, F,
2034 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002035}
2036
Chandler Carruthedc2c642011-07-02 00:01:44 +00002037static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002038 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002039 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002040 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002041 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2042 return;
2043 }
2044 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002045 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2046 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002047 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002048 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2049 return;
2050 }
2051 }
2052 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002053 // It is okay to include this attribute on properties, e.g.:
2054 //
2055 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2056 //
2057 // In this case it follows tradition and suppresses an error in the above
2058 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002059 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002060 }
Michael Han99315932013-01-24 16:46:58 +00002061 D->addAttr(::new (S.Context)
2062 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2063 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002064}
2065
Chandler Carruthedc2c642011-07-02 00:01:44 +00002066static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002067 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002068 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002069 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002070 return;
2071 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002072
Aaron Ballman00e99962013-08-31 01:11:41 +00002073 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002074 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002075 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2076 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2077 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002078 return;
2079 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002080
Michael Han99315932013-01-24 16:46:58 +00002081 D->addAttr(::new (S.Context)
2082 BlocksAttr(Attr.getRange(), S.Context, type,
2083 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002084}
2085
Chandler Carruthedc2c642011-07-02 00:01:44 +00002086static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002087 // check the attribute arguments.
2088 if (Attr.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00002089 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
2090 << Attr.getName() << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002091 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002092 }
2093
Aaron Ballman18a78382013-11-21 00:28:23 +00002094 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002095 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002096 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002097 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002098 if (E->isTypeDependent() || E->isValueDependent() ||
2099 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002100 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002101 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002102 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002103 return;
2104 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002105
John McCallb46f2872011-09-09 07:56:05 +00002106 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002107 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2108 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002109 return;
2110 }
John McCallb46f2872011-09-09 07:56:05 +00002111
2112 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002113 }
2114
Aaron Ballman18a78382013-11-21 00:28:23 +00002115 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002116 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002117 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002118 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002119 if (E->isTypeDependent() || E->isValueDependent() ||
2120 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002121 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002122 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002123 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002124 return;
2125 }
2126 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002127
John McCallb46f2872011-09-09 07:56:05 +00002128 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002129 // FIXME: This error message could be improved, it would be nice
2130 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002131 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2132 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002133 return;
2134 }
2135 }
2136
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002137 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002138 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002139 if (isa<FunctionNoProtoType>(FT)) {
2140 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2141 return;
2142 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002143
Chris Lattner9363e312009-03-17 23:03:47 +00002144 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002145 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002146 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002147 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002148 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002149 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002150 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002151 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002152 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002153 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2154 if (!BD->isVariadic()) {
2155 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2156 return;
2157 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002158 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002159 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002160 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002161 const FunctionType *FT = Ty->isFunctionPointerType()
2162 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002163 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002164 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002165 int m = Ty->isFunctionPointerType() ? 0 : 1;
2166 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002167 return;
2168 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002169 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002170 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002171 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002172 return;
2173 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002174 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002175 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002176 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002177 return;
2178 }
Michael Han99315932013-01-24 16:46:58 +00002179 D->addAttr(::new (S.Context)
2180 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2181 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002182}
2183
Chandler Carruthedc2c642011-07-02 00:01:44 +00002184static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002185 if (D->getFunctionType() && D->getFunctionType()->getResultType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002186 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2187 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002188 return;
2189 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002190 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2191 if (MD->getResultType()->isVoidType()) {
2192 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2193 << Attr.getName() << 1;
2194 return;
2195 }
2196
Michael Han99315932013-01-24 16:46:58 +00002197 D->addAttr(::new (S.Context)
2198 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2199 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002200}
2201
Chandler Carruthedc2c642011-07-02 00:01:44 +00002202static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002203 // weak_import only applies to variable & function declarations.
2204 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002205 if (!D->canBeWeakImported(isDef)) {
2206 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002207 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2208 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002209 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002210 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002211 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002212 // Nothing to warn about here.
2213 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002214 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002215 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002216
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002217 return;
2218 }
2219
Michael Han99315932013-01-24 16:46:58 +00002220 D->addAttr(::new (S.Context)
2221 WeakImportAttr(Attr.getRange(), S.Context,
2222 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002223}
2224
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002225// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002226template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002227static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002228 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002229 uint32_t WGSize[3];
2230 for (unsigned i = 0; i < 3; ++i)
2231 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002232 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002233
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002234 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2235 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2236 Existing->getYDim() == WGSize[1] &&
2237 Existing->getZDim() == WGSize[2]))
2238 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002239
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002240 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2241 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002242 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002243}
2244
Joey Goulyaba589c2013-03-08 09:42:32 +00002245static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002246 if (!Attr.hasParsedType()) {
2247 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2248 << Attr.getName() << 1;
2249 return;
2250 }
2251
Richard Smithb87c4652013-10-31 21:23:20 +00002252 TypeSourceInfo *ParmTSI = 0;
2253 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2254 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002255
2256 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2257 (ParmType->isBooleanType() ||
2258 !ParmType->isIntegralType(S.getASTContext()))) {
2259 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2260 << ParmType;
2261 return;
2262 }
2263
Aaron Ballmana9e05402013-12-02 22:16:55 +00002264 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002265 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002266 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2267 return;
2268 }
2269 }
2270
2271 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002272 ParmTSI,
2273 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002274}
2275
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002276SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002277 StringRef Name,
2278 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002279 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2280 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002281 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002282 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2283 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002284 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002285 }
Michael Han99315932013-01-24 16:46:58 +00002286 return ::new (Context) SectionAttr(Range, Context, Name,
2287 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002288}
2289
Chandler Carruthedc2c642011-07-02 00:01:44 +00002290static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002291 // Make sure that there is a string literal as the sections's single
2292 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002293 StringRef Str;
2294 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002295 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002296 return;
Mike Stump11289f42009-09-09 15:08:12 +00002297
Chris Lattner30ba6742009-08-10 19:03:04 +00002298 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002299 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002300 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002301 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002302 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002303 return;
2304 }
Mike Stump11289f42009-09-09 15:08:12 +00002305
Michael Han99315932013-01-24 16:46:58 +00002306 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002307 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002308 if (NewAttr)
2309 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002310}
2311
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002312
Chandler Carruthedc2c642011-07-02 00:01:44 +00002313static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002314 VarDecl *VD = cast<VarDecl>(D);
2315 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002316 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002317 return;
2318 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002319
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002320 Expr *E = Attr.getArgAsExpr(0);
2321 SourceLocation Loc = E->getExprLoc();
2322 FunctionDecl *FD = 0;
2323 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002324
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002325 // gcc only allows for simple identifiers. Since we support more than gcc, we
2326 // will warn the user.
2327 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2328 if (DRE->hasQualifier())
2329 S.Diag(Loc, diag::warn_cleanup_ext);
2330 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2331 NI = DRE->getNameInfo();
2332 if (!FD) {
2333 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2334 << NI.getName();
2335 return;
2336 }
2337 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2338 if (ULE->hasExplicitTemplateArgs())
2339 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002340 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2341 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002342 if (!FD) {
2343 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2344 << NI.getName();
2345 if (ULE->getType() == S.Context.OverloadTy)
2346 S.NoteAllOverloadCandidates(ULE);
2347 return;
2348 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002349 } else {
2350 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002351 return;
2352 }
2353
Anders Carlssond277d792009-01-31 01:16:18 +00002354 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002355 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2356 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002357 return;
2358 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002359
Anders Carlsson723f55d2009-02-07 23:16:50 +00002360 // We're currently more strict than GCC about what function types we accept.
2361 // If this ever proves to be a problem it should be easy to fix.
2362 QualType Ty = S.Context.getPointerType(VD->getType());
2363 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002364 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2365 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002366 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2367 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002368 return;
2369 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002370
Michael Han99315932013-01-24 16:46:58 +00002371 D->addAttr(::new (S.Context)
2372 CleanupAttr(Attr.getRange(), S.Context, FD,
2373 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002374}
2375
Mike Stumpd3bb5572009-07-24 19:02:52 +00002376/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002377/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002378static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002379 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002380 uint64_t ArgIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002381 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002382 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002383
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002384 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002385 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002386
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002387 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2388 if (not_nsstring_type &&
2389 !isCFStringType(Ty, S.Context) &&
2390 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002391 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002392 // FIXME: Should highlight the actual expression that has the wrong type.
2393 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002394 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002395 << IdxExpr->getSourceRange();
2396 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002397 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002398 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002399 if (!isNSStringType(Ty, S.Context) &&
2400 !isCFStringType(Ty, S.Context) &&
2401 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002402 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002403 // FIXME: Should highlight the actual expression that has the wrong type.
2404 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002405 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002406 << IdxExpr->getSourceRange();
2407 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002408 }
2409
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002410 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2411 // because that has corrected for the implicit this parameter, and is zero-
2412 // based. The attribute expects what the user wrote explicitly.
2413 llvm::APSInt Val;
2414 IdxExpr->EvaluateAsInt(Val, S.Context);
2415
Michael Han99315932013-01-24 16:46:58 +00002416 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002417 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002418 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002419}
2420
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002421enum FormatAttrKind {
2422 CFStringFormat,
2423 NSStringFormat,
2424 StrftimeFormat,
2425 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002426 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002427 InvalidFormat
2428};
2429
2430/// getFormatAttrKind - Map from format attribute names to supported format
2431/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002432static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002433 return llvm::StringSwitch<FormatAttrKind>(Format)
2434 // Check for formats that get handled specially.
2435 .Case("NSString", NSStringFormat)
2436 .Case("CFString", CFStringFormat)
2437 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002438
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002439 // Otherwise, check for supported formats.
2440 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2441 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2442 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002443
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002444 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2445 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002446}
2447
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002448/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002449/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002450static void handleInitPriorityAttr(Sema &S, Decl *D,
2451 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002452 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002453 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2454 return;
2455 }
2456
Aaron Ballman4a611152013-11-27 16:34:09 +00002457 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002458 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2459 Attr.setInvalid();
2460 return;
2461 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002462 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002463 if (S.Context.getAsArrayType(T))
2464 T = S.Context.getBaseElementType(T);
2465 if (!T->getAs<RecordType>()) {
2466 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2467 Attr.setInvalid();
2468 return;
2469 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002470
2471 Expr *E = Attr.getArgAsExpr(0);
2472 uint32_t prioritynum;
2473 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002474 Attr.setInvalid();
2475 return;
2476 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002477
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002478 if (prioritynum < 101 || prioritynum > 65535) {
2479 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002480 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002481 Attr.setInvalid();
2482 return;
2483 }
Michael Han99315932013-01-24 16:46:58 +00002484 D->addAttr(::new (S.Context)
2485 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2486 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002487}
2488
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002489FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2490 IdentifierInfo *Format, int FormatIdx,
2491 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002492 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002493 // Check whether we already have an equivalent format attribute.
2494 for (specific_attr_iterator<FormatAttr>
2495 i = D->specific_attr_begin<FormatAttr>(),
2496 e = D->specific_attr_end<FormatAttr>();
2497 i != e ; ++i) {
2498 FormatAttr *f = *i;
2499 if (f->getType() == Format &&
2500 f->getFormatIdx() == FormatIdx &&
2501 f->getFirstArg() == FirstArg) {
2502 // If we don't have a valid location for this attribute, adopt the
2503 // location.
2504 if (f->getLocation().isInvalid())
2505 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002506 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002507 }
2508 }
2509
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002510 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2511 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002512}
2513
Mike Stumpd3bb5572009-07-24 19:02:52 +00002514/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002515/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002516static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002517 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002518 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002519 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002520 return;
2521 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002522
Chandler Carruth743682b2010-11-16 08:35:43 +00002523 // In C++ the implicit 'this' function parameter also counts, and they are
2524 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002525 bool HasImplicitThisParam = isInstanceMethod(D);
2526 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002527
Aaron Ballman00e99962013-08-31 01:11:41 +00002528 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2529 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002530
2531 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002532 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002533 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002534 // If we've modified the string name, we need a new identifier for it.
2535 II = &S.Context.Idents.get(Format);
2536 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002537
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002538 // Check for supported formats.
2539 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002540
2541 if (Kind == IgnoredFormat)
2542 return;
2543
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002544 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002545 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002546 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002547 return;
2548 }
2549
2550 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002551 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002552 uint32_t Idx;
2553 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002554 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002555
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002556 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002557 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002558 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002559 return;
2560 }
2561
2562 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002563 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002564
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002565 if (HasImplicitThisParam) {
2566 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002567 S.Diag(Attr.getLoc(),
2568 diag::err_format_attribute_implicit_this_format_string)
2569 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002570 return;
2571 }
2572 ArgIdx--;
2573 }
Mike Stump11289f42009-09-09 15:08:12 +00002574
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002575 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002576 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002577
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002578 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002579 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002580 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2581 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002582 return;
2583 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002584 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002585 // FIXME: do we need to check if the type is NSString*? What are the
2586 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002587 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002588 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002589 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2590 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002591 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002592 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002593 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002594 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002595 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002596 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2597 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002598 return;
2599 }
2600
2601 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002602 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002603 uint32_t FirstArg;
2604 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002605 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002606
2607 // check if the function is variadic if the 3rd argument non-zero
2608 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002609 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002610 ++NumArgs; // +1 for ...
2611 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002612 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002613 return;
2614 }
2615 }
2616
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002617 // strftime requires FirstArg to be 0 because it doesn't read from any
2618 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002619 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002620 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002621 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2622 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002623 return;
2624 }
2625 // if 0 it disables parameter checking (to use with e.g. va_list)
2626 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002627 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002628 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002629 return;
2630 }
2631
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002632 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002633 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002634 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002635 if (NewAttr)
2636 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002637}
2638
Chandler Carruthedc2c642011-07-02 00:01:44 +00002639static void handleTransparentUnionAttr(Sema &S, Decl *D,
2640 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002641 // Try to find the underlying union declaration.
2642 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002643 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002644 if (TD && TD->getUnderlyingType()->isUnionType())
2645 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2646 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002647 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002648
2649 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002650 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002651 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002652 return;
2653 }
2654
John McCallf937c022011-10-07 06:10:15 +00002655 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002656 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002657 diag::warn_transparent_union_attribute_not_definition);
2658 return;
2659 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002660
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002661 RecordDecl::field_iterator Field = RD->field_begin(),
2662 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002663 if (Field == FieldEnd) {
2664 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2665 return;
2666 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002667
David Blaikie40ed2972012-06-06 20:45:41 +00002668 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002669 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002670 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002671 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002672 diag::warn_transparent_union_attribute_floating)
2673 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002674 return;
2675 }
2676
2677 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2678 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2679 for (; Field != FieldEnd; ++Field) {
2680 QualType FieldType = Field->getType();
2681 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2682 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2683 // Warn if we drop the attribute.
2684 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002685 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002686 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002687 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002688 diag::warn_transparent_union_attribute_field_size_align)
2689 << isSize << Field->getDeclName() << FieldBits;
2690 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002691 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002692 diag::note_transparent_union_first_field_size_align)
2693 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002694 return;
2695 }
2696 }
2697
Michael Han99315932013-01-24 16:46:58 +00002698 RD->addAttr(::new (S.Context)
2699 TransparentUnionAttr(Attr.getRange(), S.Context,
2700 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002701}
2702
Chandler Carruthedc2c642011-07-02 00:01:44 +00002703static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002704 // Make sure that there is a string literal as the annotation's single
2705 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002706 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002707 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002708 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002709
2710 // Don't duplicate annotations that are already set.
2711 for (specific_attr_iterator<AnnotateAttr>
2712 i = D->specific_attr_begin<AnnotateAttr>(),
2713 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002714 if ((*i)->getAnnotation() == Str)
2715 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002716 }
Michael Han99315932013-01-24 16:46:58 +00002717
2718 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002719 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002720 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002721}
2722
Chandler Carruthedc2c642011-07-02 00:01:44 +00002723static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002724 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002725 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002726 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2727 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002728 return;
2729 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002730
Richard Smith848e1f12013-02-01 08:12:08 +00002731 if (Attr.getNumArgs() == 0) {
2732 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2733 true, 0, Attr.getAttributeSpellingListIndex()));
2734 return;
2735 }
2736
Aaron Ballman00e99962013-08-31 01:11:41 +00002737 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002738 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2739 S.Diag(Attr.getEllipsisLoc(),
2740 diag::err_pack_expansion_without_parameter_packs);
2741 return;
2742 }
2743
2744 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2745 return;
2746
2747 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2748 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002749}
2750
2751void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002752 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002753 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2754 SourceLocation AttrLoc = AttrRange.getBegin();
2755
Richard Smith1dba27c2013-01-29 09:02:09 +00002756 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002757 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002758 // C++11 [dcl.align]p1:
2759 // An alignment-specifier may be applied to a variable or to a class
2760 // data member, but it shall not be applied to a bit-field, a function
2761 // parameter, the formal parameter of a catch clause, or a variable
2762 // declared with the register storage class specifier. An
2763 // alignment-specifier may also be applied to the declaration of a class
2764 // or enumeration type.
2765 // C11 6.7.5/2:
2766 // An alignment attribute shall not be specified in a declaration of
2767 // a typedef, or a bit-field, or a function, or a parameter, or an
2768 // object declared with the register storage-class specifier.
2769 int DiagKind = -1;
2770 if (isa<ParmVarDecl>(D)) {
2771 DiagKind = 0;
2772 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2773 if (VD->getStorageClass() == SC_Register)
2774 DiagKind = 1;
2775 if (VD->isExceptionVariable())
2776 DiagKind = 2;
2777 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2778 if (FD->isBitField())
2779 DiagKind = 3;
2780 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00002781 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00002782 << (TmpAttr.isC11() ? ExpectedVariableOrField
2783 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002784 return;
2785 }
2786 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002787 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00002788 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002789 return;
2790 }
2791 }
2792
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002793 if (E->isTypeDependent() || E->isValueDependent()) {
2794 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002795 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2796 AA->setPackExpansion(IsPackExpansion);
2797 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002798 return;
2799 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002800
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002801 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002802 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002803 ExprResult ICE
2804 = VerifyIntegerConstantExpression(E, &Alignment,
2805 diag::err_aligned_attribute_argument_not_int,
2806 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002807 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002808 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002809
2810 // C++11 [dcl.align]p2:
2811 // -- if the constant expression evaluates to zero, the alignment
2812 // specifier shall have no effect
2813 // C11 6.7.5p6:
2814 // An alignment specification of zero has no effect.
2815 if (!(TmpAttr.isAlignas() && !Alignment) &&
2816 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002817 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2818 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002819 return;
2820 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002821
Richard Smith848e1f12013-02-01 08:12:08 +00002822 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00002823 // We've already verified it's a power of 2, now let's make sure it's
2824 // 8192 or less.
2825 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00002826 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00002827 << E->getSourceRange();
2828 return;
2829 }
2830 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002831
Richard Smith44c247f2013-02-22 08:32:16 +00002832 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2833 ICE.take(), SpellingListIndex);
2834 AA->setPackExpansion(IsPackExpansion);
2835 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002836}
2837
Michael Hanaf02bbe2013-02-01 01:19:17 +00002838void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002839 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002840 // FIXME: Cache the number on the Attr object if non-dependent?
2841 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002842 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2843 SpellingListIndex);
2844 AA->setPackExpansion(IsPackExpansion);
2845 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002846}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002847
Richard Smith848e1f12013-02-01 08:12:08 +00002848void Sema::CheckAlignasUnderalignment(Decl *D) {
2849 assert(D->hasAttrs() && "no attributes on decl");
2850
2851 QualType Ty;
2852 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2853 Ty = VD->getType();
2854 else
2855 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002856 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002857 return;
2858
2859 // C++11 [dcl.align]p5, C11 6.7.5/4:
2860 // The combined effect of all alignment attributes in a declaration shall
2861 // not specify an alignment that is less strict than the alignment that
2862 // would otherwise be required for the entity being declared.
2863 AlignedAttr *AlignasAttr = 0;
2864 unsigned Align = 0;
2865 for (specific_attr_iterator<AlignedAttr>
2866 I = D->specific_attr_begin<AlignedAttr>(),
2867 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
2868 if (I->isAlignmentDependent())
2869 return;
2870 if (I->isAlignas())
2871 AlignasAttr = *I;
2872 Align = std::max(Align, I->getAlignment(Context));
2873 }
2874
2875 if (AlignasAttr && Align) {
2876 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2877 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2878 if (NaturalAlign > RequestedAlign)
2879 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2880 << Ty << (unsigned)NaturalAlign.getQuantity();
2881 }
2882}
2883
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002884/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002885/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002886///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002887/// Despite what would be logical, the mode attribute is a decl attribute, not a
2888/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2889/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002890static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002891 // This attribute isn't documented, but glibc uses it. It changes
2892 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002893 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002894 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2895 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002896 return;
2897 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002898
Aaron Ballman00e99962013-08-31 01:11:41 +00002899 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2900 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002901
2902 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002903 if (Str.startswith("__") && Str.endswith("__"))
2904 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002905
2906 unsigned DestWidth = 0;
2907 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002908 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002909 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002910 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002911 switch (Str[0]) {
2912 case 'Q': DestWidth = 8; break;
2913 case 'H': DestWidth = 16; break;
2914 case 'S': DestWidth = 32; break;
2915 case 'D': DestWidth = 64; break;
2916 case 'X': DestWidth = 96; break;
2917 case 'T': DestWidth = 128; break;
2918 }
2919 if (Str[1] == 'F') {
2920 IntegerMode = false;
2921 } else if (Str[1] == 'C') {
2922 IntegerMode = false;
2923 ComplexMode = true;
2924 } else if (Str[1] != 'I') {
2925 DestWidth = 0;
2926 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002927 break;
2928 case 4:
2929 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2930 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002931 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002932 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002933 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002934 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002935 break;
2936 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002937 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002938 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002939 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002940 case 11:
2941 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00002942 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002943 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002944 }
2945
2946 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002947 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002948 OldTy = TD->getUnderlyingType();
2949 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2950 OldTy = VD->getType();
2951 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002952 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00002953 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002954 return;
2955 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002956
John McCall9dd450b2009-09-21 23:43:11 +00002957 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002958 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2959 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002960 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002961 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2962 } else if (ComplexMode) {
2963 if (!OldTy->isComplexType())
2964 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2965 } else {
2966 if (!OldTy->isFloatingType())
2967 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2968 }
2969
Mike Stump87c57ac2009-05-16 07:39:55 +00002970 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2971 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002972 // FIXME: Make sure floating-point mappings are accurate
2973 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002974 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00002975 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002976 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002977 }
2978
2979 QualType NewTy;
2980
2981 if (IntegerMode)
2982 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
2983 OldTy->isSignedIntegerType());
2984 else
2985 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
2986
2987 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00002988 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002989 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002990 }
2991
Eli Friedman4735374e2009-03-03 06:41:03 +00002992 if (ComplexMode) {
2993 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002994 }
2995
2996 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002997 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
2998 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
2999 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003000 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003001
3002 D->addAttr(::new (S.Context)
3003 ModeAttr(Attr.getRange(), S.Context, Name,
3004 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003005}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003006
Chandler Carruthedc2c642011-07-02 00:01:44 +00003007static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003008 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3009 if (!VD->hasGlobalStorage())
3010 S.Diag(Attr.getLoc(),
3011 diag::warn_attribute_requires_functions_or_static_globals)
3012 << Attr.getName();
3013 } else if (!isFunctionOrMethod(D)) {
3014 S.Diag(Attr.getLoc(),
3015 diag::warn_attribute_requires_functions_or_static_globals)
3016 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003017 return;
3018 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003019
Michael Han99315932013-01-24 16:46:58 +00003020 D->addAttr(::new (S.Context)
3021 NoDebugAttr(Attr.getRange(), S.Context,
3022 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003023}
3024
Chandler Carruthedc2c642011-07-02 00:01:44 +00003025static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003026 FunctionDecl *FD = cast<FunctionDecl>(D);
3027 if (!FD->getResultType()->isVoidType()) {
3028 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
3029 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
3030 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3031 << FD->getType()
3032 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
3033 "void");
3034 } else {
3035 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3036 << FD->getType();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003037 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003038 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003039 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003040
Aaron Ballman3aff6332013-12-02 19:30:36 +00003041 D->addAttr(::new (S.Context)
3042 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003043 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003044}
3045
Chandler Carruthedc2c642011-07-02 00:01:44 +00003046static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003047 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003048 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003049 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003050 return;
3051 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003052
Michael Han99315932013-01-24 16:46:58 +00003053 D->addAttr(::new (S.Context)
3054 GNUInlineAttr(Attr.getRange(), S.Context,
3055 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003056}
3057
Chandler Carruthedc2c642011-07-02 00:01:44 +00003058static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003059 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003060
Aaron Ballman02df2e02012-12-09 17:45:41 +00003061 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003062 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003063 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3064 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003065 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003066 return;
3067
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003068 if (!isa<ObjCMethodDecl>(D)) {
3069 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3070 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003071 return;
3072 }
3073
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003074 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003075 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003076 D->addAttr(::new (S.Context)
3077 FastCallAttr(Attr.getRange(), S.Context,
3078 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003079 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003080 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003081 D->addAttr(::new (S.Context)
3082 StdCallAttr(Attr.getRange(), S.Context,
3083 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003084 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003085 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003086 D->addAttr(::new (S.Context)
3087 ThisCallAttr(Attr.getRange(), S.Context,
3088 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003089 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003090 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003091 D->addAttr(::new (S.Context)
3092 CDeclAttr(Attr.getRange(), S.Context,
3093 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003094 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003095 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003096 D->addAttr(::new (S.Context)
3097 PascalAttr(Attr.getRange(), S.Context,
3098 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003099 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003100 case AttributeList::AT_MSABI:
3101 D->addAttr(::new (S.Context)
3102 MSABIAttr(Attr.getRange(), S.Context,
3103 Attr.getAttributeSpellingListIndex()));
3104 return;
3105 case AttributeList::AT_SysVABI:
3106 D->addAttr(::new (S.Context)
3107 SysVABIAttr(Attr.getRange(), S.Context,
3108 Attr.getAttributeSpellingListIndex()));
3109 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003110 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003111 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003112 switch (CC) {
3113 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003114 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003115 break;
3116 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003117 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003118 break;
3119 default:
3120 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003121 }
3122
Michael Han99315932013-01-24 16:46:58 +00003123 D->addAttr(::new (S.Context)
3124 PcsAttr(Attr.getRange(), S.Context, PCS,
3125 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003126 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003127 }
Derek Schuffa2020962012-10-16 22:30:41 +00003128 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003129 D->addAttr(::new (S.Context)
3130 PnaclCallAttr(Attr.getRange(), S.Context,
3131 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003132 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003133 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003134 D->addAttr(::new (S.Context)
3135 IntelOclBiccAttr(Attr.getRange(), S.Context,
3136 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003137 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003138
Abramo Bagnara50099372010-04-30 13:10:51 +00003139 default:
3140 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003141 }
3142}
3143
Aaron Ballman02df2e02012-12-09 17:45:41 +00003144bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3145 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003146 if (attr.isInvalid())
3147 return true;
3148
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003149 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003150 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003151 attr.setInvalid();
3152 return true;
3153 }
3154
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003155 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003156 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003157 case AttributeList::AT_CDecl: CC = CC_C; break;
3158 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3159 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3160 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3161 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003162 case AttributeList::AT_MSABI:
3163 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3164 CC_X86_64Win64;
3165 break;
3166 case AttributeList::AT_SysVABI:
3167 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3168 CC_C;
3169 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003170 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003171 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003172 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003173 attr.setInvalid();
3174 return true;
3175 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003176 if (StrRef == "aapcs") {
3177 CC = CC_AAPCS;
3178 break;
3179 } else if (StrRef == "aapcs-vfp") {
3180 CC = CC_AAPCS_VFP;
3181 break;
3182 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003183
3184 attr.setInvalid();
3185 Diag(attr.getLoc(), diag::err_invalid_pcs);
3186 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003187 }
Derek Schuffa2020962012-10-16 22:30:41 +00003188 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003189 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003190 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003191 }
3192
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003193 const TargetInfo &TI = Context.getTargetInfo();
3194 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3195 if (A == TargetInfo::CCCR_Warning) {
3196 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003197
3198 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3199 if (FD)
3200 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3201 TargetInfo::CCMT_NonMember;
3202 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003203 }
3204
John McCall3882ace2011-01-05 12:14:39 +00003205 return false;
3206}
3207
John McCall3882ace2011-01-05 12:14:39 +00003208/// Checks a regparm attribute, returning true if it is ill-formed and
3209/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003210bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3211 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003212 return true;
3213
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003214 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003215 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003216 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003217 }
Eli Friedman7044b762009-03-27 21:06:47 +00003218
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003219 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003220 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003221 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003222 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003223 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003224 }
3225
Douglas Gregore8bbc122011-09-02 00:18:52 +00003226 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003227 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003228 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003229 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003230 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003231 }
3232
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003233 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003234 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003235 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003236 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003237 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003238 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003239 }
3240
John McCall3882ace2011-01-05 12:14:39 +00003241 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003242}
3243
Aaron Ballman66039932013-12-19 00:41:31 +00003244static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3245 const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003246 // check the attribute arguments.
3247 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3248 // FIXME: 0 is not okay.
Aaron Ballman05e420a2014-01-02 21:26:14 +00003249 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3250 << Attr.getName() << 2;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003251 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003252 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003253
Aaron Ballman66039932013-12-19 00:41:31 +00003254 uint32_t MaxThreads, MinBlocks = 0;
3255 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3256 return;
3257 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3258 Attr.getArgAsExpr(1),
3259 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003260 return;
3261
3262 D->addAttr(::new (S.Context)
3263 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3264 MaxThreads, MinBlocks,
3265 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003266}
3267
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003268static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3269 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003270 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003271 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003272 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003273 return;
3274 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003275
3276 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003277 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003278
Aaron Ballman00e99962013-08-31 01:11:41 +00003279 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003280
3281 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3282 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3283 << Attr.getName() << ExpectedFunctionOrMethod;
3284 return;
3285 }
3286
3287 uint64_t ArgumentIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003288 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3289 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003290 return;
3291
3292 uint64_t TypeTagIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003293 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3294 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003295 return;
3296
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003297 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003298 if (IsPointer) {
3299 // Ensure that buffer has a pointer type.
3300 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3301 if (!BufferTy->isPointerType()) {
3302 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003303 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003304 }
3305 }
3306
Michael Han99315932013-01-24 16:46:58 +00003307 D->addAttr(::new (S.Context)
3308 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3309 ArgumentIdx, TypeTagIdx, IsPointer,
3310 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003311}
3312
3313static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3314 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003315 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003316 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003317 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003318 return;
3319 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003320
3321 if (!checkAttributeNumArgs(S, Attr, 1))
3322 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003323
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003324 if (!isa<VarDecl>(D)) {
3325 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3326 << Attr.getName() << ExpectedVariable;
3327 return;
3328 }
3329
Aaron Ballman00e99962013-08-31 01:11:41 +00003330 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003331 TypeSourceInfo *MatchingCTypeLoc = 0;
3332 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3333 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003334
Michael Han99315932013-01-24 16:46:58 +00003335 D->addAttr(::new (S.Context)
3336 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003337 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003338 Attr.getLayoutCompatible(),
3339 Attr.getMustBeNull(),
3340 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003341}
3342
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003343//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003344// Checker-specific attribute handlers.
3345//===----------------------------------------------------------------------===//
3346
John McCalled433932011-01-25 03:31:58 +00003347static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003348 return type->isDependentType() ||
3349 type->isObjCObjectPointerType() ||
3350 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003351}
3352static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003353 return type->isDependentType() ||
3354 type->isPointerType() ||
3355 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003356}
3357
Chandler Carruthedc2c642011-07-02 00:01:44 +00003358static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003359 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003360 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003361
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003362 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003363 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3364 cf = false;
3365 } else {
3366 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3367 cf = true;
3368 }
3369
3370 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003371 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003372 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003373 return;
3374 }
3375
3376 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003377 param->addAttr(::new (S.Context)
3378 CFConsumedAttr(Attr.getRange(), S.Context,
3379 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003380 else
Michael Han99315932013-01-24 16:46:58 +00003381 param->addAttr(::new (S.Context)
3382 NSConsumedAttr(Attr.getRange(), S.Context,
3383 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003384}
3385
Chandler Carruthedc2c642011-07-02 00:01:44 +00003386static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3387 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003388
John McCalled433932011-01-25 03:31:58 +00003389 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003390
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003391 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003392 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003393 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003394 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003395 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003396 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3397 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003398 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003399 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003400 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003401 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003402 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003403 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003404 return;
3405 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003406
John McCalled433932011-01-25 03:31:58 +00003407 bool typeOK;
3408 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003409 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003410 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003411 case AttributeList::AT_NSReturnsAutoreleased:
3412 case AttributeList::AT_NSReturnsRetained:
3413 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003414 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3415 cf = false;
3416 break;
3417
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003418 case AttributeList::AT_CFReturnsRetained:
3419 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003420 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3421 cf = true;
3422 break;
3423 }
3424
3425 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003426 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003427 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003428 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003429 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003430
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003431 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003432 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003433 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003434 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003435 D->addAttr(::new (S.Context)
3436 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3437 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003438 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003439 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003440 D->addAttr(::new (S.Context)
3441 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3442 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003443 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003444 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003445 D->addAttr(::new (S.Context)
3446 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3447 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003448 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003449 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003450 D->addAttr(::new (S.Context)
3451 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3452 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003453 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003454 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003455 D->addAttr(::new (S.Context)
3456 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3457 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003458 return;
3459 };
3460}
3461
John McCallcf166702011-07-22 08:53:00 +00003462static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3463 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003464 const int EP_ObjCMethod = 1;
3465 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003466
John McCallcf166702011-07-22 08:53:00 +00003467 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003468 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003469 if (isa<ObjCMethodDecl>(D))
3470 resultType = cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003471 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003472 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003473
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003474 if (!resultType->isReferenceType() &&
3475 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003476 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003477 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003478 << attr.getName()
3479 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003480 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003481
3482 // Drop the attribute.
3483 return;
3484 }
3485
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003486 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003487 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3488 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003489}
3490
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003491static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3492 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003493 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003494
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003495 DeclContext *DC = method->getDeclContext();
3496 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3497 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3498 << attr.getName() << 0;
3499 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3500 return;
3501 }
3502 if (method->getMethodFamily() == OMF_dealloc) {
3503 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3504 << attr.getName() << 1;
3505 return;
3506 }
3507
Michael Han99315932013-01-24 16:46:58 +00003508 method->addAttr(::new (S.Context)
3509 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3510 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003511}
3512
Aaron Ballmanfb763042013-12-02 18:05:46 +00003513static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3514 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003515 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00003516 return;
John McCall32f5fe12011-09-30 05:12:12 +00003517
Aaron Ballmanfb763042013-12-02 18:05:46 +00003518 D->addAttr(::new (S.Context)
3519 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3520 Attr.getAttributeSpellingListIndex()));
3521}
3522
3523static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3524 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003525 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00003526 return;
3527
3528 D->addAttr(::new (S.Context)
3529 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3530 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003531}
3532
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003533static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3534 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003535 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003536
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003537 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003538 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003539 return;
3540 }
3541
3542 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003543 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003544 Attr.getAttributeSpellingListIndex()));
3545}
3546
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003547static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3548 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003549 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003550
3551 if (!Parm) {
3552 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3553 return;
3554 }
3555
3556 D->addAttr(::new (S.Context)
3557 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3558 Attr.getAttributeSpellingListIndex()));
3559}
3560
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003561static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3562 const AttributeList &Attr) {
3563 IdentifierInfo *RelatedClass =
3564 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0;
3565 if (!RelatedClass) {
3566 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3567 return;
3568 }
3569 IdentifierInfo *ClassMethod =
3570 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0;
3571 IdentifierInfo *InstanceMethod =
3572 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0;
3573 D->addAttr(::new (S.Context)
3574 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3575 ClassMethod, InstanceMethod,
3576 Attr.getAttributeSpellingListIndex()));
3577}
3578
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003579static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3580 const AttributeList &Attr) {
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003581 ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003582 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003583 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003584 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3585 Attr.getAttributeSpellingListIndex()));
3586}
3587
Chandler Carruthedc2c642011-07-02 00:01:44 +00003588static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3589 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003590 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003591
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003592 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003593 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003594}
3595
Chandler Carruthedc2c642011-07-02 00:01:44 +00003596static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3597 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003598 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003599 QualType type = vd->getType();
3600
3601 if (!type->isDependentType() &&
3602 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003603 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003604 << type;
3605 return;
3606 }
3607
3608 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3609
3610 // If we have no lifetime yet, check the lifetime we're presumably
3611 // going to infer.
3612 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3613 lifetime = type->getObjCARCImplicitLifetime();
3614
3615 switch (lifetime) {
3616 case Qualifiers::OCL_None:
3617 assert(type->isDependentType() &&
3618 "didn't infer lifetime for non-dependent type?");
3619 break;
3620
3621 case Qualifiers::OCL_Weak: // meaningful
3622 case Qualifiers::OCL_Strong: // meaningful
3623 break;
3624
3625 case Qualifiers::OCL_ExplicitNone:
3626 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003627 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003628 << (lifetime == Qualifiers::OCL_Autoreleasing);
3629 break;
3630 }
3631
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003632 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003633 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3634 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003635}
3636
Francois Picheta83957a2010-12-19 06:50:37 +00003637//===----------------------------------------------------------------------===//
3638// Microsoft specific attribute handlers.
3639//===----------------------------------------------------------------------===//
3640
Chandler Carruthedc2c642011-07-02 00:01:44 +00003641static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003642 if (!S.LangOpts.CPlusPlus) {
3643 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3644 << Attr.getName() << AttributeLangSupport::C;
3645 return;
3646 }
3647
Aaron Ballman60e705e2013-11-24 20:58:02 +00003648 if (!isa<CXXRecordDecl>(D)) {
3649 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3650 << Attr.getName() << ExpectedClass;
3651 return;
3652 }
3653
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003654 StringRef StrRef;
3655 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003656 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003657 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003658
David Majnemer89085342013-08-09 08:56:20 +00003659 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3660 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003661 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3662 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003663
Reid Kleckner140c4a72013-05-17 14:04:52 +00003664 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003665 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003666 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003667 return;
3668 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003669
David Majnemer89085342013-08-09 08:56:20 +00003670 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003671 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003672 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003673 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003674 return;
3675 }
David Majnemer89085342013-08-09 08:56:20 +00003676 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003677 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003678 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003679 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003680 }
Francois Picheta83957a2010-12-19 06:50:37 +00003681
David Majnemer89085342013-08-09 08:56:20 +00003682 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3683 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003684}
3685
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003686static void handleARMInterruptAttr(Sema &S, Decl *D,
3687 const AttributeList &Attr) {
3688 // Check the attribute arguments.
3689 if (Attr.getNumArgs() > 1) {
3690 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3691 << Attr.getName() << 1;
3692 return;
3693 }
3694
3695 StringRef Str;
3696 SourceLocation ArgLoc;
3697
3698 if (Attr.getNumArgs() == 0)
3699 Str = "";
3700 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
3701 return;
3702
3703 ARMInterruptAttr::InterruptType Kind;
3704 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
3705 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
3706 << Attr.getName() << Str << ArgLoc;
3707 return;
3708 }
3709
3710 unsigned Index = Attr.getAttributeSpellingListIndex();
3711 D->addAttr(::new (S.Context)
3712 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
3713}
3714
3715static void handleMSP430InterruptAttr(Sema &S, Decl *D,
3716 const AttributeList &Attr) {
3717 if (!checkAttributeNumArgs(S, Attr, 1))
3718 return;
3719
3720 if (!Attr.isArgExpr(0)) {
3721 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3722 << AANT_ArgumentIntegerConstant;
3723 return;
3724 }
3725
3726 // FIXME: Check for decl - it should be void ()(void).
3727
3728 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3729 llvm::APSInt NumParams(32);
3730 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
3731 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3732 << Attr.getName() << AANT_ArgumentIntegerConstant
3733 << NumParamsExpr->getSourceRange();
3734 return;
3735 }
3736
3737 unsigned Num = NumParams.getLimitedValue(255);
3738 if ((Num & 1) || Num > 30) {
3739 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3740 << Attr.getName() << (int)NumParams.getSExtValue()
3741 << NumParamsExpr->getSourceRange();
3742 return;
3743 }
3744
Aaron Ballman36a53502014-01-16 13:03:14 +00003745 D->addAttr(::new (S.Context)
3746 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
3747 Attr.getAttributeSpellingListIndex()));
3748 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003749}
3750
3751static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3752 // Dispatch the interrupt attribute based on the current target.
3753 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
3754 handleMSP430InterruptAttr(S, D, Attr);
3755 else
3756 handleARMInterruptAttr(S, D, Attr);
3757}
3758
3759static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
3760 const AttributeList& Attr) {
3761 // If we try to apply it to a function pointer, don't warn, but don't
3762 // do anything, either. It doesn't matter anyway, because there's nothing
3763 // special about calling a force_align_arg_pointer function.
3764 ValueDecl *VD = dyn_cast<ValueDecl>(D);
3765 if (VD && VD->getType()->isFunctionPointerType())
3766 return;
3767 // Also don't warn on function pointer typedefs.
3768 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
3769 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
3770 TD->getUnderlyingType()->isFunctionType()))
3771 return;
3772 // Attribute can only be applied to function types.
3773 if (!isa<FunctionDecl>(D)) {
3774 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3775 << Attr.getName() << /* function */0;
3776 return;
3777 }
3778
Aaron Ballman36a53502014-01-16 13:03:14 +00003779 D->addAttr(::new (S.Context)
3780 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
3781 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003782}
3783
3784DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
3785 unsigned AttrSpellingListIndex) {
3786 if (D->hasAttr<DLLExportAttr>()) {
3787 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "dllimport";
3788 return NULL;
3789 }
3790
3791 if (D->hasAttr<DLLImportAttr>())
3792 return NULL;
3793
3794 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3795 if (VD->hasDefinition()) {
3796 // dllimport cannot be applied to definitions.
3797 Diag(D->getLocation(), diag::warn_attribute_invalid_on_definition)
3798 << "dllimport";
3799 return NULL;
3800 }
3801 }
3802
3803 return ::new (Context)DLLImportAttr(Range, Context,
3804 AttrSpellingListIndex);
3805}
3806
3807static void handleDLLImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3808 // Attribute can be applied only to functions or variables.
3809 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3810 if (!FD && !isa<VarDecl>(D)) {
3811 // Apparently Visual C++ thinks it is okay to not emit a warning
3812 // in this case, so only emit a warning when -fms-extensions is not
3813 // specified.
3814 if (!S.getLangOpts().MicrosoftExt)
3815 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3816 << Attr.getName() << 2 /*variable and function*/;
3817 return;
3818 }
3819
3820 // Currently, the dllimport attribute is ignored for inlined functions.
3821 // Warning is emitted.
3822 if (FD && FD->isInlineSpecified()) {
3823 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3824 return;
3825 }
3826
3827 unsigned Index = Attr.getAttributeSpellingListIndex();
3828 DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index);
3829 if (NewAttr)
3830 D->addAttr(NewAttr);
3831}
3832
3833DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
3834 unsigned AttrSpellingListIndex) {
3835 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
3836 Diag(Import->getLocation(), diag::warn_attribute_ignored) << "dllimport";
3837 D->dropAttr<DLLImportAttr>();
3838 }
3839
3840 if (D->hasAttr<DLLExportAttr>())
3841 return NULL;
3842
3843 return ::new (Context)DLLExportAttr(Range, Context,
3844 AttrSpellingListIndex);
3845}
3846
3847static void handleDLLExportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3848 // Currently, the dllexport attribute is ignored for inlined functions, unless
3849 // the -fkeep-inline-functions flag has been used. Warning is emitted;
3850 if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isInlineSpecified()) {
3851 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
3852 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3853 return;
3854 }
3855
3856 unsigned Index = Attr.getAttributeSpellingListIndex();
3857 DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index);
3858 if (NewAttr)
3859 D->addAttr(NewAttr);
3860}
3861
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003862/// Handles semantic checking for features that are common to all attributes,
3863/// such as checking whether a parameter was properly specified, or the correct
3864/// number of arguments were passed, etc.
3865static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3866 const AttributeList &Attr) {
3867 // Several attributes carry different semantics than the parsing requires, so
3868 // those are opted out of the common handling.
3869 //
3870 // We also bail on unknown and ignored attributes because those are handled
3871 // as part of the target-specific handling logic.
3872 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003873 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003874 return false;
3875
Aaron Ballman3aff6332013-12-02 19:30:36 +00003876 // Check whether the attribute requires specific language extensions to be
3877 // enabled.
3878 if (!Attr.diagnoseLangOpts(S))
3879 return true;
3880
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003881 // If there are no optional arguments, then checking for the argument count
3882 // is trivial.
3883 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
3884 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
3885 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003886
3887 // Check whether the attribute appertains to the given subject.
3888 if (!Attr.diagnoseAppertainsTo(S, D))
3889 return true;
3890
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003891 return false;
3892}
3893
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003894//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003895// Top Level Sema Entry Points
3896//===----------------------------------------------------------------------===//
3897
Richard Smithf8a75c32013-08-29 00:47:48 +00003898/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3899/// the attribute applies to decls. If the attribute is a type attribute, just
3900/// silently ignore it if a GNU attribute.
3901static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3902 const AttributeList &Attr,
3903 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003904 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00003905 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003906
Richard Smithf8a75c32013-08-29 00:47:48 +00003907 // Ignore C++11 attributes on declarator chunks: they appertain to the type
3908 // instead.
3909 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
3910 return;
3911
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003912 // Unknown attributes are automatically warned on. Target-specific attributes
3913 // which do not apply to the current target architecture are treated as
3914 // though they were unknown attributes.
3915 if (Attr.getKind() == AttributeList::UnknownAttribute ||
3916 !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) {
3917 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
3918 diag::warn_unhandled_ms_attribute_ignored :
3919 diag::warn_unknown_attribute_ignored) << Attr.getName();
3920 return;
3921 }
3922
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003923 if (handleCommonAttributeFeatures(S, scope, D, Attr))
3924 return;
3925
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003926 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003927 default:
3928 // Type attributes are handled elsewhere; silently move on.
3929 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
3930 break;
3931 case AttributeList::AT_Interrupt:
3932 handleInterruptAttr(S, D, Attr); break;
3933 case AttributeList::AT_X86ForceAlignArgPointer:
3934 handleX86ForceAlignArgPointerAttr(S, D, Attr); break;
3935 case AttributeList::AT_DLLExport:
3936 handleDLLExportAttr(S, D, Attr); break;
3937 case AttributeList::AT_DLLImport:
3938 handleDLLImportAttr(S, D, Attr); break;
3939 case AttributeList::AT_Mips16:
3940 handleSimpleAttribute<Mips16Attr>(S, D, Attr); break;
3941 case AttributeList::AT_NoMips16:
3942 handleSimpleAttribute<NoMips16Attr>(S, D, Attr); break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00003943 case AttributeList::AT_IBAction:
3944 handleSimpleAttribute<IBActionAttr>(S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003945 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
3946 case AttributeList::AT_IBOutletCollection:
3947 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003948 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
3949 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003950 case AttributeList::AT_AlwaysInline:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003951 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003952 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003953 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00003954 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003955 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
3956 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
3957 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00003958 handleDependencyAttr(S, scope, D, Attr);
3959 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003960 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003961 case AttributeList::AT_CUDAConstant:
3962 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003963 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003964 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman3a8e2d92013-11-27 18:53:58 +00003965 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003966 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00003967 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00003968 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003969 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
Nick Lewycky35a6ef42014-01-11 02:50:57 +00003970 case AttributeList::AT_EnableIf: handleEnableIfAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003971 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003972 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003973 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00003974 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003975 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00003976 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003977 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
3978 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
3979 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00003980 case AttributeList::AT_CUDADevice:
3981 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003982 case AttributeList::AT_CUDAHost:
3983 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003984 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
3985 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003986 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00003987 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003988 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003989 case AttributeList::AT_MayAlias:
3990 handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00003991 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003992 case AttributeList::AT_NoCommon:
3993 handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00003994 case AttributeList::AT_NonNull:
3995 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
3996 handleNonNullAttrParameter(S, PVD, Attr);
3997 else
3998 handleNonNullAttr(S, D, Attr);
3999 break;
Ted Kremenekdbf62e32014-01-20 05:50:47 +00004000 case AttributeList::AT_ReturnsNonNull:
4001 handleReturnsNonNullAttr(S, D, Attr);
4002 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004003 case AttributeList::AT_Overloadable:
4004 handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00004005 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004006 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4007 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004008 case AttributeList::AT_Naked:
4009 handleSimpleAttribute<NakedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004010 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004011 case AttributeList::AT_NoThrow:
4012 handleSimpleAttribute<NoThrowAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004013 case AttributeList::AT_CUDAShared:
4014 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004015 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004016
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004017 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004018 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004019 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004020 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004021
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004022 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004023 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4024
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004025 case AttributeList::AT_ObjCRequiresSuper:
4026 handleObjCRequiresSuperAttr(S, D, Attr); break;
4027
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004028 case AttributeList::AT_ObjCBridge:
4029 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004030
4031 case AttributeList::AT_ObjCBridgeMutable:
4032 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004033
4034 case AttributeList::AT_ObjCBridgeRelated:
4035 handleObjCBridgeRelatedAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00004036
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004037 case AttributeList::AT_ObjCDesignatedInitializer:
4038 handleObjCDesignatedInitializer(S, D, Attr); break;
4039
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004040 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00004041 handleCFAuditedTransferAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004042 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00004043 handleCFUnknownTransferAttr(S, D, Attr); break;
John McCall32f5fe12011-09-30 05:12:12 +00004044
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004045 case AttributeList::AT_CFConsumed:
4046 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4047 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004048 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004049
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004050 case AttributeList::AT_NSReturnsAutoreleased:
4051 case AttributeList::AT_NSReturnsNotRetained:
4052 case AttributeList::AT_CFReturnsNotRetained:
4053 case AttributeList::AT_NSReturnsRetained:
4054 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004055 handleNSReturnsRetainedAttr(S, D, Attr); break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004056 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004057 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004058 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004059 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004060 case AttributeList::AT_VecTypeHint:
4061 handleVecTypeHint(S, D, Attr); break;
4062
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004063 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004064 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004065
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004066 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4067 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4068 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004069 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004070 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004071 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004072 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004073 case AttributeList::AT_ObjCRootClass:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004074 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004075 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek28eace62013-11-23 01:01:34 +00004076 handleObjCSuppresProtocolAttr(S, D, Attr);
4077 break;
4078 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004079 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00004080 case AttributeList::AT_Unused:
4081 handleSimpleAttribute<UnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004082 case AttributeList::AT_ReturnsTwice:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004083 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004084 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004085 case AttributeList::AT_Visibility:
4086 handleVisibilityAttr(S, D, Attr, false);
4087 break;
4088 case AttributeList::AT_TypeVisibility:
4089 handleVisibilityAttr(S, D, Attr, true);
4090 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004091 case AttributeList::AT_WarnUnused:
Aaron Ballman6a42b5a2013-11-27 16:59:17 +00004092 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004093 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004094 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004095 case AttributeList::AT_Weak:
4096 handleSimpleAttribute<WeakAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004097 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4098 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4099 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004100 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004101 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004102 case AttributeList::AT_ObjCException:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004103 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004104 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004105 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004106 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004107 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4108 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4109 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004110 case AttributeList::AT_Const:
4111 handleSimpleAttribute<ConstAttr>(S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004112 case AttributeList::AT_Pure:
4113 handleSimpleAttribute<PureAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004114 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4115 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004116 case AttributeList::AT_NoInline:
4117 handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004118 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004119 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004120 case AttributeList::AT_StdCall:
4121 case AttributeList::AT_CDecl:
4122 case AttributeList::AT_FastCall:
4123 case AttributeList::AT_ThisCall:
4124 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004125 case AttributeList::AT_MSABI:
4126 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004127 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004128 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004129 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004130 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004131 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004132 case AttributeList::AT_OpenCLKernel:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004133 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004134 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman26891332014-01-14 17:41:53 +00004135 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr); break;
John McCall8d32c052012-05-22 21:28:12 +00004136
4137 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004138 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004139 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004140 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004141 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004142 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004143 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004144 case AttributeList::AT_MSInheritance:
4145 handleSimpleAttribute<MSInheritanceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004146 case AttributeList::AT_ForceInline:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004147 handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004148 case AttributeList::AT_SelectAny:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004149 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004150
4151 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004152 case AttributeList::AT_AssertExclusiveLock:
4153 handleAssertExclusiveLockAttr(S, D, Attr);
4154 break;
4155 case AttributeList::AT_AssertSharedLock:
4156 handleAssertSharedLockAttr(S, D, Attr);
4157 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004158 case AttributeList::AT_GuardedVar:
Aaron Ballmane61b8b82013-12-02 15:02:49 +00004159 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004160 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004161 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004162 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004163 case AttributeList::AT_ScopedLockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004164 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004165 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004166 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004167 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004168 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004169 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004170 break;
4171 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004172 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004173 break;
4174 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004175 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004176 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004177 case AttributeList::AT_Lockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004178 handleSimpleAttribute<LockableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004179 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004180 handleGuardedByAttr(S, D, Attr);
4181 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004182 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004183 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004184 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004185 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004186 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004187 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004188 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004189 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004190 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004191 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004192 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004193 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004194 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004195 handleLockReturnedAttr(S, D, Attr);
4196 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004197 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004198 handleLocksExcludedAttr(S, D, Attr);
4199 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004200 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004201 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004202 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004203 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004204 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004205 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004206 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004207 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004208 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004209 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004210 handleUnlockFunAttr(S, D, Attr);
4211 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004212 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004213 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004214 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004215 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004216 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004217 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004218
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004219 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004220 case AttributeList::AT_Consumable:
4221 handleConsumableAttr(S, D, Attr);
4222 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004223 case AttributeList::AT_ConsumableAutoCast:
4224 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr); break;
4225 break;
4226 case AttributeList::AT_ConsumableSetOnRead:
4227 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr); break;
4228 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004229 case AttributeList::AT_CallableWhen:
4230 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004231 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004232 case AttributeList::AT_ParamTypestate:
4233 handleParamTypestateAttr(S, D, Attr);
4234 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004235 case AttributeList::AT_ReturnTypestate:
4236 handleReturnTypestateAttr(S, D, Attr);
4237 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004238 case AttributeList::AT_SetTypestate:
4239 handleSetTypestateAttr(S, D, Attr);
4240 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004241 case AttributeList::AT_TestTypestate:
4242 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004243 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004244
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004245 // Type safety attributes.
4246 case AttributeList::AT_ArgumentWithTypeTag:
4247 handleArgumentWithTypeTagAttr(S, D, Attr);
4248 break;
4249 case AttributeList::AT_TypeTagForDatatype:
4250 handleTypeTagForDatatypeAttr(S, D, Attr);
4251 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004252 }
4253}
4254
4255/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4256/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004257void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004258 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004259 bool IncludeCXX11Attributes) {
4260 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004261 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004262
Joey Gouly2cd9db12013-12-13 16:15:28 +00004263 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004264 // GCC accepts
4265 // static int a9 __attribute__((weakref));
4266 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004267 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00004268 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
4269 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00004270 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004271 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004272 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004273
4274 if (!D->hasAttr<OpenCLKernelAttr>()) {
4275 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004276 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
4277 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004278 D->setInvalidDecl();
4279 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004280 if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
4281 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004282 D->setInvalidDecl();
4283 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004284 if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
4285 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004286 D->setInvalidDecl();
4287 }
4288 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004289}
4290
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004291// Annotation attributes are the only attributes allowed after an access
4292// specifier.
4293bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4294 const AttributeList *AttrList) {
4295 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004296 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004297 handleAnnotateAttr(*this, ASDecl, *l);
4298 } else {
4299 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4300 return true;
4301 }
4302 }
4303
4304 return false;
4305}
4306
John McCall42856de2011-10-01 05:17:03 +00004307/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4308/// contains any decl attributes that we should warn about.
4309static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4310 for ( ; A; A = A->getNext()) {
4311 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004312 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004313 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4314
4315 if (A->getKind() == AttributeList::UnknownAttribute) {
4316 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4317 << A->getName() << A->getRange();
4318 } else {
4319 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4320 << A->getName() << A->getRange();
4321 }
4322 }
4323}
4324
4325/// checkUnusedDeclAttributes - Given a declarator which is not being
4326/// used to build a declaration, complain about any decl attributes
4327/// which might be lying around on it.
4328void Sema::checkUnusedDeclAttributes(Declarator &D) {
4329 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4330 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4331 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4332 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4333}
4334
Ryan Flynn7d470f32009-07-30 03:15:39 +00004335/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004336/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004337NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4338 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004339 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004340 NamedDecl *NewD = 0;
4341 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004342 FunctionDecl *NewFD;
4343 // FIXME: Missing call to CheckFunctionDeclaration().
4344 // FIXME: Mangling?
4345 // FIXME: Is the qualifier info correct?
4346 // FIXME: Is the DeclContext correct?
4347 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4348 Loc, Loc, DeclarationName(II),
4349 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004350 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004351 FD->hasPrototype(),
4352 false/*isConstexprSpecified*/);
4353 NewD = NewFD;
4354
4355 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004356 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004357
4358 // Fake up parameter variables; they are declared as if this were
4359 // a typedef.
4360 QualType FDTy = FD->getType();
4361 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4362 SmallVector<ParmVarDecl*, 16> Params;
4363 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4364 AE = FT->arg_type_end(); AI != AE; ++AI) {
4365 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4366 Param->setScopeInfo(0, Params.size());
4367 Params.push_back(Param);
4368 }
David Blaikie9c70e042011-09-21 18:16:56 +00004369 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004370 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004371 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4372 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004373 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004374 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004375 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004376 if (VD->getQualifier()) {
4377 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004378 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004379 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004380 }
4381 return NewD;
4382}
4383
James Dennett634962f2012-06-14 21:40:34 +00004384/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004385/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004386void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004387 if (W.getUsed()) return; // only do this once
4388 W.setUsed(true);
4389 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4390 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004391 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00004392 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
4393 W.getLocation()));
4394 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00004395 WeakTopLevelDecl.push_back(NewD);
4396 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4397 // to insert Decl at TU scope, sorry.
4398 DeclContext *SavedContext = CurContext;
4399 CurContext = Context.getTranslationUnitDecl();
4400 PushOnScopeChains(NewD, S);
4401 CurContext = SavedContext;
4402 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00004403 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004404 }
4405}
4406
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004407void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4408 // It's valid to "forward-declare" #pragma weak, in which case we
4409 // have to do this.
4410 LoadExternalWeakUndeclaredIdentifiers();
4411 if (!WeakUndeclaredIdentifiers.empty()) {
4412 NamedDecl *ND = NULL;
4413 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4414 if (VD->isExternC())
4415 ND = VD;
4416 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4417 if (FD->isExternC())
4418 ND = FD;
4419 if (ND) {
4420 if (IdentifierInfo *Id = ND->getIdentifier()) {
4421 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4422 = WeakUndeclaredIdentifiers.find(Id);
4423 if (I != WeakUndeclaredIdentifiers.end()) {
4424 WeakInfo W = I->second;
4425 DeclApplyPragmaWeak(S, ND, W);
4426 WeakUndeclaredIdentifiers[Id] = W;
4427 }
4428 }
4429 }
4430 }
4431}
4432
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004433/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4434/// it, apply them to D. This is a bit tricky because PD can have attributes
4435/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004436void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004437 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004438 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004439 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004440
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004441 // Walk the declarator structure, applying decl attributes that were in a type
4442 // position to the decl itself. This handles cases like:
4443 // int *__attr__(x)** D;
4444 // when X is a decl attribute.
4445 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4446 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004447 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004448
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004449 // Finally, apply any attributes on the decl itself.
4450 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004451 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004452}
John McCall28a6aea2009-11-04 02:18:39 +00004453
John McCall31168b02011-06-15 23:02:42 +00004454/// Is the given declaration allowed to use a forbidden type?
4455static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4456 // Private ivars are always okay. Unfortunately, people don't
4457 // always properly make their ivars private, even in system headers.
4458 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004459 // Function declarations in sys headers will be marked unavailable.
4460 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4461 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004462 return false;
4463
4464 // Require it to be declared in a system header.
4465 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4466}
4467
4468/// Handle a delayed forbidden-type diagnostic.
4469static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4470 Decl *decl) {
4471 if (decl && isForbiddenTypeAllowed(S, decl)) {
Aaron Ballman36a53502014-01-16 13:03:14 +00004472 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context,
4473 "this system declaration uses an unsupported type",
4474 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00004475 return;
4476 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004477 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004478 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004479 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004480 // kind of forbidden type messages on unavailable functions.
4481 if (FD->hasAttr<UnavailableAttr>() &&
4482 diag.getForbiddenTypeDiagnostic() ==
4483 diag::err_arc_array_param_no_ownership) {
4484 diag.Triggered = true;
4485 return;
4486 }
4487 }
John McCall31168b02011-06-15 23:02:42 +00004488
4489 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4490 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4491 diag.Triggered = true;
4492}
4493
John McCall2ec85372012-05-07 06:16:41 +00004494void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4495 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004496 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004497 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004498
John McCall2ec85372012-05-07 06:16:41 +00004499 // When delaying diagnostics to run in the context of a parsed
4500 // declaration, we only want to actually emit anything if parsing
4501 // succeeds.
4502 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004503
John McCall2ec85372012-05-07 06:16:41 +00004504 // We emit all the active diagnostics in this pool or any of its
4505 // parents. In general, we'll get one pool for the decl spec
4506 // and a child pool for each declarator; in a decl group like:
4507 // deprecated_typedef foo, *bar, baz();
4508 // only the declarator pops will be passed decls. This is correct;
4509 // we really do need to consider delayed diagnostics from the decl spec
4510 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004511 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004512 do {
John McCall6347b682012-05-07 06:16:58 +00004513 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004514 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4515 // This const_cast is a bit lame. Really, Triggered should be mutable.
4516 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004517 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004518 continue;
4519
John McCallc1465822011-02-14 07:13:47 +00004520 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004521 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004522 case DelayedDiagnostic::Unavailable:
4523 // Don't bother giving deprecation/unavailable diagnostics if
4524 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004525 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004526 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004527 break;
4528
4529 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004530 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004531 break;
John McCall31168b02011-06-15 23:02:42 +00004532
4533 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004534 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004535 break;
John McCall86121512010-01-27 03:50:35 +00004536 }
4537 }
John McCall2ec85372012-05-07 06:16:41 +00004538 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004539}
4540
John McCall6347b682012-05-07 06:16:58 +00004541/// Given a set of delayed diagnostics, re-emit them as if they had
4542/// been delayed in the current context instead of in the given pool.
4543/// Essentially, this just moves them to the current pool.
4544void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4545 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4546 assert(curPool && "re-emitting in undelayed context not supported");
4547 curPool->steal(pool);
4548}
4549
John McCall28a6aea2009-11-04 02:18:39 +00004550static bool isDeclDeprecated(Decl *D) {
4551 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004552 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004553 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004554 // A category implicitly has the availability of the interface.
4555 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4556 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004557 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4558 return false;
4559}
4560
Ted Kremenekb79ee572013-12-18 23:30:06 +00004561static bool isDeclUnavailable(Decl *D) {
4562 do {
4563 if (D->isUnavailable())
4564 return true;
4565 // A category implicitly has the availability of the interface.
4566 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4567 return CatD->getClassInterface()->isUnavailable();
4568 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4569 return false;
4570}
4571
Eli Friedman971bfa12012-08-08 21:52:41 +00004572static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004573DoEmitAvailabilityWarning(Sema &S,
4574 DelayedDiagnostic::DDKind K,
4575 Decl *Ctx,
4576 const NamedDecl *D,
4577 StringRef Message,
4578 SourceLocation Loc,
4579 const ObjCInterfaceDecl *UnknownObjCClass,
4580 const ObjCPropertyDecl *ObjCProperty) {
4581
4582 // Diagnostics for deprecated or unavailable.
4583 unsigned diag, diag_message, diag_fwdclass_message;
4584
4585 // Matches 'diag::note_property_attribute' options.
4586 unsigned property_note_select;
4587
4588 // Matches diag::note_availability_specified_here.
4589 unsigned available_here_select_kind;
4590
4591 // Don't warn if our current context is deprecated or unavailable.
4592 switch (K) {
4593 case DelayedDiagnostic::Deprecation:
4594 if (isDeclDeprecated(Ctx))
4595 return;
4596 diag = diag::warn_deprecated;
4597 diag_message = diag::warn_deprecated_message;
4598 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4599 property_note_select = /* deprecated */ 0;
4600 available_here_select_kind = /* deprecated */ 2;
4601 break;
4602
4603 case DelayedDiagnostic::Unavailable:
4604 if (isDeclUnavailable(Ctx))
4605 return;
4606 diag = diag::err_unavailable;
4607 diag_message = diag::err_unavailable_message;
4608 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4609 property_note_select = /* unavailable */ 1;
4610 available_here_select_kind = /* unavailable */ 0;
4611 break;
4612
4613 default:
4614 llvm_unreachable("Neither a deprecation or unavailable kind");
4615 }
4616
Eli Friedman971bfa12012-08-08 21:52:41 +00004617 DeclarationName Name = D->getDeclName();
4618 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004619 S.Diag(Loc, diag_message) << Name << Message;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004620 if (ObjCProperty)
4621 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4622 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004623 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004624 S.Diag(Loc, diag) << Name;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004625 if (ObjCProperty)
4626 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4627 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004628 } else {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004629 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004630 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4631 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004632
4633 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4634 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004635}
4636
Ted Kremenekb79ee572013-12-18 23:30:06 +00004637void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4638 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004639 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004640 DoEmitAvailabilityWarning(*this,
4641 (DelayedDiagnostic::DDKind) DD.Kind,
4642 Ctx,
4643 DD.getDeprecationDecl(),
4644 DD.getDeprecationMessage(),
4645 DD.Loc,
4646 DD.getUnknownObjCClass(),
4647 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004648}
4649
Ted Kremenekb79ee572013-12-18 23:30:06 +00004650void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4651 NamedDecl *D, StringRef Message,
4652 SourceLocation Loc,
4653 const ObjCInterfaceDecl *UnknownObjCClass,
4654 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004655 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004656 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004657 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4658 UnknownObjCClass,
4659 ObjCProperty,
4660 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004661 return;
4662 }
4663
Ted Kremenekb79ee572013-12-18 23:30:06 +00004664 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4665 DelayedDiagnostic::DDKind K;
4666 switch (AD) {
4667 case AD_Deprecation:
4668 K = DelayedDiagnostic::Deprecation;
4669 break;
4670 case AD_Unavailable:
4671 K = DelayedDiagnostic::Unavailable;
4672 break;
4673 }
4674
4675 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
4676 UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004677}