blob: 945525bdd54e956e354ae7ef01e95aa5686137bb [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
Chandler Carruthff4c4f02011-07-01 23:49:12 +000046static const FunctionType *getFunctionType(const Decl *D,
Ted Kremenek527042b2009-08-14 20:49:40 +000047 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000048 QualType Ty;
Chandler Carruthff4c4f02011-07-01 23:49:12 +000049 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000050 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000051 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000052 Ty = decl->getUnderlyingType();
53 else
54 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000055
Chris Lattner2c6fcf52008-06-26 18:38:35 +000056 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000057 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000058 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000059 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000060
John McCall9dd450b2009-09-21 23:43:11 +000061 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000062}
63
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000064// FIXME: We should provide an abstraction around a method or function
65// to provide the following bits of information.
66
Nuno Lopes518e3702009-12-20 23:11:08 +000067/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000068/// type (function or function-typed variable).
Chandler Carruthff4c4f02011-07-01 23:49:12 +000069static bool isFunction(const Decl *D) {
70 return getFunctionType(D, false) != NULL;
Ted Kremenek527042b2009-08-14 20:49:40 +000071}
72
73/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000074/// type (function or function-typed variable) or an Objective-C
75/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000076static bool isFunctionOrMethod(const Decl *D) {
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +000077 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000078}
79
Fariborz Jahanian4447e172009-05-15 23:15:03 +000080/// isFunctionOrMethodOrBlock - Return true if the given decl has function
81/// type (function or function-typed variable) or an Objective-C
82/// method or a block.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000083static bool isFunctionOrMethodOrBlock(const Decl *D) {
84 if (isFunctionOrMethod(D))
Fariborz Jahanian4447e172009-05-15 23:15:03 +000085 return true;
86 // check for block is more involved.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000087 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000088 QualType Ty = V->getType();
89 return Ty->isBlockPointerType();
90 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +000091 return isa<BlockDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000092}
93
John McCall3882ace2011-01-05 12:14:39 +000094/// Return true if the given decl has a declarator that should have
95/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000096static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000097 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000098 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
99 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +0000100}
101
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000102/// hasFunctionProto - Return true if the given decl has a argument
103/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000104/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000105static bool hasFunctionProto(const Decl *D) {
106 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000107 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000108 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000109 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000110 return true;
111 }
112}
113
114/// getFunctionOrMethodNumArgs - Return number of function or method
115/// arguments. It is an error to call this on a K&R function (use
116/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000117static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
118 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000119 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000120 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000121 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000122 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000123}
124
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000125static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
126 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000127 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000128 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000129 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000130
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000131 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000132}
133
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000134static QualType getFunctionOrMethodResultType(const Decl *D) {
135 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000136 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000137 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000138}
139
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000140static bool isFunctionOrMethodVariadic(const Decl *D) {
141 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000142 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000143 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000144 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000145 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000146 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000147 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000148 }
149}
150
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000151static bool isInstanceMethod(const Decl *D) {
152 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000153 return MethodDecl->isInstance();
154 return false;
155}
156
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000157static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000158 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000159 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000160 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000161
John McCall96fa4842010-05-17 21:00:27 +0000162 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
163 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000164 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000165
John McCall96fa4842010-05-17 21:00:27 +0000166 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000167
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000168 // FIXME: Should we walk the chain of classes?
169 return ClsName == &Ctx.Idents.get("NSString") ||
170 ClsName == &Ctx.Idents.get("NSMutableString");
171}
172
Daniel Dunbar980c6692008-09-26 03:32:58 +0000173static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000174 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000175 if (!PT)
176 return false;
177
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000178 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000179 if (!RT)
180 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000181
Daniel Dunbar980c6692008-09-26 03:32:58 +0000182 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000183 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000184 return false;
185
186 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
187}
188
Richard Smithb87c4652013-10-31 21:23:20 +0000189static unsigned getNumAttributeArgs(const AttributeList &Attr) {
190 // FIXME: Include the type in the argument list.
191 return Attr.getNumArgs() + Attr.hasParsedType();
192}
193
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000194/// \brief Check if the attribute has exactly as many args as Num. May
195/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000196static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000197 unsigned Num) {
198 if (getNumAttributeArgs(Attr) != Num) {
Aaron Ballmanb7243382013-07-23 19:30:11 +0000199 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
200 << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000201 return false;
202 }
203
204 return true;
205}
206
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000207/// \brief Check if the attribute has at least as many args as Num. May
208/// output an error.
209static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000210 unsigned Num) {
211 if (getNumAttributeArgs(Attr) < Num) {
Aaron Ballman05e420a2014-01-02 21:26:14 +0000212 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments)
213 << Attr.getName() << Num;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000214 return false;
215 }
216
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000217 return true;
218}
219
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000220/// \brief If Expr is a valid integer constant, get the value of the integer
221/// expression and return success or failure. May output an error.
222static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
223 const Expr *Expr, uint32_t &Val,
224 unsigned Idx = UINT_MAX) {
225 llvm::APSInt I(32);
226 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
227 !Expr->isIntegerConstantExpr(I, S.Context)) {
228 if (Idx != UINT_MAX)
229 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
230 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
231 << Expr->getSourceRange();
232 else
233 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
234 << Attr.getName() << AANT_ArgumentIntegerConstant
235 << Expr->getSourceRange();
236 return false;
237 }
238 Val = (uint32_t)I.getZExtValue();
239 return true;
240}
241
Aaron Ballmanfb763042013-12-02 18:05:46 +0000242/// \brief Diagnose mutually exclusive attributes when present on a given
243/// declaration. Returns true if diagnosed.
244template <typename AttrTy>
245static bool checkAttrMutualExclusion(Sema &S, Decl *D,
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000246 const AttributeList &Attr) {
247 if (AttrTy *A = D->getAttr<AttrTy>()) {
Aaron Ballmanfb763042013-12-02 18:05:46 +0000248 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000249 << Attr.getName() << A;
Aaron Ballmanfb763042013-12-02 18:05:46 +0000250 return true;
251 }
252 return false;
253}
254
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000255/// \brief Check if IdxExpr is a valid argument index for a function or
256/// instance method D. May output an error.
257///
258/// \returns true if IdxExpr is a valid index.
259static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000260 const AttributeList &Attr,
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000261 unsigned AttrArgNum,
262 const Expr *IdxExpr,
263 uint64_t &Idx)
264{
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000265 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000266
267 // In C++ the implicit 'this' function parameter also counts.
268 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000269 bool HP = hasFunctionProto(D);
270 bool HasImplicitThisParam = isInstanceMethod(D);
271 bool IV = HP && isFunctionOrMethodVariadic(D);
272 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
273 HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000274
275 llvm::APSInt IdxInt;
276 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
277 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000278 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
279 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
280 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000281 return false;
282 }
283
284 Idx = IdxInt.getLimitedValue();
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000285 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000286 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
287 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000288 return false;
289 }
290 Idx--; // Convert to zero-based.
291 if (HasImplicitThisParam) {
292 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000293 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000294 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000295 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000296 return false;
297 }
298 --Idx;
299 }
300
301 return true;
302}
303
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000304/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
305/// If not emit an error and return false. If the argument is an identifier it
306/// will emit an error with a fixit hint and treat it as if it was a string
307/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000308bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
309 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000310 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000311 // Look for identifiers. If we have one emit a hint to fix it to a literal.
312 if (Attr.isArgIdent(ArgNum)) {
313 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000314 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000315 << Attr.getName() << AANT_ArgumentString
316 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000317 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000318 Str = Loc->Ident->getName();
319 if (ArgLocation)
320 *ArgLocation = Loc->Loc;
321 return true;
322 }
323
324 // Now check for an actual string literal.
325 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
326 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
327 if (ArgLocation)
328 *ArgLocation = ArgExpr->getLocStart();
329
330 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000331 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000332 << Attr.getName() << AANT_ArgumentString;
333 return false;
334 }
335
336 Str = Literal->getString();
337 return true;
338}
339
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000340/// \brief Applies the given attribute to the Decl without performing any
341/// additional semantic checking.
342template <typename AttrType>
343static void handleSimpleAttribute(Sema &S, Decl *D,
344 const AttributeList &Attr) {
345 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
346 Attr.getAttributeSpellingListIndex()));
347}
348
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000349/// \brief Check if the passed-in expression is of type int or bool.
350static bool isIntOrBool(Expr *Exp) {
351 QualType QT = Exp->getType();
352 return QT->isBooleanType() || QT->isIntegerType();
353}
354
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000355
356// Check to see if the type is a smart pointer of some kind. We assume
357// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000358static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
359 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
360 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000361 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000362 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000363
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000364 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
365 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000366 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000367 return false;
368
369 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000370}
371
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000372/// \brief Check if passed in Decl is a pointer type.
373/// Note that this function may produce an error message.
374/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000375static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
376 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000377 const ValueDecl *vd = cast<ValueDecl>(D);
378 QualType QT = vd->getType();
379 if (QT->isAnyPointerType())
380 return true;
381
382 if (const RecordType *RT = QT->getAs<RecordType>()) {
383 // If it's an incomplete type, it could be a smart pointer; skip it.
384 // (We don't want to force template instantiation if we can avoid it,
385 // since that would alter the order in which templates are instantiated.)
386 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000387 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000388
Aaron Ballman553e6812013-12-26 14:54:11 +0000389 if (threadSafetyCheckIsSmartPointer(S, RT))
390 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000391 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000392
393 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000394 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000395 return false;
396}
397
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000398/// \brief Checks that the passed in QualType either is of RecordType or points
399/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000400static const RecordType *getRecordType(QualType QT) {
401 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000402 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000403
404 // Now check if we point to record type.
405 if (const PointerType *PT = QT->getAs<PointerType>())
406 return PT->getPointeeType()->getAs<RecordType>();
407
408 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000409}
410
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000411
Jordy Rose740b0c22012-05-08 03:27:22 +0000412static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
413 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000414 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
Aaron Ballman9ead1242013-12-19 02:39:40 +0000415 return RT->getDecl()->hasAttr<LockableAttr>();
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000416}
417
418
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000419/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000420/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000421static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
422 QualType Ty) {
423 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000424
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000425 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000426 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000427 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
Aaron Ballman1da282a2014-01-02 23:15:58 +0000428 << Attr.getName() << Ty;
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000429 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000430 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000431
Michael Hana9171bc2012-08-03 17:40:43 +0000432 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000433 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000434 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000435
436 // Allow smart pointers to be used as lockable objects.
437 // FIXME -- Check the type that the smart pointer points to.
438 if (threadSafetyCheckIsSmartPointer(S, RT))
439 return;
440
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000441 // Check if the type is lockable.
442 RecordDecl *RD = RT->getDecl();
Aaron Ballman9ead1242013-12-19 02:39:40 +0000443 if (RD->hasAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000444 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000445
446 // Else check if any base classes are lockable.
447 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
448 CXXBasePaths BPaths(false, false);
449 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
450 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000451 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000452
453 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
Aaron Ballman1da282a2014-01-02 23:15:58 +0000454 << Attr.getName() << Ty;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000455}
456
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000457/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000458/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000459/// \param Sidx The attribute argument index to start checking with.
460/// \param ParamIdxOk Whether an argument can be indexing into a function
461/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000462static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000463 const AttributeList &Attr,
464 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000465 int Sidx = 0,
466 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000467 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000468 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000469
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000470 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000471 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000472 Args.push_back(ArgExp);
473 continue;
474 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000475
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000476 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000477 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000478 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000479 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000480 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000481 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000482 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000483 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000484
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000485 // We allow constant strings to be used as a placeholder for expressions
486 // that are not valid C++ syntax, but warn that they are ignored.
487 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
488 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000489 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000490 continue;
491 }
492
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000493 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000494
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000495 // A pointer to member expression of the form &MyClass::mu is treated
496 // specially -- we need to look at the type of the member.
497 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
498 if (UOp->getOpcode() == UO_AddrOf)
499 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
500 if (DRE->getDecl()->isCXXInstanceMember())
501 ArgTy = DRE->getDecl()->getType();
502
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000503 // First see if we can just cast to record type, or point to record type.
504 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000505
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000506 // Now check if we index into a record type function param.
507 if(!RT && ParamIdxOk) {
508 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000509 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
510 if(FD && IL) {
511 unsigned int NumParams = FD->getNumParams();
512 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000513 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
514 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
515 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000516 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
517 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000518 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000519 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000520 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000521 }
522 }
523
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000524 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000525
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000526 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000527 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000528}
529
Chris Lattner58418ff2008-06-29 00:16:31 +0000530//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000531// Attribute Implementations
532//===----------------------------------------------------------------------===//
533
Daniel Dunbar032db472008-07-31 22:40:48 +0000534// FIXME: All this manual attribute parsing code is gross. At the
535// least add some helper functions to check most argument patterns (#
536// and types of args).
537
Michael Hana9171bc2012-08-03 17:40:43 +0000538static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000539 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000540 if (!threadSafetyCheckIsPointer(S, D, Attr))
541 return;
542
Michael Han99315932013-01-24 16:46:58 +0000543 D->addAttr(::new (S.Context)
544 PtGuardedVarAttr(Attr.getRange(), S.Context,
545 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000546}
547
Michael Hana9171bc2012-08-03 17:40:43 +0000548static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
549 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000550 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000551 SmallVector<Expr*, 1> Args;
552 // check that all arguments are lockable objects
553 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
554 unsigned Size = Args.size();
555 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000556 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000557
Michael Han3be3b442012-07-23 18:48:41 +0000558 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000559
Michael Han3be3b442012-07-23 18:48:41 +0000560 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000561}
562
Michael Han3be3b442012-07-23 18:48:41 +0000563static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
564 Expr *Arg = 0;
565 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
566 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000567
Michael Han3be3b442012-07-23 18:48:41 +0000568 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
569}
570
Michael Hana9171bc2012-08-03 17:40:43 +0000571static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000572 const AttributeList &Attr) {
573 Expr *Arg = 0;
574 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
575 return;
576
577 if (!threadSafetyCheckIsPointer(S, D, Attr))
578 return;
579
580 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
581 S.Context, Arg));
582}
583
Michael Hana9171bc2012-08-03 17:40:43 +0000584static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
585 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000586 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000587 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000588 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000589
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000590 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000591 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000592 if (!QT->isDependentType()) {
593 const RecordType *RT = getRecordType(QT);
Aaron Ballman9ead1242013-12-19 02:39:40 +0000594 if (!RT || !RT->getDecl()->hasAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000595 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000596 << Attr.getName();
597 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000598 }
599 }
600
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000601 // Check that all arguments are lockable objects.
602 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000603 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000604 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000605
Michael Han3be3b442012-07-23 18:48:41 +0000606 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000607}
608
Michael Hana9171bc2012-08-03 17:40:43 +0000609static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000610 const AttributeList &Attr) {
611 SmallVector<Expr*, 1> Args;
612 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
613 return;
614
615 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000616 D->addAttr(::new (S.Context)
617 AcquiredAfterAttr(Attr.getRange(), S.Context,
618 StartArg, Args.size(),
619 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000620}
621
Michael Hana9171bc2012-08-03 17:40:43 +0000622static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000623 const AttributeList &Attr) {
624 SmallVector<Expr*, 1> Args;
625 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
626 return;
627
628 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000629 D->addAttr(::new (S.Context)
630 AcquiredBeforeAttr(Attr.getRange(), S.Context,
631 StartArg, Args.size(),
632 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000633}
634
Michael Hana9171bc2012-08-03 17:40:43 +0000635static bool checkLockFunAttrCommon(Sema &S, Decl *D,
636 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000637 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000638 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000639 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000640 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000641
Michael Han3be3b442012-07-23 18:48:41 +0000642 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000643}
644
Michael Hana9171bc2012-08-03 17:40:43 +0000645static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000646 const AttributeList &Attr) {
647 SmallVector<Expr*, 1> Args;
648 if (!checkLockFunAttrCommon(S, D, Attr, Args))
649 return;
650
651 unsigned Size = Args.size();
652 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000653 D->addAttr(::new (S.Context)
654 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
655 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000656}
657
Michael Hana9171bc2012-08-03 17:40:43 +0000658static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000659 const AttributeList &Attr) {
660 SmallVector<Expr*, 1> Args;
661 if (!checkLockFunAttrCommon(S, D, Attr, Args))
662 return;
663
664 unsigned Size = Args.size();
665 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000666 D->addAttr(::new (S.Context)
667 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
668 StartArg, Size,
669 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000670}
671
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000672static void handleAssertSharedLockAttr(Sema &S, Decl *D,
673 const AttributeList &Attr) {
674 SmallVector<Expr*, 1> Args;
675 if (!checkLockFunAttrCommon(S, D, Attr, Args))
676 return;
677
678 unsigned Size = Args.size();
679 Expr **StartArg = Size == 0 ? 0 : &Args[0];
680 D->addAttr(::new (S.Context)
681 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
682 Attr.getAttributeSpellingListIndex()));
683}
684
685static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
686 const AttributeList &Attr) {
687 SmallVector<Expr*, 1> Args;
688 if (!checkLockFunAttrCommon(S, D, Attr, Args))
689 return;
690
691 unsigned Size = Args.size();
692 Expr **StartArg = Size == 0 ? 0 : &Args[0];
693 D->addAttr(::new (S.Context)
694 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
695 StartArg, Size,
696 Attr.getAttributeSpellingListIndex()));
697}
698
699
Michael Hana9171bc2012-08-03 17:40:43 +0000700static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
701 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000702 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000703 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000704 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000705
Aaron Ballman00e99962013-08-31 01:11:41 +0000706 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000707 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000708 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000709 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000710 }
711
712 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000713 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000714
Michael Han3be3b442012-07-23 18:48:41 +0000715 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000716}
717
Michael Hana9171bc2012-08-03 17:40:43 +0000718static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000719 const AttributeList &Attr) {
720 SmallVector<Expr*, 2> Args;
721 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
722 return;
723
Michael Han99315932013-01-24 16:46:58 +0000724 D->addAttr(::new (S.Context)
725 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000726 Attr.getArgAsExpr(0),
727 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000728 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000729}
730
Michael Hana9171bc2012-08-03 17:40:43 +0000731static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000732 const AttributeList &Attr) {
733 SmallVector<Expr*, 2> Args;
734 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
735 return;
736
Michael Han99315932013-01-24 16:46:58 +0000737 D->addAttr(::new (S.Context)
738 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000739 Attr.getArgAsExpr(0),
740 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000741 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000742}
743
Michael Hana9171bc2012-08-03 17:40:43 +0000744static bool checkLocksRequiredCommon(Sema &S, Decl *D,
745 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000746 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000747 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000748 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000749
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000750 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000751 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000752 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000753 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000754
Michael Han3be3b442012-07-23 18:48:41 +0000755 return true;
756}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000757
Michael Hana9171bc2012-08-03 17:40:43 +0000758static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000759 const AttributeList &Attr) {
760 SmallVector<Expr*, 1> Args;
761 if (!checkLocksRequiredCommon(S, D, Attr, Args))
762 return;
763
764 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000765 D->addAttr(::new (S.Context)
766 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
767 StartArg, Args.size(),
768 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000769}
770
Michael Hana9171bc2012-08-03 17:40:43 +0000771static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000772 const AttributeList &Attr) {
773 SmallVector<Expr*, 1> Args;
774 if (!checkLocksRequiredCommon(S, D, Attr, Args))
775 return;
776
777 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000778 D->addAttr(::new (S.Context)
779 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
780 StartArg, Args.size(),
781 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000782}
783
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000784static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000785 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000786 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000787 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000788 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000789 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000790 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000791 Expr **StartArg = Size == 0 ? 0 : &Args[0];
792
Michael Han99315932013-01-24 16:46:58 +0000793 D->addAttr(::new (S.Context)
794 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
795 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000796}
797
798static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000799 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000800 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000801 SmallVector<Expr*, 1> Args;
802 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
803 unsigned Size = Args.size();
804 if (Size == 0)
805 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000806
Michael Han99315932013-01-24 16:46:58 +0000807 D->addAttr(::new (S.Context)
808 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
809 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000810}
811
812static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000813 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000814 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000815 return;
816
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000817 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000818 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000819 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000820 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000821 if (Size == 0)
822 return;
823 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000824
Michael Han99315932013-01-24 16:46:58 +0000825 D->addAttr(::new (S.Context)
826 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
827 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000828}
829
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000830static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000831 ConsumableAttr::ConsumedState DefaultState;
832
833 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000834 IdentifierLoc *IL = Attr.getArgAsIdent(0);
835 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
836 DefaultState)) {
837 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
838 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000839 return;
840 }
David Blaikie16f76d22013-09-06 01:28:43 +0000841 } else {
842 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
843 << Attr.getName() << AANT_ArgumentIdentifier;
844 return;
845 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000846
847 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000848 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000849 Attr.getAttributeSpellingListIndex()));
850}
851
852static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
853 const AttributeList &Attr) {
854 ASTContext &CurrContext = S.getASTContext();
855 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
856
857 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
858 if (!RD->hasAttr<ConsumableAttr>()) {
859 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
860 RD->getNameAsString();
861
862 return false;
863 }
864 }
865
866 return true;
867}
868
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000869
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000870static void handleCallableWhenAttr(Sema &S, Decl *D,
871 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000872 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
873 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000874
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000875 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
876 return;
877
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000878 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
879 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
880 CallableWhenAttr::ConsumedState CallableState;
881
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000882 StringRef StateString;
883 SourceLocation Loc;
884 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
885 return;
886
887 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000888 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000889 S.Diag(Loc, diag::warn_attribute_type_not_supported)
890 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000891 return;
892 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000893
894 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000895 }
896
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000897 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000898 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
899 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000900}
901
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000902
DeLesley Hutchins69391772013-10-17 23:23:53 +0000903static void handleParamTypestateAttr(Sema &S, Decl *D,
904 const AttributeList &Attr) {
905 if (!checkAttributeNumArgs(S, Attr, 1)) return;
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000906
DeLesley Hutchins69391772013-10-17 23:23:53 +0000907 ParamTypestateAttr::ConsumedState ParamState;
908
909 if (Attr.isArgIdent(0)) {
910 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
911 StringRef StateString = Ident->Ident->getName();
912
913 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
914 ParamState)) {
915 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
916 << Attr.getName() << StateString;
917 return;
918 }
919 } else {
920 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
921 Attr.getName() << AANT_ArgumentIdentifier;
922 return;
923 }
924
925 // FIXME: This check is currently being done in the analysis. It can be
926 // enabled here only after the parser propagates attributes at
927 // template specialization definition, not declaration.
928 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
929 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
930 //
931 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
932 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
933 // ReturnType.getAsString();
934 // return;
935 //}
936
937 D->addAttr(::new (S.Context)
938 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
939 Attr.getAttributeSpellingListIndex()));
940}
941
942
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000943static void handleReturnTypestateAttr(Sema &S, Decl *D,
944 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000945 if (!checkAttributeNumArgs(S, Attr, 1)) return;
946
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000947 ReturnTypestateAttr::ConsumedState ReturnState;
948
949 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000950 IdentifierLoc *IL = Attr.getArgAsIdent(0);
951 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
952 ReturnState)) {
953 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
954 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000955 return;
956 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000957 } else {
958 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
959 Attr.getName() << AANT_ArgumentIdentifier;
960 return;
961 }
962
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000963 // FIXME: This check is currently being done in the analysis. It can be
964 // enabled here only after the parser propagates attributes at
965 // template specialization definition, not declaration.
966 //QualType ReturnType;
967 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000968 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
969 // ReturnType = Param->getType();
970 //
971 //} else if (const CXXConstructorDecl *Constructor =
972 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000973 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
974 //
975 //} else {
976 //
977 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
978 //}
979 //
980 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
981 //
982 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
983 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
984 // ReturnType.getAsString();
985 // return;
986 //}
987
988 D->addAttr(::new (S.Context)
989 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
990 Attr.getAttributeSpellingListIndex()));
991}
992
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000993
994static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000995 if (!checkAttributeNumArgs(S, Attr, 1))
996 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000997
998 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
999 return;
1000
1001 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001002 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001003 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1004 StringRef Param = Ident->Ident->getName();
1005 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1006 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1007 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001008 return;
1009 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001010 } else {
1011 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1012 Attr.getName() << AANT_ArgumentIdentifier;
1013 return;
1014 }
1015
1016 D->addAttr(::new (S.Context)
1017 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1018 Attr.getAttributeSpellingListIndex()));
1019}
1020
Chris Wailes9385f9f2013-10-29 20:28:41 +00001021static void handleTestTypestateAttr(Sema &S, Decl *D,
1022 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001023 if (!checkAttributeNumArgs(S, Attr, 1))
1024 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001025
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001026 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1027 return;
1028
Chris Wailes9385f9f2013-10-29 20:28:41 +00001029 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001030 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001031 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1032 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001033 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001034 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1035 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001036 return;
1037 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001038 } else {
1039 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1040 Attr.getName() << AANT_ArgumentIdentifier;
1041 return;
1042 }
1043
1044 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001045 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001046 Attr.getAttributeSpellingListIndex()));
1047}
1048
Chandler Carruthedc2c642011-07-02 00:01:44 +00001049static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1050 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001051 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001052 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001053}
1054
Chandler Carruthedc2c642011-07-02 00:01:44 +00001055static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001056 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001057 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001058 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001059 // If the alignment is less than or equal to 8 bits, the packed attribute
1060 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001061 if (!FD->getType()->isDependentType() &&
1062 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001063 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001064 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001065 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001066 else
Michael Han99315932013-01-24 16:46:58 +00001067 FD->addAttr(::new (S.Context)
1068 PackedAttr(Attr.getRange(), S.Context,
1069 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001070 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001071 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001072}
1073
Ted Kremenek7fd17232011-09-29 07:02:25 +00001074static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1075 // The IBOutlet/IBOutletCollection attributes only apply to instance
1076 // variables or properties of Objective-C classes. The outlet must also
1077 // have an object reference type.
1078 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1079 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001080 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001081 << Attr.getName() << VD->getType() << 0;
1082 return false;
1083 }
1084 }
1085 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1086 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001087 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001088 << Attr.getName() << PD->getType() << 1;
1089 return false;
1090 }
1091 }
1092 else {
1093 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1094 return false;
1095 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001096
Ted Kremenek7fd17232011-09-29 07:02:25 +00001097 return true;
1098}
1099
Chandler Carruthedc2c642011-07-02 00:01:44 +00001100static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001101 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001102 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001103
Michael Han99315932013-01-24 16:46:58 +00001104 D->addAttr(::new (S.Context)
1105 IBOutletAttr(Attr.getRange(), S.Context,
1106 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001107}
1108
Chandler Carruthedc2c642011-07-02 00:01:44 +00001109static void handleIBOutletCollection(Sema &S, Decl *D,
1110 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001111
1112 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001113 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001114 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1115 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001116 return;
1117 }
1118
Ted Kremenek7fd17232011-09-29 07:02:25 +00001119 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001120 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001121
Richard Smithb1f9a282013-10-31 01:56:18 +00001122 ParsedType PT;
1123
1124 if (Attr.hasParsedType())
1125 PT = Attr.getTypeArg();
1126 else {
1127 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1128 S.getScopeForContext(D->getDeclContext()->getParent()));
1129 if (!PT) {
1130 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1131 return;
1132 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001133 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001134
Richard Smithb87c4652013-10-31 21:23:20 +00001135 TypeSourceInfo *QTLoc = 0;
1136 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1137 if (!QTLoc)
1138 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001139
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001140 // Diagnose use of non-object type in iboutletcollection attribute.
1141 // FIXME. Gnu attribute extension ignores use of builtin types in
1142 // attributes. So, __attribute__((iboutletcollection(char))) will be
1143 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001144 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001145 S.Diag(Attr.getLoc(),
1146 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1147 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001148 return;
1149 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001150
Michael Han99315932013-01-24 16:46:58 +00001151 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001152 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001153 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001154}
1155
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001156static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001157 if (const RecordType *UT = T->getAsUnionType())
1158 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1159 RecordDecl *UD = UT->getDecl();
1160 for (RecordDecl::field_iterator it = UD->field_begin(),
1161 itend = UD->field_end(); it != itend; ++it) {
1162 QualType QT = it->getType();
1163 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1164 T = QT;
1165 return;
1166 }
1167 }
1168 }
1169}
1170
Chandler Carruthedc2c642011-07-02 00:01:44 +00001171static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001172 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1173 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001174 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001175 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001176 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001177 return;
1178 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001179
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001180 SmallVector<unsigned, 8> NonNullArgs;
1181 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001182 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001183 uint64_t Idx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00001184 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001185 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001186
1187 // Is the function argument a pointer type?
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001188 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001189 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001190
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001191 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001192 // FIXME: Should also highlight argument in decl.
Aaron Ballmancedaaea2013-12-26 17:07:49 +00001193 S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only)
1194 << Attr.getName() << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001195 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001196 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001197
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001198 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001199 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001200
1201 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1202 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001203 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001204 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1205 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001206 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001207 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001208 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001209 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001210
Ted Kremenek22813f42010-10-21 18:49:36 +00001211 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001212 if (NonNullArgs.empty()) {
1213 // Warn the trivial case only if attribute is not coming from a
1214 // macro instantiation.
1215 if (Attr.getLoc().isFileID())
1216 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001217 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001218 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001219 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001220
Nick Lewyckye1121512013-01-24 01:12:16 +00001221 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001222 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001223 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001224 D->addAttr(::new (S.Context)
1225 NonNullAttr(Attr.getRange(), S.Context, start, size,
1226 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001227}
1228
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001229static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1230 switch (K) {
1231 case OwnershipAttr::Holds: return "'ownership_holds'";
1232 case OwnershipAttr::Takes: return "'ownership_takes'";
1233 case OwnershipAttr::Returns: return "'ownership_returns'";
1234 }
1235 llvm_unreachable("unknown ownership");
1236}
1237
Chandler Carruthedc2c642011-07-02 00:01:44 +00001238static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001239 // This attribute must be applied to a function declaration. The first
1240 // argument to the attribute must be an identifier, the name of the resource,
1241 // for example: malloc. The following arguments must be argument indexes, the
1242 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001243 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001244 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001245 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001246
Aaron Ballman00e99962013-08-31 01:11:41 +00001247 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001248 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001249 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001250 return;
1251 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001252
Richard Smith852e9ce2013-11-27 01:46:48 +00001253 // Figure out our Kind.
1254 OwnershipAttr::OwnershipKind K =
1255 OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0,
1256 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001257
Richard Smith852e9ce2013-11-27 01:46:48 +00001258 // Check arguments.
1259 switch (K) {
1260 case OwnershipAttr::Takes:
1261 case OwnershipAttr::Holds:
1262 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001263 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1264 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001265 return;
1266 }
1267 break;
1268 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001269 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001270 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1271 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001272 return;
1273 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001274 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001275 }
1276
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001277 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001278 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1279 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001280 return;
1281 }
1282
Richard Smith852e9ce2013-11-27 01:46:48 +00001283 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001284
1285 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001286 StringRef ModuleName = Module->getName();
1287 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1288 ModuleName.size() > 4) {
1289 ModuleName = ModuleName.drop_front(2).drop_back(2);
1290 Module = &S.PP.getIdentifierTable().get(ModuleName);
1291 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001292
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001293 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001294 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1295 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001296 uint64_t Idx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00001297 if (!checkFunctionOrMethodArgumentIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001298 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001299
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001300 // Is the function argument a pointer type?
1301 QualType T = getFunctionOrMethodArgType(D, Idx);
1302 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001303 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001304 case OwnershipAttr::Takes:
1305 case OwnershipAttr::Holds:
1306 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1307 Err = 0;
1308 break;
1309 case OwnershipAttr::Returns:
1310 if (!T->isIntegerType())
1311 Err = 1;
1312 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001313 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001314 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001315 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001316 << Ex->getSourceRange();
1317 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001318 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001319
1320 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001321 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001322 i = D->specific_attr_begin<OwnershipAttr>(),
1323 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001324 // FIXME: A returns attribute should conflict with any returns attribute
1325 // with a different index too.
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001326 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1327 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1328 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1329 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1330 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001331 }
1332 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001333 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001334 }
1335
1336 unsigned* start = OwnershipArgs.data();
1337 unsigned size = OwnershipArgs.size();
1338 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001339
Michael Han99315932013-01-24 16:46:58 +00001340 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001341 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001342 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001343}
1344
Chandler Carruthedc2c642011-07-02 00:01:44 +00001345static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001346 // Check the attribute arguments.
1347 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001348 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1349 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001350 return;
1351 }
1352
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001353 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001354
Rafael Espindolac18086a2010-02-23 22:00:30 +00001355 // gcc rejects
1356 // class c {
1357 // static int a __attribute__((weakref ("v2")));
1358 // static int b() __attribute__((weakref ("f3")));
1359 // };
1360 // and ignores the attributes of
1361 // void f(void) {
1362 // static int a __attribute__((weakref ("v2")));
1363 // }
1364 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001365 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001366 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001367 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1368 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001369 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001370 }
1371
1372 // The GCC manual says
1373 //
1374 // At present, a declaration to which `weakref' is attached can only
1375 // be `static'.
1376 //
1377 // It also says
1378 //
1379 // Without a TARGET,
1380 // given as an argument to `weakref' or to `alias', `weakref' is
1381 // equivalent to `weak'.
1382 //
1383 // gcc 4.4.1 will accept
1384 // int a7 __attribute__((weakref));
1385 // as
1386 // int a7 __attribute__((weak));
1387 // This looks like a bug in gcc. We reject that for now. We should revisit
1388 // it if this behaviour is actually used.
1389
Rafael Espindolac18086a2010-02-23 22:00:30 +00001390 // GCC rejects
1391 // static ((alias ("y"), weakref)).
1392 // Should we? How to check that weakref is before or after alias?
1393
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001394 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1395 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1396 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001397 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001398 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001399 // GCC will accept anything as the argument of weakref. Should we
1400 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001401 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1402 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001403
Michael Han99315932013-01-24 16:46:58 +00001404 D->addAttr(::new (S.Context)
1405 WeakRefAttr(Attr.getRange(), S.Context,
1406 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001407}
1408
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001409static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1410 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001411 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001412 return;
1413
Douglas Gregore8bbc122011-09-02 00:18:52 +00001414 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001415 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1416 return;
1417 }
1418
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001419 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001420
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001421 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001422 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001423}
1424
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001425static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001426 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001427 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001428
Michael Han99315932013-01-24 16:46:58 +00001429 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1430 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001431}
1432
1433static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001434 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001435 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001436
Michael Han99315932013-01-24 16:46:58 +00001437 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1438 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001439}
1440
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001441static void handleTLSModelAttr(Sema &S, Decl *D,
1442 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001443 StringRef Model;
1444 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001445 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001446 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001447 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001448
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001449 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001450 if (Model != "global-dynamic" && Model != "local-dynamic"
1451 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001452 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001453 return;
1454 }
1455
Michael Han99315932013-01-24 16:46:58 +00001456 D->addAttr(::new (S.Context)
1457 TLSModelAttr(Attr.getRange(), S.Context, Model,
1458 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001459}
1460
Chandler Carruthedc2c642011-07-02 00:01:44 +00001461static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001462 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001463 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001464 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001465 D->addAttr(::new (S.Context)
1466 MallocAttr(Attr.getRange(), S.Context,
1467 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001468 return;
1469 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001470 }
1471
Ted Kremenek08479ae2009-08-15 00:51:46 +00001472 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001473}
1474
Chandler Carruthedc2c642011-07-02 00:01:44 +00001475static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001476 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001477 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1478 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001479 return;
1480 }
1481
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001482 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1483 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001484}
1485
Chandler Carruthedc2c642011-07-02 00:01:44 +00001486static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001487 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001488
1489 if (S.CheckNoReturnAttr(attr)) return;
1490
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001491 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001492 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001493 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001494 return;
1495 }
1496
Michael Han99315932013-01-24 16:46:58 +00001497 D->addAttr(::new (S.Context)
1498 NoReturnAttr(attr.getRange(), S.Context,
1499 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001500}
1501
1502bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001503 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001504 attr.setInvalid();
1505 return true;
1506 }
1507
1508 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001509}
1510
Chandler Carruthedc2c642011-07-02 00:01:44 +00001511static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1512 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001513
1514 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1515 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001516 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1517 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001518 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1519 && !VD->getType()->isFunctionPointerType())) {
1520 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001521 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001522 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001523 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001524 return;
1525 }
1526 }
1527
Michael Han99315932013-01-24 16:46:58 +00001528 D->addAttr(::new (S.Context)
1529 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1530 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001531}
1532
John Thompsoncdb847ba2010-08-09 21:53:52 +00001533// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001534static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001535/*
1536 Returning a Vector Class in Registers
1537
Eric Christopherbc638a82010-12-01 22:13:54 +00001538 According to the PPU ABI specifications, a class with a single member of
1539 vector type is returned in memory when used as the return value of a function.
1540 This results in inefficient code when implementing vector classes. To return
1541 the value in a single vector register, add the vecreturn attribute to the
1542 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001543
1544 Example:
1545
1546 struct Vector
1547 {
1548 __vector float xyzw;
1549 } __attribute__((vecreturn));
1550
1551 Vector Add(Vector lhs, Vector rhs)
1552 {
1553 Vector result;
1554 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1555 return result; // This will be returned in a register
1556 }
1557*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001558 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1559 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001560 return;
1561 }
1562
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001563 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001564 int count = 0;
1565
1566 if (!isa<CXXRecordDecl>(record)) {
1567 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1568 return;
1569 }
1570
1571 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1572 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1573 return;
1574 }
1575
Eric Christopherbc638a82010-12-01 22:13:54 +00001576 for (RecordDecl::field_iterator iter = record->field_begin();
1577 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001578 if ((count == 1) || !iter->getType()->isVectorType()) {
1579 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1580 return;
1581 }
1582 count++;
1583 }
1584
Michael Han99315932013-01-24 16:46:58 +00001585 D->addAttr(::new (S.Context)
1586 VecReturnAttr(Attr.getRange(), S.Context,
1587 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001588}
1589
Richard Smithe233fbf2013-01-28 22:42:45 +00001590static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1591 const AttributeList &Attr) {
1592 if (isa<ParmVarDecl>(D)) {
1593 // [[carries_dependency]] can only be applied to a parameter if it is a
1594 // parameter of a function declaration or lambda.
1595 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1596 S.Diag(Attr.getLoc(),
1597 diag::err_carries_dependency_param_not_function_decl);
1598 return;
1599 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001600 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001601
1602 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1603 Attr.getRange(), S.Context,
1604 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001605}
1606
Chandler Carruthedc2c642011-07-02 00:01:44 +00001607static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001608 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001609 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001610 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001611 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001612 return;
1613 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001614
Michael Han99315932013-01-24 16:46:58 +00001615 D->addAttr(::new (S.Context)
1616 UnusedAttr(Attr.getRange(), S.Context,
1617 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001618}
1619
Chandler Carruthedc2c642011-07-02 00:01:44 +00001620static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001621 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001622 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001623 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001624 return;
1625 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001626 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001627 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001628 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001629 return;
1630 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001631
Michael Han99315932013-01-24 16:46:58 +00001632 D->addAttr(::new (S.Context)
1633 UsedAttr(Attr.getRange(), S.Context,
1634 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001635}
1636
Chandler Carruthedc2c642011-07-02 00:01:44 +00001637static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001638 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001639 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001640 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1641 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001642 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001643 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001644
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001645 uint32_t priority = ConstructorAttr::DefaultPriority;
1646 if (Attr.getNumArgs() > 0 &&
1647 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1648 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001649
Michael Han99315932013-01-24 16:46:58 +00001650 D->addAttr(::new (S.Context)
1651 ConstructorAttr(Attr.getRange(), S.Context, priority,
1652 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001653}
1654
Chandler Carruthedc2c642011-07-02 00:01:44 +00001655static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001656 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001657 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001658 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1659 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001660 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001661 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001662
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001663 uint32_t priority = ConstructorAttr::DefaultPriority;
1664 if (Attr.getNumArgs() > 0 &&
1665 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1666 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001667
Michael Han99315932013-01-24 16:46:58 +00001668 D->addAttr(::new (S.Context)
1669 DestructorAttr(Attr.getRange(), S.Context, priority,
1670 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001671}
1672
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001673template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001674static void handleAttrWithMessage(Sema &S, Decl *D,
1675 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001676 unsigned NumArgs = Attr.getNumArgs();
1677 if (NumArgs > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001678 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1679 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001680 return;
1681 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001682
1683 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001684 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001685 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001686 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001687
Michael Han99315932013-01-24 16:46:58 +00001688 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1689 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001690}
1691
Ted Kremenek28eace62013-11-23 01:01:34 +00001692static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
1693 const AttributeList &Attr) {
Ted Kremenek28eace62013-11-23 01:01:34 +00001694 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001695 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1696 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001697}
1698
Jordy Rose740b0c22012-05-08 03:27:22 +00001699static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1700 IdentifierInfo *Platform,
1701 VersionTuple Introduced,
1702 VersionTuple Deprecated,
1703 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001704 StringRef PlatformName
1705 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1706 if (PlatformName.empty())
1707 PlatformName = Platform->getName();
1708
1709 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1710 // of these steps are needed).
1711 if (!Introduced.empty() && !Deprecated.empty() &&
1712 !(Introduced <= Deprecated)) {
1713 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1714 << 1 << PlatformName << Deprecated.getAsString()
1715 << 0 << Introduced.getAsString();
1716 return true;
1717 }
1718
1719 if (!Introduced.empty() && !Obsoleted.empty() &&
1720 !(Introduced <= Obsoleted)) {
1721 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1722 << 2 << PlatformName << Obsoleted.getAsString()
1723 << 0 << Introduced.getAsString();
1724 return true;
1725 }
1726
1727 if (!Deprecated.empty() && !Obsoleted.empty() &&
1728 !(Deprecated <= Obsoleted)) {
1729 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1730 << 2 << PlatformName << Obsoleted.getAsString()
1731 << 1 << Deprecated.getAsString();
1732 return true;
1733 }
1734
1735 return false;
1736}
1737
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001738/// \brief Check whether the two versions match.
1739///
1740/// If either version tuple is empty, then they are assumed to match. If
1741/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1742static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1743 bool BeforeIsOkay) {
1744 if (X.empty() || Y.empty())
1745 return true;
1746
1747 if (X == Y)
1748 return true;
1749
1750 if (BeforeIsOkay && X < Y)
1751 return true;
1752
1753 return false;
1754}
1755
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001756AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001757 IdentifierInfo *Platform,
1758 VersionTuple Introduced,
1759 VersionTuple Deprecated,
1760 VersionTuple Obsoleted,
1761 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001762 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001763 bool Override,
1764 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001765 VersionTuple MergedIntroduced = Introduced;
1766 VersionTuple MergedDeprecated = Deprecated;
1767 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001768 bool FoundAny = false;
1769
Rafael Espindolac67f2232012-05-10 02:50:16 +00001770 if (D->hasAttrs()) {
1771 AttrVec &Attrs = D->getAttrs();
1772 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1773 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1774 if (!OldAA) {
1775 ++i;
1776 continue;
1777 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001778
Rafael Espindolac67f2232012-05-10 02:50:16 +00001779 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1780 if (OldPlatform != Platform) {
1781 ++i;
1782 continue;
1783 }
1784
1785 FoundAny = true;
1786 VersionTuple OldIntroduced = OldAA->getIntroduced();
1787 VersionTuple OldDeprecated = OldAA->getDeprecated();
1788 VersionTuple OldObsoleted = OldAA->getObsoleted();
1789 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001790
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001791 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1792 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1793 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1794 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001795 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001796 if (Override) {
1797 int Which = -1;
1798 VersionTuple FirstVersion;
1799 VersionTuple SecondVersion;
1800 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1801 Which = 0;
1802 FirstVersion = OldIntroduced;
1803 SecondVersion = Introduced;
1804 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1805 Which = 1;
1806 FirstVersion = Deprecated;
1807 SecondVersion = OldDeprecated;
1808 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1809 Which = 2;
1810 FirstVersion = Obsoleted;
1811 SecondVersion = OldObsoleted;
1812 }
1813
1814 if (Which == -1) {
1815 Diag(OldAA->getLocation(),
1816 diag::warn_mismatched_availability_override_unavail)
1817 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1818 } else {
1819 Diag(OldAA->getLocation(),
1820 diag::warn_mismatched_availability_override)
1821 << Which
1822 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1823 << FirstVersion.getAsString() << SecondVersion.getAsString();
1824 }
1825 Diag(Range.getBegin(), diag::note_overridden_method);
1826 } else {
1827 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1828 Diag(Range.getBegin(), diag::note_previous_attribute);
1829 }
1830
Rafael Espindolac67f2232012-05-10 02:50:16 +00001831 Attrs.erase(Attrs.begin() + i);
1832 --e;
1833 continue;
1834 }
1835
1836 VersionTuple MergedIntroduced2 = MergedIntroduced;
1837 VersionTuple MergedDeprecated2 = MergedDeprecated;
1838 VersionTuple MergedObsoleted2 = MergedObsoleted;
1839
1840 if (MergedIntroduced2.empty())
1841 MergedIntroduced2 = OldIntroduced;
1842 if (MergedDeprecated2.empty())
1843 MergedDeprecated2 = OldDeprecated;
1844 if (MergedObsoleted2.empty())
1845 MergedObsoleted2 = OldObsoleted;
1846
1847 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1848 MergedIntroduced2, MergedDeprecated2,
1849 MergedObsoleted2)) {
1850 Attrs.erase(Attrs.begin() + i);
1851 --e;
1852 continue;
1853 }
1854
1855 MergedIntroduced = MergedIntroduced2;
1856 MergedDeprecated = MergedDeprecated2;
1857 MergedObsoleted = MergedObsoleted2;
1858 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001859 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001860 }
1861
1862 if (FoundAny &&
1863 MergedIntroduced == Introduced &&
1864 MergedDeprecated == Deprecated &&
1865 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001866 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001867
Ted Kremenekb5445722013-04-06 00:34:27 +00001868 // Only create a new attribute if !Override, but we want to do
1869 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001870 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001871 MergedDeprecated, MergedObsoleted) &&
1872 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001873 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1874 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001875 Obsoleted, IsUnavailable, Message,
1876 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001877 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001878 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001879}
1880
Chandler Carruthedc2c642011-07-02 00:01:44 +00001881static void handleAvailabilityAttr(Sema &S, Decl *D,
1882 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001883 if (!checkAttributeNumArgs(S, Attr, 1))
1884 return;
1885 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001886 unsigned Index = Attr.getAttributeSpellingListIndex();
1887
Aaron Ballman00e99962013-08-31 01:11:41 +00001888 IdentifierInfo *II = Platform->Ident;
1889 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1890 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1891 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001892
Rafael Espindolac231fab2013-01-08 21:30:32 +00001893 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1894 if (!ND) {
1895 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1896 return;
1897 }
1898
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001899 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1900 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1901 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001902 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001903 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001904 if (const StringLiteral *SE =
1905 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001906 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001907
Aaron Ballman00e99962013-08-31 01:11:41 +00001908 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001909 Introduced.Version,
1910 Deprecated.Version,
1911 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001912 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001913 /*Override=*/false,
1914 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001915 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001916 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001917}
1918
John McCalld041a9b2013-02-20 01:54:26 +00001919template <class T>
1920static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1921 typename T::VisibilityType value,
1922 unsigned attrSpellingListIndex) {
1923 T *existingAttr = D->getAttr<T>();
1924 if (existingAttr) {
1925 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1926 if (existingValue == value)
1927 return NULL;
1928 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1929 S.Diag(range.getBegin(), diag::note_previous_attribute);
1930 D->dropAttr<T>();
1931 }
1932 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1933}
1934
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001935VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001936 VisibilityAttr::VisibilityType Vis,
1937 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001938 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1939 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001940}
1941
John McCalld041a9b2013-02-20 01:54:26 +00001942TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1943 TypeVisibilityAttr::VisibilityType Vis,
1944 unsigned AttrSpellingListIndex) {
1945 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1946 AttrSpellingListIndex);
1947}
1948
1949static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1950 bool isTypeVisibility) {
1951 // Visibility attributes don't mean anything on a typedef.
1952 if (isa<TypedefNameDecl>(D)) {
1953 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
1954 << Attr.getName();
1955 return;
1956 }
1957
1958 // 'type_visibility' can only go on a type or namespace.
1959 if (isTypeVisibility &&
1960 !(isa<TagDecl>(D) ||
1961 isa<ObjCInterfaceDecl>(D) ||
1962 isa<NamespaceDecl>(D))) {
1963 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
1964 << Attr.getName() << ExpectedTypeOrNamespace;
1965 return;
1966 }
1967
Benjamin Kramer70370212013-09-09 15:08:57 +00001968 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001969 StringRef TypeStr;
1970 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001971 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001972 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001973
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001974 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00001975 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001976 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00001977 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001978 return;
1979 }
Aaron Ballman682ee422013-09-11 19:47:58 +00001980
1981 // Complain about attempts to use protected visibility on targets
1982 // (like Darwin) that don't support it.
1983 if (type == VisibilityAttr::Protected &&
1984 !S.Context.getTargetInfo().hasProtectedVisibility()) {
1985 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1986 type = VisibilityAttr::Default;
1987 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001988
Michael Han99315932013-01-24 16:46:58 +00001989 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00001990 clang::Attr *newAttr;
1991 if (isTypeVisibility) {
1992 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
1993 (TypeVisibilityAttr::VisibilityType) type,
1994 Index);
1995 } else {
1996 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
1997 }
1998 if (newAttr)
1999 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002000}
2001
Chandler Carruthedc2c642011-07-02 00:01:44 +00002002static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2003 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002004 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002005 if (!Attr.isArgIdent(0)) {
2006 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2007 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002008 return;
2009 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002010
Aaron Ballman682ee422013-09-11 19:47:58 +00002011 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2012 ObjCMethodFamilyAttr::FamilyKind F;
2013 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2014 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2015 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002016 return;
2017 }
2018
Aaron Ballman682ee422013-09-11 19:47:58 +00002019 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002020 !method->getResultType()->isObjCObjectPointerType()) {
2021 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2022 << method->getResultType();
2023 // Ignore the attribute.
2024 return;
2025 }
2026
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002027 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman682ee422013-09-11 19:47:58 +00002028 S.Context, F));
John McCall86bc21f2011-03-02 11:33:24 +00002029}
2030
Chandler Carruthedc2c642011-07-02 00:01:44 +00002031static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002032 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002033 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002034 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002035 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2036 return;
2037 }
2038 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002039 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2040 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002041 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002042 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2043 return;
2044 }
2045 }
2046 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002047 // It is okay to include this attribute on properties, e.g.:
2048 //
2049 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2050 //
2051 // In this case it follows tradition and suppresses an error in the above
2052 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002053 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002054 }
Michael Han99315932013-01-24 16:46:58 +00002055 D->addAttr(::new (S.Context)
2056 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2057 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002058}
2059
Chandler Carruthedc2c642011-07-02 00:01:44 +00002060static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002061 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002062 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002063 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002064 return;
2065 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002066
Aaron Ballman00e99962013-08-31 01:11:41 +00002067 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002068 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002069 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2070 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2071 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002072 return;
2073 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002074
Michael Han99315932013-01-24 16:46:58 +00002075 D->addAttr(::new (S.Context)
2076 BlocksAttr(Attr.getRange(), S.Context, type,
2077 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002078}
2079
Chandler Carruthedc2c642011-07-02 00:01:44 +00002080static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002081 // check the attribute arguments.
2082 if (Attr.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00002083 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
2084 << Attr.getName() << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002085 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002086 }
2087
Aaron Ballman18a78382013-11-21 00:28:23 +00002088 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002089 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002090 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002091 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002092 if (E->isTypeDependent() || E->isValueDependent() ||
2093 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002094 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002095 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002096 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002097 return;
2098 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002099
John McCallb46f2872011-09-09 07:56:05 +00002100 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002101 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2102 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002103 return;
2104 }
John McCallb46f2872011-09-09 07:56:05 +00002105
2106 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002107 }
2108
Aaron Ballman18a78382013-11-21 00:28:23 +00002109 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002110 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002111 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002112 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002113 if (E->isTypeDependent() || E->isValueDependent() ||
2114 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002115 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002116 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002117 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002118 return;
2119 }
2120 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002121
John McCallb46f2872011-09-09 07:56:05 +00002122 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002123 // FIXME: This error message could be improved, it would be nice
2124 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002125 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2126 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002127 return;
2128 }
2129 }
2130
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002131 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002132 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002133 if (isa<FunctionNoProtoType>(FT)) {
2134 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2135 return;
2136 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002137
Chris Lattner9363e312009-03-17 23:03:47 +00002138 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002139 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002140 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002141 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002142 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002143 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002144 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002145 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002146 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002147 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2148 if (!BD->isVariadic()) {
2149 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2150 return;
2151 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002152 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002153 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002154 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002155 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002156 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002157 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002158 int m = Ty->isFunctionPointerType() ? 0 : 1;
2159 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002160 return;
2161 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002162 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002163 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002164 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002165 return;
2166 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002167 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002168 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002169 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002170 return;
2171 }
Michael Han99315932013-01-24 16:46:58 +00002172 D->addAttr(::new (S.Context)
2173 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2174 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002175}
2176
Chandler Carruthedc2c642011-07-02 00:01:44 +00002177static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002178 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002179 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002180 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002181 return;
2182 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002183
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002184 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2185 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2186 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002187 return;
2188 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002189 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2190 if (MD->getResultType()->isVoidType()) {
2191 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2192 << Attr.getName() << 1;
2193 return;
2194 }
2195
Michael Han99315932013-01-24 16:46:58 +00002196 D->addAttr(::new (S.Context)
2197 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2198 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002199}
2200
Chandler Carruthedc2c642011-07-02 00:01:44 +00002201static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002202 // weak_import only applies to variable & function declarations.
2203 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002204 if (!D->canBeWeakImported(isDef)) {
2205 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002206 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2207 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002208 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002209 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002210 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002211 // Nothing to warn about here.
2212 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002213 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002214 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002215
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002216 return;
2217 }
2218
Michael Han99315932013-01-24 16:46:58 +00002219 D->addAttr(::new (S.Context)
2220 WeakImportAttr(Attr.getRange(), S.Context,
2221 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002222}
2223
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002224// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002225template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002226static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002227 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002228 uint32_t WGSize[3];
2229 for (unsigned i = 0; i < 3; ++i)
2230 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002231 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002232
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002233 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2234 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2235 Existing->getYDim() == WGSize[1] &&
2236 Existing->getZDim() == WGSize[2]))
2237 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002238
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002239 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2240 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002241 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002242}
2243
Joey Goulyaba589c2013-03-08 09:42:32 +00002244static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002245 if (!Attr.hasParsedType()) {
2246 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2247 << Attr.getName() << 1;
2248 return;
2249 }
2250
Richard Smithb87c4652013-10-31 21:23:20 +00002251 TypeSourceInfo *ParmTSI = 0;
2252 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2253 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002254
2255 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2256 (ParmType->isBooleanType() ||
2257 !ParmType->isIntegralType(S.getASTContext()))) {
2258 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2259 << ParmType;
2260 return;
2261 }
2262
Aaron Ballmana9e05402013-12-02 22:16:55 +00002263 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002264 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002265 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2266 return;
2267 }
2268 }
2269
2270 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithb87c4652013-10-31 21:23:20 +00002271 ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002272}
2273
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002274SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002275 StringRef Name,
2276 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002277 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2278 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002279 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002280 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2281 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002282 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002283 }
Michael Han99315932013-01-24 16:46:58 +00002284 return ::new (Context) SectionAttr(Range, Context, Name,
2285 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002286}
2287
Chandler Carruthedc2c642011-07-02 00:01:44 +00002288static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002289 // Make sure that there is a string literal as the sections's single
2290 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002291 StringRef Str;
2292 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002293 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002294 return;
Mike Stump11289f42009-09-09 15:08:12 +00002295
Chris Lattner30ba6742009-08-10 19:03:04 +00002296 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002297 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002298 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002299 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002300 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002301 return;
2302 }
Mike Stump11289f42009-09-09 15:08:12 +00002303
Michael Han99315932013-01-24 16:46:58 +00002304 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002305 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002306 if (NewAttr)
2307 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002308}
2309
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002310
Chandler Carruthedc2c642011-07-02 00:01:44 +00002311static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002312 VarDecl *VD = cast<VarDecl>(D);
2313 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002314 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002315 return;
2316 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002317
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002318 Expr *E = Attr.getArgAsExpr(0);
2319 SourceLocation Loc = E->getExprLoc();
2320 FunctionDecl *FD = 0;
2321 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002322
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002323 // gcc only allows for simple identifiers. Since we support more than gcc, we
2324 // will warn the user.
2325 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2326 if (DRE->hasQualifier())
2327 S.Diag(Loc, diag::warn_cleanup_ext);
2328 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2329 NI = DRE->getNameInfo();
2330 if (!FD) {
2331 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2332 << NI.getName();
2333 return;
2334 }
2335 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2336 if (ULE->hasExplicitTemplateArgs())
2337 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002338 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2339 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002340 if (!FD) {
2341 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2342 << NI.getName();
2343 if (ULE->getType() == S.Context.OverloadTy)
2344 S.NoteAllOverloadCandidates(ULE);
2345 return;
2346 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002347 } else {
2348 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002349 return;
2350 }
2351
Anders Carlssond277d792009-01-31 01:16:18 +00002352 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002353 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2354 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002355 return;
2356 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002357
Anders Carlsson723f55d2009-02-07 23:16:50 +00002358 // We're currently more strict than GCC about what function types we accept.
2359 // If this ever proves to be a problem it should be easy to fix.
2360 QualType Ty = S.Context.getPointerType(VD->getType());
2361 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002362 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2363 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002364 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2365 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002366 return;
2367 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002368
Michael Han99315932013-01-24 16:46:58 +00002369 D->addAttr(::new (S.Context)
2370 CleanupAttr(Attr.getRange(), S.Context, FD,
2371 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002372}
2373
Mike Stumpd3bb5572009-07-24 19:02:52 +00002374/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002375/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002376static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002377 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002378 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002379 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002380 return;
2381 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002382
Aaron Ballman00e99962013-08-31 01:11:41 +00002383 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002384 uint64_t ArgIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002385 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002386 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002387
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002388 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002389 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002390
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002391 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2392 if (not_nsstring_type &&
2393 !isCFStringType(Ty, S.Context) &&
2394 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002395 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002396 // FIXME: Should highlight the actual expression that has the wrong type.
2397 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002398 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002399 << IdxExpr->getSourceRange();
2400 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002401 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002402 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002403 if (!isNSStringType(Ty, S.Context) &&
2404 !isCFStringType(Ty, S.Context) &&
2405 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002406 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002407 // FIXME: Should highlight the actual expression that has the wrong type.
2408 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002409 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002410 << IdxExpr->getSourceRange();
2411 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002412 }
2413
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002414 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2415 // because that has corrected for the implicit this parameter, and is zero-
2416 // based. The attribute expects what the user wrote explicitly.
2417 llvm::APSInt Val;
2418 IdxExpr->EvaluateAsInt(Val, S.Context);
2419
Michael Han99315932013-01-24 16:46:58 +00002420 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002421 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002422 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002423}
2424
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002425enum FormatAttrKind {
2426 CFStringFormat,
2427 NSStringFormat,
2428 StrftimeFormat,
2429 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002430 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002431 InvalidFormat
2432};
2433
2434/// getFormatAttrKind - Map from format attribute names to supported format
2435/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002436static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002437 return llvm::StringSwitch<FormatAttrKind>(Format)
2438 // Check for formats that get handled specially.
2439 .Case("NSString", NSStringFormat)
2440 .Case("CFString", CFStringFormat)
2441 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002442
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002443 // Otherwise, check for supported formats.
2444 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2445 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2446 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002447
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002448 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2449 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002450}
2451
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002452/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002453/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002454static void handleInitPriorityAttr(Sema &S, Decl *D,
2455 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002456 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002457 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2458 return;
2459 }
2460
Aaron Ballman4a611152013-11-27 16:34:09 +00002461 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002462 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2463 Attr.setInvalid();
2464 return;
2465 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002466 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002467 if (S.Context.getAsArrayType(T))
2468 T = S.Context.getBaseElementType(T);
2469 if (!T->getAs<RecordType>()) {
2470 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2471 Attr.setInvalid();
2472 return;
2473 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002474
2475 Expr *E = Attr.getArgAsExpr(0);
2476 uint32_t prioritynum;
2477 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002478 Attr.setInvalid();
2479 return;
2480 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002481
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002482 if (prioritynum < 101 || prioritynum > 65535) {
2483 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002484 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002485 Attr.setInvalid();
2486 return;
2487 }
Michael Han99315932013-01-24 16:46:58 +00002488 D->addAttr(::new (S.Context)
2489 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2490 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002491}
2492
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002493FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2494 IdentifierInfo *Format, int FormatIdx,
2495 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002496 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002497 // Check whether we already have an equivalent format attribute.
2498 for (specific_attr_iterator<FormatAttr>
2499 i = D->specific_attr_begin<FormatAttr>(),
2500 e = D->specific_attr_end<FormatAttr>();
2501 i != e ; ++i) {
2502 FormatAttr *f = *i;
2503 if (f->getType() == Format &&
2504 f->getFormatIdx() == FormatIdx &&
2505 f->getFirstArg() == FirstArg) {
2506 // If we don't have a valid location for this attribute, adopt the
2507 // location.
2508 if (f->getLocation().isInvalid())
2509 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002510 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002511 }
2512 }
2513
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002514 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2515 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002516}
2517
Mike Stumpd3bb5572009-07-24 19:02:52 +00002518/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002519/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002520static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002521 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002522 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002523 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002524 return;
2525 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002526
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002527 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002528 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002529 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002530 return;
2531 }
2532
Chandler Carruth743682b2010-11-16 08:35:43 +00002533 // In C++ the implicit 'this' function parameter also counts, and they are
2534 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002535 bool HasImplicitThisParam = isInstanceMethod(D);
2536 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002537
Aaron Ballman00e99962013-08-31 01:11:41 +00002538 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2539 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002540
2541 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002542 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002543 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002544 // If we've modified the string name, we need a new identifier for it.
2545 II = &S.Context.Idents.get(Format);
2546 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002547
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002548 // Check for supported formats.
2549 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002550
2551 if (Kind == IgnoredFormat)
2552 return;
2553
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002554 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002555 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002556 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002557 return;
2558 }
2559
2560 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002561 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002562 uint32_t Idx;
2563 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002564 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002565
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002566 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002567 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002568 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002569 return;
2570 }
2571
2572 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002573 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002574
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002575 if (HasImplicitThisParam) {
2576 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002577 S.Diag(Attr.getLoc(),
2578 diag::err_format_attribute_implicit_this_format_string)
2579 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002580 return;
2581 }
2582 ArgIdx--;
2583 }
Mike Stump11289f42009-09-09 15:08:12 +00002584
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002585 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002586 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002587
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002588 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002589 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002590 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2591 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002592 return;
2593 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002594 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002595 // FIXME: do we need to check if the type is NSString*? What are the
2596 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002597 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002598 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002599 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2600 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002601 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002602 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002603 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002604 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002605 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002606 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2607 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002608 return;
2609 }
2610
2611 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002612 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002613 uint32_t FirstArg;
2614 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002615 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002616
2617 // check if the function is variadic if the 3rd argument non-zero
2618 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002619 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002620 ++NumArgs; // +1 for ...
2621 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002622 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002623 return;
2624 }
2625 }
2626
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002627 // strftime requires FirstArg to be 0 because it doesn't read from any
2628 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002629 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002630 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002631 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2632 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002633 return;
2634 }
2635 // if 0 it disables parameter checking (to use with e.g. va_list)
2636 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002637 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002638 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002639 return;
2640 }
2641
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002642 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002643 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002644 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002645 if (NewAttr)
2646 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002647}
2648
Chandler Carruthedc2c642011-07-02 00:01:44 +00002649static void handleTransparentUnionAttr(Sema &S, Decl *D,
2650 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002651 // Try to find the underlying union declaration.
2652 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002653 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002654 if (TD && TD->getUnderlyingType()->isUnionType())
2655 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2656 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002657 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002658
2659 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002660 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002661 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002662 return;
2663 }
2664
John McCallf937c022011-10-07 06:10:15 +00002665 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002666 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002667 diag::warn_transparent_union_attribute_not_definition);
2668 return;
2669 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002670
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002671 RecordDecl::field_iterator Field = RD->field_begin(),
2672 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002673 if (Field == FieldEnd) {
2674 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2675 return;
2676 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002677
David Blaikie40ed2972012-06-06 20:45:41 +00002678 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002679 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002680 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002681 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002682 diag::warn_transparent_union_attribute_floating)
2683 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002684 return;
2685 }
2686
2687 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2688 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2689 for (; Field != FieldEnd; ++Field) {
2690 QualType FieldType = Field->getType();
2691 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2692 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2693 // Warn if we drop the attribute.
2694 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002695 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002696 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002697 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002698 diag::warn_transparent_union_attribute_field_size_align)
2699 << isSize << Field->getDeclName() << FieldBits;
2700 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002701 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002702 diag::note_transparent_union_first_field_size_align)
2703 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002704 return;
2705 }
2706 }
2707
Michael Han99315932013-01-24 16:46:58 +00002708 RD->addAttr(::new (S.Context)
2709 TransparentUnionAttr(Attr.getRange(), S.Context,
2710 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002711}
2712
Chandler Carruthedc2c642011-07-02 00:01:44 +00002713static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002714 // Make sure that there is a string literal as the annotation's single
2715 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002716 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002717 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002718 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002719
2720 // Don't duplicate annotations that are already set.
2721 for (specific_attr_iterator<AnnotateAttr>
2722 i = D->specific_attr_begin<AnnotateAttr>(),
2723 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002724 if ((*i)->getAnnotation() == Str)
2725 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002726 }
Michael Han99315932013-01-24 16:46:58 +00002727
2728 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002729 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002730 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002731}
2732
Chandler Carruthedc2c642011-07-02 00:01:44 +00002733static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002734 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002735 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002736 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2737 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002738 return;
2739 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002740
Richard Smith848e1f12013-02-01 08:12:08 +00002741 if (Attr.getNumArgs() == 0) {
2742 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2743 true, 0, Attr.getAttributeSpellingListIndex()));
2744 return;
2745 }
2746
Aaron Ballman00e99962013-08-31 01:11:41 +00002747 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002748 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2749 S.Diag(Attr.getEllipsisLoc(),
2750 diag::err_pack_expansion_without_parameter_packs);
2751 return;
2752 }
2753
2754 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2755 return;
2756
2757 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2758 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002759}
2760
2761void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002762 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002763 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2764 SourceLocation AttrLoc = AttrRange.getBegin();
2765
Richard Smith1dba27c2013-01-29 09:02:09 +00002766 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002767 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002768 // C++11 [dcl.align]p1:
2769 // An alignment-specifier may be applied to a variable or to a class
2770 // data member, but it shall not be applied to a bit-field, a function
2771 // parameter, the formal parameter of a catch clause, or a variable
2772 // declared with the register storage class specifier. An
2773 // alignment-specifier may also be applied to the declaration of a class
2774 // or enumeration type.
2775 // C11 6.7.5/2:
2776 // An alignment attribute shall not be specified in a declaration of
2777 // a typedef, or a bit-field, or a function, or a parameter, or an
2778 // object declared with the register storage-class specifier.
2779 int DiagKind = -1;
2780 if (isa<ParmVarDecl>(D)) {
2781 DiagKind = 0;
2782 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2783 if (VD->getStorageClass() == SC_Register)
2784 DiagKind = 1;
2785 if (VD->isExceptionVariable())
2786 DiagKind = 2;
2787 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2788 if (FD->isBitField())
2789 DiagKind = 3;
2790 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00002791 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00002792 << (TmpAttr.isC11() ? ExpectedVariableOrField
2793 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002794 return;
2795 }
2796 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002797 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00002798 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002799 return;
2800 }
2801 }
2802
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002803 if (E->isTypeDependent() || E->isValueDependent()) {
2804 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002805 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2806 AA->setPackExpansion(IsPackExpansion);
2807 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002808 return;
2809 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002810
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002811 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002812 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002813 ExprResult ICE
2814 = VerifyIntegerConstantExpression(E, &Alignment,
2815 diag::err_aligned_attribute_argument_not_int,
2816 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002817 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002818 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002819
2820 // C++11 [dcl.align]p2:
2821 // -- if the constant expression evaluates to zero, the alignment
2822 // specifier shall have no effect
2823 // C11 6.7.5p6:
2824 // An alignment specification of zero has no effect.
2825 if (!(TmpAttr.isAlignas() && !Alignment) &&
2826 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002827 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2828 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002829 return;
2830 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002831
Richard Smith848e1f12013-02-01 08:12:08 +00002832 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00002833 // We've already verified it's a power of 2, now let's make sure it's
2834 // 8192 or less.
2835 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00002836 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00002837 << E->getSourceRange();
2838 return;
2839 }
2840 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002841
Richard Smith44c247f2013-02-22 08:32:16 +00002842 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2843 ICE.take(), SpellingListIndex);
2844 AA->setPackExpansion(IsPackExpansion);
2845 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002846}
2847
Michael Hanaf02bbe2013-02-01 01:19:17 +00002848void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002849 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002850 // FIXME: Cache the number on the Attr object if non-dependent?
2851 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002852 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2853 SpellingListIndex);
2854 AA->setPackExpansion(IsPackExpansion);
2855 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002856}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002857
Richard Smith848e1f12013-02-01 08:12:08 +00002858void Sema::CheckAlignasUnderalignment(Decl *D) {
2859 assert(D->hasAttrs() && "no attributes on decl");
2860
2861 QualType Ty;
2862 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2863 Ty = VD->getType();
2864 else
2865 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002866 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002867 return;
2868
2869 // C++11 [dcl.align]p5, C11 6.7.5/4:
2870 // The combined effect of all alignment attributes in a declaration shall
2871 // not specify an alignment that is less strict than the alignment that
2872 // would otherwise be required for the entity being declared.
2873 AlignedAttr *AlignasAttr = 0;
2874 unsigned Align = 0;
2875 for (specific_attr_iterator<AlignedAttr>
2876 I = D->specific_attr_begin<AlignedAttr>(),
2877 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
2878 if (I->isAlignmentDependent())
2879 return;
2880 if (I->isAlignas())
2881 AlignasAttr = *I;
2882 Align = std::max(Align, I->getAlignment(Context));
2883 }
2884
2885 if (AlignasAttr && Align) {
2886 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2887 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2888 if (NaturalAlign > RequestedAlign)
2889 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2890 << Ty << (unsigned)NaturalAlign.getQuantity();
2891 }
2892}
2893
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002894/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002895/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002896///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002897/// Despite what would be logical, the mode attribute is a decl attribute, not a
2898/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2899/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002900static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002901 // This attribute isn't documented, but glibc uses it. It changes
2902 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002903 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002904 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2905 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002906 return;
2907 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002908
Aaron Ballman00e99962013-08-31 01:11:41 +00002909 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2910 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002911
2912 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002913 if (Str.startswith("__") && Str.endswith("__"))
2914 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002915
2916 unsigned DestWidth = 0;
2917 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002918 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002919 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002920 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002921 switch (Str[0]) {
2922 case 'Q': DestWidth = 8; break;
2923 case 'H': DestWidth = 16; break;
2924 case 'S': DestWidth = 32; break;
2925 case 'D': DestWidth = 64; break;
2926 case 'X': DestWidth = 96; break;
2927 case 'T': DestWidth = 128; break;
2928 }
2929 if (Str[1] == 'F') {
2930 IntegerMode = false;
2931 } else if (Str[1] == 'C') {
2932 IntegerMode = false;
2933 ComplexMode = true;
2934 } else if (Str[1] != 'I') {
2935 DestWidth = 0;
2936 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002937 break;
2938 case 4:
2939 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2940 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002941 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002942 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002943 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002944 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002945 break;
2946 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002947 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002948 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002949 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002950 case 11:
2951 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00002952 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002953 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002954 }
2955
2956 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002957 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002958 OldTy = TD->getUnderlyingType();
2959 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2960 OldTy = VD->getType();
2961 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002962 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00002963 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002964 return;
2965 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002966
John McCall9dd450b2009-09-21 23:43:11 +00002967 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002968 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2969 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002970 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002971 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2972 } else if (ComplexMode) {
2973 if (!OldTy->isComplexType())
2974 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2975 } else {
2976 if (!OldTy->isFloatingType())
2977 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2978 }
2979
Mike Stump87c57ac2009-05-16 07:39:55 +00002980 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2981 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002982 // FIXME: Make sure floating-point mappings are accurate
2983 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002984 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00002985 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002986 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002987 }
2988
2989 QualType NewTy;
2990
2991 if (IntegerMode)
2992 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
2993 OldTy->isSignedIntegerType());
2994 else
2995 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
2996
2997 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00002998 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002999 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003000 }
3001
Eli Friedman4735374e2009-03-03 06:41:03 +00003002 if (ComplexMode) {
3003 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003004 }
3005
3006 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003007 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3008 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3009 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003010 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003011
3012 D->addAttr(::new (S.Context)
3013 ModeAttr(Attr.getRange(), S.Context, Name,
3014 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003015}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003016
Chandler Carruthedc2c642011-07-02 00:01:44 +00003017static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003018 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3019 if (!VD->hasGlobalStorage())
3020 S.Diag(Attr.getLoc(),
3021 diag::warn_attribute_requires_functions_or_static_globals)
3022 << Attr.getName();
3023 } else if (!isFunctionOrMethod(D)) {
3024 S.Diag(Attr.getLoc(),
3025 diag::warn_attribute_requires_functions_or_static_globals)
3026 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003027 return;
3028 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003029
Michael Han99315932013-01-24 16:46:58 +00003030 D->addAttr(::new (S.Context)
3031 NoDebugAttr(Attr.getRange(), S.Context,
3032 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003033}
3034
Chandler Carruthedc2c642011-07-02 00:01:44 +00003035static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003036 FunctionDecl *FD = cast<FunctionDecl>(D);
3037 if (!FD->getResultType()->isVoidType()) {
3038 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
3039 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
3040 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3041 << FD->getType()
3042 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
3043 "void");
3044 } else {
3045 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3046 << FD->getType();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003047 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003048 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003049 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003050
Aaron Ballman3aff6332013-12-02 19:30:36 +00003051 D->addAttr(::new (S.Context)
3052 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003053 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003054}
3055
Chandler Carruthedc2c642011-07-02 00:01:44 +00003056static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003057 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003058 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003059 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003060 return;
3061 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003062
Michael Han99315932013-01-24 16:46:58 +00003063 D->addAttr(::new (S.Context)
3064 GNUInlineAttr(Attr.getRange(), S.Context,
3065 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003066}
3067
Chandler Carruthedc2c642011-07-02 00:01:44 +00003068static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003069 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003070
Aaron Ballman02df2e02012-12-09 17:45:41 +00003071 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003072 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003073 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3074 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003075 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003076 return;
3077
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003078 if (!isa<ObjCMethodDecl>(D)) {
3079 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3080 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003081 return;
3082 }
3083
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003084 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003085 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003086 D->addAttr(::new (S.Context)
3087 FastCallAttr(Attr.getRange(), S.Context,
3088 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003089 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003090 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003091 D->addAttr(::new (S.Context)
3092 StdCallAttr(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_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003096 D->addAttr(::new (S.Context)
3097 ThisCallAttr(Attr.getRange(), S.Context,
3098 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003099 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003100 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003101 D->addAttr(::new (S.Context)
3102 CDeclAttr(Attr.getRange(), S.Context,
3103 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003104 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003105 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003106 D->addAttr(::new (S.Context)
3107 PascalAttr(Attr.getRange(), S.Context,
3108 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003109 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003110 case AttributeList::AT_MSABI:
3111 D->addAttr(::new (S.Context)
3112 MSABIAttr(Attr.getRange(), S.Context,
3113 Attr.getAttributeSpellingListIndex()));
3114 return;
3115 case AttributeList::AT_SysVABI:
3116 D->addAttr(::new (S.Context)
3117 SysVABIAttr(Attr.getRange(), S.Context,
3118 Attr.getAttributeSpellingListIndex()));
3119 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003120 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003121 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003122 switch (CC) {
3123 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003124 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003125 break;
3126 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003127 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003128 break;
3129 default:
3130 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003131 }
3132
Michael Han99315932013-01-24 16:46:58 +00003133 D->addAttr(::new (S.Context)
3134 PcsAttr(Attr.getRange(), S.Context, PCS,
3135 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003136 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003137 }
Derek Schuffa2020962012-10-16 22:30:41 +00003138 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003139 D->addAttr(::new (S.Context)
3140 PnaclCallAttr(Attr.getRange(), S.Context,
3141 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003142 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003143 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003144 D->addAttr(::new (S.Context)
3145 IntelOclBiccAttr(Attr.getRange(), S.Context,
3146 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003147 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003148
Abramo Bagnara50099372010-04-30 13:10:51 +00003149 default:
3150 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003151 }
3152}
3153
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003154static void handleOpenCLImageAccessAttr(Sema &S, Decl *D,
3155 const AttributeList &Attr) {
3156 uint32_t ArgNum;
3157 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum))
Guy Benyeifb36ede2013-03-24 13:58:12 +00003158 return;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003159
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003160 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(),
3161 S.Context, ArgNum));
Guy Benyeifb36ede2013-03-24 13:58:12 +00003162}
3163
Aaron Ballman02df2e02012-12-09 17:45:41 +00003164bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3165 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003166 if (attr.isInvalid())
3167 return true;
3168
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003169 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003170 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003171 attr.setInvalid();
3172 return true;
3173 }
3174
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003175 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003176 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003177 case AttributeList::AT_CDecl: CC = CC_C; break;
3178 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3179 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3180 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3181 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003182 case AttributeList::AT_MSABI:
3183 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3184 CC_X86_64Win64;
3185 break;
3186 case AttributeList::AT_SysVABI:
3187 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3188 CC_C;
3189 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003190 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003191 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003192 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003193 attr.setInvalid();
3194 return true;
3195 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003196 if (StrRef == "aapcs") {
3197 CC = CC_AAPCS;
3198 break;
3199 } else if (StrRef == "aapcs-vfp") {
3200 CC = CC_AAPCS_VFP;
3201 break;
3202 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003203
3204 attr.setInvalid();
3205 Diag(attr.getLoc(), diag::err_invalid_pcs);
3206 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003207 }
Derek Schuffa2020962012-10-16 22:30:41 +00003208 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003209 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003210 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003211 }
3212
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003213 const TargetInfo &TI = Context.getTargetInfo();
3214 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3215 if (A == TargetInfo::CCCR_Warning) {
3216 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003217
3218 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3219 if (FD)
3220 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3221 TargetInfo::CCMT_NonMember;
3222 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003223 }
3224
John McCall3882ace2011-01-05 12:14:39 +00003225 return false;
3226}
3227
John McCall3882ace2011-01-05 12:14:39 +00003228/// Checks a regparm attribute, returning true if it is ill-formed and
3229/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003230bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3231 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003232 return true;
3233
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003234 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003235 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003236 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003237 }
Eli Friedman7044b762009-03-27 21:06:47 +00003238
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003239 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003240 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003241 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003242 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003243 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003244 }
3245
Douglas Gregore8bbc122011-09-02 00:18:52 +00003246 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003247 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003248 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003249 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003250 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003251 }
3252
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003253 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003254 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003255 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003256 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003257 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003258 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003259 }
3260
John McCall3882ace2011-01-05 12:14:39 +00003261 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003262}
3263
Aaron Ballman66039932013-12-19 00:41:31 +00003264static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3265 const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003266 // check the attribute arguments.
3267 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3268 // FIXME: 0 is not okay.
Aaron Ballman05e420a2014-01-02 21:26:14 +00003269 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3270 << Attr.getName() << 2;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003271 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003272 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003273
3274 if (!isFunctionOrMethod(D)) {
3275 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3276 << Attr.getName() << ExpectedFunctionOrMethod;
3277 return;
3278 }
3279
Aaron Ballman66039932013-12-19 00:41:31 +00003280 uint32_t MaxThreads, MinBlocks = 0;
3281 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3282 return;
3283 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3284 Attr.getArgAsExpr(1),
3285 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003286 return;
3287
3288 D->addAttr(::new (S.Context)
3289 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3290 MaxThreads, MinBlocks,
3291 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003292}
3293
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003294static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3295 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003296 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003297 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003298 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003299 return;
3300 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003301
3302 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003303 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003304
Aaron Ballman00e99962013-08-31 01:11:41 +00003305 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003306
3307 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3308 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3309 << Attr.getName() << ExpectedFunctionOrMethod;
3310 return;
3311 }
3312
3313 uint64_t ArgumentIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003314 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3315 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003316 return;
3317
3318 uint64_t TypeTagIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003319 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3320 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003321 return;
3322
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003323 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003324 if (IsPointer) {
3325 // Ensure that buffer has a pointer type.
3326 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3327 if (!BufferTy->isPointerType()) {
3328 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003329 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003330 }
3331 }
3332
Michael Han99315932013-01-24 16:46:58 +00003333 D->addAttr(::new (S.Context)
3334 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3335 ArgumentIdx, TypeTagIdx, IsPointer,
3336 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003337}
3338
3339static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3340 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003341 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003342 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003343 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003344 return;
3345 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003346
3347 if (!checkAttributeNumArgs(S, Attr, 1))
3348 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003349
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003350 if (!isa<VarDecl>(D)) {
3351 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3352 << Attr.getName() << ExpectedVariable;
3353 return;
3354 }
3355
Aaron Ballman00e99962013-08-31 01:11:41 +00003356 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003357 TypeSourceInfo *MatchingCTypeLoc = 0;
3358 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3359 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003360
Michael Han99315932013-01-24 16:46:58 +00003361 D->addAttr(::new (S.Context)
3362 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003363 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003364 Attr.getLayoutCompatible(),
3365 Attr.getMustBeNull(),
3366 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003367}
3368
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003369//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003370// Checker-specific attribute handlers.
3371//===----------------------------------------------------------------------===//
3372
John McCalled433932011-01-25 03:31:58 +00003373static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003374 return type->isDependentType() ||
3375 type->isObjCObjectPointerType() ||
3376 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003377}
3378static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003379 return type->isDependentType() ||
3380 type->isPointerType() ||
3381 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003382}
3383
Chandler Carruthedc2c642011-07-02 00:01:44 +00003384static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003385 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003386 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003387
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003388 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003389 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3390 cf = false;
3391 } else {
3392 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3393 cf = true;
3394 }
3395
3396 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003397 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003398 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003399 return;
3400 }
3401
3402 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003403 param->addAttr(::new (S.Context)
3404 CFConsumedAttr(Attr.getRange(), S.Context,
3405 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003406 else
Michael Han99315932013-01-24 16:46:58 +00003407 param->addAttr(::new (S.Context)
3408 NSConsumedAttr(Attr.getRange(), S.Context,
3409 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003410}
3411
Chandler Carruthedc2c642011-07-02 00:01:44 +00003412static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3413 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003414
John McCalled433932011-01-25 03:31:58 +00003415 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003416
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003417 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003418 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003419 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003420 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003421 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003422 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3423 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003424 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003425 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003426 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003427 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003428 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003429 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003430 return;
3431 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003432
John McCalled433932011-01-25 03:31:58 +00003433 bool typeOK;
3434 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003435 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003436 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003437 case AttributeList::AT_NSReturnsAutoreleased:
3438 case AttributeList::AT_NSReturnsRetained:
3439 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003440 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3441 cf = false;
3442 break;
3443
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003444 case AttributeList::AT_CFReturnsRetained:
3445 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003446 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3447 cf = true;
3448 break;
3449 }
3450
3451 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003452 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003453 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003454 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003455 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003456
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003457 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003458 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003459 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003460 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003461 D->addAttr(::new (S.Context)
3462 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3463 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003464 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003465 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003466 D->addAttr(::new (S.Context)
3467 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3468 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003469 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003470 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003471 D->addAttr(::new (S.Context)
3472 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3473 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003474 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003475 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003476 D->addAttr(::new (S.Context)
3477 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3478 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003479 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003480 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003481 D->addAttr(::new (S.Context)
3482 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3483 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003484 return;
3485 };
3486}
3487
John McCallcf166702011-07-22 08:53:00 +00003488static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3489 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003490 const int EP_ObjCMethod = 1;
3491 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003492
John McCallcf166702011-07-22 08:53:00 +00003493 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003494 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003495 if (isa<ObjCMethodDecl>(D))
3496 resultType = cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003497 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003498 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003499
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003500 if (!resultType->isReferenceType() &&
3501 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003502 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003503 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003504 << attr.getName()
3505 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003506 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003507
3508 // Drop the attribute.
3509 return;
3510 }
3511
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003512 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003513 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3514 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003515}
3516
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003517static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3518 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003519 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003520
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003521 DeclContext *DC = method->getDeclContext();
3522 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3523 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3524 << attr.getName() << 0;
3525 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3526 return;
3527 }
3528 if (method->getMethodFamily() == OMF_dealloc) {
3529 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3530 << attr.getName() << 1;
3531 return;
3532 }
3533
Michael Han99315932013-01-24 16:46:58 +00003534 method->addAttr(::new (S.Context)
3535 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3536 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003537}
3538
Aaron Ballmanfb763042013-12-02 18:05:46 +00003539static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3540 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003541 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00003542 return;
John McCall32f5fe12011-09-30 05:12:12 +00003543
Aaron Ballmanfb763042013-12-02 18:05:46 +00003544 D->addAttr(::new (S.Context)
3545 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3546 Attr.getAttributeSpellingListIndex()));
3547}
3548
3549static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3550 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003551 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00003552 return;
3553
3554 D->addAttr(::new (S.Context)
3555 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3556 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003557}
3558
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003559static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3560 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003561 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003562
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003563 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003564 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003565 return;
3566 }
3567
3568 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003569 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003570 Attr.getAttributeSpellingListIndex()));
3571}
3572
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003573static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3574 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003575 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003576
3577 if (!Parm) {
3578 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3579 return;
3580 }
3581
3582 D->addAttr(::new (S.Context)
3583 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3584 Attr.getAttributeSpellingListIndex()));
3585}
3586
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003587static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3588 const AttributeList &Attr) {
3589 IdentifierInfo *RelatedClass =
3590 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0;
3591 if (!RelatedClass) {
3592 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3593 return;
3594 }
3595 IdentifierInfo *ClassMethod =
3596 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0;
3597 IdentifierInfo *InstanceMethod =
3598 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0;
3599 D->addAttr(::new (S.Context)
3600 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3601 ClassMethod, InstanceMethod,
3602 Attr.getAttributeSpellingListIndex()));
3603}
3604
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003605static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3606 const AttributeList &Attr) {
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003607 ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003608 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003609 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003610 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3611 Attr.getAttributeSpellingListIndex()));
3612}
3613
Chandler Carruthedc2c642011-07-02 00:01:44 +00003614static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3615 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003616 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003617
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003618 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003619 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003620}
3621
Chandler Carruthedc2c642011-07-02 00:01:44 +00003622static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3623 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003624 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003625 QualType type = vd->getType();
3626
3627 if (!type->isDependentType() &&
3628 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003629 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003630 << type;
3631 return;
3632 }
3633
3634 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3635
3636 // If we have no lifetime yet, check the lifetime we're presumably
3637 // going to infer.
3638 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3639 lifetime = type->getObjCARCImplicitLifetime();
3640
3641 switch (lifetime) {
3642 case Qualifiers::OCL_None:
3643 assert(type->isDependentType() &&
3644 "didn't infer lifetime for non-dependent type?");
3645 break;
3646
3647 case Qualifiers::OCL_Weak: // meaningful
3648 case Qualifiers::OCL_Strong: // meaningful
3649 break;
3650
3651 case Qualifiers::OCL_ExplicitNone:
3652 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003653 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003654 << (lifetime == Qualifiers::OCL_Autoreleasing);
3655 break;
3656 }
3657
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003658 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003659 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3660 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003661}
3662
Francois Picheta83957a2010-12-19 06:50:37 +00003663//===----------------------------------------------------------------------===//
3664// Microsoft specific attribute handlers.
3665//===----------------------------------------------------------------------===//
3666
Chandler Carruthedc2c642011-07-02 00:01:44 +00003667static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003668 if (!S.LangOpts.CPlusPlus) {
3669 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3670 << Attr.getName() << AttributeLangSupport::C;
3671 return;
3672 }
3673
Aaron Ballman60e705e2013-11-24 20:58:02 +00003674 if (!isa<CXXRecordDecl>(D)) {
3675 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3676 << Attr.getName() << ExpectedClass;
3677 return;
3678 }
3679
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003680 StringRef StrRef;
3681 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003682 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003683 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003684
David Majnemer89085342013-08-09 08:56:20 +00003685 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3686 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003687 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3688 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003689
Reid Kleckner140c4a72013-05-17 14:04:52 +00003690 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003691 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003692 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003693 return;
3694 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003695
David Majnemer89085342013-08-09 08:56:20 +00003696 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003697 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003698 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003699 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003700 return;
3701 }
David Majnemer89085342013-08-09 08:56:20 +00003702 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003703 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003704 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003705 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003706 }
Francois Picheta83957a2010-12-19 06:50:37 +00003707
David Majnemer89085342013-08-09 08:56:20 +00003708 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3709 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003710}
3711
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003712static void handleARMInterruptAttr(Sema &S, Decl *D,
3713 const AttributeList &Attr) {
3714 // Check the attribute arguments.
3715 if (Attr.getNumArgs() > 1) {
3716 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3717 << Attr.getName() << 1;
3718 return;
3719 }
3720
3721 StringRef Str;
3722 SourceLocation ArgLoc;
3723
3724 if (Attr.getNumArgs() == 0)
3725 Str = "";
3726 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
3727 return;
3728
3729 ARMInterruptAttr::InterruptType Kind;
3730 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
3731 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
3732 << Attr.getName() << Str << ArgLoc;
3733 return;
3734 }
3735
3736 unsigned Index = Attr.getAttributeSpellingListIndex();
3737 D->addAttr(::new (S.Context)
3738 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
3739}
3740
3741static void handleMSP430InterruptAttr(Sema &S, Decl *D,
3742 const AttributeList &Attr) {
3743 if (!checkAttributeNumArgs(S, Attr, 1))
3744 return;
3745
3746 if (!Attr.isArgExpr(0)) {
3747 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3748 << AANT_ArgumentIntegerConstant;
3749 return;
3750 }
3751
3752 // FIXME: Check for decl - it should be void ()(void).
3753
3754 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3755 llvm::APSInt NumParams(32);
3756 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
3757 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3758 << Attr.getName() << AANT_ArgumentIntegerConstant
3759 << NumParamsExpr->getSourceRange();
3760 return;
3761 }
3762
3763 unsigned Num = NumParams.getLimitedValue(255);
3764 if ((Num & 1) || Num > 30) {
3765 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3766 << Attr.getName() << (int)NumParams.getSExtValue()
3767 << NumParamsExpr->getSourceRange();
3768 return;
3769 }
3770
3771 D->addAttr(::new (S.Context) MSP430InterruptAttr(Attr.getLoc(), S.Context, Num));
3772 D->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
3773}
3774
3775static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3776 // Dispatch the interrupt attribute based on the current target.
3777 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
3778 handleMSP430InterruptAttr(S, D, Attr);
3779 else
3780 handleARMInterruptAttr(S, D, Attr);
3781}
3782
3783static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
3784 const AttributeList& Attr) {
3785 // If we try to apply it to a function pointer, don't warn, but don't
3786 // do anything, either. It doesn't matter anyway, because there's nothing
3787 // special about calling a force_align_arg_pointer function.
3788 ValueDecl *VD = dyn_cast<ValueDecl>(D);
3789 if (VD && VD->getType()->isFunctionPointerType())
3790 return;
3791 // Also don't warn on function pointer typedefs.
3792 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
3793 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
3794 TD->getUnderlyingType()->isFunctionType()))
3795 return;
3796 // Attribute can only be applied to function types.
3797 if (!isa<FunctionDecl>(D)) {
3798 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3799 << Attr.getName() << /* function */0;
3800 return;
3801 }
3802
3803 D->addAttr(::new (S.Context) X86ForceAlignArgPointerAttr(Attr.getRange(),
3804 S.Context));
3805}
3806
3807DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
3808 unsigned AttrSpellingListIndex) {
3809 if (D->hasAttr<DLLExportAttr>()) {
3810 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "dllimport";
3811 return NULL;
3812 }
3813
3814 if (D->hasAttr<DLLImportAttr>())
3815 return NULL;
3816
3817 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3818 if (VD->hasDefinition()) {
3819 // dllimport cannot be applied to definitions.
3820 Diag(D->getLocation(), diag::warn_attribute_invalid_on_definition)
3821 << "dllimport";
3822 return NULL;
3823 }
3824 }
3825
3826 return ::new (Context)DLLImportAttr(Range, Context,
3827 AttrSpellingListIndex);
3828}
3829
3830static void handleDLLImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3831 // Attribute can be applied only to functions or variables.
3832 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3833 if (!FD && !isa<VarDecl>(D)) {
3834 // Apparently Visual C++ thinks it is okay to not emit a warning
3835 // in this case, so only emit a warning when -fms-extensions is not
3836 // specified.
3837 if (!S.getLangOpts().MicrosoftExt)
3838 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3839 << Attr.getName() << 2 /*variable and function*/;
3840 return;
3841 }
3842
3843 // Currently, the dllimport attribute is ignored for inlined functions.
3844 // Warning is emitted.
3845 if (FD && FD->isInlineSpecified()) {
3846 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3847 return;
3848 }
3849
3850 unsigned Index = Attr.getAttributeSpellingListIndex();
3851 DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index);
3852 if (NewAttr)
3853 D->addAttr(NewAttr);
3854}
3855
3856DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
3857 unsigned AttrSpellingListIndex) {
3858 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
3859 Diag(Import->getLocation(), diag::warn_attribute_ignored) << "dllimport";
3860 D->dropAttr<DLLImportAttr>();
3861 }
3862
3863 if (D->hasAttr<DLLExportAttr>())
3864 return NULL;
3865
3866 return ::new (Context)DLLExportAttr(Range, Context,
3867 AttrSpellingListIndex);
3868}
3869
3870static void handleDLLExportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3871 // Currently, the dllexport attribute is ignored for inlined functions, unless
3872 // the -fkeep-inline-functions flag has been used. Warning is emitted;
3873 if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isInlineSpecified()) {
3874 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
3875 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3876 return;
3877 }
3878
3879 unsigned Index = Attr.getAttributeSpellingListIndex();
3880 DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index);
3881 if (NewAttr)
3882 D->addAttr(NewAttr);
3883}
3884
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003885/// Handles semantic checking for features that are common to all attributes,
3886/// such as checking whether a parameter was properly specified, or the correct
3887/// number of arguments were passed, etc.
3888static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3889 const AttributeList &Attr) {
3890 // Several attributes carry different semantics than the parsing requires, so
3891 // those are opted out of the common handling.
3892 //
3893 // We also bail on unknown and ignored attributes because those are handled
3894 // as part of the target-specific handling logic.
3895 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003896 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003897 return false;
3898
Aaron Ballman3aff6332013-12-02 19:30:36 +00003899 // Check whether the attribute requires specific language extensions to be
3900 // enabled.
3901 if (!Attr.diagnoseLangOpts(S))
3902 return true;
3903
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003904 // If there are no optional arguments, then checking for the argument count
3905 // is trivial.
3906 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
3907 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
3908 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003909
3910 // Check whether the attribute appertains to the given subject.
3911 if (!Attr.diagnoseAppertainsTo(S, D))
3912 return true;
3913
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003914 return false;
3915}
3916
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003917//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003918// Top Level Sema Entry Points
3919//===----------------------------------------------------------------------===//
3920
Richard Smithf8a75c32013-08-29 00:47:48 +00003921/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3922/// the attribute applies to decls. If the attribute is a type attribute, just
3923/// silently ignore it if a GNU attribute.
3924static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3925 const AttributeList &Attr,
3926 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003927 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00003928 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003929
Richard Smithf8a75c32013-08-29 00:47:48 +00003930 // Ignore C++11 attributes on declarator chunks: they appertain to the type
3931 // instead.
3932 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
3933 return;
3934
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003935 // Unknown attributes are automatically warned on. Target-specific attributes
3936 // which do not apply to the current target architecture are treated as
3937 // though they were unknown attributes.
3938 if (Attr.getKind() == AttributeList::UnknownAttribute ||
3939 !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) {
3940 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
3941 diag::warn_unhandled_ms_attribute_ignored :
3942 diag::warn_unknown_attribute_ignored) << Attr.getName();
3943 return;
3944 }
3945
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003946 if (handleCommonAttributeFeatures(S, scope, D, Attr))
3947 return;
3948
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003949 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003950 default:
3951 // Type attributes are handled elsewhere; silently move on.
3952 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
3953 break;
3954 case AttributeList::AT_Interrupt:
3955 handleInterruptAttr(S, D, Attr); break;
3956 case AttributeList::AT_X86ForceAlignArgPointer:
3957 handleX86ForceAlignArgPointerAttr(S, D, Attr); break;
3958 case AttributeList::AT_DLLExport:
3959 handleDLLExportAttr(S, D, Attr); break;
3960 case AttributeList::AT_DLLImport:
3961 handleDLLImportAttr(S, D, Attr); break;
3962 case AttributeList::AT_Mips16:
3963 handleSimpleAttribute<Mips16Attr>(S, D, Attr); break;
3964 case AttributeList::AT_NoMips16:
3965 handleSimpleAttribute<NoMips16Attr>(S, D, Attr); break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00003966 case AttributeList::AT_IBAction:
3967 handleSimpleAttribute<IBActionAttr>(S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003968 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
3969 case AttributeList::AT_IBOutletCollection:
3970 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003971 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
3972 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003973 case AttributeList::AT_AlwaysInline:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003974 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003975 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003976 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00003977 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003978 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
3979 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
3980 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00003981 handleDependencyAttr(S, scope, D, Attr);
3982 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003983 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003984 case AttributeList::AT_CUDAConstant:
3985 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003986 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003987 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman3a8e2d92013-11-27 18:53:58 +00003988 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003989 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00003990 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00003991 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003992 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
3993 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003994 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003995 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00003996 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003997 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00003998 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003999 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4000 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4001 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00004002 case AttributeList::AT_CUDADevice:
4003 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004004 case AttributeList::AT_CUDAHost:
4005 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004006 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4007 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004008 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004009 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004010 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004011 case AttributeList::AT_MayAlias:
4012 handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004013 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004014 case AttributeList::AT_NoCommon:
4015 handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004016 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004017 case AttributeList::AT_Overloadable:
4018 handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00004019 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004020 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4021 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004022 case AttributeList::AT_Naked:
4023 handleSimpleAttribute<NakedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004024 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004025 case AttributeList::AT_NoThrow:
4026 handleSimpleAttribute<NoThrowAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004027 case AttributeList::AT_CUDAShared:
4028 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004029 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004030
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004031 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004032 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004033 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004034 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004035
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004036 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004037 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4038
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004039 case AttributeList::AT_ObjCRequiresSuper:
4040 handleObjCRequiresSuperAttr(S, D, Attr); break;
4041
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004042 case AttributeList::AT_ObjCBridge:
4043 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004044
4045 case AttributeList::AT_ObjCBridgeMutable:
4046 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004047
4048 case AttributeList::AT_ObjCBridgeRelated:
4049 handleObjCBridgeRelatedAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00004050
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004051 case AttributeList::AT_ObjCDesignatedInitializer:
4052 handleObjCDesignatedInitializer(S, D, Attr); break;
4053
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004054 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00004055 handleCFAuditedTransferAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004056 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00004057 handleCFUnknownTransferAttr(S, D, Attr); break;
John McCall32f5fe12011-09-30 05:12:12 +00004058
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004059 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004060 case AttributeList::AT_CFConsumed:
4061 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4062 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004063 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004064
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004065 case AttributeList::AT_NSReturnsAutoreleased:
4066 case AttributeList::AT_NSReturnsNotRetained:
4067 case AttributeList::AT_CFReturnsNotRetained:
4068 case AttributeList::AT_NSReturnsRetained:
4069 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004070 handleNSReturnsRetainedAttr(S, D, Attr); break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004071 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004072 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004073 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004074 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004075 case AttributeList::AT_VecTypeHint:
4076 handleVecTypeHint(S, D, Attr); break;
4077
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004078 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004079 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004080
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004081 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4082 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4083 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004084 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004085 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004086 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004087 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004088 case AttributeList::AT_ObjCRootClass:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004089 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004090 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek28eace62013-11-23 01:01:34 +00004091 handleObjCSuppresProtocolAttr(S, D, Attr);
4092 break;
4093 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004094 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004095 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4096 case AttributeList::AT_ReturnsTwice:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004097 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004098 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004099 case AttributeList::AT_Visibility:
4100 handleVisibilityAttr(S, D, Attr, false);
4101 break;
4102 case AttributeList::AT_TypeVisibility:
4103 handleVisibilityAttr(S, D, Attr, true);
4104 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004105 case AttributeList::AT_WarnUnused:
Aaron Ballman6a42b5a2013-11-27 16:59:17 +00004106 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004107 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004108 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004109 case AttributeList::AT_Weak:
4110 handleSimpleAttribute<WeakAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004111 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4112 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4113 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004114 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004115 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004116 case AttributeList::AT_ObjCException:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004117 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004118 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004119 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004120 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004121 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4122 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4123 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004124 case AttributeList::AT_Const:
4125 handleSimpleAttribute<ConstAttr>(S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004126 case AttributeList::AT_Pure:
4127 handleSimpleAttribute<PureAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004128 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4129 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004130 case AttributeList::AT_NoInline:
4131 handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004132 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004133 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004134 case AttributeList::AT_StdCall:
4135 case AttributeList::AT_CDecl:
4136 case AttributeList::AT_FastCall:
4137 case AttributeList::AT_ThisCall:
4138 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004139 case AttributeList::AT_MSABI:
4140 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004141 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004142 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004143 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004144 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004145 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004146 case AttributeList::AT_OpenCLKernel:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004147 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004148 case AttributeList::AT_OpenCLImageAccess:
4149 handleOpenCLImageAccessAttr(S, D, Attr);
4150 break;
John McCall8d32c052012-05-22 21:28:12 +00004151
4152 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004153 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004154 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004155 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004156 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004157 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004158 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004159 case AttributeList::AT_MSInheritance:
4160 handleSimpleAttribute<MSInheritanceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004161 case AttributeList::AT_ForceInline:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004162 handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004163 case AttributeList::AT_SelectAny:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004164 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004165
4166 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004167 case AttributeList::AT_AssertExclusiveLock:
4168 handleAssertExclusiveLockAttr(S, D, Attr);
4169 break;
4170 case AttributeList::AT_AssertSharedLock:
4171 handleAssertSharedLockAttr(S, D, Attr);
4172 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004173 case AttributeList::AT_GuardedVar:
Aaron Ballmane61b8b82013-12-02 15:02:49 +00004174 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004175 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004176 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004177 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004178 case AttributeList::AT_ScopedLockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004179 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004180 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004181 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004182 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004183 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004184 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004185 break;
4186 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004187 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004188 break;
4189 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004190 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004191 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004192 case AttributeList::AT_Lockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004193 handleSimpleAttribute<LockableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004194 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004195 handleGuardedByAttr(S, D, Attr);
4196 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004197 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004198 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004199 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004200 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004201 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004202 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004203 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004204 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004205 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004206 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004207 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004208 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004209 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004210 handleLockReturnedAttr(S, D, Attr);
4211 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004212 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004213 handleLocksExcludedAttr(S, D, Attr);
4214 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004215 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004216 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004217 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004218 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004219 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004220 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004221 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004222 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004223 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004224 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004225 handleUnlockFunAttr(S, D, Attr);
4226 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004227 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004228 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004229 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004230 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004231 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004232 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004233
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004234 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004235 case AttributeList::AT_Consumable:
4236 handleConsumableAttr(S, D, Attr);
4237 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004238 case AttributeList::AT_CallableWhen:
4239 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004240 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004241 case AttributeList::AT_ParamTypestate:
4242 handleParamTypestateAttr(S, D, Attr);
4243 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004244 case AttributeList::AT_ReturnTypestate:
4245 handleReturnTypestateAttr(S, D, Attr);
4246 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004247 case AttributeList::AT_SetTypestate:
4248 handleSetTypestateAttr(S, D, Attr);
4249 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004250 case AttributeList::AT_TestTypestate:
4251 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004252 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004253
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004254 // Type safety attributes.
4255 case AttributeList::AT_ArgumentWithTypeTag:
4256 handleArgumentWithTypeTagAttr(S, D, Attr);
4257 break;
4258 case AttributeList::AT_TypeTagForDatatype:
4259 handleTypeTagForDatatypeAttr(S, D, Attr);
4260 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004261 }
4262}
4263
4264/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4265/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004266void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004267 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004268 bool IncludeCXX11Attributes) {
4269 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004270 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004271
Joey Gouly2cd9db12013-12-13 16:15:28 +00004272 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004273 // GCC accepts
4274 // static int a9 __attribute__((weakref));
4275 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004276 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00004277 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
4278 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00004279 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004280 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004281 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004282
4283 if (!D->hasAttr<OpenCLKernelAttr>()) {
4284 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004285 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
4286 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004287 D->setInvalidDecl();
4288 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004289 if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
4290 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004291 D->setInvalidDecl();
4292 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004293 if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
4294 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004295 D->setInvalidDecl();
4296 }
4297 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004298}
4299
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004300// Annotation attributes are the only attributes allowed after an access
4301// specifier.
4302bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4303 const AttributeList *AttrList) {
4304 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004305 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004306 handleAnnotateAttr(*this, ASDecl, *l);
4307 } else {
4308 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4309 return true;
4310 }
4311 }
4312
4313 return false;
4314}
4315
John McCall42856de2011-10-01 05:17:03 +00004316/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4317/// contains any decl attributes that we should warn about.
4318static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4319 for ( ; A; A = A->getNext()) {
4320 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004321 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004322 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4323
4324 if (A->getKind() == AttributeList::UnknownAttribute) {
4325 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4326 << A->getName() << A->getRange();
4327 } else {
4328 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4329 << A->getName() << A->getRange();
4330 }
4331 }
4332}
4333
4334/// checkUnusedDeclAttributes - Given a declarator which is not being
4335/// used to build a declaration, complain about any decl attributes
4336/// which might be lying around on it.
4337void Sema::checkUnusedDeclAttributes(Declarator &D) {
4338 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4339 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4340 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4341 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4342}
4343
Ryan Flynn7d470f32009-07-30 03:15:39 +00004344/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004345/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004346NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4347 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004348 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004349 NamedDecl *NewD = 0;
4350 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004351 FunctionDecl *NewFD;
4352 // FIXME: Missing call to CheckFunctionDeclaration().
4353 // FIXME: Mangling?
4354 // FIXME: Is the qualifier info correct?
4355 // FIXME: Is the DeclContext correct?
4356 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4357 Loc, Loc, DeclarationName(II),
4358 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004359 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004360 FD->hasPrototype(),
4361 false/*isConstexprSpecified*/);
4362 NewD = NewFD;
4363
4364 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004365 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004366
4367 // Fake up parameter variables; they are declared as if this were
4368 // a typedef.
4369 QualType FDTy = FD->getType();
4370 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4371 SmallVector<ParmVarDecl*, 16> Params;
4372 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4373 AE = FT->arg_type_end(); AI != AE; ++AI) {
4374 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4375 Param->setScopeInfo(0, Params.size());
4376 Params.push_back(Param);
4377 }
David Blaikie9c70e042011-09-21 18:16:56 +00004378 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004379 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004380 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4381 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004382 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004383 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004384 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004385 if (VD->getQualifier()) {
4386 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004387 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004388 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004389 }
4390 return NewD;
4391}
4392
James Dennett634962f2012-06-14 21:40:34 +00004393/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004394/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004395void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004396 if (W.getUsed()) return; // only do this once
4397 W.setUsed(true);
4398 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4399 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004400 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004401 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4402 NDId->getName()));
4403 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004404 WeakTopLevelDecl.push_back(NewD);
4405 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4406 // to insert Decl at TU scope, sorry.
4407 DeclContext *SavedContext = CurContext;
4408 CurContext = Context.getTranslationUnitDecl();
4409 PushOnScopeChains(NewD, S);
4410 CurContext = SavedContext;
4411 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004412 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004413 }
4414}
4415
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004416void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4417 // It's valid to "forward-declare" #pragma weak, in which case we
4418 // have to do this.
4419 LoadExternalWeakUndeclaredIdentifiers();
4420 if (!WeakUndeclaredIdentifiers.empty()) {
4421 NamedDecl *ND = NULL;
4422 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4423 if (VD->isExternC())
4424 ND = VD;
4425 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4426 if (FD->isExternC())
4427 ND = FD;
4428 if (ND) {
4429 if (IdentifierInfo *Id = ND->getIdentifier()) {
4430 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4431 = WeakUndeclaredIdentifiers.find(Id);
4432 if (I != WeakUndeclaredIdentifiers.end()) {
4433 WeakInfo W = I->second;
4434 DeclApplyPragmaWeak(S, ND, W);
4435 WeakUndeclaredIdentifiers[Id] = W;
4436 }
4437 }
4438 }
4439 }
4440}
4441
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004442/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4443/// it, apply them to D. This is a bit tricky because PD can have attributes
4444/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004445void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004446 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004447 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004448 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004449
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004450 // Walk the declarator structure, applying decl attributes that were in a type
4451 // position to the decl itself. This handles cases like:
4452 // int *__attr__(x)** D;
4453 // when X is a decl attribute.
4454 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4455 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004456 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004457
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004458 // Finally, apply any attributes on the decl itself.
4459 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004460 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004461}
John McCall28a6aea2009-11-04 02:18:39 +00004462
John McCall31168b02011-06-15 23:02:42 +00004463/// Is the given declaration allowed to use a forbidden type?
4464static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4465 // Private ivars are always okay. Unfortunately, people don't
4466 // always properly make their ivars private, even in system headers.
4467 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004468 // Function declarations in sys headers will be marked unavailable.
4469 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4470 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004471 return false;
4472
4473 // Require it to be declared in a system header.
4474 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4475}
4476
4477/// Handle a delayed forbidden-type diagnostic.
4478static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4479 Decl *decl) {
4480 if (decl && isForbiddenTypeAllowed(S, decl)) {
4481 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4482 "this system declaration uses an unsupported type"));
4483 return;
4484 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004485 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004486 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004487 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004488 // kind of forbidden type messages on unavailable functions.
4489 if (FD->hasAttr<UnavailableAttr>() &&
4490 diag.getForbiddenTypeDiagnostic() ==
4491 diag::err_arc_array_param_no_ownership) {
4492 diag.Triggered = true;
4493 return;
4494 }
4495 }
John McCall31168b02011-06-15 23:02:42 +00004496
4497 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4498 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4499 diag.Triggered = true;
4500}
4501
John McCall2ec85372012-05-07 06:16:41 +00004502void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4503 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004504 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004505 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004506
John McCall2ec85372012-05-07 06:16:41 +00004507 // When delaying diagnostics to run in the context of a parsed
4508 // declaration, we only want to actually emit anything if parsing
4509 // succeeds.
4510 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004511
John McCall2ec85372012-05-07 06:16:41 +00004512 // We emit all the active diagnostics in this pool or any of its
4513 // parents. In general, we'll get one pool for the decl spec
4514 // and a child pool for each declarator; in a decl group like:
4515 // deprecated_typedef foo, *bar, baz();
4516 // only the declarator pops will be passed decls. This is correct;
4517 // we really do need to consider delayed diagnostics from the decl spec
4518 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004519 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004520 do {
John McCall6347b682012-05-07 06:16:58 +00004521 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004522 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4523 // This const_cast is a bit lame. Really, Triggered should be mutable.
4524 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004525 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004526 continue;
4527
John McCallc1465822011-02-14 07:13:47 +00004528 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004529 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004530 case DelayedDiagnostic::Unavailable:
4531 // Don't bother giving deprecation/unavailable diagnostics if
4532 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004533 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004534 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004535 break;
4536
4537 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004538 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004539 break;
John McCall31168b02011-06-15 23:02:42 +00004540
4541 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004542 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004543 break;
John McCall86121512010-01-27 03:50:35 +00004544 }
4545 }
John McCall2ec85372012-05-07 06:16:41 +00004546 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004547}
4548
John McCall6347b682012-05-07 06:16:58 +00004549/// Given a set of delayed diagnostics, re-emit them as if they had
4550/// been delayed in the current context instead of in the given pool.
4551/// Essentially, this just moves them to the current pool.
4552void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4553 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4554 assert(curPool && "re-emitting in undelayed context not supported");
4555 curPool->steal(pool);
4556}
4557
John McCall28a6aea2009-11-04 02:18:39 +00004558static bool isDeclDeprecated(Decl *D) {
4559 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004560 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004561 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004562 // A category implicitly has the availability of the interface.
4563 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4564 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004565 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4566 return false;
4567}
4568
Ted Kremenekb79ee572013-12-18 23:30:06 +00004569static bool isDeclUnavailable(Decl *D) {
4570 do {
4571 if (D->isUnavailable())
4572 return true;
4573 // A category implicitly has the availability of the interface.
4574 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4575 return CatD->getClassInterface()->isUnavailable();
4576 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4577 return false;
4578}
4579
Eli Friedman971bfa12012-08-08 21:52:41 +00004580static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004581DoEmitAvailabilityWarning(Sema &S,
4582 DelayedDiagnostic::DDKind K,
4583 Decl *Ctx,
4584 const NamedDecl *D,
4585 StringRef Message,
4586 SourceLocation Loc,
4587 const ObjCInterfaceDecl *UnknownObjCClass,
4588 const ObjCPropertyDecl *ObjCProperty) {
4589
4590 // Diagnostics for deprecated or unavailable.
4591 unsigned diag, diag_message, diag_fwdclass_message;
4592
4593 // Matches 'diag::note_property_attribute' options.
4594 unsigned property_note_select;
4595
4596 // Matches diag::note_availability_specified_here.
4597 unsigned available_here_select_kind;
4598
4599 // Don't warn if our current context is deprecated or unavailable.
4600 switch (K) {
4601 case DelayedDiagnostic::Deprecation:
4602 if (isDeclDeprecated(Ctx))
4603 return;
4604 diag = diag::warn_deprecated;
4605 diag_message = diag::warn_deprecated_message;
4606 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4607 property_note_select = /* deprecated */ 0;
4608 available_here_select_kind = /* deprecated */ 2;
4609 break;
4610
4611 case DelayedDiagnostic::Unavailable:
4612 if (isDeclUnavailable(Ctx))
4613 return;
4614 diag = diag::err_unavailable;
4615 diag_message = diag::err_unavailable_message;
4616 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4617 property_note_select = /* unavailable */ 1;
4618 available_here_select_kind = /* unavailable */ 0;
4619 break;
4620
4621 default:
4622 llvm_unreachable("Neither a deprecation or unavailable kind");
4623 }
4624
Eli Friedman971bfa12012-08-08 21:52:41 +00004625 DeclarationName Name = D->getDeclName();
4626 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004627 S.Diag(Loc, diag_message) << Name << Message;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004628 if (ObjCProperty)
4629 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4630 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004631 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004632 S.Diag(Loc, diag) << Name;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004633 if (ObjCProperty)
4634 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4635 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004636 } else {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004637 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004638 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4639 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004640
4641 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4642 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004643}
4644
Ted Kremenekb79ee572013-12-18 23:30:06 +00004645void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4646 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004647 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004648 DoEmitAvailabilityWarning(*this,
4649 (DelayedDiagnostic::DDKind) DD.Kind,
4650 Ctx,
4651 DD.getDeprecationDecl(),
4652 DD.getDeprecationMessage(),
4653 DD.Loc,
4654 DD.getUnknownObjCClass(),
4655 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004656}
4657
Ted Kremenekb79ee572013-12-18 23:30:06 +00004658void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4659 NamedDecl *D, StringRef Message,
4660 SourceLocation Loc,
4661 const ObjCInterfaceDecl *UnknownObjCClass,
4662 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004663 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004664 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004665 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4666 UnknownObjCClass,
4667 ObjCProperty,
4668 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004669 return;
4670 }
4671
Ted Kremenekb79ee572013-12-18 23:30:06 +00004672 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4673 DelayedDiagnostic::DDKind K;
4674 switch (AD) {
4675 case AD_Deprecation:
4676 K = DelayedDiagnostic::Deprecation;
4677 break;
4678 case AD_Unavailable:
4679 K = DelayedDiagnostic::Unavailable;
4680 break;
4681 }
4682
4683 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
4684 UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004685}