blob: 19b79555489e59b7c337b1df93d005ba140c29d8 [file] [log] [blame]
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000015#include "clang/AST/ASTContext.h"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000016#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000020#include "clang/AST/Expr.h"
Rafael Espindoladb77c4a2013-02-26 19:13:56 +000021#include "clang/AST/Mangle.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000022#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000023#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000024#include "clang/Basic/TargetInfo.h"
Benjamin Kramer6ee15622013-09-13 15:35:43 +000025#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000026#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000027#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000028#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000029#include "clang/Sema/Scope.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000030#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000031using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000032using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000033
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000034namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000035 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000036 C,
37 Cpp,
38 ObjC
39 };
40}
41
Chris Lattner58418ff2008-06-29 00:16:31 +000042//===----------------------------------------------------------------------===//
43// Helper functions
44//===----------------------------------------------------------------------===//
45
Ted Kremenek527042b2009-08-14 20:49:40 +000046/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000047/// type (function or function-typed variable) or an Objective-C
48/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000049static bool isFunctionOrMethod(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000050 return (D->getFunctionType() != NULL) || isa<ObjCMethodDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000051}
52
John McCall3882ace2011-01-05 12:14:39 +000053/// Return true if the given decl has a declarator that should have
54/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000055static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000056 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000057 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
58 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +000059}
60
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000061/// hasFunctionProto - Return true if the given decl has a argument
62/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000063/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000064static bool hasFunctionProto(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000065 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000066 return isa<FunctionProtoType>(FnTy);
Aaron Ballman12b9f652014-01-16 13:55:42 +000067 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000068}
69
Alp Toker601b22c2014-01-21 23:35:24 +000070/// getFunctionOrMethodNumParams - Return number of function or method
71/// parameters. It is an error to call this on a K&R function (use
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000072/// hasFunctionProto first).
Alp Toker601b22c2014-01-21 23:35:24 +000073static unsigned getFunctionOrMethodNumParams(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000074 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000075 return cast<FunctionProtoType>(FnTy)->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000076 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000077 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000078 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000079}
80
Alp Toker601b22c2014-01-21 23:35:24 +000081static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000082 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000083 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +000084 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000085 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +000086
Chandler Carruthff4c4f02011-07-01 23:49:12 +000087 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000088}
89
Chandler Carruthff4c4f02011-07-01 23:49:12 +000090static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000091 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker314cc812014-01-25 16:55:45 +000092 return cast<FunctionProtoType>(FnTy)->getReturnType();
93 return cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +000094}
95
Chandler Carruthff4c4f02011-07-01 23:49:12 +000096static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000097 if (const FunctionType *FnTy = D->getFunctionType()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +000098 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000099 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000100 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000101 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000102 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000103 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000104 }
105}
106
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000107static bool isInstanceMethod(const Decl *D) {
108 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000109 return MethodDecl->isInstance();
110 return false;
111}
112
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000113static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000114 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000115 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000116 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000117
John McCall96fa4842010-05-17 21:00:27 +0000118 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
119 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000120 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000121
John McCall96fa4842010-05-17 21:00:27 +0000122 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000123
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000124 // FIXME: Should we walk the chain of classes?
125 return ClsName == &Ctx.Idents.get("NSString") ||
126 ClsName == &Ctx.Idents.get("NSMutableString");
127}
128
Daniel Dunbar980c6692008-09-26 03:32:58 +0000129static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000130 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000131 if (!PT)
132 return false;
133
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000134 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000135 if (!RT)
136 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000137
Daniel Dunbar980c6692008-09-26 03:32:58 +0000138 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000139 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000140 return false;
141
142 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
143}
144
Richard Smithb87c4652013-10-31 21:23:20 +0000145static unsigned getNumAttributeArgs(const AttributeList &Attr) {
146 // FIXME: Include the type in the argument list.
147 return Attr.getNumArgs() + Attr.hasParsedType();
148}
149
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000150/// \brief Check if the attribute has exactly as many args as Num. May
151/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000152static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000153 unsigned Num) {
154 if (getNumAttributeArgs(Attr) != Num) {
Aaron Ballmanb7243382013-07-23 19:30:11 +0000155 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
156 << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000157 return false;
158 }
159
160 return true;
161}
162
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000163/// \brief Check if the attribute has at least as many args as Num. May
164/// output an error.
165static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000166 unsigned Num) {
167 if (getNumAttributeArgs(Attr) < Num) {
Aaron Ballman05e420a2014-01-02 21:26:14 +0000168 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments)
169 << Attr.getName() << Num;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000170 return false;
171 }
172
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000173 return true;
174}
175
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000176/// \brief If Expr is a valid integer constant, get the value of the integer
177/// expression and return success or failure. May output an error.
178static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
179 const Expr *Expr, uint32_t &Val,
180 unsigned Idx = UINT_MAX) {
181 llvm::APSInt I(32);
182 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
183 !Expr->isIntegerConstantExpr(I, S.Context)) {
184 if (Idx != UINT_MAX)
185 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
186 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
187 << Expr->getSourceRange();
188 else
189 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
190 << Attr.getName() << AANT_ArgumentIntegerConstant
191 << Expr->getSourceRange();
192 return false;
193 }
194 Val = (uint32_t)I.getZExtValue();
195 return true;
196}
197
Aaron Ballmanfb763042013-12-02 18:05:46 +0000198/// \brief Diagnose mutually exclusive attributes when present on a given
199/// declaration. Returns true if diagnosed.
200template <typename AttrTy>
201static bool checkAttrMutualExclusion(Sema &S, Decl *D,
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000202 const AttributeList &Attr) {
203 if (AttrTy *A = D->getAttr<AttrTy>()) {
Aaron Ballmanfb763042013-12-02 18:05:46 +0000204 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000205 << Attr.getName() << A;
Aaron Ballmanfb763042013-12-02 18:05:46 +0000206 return true;
207 }
208 return false;
209}
210
Alp Toker601b22c2014-01-21 23:35:24 +0000211/// \brief Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000212/// instance method D. May output an error.
213///
214/// \returns true if IdxExpr is a valid index.
Alp Toker601b22c2014-01-21 23:35:24 +0000215static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D,
216 const AttributeList &Attr,
217 unsigned AttrArgNum,
218 const Expr *IdxExpr,
219 uint64_t &Idx) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000220 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000221
222 // In C++ the implicit 'this' function parameter also counts.
223 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000224 bool HP = hasFunctionProto(D);
225 bool HasImplicitThisParam = isInstanceMethod(D);
226 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000227 unsigned NumParams =
228 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000229
230 llvm::APSInt IdxInt;
231 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
232 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000233 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
234 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
235 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000236 return false;
237 }
238
239 Idx = IdxInt.getLimitedValue();
Alp Toker601b22c2014-01-21 23:35:24 +0000240 if (Idx < 1 || (!IV && Idx > NumParams)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000241 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
242 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000243 return false;
244 }
245 Idx--; // Convert to zero-based.
246 if (HasImplicitThisParam) {
247 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000248 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000249 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000250 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000251 return false;
252 }
253 --Idx;
254 }
255
256 return true;
257}
258
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000259/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
260/// If not emit an error and return false. If the argument is an identifier it
261/// will emit an error with a fixit hint and treat it as if it was a string
262/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000263bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
264 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000265 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000266 // Look for identifiers. If we have one emit a hint to fix it to a literal.
267 if (Attr.isArgIdent(ArgNum)) {
268 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000269 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000270 << Attr.getName() << AANT_ArgumentString
271 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000272 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000273 Str = Loc->Ident->getName();
274 if (ArgLocation)
275 *ArgLocation = Loc->Loc;
276 return true;
277 }
278
279 // Now check for an actual string literal.
280 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
281 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
282 if (ArgLocation)
283 *ArgLocation = ArgExpr->getLocStart();
284
285 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000286 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000287 << Attr.getName() << AANT_ArgumentString;
288 return false;
289 }
290
291 Str = Literal->getString();
292 return true;
293}
294
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000295/// \brief Applies the given attribute to the Decl without performing any
296/// additional semantic checking.
297template <typename AttrType>
298static void handleSimpleAttribute(Sema &S, Decl *D,
299 const AttributeList &Attr) {
300 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
301 Attr.getAttributeSpellingListIndex()));
302}
303
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000304/// \brief Check if the passed-in expression is of type int or bool.
305static bool isIntOrBool(Expr *Exp) {
306 QualType QT = Exp->getType();
307 return QT->isBooleanType() || QT->isIntegerType();
308}
309
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000310
311// Check to see if the type is a smart pointer of some kind. We assume
312// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000313static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
314 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
315 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000316 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000317 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000318
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000319 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
320 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000321 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000322 return false;
323
324 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000325}
326
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000327/// \brief Check if passed in Decl is a pointer type.
328/// Note that this function may produce an error message.
329/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000330static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
331 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000332 const ValueDecl *vd = cast<ValueDecl>(D);
333 QualType QT = vd->getType();
334 if (QT->isAnyPointerType())
335 return true;
336
337 if (const RecordType *RT = QT->getAs<RecordType>()) {
338 // If it's an incomplete type, it could be a smart pointer; skip it.
339 // (We don't want to force template instantiation if we can avoid it,
340 // since that would alter the order in which templates are instantiated.)
341 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000342 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000343
Aaron Ballman553e6812013-12-26 14:54:11 +0000344 if (threadSafetyCheckIsSmartPointer(S, RT))
345 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000346 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000347
348 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000349 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000350 return false;
351}
352
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000353/// \brief Checks that the passed in QualType either is of RecordType or points
354/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000355static const RecordType *getRecordType(QualType QT) {
356 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000357 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000358
359 // Now check if we point to record type.
360 if (const PointerType *PT = QT->getAs<PointerType>())
361 return PT->getPointeeType()->getAs<RecordType>();
362
363 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000364}
365
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000366
Jordy Rose740b0c22012-05-08 03:27:22 +0000367static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
368 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000369 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000370 return RT->getDecl()->hasAttr<CapabilityAttr>();
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000371}
372
373
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000374/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000375/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000376static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
377 QualType Ty) {
378 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000379
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000380 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000381 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000382 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
Aaron Ballman1da282a2014-01-02 23:15:58 +0000383 << Attr.getName() << Ty;
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000384 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000385 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000386
Michael Hana9171bc2012-08-03 17:40:43 +0000387 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000388 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000389 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000390
391 // Allow smart pointers to be used as lockable objects.
392 // FIXME -- Check the type that the smart pointer points to.
393 if (threadSafetyCheckIsSmartPointer(S, RT))
394 return;
395
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000396 // Check if the type is lockable.
397 RecordDecl *RD = RT->getDecl();
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000398 if (RD->hasAttr<CapabilityAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000399 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000400
401 // Else check if any base classes are lockable.
402 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
403 CXXBasePaths BPaths(false, false);
404 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
405 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000406 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000407
408 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
Aaron Ballman1da282a2014-01-02 23:15:58 +0000409 << Attr.getName() << Ty;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000410}
411
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000412/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000413/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000414/// \param Sidx The attribute argument index to start checking with.
415/// \param ParamIdxOk Whether an argument can be indexing into a function
416/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000417static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000418 const AttributeList &Attr,
419 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000420 int Sidx = 0,
421 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000422 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000423 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000424
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000425 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000426 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000427 Args.push_back(ArgExp);
428 continue;
429 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000430
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000431 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000432 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000433 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000434 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000435 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000436 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000437 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000438 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000439
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000440 // We allow constant strings to be used as a placeholder for expressions
441 // that are not valid C++ syntax, but warn that they are ignored.
442 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
443 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000444 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000445 continue;
446 }
447
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000448 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000449
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000450 // A pointer to member expression of the form &MyClass::mu is treated
451 // specially -- we need to look at the type of the member.
452 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
453 if (UOp->getOpcode() == UO_AddrOf)
454 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
455 if (DRE->getDecl()->isCXXInstanceMember())
456 ArgTy = DRE->getDecl()->getType();
457
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000458 // First see if we can just cast to record type, or point to record type.
459 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000460
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000461 // Now check if we index into a record type function param.
462 if(!RT && ParamIdxOk) {
463 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000464 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
465 if(FD && IL) {
466 unsigned int NumParams = FD->getNumParams();
467 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000468 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
469 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
470 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000471 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
472 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000473 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000474 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000475 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000476 }
477 }
478
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000479 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000480
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000481 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000482 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000483}
484
Chris Lattner58418ff2008-06-29 00:16:31 +0000485//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000486// Attribute Implementations
487//===----------------------------------------------------------------------===//
488
Daniel Dunbar032db472008-07-31 22:40:48 +0000489// FIXME: All this manual attribute parsing code is gross. At the
490// least add some helper functions to check most argument patterns (#
491// and types of args).
492
Michael Hana9171bc2012-08-03 17:40:43 +0000493static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000494 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000495 if (!threadSafetyCheckIsPointer(S, D, Attr))
496 return;
497
Michael Han99315932013-01-24 16:46:58 +0000498 D->addAttr(::new (S.Context)
499 PtGuardedVarAttr(Attr.getRange(), S.Context,
500 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000501}
502
Michael Hana9171bc2012-08-03 17:40:43 +0000503static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
504 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000505 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000506 SmallVector<Expr*, 1> Args;
507 // check that all arguments are lockable objects
508 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
509 unsigned Size = Args.size();
510 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000511 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000512
Michael Han3be3b442012-07-23 18:48:41 +0000513 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000514
Michael Han3be3b442012-07-23 18:48:41 +0000515 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000516}
517
Michael Han3be3b442012-07-23 18:48:41 +0000518static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
519 Expr *Arg = 0;
520 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
521 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000522
Aaron Ballman36a53502014-01-16 13:03:14 +0000523 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg,
524 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000525}
526
Michael Hana9171bc2012-08-03 17:40:43 +0000527static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000528 const AttributeList &Attr) {
529 Expr *Arg = 0;
530 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
531 return;
532
533 if (!threadSafetyCheckIsPointer(S, D, Attr))
534 return;
535
536 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +0000537 S.Context, Arg,
538 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000539}
540
Michael Hana9171bc2012-08-03 17:40:43 +0000541static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
542 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000543 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000544 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000545 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000546
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000547 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000548 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000549 if (!QT->isDependentType()) {
550 const RecordType *RT = getRecordType(QT);
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000551 if (!RT || !RT->getDecl()->hasAttr<CapabilityAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000552 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000553 << Attr.getName();
554 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000555 }
556 }
557
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000558 // Check that all arguments are lockable objects.
559 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000560 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000561 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000562
Michael Han3be3b442012-07-23 18:48:41 +0000563 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000564}
565
Michael Hana9171bc2012-08-03 17:40:43 +0000566static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000567 const AttributeList &Attr) {
568 SmallVector<Expr*, 1> Args;
569 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
570 return;
571
572 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000573 D->addAttr(::new (S.Context)
574 AcquiredAfterAttr(Attr.getRange(), S.Context,
575 StartArg, Args.size(),
576 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000577}
578
Michael Hana9171bc2012-08-03 17:40:43 +0000579static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000580 const AttributeList &Attr) {
581 SmallVector<Expr*, 1> Args;
582 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
583 return;
584
585 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000586 D->addAttr(::new (S.Context)
587 AcquiredBeforeAttr(Attr.getRange(), S.Context,
588 StartArg, Args.size(),
589 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000590}
591
Michael Hana9171bc2012-08-03 17:40:43 +0000592static bool checkLockFunAttrCommon(Sema &S, Decl *D,
593 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000594 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000595 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000596 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000597 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000598
Michael Han3be3b442012-07-23 18:48:41 +0000599 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000600}
601
Michael Hana9171bc2012-08-03 17:40:43 +0000602static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000603 const AttributeList &Attr) {
604 SmallVector<Expr*, 1> Args;
605 if (!checkLockFunAttrCommon(S, D, Attr, Args))
606 return;
607
608 unsigned Size = Args.size();
609 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000610 D->addAttr(::new (S.Context)
611 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
612 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000613}
614
Michael Hana9171bc2012-08-03 17:40:43 +0000615static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000616 const AttributeList &Attr) {
617 SmallVector<Expr*, 1> Args;
618 if (!checkLockFunAttrCommon(S, D, Attr, Args))
619 return;
620
621 unsigned Size = Args.size();
622 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000623 D->addAttr(::new (S.Context)
624 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
625 StartArg, Size,
626 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000627}
628
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000629static void handleAssertSharedLockAttr(Sema &S, Decl *D,
630 const AttributeList &Attr) {
631 SmallVector<Expr*, 1> Args;
632 if (!checkLockFunAttrCommon(S, D, Attr, Args))
633 return;
634
635 unsigned Size = Args.size();
636 Expr **StartArg = Size == 0 ? 0 : &Args[0];
637 D->addAttr(::new (S.Context)
638 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
639 Attr.getAttributeSpellingListIndex()));
640}
641
642static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
643 const AttributeList &Attr) {
644 SmallVector<Expr*, 1> Args;
645 if (!checkLockFunAttrCommon(S, D, Attr, Args))
646 return;
647
648 unsigned Size = Args.size();
649 Expr **StartArg = Size == 0 ? 0 : &Args[0];
650 D->addAttr(::new (S.Context)
651 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
652 StartArg, Size,
653 Attr.getAttributeSpellingListIndex()));
654}
655
656
Michael Hana9171bc2012-08-03 17:40:43 +0000657static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
658 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000659 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000660 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000661 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000662
Aaron Ballman00e99962013-08-31 01:11:41 +0000663 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000664 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000665 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000666 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000667 }
668
669 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000670 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000671
Michael Han3be3b442012-07-23 18:48:41 +0000672 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000673}
674
Michael Hana9171bc2012-08-03 17:40:43 +0000675static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000676 const AttributeList &Attr) {
677 SmallVector<Expr*, 2> Args;
678 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
679 return;
680
Michael Han99315932013-01-24 16:46:58 +0000681 D->addAttr(::new (S.Context)
682 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000683 Attr.getArgAsExpr(0),
684 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000685 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000686}
687
Michael Hana9171bc2012-08-03 17:40:43 +0000688static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000689 const AttributeList &Attr) {
690 SmallVector<Expr*, 2> Args;
691 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
692 return;
693
Michael Han99315932013-01-24 16:46:58 +0000694 D->addAttr(::new (S.Context)
695 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000696 Attr.getArgAsExpr(0),
697 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000698 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000699}
700
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000701static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000702 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000703 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000704 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000705 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000706 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000707 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000708 Expr **StartArg = Size == 0 ? 0 : &Args[0];
709
Michael Han99315932013-01-24 16:46:58 +0000710 D->addAttr(::new (S.Context)
711 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
712 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000713}
714
715static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000716 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000717 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000718 SmallVector<Expr*, 1> Args;
719 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
720 unsigned Size = Args.size();
721 if (Size == 0)
722 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000723
Michael Han99315932013-01-24 16:46:58 +0000724 D->addAttr(::new (S.Context)
725 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
726 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000727}
728
729static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000730 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000731 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000732 return;
733
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000734 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000735 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000736 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000737 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000738 if (Size == 0)
739 return;
740 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000741
Michael Han99315932013-01-24 16:46:58 +0000742 D->addAttr(::new (S.Context)
743 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
744 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000745}
746
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000747static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
748 Expr *Cond = Attr.getArgAsExpr(0);
749 if (!Cond->isTypeDependent()) {
750 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
751 if (Converted.isInvalid())
752 return;
753 Cond = Converted.take();
754 }
755
756 StringRef Msg;
757 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
758 return;
759
760 SmallVector<PartialDiagnosticAt, 8> Diags;
761 if (!Cond->isValueDependent() &&
762 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
763 Diags)) {
764 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
765 for (int I = 0, N = Diags.size(); I != N; ++I)
766 S.Diag(Diags[I].first, Diags[I].second);
767 return;
768 }
769
770 D->addAttr(::new (S.Context)
771 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
772 Attr.getAttributeSpellingListIndex()));
773}
774
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000775static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000776 ConsumableAttr::ConsumedState DefaultState;
777
778 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000779 IdentifierLoc *IL = Attr.getArgAsIdent(0);
780 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
781 DefaultState)) {
782 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
783 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000784 return;
785 }
David Blaikie16f76d22013-09-06 01:28:43 +0000786 } else {
787 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
788 << Attr.getName() << AANT_ArgumentIdentifier;
789 return;
790 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000791
792 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000793 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000794 Attr.getAttributeSpellingListIndex()));
795}
796
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000797
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000798static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
799 const AttributeList &Attr) {
800 ASTContext &CurrContext = S.getASTContext();
801 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
802
803 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
804 if (!RD->hasAttr<ConsumableAttr>()) {
805 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
806 RD->getNameAsString();
807
808 return false;
809 }
810 }
811
812 return true;
813}
814
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000815
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000816static void handleCallableWhenAttr(Sema &S, Decl *D,
817 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000818 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
819 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000820
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000821 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
822 return;
823
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000824 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
825 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
826 CallableWhenAttr::ConsumedState CallableState;
827
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000828 StringRef StateString;
829 SourceLocation Loc;
830 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
831 return;
832
833 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000834 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000835 S.Diag(Loc, diag::warn_attribute_type_not_supported)
836 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000837 return;
838 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000839
840 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000841 }
842
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000843 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000844 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
845 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000846}
847
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000848
DeLesley Hutchins69391772013-10-17 23:23:53 +0000849static void handleParamTypestateAttr(Sema &S, Decl *D,
850 const AttributeList &Attr) {
851 if (!checkAttributeNumArgs(S, Attr, 1)) return;
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000852
DeLesley Hutchins69391772013-10-17 23:23:53 +0000853 ParamTypestateAttr::ConsumedState ParamState;
854
855 if (Attr.isArgIdent(0)) {
856 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
857 StringRef StateString = Ident->Ident->getName();
858
859 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
860 ParamState)) {
861 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
862 << Attr.getName() << StateString;
863 return;
864 }
865 } else {
866 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
867 Attr.getName() << AANT_ArgumentIdentifier;
868 return;
869 }
870
871 // FIXME: This check is currently being done in the analysis. It can be
872 // enabled here only after the parser propagates attributes at
873 // template specialization definition, not declaration.
874 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
875 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
876 //
877 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
878 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
879 // ReturnType.getAsString();
880 // return;
881 //}
882
883 D->addAttr(::new (S.Context)
884 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
885 Attr.getAttributeSpellingListIndex()));
886}
887
888
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000889static void handleReturnTypestateAttr(Sema &S, Decl *D,
890 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000891 if (!checkAttributeNumArgs(S, Attr, 1)) return;
892
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000893 ReturnTypestateAttr::ConsumedState ReturnState;
894
895 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000896 IdentifierLoc *IL = Attr.getArgAsIdent(0);
897 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
898 ReturnState)) {
899 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
900 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000901 return;
902 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000903 } else {
904 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
905 Attr.getName() << AANT_ArgumentIdentifier;
906 return;
907 }
908
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000909 // FIXME: This check is currently being done in the analysis. It can be
910 // enabled here only after the parser propagates attributes at
911 // template specialization definition, not declaration.
912 //QualType ReturnType;
913 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000914 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
915 // ReturnType = Param->getType();
916 //
917 //} else if (const CXXConstructorDecl *Constructor =
918 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000919 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
920 //
921 //} else {
922 //
923 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
924 //}
925 //
926 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
927 //
928 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
929 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
930 // ReturnType.getAsString();
931 // return;
932 //}
933
934 D->addAttr(::new (S.Context)
935 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
936 Attr.getAttributeSpellingListIndex()));
937}
938
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000939
940static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000941 if (!checkAttributeNumArgs(S, Attr, 1))
942 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000943
944 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
945 return;
946
947 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000948 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000949 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
950 StringRef Param = Ident->Ident->getName();
951 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
952 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
953 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000954 return;
955 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000956 } else {
957 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
958 Attr.getName() << AANT_ArgumentIdentifier;
959 return;
960 }
961
962 D->addAttr(::new (S.Context)
963 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
964 Attr.getAttributeSpellingListIndex()));
965}
966
Chris Wailes9385f9f2013-10-29 20:28:41 +0000967static void handleTestTypestateAttr(Sema &S, Decl *D,
968 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000969 if (!checkAttributeNumArgs(S, Attr, 1))
970 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000971
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000972 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
973 return;
974
Chris Wailes9385f9f2013-10-29 20:28:41 +0000975 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000976 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000977 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
978 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +0000979 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000980 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
981 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000982 return;
983 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000984 } else {
985 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
986 Attr.getName() << AANT_ArgumentIdentifier;
987 return;
988 }
989
990 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +0000991 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000992 Attr.getAttributeSpellingListIndex()));
993}
994
Chandler Carruthedc2c642011-07-02 00:01:44 +0000995static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
996 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +0000997 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000998 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000999}
1000
Chandler Carruthedc2c642011-07-02 00:01:44 +00001001static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001002 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Aaron Ballman36a53502014-01-16 13:03:14 +00001003 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context,
1004 Attr.getAttributeSpellingListIndex()));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001005 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001006 // If the alignment is less than or equal to 8 bits, the packed attribute
1007 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001008 if (!FD->getType()->isDependentType() &&
1009 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001010 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001011 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001012 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001013 else
Michael Han99315932013-01-24 16:46:58 +00001014 FD->addAttr(::new (S.Context)
1015 PackedAttr(Attr.getRange(), S.Context,
1016 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001017 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001018 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001019}
1020
Ted Kremenek7fd17232011-09-29 07:02:25 +00001021static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1022 // The IBOutlet/IBOutletCollection attributes only apply to instance
1023 // variables or properties of Objective-C classes. The outlet must also
1024 // have an object reference type.
1025 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1026 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001027 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001028 << Attr.getName() << VD->getType() << 0;
1029 return false;
1030 }
1031 }
1032 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1033 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001034 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001035 << Attr.getName() << PD->getType() << 1;
1036 return false;
1037 }
1038 }
1039 else {
1040 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1041 return false;
1042 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001043
Ted Kremenek7fd17232011-09-29 07:02:25 +00001044 return true;
1045}
1046
Chandler Carruthedc2c642011-07-02 00:01:44 +00001047static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001048 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001049 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001050
Michael Han99315932013-01-24 16:46:58 +00001051 D->addAttr(::new (S.Context)
1052 IBOutletAttr(Attr.getRange(), S.Context,
1053 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001054}
1055
Chandler Carruthedc2c642011-07-02 00:01:44 +00001056static void handleIBOutletCollection(Sema &S, Decl *D,
1057 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001058
1059 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001060 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001061 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1062 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001063 return;
1064 }
1065
Ted Kremenek7fd17232011-09-29 07:02:25 +00001066 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001067 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001068
Richard Smithb1f9a282013-10-31 01:56:18 +00001069 ParsedType PT;
1070
1071 if (Attr.hasParsedType())
1072 PT = Attr.getTypeArg();
1073 else {
1074 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1075 S.getScopeForContext(D->getDeclContext()->getParent()));
1076 if (!PT) {
1077 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1078 return;
1079 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001080 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001081
Richard Smithb87c4652013-10-31 21:23:20 +00001082 TypeSourceInfo *QTLoc = 0;
1083 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1084 if (!QTLoc)
1085 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001086
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001087 // Diagnose use of non-object type in iboutletcollection attribute.
1088 // FIXME. Gnu attribute extension ignores use of builtin types in
1089 // attributes. So, __attribute__((iboutletcollection(char))) will be
1090 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001091 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001092 S.Diag(Attr.getLoc(),
1093 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1094 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001095 return;
1096 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001097
Michael Han99315932013-01-24 16:46:58 +00001098 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001099 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001100 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001101}
1102
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001103static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001104 if (const RecordType *UT = T->getAsUnionType())
1105 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1106 RecordDecl *UD = UT->getDecl();
1107 for (RecordDecl::field_iterator it = UD->field_begin(),
1108 itend = UD->field_end(); it != itend; ++it) {
1109 QualType QT = it->getType();
1110 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1111 T = QT;
1112 return;
1113 }
1114 }
1115 }
1116}
1117
Ted Kremenek9aedc152014-01-17 06:24:56 +00001118static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001119 SourceRange R, bool isReturnValue = false) {
Ted Kremenek9aedc152014-01-17 06:24:56 +00001120 T = T.getNonReferenceType();
1121 possibleTransparentUnionPointerType(T);
1122
1123 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001124 S.Diag(Attr.getLoc(),
1125 isReturnValue ? diag::warn_attribute_return_pointers_only
1126 : diag::warn_attribute_pointers_only)
Ted Kremenek9aedc152014-01-17 06:24:56 +00001127 << Attr.getName() << R;
1128 return false;
1129 }
1130 return true;
1131}
1132
Chandler Carruthedc2c642011-07-02 00:01:44 +00001133static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001134 SmallVector<unsigned, 8> NonNullArgs;
1135 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001136 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001137 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001138 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001139 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001140
1141 // Is the function argument a pointer type?
Ted Kremenek9aedc152014-01-17 06:24:56 +00001142 // FIXME: Should also highlight argument in decl in the diagnostic.
Alp Toker601b22c2014-01-21 23:35:24 +00001143 if (!attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr,
1144 Ex->getSourceRange()))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001145 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001146
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001147 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001148 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001149
1150 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1151 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001152 if (NonNullArgs.empty()) {
Alp Toker601b22c2014-01-21 23:35:24 +00001153 for (unsigned i = 0, e = getFunctionOrMethodNumParams(D); i != e; ++i) {
1154 QualType T = getFunctionOrMethodParamType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001155 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001156 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001157 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001158 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001159
Ted Kremenek22813f42010-10-21 18:49:36 +00001160 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001161 if (NonNullArgs.empty()) {
1162 // Warn the trivial case only if attribute is not coming from a
1163 // macro instantiation.
1164 if (Attr.getLoc().isFileID())
1165 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001166 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001167 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001168 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001169
Nick Lewyckye1121512013-01-24 01:12:16 +00001170 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001171 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001172 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001173 D->addAttr(::new (S.Context)
1174 NonNullAttr(Attr.getRange(), S.Context, start, size,
1175 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001176}
1177
Jordan Rosec9399072014-02-11 17:27:59 +00001178static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1179 const AttributeList &Attr) {
1180 if (Attr.getNumArgs() > 0) {
1181 if (D->getFunctionType()) {
1182 handleNonNullAttr(S, D, Attr);
1183 } else {
1184 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1185 << D->getSourceRange();
1186 }
1187 return;
1188 }
1189
1190 // Is the argument a pointer type?
1191 if (!attrNonNullArgCheck(S, D->getType(), Attr, D->getSourceRange()))
1192 return;
1193
1194 D->addAttr(::new (S.Context)
1195 NonNullAttr(Attr.getRange(), S.Context, 0, 0,
1196 Attr.getAttributeSpellingListIndex()));
1197}
1198
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001199static void handleReturnsNonNullAttr(Sema &S, Decl *D,
1200 const AttributeList &Attr) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001201 QualType ResultType = getFunctionOrMethodResultType(D);
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001202 if (!attrNonNullArgCheck(S, ResultType, Attr, Attr.getRange(),
1203 /* isReturnValue */ true))
1204 return;
1205
1206 D->addAttr(::new (S.Context)
1207 ReturnsNonNullAttr(Attr.getRange(), S.Context,
1208 Attr.getAttributeSpellingListIndex()));
1209}
1210
Chandler Carruthedc2c642011-07-02 00:01:44 +00001211static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001212 // This attribute must be applied to a function declaration. The first
1213 // argument to the attribute must be an identifier, the name of the resource,
1214 // for example: malloc. The following arguments must be argument indexes, the
1215 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001216 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001217 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001218 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001219
Aaron Ballman00e99962013-08-31 01:11:41 +00001220 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001221 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001222 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001223 return;
1224 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001225
Richard Smith852e9ce2013-11-27 01:46:48 +00001226 // Figure out our Kind.
1227 OwnershipAttr::OwnershipKind K =
1228 OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0,
1229 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001230
Richard Smith852e9ce2013-11-27 01:46:48 +00001231 // Check arguments.
1232 switch (K) {
1233 case OwnershipAttr::Takes:
1234 case OwnershipAttr::Holds:
1235 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001236 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1237 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001238 return;
1239 }
1240 break;
1241 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001242 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001243 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1244 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001245 return;
1246 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001247 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001248 }
1249
Richard Smith852e9ce2013-11-27 01:46:48 +00001250 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001251
1252 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001253 StringRef ModuleName = Module->getName();
1254 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1255 ModuleName.size() > 4) {
1256 ModuleName = ModuleName.drop_front(2).drop_back(2);
1257 Module = &S.PP.getIdentifierTable().get(ModuleName);
1258 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001259
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001260 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001261 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1262 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001263 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001264 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001265 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001266
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001267 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001268 QualType T = getFunctionOrMethodParamType(D, Idx);
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001269 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001270 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001271 case OwnershipAttr::Takes:
1272 case OwnershipAttr::Holds:
1273 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1274 Err = 0;
1275 break;
1276 case OwnershipAttr::Returns:
1277 if (!T->isIntegerType())
1278 Err = 1;
1279 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001280 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001281 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001282 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001283 << Ex->getSourceRange();
1284 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001285 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001286
1287 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001288 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001289 i = D->specific_attr_begin<OwnershipAttr>(),
1290 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001291 // FIXME: A returns attribute should conflict with any returns attribute
1292 // with a different index too.
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001293 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1294 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1295 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballman05a63782014-01-20 15:06:09 +00001296 << AL.getName() << *i;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001297 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001298 }
1299 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001300 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001301 }
1302
1303 unsigned* start = OwnershipArgs.data();
1304 unsigned size = OwnershipArgs.size();
1305 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001306
Michael Han99315932013-01-24 16:46:58 +00001307 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001308 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001309 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001310}
1311
Chandler Carruthedc2c642011-07-02 00:01:44 +00001312static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001313 // Check the attribute arguments.
1314 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001315 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1316 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001317 return;
1318 }
1319
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001320 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001321
Rafael Espindolac18086a2010-02-23 22:00:30 +00001322 // gcc rejects
1323 // class c {
1324 // static int a __attribute__((weakref ("v2")));
1325 // static int b() __attribute__((weakref ("f3")));
1326 // };
1327 // and ignores the attributes of
1328 // void f(void) {
1329 // static int a __attribute__((weakref ("v2")));
1330 // }
1331 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001332 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001333 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001334 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1335 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001336 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001337 }
1338
1339 // The GCC manual says
1340 //
1341 // At present, a declaration to which `weakref' is attached can only
1342 // be `static'.
1343 //
1344 // It also says
1345 //
1346 // Without a TARGET,
1347 // given as an argument to `weakref' or to `alias', `weakref' is
1348 // equivalent to `weak'.
1349 //
1350 // gcc 4.4.1 will accept
1351 // int a7 __attribute__((weakref));
1352 // as
1353 // int a7 __attribute__((weak));
1354 // This looks like a bug in gcc. We reject that for now. We should revisit
1355 // it if this behaviour is actually used.
1356
Rafael Espindolac18086a2010-02-23 22:00:30 +00001357 // GCC rejects
1358 // static ((alias ("y"), weakref)).
1359 // Should we? How to check that weakref is before or after alias?
1360
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001361 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1362 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1363 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001364 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001365 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001366 // GCC will accept anything as the argument of weakref. Should we
1367 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001368 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1369 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001370
Michael Han99315932013-01-24 16:46:58 +00001371 D->addAttr(::new (S.Context)
1372 WeakRefAttr(Attr.getRange(), S.Context,
1373 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001374}
1375
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001376static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1377 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001378 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001379 return;
1380
Douglas Gregore8bbc122011-09-02 00:18:52 +00001381 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001382 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1383 return;
1384 }
1385
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001386 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001387
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001388 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001389 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001390}
1391
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001392static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001393 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001394 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001395
Michael Han99315932013-01-24 16:46:58 +00001396 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1397 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001398}
1399
1400static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001401 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001402 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001403
Michael Han99315932013-01-24 16:46:58 +00001404 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1405 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001406}
1407
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001408static void handleTLSModelAttr(Sema &S, Decl *D,
1409 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001410 StringRef Model;
1411 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001412 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001413 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001414 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001415
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001416 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001417 if (Model != "global-dynamic" && Model != "local-dynamic"
1418 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001419 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001420 return;
1421 }
1422
Michael Han99315932013-01-24 16:46:58 +00001423 D->addAttr(::new (S.Context)
1424 TLSModelAttr(Attr.getRange(), S.Context, Model,
1425 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001426}
1427
Chandler Carruthedc2c642011-07-02 00:01:44 +00001428static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001429 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Alp Toker314cc812014-01-25 16:55:45 +00001430 QualType RetTy = FD->getReturnType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001431 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001432 D->addAttr(::new (S.Context)
1433 MallocAttr(Attr.getRange(), S.Context,
1434 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001435 return;
1436 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001437 }
1438
Ted Kremenek08479ae2009-08-15 00:51:46 +00001439 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001440}
1441
Chandler Carruthedc2c642011-07-02 00:01:44 +00001442static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001443 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001444 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1445 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001446 return;
1447 }
1448
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001449 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1450 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001451}
1452
Chandler Carruthedc2c642011-07-02 00:01:44 +00001453static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001454 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001455
1456 if (S.CheckNoReturnAttr(attr)) return;
1457
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001458 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001459 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001460 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001461 return;
1462 }
1463
Michael Han99315932013-01-24 16:46:58 +00001464 D->addAttr(::new (S.Context)
1465 NoReturnAttr(attr.getRange(), S.Context,
1466 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001467}
1468
1469bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001470 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001471 attr.setInvalid();
1472 return true;
1473 }
1474
1475 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001476}
1477
Chandler Carruthedc2c642011-07-02 00:01:44 +00001478static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1479 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001480
1481 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1482 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001483 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1484 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001485 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1486 && !VD->getType()->isFunctionPointerType())) {
1487 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001488 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001489 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001490 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001491 return;
1492 }
1493 }
1494
Michael Han99315932013-01-24 16:46:58 +00001495 D->addAttr(::new (S.Context)
1496 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1497 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001498}
1499
John Thompsoncdb847ba2010-08-09 21:53:52 +00001500// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001501static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001502/*
1503 Returning a Vector Class in Registers
1504
Eric Christopherbc638a82010-12-01 22:13:54 +00001505 According to the PPU ABI specifications, a class with a single member of
1506 vector type is returned in memory when used as the return value of a function.
1507 This results in inefficient code when implementing vector classes. To return
1508 the value in a single vector register, add the vecreturn attribute to the
1509 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001510
1511 Example:
1512
1513 struct Vector
1514 {
1515 __vector float xyzw;
1516 } __attribute__((vecreturn));
1517
1518 Vector Add(Vector lhs, Vector rhs)
1519 {
1520 Vector result;
1521 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1522 return result; // This will be returned in a register
1523 }
1524*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001525 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1526 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001527 return;
1528 }
1529
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001530 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001531 int count = 0;
1532
1533 if (!isa<CXXRecordDecl>(record)) {
1534 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1535 return;
1536 }
1537
1538 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1539 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1540 return;
1541 }
1542
Eric Christopherbc638a82010-12-01 22:13:54 +00001543 for (RecordDecl::field_iterator iter = record->field_begin();
1544 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001545 if ((count == 1) || !iter->getType()->isVectorType()) {
1546 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1547 return;
1548 }
1549 count++;
1550 }
1551
Michael Han99315932013-01-24 16:46:58 +00001552 D->addAttr(::new (S.Context)
1553 VecReturnAttr(Attr.getRange(), S.Context,
1554 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001555}
1556
Richard Smithe233fbf2013-01-28 22:42:45 +00001557static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1558 const AttributeList &Attr) {
1559 if (isa<ParmVarDecl>(D)) {
1560 // [[carries_dependency]] can only be applied to a parameter if it is a
1561 // parameter of a function declaration or lambda.
1562 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1563 S.Diag(Attr.getLoc(),
1564 diag::err_carries_dependency_param_not_function_decl);
1565 return;
1566 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001567 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001568
1569 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1570 Attr.getRange(), S.Context,
1571 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001572}
1573
Chandler Carruthedc2c642011-07-02 00:01:44 +00001574static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001575 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001576 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001577 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001578 return;
1579 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001580 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001581 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001582 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001583 return;
1584 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001585
Michael Han99315932013-01-24 16:46:58 +00001586 D->addAttr(::new (S.Context)
1587 UsedAttr(Attr.getRange(), S.Context,
1588 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001589}
1590
Chandler Carruthedc2c642011-07-02 00:01:44 +00001591static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001592 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001593 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001594 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1595 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001596 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001597 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001598
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001599 uint32_t priority = ConstructorAttr::DefaultPriority;
1600 if (Attr.getNumArgs() > 0 &&
1601 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1602 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001603
Michael Han99315932013-01-24 16:46:58 +00001604 D->addAttr(::new (S.Context)
1605 ConstructorAttr(Attr.getRange(), S.Context, priority,
1606 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001607}
1608
Chandler Carruthedc2c642011-07-02 00:01:44 +00001609static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001610 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001611 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001612 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1613 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001614 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001615 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001616
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001617 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001618 if (Attr.getNumArgs() > 0 &&
1619 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1620 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001621
Michael Han99315932013-01-24 16:46:58 +00001622 D->addAttr(::new (S.Context)
1623 DestructorAttr(Attr.getRange(), S.Context, priority,
1624 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001625}
1626
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001627template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001628static void handleAttrWithMessage(Sema &S, Decl *D,
1629 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001630 unsigned NumArgs = Attr.getNumArgs();
1631 if (NumArgs > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001632 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1633 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001634 return;
1635 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001636
1637 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001638 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001639 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001640 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001641
Michael Han99315932013-01-24 16:46:58 +00001642 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1643 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001644}
1645
Ted Kremenek28eace62013-11-23 01:01:34 +00001646static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
1647 const AttributeList &Attr) {
Ted Kremenek28eace62013-11-23 01:01:34 +00001648 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001649 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1650 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001651}
1652
Jordy Rose740b0c22012-05-08 03:27:22 +00001653static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1654 IdentifierInfo *Platform,
1655 VersionTuple Introduced,
1656 VersionTuple Deprecated,
1657 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001658 StringRef PlatformName
1659 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1660 if (PlatformName.empty())
1661 PlatformName = Platform->getName();
1662
1663 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1664 // of these steps are needed).
1665 if (!Introduced.empty() && !Deprecated.empty() &&
1666 !(Introduced <= Deprecated)) {
1667 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1668 << 1 << PlatformName << Deprecated.getAsString()
1669 << 0 << Introduced.getAsString();
1670 return true;
1671 }
1672
1673 if (!Introduced.empty() && !Obsoleted.empty() &&
1674 !(Introduced <= Obsoleted)) {
1675 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1676 << 2 << PlatformName << Obsoleted.getAsString()
1677 << 0 << Introduced.getAsString();
1678 return true;
1679 }
1680
1681 if (!Deprecated.empty() && !Obsoleted.empty() &&
1682 !(Deprecated <= Obsoleted)) {
1683 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1684 << 2 << PlatformName << Obsoleted.getAsString()
1685 << 1 << Deprecated.getAsString();
1686 return true;
1687 }
1688
1689 return false;
1690}
1691
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001692/// \brief Check whether the two versions match.
1693///
1694/// If either version tuple is empty, then they are assumed to match. If
1695/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1696static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1697 bool BeforeIsOkay) {
1698 if (X.empty() || Y.empty())
1699 return true;
1700
1701 if (X == Y)
1702 return true;
1703
1704 if (BeforeIsOkay && X < Y)
1705 return true;
1706
1707 return false;
1708}
1709
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001710AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001711 IdentifierInfo *Platform,
1712 VersionTuple Introduced,
1713 VersionTuple Deprecated,
1714 VersionTuple Obsoleted,
1715 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001716 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001717 bool Override,
1718 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001719 VersionTuple MergedIntroduced = Introduced;
1720 VersionTuple MergedDeprecated = Deprecated;
1721 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001722 bool FoundAny = false;
1723
Rafael Espindolac67f2232012-05-10 02:50:16 +00001724 if (D->hasAttrs()) {
1725 AttrVec &Attrs = D->getAttrs();
1726 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1727 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1728 if (!OldAA) {
1729 ++i;
1730 continue;
1731 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001732
Rafael Espindolac67f2232012-05-10 02:50:16 +00001733 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1734 if (OldPlatform != Platform) {
1735 ++i;
1736 continue;
1737 }
1738
1739 FoundAny = true;
1740 VersionTuple OldIntroduced = OldAA->getIntroduced();
1741 VersionTuple OldDeprecated = OldAA->getDeprecated();
1742 VersionTuple OldObsoleted = OldAA->getObsoleted();
1743 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001744
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001745 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1746 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1747 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1748 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001749 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001750 if (Override) {
1751 int Which = -1;
1752 VersionTuple FirstVersion;
1753 VersionTuple SecondVersion;
1754 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1755 Which = 0;
1756 FirstVersion = OldIntroduced;
1757 SecondVersion = Introduced;
1758 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1759 Which = 1;
1760 FirstVersion = Deprecated;
1761 SecondVersion = OldDeprecated;
1762 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1763 Which = 2;
1764 FirstVersion = Obsoleted;
1765 SecondVersion = OldObsoleted;
1766 }
1767
1768 if (Which == -1) {
1769 Diag(OldAA->getLocation(),
1770 diag::warn_mismatched_availability_override_unavail)
1771 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1772 } else {
1773 Diag(OldAA->getLocation(),
1774 diag::warn_mismatched_availability_override)
1775 << Which
1776 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1777 << FirstVersion.getAsString() << SecondVersion.getAsString();
1778 }
1779 Diag(Range.getBegin(), diag::note_overridden_method);
1780 } else {
1781 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1782 Diag(Range.getBegin(), diag::note_previous_attribute);
1783 }
1784
Rafael Espindolac67f2232012-05-10 02:50:16 +00001785 Attrs.erase(Attrs.begin() + i);
1786 --e;
1787 continue;
1788 }
1789
1790 VersionTuple MergedIntroduced2 = MergedIntroduced;
1791 VersionTuple MergedDeprecated2 = MergedDeprecated;
1792 VersionTuple MergedObsoleted2 = MergedObsoleted;
1793
1794 if (MergedIntroduced2.empty())
1795 MergedIntroduced2 = OldIntroduced;
1796 if (MergedDeprecated2.empty())
1797 MergedDeprecated2 = OldDeprecated;
1798 if (MergedObsoleted2.empty())
1799 MergedObsoleted2 = OldObsoleted;
1800
1801 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1802 MergedIntroduced2, MergedDeprecated2,
1803 MergedObsoleted2)) {
1804 Attrs.erase(Attrs.begin() + i);
1805 --e;
1806 continue;
1807 }
1808
1809 MergedIntroduced = MergedIntroduced2;
1810 MergedDeprecated = MergedDeprecated2;
1811 MergedObsoleted = MergedObsoleted2;
1812 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001813 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001814 }
1815
1816 if (FoundAny &&
1817 MergedIntroduced == Introduced &&
1818 MergedDeprecated == Deprecated &&
1819 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001820 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001821
Ted Kremenekb5445722013-04-06 00:34:27 +00001822 // Only create a new attribute if !Override, but we want to do
1823 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001824 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001825 MergedDeprecated, MergedObsoleted) &&
1826 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001827 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1828 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001829 Obsoleted, IsUnavailable, Message,
1830 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001831 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001832 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001833}
1834
Chandler Carruthedc2c642011-07-02 00:01:44 +00001835static void handleAvailabilityAttr(Sema &S, Decl *D,
1836 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001837 if (!checkAttributeNumArgs(S, Attr, 1))
1838 return;
1839 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001840 unsigned Index = Attr.getAttributeSpellingListIndex();
1841
Aaron Ballman00e99962013-08-31 01:11:41 +00001842 IdentifierInfo *II = Platform->Ident;
1843 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1844 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1845 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001846
Rafael Espindolac231fab2013-01-08 21:30:32 +00001847 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1848 if (!ND) {
1849 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1850 return;
1851 }
1852
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001853 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1854 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1855 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001856 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001857 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001858 if (const StringLiteral *SE =
1859 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001860 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001861
Aaron Ballman00e99962013-08-31 01:11:41 +00001862 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001863 Introduced.Version,
1864 Deprecated.Version,
1865 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001866 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001867 /*Override=*/false,
1868 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001869 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001870 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001871}
1872
John McCalld041a9b2013-02-20 01:54:26 +00001873template <class T>
1874static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1875 typename T::VisibilityType value,
1876 unsigned attrSpellingListIndex) {
1877 T *existingAttr = D->getAttr<T>();
1878 if (existingAttr) {
1879 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1880 if (existingValue == value)
1881 return NULL;
1882 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1883 S.Diag(range.getBegin(), diag::note_previous_attribute);
1884 D->dropAttr<T>();
1885 }
1886 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1887}
1888
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001889VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001890 VisibilityAttr::VisibilityType Vis,
1891 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001892 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1893 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001894}
1895
John McCalld041a9b2013-02-20 01:54:26 +00001896TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1897 TypeVisibilityAttr::VisibilityType Vis,
1898 unsigned AttrSpellingListIndex) {
1899 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1900 AttrSpellingListIndex);
1901}
1902
1903static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1904 bool isTypeVisibility) {
1905 // Visibility attributes don't mean anything on a typedef.
1906 if (isa<TypedefNameDecl>(D)) {
1907 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
1908 << Attr.getName();
1909 return;
1910 }
1911
1912 // 'type_visibility' can only go on a type or namespace.
1913 if (isTypeVisibility &&
1914 !(isa<TagDecl>(D) ||
1915 isa<ObjCInterfaceDecl>(D) ||
1916 isa<NamespaceDecl>(D))) {
1917 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
1918 << Attr.getName() << ExpectedTypeOrNamespace;
1919 return;
1920 }
1921
Benjamin Kramer70370212013-09-09 15:08:57 +00001922 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001923 StringRef TypeStr;
1924 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001925 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001926 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001927
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001928 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00001929 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001930 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00001931 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001932 return;
1933 }
Aaron Ballman682ee422013-09-11 19:47:58 +00001934
1935 // Complain about attempts to use protected visibility on targets
1936 // (like Darwin) that don't support it.
1937 if (type == VisibilityAttr::Protected &&
1938 !S.Context.getTargetInfo().hasProtectedVisibility()) {
1939 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1940 type = VisibilityAttr::Default;
1941 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001942
Michael Han99315932013-01-24 16:46:58 +00001943 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00001944 clang::Attr *newAttr;
1945 if (isTypeVisibility) {
1946 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
1947 (TypeVisibilityAttr::VisibilityType) type,
1948 Index);
1949 } else {
1950 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
1951 }
1952 if (newAttr)
1953 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001954}
1955
Chandler Carruthedc2c642011-07-02 00:01:44 +00001956static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1957 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001958 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00001959 if (!Attr.isArgIdent(0)) {
1960 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1961 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00001962 return;
1963 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001964
Aaron Ballman682ee422013-09-11 19:47:58 +00001965 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1966 ObjCMethodFamilyAttr::FamilyKind F;
1967 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
1968 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
1969 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00001970 return;
1971 }
1972
Alp Toker314cc812014-01-25 16:55:45 +00001973 if (F == ObjCMethodFamilyAttr::OMF_init &&
1974 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00001975 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00001976 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00001977 // Ignore the attribute.
1978 return;
1979 }
1980
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001981 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00001982 S.Context, F,
1983 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00001984}
1985
Chandler Carruthedc2c642011-07-02 00:01:44 +00001986static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00001987 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001988 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00001989 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001990 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1991 return;
1992 }
1993 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00001994 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1995 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00001996 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00001997 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
1998 return;
1999 }
2000 }
2001 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002002 // It is okay to include this attribute on properties, e.g.:
2003 //
2004 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2005 //
2006 // In this case it follows tradition and suppresses an error in the above
2007 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002008 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002009 }
Michael Han99315932013-01-24 16:46:58 +00002010 D->addAttr(::new (S.Context)
2011 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2012 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002013}
2014
Chandler Carruthedc2c642011-07-02 00:01:44 +00002015static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002016 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002017 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002018 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002019 return;
2020 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002021
Aaron Ballman00e99962013-08-31 01:11:41 +00002022 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002023 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002024 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2025 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2026 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002027 return;
2028 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002029
Michael Han99315932013-01-24 16:46:58 +00002030 D->addAttr(::new (S.Context)
2031 BlocksAttr(Attr.getRange(), S.Context, type,
2032 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002033}
2034
Chandler Carruthedc2c642011-07-02 00:01:44 +00002035static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002036 // check the attribute arguments.
2037 if (Attr.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00002038 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
2039 << Attr.getName() << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002040 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002041 }
2042
Aaron Ballman18a78382013-11-21 00:28:23 +00002043 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002044 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002045 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002046 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002047 if (E->isTypeDependent() || E->isValueDependent() ||
2048 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002049 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002050 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002051 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002052 return;
2053 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002054
John McCallb46f2872011-09-09 07:56:05 +00002055 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002056 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2057 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002058 return;
2059 }
John McCallb46f2872011-09-09 07:56:05 +00002060
2061 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002062 }
2063
Aaron Ballman18a78382013-11-21 00:28:23 +00002064 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002065 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002066 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002067 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002068 if (E->isTypeDependent() || E->isValueDependent() ||
2069 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002070 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002071 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002072 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002073 return;
2074 }
2075 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002076
John McCallb46f2872011-09-09 07:56:05 +00002077 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002078 // FIXME: This error message could be improved, it would be nice
2079 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002080 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2081 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002082 return;
2083 }
2084 }
2085
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002086 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002087 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002088 if (isa<FunctionNoProtoType>(FT)) {
2089 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2090 return;
2091 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002092
Chris Lattner9363e312009-03-17 23:03:47 +00002093 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002094 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002095 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002096 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002097 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002098 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002099 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002100 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002101 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002102 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2103 if (!BD->isVariadic()) {
2104 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2105 return;
2106 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002107 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002108 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002109 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002110 const FunctionType *FT = Ty->isFunctionPointerType()
2111 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002112 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002113 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002114 int m = Ty->isFunctionPointerType() ? 0 : 1;
2115 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002116 return;
2117 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002118 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002119 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002120 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002121 return;
2122 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002123 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002124 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002125 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002126 return;
2127 }
Michael Han99315932013-01-24 16:46:58 +00002128 D->addAttr(::new (S.Context)
2129 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2130 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002131}
2132
Chandler Carruthedc2c642011-07-02 00:01:44 +00002133static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002134 if (D->getFunctionType() &&
2135 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002136 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2137 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002138 return;
2139 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002140 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002141 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002142 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2143 << Attr.getName() << 1;
2144 return;
2145 }
2146
Michael Han99315932013-01-24 16:46:58 +00002147 D->addAttr(::new (S.Context)
2148 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2149 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002150}
2151
Chandler Carruthedc2c642011-07-02 00:01:44 +00002152static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002153 // weak_import only applies to variable & function declarations.
2154 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002155 if (!D->canBeWeakImported(isDef)) {
2156 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002157 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2158 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002159 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002160 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002161 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002162 // Nothing to warn about here.
2163 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002164 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002165 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002166
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002167 return;
2168 }
2169
Michael Han99315932013-01-24 16:46:58 +00002170 D->addAttr(::new (S.Context)
2171 WeakImportAttr(Attr.getRange(), S.Context,
2172 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002173}
2174
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002175// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002176template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002177static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002178 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002179 uint32_t WGSize[3];
2180 for (unsigned i = 0; i < 3; ++i)
2181 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002182 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002183
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002184 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2185 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2186 Existing->getYDim() == WGSize[1] &&
2187 Existing->getZDim() == WGSize[2]))
2188 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002189
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002190 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2191 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002192 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002193}
2194
Joey Goulyaba589c2013-03-08 09:42:32 +00002195static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002196 if (!Attr.hasParsedType()) {
2197 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2198 << Attr.getName() << 1;
2199 return;
2200 }
2201
Richard Smithb87c4652013-10-31 21:23:20 +00002202 TypeSourceInfo *ParmTSI = 0;
2203 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2204 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002205
2206 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2207 (ParmType->isBooleanType() ||
2208 !ParmType->isIntegralType(S.getASTContext()))) {
2209 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2210 << ParmType;
2211 return;
2212 }
2213
Aaron Ballmana9e05402013-12-02 22:16:55 +00002214 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002215 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002216 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2217 return;
2218 }
2219 }
2220
2221 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002222 ParmTSI,
2223 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002224}
2225
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002226SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002227 StringRef Name,
2228 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002229 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2230 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002231 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002232 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2233 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002234 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002235 }
Michael Han99315932013-01-24 16:46:58 +00002236 return ::new (Context) SectionAttr(Range, Context, Name,
2237 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002238}
2239
Chandler Carruthedc2c642011-07-02 00:01:44 +00002240static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002241 // Make sure that there is a string literal as the sections's single
2242 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002243 StringRef Str;
2244 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002245 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002246 return;
Mike Stump11289f42009-09-09 15:08:12 +00002247
Chris Lattner30ba6742009-08-10 19:03:04 +00002248 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002249 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002250 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002251 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002252 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002253 return;
2254 }
Mike Stump11289f42009-09-09 15:08:12 +00002255
Michael Han99315932013-01-24 16:46:58 +00002256 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002257 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002258 if (NewAttr)
2259 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002260}
2261
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002262
Chandler Carruthedc2c642011-07-02 00:01:44 +00002263static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002264 VarDecl *VD = cast<VarDecl>(D);
2265 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002266 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002267 return;
2268 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002269
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002270 Expr *E = Attr.getArgAsExpr(0);
2271 SourceLocation Loc = E->getExprLoc();
2272 FunctionDecl *FD = 0;
2273 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002274
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002275 // gcc only allows for simple identifiers. Since we support more than gcc, we
2276 // will warn the user.
2277 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2278 if (DRE->hasQualifier())
2279 S.Diag(Loc, diag::warn_cleanup_ext);
2280 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2281 NI = DRE->getNameInfo();
2282 if (!FD) {
2283 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2284 << NI.getName();
2285 return;
2286 }
2287 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2288 if (ULE->hasExplicitTemplateArgs())
2289 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002290 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2291 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002292 if (!FD) {
2293 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2294 << NI.getName();
2295 if (ULE->getType() == S.Context.OverloadTy)
2296 S.NoteAllOverloadCandidates(ULE);
2297 return;
2298 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002299 } else {
2300 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002301 return;
2302 }
2303
Anders Carlssond277d792009-01-31 01:16:18 +00002304 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002305 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2306 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002307 return;
2308 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002309
Anders Carlsson723f55d2009-02-07 23:16:50 +00002310 // We're currently more strict than GCC about what function types we accept.
2311 // If this ever proves to be a problem it should be easy to fix.
2312 QualType Ty = S.Context.getPointerType(VD->getType());
2313 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002314 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2315 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002316 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2317 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002318 return;
2319 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002320
Michael Han99315932013-01-24 16:46:58 +00002321 D->addAttr(::new (S.Context)
2322 CleanupAttr(Attr.getRange(), S.Context, FD,
2323 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002324}
2325
Mike Stumpd3bb5572009-07-24 19:02:52 +00002326/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002327/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002328static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002329 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002330 uint64_t Idx;
2331 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002332 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002333
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002334 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002335 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002336
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002337 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2338 if (not_nsstring_type &&
2339 !isCFStringType(Ty, S.Context) &&
2340 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002341 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002342 // FIXME: Should highlight the actual expression that has the wrong type.
2343 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002344 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002345 << IdxExpr->getSourceRange();
2346 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002347 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002348 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002349 if (!isNSStringType(Ty, S.Context) &&
2350 !isCFStringType(Ty, S.Context) &&
2351 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002352 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002353 // FIXME: Should highlight the actual expression that has the wrong type.
2354 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002355 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002356 << IdxExpr->getSourceRange();
2357 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002358 }
2359
Alp Toker601b22c2014-01-21 23:35:24 +00002360 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002361 // because that has corrected for the implicit this parameter, and is zero-
2362 // based. The attribute expects what the user wrote explicitly.
2363 llvm::APSInt Val;
2364 IdxExpr->EvaluateAsInt(Val, S.Context);
2365
Michael Han99315932013-01-24 16:46:58 +00002366 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002367 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002368 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002369}
2370
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002371enum FormatAttrKind {
2372 CFStringFormat,
2373 NSStringFormat,
2374 StrftimeFormat,
2375 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002376 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002377 InvalidFormat
2378};
2379
2380/// getFormatAttrKind - Map from format attribute names to supported format
2381/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002382static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002383 return llvm::StringSwitch<FormatAttrKind>(Format)
2384 // Check for formats that get handled specially.
2385 .Case("NSString", NSStringFormat)
2386 .Case("CFString", CFStringFormat)
2387 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002388
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002389 // Otherwise, check for supported formats.
2390 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2391 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2392 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002393
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002394 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2395 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002396}
2397
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002398/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002399/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002400static void handleInitPriorityAttr(Sema &S, Decl *D,
2401 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002402 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002403 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2404 return;
2405 }
2406
Aaron Ballman4a611152013-11-27 16:34:09 +00002407 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002408 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2409 Attr.setInvalid();
2410 return;
2411 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002412 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002413 if (S.Context.getAsArrayType(T))
2414 T = S.Context.getBaseElementType(T);
2415 if (!T->getAs<RecordType>()) {
2416 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2417 Attr.setInvalid();
2418 return;
2419 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002420
2421 Expr *E = Attr.getArgAsExpr(0);
2422 uint32_t prioritynum;
2423 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002424 Attr.setInvalid();
2425 return;
2426 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002427
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002428 if (prioritynum < 101 || prioritynum > 65535) {
2429 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002430 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002431 Attr.setInvalid();
2432 return;
2433 }
Michael Han99315932013-01-24 16:46:58 +00002434 D->addAttr(::new (S.Context)
2435 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2436 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002437}
2438
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002439FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2440 IdentifierInfo *Format, int FormatIdx,
2441 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002442 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002443 // Check whether we already have an equivalent format attribute.
2444 for (specific_attr_iterator<FormatAttr>
2445 i = D->specific_attr_begin<FormatAttr>(),
2446 e = D->specific_attr_end<FormatAttr>();
2447 i != e ; ++i) {
2448 FormatAttr *f = *i;
2449 if (f->getType() == Format &&
2450 f->getFormatIdx() == FormatIdx &&
2451 f->getFirstArg() == FirstArg) {
2452 // If we don't have a valid location for this attribute, adopt the
2453 // location.
2454 if (f->getLocation().isInvalid())
2455 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002456 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002457 }
2458 }
2459
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002460 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2461 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002462}
2463
Mike Stumpd3bb5572009-07-24 19:02:52 +00002464/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002465/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002466static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002467 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002468 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002469 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002470 return;
2471 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002472
Chandler Carruth743682b2010-11-16 08:35:43 +00002473 // In C++ the implicit 'this' function parameter also counts, and they are
2474 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002475 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002476 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002477
Aaron Ballman00e99962013-08-31 01:11:41 +00002478 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2479 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002480
2481 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002482 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002483 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002484 // If we've modified the string name, we need a new identifier for it.
2485 II = &S.Context.Idents.get(Format);
2486 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002487
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002488 // Check for supported formats.
2489 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002490
2491 if (Kind == IgnoredFormat)
2492 return;
2493
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002494 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002495 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002496 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002497 return;
2498 }
2499
2500 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002501 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002502 uint32_t Idx;
2503 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002504 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002505
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002506 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002507 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002508 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002509 return;
2510 }
2511
2512 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002513 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002514
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002515 if (HasImplicitThisParam) {
2516 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002517 S.Diag(Attr.getLoc(),
2518 diag::err_format_attribute_implicit_this_format_string)
2519 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002520 return;
2521 }
2522 ArgIdx--;
2523 }
Mike Stump11289f42009-09-09 15:08:12 +00002524
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002525 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002526 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002527
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002528 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002529 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002530 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2531 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002532 return;
2533 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002534 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002535 // FIXME: do we need to check if the type is NSString*? What are the
2536 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002537 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002538 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002539 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2540 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002541 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002542 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002543 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002544 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002545 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002546 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2547 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002548 return;
2549 }
2550
2551 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002552 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002553 uint32_t FirstArg;
2554 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002555 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002556
2557 // check if the function is variadic if the 3rd argument non-zero
2558 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002559 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002560 ++NumArgs; // +1 for ...
2561 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002562 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002563 return;
2564 }
2565 }
2566
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002567 // strftime requires FirstArg to be 0 because it doesn't read from any
2568 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002569 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002570 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002571 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2572 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002573 return;
2574 }
2575 // if 0 it disables parameter checking (to use with e.g. va_list)
2576 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002577 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002578 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002579 return;
2580 }
2581
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002582 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002583 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002584 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002585 if (NewAttr)
2586 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002587}
2588
Chandler Carruthedc2c642011-07-02 00:01:44 +00002589static void handleTransparentUnionAttr(Sema &S, Decl *D,
2590 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002591 // Try to find the underlying union declaration.
2592 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002593 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002594 if (TD && TD->getUnderlyingType()->isUnionType())
2595 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2596 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002597 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002598
2599 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002600 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002601 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002602 return;
2603 }
2604
John McCallf937c022011-10-07 06:10:15 +00002605 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002606 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002607 diag::warn_transparent_union_attribute_not_definition);
2608 return;
2609 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002610
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002611 RecordDecl::field_iterator Field = RD->field_begin(),
2612 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002613 if (Field == FieldEnd) {
2614 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2615 return;
2616 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002617
David Blaikie40ed2972012-06-06 20:45:41 +00002618 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002619 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002620 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002621 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002622 diag::warn_transparent_union_attribute_floating)
2623 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002624 return;
2625 }
2626
2627 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2628 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2629 for (; Field != FieldEnd; ++Field) {
2630 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002631 // FIXME: this isn't fully correct; we also need to test whether the
2632 // members of the union would all have the same calling convention as the
2633 // first member of the union. Checking just the size and alignment isn't
2634 // sufficient (consider structs passed on the stack instead of in registers
2635 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002636 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002637 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002638 // Warn if we drop the attribute.
2639 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002640 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002641 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002642 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002643 diag::warn_transparent_union_attribute_field_size_align)
2644 << isSize << Field->getDeclName() << FieldBits;
2645 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002646 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002647 diag::note_transparent_union_first_field_size_align)
2648 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002649 return;
2650 }
2651 }
2652
Michael Han99315932013-01-24 16:46:58 +00002653 RD->addAttr(::new (S.Context)
2654 TransparentUnionAttr(Attr.getRange(), S.Context,
2655 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002656}
2657
Chandler Carruthedc2c642011-07-02 00:01:44 +00002658static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002659 // Make sure that there is a string literal as the annotation's single
2660 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002661 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002662 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002663 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002664
2665 // Don't duplicate annotations that are already set.
2666 for (specific_attr_iterator<AnnotateAttr>
2667 i = D->specific_attr_begin<AnnotateAttr>(),
2668 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002669 if ((*i)->getAnnotation() == Str)
2670 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002671 }
Michael Han99315932013-01-24 16:46:58 +00002672
2673 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002674 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002675 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002676}
2677
Chandler Carruthedc2c642011-07-02 00:01:44 +00002678static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002679 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002680 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002681 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2682 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002683 return;
2684 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002685
Richard Smith848e1f12013-02-01 08:12:08 +00002686 if (Attr.getNumArgs() == 0) {
2687 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2688 true, 0, Attr.getAttributeSpellingListIndex()));
2689 return;
2690 }
2691
Aaron Ballman00e99962013-08-31 01:11:41 +00002692 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002693 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2694 S.Diag(Attr.getEllipsisLoc(),
2695 diag::err_pack_expansion_without_parameter_packs);
2696 return;
2697 }
2698
2699 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2700 return;
2701
2702 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2703 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002704}
2705
2706void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002707 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002708 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2709 SourceLocation AttrLoc = AttrRange.getBegin();
2710
Richard Smith1dba27c2013-01-29 09:02:09 +00002711 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002712 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002713 // C++11 [dcl.align]p1:
2714 // An alignment-specifier may be applied to a variable or to a class
2715 // data member, but it shall not be applied to a bit-field, a function
2716 // parameter, the formal parameter of a catch clause, or a variable
2717 // declared with the register storage class specifier. An
2718 // alignment-specifier may also be applied to the declaration of a class
2719 // or enumeration type.
2720 // C11 6.7.5/2:
2721 // An alignment attribute shall not be specified in a declaration of
2722 // a typedef, or a bit-field, or a function, or a parameter, or an
2723 // object declared with the register storage-class specifier.
2724 int DiagKind = -1;
2725 if (isa<ParmVarDecl>(D)) {
2726 DiagKind = 0;
2727 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2728 if (VD->getStorageClass() == SC_Register)
2729 DiagKind = 1;
2730 if (VD->isExceptionVariable())
2731 DiagKind = 2;
2732 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2733 if (FD->isBitField())
2734 DiagKind = 3;
2735 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00002736 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00002737 << (TmpAttr.isC11() ? ExpectedVariableOrField
2738 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002739 return;
2740 }
2741 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002742 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00002743 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002744 return;
2745 }
2746 }
2747
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002748 if (E->isTypeDependent() || E->isValueDependent()) {
2749 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002750 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2751 AA->setPackExpansion(IsPackExpansion);
2752 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002753 return;
2754 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002755
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002756 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002757 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002758 ExprResult ICE
2759 = VerifyIntegerConstantExpression(E, &Alignment,
2760 diag::err_aligned_attribute_argument_not_int,
2761 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002762 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002763 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002764
2765 // C++11 [dcl.align]p2:
2766 // -- if the constant expression evaluates to zero, the alignment
2767 // specifier shall have no effect
2768 // C11 6.7.5p6:
2769 // An alignment specification of zero has no effect.
2770 if (!(TmpAttr.isAlignas() && !Alignment) &&
2771 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002772 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2773 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002774 return;
2775 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002776
David Majnemerabecae72014-02-12 20:36:10 +00002777 // Alignment calculations can wrap around if it's greater than 2**28.
2778 unsigned MaxValidAlignment = TmpAttr.isDeclspec() ? 8192 : 268435456;
2779 if (Alignment.getZExtValue() > MaxValidAlignment) {
2780 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
2781 << E->getSourceRange();
2782 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00002783 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002784
Richard Smith44c247f2013-02-22 08:32:16 +00002785 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2786 ICE.take(), SpellingListIndex);
2787 AA->setPackExpansion(IsPackExpansion);
2788 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002789}
2790
Michael Hanaf02bbe2013-02-01 01:19:17 +00002791void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002792 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002793 // FIXME: Cache the number on the Attr object if non-dependent?
2794 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002795 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2796 SpellingListIndex);
2797 AA->setPackExpansion(IsPackExpansion);
2798 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002799}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002800
Richard Smith848e1f12013-02-01 08:12:08 +00002801void Sema::CheckAlignasUnderalignment(Decl *D) {
2802 assert(D->hasAttrs() && "no attributes on decl");
2803
2804 QualType Ty;
2805 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2806 Ty = VD->getType();
2807 else
2808 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002809 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002810 return;
2811
2812 // C++11 [dcl.align]p5, C11 6.7.5/4:
2813 // The combined effect of all alignment attributes in a declaration shall
2814 // not specify an alignment that is less strict than the alignment that
2815 // would otherwise be required for the entity being declared.
2816 AlignedAttr *AlignasAttr = 0;
2817 unsigned Align = 0;
2818 for (specific_attr_iterator<AlignedAttr>
2819 I = D->specific_attr_begin<AlignedAttr>(),
2820 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
2821 if (I->isAlignmentDependent())
2822 return;
2823 if (I->isAlignas())
2824 AlignasAttr = *I;
2825 Align = std::max(Align, I->getAlignment(Context));
2826 }
2827
2828 if (AlignasAttr && Align) {
2829 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2830 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2831 if (NaturalAlign > RequestedAlign)
2832 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2833 << Ty << (unsigned)NaturalAlign.getQuantity();
2834 }
2835}
2836
David Majnemer2c4e00a2014-01-29 22:07:36 +00002837bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00002838 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00002839 MSInheritanceAttr::Spelling SemanticSpelling) {
2840 assert(RD->hasDefinition() && "RD has no definition!");
2841
David Majnemer98c9ee22014-02-07 00:43:07 +00002842 // We may not have seen base specifiers or any virtual methods yet. We will
2843 // have to wait until the record is defined to catch any mismatches.
2844 if (!RD->getDefinition()->isCompleteDefinition())
2845 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002846
David Majnemer98c9ee22014-02-07 00:43:07 +00002847 // The unspecified model never matches what a definition could need.
2848 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
2849 return false;
2850
David Majnemer4bb09802014-02-10 19:50:15 +00002851 if (BestCase) {
2852 if (RD->calculateInheritanceModel() == SemanticSpelling)
2853 return false;
2854 } else {
2855 if (RD->calculateInheritanceModel() <= SemanticSpelling)
2856 return false;
2857 }
David Majnemer98c9ee22014-02-07 00:43:07 +00002858
2859 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
2860 << 0 /*definition*/;
2861 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
2862 << RD->getNameAsString();
2863 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002864}
2865
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002866/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002867/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002868///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002869/// Despite what would be logical, the mode attribute is a decl attribute, not a
2870/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2871/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002872static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002873 // This attribute isn't documented, but glibc uses it. It changes
2874 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002875 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002876 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2877 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002878 return;
2879 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002880
Aaron Ballman00e99962013-08-31 01:11:41 +00002881 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2882 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002883
2884 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002885 if (Str.startswith("__") && Str.endswith("__"))
2886 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002887
2888 unsigned DestWidth = 0;
2889 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002890 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002891 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002892 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002893 switch (Str[0]) {
2894 case 'Q': DestWidth = 8; break;
2895 case 'H': DestWidth = 16; break;
2896 case 'S': DestWidth = 32; break;
2897 case 'D': DestWidth = 64; break;
2898 case 'X': DestWidth = 96; break;
2899 case 'T': DestWidth = 128; break;
2900 }
2901 if (Str[1] == 'F') {
2902 IntegerMode = false;
2903 } else if (Str[1] == 'C') {
2904 IntegerMode = false;
2905 ComplexMode = true;
2906 } else if (Str[1] != 'I') {
2907 DestWidth = 0;
2908 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002909 break;
2910 case 4:
2911 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2912 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002913 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002914 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002915 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002916 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002917 break;
2918 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002919 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002920 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002921 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002922 case 11:
2923 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00002924 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002925 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002926 }
2927
2928 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002929 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002930 OldTy = TD->getUnderlyingType();
2931 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2932 OldTy = VD->getType();
2933 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002934 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00002935 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002936 return;
2937 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002938
John McCall9dd450b2009-09-21 23:43:11 +00002939 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002940 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2941 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002942 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002943 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2944 } else if (ComplexMode) {
2945 if (!OldTy->isComplexType())
2946 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2947 } else {
2948 if (!OldTy->isFloatingType())
2949 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2950 }
2951
Mike Stump87c57ac2009-05-16 07:39:55 +00002952 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2953 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002954 // FIXME: Make sure floating-point mappings are accurate
2955 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002956 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00002957 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002958 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002959 }
2960
2961 QualType NewTy;
2962
2963 if (IntegerMode)
2964 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
2965 OldTy->isSignedIntegerType());
2966 else
2967 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
2968
2969 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00002970 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002971 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002972 }
2973
Eli Friedman4735374e2009-03-03 06:41:03 +00002974 if (ComplexMode) {
2975 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002976 }
2977
2978 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002979 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
2980 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
2981 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00002982 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002983
2984 D->addAttr(::new (S.Context)
2985 ModeAttr(Attr.getRange(), S.Context, Name,
2986 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00002987}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002988
Chandler Carruthedc2c642011-07-02 00:01:44 +00002989static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00002990 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2991 if (!VD->hasGlobalStorage())
2992 S.Diag(Attr.getLoc(),
2993 diag::warn_attribute_requires_functions_or_static_globals)
2994 << Attr.getName();
2995 } else if (!isFunctionOrMethod(D)) {
2996 S.Diag(Attr.getLoc(),
2997 diag::warn_attribute_requires_functions_or_static_globals)
2998 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00002999 return;
3000 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003001
Michael Han99315932013-01-24 16:46:58 +00003002 D->addAttr(::new (S.Context)
3003 NoDebugAttr(Attr.getRange(), S.Context,
3004 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003005}
3006
Chandler Carruthedc2c642011-07-02 00:01:44 +00003007static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003008 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003009 if (!FD->getReturnType()->isVoidType()) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003010 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
3011 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
3012 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3013 << FD->getType()
Alp Toker42a16a62014-01-25 23:51:36 +00003014 << FixItHint::CreateReplacement(FTL.getReturnLoc().getSourceRange(),
Aaron Ballman3aff6332013-12-02 19:30:36 +00003015 "void");
3016 } else {
3017 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3018 << FD->getType();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003019 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003020 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003021 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003022
Aaron Ballman3aff6332013-12-02 19:30:36 +00003023 D->addAttr(::new (S.Context)
3024 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003025 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003026}
3027
Chandler Carruthedc2c642011-07-02 00:01:44 +00003028static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003029 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003030 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003031 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003032 return;
3033 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003034
Michael Han99315932013-01-24 16:46:58 +00003035 D->addAttr(::new (S.Context)
3036 GNUInlineAttr(Attr.getRange(), S.Context,
3037 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003038}
3039
Chandler Carruthedc2c642011-07-02 00:01:44 +00003040static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003041 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003042
Aaron Ballman02df2e02012-12-09 17:45:41 +00003043 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003044 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003045 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3046 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003047 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003048 return;
3049
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003050 if (!isa<ObjCMethodDecl>(D)) {
3051 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3052 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003053 return;
3054 }
3055
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003056 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003057 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003058 D->addAttr(::new (S.Context)
3059 FastCallAttr(Attr.getRange(), S.Context,
3060 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003061 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003062 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003063 D->addAttr(::new (S.Context)
3064 StdCallAttr(Attr.getRange(), S.Context,
3065 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003066 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003067 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003068 D->addAttr(::new (S.Context)
3069 ThisCallAttr(Attr.getRange(), S.Context,
3070 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003071 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003072 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003073 D->addAttr(::new (S.Context)
3074 CDeclAttr(Attr.getRange(), S.Context,
3075 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003076 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003077 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003078 D->addAttr(::new (S.Context)
3079 PascalAttr(Attr.getRange(), S.Context,
3080 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003081 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003082 case AttributeList::AT_MSABI:
3083 D->addAttr(::new (S.Context)
3084 MSABIAttr(Attr.getRange(), S.Context,
3085 Attr.getAttributeSpellingListIndex()));
3086 return;
3087 case AttributeList::AT_SysVABI:
3088 D->addAttr(::new (S.Context)
3089 SysVABIAttr(Attr.getRange(), S.Context,
3090 Attr.getAttributeSpellingListIndex()));
3091 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003092 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003093 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003094 switch (CC) {
3095 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003096 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003097 break;
3098 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003099 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003100 break;
3101 default:
3102 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003103 }
3104
Michael Han99315932013-01-24 16:46:58 +00003105 D->addAttr(::new (S.Context)
3106 PcsAttr(Attr.getRange(), S.Context, PCS,
3107 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003108 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003109 }
Derek Schuffa2020962012-10-16 22:30:41 +00003110 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003111 D->addAttr(::new (S.Context)
3112 PnaclCallAttr(Attr.getRange(), S.Context,
3113 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003114 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003115 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003116 D->addAttr(::new (S.Context)
3117 IntelOclBiccAttr(Attr.getRange(), S.Context,
3118 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003119 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003120
Abramo Bagnara50099372010-04-30 13:10:51 +00003121 default:
3122 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003123 }
3124}
3125
Aaron Ballman02df2e02012-12-09 17:45:41 +00003126bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3127 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003128 if (attr.isInvalid())
3129 return true;
3130
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003131 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003132 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003133 attr.setInvalid();
3134 return true;
3135 }
3136
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003137 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003138 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003139 case AttributeList::AT_CDecl: CC = CC_C; break;
3140 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3141 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3142 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3143 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003144 case AttributeList::AT_MSABI:
3145 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3146 CC_X86_64Win64;
3147 break;
3148 case AttributeList::AT_SysVABI:
3149 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3150 CC_C;
3151 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003152 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003153 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003154 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003155 attr.setInvalid();
3156 return true;
3157 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003158 if (StrRef == "aapcs") {
3159 CC = CC_AAPCS;
3160 break;
3161 } else if (StrRef == "aapcs-vfp") {
3162 CC = CC_AAPCS_VFP;
3163 break;
3164 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003165
3166 attr.setInvalid();
3167 Diag(attr.getLoc(), diag::err_invalid_pcs);
3168 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003169 }
Derek Schuffa2020962012-10-16 22:30:41 +00003170 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003171 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003172 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003173 }
3174
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003175 const TargetInfo &TI = Context.getTargetInfo();
3176 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3177 if (A == TargetInfo::CCCR_Warning) {
3178 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003179
3180 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3181 if (FD)
3182 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3183 TargetInfo::CCMT_NonMember;
3184 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003185 }
3186
John McCall3882ace2011-01-05 12:14:39 +00003187 return false;
3188}
3189
John McCall3882ace2011-01-05 12:14:39 +00003190/// Checks a regparm attribute, returning true if it is ill-formed and
3191/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003192bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3193 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003194 return true;
3195
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003196 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003197 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003198 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003199 }
Eli Friedman7044b762009-03-27 21:06:47 +00003200
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003201 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003202 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003203 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003204 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003205 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003206 }
3207
Douglas Gregore8bbc122011-09-02 00:18:52 +00003208 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003209 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003210 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003211 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003212 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003213 }
3214
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003215 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003216 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003217 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003218 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003219 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003220 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003221 }
3222
John McCall3882ace2011-01-05 12:14:39 +00003223 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003224}
3225
Aaron Ballman66039932013-12-19 00:41:31 +00003226static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3227 const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003228 // check the attribute arguments.
3229 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3230 // FIXME: 0 is not okay.
Aaron Ballman05e420a2014-01-02 21:26:14 +00003231 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3232 << Attr.getName() << 2;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003233 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003234 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003235
Aaron Ballman66039932013-12-19 00:41:31 +00003236 uint32_t MaxThreads, MinBlocks = 0;
3237 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3238 return;
3239 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3240 Attr.getArgAsExpr(1),
3241 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003242 return;
3243
3244 D->addAttr(::new (S.Context)
3245 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3246 MaxThreads, MinBlocks,
3247 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003248}
3249
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003250static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3251 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003252 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003253 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003254 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003255 return;
3256 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003257
3258 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003259 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003260
Aaron Ballman00e99962013-08-31 01:11:41 +00003261 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003262
3263 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3264 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3265 << Attr.getName() << ExpectedFunctionOrMethod;
3266 return;
3267 }
3268
3269 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003270 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3271 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003272 return;
3273
3274 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003275 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3276 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003277 return;
3278
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003279 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003280 if (IsPointer) {
3281 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003282 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003283 if (!BufferTy->isPointerType()) {
3284 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003285 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003286 }
3287 }
3288
Michael Han99315932013-01-24 16:46:58 +00003289 D->addAttr(::new (S.Context)
3290 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3291 ArgumentIdx, TypeTagIdx, IsPointer,
3292 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003293}
3294
3295static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3296 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003297 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003298 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003299 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003300 return;
3301 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003302
3303 if (!checkAttributeNumArgs(S, Attr, 1))
3304 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003305
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003306 if (!isa<VarDecl>(D)) {
3307 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3308 << Attr.getName() << ExpectedVariable;
3309 return;
3310 }
3311
Aaron Ballman00e99962013-08-31 01:11:41 +00003312 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003313 TypeSourceInfo *MatchingCTypeLoc = 0;
3314 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3315 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003316
Michael Han99315932013-01-24 16:46:58 +00003317 D->addAttr(::new (S.Context)
3318 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003319 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003320 Attr.getLayoutCompatible(),
3321 Attr.getMustBeNull(),
3322 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003323}
3324
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003325//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003326// Checker-specific attribute handlers.
3327//===----------------------------------------------------------------------===//
3328
John McCalled433932011-01-25 03:31:58 +00003329static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003330 return type->isDependentType() ||
3331 type->isObjCObjectPointerType() ||
3332 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003333}
3334static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003335 return type->isDependentType() ||
3336 type->isPointerType() ||
3337 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003338}
3339
Chandler Carruthedc2c642011-07-02 00:01:44 +00003340static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003341 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003342 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003343
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003344 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003345 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3346 cf = false;
3347 } else {
3348 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3349 cf = true;
3350 }
3351
3352 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003353 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003354 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003355 return;
3356 }
3357
3358 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003359 param->addAttr(::new (S.Context)
3360 CFConsumedAttr(Attr.getRange(), S.Context,
3361 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003362 else
Michael Han99315932013-01-24 16:46:58 +00003363 param->addAttr(::new (S.Context)
3364 NSConsumedAttr(Attr.getRange(), S.Context,
3365 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003366}
3367
Chandler Carruthedc2c642011-07-02 00:01:44 +00003368static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3369 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003370
John McCalled433932011-01-25 03:31:58 +00003371 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003372
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003373 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003374 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003375 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003376 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003377 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003378 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3379 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003380 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003381 returnType = FD->getReturnType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003382 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003383 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003384 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003385 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003386 return;
3387 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003388
John McCalled433932011-01-25 03:31:58 +00003389 bool typeOK;
3390 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003391 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003392 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003393 case AttributeList::AT_NSReturnsAutoreleased:
3394 case AttributeList::AT_NSReturnsRetained:
3395 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003396 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3397 cf = false;
3398 break;
3399
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003400 case AttributeList::AT_CFReturnsRetained:
3401 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003402 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3403 cf = true;
3404 break;
3405 }
3406
3407 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003408 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003409 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003410 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003411 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003412
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003413 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003414 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003415 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003416 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003417 D->addAttr(::new (S.Context)
3418 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3419 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003420 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003421 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003422 D->addAttr(::new (S.Context)
3423 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3424 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003425 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003426 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003427 D->addAttr(::new (S.Context)
3428 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3429 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003430 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003431 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003432 D->addAttr(::new (S.Context)
3433 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3434 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003435 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003436 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003437 D->addAttr(::new (S.Context)
3438 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3439 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003440 return;
3441 };
3442}
3443
John McCallcf166702011-07-22 08:53:00 +00003444static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3445 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003446 const int EP_ObjCMethod = 1;
3447 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003448
John McCallcf166702011-07-22 08:53:00 +00003449 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003450 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003451 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003452 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003453 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003454 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003455
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003456 if (!resultType->isReferenceType() &&
3457 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003458 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003459 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003460 << attr.getName()
3461 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003462 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003463
3464 // Drop the attribute.
3465 return;
3466 }
3467
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003468 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003469 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3470 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003471}
3472
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003473static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3474 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003475 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003476
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003477 DeclContext *DC = method->getDeclContext();
3478 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3479 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3480 << attr.getName() << 0;
3481 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3482 return;
3483 }
3484 if (method->getMethodFamily() == OMF_dealloc) {
3485 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3486 << attr.getName() << 1;
3487 return;
3488 }
3489
Michael Han99315932013-01-24 16:46:58 +00003490 method->addAttr(::new (S.Context)
3491 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3492 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003493}
3494
Aaron Ballmanfb763042013-12-02 18:05:46 +00003495static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3496 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003497 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00003498 return;
John McCall32f5fe12011-09-30 05:12:12 +00003499
Aaron Ballmanfb763042013-12-02 18:05:46 +00003500 D->addAttr(::new (S.Context)
3501 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3502 Attr.getAttributeSpellingListIndex()));
3503}
3504
3505static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3506 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003507 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00003508 return;
3509
3510 D->addAttr(::new (S.Context)
3511 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3512 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003513}
3514
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003515static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3516 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003517 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003518
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003519 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003520 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003521 return;
3522 }
3523
3524 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003525 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003526 Attr.getAttributeSpellingListIndex()));
3527}
3528
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003529static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3530 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003531 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003532
3533 if (!Parm) {
3534 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3535 return;
3536 }
3537
3538 D->addAttr(::new (S.Context)
3539 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3540 Attr.getAttributeSpellingListIndex()));
3541}
3542
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003543static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3544 const AttributeList &Attr) {
3545 IdentifierInfo *RelatedClass =
3546 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0;
3547 if (!RelatedClass) {
3548 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3549 return;
3550 }
3551 IdentifierInfo *ClassMethod =
3552 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0;
3553 IdentifierInfo *InstanceMethod =
3554 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0;
3555 D->addAttr(::new (S.Context)
3556 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3557 ClassMethod, InstanceMethod,
3558 Attr.getAttributeSpellingListIndex()));
3559}
3560
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003561static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3562 const AttributeList &Attr) {
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003563 ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003564 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003565 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003566 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3567 Attr.getAttributeSpellingListIndex()));
3568}
3569
Chandler Carruthedc2c642011-07-02 00:01:44 +00003570static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3571 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003572 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003573
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003574 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003575 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003576}
3577
Chandler Carruthedc2c642011-07-02 00:01:44 +00003578static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3579 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003580 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003581 QualType type = vd->getType();
3582
3583 if (!type->isDependentType() &&
3584 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003585 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003586 << type;
3587 return;
3588 }
3589
3590 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3591
3592 // If we have no lifetime yet, check the lifetime we're presumably
3593 // going to infer.
3594 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3595 lifetime = type->getObjCARCImplicitLifetime();
3596
3597 switch (lifetime) {
3598 case Qualifiers::OCL_None:
3599 assert(type->isDependentType() &&
3600 "didn't infer lifetime for non-dependent type?");
3601 break;
3602
3603 case Qualifiers::OCL_Weak: // meaningful
3604 case Qualifiers::OCL_Strong: // meaningful
3605 break;
3606
3607 case Qualifiers::OCL_ExplicitNone:
3608 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003609 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003610 << (lifetime == Qualifiers::OCL_Autoreleasing);
3611 break;
3612 }
3613
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003614 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003615 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3616 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003617}
3618
Francois Picheta83957a2010-12-19 06:50:37 +00003619//===----------------------------------------------------------------------===//
3620// Microsoft specific attribute handlers.
3621//===----------------------------------------------------------------------===//
3622
Chandler Carruthedc2c642011-07-02 00:01:44 +00003623static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003624 if (!S.LangOpts.CPlusPlus) {
3625 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3626 << Attr.getName() << AttributeLangSupport::C;
3627 return;
3628 }
3629
Aaron Ballman60e705e2013-11-24 20:58:02 +00003630 if (!isa<CXXRecordDecl>(D)) {
3631 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3632 << Attr.getName() << ExpectedClass;
3633 return;
3634 }
3635
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003636 StringRef StrRef;
3637 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003638 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003639 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003640
David Majnemer89085342013-08-09 08:56:20 +00003641 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3642 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003643 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3644 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003645
Reid Kleckner140c4a72013-05-17 14:04:52 +00003646 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003647 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003648 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003649 return;
3650 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003651
David Majnemer89085342013-08-09 08:56:20 +00003652 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003653 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003654 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003655 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003656 return;
3657 }
David Majnemer89085342013-08-09 08:56:20 +00003658 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003659 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003660 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003661 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003662 }
Francois Picheta83957a2010-12-19 06:50:37 +00003663
David Majnemer89085342013-08-09 08:56:20 +00003664 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3665 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003666}
3667
David Majnemer2c4e00a2014-01-29 22:07:36 +00003668static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3669 if (!S.LangOpts.CPlusPlus) {
3670 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3671 << Attr.getName() << AttributeLangSupport::C;
3672 return;
3673 }
3674 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00003675 D, Attr.getRange(), /*BestCase=*/true,
3676 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00003677 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
3678 if (IA)
3679 D->addAttr(IA);
3680}
3681
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003682static void handleARMInterruptAttr(Sema &S, Decl *D,
3683 const AttributeList &Attr) {
3684 // Check the attribute arguments.
3685 if (Attr.getNumArgs() > 1) {
3686 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3687 << Attr.getName() << 1;
3688 return;
3689 }
3690
3691 StringRef Str;
3692 SourceLocation ArgLoc;
3693
3694 if (Attr.getNumArgs() == 0)
3695 Str = "";
3696 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
3697 return;
3698
3699 ARMInterruptAttr::InterruptType Kind;
3700 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
3701 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
3702 << Attr.getName() << Str << ArgLoc;
3703 return;
3704 }
3705
3706 unsigned Index = Attr.getAttributeSpellingListIndex();
3707 D->addAttr(::new (S.Context)
3708 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
3709}
3710
3711static void handleMSP430InterruptAttr(Sema &S, Decl *D,
3712 const AttributeList &Attr) {
3713 if (!checkAttributeNumArgs(S, Attr, 1))
3714 return;
3715
3716 if (!Attr.isArgExpr(0)) {
3717 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3718 << AANT_ArgumentIntegerConstant;
3719 return;
3720 }
3721
3722 // FIXME: Check for decl - it should be void ()(void).
3723
3724 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3725 llvm::APSInt NumParams(32);
3726 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
3727 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3728 << Attr.getName() << AANT_ArgumentIntegerConstant
3729 << NumParamsExpr->getSourceRange();
3730 return;
3731 }
3732
3733 unsigned Num = NumParams.getLimitedValue(255);
3734 if ((Num & 1) || Num > 30) {
3735 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3736 << Attr.getName() << (int)NumParams.getSExtValue()
3737 << NumParamsExpr->getSourceRange();
3738 return;
3739 }
3740
Aaron Ballman36a53502014-01-16 13:03:14 +00003741 D->addAttr(::new (S.Context)
3742 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
3743 Attr.getAttributeSpellingListIndex()));
3744 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003745}
3746
3747static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3748 // Dispatch the interrupt attribute based on the current target.
3749 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
3750 handleMSP430InterruptAttr(S, D, Attr);
3751 else
3752 handleARMInterruptAttr(S, D, Attr);
3753}
3754
3755static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
3756 const AttributeList& Attr) {
3757 // If we try to apply it to a function pointer, don't warn, but don't
3758 // do anything, either. It doesn't matter anyway, because there's nothing
3759 // special about calling a force_align_arg_pointer function.
3760 ValueDecl *VD = dyn_cast<ValueDecl>(D);
3761 if (VD && VD->getType()->isFunctionPointerType())
3762 return;
3763 // Also don't warn on function pointer typedefs.
3764 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
3765 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
3766 TD->getUnderlyingType()->isFunctionType()))
3767 return;
3768 // Attribute can only be applied to function types.
3769 if (!isa<FunctionDecl>(D)) {
3770 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3771 << Attr.getName() << /* function */0;
3772 return;
3773 }
3774
Aaron Ballman36a53502014-01-16 13:03:14 +00003775 D->addAttr(::new (S.Context)
3776 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
3777 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003778}
3779
3780DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
3781 unsigned AttrSpellingListIndex) {
3782 if (D->hasAttr<DLLExportAttr>()) {
3783 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "dllimport";
3784 return NULL;
3785 }
3786
3787 if (D->hasAttr<DLLImportAttr>())
3788 return NULL;
3789
3790 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3791 if (VD->hasDefinition()) {
3792 // dllimport cannot be applied to definitions.
3793 Diag(D->getLocation(), diag::warn_attribute_invalid_on_definition)
3794 << "dllimport";
3795 return NULL;
3796 }
3797 }
3798
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003799 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003800}
3801
3802static void handleDLLImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3803 // Attribute can be applied only to functions or variables.
3804 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3805 if (!FD && !isa<VarDecl>(D)) {
3806 // Apparently Visual C++ thinks it is okay to not emit a warning
3807 // in this case, so only emit a warning when -fms-extensions is not
3808 // specified.
3809 if (!S.getLangOpts().MicrosoftExt)
3810 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003811 << Attr.getName() << ExpectedVariableOrFunction;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003812 return;
3813 }
3814
3815 // Currently, the dllimport attribute is ignored for inlined functions.
3816 // Warning is emitted.
3817 if (FD && FD->isInlineSpecified()) {
3818 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3819 return;
3820 }
3821
3822 unsigned Index = Attr.getAttributeSpellingListIndex();
3823 DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index);
3824 if (NewAttr)
3825 D->addAttr(NewAttr);
3826}
3827
3828DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
3829 unsigned AttrSpellingListIndex) {
3830 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
3831 Diag(Import->getLocation(), diag::warn_attribute_ignored) << "dllimport";
3832 D->dropAttr<DLLImportAttr>();
3833 }
3834
3835 if (D->hasAttr<DLLExportAttr>())
3836 return NULL;
3837
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003838 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003839}
3840
3841static void handleDLLExportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3842 // Currently, the dllexport attribute is ignored for inlined functions, unless
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003843 // the -fkeep-inline-functions flag has been used. Warning is emitted.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003844 if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isInlineSpecified()) {
3845 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
3846 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3847 return;
3848 }
3849
3850 unsigned Index = Attr.getAttributeSpellingListIndex();
3851 DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index);
3852 if (NewAttr)
3853 D->addAttr(NewAttr);
3854}
3855
David Majnemer2c4e00a2014-01-29 22:07:36 +00003856MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00003857Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003858 unsigned AttrSpellingListIndex,
3859 MSInheritanceAttr::Spelling SemanticSpelling) {
3860 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
3861 if (IA->getSemanticSpelling() == SemanticSpelling)
3862 return 0;
3863 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
3864 << 1 /*previous declaration*/;
3865 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
3866 D->dropAttr<MSInheritanceAttr>();
3867 }
3868
3869 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
3870 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00003871 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
3872 SemanticSpelling)) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00003873 return 0;
3874 }
3875 } else {
3876 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
3877 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
3878 << 1 /*partial specialization*/;
3879 return 0;
3880 }
3881 if (RD->getDescribedClassTemplate()) {
3882 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
3883 << 0 /*primary template*/;
3884 return 0;
3885 }
3886 }
3887
3888 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00003889 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00003890}
3891
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003892static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3893 // The capability attributes take a single string parameter for the name of
3894 // the capability they represent. The lockable attribute does not take any
3895 // parameters. However, semantically, both attributes represent the same
3896 // concept, and so they use the same semantic attribute. Eventually, the
3897 // lockable attribute will be removed.
3898 StringRef N;
3899 SourceLocation LiteralLoc;
3900 if (Attr.getKind() == AttributeList::AT_Capability &&
3901 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
3902 return;
3903
3904 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
3905 Attr.getAttributeSpellingListIndex()));
3906}
3907
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003908static void handleAssertCapabilityAttr(Sema &S, Decl *D,
3909 const AttributeList &Attr) {
3910 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
3911 Attr.getArgAsExpr(0),
3912 Attr.getAttributeSpellingListIndex()));
3913}
3914
3915static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
3916 const AttributeList &Attr) {
3917 SmallVector<Expr*, 1> Args;
3918 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
3919 return;
3920
3921 // Check that all arguments are lockable objects.
3922 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
3923 if (Args.empty())
3924 return;
3925
3926 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
3927 S.Context,
3928 Args.data(), Args.size(),
3929 Attr.getAttributeSpellingListIndex()));
3930}
3931
3932static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
3933 const AttributeList &Attr) {
3934 SmallVector<Expr*, 2> Args;
3935 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
3936 return;
3937
3938 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
3939 S.Context,
3940 Attr.getArgAsExpr(0),
3941 Args.data(),
3942 Args.size(),
3943 Attr.getAttributeSpellingListIndex()));
3944}
3945
3946static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
3947 const AttributeList &Attr) {
3948 SmallVector<Expr*, 1> Args;
3949 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
3950 return;
3951
3952 // Check that all arguments are lockable objects.
3953 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
3954 if (Args.empty())
3955 return;
3956
3957 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(Attr.getRange(),
3958 S.Context,
3959 Args.data(), Args.size(),
3960 Attr.getAttributeSpellingListIndex()));
3961}
3962
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003963static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
3964 const AttributeList &Attr) {
3965 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
3966 return;
3967
3968 // check that all arguments are lockable objects
3969 SmallVector<Expr*, 1> Args;
3970 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
3971 if (Args.empty())
3972 return;
3973
3974 RequiresCapabilityAttr *RCA = ::new (S.Context)
3975 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
3976 Args.size(), Attr.getAttributeSpellingListIndex());
3977
3978 D->addAttr(RCA);
3979}
3980
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003981/// Handles semantic checking for features that are common to all attributes,
3982/// such as checking whether a parameter was properly specified, or the correct
3983/// number of arguments were passed, etc.
3984static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3985 const AttributeList &Attr) {
3986 // Several attributes carry different semantics than the parsing requires, so
3987 // those are opted out of the common handling.
3988 //
3989 // We also bail on unknown and ignored attributes because those are handled
3990 // as part of the target-specific handling logic.
3991 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003992 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003993 return false;
3994
Aaron Ballman3aff6332013-12-02 19:30:36 +00003995 // Check whether the attribute requires specific language extensions to be
3996 // enabled.
3997 if (!Attr.diagnoseLangOpts(S))
3998 return true;
3999
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004000 // If there are no optional arguments, then checking for the argument count
4001 // is trivial.
4002 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4003 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4004 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004005
4006 // Check whether the attribute appertains to the given subject.
4007 if (!Attr.diagnoseAppertainsTo(S, D))
4008 return true;
4009
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004010 return false;
4011}
4012
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004013//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004014// Top Level Sema Entry Points
4015//===----------------------------------------------------------------------===//
4016
Richard Smithf8a75c32013-08-29 00:47:48 +00004017/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4018/// the attribute applies to decls. If the attribute is a type attribute, just
4019/// silently ignore it if a GNU attribute.
4020static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4021 const AttributeList &Attr,
4022 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004023 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00004024 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004025
Richard Smithf8a75c32013-08-29 00:47:48 +00004026 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4027 // instead.
4028 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4029 return;
4030
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004031 // Unknown attributes are automatically warned on. Target-specific attributes
4032 // which do not apply to the current target architecture are treated as
4033 // though they were unknown attributes.
4034 if (Attr.getKind() == AttributeList::UnknownAttribute ||
4035 !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) {
4036 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4037 diag::warn_unhandled_ms_attribute_ignored :
4038 diag::warn_unknown_attribute_ignored) << Attr.getName();
4039 return;
4040 }
4041
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004042 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4043 return;
4044
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004045 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004046 default:
4047 // Type attributes are handled elsewhere; silently move on.
4048 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
4049 break;
4050 case AttributeList::AT_Interrupt:
4051 handleInterruptAttr(S, D, Attr); break;
4052 case AttributeList::AT_X86ForceAlignArgPointer:
4053 handleX86ForceAlignArgPointerAttr(S, D, Attr); break;
4054 case AttributeList::AT_DLLExport:
4055 handleDLLExportAttr(S, D, Attr); break;
4056 case AttributeList::AT_DLLImport:
4057 handleDLLImportAttr(S, D, Attr); break;
4058 case AttributeList::AT_Mips16:
4059 handleSimpleAttribute<Mips16Attr>(S, D, Attr); break;
4060 case AttributeList::AT_NoMips16:
4061 handleSimpleAttribute<NoMips16Attr>(S, D, Attr); break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00004062 case AttributeList::AT_IBAction:
4063 handleSimpleAttribute<IBActionAttr>(S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004064 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4065 case AttributeList::AT_IBOutletCollection:
4066 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004067 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4068 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004069 case AttributeList::AT_AlwaysInline:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004070 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004071 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004072 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004073 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004074 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4075 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4076 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004077 handleDependencyAttr(S, scope, D, Attr);
4078 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004079 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004080 case AttributeList::AT_CUDAConstant:
4081 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004082 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004083 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman3a8e2d92013-11-27 18:53:58 +00004084 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004085 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004086 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004087 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004088 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
Nick Lewycky35a6ef42014-01-11 02:50:57 +00004089 case AttributeList::AT_EnableIf: handleEnableIfAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004090 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004091 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004092 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004093 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004094 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004095 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004096 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4097 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4098 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00004099 case AttributeList::AT_CUDADevice:
4100 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004101 case AttributeList::AT_CUDAHost:
4102 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004103 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4104 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004105 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004106 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004107 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004108 case AttributeList::AT_MayAlias:
4109 handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004110 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004111 case AttributeList::AT_NoCommon:
4112 handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00004113 case AttributeList::AT_NonNull:
4114 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
4115 handleNonNullAttrParameter(S, PVD, Attr);
4116 else
4117 handleNonNullAttr(S, D, Attr);
4118 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00004119 case AttributeList::AT_ReturnsNonNull:
4120 handleReturnsNonNullAttr(S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004121 case AttributeList::AT_Overloadable:
4122 handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00004123 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004124 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4125 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004126 case AttributeList::AT_Naked:
4127 handleSimpleAttribute<NakedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004128 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004129 case AttributeList::AT_NoThrow:
4130 handleSimpleAttribute<NoThrowAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004131 case AttributeList::AT_CUDAShared:
4132 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004133 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004134
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004135 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004136 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004137 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004138 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004139
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004140 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004141 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4142
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004143 case AttributeList::AT_ObjCRequiresSuper:
4144 handleObjCRequiresSuperAttr(S, D, Attr); break;
4145
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004146 case AttributeList::AT_ObjCBridge:
4147 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004148
4149 case AttributeList::AT_ObjCBridgeMutable:
4150 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004151
4152 case AttributeList::AT_ObjCBridgeRelated:
4153 handleObjCBridgeRelatedAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00004154
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004155 case AttributeList::AT_ObjCDesignatedInitializer:
4156 handleObjCDesignatedInitializer(S, D, Attr); break;
4157
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004158 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00004159 handleCFAuditedTransferAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004160 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00004161 handleCFUnknownTransferAttr(S, D, Attr); break;
John McCall32f5fe12011-09-30 05:12:12 +00004162
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004163 case AttributeList::AT_CFConsumed:
4164 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4165 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004166 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004167
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004168 case AttributeList::AT_NSReturnsAutoreleased:
4169 case AttributeList::AT_NSReturnsNotRetained:
4170 case AttributeList::AT_CFReturnsNotRetained:
4171 case AttributeList::AT_NSReturnsRetained:
4172 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004173 handleNSReturnsRetainedAttr(S, D, Attr); break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004174 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004175 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004176 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004177 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004178 case AttributeList::AT_VecTypeHint:
4179 handleVecTypeHint(S, D, Attr); break;
4180
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004181 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004182 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004183
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004184 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4185 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4186 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004187 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004188 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004189 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004190 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004191 case AttributeList::AT_ObjCRootClass:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004192 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004193 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek28eace62013-11-23 01:01:34 +00004194 handleObjCSuppresProtocolAttr(S, D, Attr);
4195 break;
4196 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004197 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00004198 case AttributeList::AT_Unused:
4199 handleSimpleAttribute<UnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004200 case AttributeList::AT_ReturnsTwice:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004201 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004202 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004203 case AttributeList::AT_Visibility:
4204 handleVisibilityAttr(S, D, Attr, false);
4205 break;
4206 case AttributeList::AT_TypeVisibility:
4207 handleVisibilityAttr(S, D, Attr, true);
4208 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004209 case AttributeList::AT_WarnUnused:
Aaron Ballman6a42b5a2013-11-27 16:59:17 +00004210 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004211 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004212 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004213 case AttributeList::AT_Weak:
4214 handleSimpleAttribute<WeakAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004215 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4216 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4217 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004218 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004219 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004220 case AttributeList::AT_ObjCException:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004221 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004222 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004223 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004224 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004225 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4226 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4227 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004228 case AttributeList::AT_Const:
4229 handleSimpleAttribute<ConstAttr>(S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004230 case AttributeList::AT_Pure:
4231 handleSimpleAttribute<PureAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004232 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4233 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004234 case AttributeList::AT_NoInline:
4235 handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004236 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004237 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004238 case AttributeList::AT_StdCall:
4239 case AttributeList::AT_CDecl:
4240 case AttributeList::AT_FastCall:
4241 case AttributeList::AT_ThisCall:
4242 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004243 case AttributeList::AT_MSABI:
4244 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004245 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004246 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004247 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004248 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004249 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004250 case AttributeList::AT_OpenCLKernel:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004251 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004252 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman26891332014-01-14 17:41:53 +00004253 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr); break;
John McCall8d32c052012-05-22 21:28:12 +00004254
4255 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004256 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004257 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004258 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004259 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004260 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004261 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004262 case AttributeList::AT_MSInheritance:
David Majnemer2c4e00a2014-01-29 22:07:36 +00004263 handleMSInheritanceAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004264 case AttributeList::AT_ForceInline:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004265 handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004266 case AttributeList::AT_SelectAny:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004267 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004268
4269 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004270 case AttributeList::AT_AssertExclusiveLock:
4271 handleAssertExclusiveLockAttr(S, D, Attr);
4272 break;
4273 case AttributeList::AT_AssertSharedLock:
4274 handleAssertSharedLockAttr(S, D, Attr);
4275 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004276 case AttributeList::AT_GuardedVar:
Aaron Ballmane61b8b82013-12-02 15:02:49 +00004277 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004278 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004279 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004280 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004281 case AttributeList::AT_ScopedLockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004282 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004283 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004284 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004285 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004286 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004287 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004288 break;
4289 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004290 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004291 break;
4292 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004293 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004294 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004295 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004296 handleGuardedByAttr(S, D, Attr);
4297 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004298 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004299 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004300 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004301 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004302 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004303 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004304 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004305 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004306 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004307 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004308 handleLockReturnedAttr(S, D, Attr);
4309 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004310 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004311 handleLocksExcludedAttr(S, D, Attr);
4312 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004313 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004314 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004315 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004316 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004317 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004318 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004319 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004320 handleUnlockFunAttr(S, D, Attr);
4321 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004322 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004323 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004324 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004325 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004326 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004327 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004328
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004329 // Capability analysis attributes.
4330 case AttributeList::AT_Capability:
4331 case AttributeList::AT_Lockable:
4332 handleCapabilityAttr(S, D, Attr); break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004333 case AttributeList::AT_RequiresCapability:
4334 handleRequiresCapabilityAttr(S, D, Attr); break;
4335
4336 case AttributeList::AT_AssertCapability:
4337 handleAssertCapabilityAttr(S, D, Attr); break;
4338 case AttributeList::AT_AcquireCapability:
4339 handleAcquireCapabilityAttr(S, D, Attr); break;
4340 case AttributeList::AT_ReleaseCapability:
4341 handleReleaseCapabilityAttr(S, D, Attr); break;
4342 case AttributeList::AT_TryAcquireCapability:
4343 handleTryAcquireCapabilityAttr(S, D, Attr); break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004344
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004345 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004346 case AttributeList::AT_Consumable:
4347 handleConsumableAttr(S, D, Attr);
4348 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004349 case AttributeList::AT_ConsumableAutoCast:
4350 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr); break;
4351 break;
4352 case AttributeList::AT_ConsumableSetOnRead:
4353 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr); break;
4354 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004355 case AttributeList::AT_CallableWhen:
4356 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004357 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004358 case AttributeList::AT_ParamTypestate:
4359 handleParamTypestateAttr(S, D, Attr);
4360 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004361 case AttributeList::AT_ReturnTypestate:
4362 handleReturnTypestateAttr(S, D, Attr);
4363 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004364 case AttributeList::AT_SetTypestate:
4365 handleSetTypestateAttr(S, D, Attr);
4366 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004367 case AttributeList::AT_TestTypestate:
4368 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004369 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004370
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004371 // Type safety attributes.
4372 case AttributeList::AT_ArgumentWithTypeTag:
4373 handleArgumentWithTypeTagAttr(S, D, Attr);
4374 break;
4375 case AttributeList::AT_TypeTagForDatatype:
4376 handleTypeTagForDatatypeAttr(S, D, Attr);
4377 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004378 }
4379}
4380
4381/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4382/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004383void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004384 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004385 bool IncludeCXX11Attributes) {
4386 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004387 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004388
Joey Gouly2cd9db12013-12-13 16:15:28 +00004389 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004390 // GCC accepts
4391 // static int a9 __attribute__((weakref));
4392 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004393 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00004394 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
4395 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00004396 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004397 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004398 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004399
4400 if (!D->hasAttr<OpenCLKernelAttr>()) {
4401 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004402 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
4403 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004404 D->setInvalidDecl();
4405 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004406 if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
4407 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004408 D->setInvalidDecl();
4409 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004410 if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
4411 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004412 D->setInvalidDecl();
4413 }
4414 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004415}
4416
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004417// Annotation attributes are the only attributes allowed after an access
4418// specifier.
4419bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4420 const AttributeList *AttrList) {
4421 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004422 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004423 handleAnnotateAttr(*this, ASDecl, *l);
4424 } else {
4425 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4426 return true;
4427 }
4428 }
4429
4430 return false;
4431}
4432
John McCall42856de2011-10-01 05:17:03 +00004433/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4434/// contains any decl attributes that we should warn about.
4435static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4436 for ( ; A; A = A->getNext()) {
4437 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004438 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004439 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4440
4441 if (A->getKind() == AttributeList::UnknownAttribute) {
4442 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4443 << A->getName() << A->getRange();
4444 } else {
4445 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4446 << A->getName() << A->getRange();
4447 }
4448 }
4449}
4450
4451/// checkUnusedDeclAttributes - Given a declarator which is not being
4452/// used to build a declaration, complain about any decl attributes
4453/// which might be lying around on it.
4454void Sema::checkUnusedDeclAttributes(Declarator &D) {
4455 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4456 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4457 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4458 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4459}
4460
Ryan Flynn7d470f32009-07-30 03:15:39 +00004461/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004462/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004463NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4464 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004465 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004466 NamedDecl *NewD = 0;
4467 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004468 FunctionDecl *NewFD;
4469 // FIXME: Missing call to CheckFunctionDeclaration().
4470 // FIXME: Mangling?
4471 // FIXME: Is the qualifier info correct?
4472 // FIXME: Is the DeclContext correct?
4473 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4474 Loc, Loc, DeclarationName(II),
4475 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004476 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004477 FD->hasPrototype(),
4478 false/*isConstexprSpecified*/);
4479 NewD = NewFD;
4480
4481 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004482 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004483
4484 // Fake up parameter variables; they are declared as if this were
4485 // a typedef.
4486 QualType FDTy = FD->getType();
4487 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4488 SmallVector<ParmVarDecl*, 16> Params;
Alp Toker9cacbab2014-01-20 20:26:09 +00004489 for (FunctionProtoType::param_type_iterator AI = FT->param_type_begin(),
4490 AE = FT->param_type_end();
4491 AI != AE; ++AI) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004492 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4493 Param->setScopeInfo(0, Params.size());
4494 Params.push_back(Param);
4495 }
David Blaikie9c70e042011-09-21 18:16:56 +00004496 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004497 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004498 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4499 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004500 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004501 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004502 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004503 if (VD->getQualifier()) {
4504 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004505 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004506 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004507 }
4508 return NewD;
4509}
4510
James Dennett634962f2012-06-14 21:40:34 +00004511/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004512/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004513void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004514 if (W.getUsed()) return; // only do this once
4515 W.setUsed(true);
4516 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4517 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004518 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00004519 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
4520 W.getLocation()));
4521 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00004522 WeakTopLevelDecl.push_back(NewD);
4523 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4524 // to insert Decl at TU scope, sorry.
4525 DeclContext *SavedContext = CurContext;
4526 CurContext = Context.getTranslationUnitDecl();
4527 PushOnScopeChains(NewD, S);
4528 CurContext = SavedContext;
4529 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00004530 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004531 }
4532}
4533
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004534void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4535 // It's valid to "forward-declare" #pragma weak, in which case we
4536 // have to do this.
4537 LoadExternalWeakUndeclaredIdentifiers();
4538 if (!WeakUndeclaredIdentifiers.empty()) {
4539 NamedDecl *ND = NULL;
4540 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4541 if (VD->isExternC())
4542 ND = VD;
4543 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4544 if (FD->isExternC())
4545 ND = FD;
4546 if (ND) {
4547 if (IdentifierInfo *Id = ND->getIdentifier()) {
4548 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4549 = WeakUndeclaredIdentifiers.find(Id);
4550 if (I != WeakUndeclaredIdentifiers.end()) {
4551 WeakInfo W = I->second;
4552 DeclApplyPragmaWeak(S, ND, W);
4553 WeakUndeclaredIdentifiers[Id] = W;
4554 }
4555 }
4556 }
4557 }
4558}
4559
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004560/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4561/// it, apply them to D. This is a bit tricky because PD can have attributes
4562/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004563void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004564 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004565 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004566 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004567
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004568 // Walk the declarator structure, applying decl attributes that were in a type
4569 // position to the decl itself. This handles cases like:
4570 // int *__attr__(x)** D;
4571 // when X is a decl attribute.
4572 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4573 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004574 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004575
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004576 // Finally, apply any attributes on the decl itself.
4577 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004578 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004579}
John McCall28a6aea2009-11-04 02:18:39 +00004580
John McCall31168b02011-06-15 23:02:42 +00004581/// Is the given declaration allowed to use a forbidden type?
4582static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4583 // Private ivars are always okay. Unfortunately, people don't
4584 // always properly make their ivars private, even in system headers.
4585 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004586 // Function declarations in sys headers will be marked unavailable.
4587 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4588 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004589 return false;
4590
4591 // Require it to be declared in a system header.
4592 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4593}
4594
4595/// Handle a delayed forbidden-type diagnostic.
4596static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4597 Decl *decl) {
4598 if (decl && isForbiddenTypeAllowed(S, decl)) {
Aaron Ballman36a53502014-01-16 13:03:14 +00004599 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context,
4600 "this system declaration uses an unsupported type",
4601 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00004602 return;
4603 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004604 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004605 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004606 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004607 // kind of forbidden type messages on unavailable functions.
4608 if (FD->hasAttr<UnavailableAttr>() &&
4609 diag.getForbiddenTypeDiagnostic() ==
4610 diag::err_arc_array_param_no_ownership) {
4611 diag.Triggered = true;
4612 return;
4613 }
4614 }
John McCall31168b02011-06-15 23:02:42 +00004615
4616 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4617 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4618 diag.Triggered = true;
4619}
4620
John McCall2ec85372012-05-07 06:16:41 +00004621void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4622 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004623 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004624 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004625
John McCall2ec85372012-05-07 06:16:41 +00004626 // When delaying diagnostics to run in the context of a parsed
4627 // declaration, we only want to actually emit anything if parsing
4628 // succeeds.
4629 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004630
John McCall2ec85372012-05-07 06:16:41 +00004631 // We emit all the active diagnostics in this pool or any of its
4632 // parents. In general, we'll get one pool for the decl spec
4633 // and a child pool for each declarator; in a decl group like:
4634 // deprecated_typedef foo, *bar, baz();
4635 // only the declarator pops will be passed decls. This is correct;
4636 // we really do need to consider delayed diagnostics from the decl spec
4637 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004638 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004639 do {
John McCall6347b682012-05-07 06:16:58 +00004640 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004641 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4642 // This const_cast is a bit lame. Really, Triggered should be mutable.
4643 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004644 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004645 continue;
4646
John McCallc1465822011-02-14 07:13:47 +00004647 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004648 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004649 case DelayedDiagnostic::Unavailable:
4650 // Don't bother giving deprecation/unavailable diagnostics if
4651 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004652 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004653 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004654 break;
4655
4656 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004657 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004658 break;
John McCall31168b02011-06-15 23:02:42 +00004659
4660 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004661 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004662 break;
John McCall86121512010-01-27 03:50:35 +00004663 }
4664 }
John McCall2ec85372012-05-07 06:16:41 +00004665 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004666}
4667
John McCall6347b682012-05-07 06:16:58 +00004668/// Given a set of delayed diagnostics, re-emit them as if they had
4669/// been delayed in the current context instead of in the given pool.
4670/// Essentially, this just moves them to the current pool.
4671void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4672 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4673 assert(curPool && "re-emitting in undelayed context not supported");
4674 curPool->steal(pool);
4675}
4676
John McCall28a6aea2009-11-04 02:18:39 +00004677static bool isDeclDeprecated(Decl *D) {
4678 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004679 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004680 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004681 // A category implicitly has the availability of the interface.
4682 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4683 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004684 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4685 return false;
4686}
4687
Ted Kremenekb79ee572013-12-18 23:30:06 +00004688static bool isDeclUnavailable(Decl *D) {
4689 do {
4690 if (D->isUnavailable())
4691 return true;
4692 // A category implicitly has the availability of the interface.
4693 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4694 return CatD->getClassInterface()->isUnavailable();
4695 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4696 return false;
4697}
4698
Eli Friedman971bfa12012-08-08 21:52:41 +00004699static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004700DoEmitAvailabilityWarning(Sema &S,
4701 DelayedDiagnostic::DDKind K,
4702 Decl *Ctx,
4703 const NamedDecl *D,
4704 StringRef Message,
4705 SourceLocation Loc,
4706 const ObjCInterfaceDecl *UnknownObjCClass,
4707 const ObjCPropertyDecl *ObjCProperty) {
4708
4709 // Diagnostics for deprecated or unavailable.
4710 unsigned diag, diag_message, diag_fwdclass_message;
4711
4712 // Matches 'diag::note_property_attribute' options.
4713 unsigned property_note_select;
4714
4715 // Matches diag::note_availability_specified_here.
4716 unsigned available_here_select_kind;
4717
4718 // Don't warn if our current context is deprecated or unavailable.
4719 switch (K) {
4720 case DelayedDiagnostic::Deprecation:
4721 if (isDeclDeprecated(Ctx))
4722 return;
4723 diag = diag::warn_deprecated;
4724 diag_message = diag::warn_deprecated_message;
4725 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4726 property_note_select = /* deprecated */ 0;
4727 available_here_select_kind = /* deprecated */ 2;
4728 break;
4729
4730 case DelayedDiagnostic::Unavailable:
4731 if (isDeclUnavailable(Ctx))
4732 return;
4733 diag = diag::err_unavailable;
4734 diag_message = diag::err_unavailable_message;
4735 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4736 property_note_select = /* unavailable */ 1;
4737 available_here_select_kind = /* unavailable */ 0;
4738 break;
4739
4740 default:
4741 llvm_unreachable("Neither a deprecation or unavailable kind");
4742 }
4743
Eli Friedman971bfa12012-08-08 21:52:41 +00004744 DeclarationName Name = D->getDeclName();
4745 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004746 S.Diag(Loc, diag_message) << Name << Message;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004747 if (ObjCProperty)
4748 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4749 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004750 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004751 S.Diag(Loc, diag) << Name;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004752 if (ObjCProperty)
4753 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4754 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004755 } else {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004756 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004757 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4758 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004759
4760 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4761 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004762}
4763
Ted Kremenekb79ee572013-12-18 23:30:06 +00004764void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4765 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004766 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004767 DoEmitAvailabilityWarning(*this,
4768 (DelayedDiagnostic::DDKind) DD.Kind,
4769 Ctx,
4770 DD.getDeprecationDecl(),
4771 DD.getDeprecationMessage(),
4772 DD.Loc,
4773 DD.getUnknownObjCClass(),
4774 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004775}
4776
Ted Kremenekb79ee572013-12-18 23:30:06 +00004777void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4778 NamedDecl *D, StringRef Message,
4779 SourceLocation Loc,
4780 const ObjCInterfaceDecl *UnknownObjCClass,
4781 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004782 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004783 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004784 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4785 UnknownObjCClass,
4786 ObjCProperty,
4787 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004788 return;
4789 }
4790
Ted Kremenekb79ee572013-12-18 23:30:06 +00004791 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4792 DelayedDiagnostic::DDKind K;
4793 switch (AD) {
4794 case AD_Deprecation:
4795 K = DelayedDiagnostic::Deprecation;
4796 break;
4797 case AD_Unavailable:
4798 K = DelayedDiagnostic::Unavailable;
4799 break;
4800 }
4801
4802 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
4803 UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004804}