blob: 38158eb13346eb6d288acda23b64b721de5d6497 [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();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001107 for (const auto *I : UD->fields()) {
1108 QualType QT = I->getType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001109 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1110 T = QT;
1111 return;
1112 }
1113 }
1114 }
1115}
1116
Ted Kremenek9aedc152014-01-17 06:24:56 +00001117static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001118 SourceRange R, bool isReturnValue = false) {
Ted Kremenek9aedc152014-01-17 06:24:56 +00001119 T = T.getNonReferenceType();
1120 possibleTransparentUnionPointerType(T);
1121
1122 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001123 S.Diag(Attr.getLoc(),
1124 isReturnValue ? diag::warn_attribute_return_pointers_only
1125 : diag::warn_attribute_pointers_only)
Ted Kremenek9aedc152014-01-17 06:24:56 +00001126 << Attr.getName() << R;
1127 return false;
1128 }
1129 return true;
1130}
1131
Chandler Carruthedc2c642011-07-02 00:01:44 +00001132static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001133 SmallVector<unsigned, 8> NonNullArgs;
1134 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001135 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001136 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001137 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001138 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001139
1140 // Is the function argument a pointer type?
Ted Kremenek9aedc152014-01-17 06:24:56 +00001141 // FIXME: Should also highlight argument in decl in the diagnostic.
Alp Toker601b22c2014-01-21 23:35:24 +00001142 if (!attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr,
1143 Ex->getSourceRange()))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001144 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001145
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001146 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001147 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001148
1149 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1150 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001151 if (NonNullArgs.empty()) {
Alp Toker601b22c2014-01-21 23:35:24 +00001152 for (unsigned i = 0, e = getFunctionOrMethodNumParams(D); i != e; ++i) {
1153 QualType T = getFunctionOrMethodParamType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001154 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001155 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001156 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001157 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001158
Ted Kremenek22813f42010-10-21 18:49:36 +00001159 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001160 if (NonNullArgs.empty()) {
1161 // Warn the trivial case only if attribute is not coming from a
1162 // macro instantiation.
1163 if (Attr.getLoc().isFileID())
1164 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001165 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001166 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001167 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001168
Nick Lewyckye1121512013-01-24 01:12:16 +00001169 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001170 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001171 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001172 D->addAttr(::new (S.Context)
1173 NonNullAttr(Attr.getRange(), S.Context, start, size,
1174 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001175}
1176
Jordan Rosec9399072014-02-11 17:27:59 +00001177static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1178 const AttributeList &Attr) {
1179 if (Attr.getNumArgs() > 0) {
1180 if (D->getFunctionType()) {
1181 handleNonNullAttr(S, D, Attr);
1182 } else {
1183 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1184 << D->getSourceRange();
1185 }
1186 return;
1187 }
1188
1189 // Is the argument a pointer type?
1190 if (!attrNonNullArgCheck(S, D->getType(), Attr, D->getSourceRange()))
1191 return;
1192
1193 D->addAttr(::new (S.Context)
1194 NonNullAttr(Attr.getRange(), S.Context, 0, 0,
1195 Attr.getAttributeSpellingListIndex()));
1196}
1197
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001198static void handleReturnsNonNullAttr(Sema &S, Decl *D,
1199 const AttributeList &Attr) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001200 QualType ResultType = getFunctionOrMethodResultType(D);
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001201 if (!attrNonNullArgCheck(S, ResultType, Attr, Attr.getRange(),
1202 /* isReturnValue */ true))
1203 return;
1204
1205 D->addAttr(::new (S.Context)
1206 ReturnsNonNullAttr(Attr.getRange(), S.Context,
1207 Attr.getAttributeSpellingListIndex()));
1208}
1209
Chandler Carruthedc2c642011-07-02 00:01:44 +00001210static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001211 // This attribute must be applied to a function declaration. The first
1212 // argument to the attribute must be an identifier, the name of the resource,
1213 // for example: malloc. The following arguments must be argument indexes, the
1214 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001215 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001216 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001217 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001218
Aaron Ballman00e99962013-08-31 01:11:41 +00001219 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001220 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001221 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001222 return;
1223 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001224
Richard Smith852e9ce2013-11-27 01:46:48 +00001225 // Figure out our Kind.
1226 OwnershipAttr::OwnershipKind K =
1227 OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0,
1228 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001229
Richard Smith852e9ce2013-11-27 01:46:48 +00001230 // Check arguments.
1231 switch (K) {
1232 case OwnershipAttr::Takes:
1233 case OwnershipAttr::Holds:
1234 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001235 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1236 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001237 return;
1238 }
1239 break;
1240 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001241 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001242 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1243 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001244 return;
1245 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001246 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001247 }
1248
Richard Smith852e9ce2013-11-27 01:46:48 +00001249 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001250
1251 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001252 StringRef ModuleName = Module->getName();
1253 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1254 ModuleName.size() > 4) {
1255 ModuleName = ModuleName.drop_front(2).drop_back(2);
1256 Module = &S.PP.getIdentifierTable().get(ModuleName);
1257 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001258
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001259 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001260 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1261 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001262 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001263 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001264 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001265
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001266 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001267 QualType T = getFunctionOrMethodParamType(D, Idx);
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001268 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001269 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001270 case OwnershipAttr::Takes:
1271 case OwnershipAttr::Holds:
1272 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1273 Err = 0;
1274 break;
1275 case OwnershipAttr::Returns:
1276 if (!T->isIntegerType())
1277 Err = 1;
1278 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001279 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001280 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001281 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001282 << Ex->getSourceRange();
1283 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001284 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001285
1286 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001287 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001288 // FIXME: A returns attribute should conflict with any returns attribute
1289 // with a different index too.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001290 if (I->getOwnKind() != K && I->args_end() !=
1291 std::find(I->args_begin(), I->args_end(), Idx)) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001292 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001293 << AL.getName() << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001294 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001295 }
1296 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001297 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001298 }
1299
1300 unsigned* start = OwnershipArgs.data();
1301 unsigned size = OwnershipArgs.size();
1302 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001303
Michael Han99315932013-01-24 16:46:58 +00001304 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001305 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001306 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001307}
1308
Chandler Carruthedc2c642011-07-02 00:01:44 +00001309static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001310 // Check the attribute arguments.
1311 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001312 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1313 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001314 return;
1315 }
1316
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001317 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001318
Rafael Espindolac18086a2010-02-23 22:00:30 +00001319 // gcc rejects
1320 // class c {
1321 // static int a __attribute__((weakref ("v2")));
1322 // static int b() __attribute__((weakref ("f3")));
1323 // };
1324 // and ignores the attributes of
1325 // void f(void) {
1326 // static int a __attribute__((weakref ("v2")));
1327 // }
1328 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001329 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001330 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001331 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1332 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001333 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001334 }
1335
1336 // The GCC manual says
1337 //
1338 // At present, a declaration to which `weakref' is attached can only
1339 // be `static'.
1340 //
1341 // It also says
1342 //
1343 // Without a TARGET,
1344 // given as an argument to `weakref' or to `alias', `weakref' is
1345 // equivalent to `weak'.
1346 //
1347 // gcc 4.4.1 will accept
1348 // int a7 __attribute__((weakref));
1349 // as
1350 // int a7 __attribute__((weak));
1351 // This looks like a bug in gcc. We reject that for now. We should revisit
1352 // it if this behaviour is actually used.
1353
Rafael Espindolac18086a2010-02-23 22:00:30 +00001354 // GCC rejects
1355 // static ((alias ("y"), weakref)).
1356 // Should we? How to check that weakref is before or after alias?
1357
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001358 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1359 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1360 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001361 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001362 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001363 // GCC will accept anything as the argument of weakref. Should we
1364 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001365 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1366 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001367
Michael Han99315932013-01-24 16:46:58 +00001368 D->addAttr(::new (S.Context)
1369 WeakRefAttr(Attr.getRange(), S.Context,
1370 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001371}
1372
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001373static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1374 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001375 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001376 return;
1377
Douglas Gregore8bbc122011-09-02 00:18:52 +00001378 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001379 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1380 return;
1381 }
1382
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001383 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001384
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001385 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001386 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001387}
1388
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001389static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001390 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001391 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001392
Michael Han99315932013-01-24 16:46:58 +00001393 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1394 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001395}
1396
1397static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001398 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001399 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001400
Michael Han99315932013-01-24 16:46:58 +00001401 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1402 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001403}
1404
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001405static void handleTLSModelAttr(Sema &S, Decl *D,
1406 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001407 StringRef Model;
1408 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001409 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001410 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001411 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001412
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001413 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001414 if (Model != "global-dynamic" && Model != "local-dynamic"
1415 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001416 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001417 return;
1418 }
1419
Michael Han99315932013-01-24 16:46:58 +00001420 D->addAttr(::new (S.Context)
1421 TLSModelAttr(Attr.getRange(), S.Context, Model,
1422 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001423}
1424
Chandler Carruthedc2c642011-07-02 00:01:44 +00001425static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001426 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Alp Toker314cc812014-01-25 16:55:45 +00001427 QualType RetTy = FD->getReturnType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001428 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001429 D->addAttr(::new (S.Context)
1430 MallocAttr(Attr.getRange(), S.Context,
1431 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001432 return;
1433 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001434 }
1435
Ted Kremenek08479ae2009-08-15 00:51:46 +00001436 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001437}
1438
Chandler Carruthedc2c642011-07-02 00:01:44 +00001439static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001440 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001441 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1442 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001443 return;
1444 }
1445
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001446 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1447 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001448}
1449
Chandler Carruthedc2c642011-07-02 00:01:44 +00001450static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001451 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001452
1453 if (S.CheckNoReturnAttr(attr)) return;
1454
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001455 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001456 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001457 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001458 return;
1459 }
1460
Michael Han99315932013-01-24 16:46:58 +00001461 D->addAttr(::new (S.Context)
1462 NoReturnAttr(attr.getRange(), S.Context,
1463 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001464}
1465
1466bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001467 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001468 attr.setInvalid();
1469 return true;
1470 }
1471
1472 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001473}
1474
Chandler Carruthedc2c642011-07-02 00:01:44 +00001475static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1476 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001477
1478 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1479 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001480 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1481 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001482 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1483 && !VD->getType()->isFunctionPointerType())) {
1484 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001485 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001486 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001487 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001488 return;
1489 }
1490 }
1491
Michael Han99315932013-01-24 16:46:58 +00001492 D->addAttr(::new (S.Context)
1493 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1494 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001495}
1496
John Thompsoncdb847ba2010-08-09 21:53:52 +00001497// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001498static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001499/*
1500 Returning a Vector Class in Registers
1501
Eric Christopherbc638a82010-12-01 22:13:54 +00001502 According to the PPU ABI specifications, a class with a single member of
1503 vector type is returned in memory when used as the return value of a function.
1504 This results in inefficient code when implementing vector classes. To return
1505 the value in a single vector register, add the vecreturn attribute to the
1506 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001507
1508 Example:
1509
1510 struct Vector
1511 {
1512 __vector float xyzw;
1513 } __attribute__((vecreturn));
1514
1515 Vector Add(Vector lhs, Vector rhs)
1516 {
1517 Vector result;
1518 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1519 return result; // This will be returned in a register
1520 }
1521*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001522 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1523 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001524 return;
1525 }
1526
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001527 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001528 int count = 0;
1529
1530 if (!isa<CXXRecordDecl>(record)) {
1531 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1532 return;
1533 }
1534
1535 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1536 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1537 return;
1538 }
1539
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001540 for (const auto *I : record->fields()) {
1541 if ((count == 1) || !I->getType()->isVectorType()) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001542 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1543 return;
1544 }
1545 count++;
1546 }
1547
Michael Han99315932013-01-24 16:46:58 +00001548 D->addAttr(::new (S.Context)
1549 VecReturnAttr(Attr.getRange(), S.Context,
1550 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001551}
1552
Richard Smithe233fbf2013-01-28 22:42:45 +00001553static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1554 const AttributeList &Attr) {
1555 if (isa<ParmVarDecl>(D)) {
1556 // [[carries_dependency]] can only be applied to a parameter if it is a
1557 // parameter of a function declaration or lambda.
1558 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1559 S.Diag(Attr.getLoc(),
1560 diag::err_carries_dependency_param_not_function_decl);
1561 return;
1562 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001563 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001564
1565 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1566 Attr.getRange(), S.Context,
1567 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001568}
1569
Chandler Carruthedc2c642011-07-02 00:01:44 +00001570static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001571 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001572 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001573 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001574 return;
1575 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001576 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001577 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001578 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001579 return;
1580 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001581
Michael Han99315932013-01-24 16:46:58 +00001582 D->addAttr(::new (S.Context)
1583 UsedAttr(Attr.getRange(), S.Context,
1584 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001585}
1586
Chandler Carruthedc2c642011-07-02 00:01:44 +00001587static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001588 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001589 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001590 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1591 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001592 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001593 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001594
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001595 uint32_t priority = ConstructorAttr::DefaultPriority;
1596 if (Attr.getNumArgs() > 0 &&
1597 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1598 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001599
Michael Han99315932013-01-24 16:46:58 +00001600 D->addAttr(::new (S.Context)
1601 ConstructorAttr(Attr.getRange(), S.Context, priority,
1602 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001603}
1604
Chandler Carruthedc2c642011-07-02 00:01:44 +00001605static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001606 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001607 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001608 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1609 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001610 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001611 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001612
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001613 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001614 if (Attr.getNumArgs() > 0 &&
1615 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1616 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001617
Michael Han99315932013-01-24 16:46:58 +00001618 D->addAttr(::new (S.Context)
1619 DestructorAttr(Attr.getRange(), S.Context, priority,
1620 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001621}
1622
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001623template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001624static void handleAttrWithMessage(Sema &S, Decl *D,
1625 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001626 unsigned NumArgs = Attr.getNumArgs();
1627 if (NumArgs > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001628 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1629 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001630 return;
1631 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001632
1633 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001634 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001635 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001636 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001637
Michael Han99315932013-01-24 16:46:58 +00001638 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1639 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001640}
1641
Ted Kremenek438f8db2014-02-22 01:06:05 +00001642static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Ted Kremenek28eace62013-11-23 01:01:34 +00001643 const AttributeList &Attr) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00001644 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Ted Kremenek27cfe102014-02-21 22:49:04 +00001645 S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition)
1646 << Attr.getName() << Attr.getRange();
1647 return;
1648 }
1649
Ted Kremenek28eace62013-11-23 01:01:34 +00001650 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001651 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1652 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001653}
1654
Jordy Rose740b0c22012-05-08 03:27:22 +00001655static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1656 IdentifierInfo *Platform,
1657 VersionTuple Introduced,
1658 VersionTuple Deprecated,
1659 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001660 StringRef PlatformName
1661 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1662 if (PlatformName.empty())
1663 PlatformName = Platform->getName();
1664
1665 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1666 // of these steps are needed).
1667 if (!Introduced.empty() && !Deprecated.empty() &&
1668 !(Introduced <= Deprecated)) {
1669 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1670 << 1 << PlatformName << Deprecated.getAsString()
1671 << 0 << Introduced.getAsString();
1672 return true;
1673 }
1674
1675 if (!Introduced.empty() && !Obsoleted.empty() &&
1676 !(Introduced <= Obsoleted)) {
1677 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1678 << 2 << PlatformName << Obsoleted.getAsString()
1679 << 0 << Introduced.getAsString();
1680 return true;
1681 }
1682
1683 if (!Deprecated.empty() && !Obsoleted.empty() &&
1684 !(Deprecated <= Obsoleted)) {
1685 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1686 << 2 << PlatformName << Obsoleted.getAsString()
1687 << 1 << Deprecated.getAsString();
1688 return true;
1689 }
1690
1691 return false;
1692}
1693
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001694/// \brief Check whether the two versions match.
1695///
1696/// If either version tuple is empty, then they are assumed to match. If
1697/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1698static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1699 bool BeforeIsOkay) {
1700 if (X.empty() || Y.empty())
1701 return true;
1702
1703 if (X == Y)
1704 return true;
1705
1706 if (BeforeIsOkay && X < Y)
1707 return true;
1708
1709 return false;
1710}
1711
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001712AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001713 IdentifierInfo *Platform,
1714 VersionTuple Introduced,
1715 VersionTuple Deprecated,
1716 VersionTuple Obsoleted,
1717 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001718 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001719 bool Override,
1720 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001721 VersionTuple MergedIntroduced = Introduced;
1722 VersionTuple MergedDeprecated = Deprecated;
1723 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001724 bool FoundAny = false;
1725
Rafael Espindolac67f2232012-05-10 02:50:16 +00001726 if (D->hasAttrs()) {
1727 AttrVec &Attrs = D->getAttrs();
1728 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1729 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1730 if (!OldAA) {
1731 ++i;
1732 continue;
1733 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001734
Rafael Espindolac67f2232012-05-10 02:50:16 +00001735 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1736 if (OldPlatform != Platform) {
1737 ++i;
1738 continue;
1739 }
1740
1741 FoundAny = true;
1742 VersionTuple OldIntroduced = OldAA->getIntroduced();
1743 VersionTuple OldDeprecated = OldAA->getDeprecated();
1744 VersionTuple OldObsoleted = OldAA->getObsoleted();
1745 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001746
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001747 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1748 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1749 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1750 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001751 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001752 if (Override) {
1753 int Which = -1;
1754 VersionTuple FirstVersion;
1755 VersionTuple SecondVersion;
1756 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1757 Which = 0;
1758 FirstVersion = OldIntroduced;
1759 SecondVersion = Introduced;
1760 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1761 Which = 1;
1762 FirstVersion = Deprecated;
1763 SecondVersion = OldDeprecated;
1764 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1765 Which = 2;
1766 FirstVersion = Obsoleted;
1767 SecondVersion = OldObsoleted;
1768 }
1769
1770 if (Which == -1) {
1771 Diag(OldAA->getLocation(),
1772 diag::warn_mismatched_availability_override_unavail)
1773 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1774 } else {
1775 Diag(OldAA->getLocation(),
1776 diag::warn_mismatched_availability_override)
1777 << Which
1778 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1779 << FirstVersion.getAsString() << SecondVersion.getAsString();
1780 }
1781 Diag(Range.getBegin(), diag::note_overridden_method);
1782 } else {
1783 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1784 Diag(Range.getBegin(), diag::note_previous_attribute);
1785 }
1786
Rafael Espindolac67f2232012-05-10 02:50:16 +00001787 Attrs.erase(Attrs.begin() + i);
1788 --e;
1789 continue;
1790 }
1791
1792 VersionTuple MergedIntroduced2 = MergedIntroduced;
1793 VersionTuple MergedDeprecated2 = MergedDeprecated;
1794 VersionTuple MergedObsoleted2 = MergedObsoleted;
1795
1796 if (MergedIntroduced2.empty())
1797 MergedIntroduced2 = OldIntroduced;
1798 if (MergedDeprecated2.empty())
1799 MergedDeprecated2 = OldDeprecated;
1800 if (MergedObsoleted2.empty())
1801 MergedObsoleted2 = OldObsoleted;
1802
1803 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1804 MergedIntroduced2, MergedDeprecated2,
1805 MergedObsoleted2)) {
1806 Attrs.erase(Attrs.begin() + i);
1807 --e;
1808 continue;
1809 }
1810
1811 MergedIntroduced = MergedIntroduced2;
1812 MergedDeprecated = MergedDeprecated2;
1813 MergedObsoleted = MergedObsoleted2;
1814 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001815 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001816 }
1817
1818 if (FoundAny &&
1819 MergedIntroduced == Introduced &&
1820 MergedDeprecated == Deprecated &&
1821 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001822 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001823
Ted Kremenekb5445722013-04-06 00:34:27 +00001824 // Only create a new attribute if !Override, but we want to do
1825 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001826 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001827 MergedDeprecated, MergedObsoleted) &&
1828 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001829 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1830 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001831 Obsoleted, IsUnavailable, Message,
1832 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001833 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001834 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001835}
1836
Chandler Carruthedc2c642011-07-02 00:01:44 +00001837static void handleAvailabilityAttr(Sema &S, Decl *D,
1838 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001839 if (!checkAttributeNumArgs(S, Attr, 1))
1840 return;
1841 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001842 unsigned Index = Attr.getAttributeSpellingListIndex();
1843
Aaron Ballman00e99962013-08-31 01:11:41 +00001844 IdentifierInfo *II = Platform->Ident;
1845 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1846 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1847 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001848
Rafael Espindolac231fab2013-01-08 21:30:32 +00001849 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1850 if (!ND) {
1851 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1852 return;
1853 }
1854
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001855 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1856 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1857 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001858 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001859 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001860 if (const StringLiteral *SE =
1861 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001862 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001863
Aaron Ballman00e99962013-08-31 01:11:41 +00001864 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001865 Introduced.Version,
1866 Deprecated.Version,
1867 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001868 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001869 /*Override=*/false,
1870 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001871 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001872 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001873}
1874
John McCalld041a9b2013-02-20 01:54:26 +00001875template <class T>
1876static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1877 typename T::VisibilityType value,
1878 unsigned attrSpellingListIndex) {
1879 T *existingAttr = D->getAttr<T>();
1880 if (existingAttr) {
1881 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1882 if (existingValue == value)
1883 return NULL;
1884 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1885 S.Diag(range.getBegin(), diag::note_previous_attribute);
1886 D->dropAttr<T>();
1887 }
1888 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1889}
1890
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001891VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001892 VisibilityAttr::VisibilityType Vis,
1893 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001894 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1895 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001896}
1897
John McCalld041a9b2013-02-20 01:54:26 +00001898TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1899 TypeVisibilityAttr::VisibilityType Vis,
1900 unsigned AttrSpellingListIndex) {
1901 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1902 AttrSpellingListIndex);
1903}
1904
1905static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1906 bool isTypeVisibility) {
1907 // Visibility attributes don't mean anything on a typedef.
1908 if (isa<TypedefNameDecl>(D)) {
1909 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
1910 << Attr.getName();
1911 return;
1912 }
1913
1914 // 'type_visibility' can only go on a type or namespace.
1915 if (isTypeVisibility &&
1916 !(isa<TagDecl>(D) ||
1917 isa<ObjCInterfaceDecl>(D) ||
1918 isa<NamespaceDecl>(D))) {
1919 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
1920 << Attr.getName() << ExpectedTypeOrNamespace;
1921 return;
1922 }
1923
Benjamin Kramer70370212013-09-09 15:08:57 +00001924 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001925 StringRef TypeStr;
1926 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001927 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001928 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001929
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001930 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00001931 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001932 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00001933 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001934 return;
1935 }
Aaron Ballman682ee422013-09-11 19:47:58 +00001936
1937 // Complain about attempts to use protected visibility on targets
1938 // (like Darwin) that don't support it.
1939 if (type == VisibilityAttr::Protected &&
1940 !S.Context.getTargetInfo().hasProtectedVisibility()) {
1941 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1942 type = VisibilityAttr::Default;
1943 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001944
Michael Han99315932013-01-24 16:46:58 +00001945 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00001946 clang::Attr *newAttr;
1947 if (isTypeVisibility) {
1948 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
1949 (TypeVisibilityAttr::VisibilityType) type,
1950 Index);
1951 } else {
1952 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
1953 }
1954 if (newAttr)
1955 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001956}
1957
Chandler Carruthedc2c642011-07-02 00:01:44 +00001958static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1959 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001960 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00001961 if (!Attr.isArgIdent(0)) {
1962 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1963 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00001964 return;
1965 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001966
Aaron Ballman682ee422013-09-11 19:47:58 +00001967 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1968 ObjCMethodFamilyAttr::FamilyKind F;
1969 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
1970 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
1971 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00001972 return;
1973 }
1974
Alp Toker314cc812014-01-25 16:55:45 +00001975 if (F == ObjCMethodFamilyAttr::OMF_init &&
1976 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00001977 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00001978 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00001979 // Ignore the attribute.
1980 return;
1981 }
1982
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001983 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00001984 S.Context, F,
1985 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00001986}
1987
Chandler Carruthedc2c642011-07-02 00:01:44 +00001988static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00001989 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001990 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00001991 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001992 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1993 return;
1994 }
1995 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00001996 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1997 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00001998 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00001999 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2000 return;
2001 }
2002 }
2003 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002004 // It is okay to include this attribute on properties, e.g.:
2005 //
2006 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2007 //
2008 // In this case it follows tradition and suppresses an error in the above
2009 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002010 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002011 }
Michael Han99315932013-01-24 16:46:58 +00002012 D->addAttr(::new (S.Context)
2013 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2014 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002015}
2016
Chandler Carruthedc2c642011-07-02 00:01:44 +00002017static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002018 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002019 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002020 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002021 return;
2022 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002023
Aaron Ballman00e99962013-08-31 01:11:41 +00002024 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002025 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002026 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2027 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2028 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002029 return;
2030 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002031
Michael Han99315932013-01-24 16:46:58 +00002032 D->addAttr(::new (S.Context)
2033 BlocksAttr(Attr.getRange(), S.Context, type,
2034 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002035}
2036
Chandler Carruthedc2c642011-07-02 00:01:44 +00002037static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002038 // check the attribute arguments.
2039 if (Attr.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00002040 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
2041 << Attr.getName() << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002042 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002043 }
2044
Aaron Ballman18a78382013-11-21 00:28:23 +00002045 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002046 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002047 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002048 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002049 if (E->isTypeDependent() || E->isValueDependent() ||
2050 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002051 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002052 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002053 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002054 return;
2055 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002056
John McCallb46f2872011-09-09 07:56:05 +00002057 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002058 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2059 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002060 return;
2061 }
John McCallb46f2872011-09-09 07:56:05 +00002062
2063 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002064 }
2065
Aaron Ballman18a78382013-11-21 00:28:23 +00002066 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002067 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002068 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002069 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002070 if (E->isTypeDependent() || E->isValueDependent() ||
2071 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002072 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002073 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002074 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002075 return;
2076 }
2077 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002078
John McCallb46f2872011-09-09 07:56:05 +00002079 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002080 // FIXME: This error message could be improved, it would be nice
2081 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002082 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2083 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002084 return;
2085 }
2086 }
2087
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002088 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002089 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002090 if (isa<FunctionNoProtoType>(FT)) {
2091 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2092 return;
2093 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002094
Chris Lattner9363e312009-03-17 23:03:47 +00002095 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002096 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002097 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002098 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002099 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002100 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002101 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002102 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002103 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002104 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2105 if (!BD->isVariadic()) {
2106 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2107 return;
2108 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002109 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002110 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002111 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002112 const FunctionType *FT = Ty->isFunctionPointerType()
2113 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002114 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002115 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002116 int m = Ty->isFunctionPointerType() ? 0 : 1;
2117 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002118 return;
2119 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002120 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002121 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002122 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002123 return;
2124 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002125 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002126 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002127 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002128 return;
2129 }
Michael Han99315932013-01-24 16:46:58 +00002130 D->addAttr(::new (S.Context)
2131 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2132 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002133}
2134
Chandler Carruthedc2c642011-07-02 00:01:44 +00002135static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002136 if (D->getFunctionType() &&
2137 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002138 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2139 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002140 return;
2141 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002142 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002143 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002144 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2145 << Attr.getName() << 1;
2146 return;
2147 }
2148
Michael Han99315932013-01-24 16:46:58 +00002149 D->addAttr(::new (S.Context)
2150 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2151 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002152}
2153
Chandler Carruthedc2c642011-07-02 00:01:44 +00002154static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002155 // weak_import only applies to variable & function declarations.
2156 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002157 if (!D->canBeWeakImported(isDef)) {
2158 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002159 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2160 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002161 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002162 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002163 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002164 // Nothing to warn about here.
2165 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002166 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002167 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002168
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002169 return;
2170 }
2171
Michael Han99315932013-01-24 16:46:58 +00002172 D->addAttr(::new (S.Context)
2173 WeakImportAttr(Attr.getRange(), S.Context,
2174 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002175}
2176
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002177// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002178template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002179static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002180 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002181 uint32_t WGSize[3];
2182 for (unsigned i = 0; i < 3; ++i)
2183 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002184 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002185
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002186 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2187 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2188 Existing->getYDim() == WGSize[1] &&
2189 Existing->getZDim() == WGSize[2]))
2190 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002191
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002192 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2193 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002194 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002195}
2196
Joey Goulyaba589c2013-03-08 09:42:32 +00002197static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002198 if (!Attr.hasParsedType()) {
2199 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2200 << Attr.getName() << 1;
2201 return;
2202 }
2203
Richard Smithb87c4652013-10-31 21:23:20 +00002204 TypeSourceInfo *ParmTSI = 0;
2205 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2206 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002207
2208 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2209 (ParmType->isBooleanType() ||
2210 !ParmType->isIntegralType(S.getASTContext()))) {
2211 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2212 << ParmType;
2213 return;
2214 }
2215
Aaron Ballmana9e05402013-12-02 22:16:55 +00002216 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002217 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002218 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2219 return;
2220 }
2221 }
2222
2223 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002224 ParmTSI,
2225 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002226}
2227
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002228SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002229 StringRef Name,
2230 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002231 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2232 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002233 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002234 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2235 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002236 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002237 }
Michael Han99315932013-01-24 16:46:58 +00002238 return ::new (Context) SectionAttr(Range, Context, Name,
2239 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002240}
2241
Chandler Carruthedc2c642011-07-02 00:01:44 +00002242static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002243 // Make sure that there is a string literal as the sections's single
2244 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002245 StringRef Str;
2246 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002247 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002248 return;
Mike Stump11289f42009-09-09 15:08:12 +00002249
Chris Lattner30ba6742009-08-10 19:03:04 +00002250 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002251 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002252 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002253 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002254 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002255 return;
2256 }
Mike Stump11289f42009-09-09 15:08:12 +00002257
Michael Han99315932013-01-24 16:46:58 +00002258 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002259 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002260 if (NewAttr)
2261 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002262}
2263
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002264
Chandler Carruthedc2c642011-07-02 00:01:44 +00002265static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002266 VarDecl *VD = cast<VarDecl>(D);
2267 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002268 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002269 return;
2270 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002271
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002272 Expr *E = Attr.getArgAsExpr(0);
2273 SourceLocation Loc = E->getExprLoc();
2274 FunctionDecl *FD = 0;
2275 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002276
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002277 // gcc only allows for simple identifiers. Since we support more than gcc, we
2278 // will warn the user.
2279 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2280 if (DRE->hasQualifier())
2281 S.Diag(Loc, diag::warn_cleanup_ext);
2282 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2283 NI = DRE->getNameInfo();
2284 if (!FD) {
2285 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2286 << NI.getName();
2287 return;
2288 }
2289 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2290 if (ULE->hasExplicitTemplateArgs())
2291 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002292 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2293 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002294 if (!FD) {
2295 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2296 << NI.getName();
2297 if (ULE->getType() == S.Context.OverloadTy)
2298 S.NoteAllOverloadCandidates(ULE);
2299 return;
2300 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002301 } else {
2302 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002303 return;
2304 }
2305
Anders Carlssond277d792009-01-31 01:16:18 +00002306 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002307 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2308 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002309 return;
2310 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002311
Anders Carlsson723f55d2009-02-07 23:16:50 +00002312 // We're currently more strict than GCC about what function types we accept.
2313 // If this ever proves to be a problem it should be easy to fix.
2314 QualType Ty = S.Context.getPointerType(VD->getType());
2315 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002316 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2317 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002318 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2319 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002320 return;
2321 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002322
Michael Han99315932013-01-24 16:46:58 +00002323 D->addAttr(::new (S.Context)
2324 CleanupAttr(Attr.getRange(), S.Context, FD,
2325 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002326}
2327
Mike Stumpd3bb5572009-07-24 19:02:52 +00002328/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002329/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002330static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002331 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002332 uint64_t Idx;
2333 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002334 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002335
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002336 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002337 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002338
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002339 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2340 if (not_nsstring_type &&
2341 !isCFStringType(Ty, S.Context) &&
2342 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002343 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002344 // FIXME: Should highlight the actual expression that has the wrong type.
2345 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002346 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002347 << IdxExpr->getSourceRange();
2348 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002349 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002350 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002351 if (!isNSStringType(Ty, S.Context) &&
2352 !isCFStringType(Ty, S.Context) &&
2353 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002354 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002355 // FIXME: Should highlight the actual expression that has the wrong type.
2356 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002357 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002358 << IdxExpr->getSourceRange();
2359 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002360 }
2361
Alp Toker601b22c2014-01-21 23:35:24 +00002362 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002363 // because that has corrected for the implicit this parameter, and is zero-
2364 // based. The attribute expects what the user wrote explicitly.
2365 llvm::APSInt Val;
2366 IdxExpr->EvaluateAsInt(Val, S.Context);
2367
Michael Han99315932013-01-24 16:46:58 +00002368 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002369 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002370 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002371}
2372
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002373enum FormatAttrKind {
2374 CFStringFormat,
2375 NSStringFormat,
2376 StrftimeFormat,
2377 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002378 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002379 InvalidFormat
2380};
2381
2382/// getFormatAttrKind - Map from format attribute names to supported format
2383/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002384static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002385 return llvm::StringSwitch<FormatAttrKind>(Format)
2386 // Check for formats that get handled specially.
2387 .Case("NSString", NSStringFormat)
2388 .Case("CFString", CFStringFormat)
2389 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002390
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002391 // Otherwise, check for supported formats.
2392 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2393 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2394 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002395
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002396 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2397 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002398}
2399
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002400/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002401/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002402static void handleInitPriorityAttr(Sema &S, Decl *D,
2403 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002404 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002405 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2406 return;
2407 }
2408
Aaron Ballman4a611152013-11-27 16:34:09 +00002409 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002410 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2411 Attr.setInvalid();
2412 return;
2413 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002414 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002415 if (S.Context.getAsArrayType(T))
2416 T = S.Context.getBaseElementType(T);
2417 if (!T->getAs<RecordType>()) {
2418 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2419 Attr.setInvalid();
2420 return;
2421 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002422
2423 Expr *E = Attr.getArgAsExpr(0);
2424 uint32_t prioritynum;
2425 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002426 Attr.setInvalid();
2427 return;
2428 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002429
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002430 if (prioritynum < 101 || prioritynum > 65535) {
2431 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002432 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002433 Attr.setInvalid();
2434 return;
2435 }
Michael Han99315932013-01-24 16:46:58 +00002436 D->addAttr(::new (S.Context)
2437 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2438 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002439}
2440
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002441FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2442 IdentifierInfo *Format, int FormatIdx,
2443 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002444 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002445 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002446 for (auto *F : D->specific_attrs<FormatAttr>()) {
2447 if (F->getType() == Format &&
2448 F->getFormatIdx() == FormatIdx &&
2449 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002450 // If we don't have a valid location for this attribute, adopt the
2451 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002452 if (F->getLocation().isInvalid())
2453 F->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002454 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002455 }
2456 }
2457
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002458 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2459 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002460}
2461
Mike Stumpd3bb5572009-07-24 19:02:52 +00002462/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002463/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002464static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002465 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002466 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002467 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002468 return;
2469 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002470
Chandler Carruth743682b2010-11-16 08:35:43 +00002471 // In C++ the implicit 'this' function parameter also counts, and they are
2472 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002473 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002474 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002475
Aaron Ballman00e99962013-08-31 01:11:41 +00002476 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2477 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002478
2479 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002480 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002481 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002482 // If we've modified the string name, we need a new identifier for it.
2483 II = &S.Context.Idents.get(Format);
2484 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002485
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002486 // Check for supported formats.
2487 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002488
2489 if (Kind == IgnoredFormat)
2490 return;
2491
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002492 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002493 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002494 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002495 return;
2496 }
2497
2498 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002499 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002500 uint32_t Idx;
2501 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002502 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002503
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002504 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002505 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002506 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002507 return;
2508 }
2509
2510 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002511 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002512
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002513 if (HasImplicitThisParam) {
2514 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002515 S.Diag(Attr.getLoc(),
2516 diag::err_format_attribute_implicit_this_format_string)
2517 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002518 return;
2519 }
2520 ArgIdx--;
2521 }
Mike Stump11289f42009-09-09 15:08:12 +00002522
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002523 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002524 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002525
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002526 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002527 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002528 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2529 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002530 return;
2531 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002532 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002533 // FIXME: do we need to check if the type is NSString*? What are the
2534 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002535 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002536 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002537 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2538 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002539 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002540 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002541 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002542 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002543 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002544 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2545 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002546 return;
2547 }
2548
2549 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002550 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002551 uint32_t FirstArg;
2552 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002553 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002554
2555 // check if the function is variadic if the 3rd argument non-zero
2556 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002557 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002558 ++NumArgs; // +1 for ...
2559 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002560 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002561 return;
2562 }
2563 }
2564
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002565 // strftime requires FirstArg to be 0 because it doesn't read from any
2566 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002567 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002568 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002569 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2570 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002571 return;
2572 }
2573 // if 0 it disables parameter checking (to use with e.g. va_list)
2574 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002575 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002576 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002577 return;
2578 }
2579
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002580 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002581 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002582 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002583 if (NewAttr)
2584 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002585}
2586
Chandler Carruthedc2c642011-07-02 00:01:44 +00002587static void handleTransparentUnionAttr(Sema &S, Decl *D,
2588 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002589 // Try to find the underlying union declaration.
2590 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002591 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002592 if (TD && TD->getUnderlyingType()->isUnionType())
2593 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2594 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002595 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002596
2597 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002598 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002599 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002600 return;
2601 }
2602
John McCallf937c022011-10-07 06:10:15 +00002603 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002604 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002605 diag::warn_transparent_union_attribute_not_definition);
2606 return;
2607 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002608
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002609 RecordDecl::field_iterator Field = RD->field_begin(),
2610 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002611 if (Field == FieldEnd) {
2612 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2613 return;
2614 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002615
David Blaikie40ed2972012-06-06 20:45:41 +00002616 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002617 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002618 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002619 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002620 diag::warn_transparent_union_attribute_floating)
2621 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002622 return;
2623 }
2624
2625 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2626 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2627 for (; Field != FieldEnd; ++Field) {
2628 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002629 // FIXME: this isn't fully correct; we also need to test whether the
2630 // members of the union would all have the same calling convention as the
2631 // first member of the union. Checking just the size and alignment isn't
2632 // sufficient (consider structs passed on the stack instead of in registers
2633 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002634 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002635 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002636 // Warn if we drop the attribute.
2637 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002638 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002639 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002640 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002641 diag::warn_transparent_union_attribute_field_size_align)
2642 << isSize << Field->getDeclName() << FieldBits;
2643 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002644 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002645 diag::note_transparent_union_first_field_size_align)
2646 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002647 return;
2648 }
2649 }
2650
Michael Han99315932013-01-24 16:46:58 +00002651 RD->addAttr(::new (S.Context)
2652 TransparentUnionAttr(Attr.getRange(), S.Context,
2653 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002654}
2655
Chandler Carruthedc2c642011-07-02 00:01:44 +00002656static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002657 // Make sure that there is a string literal as the annotation's single
2658 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002659 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002660 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002661 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002662
2663 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002664 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
2665 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002666 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002667 }
Michael Han99315932013-01-24 16:46:58 +00002668
2669 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002670 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002671 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002672}
2673
Chandler Carruthedc2c642011-07-02 00:01:44 +00002674static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002675 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002676 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002677 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2678 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002679 return;
2680 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002681
Richard Smith848e1f12013-02-01 08:12:08 +00002682 if (Attr.getNumArgs() == 0) {
2683 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2684 true, 0, Attr.getAttributeSpellingListIndex()));
2685 return;
2686 }
2687
Aaron Ballman00e99962013-08-31 01:11:41 +00002688 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002689 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2690 S.Diag(Attr.getEllipsisLoc(),
2691 diag::err_pack_expansion_without_parameter_packs);
2692 return;
2693 }
2694
2695 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2696 return;
2697
2698 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2699 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002700}
2701
2702void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002703 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002704 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2705 SourceLocation AttrLoc = AttrRange.getBegin();
2706
Richard Smith1dba27c2013-01-29 09:02:09 +00002707 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002708 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002709 // C++11 [dcl.align]p1:
2710 // An alignment-specifier may be applied to a variable or to a class
2711 // data member, but it shall not be applied to a bit-field, a function
2712 // parameter, the formal parameter of a catch clause, or a variable
2713 // declared with the register storage class specifier. An
2714 // alignment-specifier may also be applied to the declaration of a class
2715 // or enumeration type.
2716 // C11 6.7.5/2:
2717 // An alignment attribute shall not be specified in a declaration of
2718 // a typedef, or a bit-field, or a function, or a parameter, or an
2719 // object declared with the register storage-class specifier.
2720 int DiagKind = -1;
2721 if (isa<ParmVarDecl>(D)) {
2722 DiagKind = 0;
2723 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2724 if (VD->getStorageClass() == SC_Register)
2725 DiagKind = 1;
2726 if (VD->isExceptionVariable())
2727 DiagKind = 2;
2728 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2729 if (FD->isBitField())
2730 DiagKind = 3;
2731 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00002732 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00002733 << (TmpAttr.isC11() ? ExpectedVariableOrField
2734 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002735 return;
2736 }
2737 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002738 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00002739 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002740 return;
2741 }
2742 }
2743
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002744 if (E->isTypeDependent() || E->isValueDependent()) {
2745 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002746 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2747 AA->setPackExpansion(IsPackExpansion);
2748 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002749 return;
2750 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002751
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002752 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002753 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002754 ExprResult ICE
2755 = VerifyIntegerConstantExpression(E, &Alignment,
2756 diag::err_aligned_attribute_argument_not_int,
2757 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002758 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002759 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002760
2761 // C++11 [dcl.align]p2:
2762 // -- if the constant expression evaluates to zero, the alignment
2763 // specifier shall have no effect
2764 // C11 6.7.5p6:
2765 // An alignment specification of zero has no effect.
2766 if (!(TmpAttr.isAlignas() && !Alignment) &&
2767 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002768 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2769 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002770 return;
2771 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002772
David Majnemerabecae72014-02-12 20:36:10 +00002773 // Alignment calculations can wrap around if it's greater than 2**28.
2774 unsigned MaxValidAlignment = TmpAttr.isDeclspec() ? 8192 : 268435456;
2775 if (Alignment.getZExtValue() > MaxValidAlignment) {
2776 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
2777 << E->getSourceRange();
2778 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00002779 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002780
Richard Smith44c247f2013-02-22 08:32:16 +00002781 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2782 ICE.take(), SpellingListIndex);
2783 AA->setPackExpansion(IsPackExpansion);
2784 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002785}
2786
Michael Hanaf02bbe2013-02-01 01:19:17 +00002787void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002788 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002789 // FIXME: Cache the number on the Attr object if non-dependent?
2790 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002791 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2792 SpellingListIndex);
2793 AA->setPackExpansion(IsPackExpansion);
2794 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002795}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002796
Richard Smith848e1f12013-02-01 08:12:08 +00002797void Sema::CheckAlignasUnderalignment(Decl *D) {
2798 assert(D->hasAttrs() && "no attributes on decl");
2799
2800 QualType Ty;
2801 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2802 Ty = VD->getType();
2803 else
2804 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002805 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002806 return;
2807
2808 // C++11 [dcl.align]p5, C11 6.7.5/4:
2809 // The combined effect of all alignment attributes in a declaration shall
2810 // not specify an alignment that is less strict than the alignment that
2811 // would otherwise be required for the entity being declared.
2812 AlignedAttr *AlignasAttr = 0;
2813 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002814 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00002815 if (I->isAlignmentDependent())
2816 return;
2817 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002818 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00002819 Align = std::max(Align, I->getAlignment(Context));
2820 }
2821
2822 if (AlignasAttr && Align) {
2823 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2824 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2825 if (NaturalAlign > RequestedAlign)
2826 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2827 << Ty << (unsigned)NaturalAlign.getQuantity();
2828 }
2829}
2830
David Majnemer2c4e00a2014-01-29 22:07:36 +00002831bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00002832 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00002833 MSInheritanceAttr::Spelling SemanticSpelling) {
2834 assert(RD->hasDefinition() && "RD has no definition!");
2835
David Majnemer98c9ee22014-02-07 00:43:07 +00002836 // We may not have seen base specifiers or any virtual methods yet. We will
2837 // have to wait until the record is defined to catch any mismatches.
2838 if (!RD->getDefinition()->isCompleteDefinition())
2839 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002840
David Majnemer98c9ee22014-02-07 00:43:07 +00002841 // The unspecified model never matches what a definition could need.
2842 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
2843 return false;
2844
David Majnemer4bb09802014-02-10 19:50:15 +00002845 if (BestCase) {
2846 if (RD->calculateInheritanceModel() == SemanticSpelling)
2847 return false;
2848 } else {
2849 if (RD->calculateInheritanceModel() <= SemanticSpelling)
2850 return false;
2851 }
David Majnemer98c9ee22014-02-07 00:43:07 +00002852
2853 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
2854 << 0 /*definition*/;
2855 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
2856 << RD->getNameAsString();
2857 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002858}
2859
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002860/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002861/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002862///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002863/// Despite what would be logical, the mode attribute is a decl attribute, not a
2864/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2865/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002866static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002867 // This attribute isn't documented, but glibc uses it. It changes
2868 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002869 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002870 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2871 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002872 return;
2873 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002874
Aaron Ballman00e99962013-08-31 01:11:41 +00002875 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2876 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002877
2878 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002879 if (Str.startswith("__") && Str.endswith("__"))
2880 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002881
2882 unsigned DestWidth = 0;
2883 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002884 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002885 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002886 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002887 switch (Str[0]) {
2888 case 'Q': DestWidth = 8; break;
2889 case 'H': DestWidth = 16; break;
2890 case 'S': DestWidth = 32; break;
2891 case 'D': DestWidth = 64; break;
2892 case 'X': DestWidth = 96; break;
2893 case 'T': DestWidth = 128; break;
2894 }
2895 if (Str[1] == 'F') {
2896 IntegerMode = false;
2897 } else if (Str[1] == 'C') {
2898 IntegerMode = false;
2899 ComplexMode = true;
2900 } else if (Str[1] != 'I') {
2901 DestWidth = 0;
2902 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002903 break;
2904 case 4:
2905 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2906 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002907 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002908 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002909 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002910 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002911 break;
2912 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002913 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002914 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002915 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002916 case 11:
2917 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00002918 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002919 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002920 }
2921
2922 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002923 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002924 OldTy = TD->getUnderlyingType();
2925 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2926 OldTy = VD->getType();
2927 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002928 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00002929 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002930 return;
2931 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002932
John McCall9dd450b2009-09-21 23:43:11 +00002933 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002934 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2935 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002936 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002937 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2938 } else if (ComplexMode) {
2939 if (!OldTy->isComplexType())
2940 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2941 } else {
2942 if (!OldTy->isFloatingType())
2943 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2944 }
2945
Mike Stump87c57ac2009-05-16 07:39:55 +00002946 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2947 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002948 // FIXME: Make sure floating-point mappings are accurate
2949 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002950 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00002951 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002952 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002953 }
2954
2955 QualType NewTy;
2956
2957 if (IntegerMode)
2958 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
2959 OldTy->isSignedIntegerType());
2960 else
2961 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
2962
2963 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00002964 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002965 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002966 }
2967
Eli Friedman4735374e2009-03-03 06:41:03 +00002968 if (ComplexMode) {
2969 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002970 }
2971
2972 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002973 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
2974 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
2975 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00002976 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002977
2978 D->addAttr(::new (S.Context)
2979 ModeAttr(Attr.getRange(), S.Context, Name,
2980 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00002981}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002982
Chandler Carruthedc2c642011-07-02 00:01:44 +00002983static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00002984 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2985 if (!VD->hasGlobalStorage())
2986 S.Diag(Attr.getLoc(),
2987 diag::warn_attribute_requires_functions_or_static_globals)
2988 << Attr.getName();
2989 } else if (!isFunctionOrMethod(D)) {
2990 S.Diag(Attr.getLoc(),
2991 diag::warn_attribute_requires_functions_or_static_globals)
2992 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00002993 return;
2994 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002995
Michael Han99315932013-01-24 16:46:58 +00002996 D->addAttr(::new (S.Context)
2997 NoDebugAttr(Attr.getRange(), S.Context,
2998 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00002999}
3000
Chandler Carruthedc2c642011-07-02 00:01:44 +00003001static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003002 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003003 if (!FD->getReturnType()->isVoidType()) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003004 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
3005 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
3006 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3007 << FD->getType()
Alp Toker42a16a62014-01-25 23:51:36 +00003008 << FixItHint::CreateReplacement(FTL.getReturnLoc().getSourceRange(),
Aaron Ballman3aff6332013-12-02 19:30:36 +00003009 "void");
3010 } else {
3011 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3012 << FD->getType();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003013 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003014 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003015 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003016
Aaron Ballman3aff6332013-12-02 19:30:36 +00003017 D->addAttr(::new (S.Context)
3018 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003019 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003020}
3021
Chandler Carruthedc2c642011-07-02 00:01:44 +00003022static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003023 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003024 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003025 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003026 return;
3027 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003028
Michael Han99315932013-01-24 16:46:58 +00003029 D->addAttr(::new (S.Context)
3030 GNUInlineAttr(Attr.getRange(), S.Context,
3031 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003032}
3033
Chandler Carruthedc2c642011-07-02 00:01:44 +00003034static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003035 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003036
Aaron Ballman02df2e02012-12-09 17:45:41 +00003037 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003038 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003039 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3040 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003041 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003042 return;
3043
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003044 if (!isa<ObjCMethodDecl>(D)) {
3045 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3046 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003047 return;
3048 }
3049
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003050 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003051 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003052 D->addAttr(::new (S.Context)
3053 FastCallAttr(Attr.getRange(), S.Context,
3054 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003055 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003056 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003057 D->addAttr(::new (S.Context)
3058 StdCallAttr(Attr.getRange(), S.Context,
3059 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003060 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003061 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003062 D->addAttr(::new (S.Context)
3063 ThisCallAttr(Attr.getRange(), S.Context,
3064 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003065 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003066 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003067 D->addAttr(::new (S.Context)
3068 CDeclAttr(Attr.getRange(), S.Context,
3069 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003070 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003071 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003072 D->addAttr(::new (S.Context)
3073 PascalAttr(Attr.getRange(), S.Context,
3074 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003075 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003076 case AttributeList::AT_MSABI:
3077 D->addAttr(::new (S.Context)
3078 MSABIAttr(Attr.getRange(), S.Context,
3079 Attr.getAttributeSpellingListIndex()));
3080 return;
3081 case AttributeList::AT_SysVABI:
3082 D->addAttr(::new (S.Context)
3083 SysVABIAttr(Attr.getRange(), S.Context,
3084 Attr.getAttributeSpellingListIndex()));
3085 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003086 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003087 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003088 switch (CC) {
3089 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003090 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003091 break;
3092 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003093 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003094 break;
3095 default:
3096 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003097 }
3098
Michael Han99315932013-01-24 16:46:58 +00003099 D->addAttr(::new (S.Context)
3100 PcsAttr(Attr.getRange(), S.Context, PCS,
3101 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003102 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003103 }
Derek Schuffa2020962012-10-16 22:30:41 +00003104 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003105 D->addAttr(::new (S.Context)
3106 PnaclCallAttr(Attr.getRange(), S.Context,
3107 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003108 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003109 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003110 D->addAttr(::new (S.Context)
3111 IntelOclBiccAttr(Attr.getRange(), S.Context,
3112 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003113 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003114
Abramo Bagnara50099372010-04-30 13:10:51 +00003115 default:
3116 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003117 }
3118}
3119
Aaron Ballman02df2e02012-12-09 17:45:41 +00003120bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3121 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003122 if (attr.isInvalid())
3123 return true;
3124
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003125 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003126 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003127 attr.setInvalid();
3128 return true;
3129 }
3130
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003131 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003132 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003133 case AttributeList::AT_CDecl: CC = CC_C; break;
3134 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3135 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3136 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3137 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003138 case AttributeList::AT_MSABI:
3139 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3140 CC_X86_64Win64;
3141 break;
3142 case AttributeList::AT_SysVABI:
3143 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3144 CC_C;
3145 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003146 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003147 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003148 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003149 attr.setInvalid();
3150 return true;
3151 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003152 if (StrRef == "aapcs") {
3153 CC = CC_AAPCS;
3154 break;
3155 } else if (StrRef == "aapcs-vfp") {
3156 CC = CC_AAPCS_VFP;
3157 break;
3158 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003159
3160 attr.setInvalid();
3161 Diag(attr.getLoc(), diag::err_invalid_pcs);
3162 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003163 }
Derek Schuffa2020962012-10-16 22:30:41 +00003164 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003165 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003166 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003167 }
3168
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003169 const TargetInfo &TI = Context.getTargetInfo();
3170 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3171 if (A == TargetInfo::CCCR_Warning) {
3172 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003173
3174 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3175 if (FD)
3176 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3177 TargetInfo::CCMT_NonMember;
3178 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003179 }
3180
John McCall3882ace2011-01-05 12:14:39 +00003181 return false;
3182}
3183
John McCall3882ace2011-01-05 12:14:39 +00003184/// Checks a regparm attribute, returning true if it is ill-formed and
3185/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003186bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3187 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003188 return true;
3189
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003190 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003191 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003192 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003193 }
Eli Friedman7044b762009-03-27 21:06:47 +00003194
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003195 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003196 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003197 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003198 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003199 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003200 }
3201
Douglas Gregore8bbc122011-09-02 00:18:52 +00003202 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003203 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003204 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003205 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003206 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003207 }
3208
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003209 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003210 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003211 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003212 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003213 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003214 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003215 }
3216
John McCall3882ace2011-01-05 12:14:39 +00003217 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003218}
3219
Aaron Ballman66039932013-12-19 00:41:31 +00003220static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3221 const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003222 // check the attribute arguments.
3223 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3224 // FIXME: 0 is not okay.
Aaron Ballman05e420a2014-01-02 21:26:14 +00003225 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3226 << Attr.getName() << 2;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003227 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003228 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003229
Aaron Ballman66039932013-12-19 00:41:31 +00003230 uint32_t MaxThreads, MinBlocks = 0;
3231 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3232 return;
3233 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3234 Attr.getArgAsExpr(1),
3235 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003236 return;
3237
3238 D->addAttr(::new (S.Context)
3239 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3240 MaxThreads, MinBlocks,
3241 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003242}
3243
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003244static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3245 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003246 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003247 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003248 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003249 return;
3250 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003251
3252 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003253 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003254
Aaron Ballman00e99962013-08-31 01:11:41 +00003255 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003256
3257 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3258 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3259 << Attr.getName() << ExpectedFunctionOrMethod;
3260 return;
3261 }
3262
3263 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003264 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3265 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003266 return;
3267
3268 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003269 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3270 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003271 return;
3272
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003273 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003274 if (IsPointer) {
3275 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003276 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003277 if (!BufferTy->isPointerType()) {
3278 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003279 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003280 }
3281 }
3282
Michael Han99315932013-01-24 16:46:58 +00003283 D->addAttr(::new (S.Context)
3284 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3285 ArgumentIdx, TypeTagIdx, IsPointer,
3286 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003287}
3288
3289static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3290 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003291 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003292 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003293 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003294 return;
3295 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003296
3297 if (!checkAttributeNumArgs(S, Attr, 1))
3298 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003299
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003300 if (!isa<VarDecl>(D)) {
3301 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3302 << Attr.getName() << ExpectedVariable;
3303 return;
3304 }
3305
Aaron Ballman00e99962013-08-31 01:11:41 +00003306 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003307 TypeSourceInfo *MatchingCTypeLoc = 0;
3308 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3309 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003310
Michael Han99315932013-01-24 16:46:58 +00003311 D->addAttr(::new (S.Context)
3312 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003313 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003314 Attr.getLayoutCompatible(),
3315 Attr.getMustBeNull(),
3316 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003317}
3318
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003319//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003320// Checker-specific attribute handlers.
3321//===----------------------------------------------------------------------===//
3322
John McCalled433932011-01-25 03:31:58 +00003323static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003324 return type->isDependentType() ||
3325 type->isObjCObjectPointerType() ||
3326 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003327}
3328static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003329 return type->isDependentType() ||
3330 type->isPointerType() ||
3331 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003332}
3333
Chandler Carruthedc2c642011-07-02 00:01:44 +00003334static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003335 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003336 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003337
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003338 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003339 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3340 cf = false;
3341 } else {
3342 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3343 cf = true;
3344 }
3345
3346 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003347 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003348 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003349 return;
3350 }
3351
3352 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003353 param->addAttr(::new (S.Context)
3354 CFConsumedAttr(Attr.getRange(), S.Context,
3355 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003356 else
Michael Han99315932013-01-24 16:46:58 +00003357 param->addAttr(::new (S.Context)
3358 NSConsumedAttr(Attr.getRange(), S.Context,
3359 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003360}
3361
Chandler Carruthedc2c642011-07-02 00:01:44 +00003362static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3363 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003364
John McCalled433932011-01-25 03:31:58 +00003365 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003366
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003367 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003368 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003369 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003370 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003371 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003372 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3373 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003374 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003375 returnType = FD->getReturnType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003376 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003377 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003378 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003379 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003380 return;
3381 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003382
John McCalled433932011-01-25 03:31:58 +00003383 bool typeOK;
3384 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003385 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003386 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003387 case AttributeList::AT_NSReturnsAutoreleased:
3388 case AttributeList::AT_NSReturnsRetained:
3389 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003390 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3391 cf = false;
3392 break;
3393
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003394 case AttributeList::AT_CFReturnsRetained:
3395 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003396 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3397 cf = true;
3398 break;
3399 }
3400
3401 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003402 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003403 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003404 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003405 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003406
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003407 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003408 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003409 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003410 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003411 D->addAttr(::new (S.Context)
3412 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3413 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003414 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003415 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003416 D->addAttr(::new (S.Context)
3417 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3418 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003419 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003420 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003421 D->addAttr(::new (S.Context)
3422 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3423 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003424 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003425 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003426 D->addAttr(::new (S.Context)
3427 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3428 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003429 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003430 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003431 D->addAttr(::new (S.Context)
3432 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3433 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003434 return;
3435 };
3436}
3437
John McCallcf166702011-07-22 08:53:00 +00003438static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3439 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003440 const int EP_ObjCMethod = 1;
3441 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003442
John McCallcf166702011-07-22 08:53:00 +00003443 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003444 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003445 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003446 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003447 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003448 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003449
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003450 if (!resultType->isReferenceType() &&
3451 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003452 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003453 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003454 << attr.getName()
3455 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003456 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003457
3458 // Drop the attribute.
3459 return;
3460 }
3461
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003462 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003463 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3464 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003465}
3466
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003467static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3468 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003469 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003470
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003471 DeclContext *DC = method->getDeclContext();
3472 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3473 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3474 << attr.getName() << 0;
3475 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3476 return;
3477 }
3478 if (method->getMethodFamily() == OMF_dealloc) {
3479 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3480 << attr.getName() << 1;
3481 return;
3482 }
3483
Michael Han99315932013-01-24 16:46:58 +00003484 method->addAttr(::new (S.Context)
3485 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3486 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003487}
3488
Aaron Ballmanfb763042013-12-02 18:05:46 +00003489static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3490 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003491 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00003492 return;
John McCall32f5fe12011-09-30 05:12:12 +00003493
Aaron Ballmanfb763042013-12-02 18:05:46 +00003494 D->addAttr(::new (S.Context)
3495 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3496 Attr.getAttributeSpellingListIndex()));
3497}
3498
3499static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3500 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003501 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00003502 return;
3503
3504 D->addAttr(::new (S.Context)
3505 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3506 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003507}
3508
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003509static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3510 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003511 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003512
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003513 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003514 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003515 return;
3516 }
3517
3518 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003519 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003520 Attr.getAttributeSpellingListIndex()));
3521}
3522
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003523static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3524 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003525 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003526
3527 if (!Parm) {
3528 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3529 return;
3530 }
3531
3532 D->addAttr(::new (S.Context)
3533 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3534 Attr.getAttributeSpellingListIndex()));
3535}
3536
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003537static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3538 const AttributeList &Attr) {
3539 IdentifierInfo *RelatedClass =
3540 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0;
3541 if (!RelatedClass) {
3542 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3543 return;
3544 }
3545 IdentifierInfo *ClassMethod =
3546 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0;
3547 IdentifierInfo *InstanceMethod =
3548 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0;
3549 D->addAttr(::new (S.Context)
3550 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3551 ClassMethod, InstanceMethod,
3552 Attr.getAttributeSpellingListIndex()));
3553}
3554
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003555static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3556 const AttributeList &Attr) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00003557 ObjCInterfaceDecl *IFace;
3558 if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
3559 IFace = CatDecl->getClassInterface();
3560 else
3561 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003562 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003563 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003564 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3565 Attr.getAttributeSpellingListIndex()));
3566}
3567
Chandler Carruthedc2c642011-07-02 00:01:44 +00003568static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3569 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003570 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003571
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003572 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003573 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003574}
3575
Chandler Carruthedc2c642011-07-02 00:01:44 +00003576static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3577 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003578 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003579 QualType type = vd->getType();
3580
3581 if (!type->isDependentType() &&
3582 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003583 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003584 << type;
3585 return;
3586 }
3587
3588 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3589
3590 // If we have no lifetime yet, check the lifetime we're presumably
3591 // going to infer.
3592 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3593 lifetime = type->getObjCARCImplicitLifetime();
3594
3595 switch (lifetime) {
3596 case Qualifiers::OCL_None:
3597 assert(type->isDependentType() &&
3598 "didn't infer lifetime for non-dependent type?");
3599 break;
3600
3601 case Qualifiers::OCL_Weak: // meaningful
3602 case Qualifiers::OCL_Strong: // meaningful
3603 break;
3604
3605 case Qualifiers::OCL_ExplicitNone:
3606 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003607 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003608 << (lifetime == Qualifiers::OCL_Autoreleasing);
3609 break;
3610 }
3611
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003612 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003613 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3614 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003615}
3616
Francois Picheta83957a2010-12-19 06:50:37 +00003617//===----------------------------------------------------------------------===//
3618// Microsoft specific attribute handlers.
3619//===----------------------------------------------------------------------===//
3620
Chandler Carruthedc2c642011-07-02 00:01:44 +00003621static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003622 if (!S.LangOpts.CPlusPlus) {
3623 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3624 << Attr.getName() << AttributeLangSupport::C;
3625 return;
3626 }
3627
Aaron Ballman60e705e2013-11-24 20:58:02 +00003628 if (!isa<CXXRecordDecl>(D)) {
3629 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3630 << Attr.getName() << ExpectedClass;
3631 return;
3632 }
3633
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003634 StringRef StrRef;
3635 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003636 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003637 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003638
David Majnemer89085342013-08-09 08:56:20 +00003639 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3640 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003641 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3642 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003643
Reid Kleckner140c4a72013-05-17 14:04:52 +00003644 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003645 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003646 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003647 return;
3648 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003649
David Majnemer89085342013-08-09 08:56:20 +00003650 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003651 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003652 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003653 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003654 return;
3655 }
David Majnemer89085342013-08-09 08:56:20 +00003656 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003657 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003658 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003659 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003660 }
Francois Picheta83957a2010-12-19 06:50:37 +00003661
David Majnemer89085342013-08-09 08:56:20 +00003662 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3663 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003664}
3665
David Majnemer2c4e00a2014-01-29 22:07:36 +00003666static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3667 if (!S.LangOpts.CPlusPlus) {
3668 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3669 << Attr.getName() << AttributeLangSupport::C;
3670 return;
3671 }
3672 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00003673 D, Attr.getRange(), /*BestCase=*/true,
3674 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00003675 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
3676 if (IA)
3677 D->addAttr(IA);
3678}
3679
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003680static void handleARMInterruptAttr(Sema &S, Decl *D,
3681 const AttributeList &Attr) {
3682 // Check the attribute arguments.
3683 if (Attr.getNumArgs() > 1) {
3684 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3685 << Attr.getName() << 1;
3686 return;
3687 }
3688
3689 StringRef Str;
3690 SourceLocation ArgLoc;
3691
3692 if (Attr.getNumArgs() == 0)
3693 Str = "";
3694 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
3695 return;
3696
3697 ARMInterruptAttr::InterruptType Kind;
3698 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
3699 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
3700 << Attr.getName() << Str << ArgLoc;
3701 return;
3702 }
3703
3704 unsigned Index = Attr.getAttributeSpellingListIndex();
3705 D->addAttr(::new (S.Context)
3706 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
3707}
3708
3709static void handleMSP430InterruptAttr(Sema &S, Decl *D,
3710 const AttributeList &Attr) {
3711 if (!checkAttributeNumArgs(S, Attr, 1))
3712 return;
3713
3714 if (!Attr.isArgExpr(0)) {
3715 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3716 << AANT_ArgumentIntegerConstant;
3717 return;
3718 }
3719
3720 // FIXME: Check for decl - it should be void ()(void).
3721
3722 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3723 llvm::APSInt NumParams(32);
3724 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
3725 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3726 << Attr.getName() << AANT_ArgumentIntegerConstant
3727 << NumParamsExpr->getSourceRange();
3728 return;
3729 }
3730
3731 unsigned Num = NumParams.getLimitedValue(255);
3732 if ((Num & 1) || Num > 30) {
3733 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3734 << Attr.getName() << (int)NumParams.getSExtValue()
3735 << NumParamsExpr->getSourceRange();
3736 return;
3737 }
3738
Aaron Ballman36a53502014-01-16 13:03:14 +00003739 D->addAttr(::new (S.Context)
3740 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
3741 Attr.getAttributeSpellingListIndex()));
3742 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003743}
3744
3745static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3746 // Dispatch the interrupt attribute based on the current target.
3747 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
3748 handleMSP430InterruptAttr(S, D, Attr);
3749 else
3750 handleARMInterruptAttr(S, D, Attr);
3751}
3752
3753static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
3754 const AttributeList& Attr) {
3755 // If we try to apply it to a function pointer, don't warn, but don't
3756 // do anything, either. It doesn't matter anyway, because there's nothing
3757 // special about calling a force_align_arg_pointer function.
3758 ValueDecl *VD = dyn_cast<ValueDecl>(D);
3759 if (VD && VD->getType()->isFunctionPointerType())
3760 return;
3761 // Also don't warn on function pointer typedefs.
3762 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
3763 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
3764 TD->getUnderlyingType()->isFunctionType()))
3765 return;
3766 // Attribute can only be applied to function types.
3767 if (!isa<FunctionDecl>(D)) {
3768 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3769 << Attr.getName() << /* function */0;
3770 return;
3771 }
3772
Aaron Ballman36a53502014-01-16 13:03:14 +00003773 D->addAttr(::new (S.Context)
3774 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
3775 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003776}
3777
3778DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
3779 unsigned AttrSpellingListIndex) {
3780 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00003781 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003782 return NULL;
3783 }
3784
3785 if (D->hasAttr<DLLImportAttr>())
3786 return NULL;
3787
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003788 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003789}
3790
3791static void handleDLLImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3792 // Attribute can be applied only to functions or variables.
3793 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3794 if (!FD && !isa<VarDecl>(D)) {
3795 // Apparently Visual C++ thinks it is okay to not emit a warning
3796 // in this case, so only emit a warning when -fms-extensions is not
3797 // specified.
3798 if (!S.getLangOpts().MicrosoftExt)
3799 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003800 << Attr.getName() << ExpectedVariableOrFunction;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003801 return;
3802 }
3803
3804 // Currently, the dllimport attribute is ignored for inlined functions.
3805 // Warning is emitted.
3806 if (FD && FD->isInlineSpecified()) {
3807 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3808 return;
3809 }
3810
3811 unsigned Index = Attr.getAttributeSpellingListIndex();
3812 DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index);
3813 if (NewAttr)
3814 D->addAttr(NewAttr);
3815}
3816
3817DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
3818 unsigned AttrSpellingListIndex) {
3819 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00003820 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003821 D->dropAttr<DLLImportAttr>();
3822 }
3823
3824 if (D->hasAttr<DLLExportAttr>())
3825 return NULL;
3826
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003827 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003828}
3829
3830static void handleDLLExportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3831 // Currently, the dllexport attribute is ignored for inlined functions, unless
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003832 // the -fkeep-inline-functions flag has been used. Warning is emitted.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003833 if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isInlineSpecified()) {
3834 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
3835 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3836 return;
3837 }
3838
3839 unsigned Index = Attr.getAttributeSpellingListIndex();
3840 DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index);
3841 if (NewAttr)
3842 D->addAttr(NewAttr);
3843}
3844
David Majnemer2c4e00a2014-01-29 22:07:36 +00003845MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00003846Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003847 unsigned AttrSpellingListIndex,
3848 MSInheritanceAttr::Spelling SemanticSpelling) {
3849 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
3850 if (IA->getSemanticSpelling() == SemanticSpelling)
3851 return 0;
3852 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
3853 << 1 /*previous declaration*/;
3854 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
3855 D->dropAttr<MSInheritanceAttr>();
3856 }
3857
3858 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
3859 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00003860 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
3861 SemanticSpelling)) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00003862 return 0;
3863 }
3864 } else {
3865 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
3866 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
3867 << 1 /*partial specialization*/;
3868 return 0;
3869 }
3870 if (RD->getDescribedClassTemplate()) {
3871 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
3872 << 0 /*primary template*/;
3873 return 0;
3874 }
3875 }
3876
3877 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00003878 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00003879}
3880
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003881static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3882 // The capability attributes take a single string parameter for the name of
3883 // the capability they represent. The lockable attribute does not take any
3884 // parameters. However, semantically, both attributes represent the same
3885 // concept, and so they use the same semantic attribute. Eventually, the
3886 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00003887 //
3888 // For backwards compatibility, any capability which has no specified string
3889 // literal will be considered a "mutex."
3890 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003891 SourceLocation LiteralLoc;
3892 if (Attr.getKind() == AttributeList::AT_Capability &&
3893 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
3894 return;
3895
Aaron Ballman6c810072014-03-05 21:47:13 +00003896 // Currently, there are only two names allowed for a capability: role and
3897 // mutex (case insensitive). Diagnose other capability names.
3898 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
3899 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
3900
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003901 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
3902 Attr.getAttributeSpellingListIndex()));
3903}
3904
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003905static void handleAssertCapabilityAttr(Sema &S, Decl *D,
3906 const AttributeList &Attr) {
3907 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
3908 Attr.getArgAsExpr(0),
3909 Attr.getAttributeSpellingListIndex()));
3910}
3911
3912static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
3913 const AttributeList &Attr) {
3914 SmallVector<Expr*, 1> Args;
3915 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
3916 return;
3917
3918 // Check that all arguments are lockable objects.
3919 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
3920 if (Args.empty())
3921 return;
3922
3923 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
3924 S.Context,
3925 Args.data(), Args.size(),
3926 Attr.getAttributeSpellingListIndex()));
3927}
3928
3929static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
3930 const AttributeList &Attr) {
3931 SmallVector<Expr*, 2> Args;
3932 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
3933 return;
3934
3935 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
3936 S.Context,
3937 Attr.getArgAsExpr(0),
3938 Args.data(),
3939 Args.size(),
3940 Attr.getAttributeSpellingListIndex()));
3941}
3942
3943static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
3944 const AttributeList &Attr) {
3945 SmallVector<Expr*, 1> Args;
3946 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
3947 return;
3948
3949 // Check that all arguments are lockable objects.
3950 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
3951 if (Args.empty())
3952 return;
3953
3954 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(Attr.getRange(),
3955 S.Context,
3956 Args.data(), Args.size(),
3957 Attr.getAttributeSpellingListIndex()));
3958}
3959
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003960static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
3961 const AttributeList &Attr) {
3962 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
3963 return;
3964
3965 // check that all arguments are lockable objects
3966 SmallVector<Expr*, 1> Args;
3967 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
3968 if (Args.empty())
3969 return;
3970
3971 RequiresCapabilityAttr *RCA = ::new (S.Context)
3972 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
3973 Args.size(), Attr.getAttributeSpellingListIndex());
3974
3975 D->addAttr(RCA);
3976}
3977
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003978/// Handles semantic checking for features that are common to all attributes,
3979/// such as checking whether a parameter was properly specified, or the correct
3980/// number of arguments were passed, etc.
3981static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3982 const AttributeList &Attr) {
3983 // Several attributes carry different semantics than the parsing requires, so
3984 // those are opted out of the common handling.
3985 //
3986 // We also bail on unknown and ignored attributes because those are handled
3987 // as part of the target-specific handling logic.
3988 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003989 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003990 return false;
3991
Aaron Ballman3aff6332013-12-02 19:30:36 +00003992 // Check whether the attribute requires specific language extensions to be
3993 // enabled.
3994 if (!Attr.diagnoseLangOpts(S))
3995 return true;
3996
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003997 // If there are no optional arguments, then checking for the argument count
3998 // is trivial.
3999 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4000 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4001 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004002
4003 // Check whether the attribute appertains to the given subject.
4004 if (!Attr.diagnoseAppertainsTo(S, D))
4005 return true;
4006
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004007 return false;
4008}
4009
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004010//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004011// Top Level Sema Entry Points
4012//===----------------------------------------------------------------------===//
4013
Richard Smithf8a75c32013-08-29 00:47:48 +00004014/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4015/// the attribute applies to decls. If the attribute is a type attribute, just
4016/// silently ignore it if a GNU attribute.
4017static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4018 const AttributeList &Attr,
4019 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004020 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00004021 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004022
Richard Smithf8a75c32013-08-29 00:47:48 +00004023 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4024 // instead.
4025 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4026 return;
4027
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004028 // Unknown attributes are automatically warned on. Target-specific attributes
4029 // which do not apply to the current target architecture are treated as
4030 // though they were unknown attributes.
4031 if (Attr.getKind() == AttributeList::UnknownAttribute ||
4032 !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) {
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004033 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute()
4034 ? diag::warn_unhandled_ms_attribute_ignored
4035 : diag::warn_unknown_attribute_ignored)
4036 << Attr.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004037 return;
4038 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004039
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004040 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4041 return;
4042
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004043 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004044 default:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004045 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004046 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004047 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004048 case AttributeList::AT_Interrupt:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004049 handleInterruptAttr(S, D, Attr);
4050 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004051 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004052 handleX86ForceAlignArgPointerAttr(S, D, Attr);
4053 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004054 case AttributeList::AT_DLLExport:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004055 handleDLLExportAttr(S, D, Attr);
4056 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004057 case AttributeList::AT_DLLImport:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004058 handleDLLImportAttr(S, D, Attr);
4059 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004060 case AttributeList::AT_Mips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004061 handleSimpleAttribute<Mips16Attr>(S, D, Attr);
4062 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004063 case AttributeList::AT_NoMips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004064 handleSimpleAttribute<NoMips16Attr>(S, D, Attr);
4065 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00004066 case AttributeList::AT_IBAction:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004067 handleSimpleAttribute<IBActionAttr>(S, D, Attr);
4068 break;
4069 case AttributeList::AT_IBOutlet:
4070 handleIBOutlet(S, D, Attr);
4071 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004072 case AttributeList::AT_IBOutletCollection:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004073 handleIBOutletCollection(S, D, Attr);
4074 break;
4075 case AttributeList::AT_Alias:
4076 handleAliasAttr(S, D, Attr);
4077 break;
4078 case AttributeList::AT_Aligned:
4079 handleAlignedAttr(S, D, Attr);
4080 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004081 case AttributeList::AT_AlwaysInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004082 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr);
4083 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004084 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004085 handleAnalyzerNoReturnAttr(S, D, Attr);
4086 break;
4087 case AttributeList::AT_TLSModel:
4088 handleTLSModelAttr(S, D, Attr);
4089 break;
4090 case AttributeList::AT_Annotate:
4091 handleAnnotateAttr(S, D, Attr);
4092 break;
4093 case AttributeList::AT_Availability:
4094 handleAvailabilityAttr(S, D, Attr);
4095 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004096 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004097 handleDependencyAttr(S, scope, D, Attr);
4098 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004099 case AttributeList::AT_Common:
4100 handleCommonAttr(S, D, Attr);
4101 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004102 case AttributeList::AT_CUDAConstant:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004103 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr);
4104 break;
4105 case AttributeList::AT_Constructor:
4106 handleConstructorAttr(S, D, Attr);
4107 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004108 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004109 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr);
4110 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004111 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004112 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004113 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004114 case AttributeList::AT_Destructor:
4115 handleDestructorAttr(S, D, Attr);
4116 break;
4117 case AttributeList::AT_EnableIf:
4118 handleEnableIfAttr(S, D, Attr);
4119 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004120 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004121 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004122 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004123 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004124 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004125 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004126 case AttributeList::AT_Format:
4127 handleFormatAttr(S, D, Attr);
4128 break;
4129 case AttributeList::AT_FormatArg:
4130 handleFormatArgAttr(S, D, Attr);
4131 break;
4132 case AttributeList::AT_CUDAGlobal:
4133 handleGlobalAttr(S, D, Attr);
4134 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00004135 case AttributeList::AT_CUDADevice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004136 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr);
4137 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004138 case AttributeList::AT_CUDAHost:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004139 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr);
4140 break;
4141 case AttributeList::AT_GNUInline:
4142 handleGNUInlineAttr(S, D, Attr);
4143 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004144 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004145 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004146 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004147 case AttributeList::AT_Malloc:
4148 handleMallocAttr(S, D, Attr);
4149 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004150 case AttributeList::AT_MayAlias:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004151 handleSimpleAttribute<MayAliasAttr>(S, D, Attr);
4152 break;
4153 case AttributeList::AT_Mode:
4154 handleModeAttr(S, D, Attr);
4155 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004156 case AttributeList::AT_NoCommon:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004157 handleSimpleAttribute<NoCommonAttr>(S, D, Attr);
4158 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00004159 case AttributeList::AT_NonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004160 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
4161 handleNonNullAttrParameter(S, PVD, Attr);
4162 else
4163 handleNonNullAttr(S, D, Attr);
4164 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00004165 case AttributeList::AT_ReturnsNonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004166 handleReturnsNonNullAttr(S, D, Attr);
4167 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004168 case AttributeList::AT_Overloadable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004169 handleSimpleAttribute<OverloadableAttr>(S, D, Attr);
4170 break;
4171 case AttributeList::AT_Ownership:
4172 handleOwnershipAttr(S, D, Attr);
4173 break;
4174 case AttributeList::AT_Cold:
4175 handleColdAttr(S, D, Attr);
4176 break;
4177 case AttributeList::AT_Hot:
4178 handleHotAttr(S, D, Attr);
4179 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004180 case AttributeList::AT_Naked:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004181 handleSimpleAttribute<NakedAttr>(S, D, Attr);
4182 break;
4183 case AttributeList::AT_NoReturn:
4184 handleNoReturnAttr(S, D, Attr);
4185 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004186 case AttributeList::AT_NoThrow:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004187 handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
4188 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004189 case AttributeList::AT_CUDAShared:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004190 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr);
4191 break;
4192 case AttributeList::AT_VecReturn:
4193 handleVecReturnAttr(S, D, Attr);
4194 break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004195
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004196 case AttributeList::AT_ObjCOwnership:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004197 handleObjCOwnershipAttr(S, D, Attr);
4198 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004199 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004200 handleObjCPreciseLifetimeAttr(S, D, Attr);
4201 break;
John McCall31168b02011-06-15 23:02:42 +00004202
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004203 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004204 handleObjCReturnsInnerPointerAttr(S, D, Attr);
4205 break;
John McCallcf166702011-07-22 08:53:00 +00004206
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004207 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004208 handleObjCRequiresSuperAttr(S, D, Attr);
4209 break;
4210
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004211 case AttributeList::AT_ObjCBridge:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004212 handleObjCBridgeAttr(S, scope, D, Attr);
4213 break;
4214
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004215 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004216 handleObjCBridgeMutableAttr(S, scope, D, Attr);
4217 break;
4218
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004219 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004220 handleObjCBridgeRelatedAttr(S, scope, D, Attr);
4221 break;
John McCallf1e8b342011-09-29 07:17:38 +00004222
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004223 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004224 handleObjCDesignatedInitializer(S, D, Attr);
4225 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004226
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004227 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004228 handleCFAuditedTransferAttr(S, D, Attr);
4229 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004230 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004231 handleCFUnknownTransferAttr(S, D, Attr);
4232 break;
John McCall32f5fe12011-09-30 05:12:12 +00004233
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004234 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004235 case AttributeList::AT_NSConsumed:
4236 handleNSConsumedAttr(S, D, Attr);
4237 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004238 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004239 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr);
4240 break;
John McCalled433932011-01-25 03:31:58 +00004241
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004242 case AttributeList::AT_NSReturnsAutoreleased:
4243 case AttributeList::AT_NSReturnsNotRetained:
4244 case AttributeList::AT_CFReturnsNotRetained:
4245 case AttributeList::AT_NSReturnsRetained:
4246 case AttributeList::AT_CFReturnsRetained:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004247 handleNSReturnsRetainedAttr(S, D, Attr);
4248 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004249 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004250 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr);
4251 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004252 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004253 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr);
4254 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004255 case AttributeList::AT_VecTypeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004256 handleVecTypeHint(S, D, Attr);
4257 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004258
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004259 case AttributeList::AT_InitPriority:
4260 handleInitPriorityAttr(S, D, Attr);
4261 break;
4262
4263 case AttributeList::AT_Packed:
4264 handlePackedAttr(S, D, Attr);
4265 break;
4266 case AttributeList::AT_Section:
4267 handleSectionAttr(S, D, Attr);
4268 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004269 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004270 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004271 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004272 case AttributeList::AT_ArcWeakrefUnavailable:
4273 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr);
4274 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004275 case AttributeList::AT_ObjCRootClass:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004276 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr);
4277 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004278 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek438f8db2014-02-22 01:06:05 +00004279 handleObjCSuppresProtocolAttr(S, D, Attr);
Ted Kremenek28eace62013-11-23 01:01:34 +00004280 break;
4281 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004282 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr);
4283 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00004284 case AttributeList::AT_Unused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004285 handleSimpleAttribute<UnusedAttr>(S, D, Attr);
4286 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004287 case AttributeList::AT_ReturnsTwice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004288 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr);
4289 break;
4290 case AttributeList::AT_Used:
4291 handleUsedAttr(S, D, Attr);
4292 break;
John McCalld041a9b2013-02-20 01:54:26 +00004293 case AttributeList::AT_Visibility:
4294 handleVisibilityAttr(S, D, Attr, false);
4295 break;
4296 case AttributeList::AT_TypeVisibility:
4297 handleVisibilityAttr(S, D, Attr, true);
4298 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004299 case AttributeList::AT_WarnUnused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004300 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr);
4301 break;
4302 case AttributeList::AT_WarnUnusedResult:
4303 handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004304 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004305 case AttributeList::AT_Weak:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004306 handleSimpleAttribute<WeakAttr>(S, D, Attr);
4307 break;
4308 case AttributeList::AT_WeakRef:
4309 handleWeakRefAttr(S, D, Attr);
4310 break;
4311 case AttributeList::AT_WeakImport:
4312 handleWeakImportAttr(S, D, Attr);
4313 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004314 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004315 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004316 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004317 case AttributeList::AT_ObjCException:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004318 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr);
4319 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004320 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004321 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004322 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004323 case AttributeList::AT_ObjCNSObject:
4324 handleObjCNSObject(S, D, Attr);
4325 break;
4326 case AttributeList::AT_Blocks:
4327 handleBlocksAttr(S, D, Attr);
4328 break;
4329 case AttributeList::AT_Sentinel:
4330 handleSentinelAttr(S, D, Attr);
4331 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004332 case AttributeList::AT_Const:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004333 handleSimpleAttribute<ConstAttr>(S, D, Attr);
4334 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004335 case AttributeList::AT_Pure:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004336 handleSimpleAttribute<PureAttr>(S, D, Attr);
4337 break;
4338 case AttributeList::AT_Cleanup:
4339 handleCleanupAttr(S, D, Attr);
4340 break;
4341 case AttributeList::AT_NoDebug:
4342 handleNoDebugAttr(S, D, Attr);
4343 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00004344 case AttributeList::AT_NoDuplicate:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004345 handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
4346 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004347 case AttributeList::AT_NoInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004348 handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
4349 break;
4350 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
4351 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr);
4352 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004353 case AttributeList::AT_StdCall:
4354 case AttributeList::AT_CDecl:
4355 case AttributeList::AT_FastCall:
4356 case AttributeList::AT_ThisCall:
4357 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004358 case AttributeList::AT_MSABI:
4359 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004360 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004361 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004362 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004363 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004364 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004365 case AttributeList::AT_OpenCLKernel:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004366 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr);
4367 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004368 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004369 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr);
4370 break;
John McCall8d32c052012-05-22 21:28:12 +00004371
4372 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004373 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004374 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004375 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004376 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004377 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004378 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004379 case AttributeList::AT_MSInheritance:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004380 handleMSInheritanceAttr(S, D, Attr);
4381 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004382 case AttributeList::AT_SelectAny:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004383 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr);
4384 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004385
4386 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004387 case AttributeList::AT_AssertExclusiveLock:
4388 handleAssertExclusiveLockAttr(S, D, Attr);
4389 break;
4390 case AttributeList::AT_AssertSharedLock:
4391 handleAssertSharedLockAttr(S, D, Attr);
4392 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004393 case AttributeList::AT_GuardedVar:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004394 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr);
4395 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004396 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004397 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004398 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004399 case AttributeList::AT_ScopedLockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004400 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr);
4401 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004402 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004403 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004404 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004405 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004406 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004407 break;
4408 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004409 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004410 break;
4411 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004412 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004413 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004414 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004415 handleGuardedByAttr(S, D, Attr);
4416 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004417 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004418 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004419 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004420 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004421 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004422 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004423 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004424 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004425 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004426 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004427 handleLockReturnedAttr(S, D, Attr);
4428 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004429 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004430 handleLocksExcludedAttr(S, D, Attr);
4431 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004432 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004433 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004434 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004435 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004436 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004437 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004438 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004439 handleUnlockFunAttr(S, D, Attr);
4440 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004441 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004442 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004443 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004444 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004445 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004446 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004447
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004448 // Capability analysis attributes.
4449 case AttributeList::AT_Capability:
4450 case AttributeList::AT_Lockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004451 handleCapabilityAttr(S, D, Attr);
4452 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004453 case AttributeList::AT_RequiresCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004454 handleRequiresCapabilityAttr(S, D, Attr);
4455 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004456
4457 case AttributeList::AT_AssertCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004458 handleAssertCapabilityAttr(S, D, Attr);
4459 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004460 case AttributeList::AT_AcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004461 handleAcquireCapabilityAttr(S, D, Attr);
4462 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004463 case AttributeList::AT_ReleaseCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004464 handleReleaseCapabilityAttr(S, D, Attr);
4465 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004466 case AttributeList::AT_TryAcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004467 handleTryAcquireCapabilityAttr(S, D, Attr);
4468 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004469
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004470 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004471 case AttributeList::AT_Consumable:
4472 handleConsumableAttr(S, D, Attr);
4473 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004474 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004475 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr);
4476 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004477 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004478 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr);
4479 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004480 case AttributeList::AT_CallableWhen:
4481 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004482 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004483 case AttributeList::AT_ParamTypestate:
4484 handleParamTypestateAttr(S, D, Attr);
4485 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004486 case AttributeList::AT_ReturnTypestate:
4487 handleReturnTypestateAttr(S, D, Attr);
4488 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004489 case AttributeList::AT_SetTypestate:
4490 handleSetTypestateAttr(S, D, Attr);
4491 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004492 case AttributeList::AT_TestTypestate:
4493 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004494 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004495
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004496 // Type safety attributes.
4497 case AttributeList::AT_ArgumentWithTypeTag:
4498 handleArgumentWithTypeTagAttr(S, D, Attr);
4499 break;
4500 case AttributeList::AT_TypeTagForDatatype:
4501 handleTypeTagForDatatypeAttr(S, D, Attr);
4502 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004503 }
4504}
4505
4506/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4507/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004508void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004509 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004510 bool IncludeCXX11Attributes) {
4511 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004512 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004513
Joey Gouly2cd9db12013-12-13 16:15:28 +00004514 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004515 // GCC accepts
4516 // static int a9 __attribute__((weakref));
4517 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004518 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00004519 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
4520 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00004521 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004522 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004523 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004524
4525 if (!D->hasAttr<OpenCLKernelAttr>()) {
4526 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004527 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
4528 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004529 D->setInvalidDecl();
4530 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004531 if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
4532 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004533 D->setInvalidDecl();
4534 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004535 if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
4536 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004537 D->setInvalidDecl();
4538 }
4539 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004540}
4541
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004542// Annotation attributes are the only attributes allowed after an access
4543// specifier.
4544bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4545 const AttributeList *AttrList) {
4546 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004547 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004548 handleAnnotateAttr(*this, ASDecl, *l);
4549 } else {
4550 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4551 return true;
4552 }
4553 }
4554
4555 return false;
4556}
4557
John McCall42856de2011-10-01 05:17:03 +00004558/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4559/// contains any decl attributes that we should warn about.
4560static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4561 for ( ; A; A = A->getNext()) {
4562 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004563 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004564 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4565
4566 if (A->getKind() == AttributeList::UnknownAttribute) {
4567 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4568 << A->getName() << A->getRange();
4569 } else {
4570 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4571 << A->getName() << A->getRange();
4572 }
4573 }
4574}
4575
4576/// checkUnusedDeclAttributes - Given a declarator which is not being
4577/// used to build a declaration, complain about any decl attributes
4578/// which might be lying around on it.
4579void Sema::checkUnusedDeclAttributes(Declarator &D) {
4580 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4581 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4582 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4583 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4584}
4585
Ryan Flynn7d470f32009-07-30 03:15:39 +00004586/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004587/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004588NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4589 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004590 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004591 NamedDecl *NewD = 0;
4592 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004593 FunctionDecl *NewFD;
4594 // FIXME: Missing call to CheckFunctionDeclaration().
4595 // FIXME: Mangling?
4596 // FIXME: Is the qualifier info correct?
4597 // FIXME: Is the DeclContext correct?
4598 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4599 Loc, Loc, DeclarationName(II),
4600 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004601 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004602 FD->hasPrototype(),
4603 false/*isConstexprSpecified*/);
4604 NewD = NewFD;
4605
4606 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004607 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004608
4609 // Fake up parameter variables; they are declared as if this were
4610 // a typedef.
4611 QualType FDTy = FD->getType();
4612 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4613 SmallVector<ParmVarDecl*, 16> Params;
Alp Toker9cacbab2014-01-20 20:26:09 +00004614 for (FunctionProtoType::param_type_iterator AI = FT->param_type_begin(),
4615 AE = FT->param_type_end();
4616 AI != AE; ++AI) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004617 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4618 Param->setScopeInfo(0, Params.size());
4619 Params.push_back(Param);
4620 }
David Blaikie9c70e042011-09-21 18:16:56 +00004621 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004622 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004623 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4624 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004625 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004626 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004627 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004628 if (VD->getQualifier()) {
4629 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004630 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004631 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004632 }
4633 return NewD;
4634}
4635
James Dennett634962f2012-06-14 21:40:34 +00004636/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004637/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004638void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004639 if (W.getUsed()) return; // only do this once
4640 W.setUsed(true);
4641 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4642 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004643 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00004644 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
4645 W.getLocation()));
4646 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00004647 WeakTopLevelDecl.push_back(NewD);
4648 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4649 // to insert Decl at TU scope, sorry.
4650 DeclContext *SavedContext = CurContext;
4651 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00004652 NewD->setDeclContext(CurContext);
4653 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00004654 PushOnScopeChains(NewD, S);
4655 CurContext = SavedContext;
4656 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00004657 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004658 }
4659}
4660
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004661void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4662 // It's valid to "forward-declare" #pragma weak, in which case we
4663 // have to do this.
4664 LoadExternalWeakUndeclaredIdentifiers();
4665 if (!WeakUndeclaredIdentifiers.empty()) {
4666 NamedDecl *ND = NULL;
4667 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4668 if (VD->isExternC())
4669 ND = VD;
4670 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4671 if (FD->isExternC())
4672 ND = FD;
4673 if (ND) {
4674 if (IdentifierInfo *Id = ND->getIdentifier()) {
4675 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4676 = WeakUndeclaredIdentifiers.find(Id);
4677 if (I != WeakUndeclaredIdentifiers.end()) {
4678 WeakInfo W = I->second;
4679 DeclApplyPragmaWeak(S, ND, W);
4680 WeakUndeclaredIdentifiers[Id] = W;
4681 }
4682 }
4683 }
4684 }
4685}
4686
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004687/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4688/// it, apply them to D. This is a bit tricky because PD can have attributes
4689/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004690void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004691 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004692 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004693 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004694
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004695 // Walk the declarator structure, applying decl attributes that were in a type
4696 // position to the decl itself. This handles cases like:
4697 // int *__attr__(x)** D;
4698 // when X is a decl attribute.
4699 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4700 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004701 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004702
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004703 // Finally, apply any attributes on the decl itself.
4704 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004705 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004706}
John McCall28a6aea2009-11-04 02:18:39 +00004707
John McCall31168b02011-06-15 23:02:42 +00004708/// Is the given declaration allowed to use a forbidden type?
4709static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4710 // Private ivars are always okay. Unfortunately, people don't
4711 // always properly make their ivars private, even in system headers.
4712 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004713 // Function declarations in sys headers will be marked unavailable.
4714 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4715 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004716 return false;
4717
4718 // Require it to be declared in a system header.
4719 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4720}
4721
4722/// Handle a delayed forbidden-type diagnostic.
4723static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4724 Decl *decl) {
4725 if (decl && isForbiddenTypeAllowed(S, decl)) {
Aaron Ballman36a53502014-01-16 13:03:14 +00004726 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context,
4727 "this system declaration uses an unsupported type",
4728 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00004729 return;
4730 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004731 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004732 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004733 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004734 // kind of forbidden type messages on unavailable functions.
4735 if (FD->hasAttr<UnavailableAttr>() &&
4736 diag.getForbiddenTypeDiagnostic() ==
4737 diag::err_arc_array_param_no_ownership) {
4738 diag.Triggered = true;
4739 return;
4740 }
4741 }
John McCall31168b02011-06-15 23:02:42 +00004742
4743 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4744 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4745 diag.Triggered = true;
4746}
4747
John McCall2ec85372012-05-07 06:16:41 +00004748void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4749 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004750 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004751 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004752
John McCall2ec85372012-05-07 06:16:41 +00004753 // When delaying diagnostics to run in the context of a parsed
4754 // declaration, we only want to actually emit anything if parsing
4755 // succeeds.
4756 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004757
John McCall2ec85372012-05-07 06:16:41 +00004758 // We emit all the active diagnostics in this pool or any of its
4759 // parents. In general, we'll get one pool for the decl spec
4760 // and a child pool for each declarator; in a decl group like:
4761 // deprecated_typedef foo, *bar, baz();
4762 // only the declarator pops will be passed decls. This is correct;
4763 // we really do need to consider delayed diagnostics from the decl spec
4764 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004765 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004766 do {
John McCall6347b682012-05-07 06:16:58 +00004767 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004768 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4769 // This const_cast is a bit lame. Really, Triggered should be mutable.
4770 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004771 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004772 continue;
4773
John McCallc1465822011-02-14 07:13:47 +00004774 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004775 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004776 case DelayedDiagnostic::Unavailable:
4777 // Don't bother giving deprecation/unavailable diagnostics if
4778 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004779 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004780 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004781 break;
4782
4783 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004784 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004785 break;
John McCall31168b02011-06-15 23:02:42 +00004786
4787 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004788 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004789 break;
John McCall86121512010-01-27 03:50:35 +00004790 }
4791 }
John McCall2ec85372012-05-07 06:16:41 +00004792 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004793}
4794
John McCall6347b682012-05-07 06:16:58 +00004795/// Given a set of delayed diagnostics, re-emit them as if they had
4796/// been delayed in the current context instead of in the given pool.
4797/// Essentially, this just moves them to the current pool.
4798void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4799 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4800 assert(curPool && "re-emitting in undelayed context not supported");
4801 curPool->steal(pool);
4802}
4803
John McCall28a6aea2009-11-04 02:18:39 +00004804static bool isDeclDeprecated(Decl *D) {
4805 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004806 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004807 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004808 // A category implicitly has the availability of the interface.
4809 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4810 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004811 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4812 return false;
4813}
4814
Ted Kremenekb79ee572013-12-18 23:30:06 +00004815static bool isDeclUnavailable(Decl *D) {
4816 do {
4817 if (D->isUnavailable())
4818 return true;
4819 // A category implicitly has the availability of the interface.
4820 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4821 return CatD->getClassInterface()->isUnavailable();
4822 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4823 return false;
4824}
4825
Eli Friedman971bfa12012-08-08 21:52:41 +00004826static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004827DoEmitAvailabilityWarning(Sema &S,
4828 DelayedDiagnostic::DDKind K,
4829 Decl *Ctx,
4830 const NamedDecl *D,
4831 StringRef Message,
4832 SourceLocation Loc,
4833 const ObjCInterfaceDecl *UnknownObjCClass,
4834 const ObjCPropertyDecl *ObjCProperty) {
4835
4836 // Diagnostics for deprecated or unavailable.
4837 unsigned diag, diag_message, diag_fwdclass_message;
4838
4839 // Matches 'diag::note_property_attribute' options.
4840 unsigned property_note_select;
4841
4842 // Matches diag::note_availability_specified_here.
4843 unsigned available_here_select_kind;
4844
4845 // Don't warn if our current context is deprecated or unavailable.
4846 switch (K) {
4847 case DelayedDiagnostic::Deprecation:
4848 if (isDeclDeprecated(Ctx))
4849 return;
4850 diag = diag::warn_deprecated;
4851 diag_message = diag::warn_deprecated_message;
4852 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4853 property_note_select = /* deprecated */ 0;
4854 available_here_select_kind = /* deprecated */ 2;
4855 break;
4856
4857 case DelayedDiagnostic::Unavailable:
4858 if (isDeclUnavailable(Ctx))
4859 return;
4860 diag = diag::err_unavailable;
4861 diag_message = diag::err_unavailable_message;
4862 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4863 property_note_select = /* unavailable */ 1;
4864 available_here_select_kind = /* unavailable */ 0;
4865 break;
4866
4867 default:
4868 llvm_unreachable("Neither a deprecation or unavailable kind");
4869 }
4870
Eli Friedman971bfa12012-08-08 21:52:41 +00004871 DeclarationName Name = D->getDeclName();
4872 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004873 S.Diag(Loc, diag_message) << Name << Message;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004874 if (ObjCProperty)
4875 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4876 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004877 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004878 S.Diag(Loc, diag) << Name;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004879 if (ObjCProperty)
4880 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4881 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004882 } else {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004883 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004884 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4885 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004886
4887 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4888 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004889}
4890
Ted Kremenekb79ee572013-12-18 23:30:06 +00004891void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4892 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004893 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004894 DoEmitAvailabilityWarning(*this,
4895 (DelayedDiagnostic::DDKind) DD.Kind,
4896 Ctx,
4897 DD.getDeprecationDecl(),
4898 DD.getDeprecationMessage(),
4899 DD.Loc,
4900 DD.getUnknownObjCClass(),
4901 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004902}
4903
Ted Kremenekb79ee572013-12-18 23:30:06 +00004904void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4905 NamedDecl *D, StringRef Message,
4906 SourceLocation Loc,
4907 const ObjCInterfaceDecl *UnknownObjCClass,
4908 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004909 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004910 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004911 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4912 UnknownObjCClass,
4913 ObjCProperty,
4914 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004915 return;
4916 }
4917
Ted Kremenekb79ee572013-12-18 23:30:06 +00004918 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4919 DelayedDiagnostic::DDKind K;
4920 switch (AD) {
4921 case AD_Deprecation:
4922 K = DelayedDiagnostic::Deprecation;
4923 break;
4924 case AD_Unavailable:
4925 K = DelayedDiagnostic::Unavailable;
4926 break;
4927 }
4928
4929 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
4930 UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004931}