blob: 237c99e48a22f4e520e73f51c861faa6368176fc [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 Kremenek438f8db2014-02-22 01:06:05 +00001646static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Ted Kremenek28eace62013-11-23 01:01:34 +00001647 const AttributeList &Attr) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00001648 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Ted Kremenek27cfe102014-02-21 22:49:04 +00001649 S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition)
1650 << Attr.getName() << Attr.getRange();
1651 return;
1652 }
1653
Ted Kremenek28eace62013-11-23 01:01:34 +00001654 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001655 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1656 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001657}
1658
Jordy Rose740b0c22012-05-08 03:27:22 +00001659static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1660 IdentifierInfo *Platform,
1661 VersionTuple Introduced,
1662 VersionTuple Deprecated,
1663 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001664 StringRef PlatformName
1665 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1666 if (PlatformName.empty())
1667 PlatformName = Platform->getName();
1668
1669 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1670 // of these steps are needed).
1671 if (!Introduced.empty() && !Deprecated.empty() &&
1672 !(Introduced <= Deprecated)) {
1673 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1674 << 1 << PlatformName << Deprecated.getAsString()
1675 << 0 << Introduced.getAsString();
1676 return true;
1677 }
1678
1679 if (!Introduced.empty() && !Obsoleted.empty() &&
1680 !(Introduced <= Obsoleted)) {
1681 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1682 << 2 << PlatformName << Obsoleted.getAsString()
1683 << 0 << Introduced.getAsString();
1684 return true;
1685 }
1686
1687 if (!Deprecated.empty() && !Obsoleted.empty() &&
1688 !(Deprecated <= Obsoleted)) {
1689 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1690 << 2 << PlatformName << Obsoleted.getAsString()
1691 << 1 << Deprecated.getAsString();
1692 return true;
1693 }
1694
1695 return false;
1696}
1697
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001698/// \brief Check whether the two versions match.
1699///
1700/// If either version tuple is empty, then they are assumed to match. If
1701/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1702static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1703 bool BeforeIsOkay) {
1704 if (X.empty() || Y.empty())
1705 return true;
1706
1707 if (X == Y)
1708 return true;
1709
1710 if (BeforeIsOkay && X < Y)
1711 return true;
1712
1713 return false;
1714}
1715
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001716AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001717 IdentifierInfo *Platform,
1718 VersionTuple Introduced,
1719 VersionTuple Deprecated,
1720 VersionTuple Obsoleted,
1721 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001722 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001723 bool Override,
1724 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001725 VersionTuple MergedIntroduced = Introduced;
1726 VersionTuple MergedDeprecated = Deprecated;
1727 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001728 bool FoundAny = false;
1729
Rafael Espindolac67f2232012-05-10 02:50:16 +00001730 if (D->hasAttrs()) {
1731 AttrVec &Attrs = D->getAttrs();
1732 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1733 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1734 if (!OldAA) {
1735 ++i;
1736 continue;
1737 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001738
Rafael Espindolac67f2232012-05-10 02:50:16 +00001739 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1740 if (OldPlatform != Platform) {
1741 ++i;
1742 continue;
1743 }
1744
1745 FoundAny = true;
1746 VersionTuple OldIntroduced = OldAA->getIntroduced();
1747 VersionTuple OldDeprecated = OldAA->getDeprecated();
1748 VersionTuple OldObsoleted = OldAA->getObsoleted();
1749 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001750
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001751 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1752 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1753 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1754 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001755 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001756 if (Override) {
1757 int Which = -1;
1758 VersionTuple FirstVersion;
1759 VersionTuple SecondVersion;
1760 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1761 Which = 0;
1762 FirstVersion = OldIntroduced;
1763 SecondVersion = Introduced;
1764 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1765 Which = 1;
1766 FirstVersion = Deprecated;
1767 SecondVersion = OldDeprecated;
1768 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1769 Which = 2;
1770 FirstVersion = Obsoleted;
1771 SecondVersion = OldObsoleted;
1772 }
1773
1774 if (Which == -1) {
1775 Diag(OldAA->getLocation(),
1776 diag::warn_mismatched_availability_override_unavail)
1777 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1778 } else {
1779 Diag(OldAA->getLocation(),
1780 diag::warn_mismatched_availability_override)
1781 << Which
1782 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1783 << FirstVersion.getAsString() << SecondVersion.getAsString();
1784 }
1785 Diag(Range.getBegin(), diag::note_overridden_method);
1786 } else {
1787 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1788 Diag(Range.getBegin(), diag::note_previous_attribute);
1789 }
1790
Rafael Espindolac67f2232012-05-10 02:50:16 +00001791 Attrs.erase(Attrs.begin() + i);
1792 --e;
1793 continue;
1794 }
1795
1796 VersionTuple MergedIntroduced2 = MergedIntroduced;
1797 VersionTuple MergedDeprecated2 = MergedDeprecated;
1798 VersionTuple MergedObsoleted2 = MergedObsoleted;
1799
1800 if (MergedIntroduced2.empty())
1801 MergedIntroduced2 = OldIntroduced;
1802 if (MergedDeprecated2.empty())
1803 MergedDeprecated2 = OldDeprecated;
1804 if (MergedObsoleted2.empty())
1805 MergedObsoleted2 = OldObsoleted;
1806
1807 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1808 MergedIntroduced2, MergedDeprecated2,
1809 MergedObsoleted2)) {
1810 Attrs.erase(Attrs.begin() + i);
1811 --e;
1812 continue;
1813 }
1814
1815 MergedIntroduced = MergedIntroduced2;
1816 MergedDeprecated = MergedDeprecated2;
1817 MergedObsoleted = MergedObsoleted2;
1818 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001819 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001820 }
1821
1822 if (FoundAny &&
1823 MergedIntroduced == Introduced &&
1824 MergedDeprecated == Deprecated &&
1825 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001826 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001827
Ted Kremenekb5445722013-04-06 00:34:27 +00001828 // Only create a new attribute if !Override, but we want to do
1829 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001830 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001831 MergedDeprecated, MergedObsoleted) &&
1832 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001833 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1834 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001835 Obsoleted, IsUnavailable, Message,
1836 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001837 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001838 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001839}
1840
Chandler Carruthedc2c642011-07-02 00:01:44 +00001841static void handleAvailabilityAttr(Sema &S, Decl *D,
1842 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001843 if (!checkAttributeNumArgs(S, Attr, 1))
1844 return;
1845 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001846 unsigned Index = Attr.getAttributeSpellingListIndex();
1847
Aaron Ballman00e99962013-08-31 01:11:41 +00001848 IdentifierInfo *II = Platform->Ident;
1849 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1850 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1851 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001852
Rafael Espindolac231fab2013-01-08 21:30:32 +00001853 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1854 if (!ND) {
1855 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1856 return;
1857 }
1858
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001859 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1860 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1861 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001862 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001863 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001864 if (const StringLiteral *SE =
1865 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001866 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001867
Aaron Ballman00e99962013-08-31 01:11:41 +00001868 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001869 Introduced.Version,
1870 Deprecated.Version,
1871 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001872 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001873 /*Override=*/false,
1874 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001875 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001876 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001877}
1878
John McCalld041a9b2013-02-20 01:54:26 +00001879template <class T>
1880static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1881 typename T::VisibilityType value,
1882 unsigned attrSpellingListIndex) {
1883 T *existingAttr = D->getAttr<T>();
1884 if (existingAttr) {
1885 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1886 if (existingValue == value)
1887 return NULL;
1888 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1889 S.Diag(range.getBegin(), diag::note_previous_attribute);
1890 D->dropAttr<T>();
1891 }
1892 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1893}
1894
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001895VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001896 VisibilityAttr::VisibilityType Vis,
1897 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001898 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1899 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001900}
1901
John McCalld041a9b2013-02-20 01:54:26 +00001902TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1903 TypeVisibilityAttr::VisibilityType Vis,
1904 unsigned AttrSpellingListIndex) {
1905 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1906 AttrSpellingListIndex);
1907}
1908
1909static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1910 bool isTypeVisibility) {
1911 // Visibility attributes don't mean anything on a typedef.
1912 if (isa<TypedefNameDecl>(D)) {
1913 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
1914 << Attr.getName();
1915 return;
1916 }
1917
1918 // 'type_visibility' can only go on a type or namespace.
1919 if (isTypeVisibility &&
1920 !(isa<TagDecl>(D) ||
1921 isa<ObjCInterfaceDecl>(D) ||
1922 isa<NamespaceDecl>(D))) {
1923 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
1924 << Attr.getName() << ExpectedTypeOrNamespace;
1925 return;
1926 }
1927
Benjamin Kramer70370212013-09-09 15:08:57 +00001928 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001929 StringRef TypeStr;
1930 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001931 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001932 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001933
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001934 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00001935 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001936 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00001937 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001938 return;
1939 }
Aaron Ballman682ee422013-09-11 19:47:58 +00001940
1941 // Complain about attempts to use protected visibility on targets
1942 // (like Darwin) that don't support it.
1943 if (type == VisibilityAttr::Protected &&
1944 !S.Context.getTargetInfo().hasProtectedVisibility()) {
1945 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1946 type = VisibilityAttr::Default;
1947 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001948
Michael Han99315932013-01-24 16:46:58 +00001949 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00001950 clang::Attr *newAttr;
1951 if (isTypeVisibility) {
1952 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
1953 (TypeVisibilityAttr::VisibilityType) type,
1954 Index);
1955 } else {
1956 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
1957 }
1958 if (newAttr)
1959 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001960}
1961
Chandler Carruthedc2c642011-07-02 00:01:44 +00001962static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1963 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001964 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00001965 if (!Attr.isArgIdent(0)) {
1966 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1967 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00001968 return;
1969 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001970
Aaron Ballman682ee422013-09-11 19:47:58 +00001971 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1972 ObjCMethodFamilyAttr::FamilyKind F;
1973 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
1974 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
1975 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00001976 return;
1977 }
1978
Alp Toker314cc812014-01-25 16:55:45 +00001979 if (F == ObjCMethodFamilyAttr::OMF_init &&
1980 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00001981 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00001982 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00001983 // Ignore the attribute.
1984 return;
1985 }
1986
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001987 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00001988 S.Context, F,
1989 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00001990}
1991
Chandler Carruthedc2c642011-07-02 00:01:44 +00001992static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00001993 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001994 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00001995 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001996 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1997 return;
1998 }
1999 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002000 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2001 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002002 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002003 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2004 return;
2005 }
2006 }
2007 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002008 // It is okay to include this attribute on properties, e.g.:
2009 //
2010 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2011 //
2012 // In this case it follows tradition and suppresses an error in the above
2013 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002014 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002015 }
Michael Han99315932013-01-24 16:46:58 +00002016 D->addAttr(::new (S.Context)
2017 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2018 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002019}
2020
Chandler Carruthedc2c642011-07-02 00:01:44 +00002021static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002022 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002023 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002024 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002025 return;
2026 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002027
Aaron Ballman00e99962013-08-31 01:11:41 +00002028 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002029 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002030 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2031 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2032 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002033 return;
2034 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002035
Michael Han99315932013-01-24 16:46:58 +00002036 D->addAttr(::new (S.Context)
2037 BlocksAttr(Attr.getRange(), S.Context, type,
2038 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002039}
2040
Chandler Carruthedc2c642011-07-02 00:01:44 +00002041static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002042 // check the attribute arguments.
2043 if (Attr.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00002044 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
2045 << Attr.getName() << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002046 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002047 }
2048
Aaron Ballman18a78382013-11-21 00:28:23 +00002049 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002050 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002051 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002052 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002053 if (E->isTypeDependent() || E->isValueDependent() ||
2054 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002055 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002056 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002057 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002058 return;
2059 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002060
John McCallb46f2872011-09-09 07:56:05 +00002061 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002062 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2063 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002064 return;
2065 }
John McCallb46f2872011-09-09 07:56:05 +00002066
2067 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002068 }
2069
Aaron Ballman18a78382013-11-21 00:28:23 +00002070 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002071 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002072 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002073 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002074 if (E->isTypeDependent() || E->isValueDependent() ||
2075 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002076 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002077 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002078 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002079 return;
2080 }
2081 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002082
John McCallb46f2872011-09-09 07:56:05 +00002083 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002084 // FIXME: This error message could be improved, it would be nice
2085 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002086 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2087 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002088 return;
2089 }
2090 }
2091
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002092 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002093 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002094 if (isa<FunctionNoProtoType>(FT)) {
2095 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2096 return;
2097 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002098
Chris Lattner9363e312009-03-17 23:03:47 +00002099 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002100 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002101 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002102 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002103 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002104 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002105 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002106 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002107 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002108 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2109 if (!BD->isVariadic()) {
2110 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2111 return;
2112 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002113 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002114 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002115 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002116 const FunctionType *FT = Ty->isFunctionPointerType()
2117 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002118 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002119 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002120 int m = Ty->isFunctionPointerType() ? 0 : 1;
2121 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002122 return;
2123 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002124 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002125 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002126 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002127 return;
2128 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002129 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002130 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002131 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002132 return;
2133 }
Michael Han99315932013-01-24 16:46:58 +00002134 D->addAttr(::new (S.Context)
2135 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2136 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002137}
2138
Chandler Carruthedc2c642011-07-02 00:01:44 +00002139static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002140 if (D->getFunctionType() &&
2141 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002142 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2143 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002144 return;
2145 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002146 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002147 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002148 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2149 << Attr.getName() << 1;
2150 return;
2151 }
2152
Michael Han99315932013-01-24 16:46:58 +00002153 D->addAttr(::new (S.Context)
2154 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2155 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002156}
2157
Chandler Carruthedc2c642011-07-02 00:01:44 +00002158static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002159 // weak_import only applies to variable & function declarations.
2160 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002161 if (!D->canBeWeakImported(isDef)) {
2162 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002163 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2164 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002165 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002166 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002167 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002168 // Nothing to warn about here.
2169 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002170 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002171 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002172
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002173 return;
2174 }
2175
Michael Han99315932013-01-24 16:46:58 +00002176 D->addAttr(::new (S.Context)
2177 WeakImportAttr(Attr.getRange(), S.Context,
2178 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002179}
2180
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002181// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002182template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002183static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002184 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002185 uint32_t WGSize[3];
2186 for (unsigned i = 0; i < 3; ++i)
2187 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002188 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002189
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002190 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2191 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2192 Existing->getYDim() == WGSize[1] &&
2193 Existing->getZDim() == WGSize[2]))
2194 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002195
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002196 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2197 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002198 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002199}
2200
Joey Goulyaba589c2013-03-08 09:42:32 +00002201static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002202 if (!Attr.hasParsedType()) {
2203 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2204 << Attr.getName() << 1;
2205 return;
2206 }
2207
Richard Smithb87c4652013-10-31 21:23:20 +00002208 TypeSourceInfo *ParmTSI = 0;
2209 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2210 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002211
2212 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2213 (ParmType->isBooleanType() ||
2214 !ParmType->isIntegralType(S.getASTContext()))) {
2215 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2216 << ParmType;
2217 return;
2218 }
2219
Aaron Ballmana9e05402013-12-02 22:16:55 +00002220 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002221 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002222 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2223 return;
2224 }
2225 }
2226
2227 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002228 ParmTSI,
2229 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002230}
2231
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002232SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002233 StringRef Name,
2234 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002235 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2236 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002237 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002238 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2239 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002240 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002241 }
Michael Han99315932013-01-24 16:46:58 +00002242 return ::new (Context) SectionAttr(Range, Context, Name,
2243 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002244}
2245
Chandler Carruthedc2c642011-07-02 00:01:44 +00002246static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002247 // Make sure that there is a string literal as the sections's single
2248 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002249 StringRef Str;
2250 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002251 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002252 return;
Mike Stump11289f42009-09-09 15:08:12 +00002253
Chris Lattner30ba6742009-08-10 19:03:04 +00002254 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002255 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002256 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002257 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002258 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002259 return;
2260 }
Mike Stump11289f42009-09-09 15:08:12 +00002261
Michael Han99315932013-01-24 16:46:58 +00002262 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002263 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002264 if (NewAttr)
2265 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002266}
2267
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002268
Chandler Carruthedc2c642011-07-02 00:01:44 +00002269static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002270 VarDecl *VD = cast<VarDecl>(D);
2271 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002272 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002273 return;
2274 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002275
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002276 Expr *E = Attr.getArgAsExpr(0);
2277 SourceLocation Loc = E->getExprLoc();
2278 FunctionDecl *FD = 0;
2279 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002280
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002281 // gcc only allows for simple identifiers. Since we support more than gcc, we
2282 // will warn the user.
2283 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2284 if (DRE->hasQualifier())
2285 S.Diag(Loc, diag::warn_cleanup_ext);
2286 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2287 NI = DRE->getNameInfo();
2288 if (!FD) {
2289 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2290 << NI.getName();
2291 return;
2292 }
2293 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2294 if (ULE->hasExplicitTemplateArgs())
2295 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002296 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2297 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002298 if (!FD) {
2299 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2300 << NI.getName();
2301 if (ULE->getType() == S.Context.OverloadTy)
2302 S.NoteAllOverloadCandidates(ULE);
2303 return;
2304 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002305 } else {
2306 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002307 return;
2308 }
2309
Anders Carlssond277d792009-01-31 01:16:18 +00002310 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002311 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2312 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002313 return;
2314 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002315
Anders Carlsson723f55d2009-02-07 23:16:50 +00002316 // We're currently more strict than GCC about what function types we accept.
2317 // If this ever proves to be a problem it should be easy to fix.
2318 QualType Ty = S.Context.getPointerType(VD->getType());
2319 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002320 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2321 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002322 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2323 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002324 return;
2325 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002326
Michael Han99315932013-01-24 16:46:58 +00002327 D->addAttr(::new (S.Context)
2328 CleanupAttr(Attr.getRange(), S.Context, FD,
2329 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002330}
2331
Mike Stumpd3bb5572009-07-24 19:02:52 +00002332/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002333/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002334static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002335 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002336 uint64_t Idx;
2337 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002338 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002339
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002340 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002341 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002342
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002343 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2344 if (not_nsstring_type &&
2345 !isCFStringType(Ty, S.Context) &&
2346 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002347 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002348 // FIXME: Should highlight the actual expression that has the wrong type.
2349 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002350 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002351 << IdxExpr->getSourceRange();
2352 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002353 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002354 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002355 if (!isNSStringType(Ty, S.Context) &&
2356 !isCFStringType(Ty, S.Context) &&
2357 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002358 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002359 // FIXME: Should highlight the actual expression that has the wrong type.
2360 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002361 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002362 << IdxExpr->getSourceRange();
2363 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002364 }
2365
Alp Toker601b22c2014-01-21 23:35:24 +00002366 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002367 // because that has corrected for the implicit this parameter, and is zero-
2368 // based. The attribute expects what the user wrote explicitly.
2369 llvm::APSInt Val;
2370 IdxExpr->EvaluateAsInt(Val, S.Context);
2371
Michael Han99315932013-01-24 16:46:58 +00002372 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002373 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002374 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002375}
2376
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002377enum FormatAttrKind {
2378 CFStringFormat,
2379 NSStringFormat,
2380 StrftimeFormat,
2381 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002382 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002383 InvalidFormat
2384};
2385
2386/// getFormatAttrKind - Map from format attribute names to supported format
2387/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002388static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002389 return llvm::StringSwitch<FormatAttrKind>(Format)
2390 // Check for formats that get handled specially.
2391 .Case("NSString", NSStringFormat)
2392 .Case("CFString", CFStringFormat)
2393 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002394
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002395 // Otherwise, check for supported formats.
2396 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2397 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2398 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002399
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002400 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2401 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002402}
2403
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002404/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002405/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002406static void handleInitPriorityAttr(Sema &S, Decl *D,
2407 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002408 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002409 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2410 return;
2411 }
2412
Aaron Ballman4a611152013-11-27 16:34:09 +00002413 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002414 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2415 Attr.setInvalid();
2416 return;
2417 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002418 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002419 if (S.Context.getAsArrayType(T))
2420 T = S.Context.getBaseElementType(T);
2421 if (!T->getAs<RecordType>()) {
2422 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2423 Attr.setInvalid();
2424 return;
2425 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002426
2427 Expr *E = Attr.getArgAsExpr(0);
2428 uint32_t prioritynum;
2429 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002430 Attr.setInvalid();
2431 return;
2432 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002433
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002434 if (prioritynum < 101 || prioritynum > 65535) {
2435 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002436 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002437 Attr.setInvalid();
2438 return;
2439 }
Michael Han99315932013-01-24 16:46:58 +00002440 D->addAttr(::new (S.Context)
2441 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2442 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002443}
2444
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002445FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2446 IdentifierInfo *Format, int FormatIdx,
2447 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002448 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002449 // Check whether we already have an equivalent format attribute.
2450 for (specific_attr_iterator<FormatAttr>
2451 i = D->specific_attr_begin<FormatAttr>(),
2452 e = D->specific_attr_end<FormatAttr>();
2453 i != e ; ++i) {
2454 FormatAttr *f = *i;
2455 if (f->getType() == Format &&
2456 f->getFormatIdx() == FormatIdx &&
2457 f->getFirstArg() == FirstArg) {
2458 // If we don't have a valid location for this attribute, adopt the
2459 // location.
2460 if (f->getLocation().isInvalid())
2461 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002462 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002463 }
2464 }
2465
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002466 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2467 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002468}
2469
Mike Stumpd3bb5572009-07-24 19:02:52 +00002470/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002471/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002472static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002473 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002474 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002475 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002476 return;
2477 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002478
Chandler Carruth743682b2010-11-16 08:35:43 +00002479 // In C++ the implicit 'this' function parameter also counts, and they are
2480 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002481 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002482 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002483
Aaron Ballman00e99962013-08-31 01:11:41 +00002484 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2485 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002486
2487 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002488 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002489 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002490 // If we've modified the string name, we need a new identifier for it.
2491 II = &S.Context.Idents.get(Format);
2492 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002493
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002494 // Check for supported formats.
2495 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002496
2497 if (Kind == IgnoredFormat)
2498 return;
2499
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002500 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002501 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002502 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002503 return;
2504 }
2505
2506 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002507 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002508 uint32_t Idx;
2509 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002510 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002511
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002512 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002513 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002514 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002515 return;
2516 }
2517
2518 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002519 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002520
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002521 if (HasImplicitThisParam) {
2522 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002523 S.Diag(Attr.getLoc(),
2524 diag::err_format_attribute_implicit_this_format_string)
2525 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002526 return;
2527 }
2528 ArgIdx--;
2529 }
Mike Stump11289f42009-09-09 15:08:12 +00002530
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002531 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002532 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002533
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002534 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002535 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002536 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2537 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002538 return;
2539 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002540 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002541 // FIXME: do we need to check if the type is NSString*? What are the
2542 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002543 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002544 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002545 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2546 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002547 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002548 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002549 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002550 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002551 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002552 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2553 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002554 return;
2555 }
2556
2557 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002558 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002559 uint32_t FirstArg;
2560 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002561 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002562
2563 // check if the function is variadic if the 3rd argument non-zero
2564 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002565 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002566 ++NumArgs; // +1 for ...
2567 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002568 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002569 return;
2570 }
2571 }
2572
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002573 // strftime requires FirstArg to be 0 because it doesn't read from any
2574 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002575 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002576 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002577 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2578 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002579 return;
2580 }
2581 // if 0 it disables parameter checking (to use with e.g. va_list)
2582 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002583 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002584 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002585 return;
2586 }
2587
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002588 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002589 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002590 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002591 if (NewAttr)
2592 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002593}
2594
Chandler Carruthedc2c642011-07-02 00:01:44 +00002595static void handleTransparentUnionAttr(Sema &S, Decl *D,
2596 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002597 // Try to find the underlying union declaration.
2598 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002599 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002600 if (TD && TD->getUnderlyingType()->isUnionType())
2601 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2602 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002603 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002604
2605 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002606 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002607 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002608 return;
2609 }
2610
John McCallf937c022011-10-07 06:10:15 +00002611 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002612 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002613 diag::warn_transparent_union_attribute_not_definition);
2614 return;
2615 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002616
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002617 RecordDecl::field_iterator Field = RD->field_begin(),
2618 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002619 if (Field == FieldEnd) {
2620 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2621 return;
2622 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002623
David Blaikie40ed2972012-06-06 20:45:41 +00002624 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002625 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002626 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002627 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002628 diag::warn_transparent_union_attribute_floating)
2629 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002630 return;
2631 }
2632
2633 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2634 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2635 for (; Field != FieldEnd; ++Field) {
2636 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002637 // FIXME: this isn't fully correct; we also need to test whether the
2638 // members of the union would all have the same calling convention as the
2639 // first member of the union. Checking just the size and alignment isn't
2640 // sufficient (consider structs passed on the stack instead of in registers
2641 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002642 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002643 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002644 // Warn if we drop the attribute.
2645 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002646 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002647 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002648 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002649 diag::warn_transparent_union_attribute_field_size_align)
2650 << isSize << Field->getDeclName() << FieldBits;
2651 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002652 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002653 diag::note_transparent_union_first_field_size_align)
2654 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002655 return;
2656 }
2657 }
2658
Michael Han99315932013-01-24 16:46:58 +00002659 RD->addAttr(::new (S.Context)
2660 TransparentUnionAttr(Attr.getRange(), S.Context,
2661 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002662}
2663
Chandler Carruthedc2c642011-07-02 00:01:44 +00002664static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002665 // Make sure that there is a string literal as the annotation's single
2666 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002667 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002668 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002669 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002670
2671 // Don't duplicate annotations that are already set.
2672 for (specific_attr_iterator<AnnotateAttr>
2673 i = D->specific_attr_begin<AnnotateAttr>(),
2674 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002675 if ((*i)->getAnnotation() == Str)
2676 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002677 }
Michael Han99315932013-01-24 16:46:58 +00002678
2679 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002680 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002681 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002682}
2683
Chandler Carruthedc2c642011-07-02 00:01:44 +00002684static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002685 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002686 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002687 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2688 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002689 return;
2690 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002691
Richard Smith848e1f12013-02-01 08:12:08 +00002692 if (Attr.getNumArgs() == 0) {
2693 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2694 true, 0, Attr.getAttributeSpellingListIndex()));
2695 return;
2696 }
2697
Aaron Ballman00e99962013-08-31 01:11:41 +00002698 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002699 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2700 S.Diag(Attr.getEllipsisLoc(),
2701 diag::err_pack_expansion_without_parameter_packs);
2702 return;
2703 }
2704
2705 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2706 return;
2707
2708 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2709 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002710}
2711
2712void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002713 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002714 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2715 SourceLocation AttrLoc = AttrRange.getBegin();
2716
Richard Smith1dba27c2013-01-29 09:02:09 +00002717 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002718 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002719 // C++11 [dcl.align]p1:
2720 // An alignment-specifier may be applied to a variable or to a class
2721 // data member, but it shall not be applied to a bit-field, a function
2722 // parameter, the formal parameter of a catch clause, or a variable
2723 // declared with the register storage class specifier. An
2724 // alignment-specifier may also be applied to the declaration of a class
2725 // or enumeration type.
2726 // C11 6.7.5/2:
2727 // An alignment attribute shall not be specified in a declaration of
2728 // a typedef, or a bit-field, or a function, or a parameter, or an
2729 // object declared with the register storage-class specifier.
2730 int DiagKind = -1;
2731 if (isa<ParmVarDecl>(D)) {
2732 DiagKind = 0;
2733 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2734 if (VD->getStorageClass() == SC_Register)
2735 DiagKind = 1;
2736 if (VD->isExceptionVariable())
2737 DiagKind = 2;
2738 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2739 if (FD->isBitField())
2740 DiagKind = 3;
2741 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00002742 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00002743 << (TmpAttr.isC11() ? ExpectedVariableOrField
2744 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002745 return;
2746 }
2747 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002748 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00002749 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002750 return;
2751 }
2752 }
2753
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002754 if (E->isTypeDependent() || E->isValueDependent()) {
2755 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002756 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2757 AA->setPackExpansion(IsPackExpansion);
2758 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002759 return;
2760 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002761
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002762 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002763 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002764 ExprResult ICE
2765 = VerifyIntegerConstantExpression(E, &Alignment,
2766 diag::err_aligned_attribute_argument_not_int,
2767 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002768 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002769 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002770
2771 // C++11 [dcl.align]p2:
2772 // -- if the constant expression evaluates to zero, the alignment
2773 // specifier shall have no effect
2774 // C11 6.7.5p6:
2775 // An alignment specification of zero has no effect.
2776 if (!(TmpAttr.isAlignas() && !Alignment) &&
2777 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002778 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2779 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002780 return;
2781 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002782
David Majnemerabecae72014-02-12 20:36:10 +00002783 // Alignment calculations can wrap around if it's greater than 2**28.
2784 unsigned MaxValidAlignment = TmpAttr.isDeclspec() ? 8192 : 268435456;
2785 if (Alignment.getZExtValue() > MaxValidAlignment) {
2786 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
2787 << E->getSourceRange();
2788 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00002789 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002790
Richard Smith44c247f2013-02-22 08:32:16 +00002791 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2792 ICE.take(), SpellingListIndex);
2793 AA->setPackExpansion(IsPackExpansion);
2794 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002795}
2796
Michael Hanaf02bbe2013-02-01 01:19:17 +00002797void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002798 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002799 // FIXME: Cache the number on the Attr object if non-dependent?
2800 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002801 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2802 SpellingListIndex);
2803 AA->setPackExpansion(IsPackExpansion);
2804 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002805}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002806
Richard Smith848e1f12013-02-01 08:12:08 +00002807void Sema::CheckAlignasUnderalignment(Decl *D) {
2808 assert(D->hasAttrs() && "no attributes on decl");
2809
2810 QualType Ty;
2811 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2812 Ty = VD->getType();
2813 else
2814 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002815 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002816 return;
2817
2818 // C++11 [dcl.align]p5, C11 6.7.5/4:
2819 // The combined effect of all alignment attributes in a declaration shall
2820 // not specify an alignment that is less strict than the alignment that
2821 // would otherwise be required for the entity being declared.
2822 AlignedAttr *AlignasAttr = 0;
2823 unsigned Align = 0;
2824 for (specific_attr_iterator<AlignedAttr>
2825 I = D->specific_attr_begin<AlignedAttr>(),
2826 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
2827 if (I->isAlignmentDependent())
2828 return;
2829 if (I->isAlignas())
2830 AlignasAttr = *I;
2831 Align = std::max(Align, I->getAlignment(Context));
2832 }
2833
2834 if (AlignasAttr && Align) {
2835 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2836 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2837 if (NaturalAlign > RequestedAlign)
2838 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2839 << Ty << (unsigned)NaturalAlign.getQuantity();
2840 }
2841}
2842
David Majnemer2c4e00a2014-01-29 22:07:36 +00002843bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00002844 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00002845 MSInheritanceAttr::Spelling SemanticSpelling) {
2846 assert(RD->hasDefinition() && "RD has no definition!");
2847
David Majnemer98c9ee22014-02-07 00:43:07 +00002848 // We may not have seen base specifiers or any virtual methods yet. We will
2849 // have to wait until the record is defined to catch any mismatches.
2850 if (!RD->getDefinition()->isCompleteDefinition())
2851 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002852
David Majnemer98c9ee22014-02-07 00:43:07 +00002853 // The unspecified model never matches what a definition could need.
2854 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
2855 return false;
2856
David Majnemer4bb09802014-02-10 19:50:15 +00002857 if (BestCase) {
2858 if (RD->calculateInheritanceModel() == SemanticSpelling)
2859 return false;
2860 } else {
2861 if (RD->calculateInheritanceModel() <= SemanticSpelling)
2862 return false;
2863 }
David Majnemer98c9ee22014-02-07 00:43:07 +00002864
2865 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
2866 << 0 /*definition*/;
2867 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
2868 << RD->getNameAsString();
2869 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002870}
2871
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002872/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002873/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002874///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002875/// Despite what would be logical, the mode attribute is a decl attribute, not a
2876/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2877/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002878static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002879 // This attribute isn't documented, but glibc uses it. It changes
2880 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002881 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002882 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2883 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002884 return;
2885 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002886
Aaron Ballman00e99962013-08-31 01:11:41 +00002887 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2888 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002889
2890 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002891 if (Str.startswith("__") && Str.endswith("__"))
2892 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002893
2894 unsigned DestWidth = 0;
2895 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002896 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002897 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002898 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002899 switch (Str[0]) {
2900 case 'Q': DestWidth = 8; break;
2901 case 'H': DestWidth = 16; break;
2902 case 'S': DestWidth = 32; break;
2903 case 'D': DestWidth = 64; break;
2904 case 'X': DestWidth = 96; break;
2905 case 'T': DestWidth = 128; break;
2906 }
2907 if (Str[1] == 'F') {
2908 IntegerMode = false;
2909 } else if (Str[1] == 'C') {
2910 IntegerMode = false;
2911 ComplexMode = true;
2912 } else if (Str[1] != 'I') {
2913 DestWidth = 0;
2914 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002915 break;
2916 case 4:
2917 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2918 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002919 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002920 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002921 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002922 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002923 break;
2924 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002925 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002926 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002927 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002928 case 11:
2929 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00002930 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002931 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002932 }
2933
2934 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002935 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002936 OldTy = TD->getUnderlyingType();
2937 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2938 OldTy = VD->getType();
2939 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002940 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00002941 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002942 return;
2943 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002944
John McCall9dd450b2009-09-21 23:43:11 +00002945 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002946 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2947 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002948 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002949 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2950 } else if (ComplexMode) {
2951 if (!OldTy->isComplexType())
2952 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2953 } else {
2954 if (!OldTy->isFloatingType())
2955 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2956 }
2957
Mike Stump87c57ac2009-05-16 07:39:55 +00002958 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2959 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002960 // FIXME: Make sure floating-point mappings are accurate
2961 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002962 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00002963 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002964 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002965 }
2966
2967 QualType NewTy;
2968
2969 if (IntegerMode)
2970 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
2971 OldTy->isSignedIntegerType());
2972 else
2973 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
2974
2975 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00002976 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002977 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002978 }
2979
Eli Friedman4735374e2009-03-03 06:41:03 +00002980 if (ComplexMode) {
2981 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002982 }
2983
2984 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002985 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
2986 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
2987 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00002988 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002989
2990 D->addAttr(::new (S.Context)
2991 ModeAttr(Attr.getRange(), S.Context, Name,
2992 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00002993}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002994
Chandler Carruthedc2c642011-07-02 00:01:44 +00002995static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00002996 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2997 if (!VD->hasGlobalStorage())
2998 S.Diag(Attr.getLoc(),
2999 diag::warn_attribute_requires_functions_or_static_globals)
3000 << Attr.getName();
3001 } else if (!isFunctionOrMethod(D)) {
3002 S.Diag(Attr.getLoc(),
3003 diag::warn_attribute_requires_functions_or_static_globals)
3004 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003005 return;
3006 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003007
Michael Han99315932013-01-24 16:46:58 +00003008 D->addAttr(::new (S.Context)
3009 NoDebugAttr(Attr.getRange(), S.Context,
3010 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003011}
3012
Chandler Carruthedc2c642011-07-02 00:01:44 +00003013static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003014 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003015 if (!FD->getReturnType()->isVoidType()) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003016 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
3017 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
3018 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3019 << FD->getType()
Alp Toker42a16a62014-01-25 23:51:36 +00003020 << FixItHint::CreateReplacement(FTL.getReturnLoc().getSourceRange(),
Aaron Ballman3aff6332013-12-02 19:30:36 +00003021 "void");
3022 } else {
3023 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3024 << FD->getType();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003025 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003026 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003027 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003028
Aaron Ballman3aff6332013-12-02 19:30:36 +00003029 D->addAttr(::new (S.Context)
3030 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003031 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003032}
3033
Chandler Carruthedc2c642011-07-02 00:01:44 +00003034static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003035 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003036 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003037 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003038 return;
3039 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003040
Michael Han99315932013-01-24 16:46:58 +00003041 D->addAttr(::new (S.Context)
3042 GNUInlineAttr(Attr.getRange(), S.Context,
3043 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003044}
3045
Chandler Carruthedc2c642011-07-02 00:01:44 +00003046static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003047 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003048
Aaron Ballman02df2e02012-12-09 17:45:41 +00003049 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003050 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003051 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3052 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003053 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003054 return;
3055
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003056 if (!isa<ObjCMethodDecl>(D)) {
3057 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3058 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003059 return;
3060 }
3061
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003062 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003063 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003064 D->addAttr(::new (S.Context)
3065 FastCallAttr(Attr.getRange(), S.Context,
3066 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003067 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003068 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003069 D->addAttr(::new (S.Context)
3070 StdCallAttr(Attr.getRange(), S.Context,
3071 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003072 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003073 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003074 D->addAttr(::new (S.Context)
3075 ThisCallAttr(Attr.getRange(), S.Context,
3076 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003077 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003078 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003079 D->addAttr(::new (S.Context)
3080 CDeclAttr(Attr.getRange(), S.Context,
3081 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003082 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003083 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003084 D->addAttr(::new (S.Context)
3085 PascalAttr(Attr.getRange(), S.Context,
3086 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003087 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003088 case AttributeList::AT_MSABI:
3089 D->addAttr(::new (S.Context)
3090 MSABIAttr(Attr.getRange(), S.Context,
3091 Attr.getAttributeSpellingListIndex()));
3092 return;
3093 case AttributeList::AT_SysVABI:
3094 D->addAttr(::new (S.Context)
3095 SysVABIAttr(Attr.getRange(), S.Context,
3096 Attr.getAttributeSpellingListIndex()));
3097 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003098 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003099 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003100 switch (CC) {
3101 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003102 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003103 break;
3104 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003105 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003106 break;
3107 default:
3108 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003109 }
3110
Michael Han99315932013-01-24 16:46:58 +00003111 D->addAttr(::new (S.Context)
3112 PcsAttr(Attr.getRange(), S.Context, PCS,
3113 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003114 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003115 }
Derek Schuffa2020962012-10-16 22:30:41 +00003116 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003117 D->addAttr(::new (S.Context)
3118 PnaclCallAttr(Attr.getRange(), S.Context,
3119 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003120 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003121 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003122 D->addAttr(::new (S.Context)
3123 IntelOclBiccAttr(Attr.getRange(), S.Context,
3124 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003125 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003126
Abramo Bagnara50099372010-04-30 13:10:51 +00003127 default:
3128 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003129 }
3130}
3131
Aaron Ballman02df2e02012-12-09 17:45:41 +00003132bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3133 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003134 if (attr.isInvalid())
3135 return true;
3136
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003137 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003138 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003139 attr.setInvalid();
3140 return true;
3141 }
3142
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003143 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003144 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003145 case AttributeList::AT_CDecl: CC = CC_C; break;
3146 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3147 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3148 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3149 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003150 case AttributeList::AT_MSABI:
3151 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3152 CC_X86_64Win64;
3153 break;
3154 case AttributeList::AT_SysVABI:
3155 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3156 CC_C;
3157 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003158 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003159 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003160 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003161 attr.setInvalid();
3162 return true;
3163 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003164 if (StrRef == "aapcs") {
3165 CC = CC_AAPCS;
3166 break;
3167 } else if (StrRef == "aapcs-vfp") {
3168 CC = CC_AAPCS_VFP;
3169 break;
3170 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003171
3172 attr.setInvalid();
3173 Diag(attr.getLoc(), diag::err_invalid_pcs);
3174 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003175 }
Derek Schuffa2020962012-10-16 22:30:41 +00003176 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003177 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003178 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003179 }
3180
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003181 const TargetInfo &TI = Context.getTargetInfo();
3182 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3183 if (A == TargetInfo::CCCR_Warning) {
3184 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003185
3186 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3187 if (FD)
3188 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3189 TargetInfo::CCMT_NonMember;
3190 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003191 }
3192
John McCall3882ace2011-01-05 12:14:39 +00003193 return false;
3194}
3195
John McCall3882ace2011-01-05 12:14:39 +00003196/// Checks a regparm attribute, returning true if it is ill-formed and
3197/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003198bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3199 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003200 return true;
3201
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003202 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003203 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003204 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003205 }
Eli Friedman7044b762009-03-27 21:06:47 +00003206
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003207 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003208 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003209 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003210 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003211 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003212 }
3213
Douglas Gregore8bbc122011-09-02 00:18:52 +00003214 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003215 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003216 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003217 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003218 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003219 }
3220
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003221 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003222 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003223 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003224 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003225 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003226 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003227 }
3228
John McCall3882ace2011-01-05 12:14:39 +00003229 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003230}
3231
Aaron Ballman66039932013-12-19 00:41:31 +00003232static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3233 const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003234 // check the attribute arguments.
3235 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3236 // FIXME: 0 is not okay.
Aaron Ballman05e420a2014-01-02 21:26:14 +00003237 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3238 << Attr.getName() << 2;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003239 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003240 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003241
Aaron Ballman66039932013-12-19 00:41:31 +00003242 uint32_t MaxThreads, MinBlocks = 0;
3243 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3244 return;
3245 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3246 Attr.getArgAsExpr(1),
3247 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003248 return;
3249
3250 D->addAttr(::new (S.Context)
3251 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3252 MaxThreads, MinBlocks,
3253 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003254}
3255
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003256static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3257 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003258 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003259 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003260 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003261 return;
3262 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003263
3264 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003265 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003266
Aaron Ballman00e99962013-08-31 01:11:41 +00003267 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003268
3269 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3270 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3271 << Attr.getName() << ExpectedFunctionOrMethod;
3272 return;
3273 }
3274
3275 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003276 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3277 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003278 return;
3279
3280 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003281 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3282 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003283 return;
3284
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003285 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003286 if (IsPointer) {
3287 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003288 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003289 if (!BufferTy->isPointerType()) {
3290 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003291 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003292 }
3293 }
3294
Michael Han99315932013-01-24 16:46:58 +00003295 D->addAttr(::new (S.Context)
3296 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3297 ArgumentIdx, TypeTagIdx, IsPointer,
3298 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003299}
3300
3301static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3302 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003303 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003304 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003305 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003306 return;
3307 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003308
3309 if (!checkAttributeNumArgs(S, Attr, 1))
3310 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003311
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003312 if (!isa<VarDecl>(D)) {
3313 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3314 << Attr.getName() << ExpectedVariable;
3315 return;
3316 }
3317
Aaron Ballman00e99962013-08-31 01:11:41 +00003318 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003319 TypeSourceInfo *MatchingCTypeLoc = 0;
3320 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3321 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003322
Michael Han99315932013-01-24 16:46:58 +00003323 D->addAttr(::new (S.Context)
3324 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003325 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003326 Attr.getLayoutCompatible(),
3327 Attr.getMustBeNull(),
3328 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003329}
3330
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003331//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003332// Checker-specific attribute handlers.
3333//===----------------------------------------------------------------------===//
3334
John McCalled433932011-01-25 03:31:58 +00003335static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003336 return type->isDependentType() ||
3337 type->isObjCObjectPointerType() ||
3338 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003339}
3340static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003341 return type->isDependentType() ||
3342 type->isPointerType() ||
3343 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003344}
3345
Chandler Carruthedc2c642011-07-02 00:01:44 +00003346static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003347 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003348 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003349
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003350 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003351 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3352 cf = false;
3353 } else {
3354 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3355 cf = true;
3356 }
3357
3358 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003359 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003360 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003361 return;
3362 }
3363
3364 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003365 param->addAttr(::new (S.Context)
3366 CFConsumedAttr(Attr.getRange(), S.Context,
3367 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003368 else
Michael Han99315932013-01-24 16:46:58 +00003369 param->addAttr(::new (S.Context)
3370 NSConsumedAttr(Attr.getRange(), S.Context,
3371 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003372}
3373
Chandler Carruthedc2c642011-07-02 00:01:44 +00003374static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3375 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003376
John McCalled433932011-01-25 03:31:58 +00003377 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003378
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003379 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003380 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003381 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003382 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003383 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003384 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3385 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003386 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003387 returnType = FD->getReturnType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003388 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003389 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003390 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003391 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003392 return;
3393 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003394
John McCalled433932011-01-25 03:31:58 +00003395 bool typeOK;
3396 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003397 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003398 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003399 case AttributeList::AT_NSReturnsAutoreleased:
3400 case AttributeList::AT_NSReturnsRetained:
3401 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003402 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3403 cf = false;
3404 break;
3405
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003406 case AttributeList::AT_CFReturnsRetained:
3407 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003408 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3409 cf = true;
3410 break;
3411 }
3412
3413 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003414 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003415 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003416 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003417 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003418
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003419 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003420 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003421 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003422 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003423 D->addAttr(::new (S.Context)
3424 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3425 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003426 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003427 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003428 D->addAttr(::new (S.Context)
3429 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3430 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003431 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003432 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003433 D->addAttr(::new (S.Context)
3434 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3435 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003436 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003437 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003438 D->addAttr(::new (S.Context)
3439 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3440 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003441 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003442 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003443 D->addAttr(::new (S.Context)
3444 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3445 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003446 return;
3447 };
3448}
3449
John McCallcf166702011-07-22 08:53:00 +00003450static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3451 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003452 const int EP_ObjCMethod = 1;
3453 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003454
John McCallcf166702011-07-22 08:53:00 +00003455 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003456 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003457 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003458 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003459 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003460 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003461
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003462 if (!resultType->isReferenceType() &&
3463 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003464 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003465 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003466 << attr.getName()
3467 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003468 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003469
3470 // Drop the attribute.
3471 return;
3472 }
3473
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003474 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003475 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3476 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003477}
3478
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003479static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3480 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003481 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003482
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003483 DeclContext *DC = method->getDeclContext();
3484 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3485 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3486 << attr.getName() << 0;
3487 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3488 return;
3489 }
3490 if (method->getMethodFamily() == OMF_dealloc) {
3491 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3492 << attr.getName() << 1;
3493 return;
3494 }
3495
Michael Han99315932013-01-24 16:46:58 +00003496 method->addAttr(::new (S.Context)
3497 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3498 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003499}
3500
Aaron Ballmanfb763042013-12-02 18:05:46 +00003501static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3502 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003503 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00003504 return;
John McCall32f5fe12011-09-30 05:12:12 +00003505
Aaron Ballmanfb763042013-12-02 18:05:46 +00003506 D->addAttr(::new (S.Context)
3507 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3508 Attr.getAttributeSpellingListIndex()));
3509}
3510
3511static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3512 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003513 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00003514 return;
3515
3516 D->addAttr(::new (S.Context)
3517 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3518 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003519}
3520
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003521static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3522 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003523 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003524
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003525 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003526 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003527 return;
3528 }
3529
3530 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003531 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003532 Attr.getAttributeSpellingListIndex()));
3533}
3534
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003535static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3536 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003537 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003538
3539 if (!Parm) {
3540 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3541 return;
3542 }
3543
3544 D->addAttr(::new (S.Context)
3545 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3546 Attr.getAttributeSpellingListIndex()));
3547}
3548
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003549static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3550 const AttributeList &Attr) {
3551 IdentifierInfo *RelatedClass =
3552 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0;
3553 if (!RelatedClass) {
3554 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3555 return;
3556 }
3557 IdentifierInfo *ClassMethod =
3558 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0;
3559 IdentifierInfo *InstanceMethod =
3560 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0;
3561 D->addAttr(::new (S.Context)
3562 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3563 ClassMethod, InstanceMethod,
3564 Attr.getAttributeSpellingListIndex()));
3565}
3566
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003567static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3568 const AttributeList &Attr) {
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003569 ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003570 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003571 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003572 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3573 Attr.getAttributeSpellingListIndex()));
3574}
3575
Chandler Carruthedc2c642011-07-02 00:01:44 +00003576static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3577 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003578 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003579
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003580 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003581 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003582}
3583
Chandler Carruthedc2c642011-07-02 00:01:44 +00003584static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3585 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003586 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003587 QualType type = vd->getType();
3588
3589 if (!type->isDependentType() &&
3590 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003591 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003592 << type;
3593 return;
3594 }
3595
3596 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3597
3598 // If we have no lifetime yet, check the lifetime we're presumably
3599 // going to infer.
3600 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3601 lifetime = type->getObjCARCImplicitLifetime();
3602
3603 switch (lifetime) {
3604 case Qualifiers::OCL_None:
3605 assert(type->isDependentType() &&
3606 "didn't infer lifetime for non-dependent type?");
3607 break;
3608
3609 case Qualifiers::OCL_Weak: // meaningful
3610 case Qualifiers::OCL_Strong: // meaningful
3611 break;
3612
3613 case Qualifiers::OCL_ExplicitNone:
3614 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003615 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003616 << (lifetime == Qualifiers::OCL_Autoreleasing);
3617 break;
3618 }
3619
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003620 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003621 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3622 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003623}
3624
Francois Picheta83957a2010-12-19 06:50:37 +00003625//===----------------------------------------------------------------------===//
3626// Microsoft specific attribute handlers.
3627//===----------------------------------------------------------------------===//
3628
Chandler Carruthedc2c642011-07-02 00:01:44 +00003629static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003630 if (!S.LangOpts.CPlusPlus) {
3631 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3632 << Attr.getName() << AttributeLangSupport::C;
3633 return;
3634 }
3635
Aaron Ballman60e705e2013-11-24 20:58:02 +00003636 if (!isa<CXXRecordDecl>(D)) {
3637 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3638 << Attr.getName() << ExpectedClass;
3639 return;
3640 }
3641
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003642 StringRef StrRef;
3643 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003644 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003645 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003646
David Majnemer89085342013-08-09 08:56:20 +00003647 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3648 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003649 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3650 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003651
Reid Kleckner140c4a72013-05-17 14:04:52 +00003652 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003653 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003654 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003655 return;
3656 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003657
David Majnemer89085342013-08-09 08:56:20 +00003658 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003659 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003660 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003661 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003662 return;
3663 }
David Majnemer89085342013-08-09 08:56:20 +00003664 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003665 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003666 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003667 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003668 }
Francois Picheta83957a2010-12-19 06:50:37 +00003669
David Majnemer89085342013-08-09 08:56:20 +00003670 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3671 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003672}
3673
David Majnemer2c4e00a2014-01-29 22:07:36 +00003674static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3675 if (!S.LangOpts.CPlusPlus) {
3676 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3677 << Attr.getName() << AttributeLangSupport::C;
3678 return;
3679 }
3680 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00003681 D, Attr.getRange(), /*BestCase=*/true,
3682 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00003683 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
3684 if (IA)
3685 D->addAttr(IA);
3686}
3687
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003688static void handleARMInterruptAttr(Sema &S, Decl *D,
3689 const AttributeList &Attr) {
3690 // Check the attribute arguments.
3691 if (Attr.getNumArgs() > 1) {
3692 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3693 << Attr.getName() << 1;
3694 return;
3695 }
3696
3697 StringRef Str;
3698 SourceLocation ArgLoc;
3699
3700 if (Attr.getNumArgs() == 0)
3701 Str = "";
3702 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
3703 return;
3704
3705 ARMInterruptAttr::InterruptType Kind;
3706 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
3707 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
3708 << Attr.getName() << Str << ArgLoc;
3709 return;
3710 }
3711
3712 unsigned Index = Attr.getAttributeSpellingListIndex();
3713 D->addAttr(::new (S.Context)
3714 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
3715}
3716
3717static void handleMSP430InterruptAttr(Sema &S, Decl *D,
3718 const AttributeList &Attr) {
3719 if (!checkAttributeNumArgs(S, Attr, 1))
3720 return;
3721
3722 if (!Attr.isArgExpr(0)) {
3723 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3724 << AANT_ArgumentIntegerConstant;
3725 return;
3726 }
3727
3728 // FIXME: Check for decl - it should be void ()(void).
3729
3730 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3731 llvm::APSInt NumParams(32);
3732 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
3733 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3734 << Attr.getName() << AANT_ArgumentIntegerConstant
3735 << NumParamsExpr->getSourceRange();
3736 return;
3737 }
3738
3739 unsigned Num = NumParams.getLimitedValue(255);
3740 if ((Num & 1) || Num > 30) {
3741 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3742 << Attr.getName() << (int)NumParams.getSExtValue()
3743 << NumParamsExpr->getSourceRange();
3744 return;
3745 }
3746
Aaron Ballman36a53502014-01-16 13:03:14 +00003747 D->addAttr(::new (S.Context)
3748 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
3749 Attr.getAttributeSpellingListIndex()));
3750 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003751}
3752
3753static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3754 // Dispatch the interrupt attribute based on the current target.
3755 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
3756 handleMSP430InterruptAttr(S, D, Attr);
3757 else
3758 handleARMInterruptAttr(S, D, Attr);
3759}
3760
3761static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
3762 const AttributeList& Attr) {
3763 // If we try to apply it to a function pointer, don't warn, but don't
3764 // do anything, either. It doesn't matter anyway, because there's nothing
3765 // special about calling a force_align_arg_pointer function.
3766 ValueDecl *VD = dyn_cast<ValueDecl>(D);
3767 if (VD && VD->getType()->isFunctionPointerType())
3768 return;
3769 // Also don't warn on function pointer typedefs.
3770 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
3771 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
3772 TD->getUnderlyingType()->isFunctionType()))
3773 return;
3774 // Attribute can only be applied to function types.
3775 if (!isa<FunctionDecl>(D)) {
3776 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3777 << Attr.getName() << /* function */0;
3778 return;
3779 }
3780
Aaron Ballman36a53502014-01-16 13:03:14 +00003781 D->addAttr(::new (S.Context)
3782 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
3783 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003784}
3785
3786DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
3787 unsigned AttrSpellingListIndex) {
3788 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00003789 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003790 return NULL;
3791 }
3792
3793 if (D->hasAttr<DLLImportAttr>())
3794 return NULL;
3795
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003796 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003797}
3798
3799static void handleDLLImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3800 // Attribute can be applied only to functions or variables.
3801 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3802 if (!FD && !isa<VarDecl>(D)) {
3803 // Apparently Visual C++ thinks it is okay to not emit a warning
3804 // in this case, so only emit a warning when -fms-extensions is not
3805 // specified.
3806 if (!S.getLangOpts().MicrosoftExt)
3807 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003808 << Attr.getName() << ExpectedVariableOrFunction;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003809 return;
3810 }
3811
3812 // Currently, the dllimport attribute is ignored for inlined functions.
3813 // Warning is emitted.
3814 if (FD && FD->isInlineSpecified()) {
3815 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3816 return;
3817 }
3818
3819 unsigned Index = Attr.getAttributeSpellingListIndex();
3820 DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index);
3821 if (NewAttr)
3822 D->addAttr(NewAttr);
3823}
3824
3825DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
3826 unsigned AttrSpellingListIndex) {
3827 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00003828 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003829 D->dropAttr<DLLImportAttr>();
3830 }
3831
3832 if (D->hasAttr<DLLExportAttr>())
3833 return NULL;
3834
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003835 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003836}
3837
3838static void handleDLLExportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3839 // Currently, the dllexport attribute is ignored for inlined functions, unless
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003840 // the -fkeep-inline-functions flag has been used. Warning is emitted.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003841 if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isInlineSpecified()) {
3842 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
3843 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3844 return;
3845 }
3846
3847 unsigned Index = Attr.getAttributeSpellingListIndex();
3848 DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index);
3849 if (NewAttr)
3850 D->addAttr(NewAttr);
3851}
3852
David Majnemer2c4e00a2014-01-29 22:07:36 +00003853MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00003854Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003855 unsigned AttrSpellingListIndex,
3856 MSInheritanceAttr::Spelling SemanticSpelling) {
3857 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
3858 if (IA->getSemanticSpelling() == SemanticSpelling)
3859 return 0;
3860 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
3861 << 1 /*previous declaration*/;
3862 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
3863 D->dropAttr<MSInheritanceAttr>();
3864 }
3865
3866 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
3867 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00003868 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
3869 SemanticSpelling)) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00003870 return 0;
3871 }
3872 } else {
3873 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
3874 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
3875 << 1 /*partial specialization*/;
3876 return 0;
3877 }
3878 if (RD->getDescribedClassTemplate()) {
3879 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
3880 << 0 /*primary template*/;
3881 return 0;
3882 }
3883 }
3884
3885 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00003886 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00003887}
3888
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003889static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3890 // The capability attributes take a single string parameter for the name of
3891 // the capability they represent. The lockable attribute does not take any
3892 // parameters. However, semantically, both attributes represent the same
3893 // concept, and so they use the same semantic attribute. Eventually, the
3894 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00003895 //
3896 // For backwards compatibility, any capability which has no specified string
3897 // literal will be considered a "mutex."
3898 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003899 SourceLocation LiteralLoc;
3900 if (Attr.getKind() == AttributeList::AT_Capability &&
3901 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
3902 return;
3903
Aaron Ballman6c810072014-03-05 21:47:13 +00003904 // Currently, there are only two names allowed for a capability: role and
3905 // mutex (case insensitive). Diagnose other capability names.
3906 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
3907 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
3908
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003909 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
3910 Attr.getAttributeSpellingListIndex()));
3911}
3912
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003913static void handleAssertCapabilityAttr(Sema &S, Decl *D,
3914 const AttributeList &Attr) {
3915 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
3916 Attr.getArgAsExpr(0),
3917 Attr.getAttributeSpellingListIndex()));
3918}
3919
3920static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
3921 const AttributeList &Attr) {
3922 SmallVector<Expr*, 1> Args;
3923 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
3924 return;
3925
3926 // Check that all arguments are lockable objects.
3927 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
3928 if (Args.empty())
3929 return;
3930
3931 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
3932 S.Context,
3933 Args.data(), Args.size(),
3934 Attr.getAttributeSpellingListIndex()));
3935}
3936
3937static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
3938 const AttributeList &Attr) {
3939 SmallVector<Expr*, 2> Args;
3940 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
3941 return;
3942
3943 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
3944 S.Context,
3945 Attr.getArgAsExpr(0),
3946 Args.data(),
3947 Args.size(),
3948 Attr.getAttributeSpellingListIndex()));
3949}
3950
3951static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
3952 const AttributeList &Attr) {
3953 SmallVector<Expr*, 1> Args;
3954 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
3955 return;
3956
3957 // Check that all arguments are lockable objects.
3958 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
3959 if (Args.empty())
3960 return;
3961
3962 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(Attr.getRange(),
3963 S.Context,
3964 Args.data(), Args.size(),
3965 Attr.getAttributeSpellingListIndex()));
3966}
3967
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003968static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
3969 const AttributeList &Attr) {
3970 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
3971 return;
3972
3973 // check that all arguments are lockable objects
3974 SmallVector<Expr*, 1> Args;
3975 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
3976 if (Args.empty())
3977 return;
3978
3979 RequiresCapabilityAttr *RCA = ::new (S.Context)
3980 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
3981 Args.size(), Attr.getAttributeSpellingListIndex());
3982
3983 D->addAttr(RCA);
3984}
3985
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003986/// Handles semantic checking for features that are common to all attributes,
3987/// such as checking whether a parameter was properly specified, or the correct
3988/// number of arguments were passed, etc.
3989static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3990 const AttributeList &Attr) {
3991 // Several attributes carry different semantics than the parsing requires, so
3992 // those are opted out of the common handling.
3993 //
3994 // We also bail on unknown and ignored attributes because those are handled
3995 // as part of the target-specific handling logic.
3996 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003997 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003998 return false;
3999
Aaron Ballman3aff6332013-12-02 19:30:36 +00004000 // Check whether the attribute requires specific language extensions to be
4001 // enabled.
4002 if (!Attr.diagnoseLangOpts(S))
4003 return true;
4004
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004005 // If there are no optional arguments, then checking for the argument count
4006 // is trivial.
4007 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4008 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4009 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004010
4011 // Check whether the attribute appertains to the given subject.
4012 if (!Attr.diagnoseAppertainsTo(S, D))
4013 return true;
4014
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004015 return false;
4016}
4017
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004018//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004019// Top Level Sema Entry Points
4020//===----------------------------------------------------------------------===//
4021
Richard Smithf8a75c32013-08-29 00:47:48 +00004022/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4023/// the attribute applies to decls. If the attribute is a type attribute, just
4024/// silently ignore it if a GNU attribute.
4025static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4026 const AttributeList &Attr,
4027 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004028 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00004029 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004030
Richard Smithf8a75c32013-08-29 00:47:48 +00004031 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4032 // instead.
4033 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4034 return;
4035
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004036 // Unknown attributes are automatically warned on. Target-specific attributes
4037 // which do not apply to the current target architecture are treated as
4038 // though they were unknown attributes.
4039 if (Attr.getKind() == AttributeList::UnknownAttribute ||
4040 !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) {
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004041 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute()
4042 ? diag::warn_unhandled_ms_attribute_ignored
4043 : diag::warn_unknown_attribute_ignored)
4044 << Attr.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004045 return;
4046 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004047
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004048 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4049 return;
4050
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004051 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004052 default:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004053 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004054 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004055 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004056 case AttributeList::AT_Interrupt:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004057 handleInterruptAttr(S, D, Attr);
4058 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004059 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004060 handleX86ForceAlignArgPointerAttr(S, D, Attr);
4061 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004062 case AttributeList::AT_DLLExport:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004063 handleDLLExportAttr(S, D, Attr);
4064 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004065 case AttributeList::AT_DLLImport:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004066 handleDLLImportAttr(S, D, Attr);
4067 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004068 case AttributeList::AT_Mips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004069 handleSimpleAttribute<Mips16Attr>(S, D, Attr);
4070 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004071 case AttributeList::AT_NoMips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004072 handleSimpleAttribute<NoMips16Attr>(S, D, Attr);
4073 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00004074 case AttributeList::AT_IBAction:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004075 handleSimpleAttribute<IBActionAttr>(S, D, Attr);
4076 break;
4077 case AttributeList::AT_IBOutlet:
4078 handleIBOutlet(S, D, Attr);
4079 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004080 case AttributeList::AT_IBOutletCollection:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004081 handleIBOutletCollection(S, D, Attr);
4082 break;
4083 case AttributeList::AT_Alias:
4084 handleAliasAttr(S, D, Attr);
4085 break;
4086 case AttributeList::AT_Aligned:
4087 handleAlignedAttr(S, D, Attr);
4088 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004089 case AttributeList::AT_AlwaysInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004090 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr);
4091 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004092 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004093 handleAnalyzerNoReturnAttr(S, D, Attr);
4094 break;
4095 case AttributeList::AT_TLSModel:
4096 handleTLSModelAttr(S, D, Attr);
4097 break;
4098 case AttributeList::AT_Annotate:
4099 handleAnnotateAttr(S, D, Attr);
4100 break;
4101 case AttributeList::AT_Availability:
4102 handleAvailabilityAttr(S, D, Attr);
4103 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004104 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004105 handleDependencyAttr(S, scope, D, Attr);
4106 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004107 case AttributeList::AT_Common:
4108 handleCommonAttr(S, D, Attr);
4109 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004110 case AttributeList::AT_CUDAConstant:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004111 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr);
4112 break;
4113 case AttributeList::AT_Constructor:
4114 handleConstructorAttr(S, D, Attr);
4115 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004116 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004117 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr);
4118 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004119 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004120 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004121 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004122 case AttributeList::AT_Destructor:
4123 handleDestructorAttr(S, D, Attr);
4124 break;
4125 case AttributeList::AT_EnableIf:
4126 handleEnableIfAttr(S, D, Attr);
4127 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004128 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004129 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004130 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004131 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004132 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004133 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004134 case AttributeList::AT_Format:
4135 handleFormatAttr(S, D, Attr);
4136 break;
4137 case AttributeList::AT_FormatArg:
4138 handleFormatArgAttr(S, D, Attr);
4139 break;
4140 case AttributeList::AT_CUDAGlobal:
4141 handleGlobalAttr(S, D, Attr);
4142 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00004143 case AttributeList::AT_CUDADevice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004144 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr);
4145 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004146 case AttributeList::AT_CUDAHost:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004147 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr);
4148 break;
4149 case AttributeList::AT_GNUInline:
4150 handleGNUInlineAttr(S, D, Attr);
4151 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004152 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004153 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004154 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004155 case AttributeList::AT_Malloc:
4156 handleMallocAttr(S, D, Attr);
4157 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004158 case AttributeList::AT_MayAlias:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004159 handleSimpleAttribute<MayAliasAttr>(S, D, Attr);
4160 break;
4161 case AttributeList::AT_Mode:
4162 handleModeAttr(S, D, Attr);
4163 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004164 case AttributeList::AT_NoCommon:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004165 handleSimpleAttribute<NoCommonAttr>(S, D, Attr);
4166 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00004167 case AttributeList::AT_NonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004168 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
4169 handleNonNullAttrParameter(S, PVD, Attr);
4170 else
4171 handleNonNullAttr(S, D, Attr);
4172 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00004173 case AttributeList::AT_ReturnsNonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004174 handleReturnsNonNullAttr(S, D, Attr);
4175 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004176 case AttributeList::AT_Overloadable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004177 handleSimpleAttribute<OverloadableAttr>(S, D, Attr);
4178 break;
4179 case AttributeList::AT_Ownership:
4180 handleOwnershipAttr(S, D, Attr);
4181 break;
4182 case AttributeList::AT_Cold:
4183 handleColdAttr(S, D, Attr);
4184 break;
4185 case AttributeList::AT_Hot:
4186 handleHotAttr(S, D, Attr);
4187 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004188 case AttributeList::AT_Naked:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004189 handleSimpleAttribute<NakedAttr>(S, D, Attr);
4190 break;
4191 case AttributeList::AT_NoReturn:
4192 handleNoReturnAttr(S, D, Attr);
4193 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004194 case AttributeList::AT_NoThrow:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004195 handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
4196 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004197 case AttributeList::AT_CUDAShared:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004198 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr);
4199 break;
4200 case AttributeList::AT_VecReturn:
4201 handleVecReturnAttr(S, D, Attr);
4202 break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004203
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004204 case AttributeList::AT_ObjCOwnership:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004205 handleObjCOwnershipAttr(S, D, Attr);
4206 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004207 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004208 handleObjCPreciseLifetimeAttr(S, D, Attr);
4209 break;
John McCall31168b02011-06-15 23:02:42 +00004210
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004211 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004212 handleObjCReturnsInnerPointerAttr(S, D, Attr);
4213 break;
John McCallcf166702011-07-22 08:53:00 +00004214
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004215 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004216 handleObjCRequiresSuperAttr(S, D, Attr);
4217 break;
4218
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004219 case AttributeList::AT_ObjCBridge:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004220 handleObjCBridgeAttr(S, scope, D, Attr);
4221 break;
4222
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004223 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004224 handleObjCBridgeMutableAttr(S, scope, D, Attr);
4225 break;
4226
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004227 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004228 handleObjCBridgeRelatedAttr(S, scope, D, Attr);
4229 break;
John McCallf1e8b342011-09-29 07:17:38 +00004230
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004231 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004232 handleObjCDesignatedInitializer(S, D, Attr);
4233 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004234
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004235 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004236 handleCFAuditedTransferAttr(S, D, Attr);
4237 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004238 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004239 handleCFUnknownTransferAttr(S, D, Attr);
4240 break;
John McCall32f5fe12011-09-30 05:12:12 +00004241
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004242 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004243 case AttributeList::AT_NSConsumed:
4244 handleNSConsumedAttr(S, D, Attr);
4245 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004246 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004247 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr);
4248 break;
John McCalled433932011-01-25 03:31:58 +00004249
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004250 case AttributeList::AT_NSReturnsAutoreleased:
4251 case AttributeList::AT_NSReturnsNotRetained:
4252 case AttributeList::AT_CFReturnsNotRetained:
4253 case AttributeList::AT_NSReturnsRetained:
4254 case AttributeList::AT_CFReturnsRetained:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004255 handleNSReturnsRetainedAttr(S, D, Attr);
4256 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004257 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004258 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr);
4259 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004260 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004261 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr);
4262 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004263 case AttributeList::AT_VecTypeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004264 handleVecTypeHint(S, D, Attr);
4265 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004266
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004267 case AttributeList::AT_InitPriority:
4268 handleInitPriorityAttr(S, D, Attr);
4269 break;
4270
4271 case AttributeList::AT_Packed:
4272 handlePackedAttr(S, D, Attr);
4273 break;
4274 case AttributeList::AT_Section:
4275 handleSectionAttr(S, D, Attr);
4276 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004277 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004278 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004279 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004280 case AttributeList::AT_ArcWeakrefUnavailable:
4281 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr);
4282 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004283 case AttributeList::AT_ObjCRootClass:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004284 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr);
4285 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004286 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek438f8db2014-02-22 01:06:05 +00004287 handleObjCSuppresProtocolAttr(S, D, Attr);
Ted Kremenek28eace62013-11-23 01:01:34 +00004288 break;
4289 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004290 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr);
4291 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00004292 case AttributeList::AT_Unused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004293 handleSimpleAttribute<UnusedAttr>(S, D, Attr);
4294 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004295 case AttributeList::AT_ReturnsTwice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004296 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr);
4297 break;
4298 case AttributeList::AT_Used:
4299 handleUsedAttr(S, D, Attr);
4300 break;
John McCalld041a9b2013-02-20 01:54:26 +00004301 case AttributeList::AT_Visibility:
4302 handleVisibilityAttr(S, D, Attr, false);
4303 break;
4304 case AttributeList::AT_TypeVisibility:
4305 handleVisibilityAttr(S, D, Attr, true);
4306 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004307 case AttributeList::AT_WarnUnused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004308 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr);
4309 break;
4310 case AttributeList::AT_WarnUnusedResult:
4311 handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004312 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004313 case AttributeList::AT_Weak:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004314 handleSimpleAttribute<WeakAttr>(S, D, Attr);
4315 break;
4316 case AttributeList::AT_WeakRef:
4317 handleWeakRefAttr(S, D, Attr);
4318 break;
4319 case AttributeList::AT_WeakImport:
4320 handleWeakImportAttr(S, D, Attr);
4321 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004322 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004323 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004324 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004325 case AttributeList::AT_ObjCException:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004326 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr);
4327 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004328 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004329 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004330 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004331 case AttributeList::AT_ObjCNSObject:
4332 handleObjCNSObject(S, D, Attr);
4333 break;
4334 case AttributeList::AT_Blocks:
4335 handleBlocksAttr(S, D, Attr);
4336 break;
4337 case AttributeList::AT_Sentinel:
4338 handleSentinelAttr(S, D, Attr);
4339 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004340 case AttributeList::AT_Const:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004341 handleSimpleAttribute<ConstAttr>(S, D, Attr);
4342 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004343 case AttributeList::AT_Pure:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004344 handleSimpleAttribute<PureAttr>(S, D, Attr);
4345 break;
4346 case AttributeList::AT_Cleanup:
4347 handleCleanupAttr(S, D, Attr);
4348 break;
4349 case AttributeList::AT_NoDebug:
4350 handleNoDebugAttr(S, D, Attr);
4351 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00004352 case AttributeList::AT_NoDuplicate:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004353 handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
4354 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004355 case AttributeList::AT_NoInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004356 handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
4357 break;
4358 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
4359 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr);
4360 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004361 case AttributeList::AT_StdCall:
4362 case AttributeList::AT_CDecl:
4363 case AttributeList::AT_FastCall:
4364 case AttributeList::AT_ThisCall:
4365 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004366 case AttributeList::AT_MSABI:
4367 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004368 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004369 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004370 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004371 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004372 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004373 case AttributeList::AT_OpenCLKernel:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004374 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr);
4375 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004376 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004377 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr);
4378 break;
John McCall8d32c052012-05-22 21:28:12 +00004379
4380 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004381 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004382 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004383 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004384 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004385 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004386 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004387 case AttributeList::AT_MSInheritance:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004388 handleMSInheritanceAttr(S, D, Attr);
4389 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004390 case AttributeList::AT_SelectAny:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004391 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr);
4392 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004393
4394 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004395 case AttributeList::AT_AssertExclusiveLock:
4396 handleAssertExclusiveLockAttr(S, D, Attr);
4397 break;
4398 case AttributeList::AT_AssertSharedLock:
4399 handleAssertSharedLockAttr(S, D, Attr);
4400 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004401 case AttributeList::AT_GuardedVar:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004402 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr);
4403 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004404 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004405 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004406 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004407 case AttributeList::AT_ScopedLockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004408 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr);
4409 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004410 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004411 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004412 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004413 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004414 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004415 break;
4416 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004417 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004418 break;
4419 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004420 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004421 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004422 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004423 handleGuardedByAttr(S, D, Attr);
4424 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004425 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004426 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004427 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004428 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004429 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004430 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004431 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004432 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004433 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004434 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004435 handleLockReturnedAttr(S, D, Attr);
4436 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004437 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004438 handleLocksExcludedAttr(S, D, Attr);
4439 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004440 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004441 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004442 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004443 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004444 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004445 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004446 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004447 handleUnlockFunAttr(S, D, Attr);
4448 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004449 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004450 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004451 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004452 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004453 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004454 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004455
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004456 // Capability analysis attributes.
4457 case AttributeList::AT_Capability:
4458 case AttributeList::AT_Lockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004459 handleCapabilityAttr(S, D, Attr);
4460 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004461 case AttributeList::AT_RequiresCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004462 handleRequiresCapabilityAttr(S, D, Attr);
4463 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004464
4465 case AttributeList::AT_AssertCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004466 handleAssertCapabilityAttr(S, D, Attr);
4467 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004468 case AttributeList::AT_AcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004469 handleAcquireCapabilityAttr(S, D, Attr);
4470 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004471 case AttributeList::AT_ReleaseCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004472 handleReleaseCapabilityAttr(S, D, Attr);
4473 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004474 case AttributeList::AT_TryAcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004475 handleTryAcquireCapabilityAttr(S, D, Attr);
4476 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004477
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004478 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004479 case AttributeList::AT_Consumable:
4480 handleConsumableAttr(S, D, Attr);
4481 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004482 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004483 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr);
4484 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004485 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004486 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr);
4487 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004488 case AttributeList::AT_CallableWhen:
4489 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004490 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004491 case AttributeList::AT_ParamTypestate:
4492 handleParamTypestateAttr(S, D, Attr);
4493 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004494 case AttributeList::AT_ReturnTypestate:
4495 handleReturnTypestateAttr(S, D, Attr);
4496 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004497 case AttributeList::AT_SetTypestate:
4498 handleSetTypestateAttr(S, D, Attr);
4499 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004500 case AttributeList::AT_TestTypestate:
4501 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004502 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004503
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004504 // Type safety attributes.
4505 case AttributeList::AT_ArgumentWithTypeTag:
4506 handleArgumentWithTypeTagAttr(S, D, Attr);
4507 break;
4508 case AttributeList::AT_TypeTagForDatatype:
4509 handleTypeTagForDatatypeAttr(S, D, Attr);
4510 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004511 }
4512}
4513
4514/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4515/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004516void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004517 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004518 bool IncludeCXX11Attributes) {
4519 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004520 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004521
Joey Gouly2cd9db12013-12-13 16:15:28 +00004522 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004523 // GCC accepts
4524 // static int a9 __attribute__((weakref));
4525 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004526 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00004527 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
4528 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00004529 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004530 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004531 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004532
4533 if (!D->hasAttr<OpenCLKernelAttr>()) {
4534 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004535 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
4536 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004537 D->setInvalidDecl();
4538 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004539 if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
4540 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004541 D->setInvalidDecl();
4542 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004543 if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
4544 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004545 D->setInvalidDecl();
4546 }
4547 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004548}
4549
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004550// Annotation attributes are the only attributes allowed after an access
4551// specifier.
4552bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4553 const AttributeList *AttrList) {
4554 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004555 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004556 handleAnnotateAttr(*this, ASDecl, *l);
4557 } else {
4558 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4559 return true;
4560 }
4561 }
4562
4563 return false;
4564}
4565
John McCall42856de2011-10-01 05:17:03 +00004566/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4567/// contains any decl attributes that we should warn about.
4568static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4569 for ( ; A; A = A->getNext()) {
4570 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004571 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004572 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4573
4574 if (A->getKind() == AttributeList::UnknownAttribute) {
4575 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4576 << A->getName() << A->getRange();
4577 } else {
4578 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4579 << A->getName() << A->getRange();
4580 }
4581 }
4582}
4583
4584/// checkUnusedDeclAttributes - Given a declarator which is not being
4585/// used to build a declaration, complain about any decl attributes
4586/// which might be lying around on it.
4587void Sema::checkUnusedDeclAttributes(Declarator &D) {
4588 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4589 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4590 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4591 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4592}
4593
Ryan Flynn7d470f32009-07-30 03:15:39 +00004594/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004595/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004596NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4597 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004598 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004599 NamedDecl *NewD = 0;
4600 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004601 FunctionDecl *NewFD;
4602 // FIXME: Missing call to CheckFunctionDeclaration().
4603 // FIXME: Mangling?
4604 // FIXME: Is the qualifier info correct?
4605 // FIXME: Is the DeclContext correct?
4606 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4607 Loc, Loc, DeclarationName(II),
4608 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004609 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004610 FD->hasPrototype(),
4611 false/*isConstexprSpecified*/);
4612 NewD = NewFD;
4613
4614 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004615 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004616
4617 // Fake up parameter variables; they are declared as if this were
4618 // a typedef.
4619 QualType FDTy = FD->getType();
4620 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4621 SmallVector<ParmVarDecl*, 16> Params;
Alp Toker9cacbab2014-01-20 20:26:09 +00004622 for (FunctionProtoType::param_type_iterator AI = FT->param_type_begin(),
4623 AE = FT->param_type_end();
4624 AI != AE; ++AI) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004625 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4626 Param->setScopeInfo(0, Params.size());
4627 Params.push_back(Param);
4628 }
David Blaikie9c70e042011-09-21 18:16:56 +00004629 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004630 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004631 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4632 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004633 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004634 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004635 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004636 if (VD->getQualifier()) {
4637 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004638 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004639 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004640 }
4641 return NewD;
4642}
4643
James Dennett634962f2012-06-14 21:40:34 +00004644/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004645/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004646void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004647 if (W.getUsed()) return; // only do this once
4648 W.setUsed(true);
4649 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4650 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004651 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00004652 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
4653 W.getLocation()));
4654 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00004655 WeakTopLevelDecl.push_back(NewD);
4656 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4657 // to insert Decl at TU scope, sorry.
4658 DeclContext *SavedContext = CurContext;
4659 CurContext = Context.getTranslationUnitDecl();
4660 PushOnScopeChains(NewD, S);
4661 CurContext = SavedContext;
4662 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00004663 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004664 }
4665}
4666
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004667void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4668 // It's valid to "forward-declare" #pragma weak, in which case we
4669 // have to do this.
4670 LoadExternalWeakUndeclaredIdentifiers();
4671 if (!WeakUndeclaredIdentifiers.empty()) {
4672 NamedDecl *ND = NULL;
4673 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4674 if (VD->isExternC())
4675 ND = VD;
4676 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4677 if (FD->isExternC())
4678 ND = FD;
4679 if (ND) {
4680 if (IdentifierInfo *Id = ND->getIdentifier()) {
4681 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4682 = WeakUndeclaredIdentifiers.find(Id);
4683 if (I != WeakUndeclaredIdentifiers.end()) {
4684 WeakInfo W = I->second;
4685 DeclApplyPragmaWeak(S, ND, W);
4686 WeakUndeclaredIdentifiers[Id] = W;
4687 }
4688 }
4689 }
4690 }
4691}
4692
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004693/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4694/// it, apply them to D. This is a bit tricky because PD can have attributes
4695/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004696void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004697 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004698 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004699 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004700
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004701 // Walk the declarator structure, applying decl attributes that were in a type
4702 // position to the decl itself. This handles cases like:
4703 // int *__attr__(x)** D;
4704 // when X is a decl attribute.
4705 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4706 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004707 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004708
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004709 // Finally, apply any attributes on the decl itself.
4710 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004711 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004712}
John McCall28a6aea2009-11-04 02:18:39 +00004713
John McCall31168b02011-06-15 23:02:42 +00004714/// Is the given declaration allowed to use a forbidden type?
4715static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4716 // Private ivars are always okay. Unfortunately, people don't
4717 // always properly make their ivars private, even in system headers.
4718 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004719 // Function declarations in sys headers will be marked unavailable.
4720 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4721 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004722 return false;
4723
4724 // Require it to be declared in a system header.
4725 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4726}
4727
4728/// Handle a delayed forbidden-type diagnostic.
4729static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4730 Decl *decl) {
4731 if (decl && isForbiddenTypeAllowed(S, decl)) {
Aaron Ballman36a53502014-01-16 13:03:14 +00004732 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context,
4733 "this system declaration uses an unsupported type",
4734 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00004735 return;
4736 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004737 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004738 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004739 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004740 // kind of forbidden type messages on unavailable functions.
4741 if (FD->hasAttr<UnavailableAttr>() &&
4742 diag.getForbiddenTypeDiagnostic() ==
4743 diag::err_arc_array_param_no_ownership) {
4744 diag.Triggered = true;
4745 return;
4746 }
4747 }
John McCall31168b02011-06-15 23:02:42 +00004748
4749 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4750 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4751 diag.Triggered = true;
4752}
4753
John McCall2ec85372012-05-07 06:16:41 +00004754void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4755 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004756 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004757 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004758
John McCall2ec85372012-05-07 06:16:41 +00004759 // When delaying diagnostics to run in the context of a parsed
4760 // declaration, we only want to actually emit anything if parsing
4761 // succeeds.
4762 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004763
John McCall2ec85372012-05-07 06:16:41 +00004764 // We emit all the active diagnostics in this pool or any of its
4765 // parents. In general, we'll get one pool for the decl spec
4766 // and a child pool for each declarator; in a decl group like:
4767 // deprecated_typedef foo, *bar, baz();
4768 // only the declarator pops will be passed decls. This is correct;
4769 // we really do need to consider delayed diagnostics from the decl spec
4770 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004771 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004772 do {
John McCall6347b682012-05-07 06:16:58 +00004773 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004774 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4775 // This const_cast is a bit lame. Really, Triggered should be mutable.
4776 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004777 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004778 continue;
4779
John McCallc1465822011-02-14 07:13:47 +00004780 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004781 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004782 case DelayedDiagnostic::Unavailable:
4783 // Don't bother giving deprecation/unavailable diagnostics if
4784 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004785 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004786 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004787 break;
4788
4789 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004790 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004791 break;
John McCall31168b02011-06-15 23:02:42 +00004792
4793 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004794 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004795 break;
John McCall86121512010-01-27 03:50:35 +00004796 }
4797 }
John McCall2ec85372012-05-07 06:16:41 +00004798 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004799}
4800
John McCall6347b682012-05-07 06:16:58 +00004801/// Given a set of delayed diagnostics, re-emit them as if they had
4802/// been delayed in the current context instead of in the given pool.
4803/// Essentially, this just moves them to the current pool.
4804void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4805 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4806 assert(curPool && "re-emitting in undelayed context not supported");
4807 curPool->steal(pool);
4808}
4809
John McCall28a6aea2009-11-04 02:18:39 +00004810static bool isDeclDeprecated(Decl *D) {
4811 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004812 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004813 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004814 // A category implicitly has the availability of the interface.
4815 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4816 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004817 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4818 return false;
4819}
4820
Ted Kremenekb79ee572013-12-18 23:30:06 +00004821static bool isDeclUnavailable(Decl *D) {
4822 do {
4823 if (D->isUnavailable())
4824 return true;
4825 // A category implicitly has the availability of the interface.
4826 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4827 return CatD->getClassInterface()->isUnavailable();
4828 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4829 return false;
4830}
4831
Eli Friedman971bfa12012-08-08 21:52:41 +00004832static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004833DoEmitAvailabilityWarning(Sema &S,
4834 DelayedDiagnostic::DDKind K,
4835 Decl *Ctx,
4836 const NamedDecl *D,
4837 StringRef Message,
4838 SourceLocation Loc,
4839 const ObjCInterfaceDecl *UnknownObjCClass,
4840 const ObjCPropertyDecl *ObjCProperty) {
4841
4842 // Diagnostics for deprecated or unavailable.
4843 unsigned diag, diag_message, diag_fwdclass_message;
4844
4845 // Matches 'diag::note_property_attribute' options.
4846 unsigned property_note_select;
4847
4848 // Matches diag::note_availability_specified_here.
4849 unsigned available_here_select_kind;
4850
4851 // Don't warn if our current context is deprecated or unavailable.
4852 switch (K) {
4853 case DelayedDiagnostic::Deprecation:
4854 if (isDeclDeprecated(Ctx))
4855 return;
4856 diag = diag::warn_deprecated;
4857 diag_message = diag::warn_deprecated_message;
4858 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4859 property_note_select = /* deprecated */ 0;
4860 available_here_select_kind = /* deprecated */ 2;
4861 break;
4862
4863 case DelayedDiagnostic::Unavailable:
4864 if (isDeclUnavailable(Ctx))
4865 return;
4866 diag = diag::err_unavailable;
4867 diag_message = diag::err_unavailable_message;
4868 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4869 property_note_select = /* unavailable */ 1;
4870 available_here_select_kind = /* unavailable */ 0;
4871 break;
4872
4873 default:
4874 llvm_unreachable("Neither a deprecation or unavailable kind");
4875 }
4876
Eli Friedman971bfa12012-08-08 21:52:41 +00004877 DeclarationName Name = D->getDeclName();
4878 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004879 S.Diag(Loc, diag_message) << Name << Message;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004880 if (ObjCProperty)
4881 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4882 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004883 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004884 S.Diag(Loc, diag) << Name;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004885 if (ObjCProperty)
4886 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4887 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004888 } else {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004889 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004890 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4891 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004892
4893 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4894 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004895}
4896
Ted Kremenekb79ee572013-12-18 23:30:06 +00004897void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4898 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004899 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004900 DoEmitAvailabilityWarning(*this,
4901 (DelayedDiagnostic::DDKind) DD.Kind,
4902 Ctx,
4903 DD.getDeprecationDecl(),
4904 DD.getDeprecationMessage(),
4905 DD.Loc,
4906 DD.getUnknownObjCClass(),
4907 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004908}
4909
Ted Kremenekb79ee572013-12-18 23:30:06 +00004910void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4911 NamedDecl *D, StringRef Message,
4912 SourceLocation Loc,
4913 const ObjCInterfaceDecl *UnknownObjCClass,
4914 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004915 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004916 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004917 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4918 UnknownObjCClass,
4919 ObjCProperty,
4920 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004921 return;
4922 }
4923
Ted Kremenekb79ee572013-12-18 23:30:06 +00004924 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4925 DelayedDiagnostic::DDKind K;
4926 switch (AD) {
4927 case AD_Deprecation:
4928 K = DelayedDiagnostic::Deprecation;
4929 break;
4930 case AD_Unavailable:
4931 K = DelayedDiagnostic::Unavailable;
4932 break;
4933 }
4934
4935 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
4936 UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004937}