blob: 5f607834b7052f139b0c91d640bd27fbaa36b34c [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
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000366static bool checkRecordTypeForCapability(Sema &S, const AttributeList &Attr,
367 QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000368 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000369
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000370 if (!RT)
371 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000372
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000373 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000374 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000375 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000376
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000377 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000378 // FIXME -- Check the type that the smart pointer points to.
379 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000380 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000381
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000382 // Check if the record itself has a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000383 RecordDecl *RD = RT->getDecl();
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000384 if (RD->hasAttr<CapabilityAttr>())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000385 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000386
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000387 // Else check if any base classes have a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000388 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
389 CXXBasePaths BPaths(false, false);
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000390 if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &P,
391 void *) {
392 return BS->getType()->getAs<RecordType>()
393 ->getDecl()->hasAttr<CapabilityAttr>();
394 }, 0, BPaths))
395 return true;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000396 }
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000397 return false;
398}
399
400static bool checkTypedefTypeForCapability(Sema &S, const AttributeList &Attr,
401 QualType Ty) {
402 const auto *TD = Ty->getAs<TypedefType>();
403 if (!TD)
404 return false;
405
406 TypedefNameDecl *TN = TD->getDecl();
407 if (!TN)
408 return false;
409
410 return TN->hasAttr<CapabilityAttr>();
411}
412
413/// \brief Checks that the passed in type is qualified as a capability. This
414/// type can either be a struct, or a typedef to a built-in type (such as int).
415static void checkForCapability(Sema &S, const AttributeList &Attr,
416 QualType Ty) {
417 if (checkTypedefTypeForCapability(S, Attr, Ty))
418 return;
419
420 if (checkRecordTypeForCapability(S, Attr, Ty))
421 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000422
423 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
Aaron Ballman1da282a2014-01-02 23:15:58 +0000424 << Attr.getName() << Ty;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000425}
426
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000427/// \brief Checks that all attribute arguments, starting from Sidx, resolve to
428/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000429/// \param Sidx The attribute argument index to start checking with.
430/// \param ParamIdxOk Whether an argument can be indexing into a function
431/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000432static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
433 const AttributeList &Attr,
434 SmallVectorImpl<Expr *> &Args,
435 int Sidx = 0,
436 bool ParamIdxOk = false) {
437 for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000438 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000439
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000440 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000441 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000442 Args.push_back(ArgExp);
443 continue;
444 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000445
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000446 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000447 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000448 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000449 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000450 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000451 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000452 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000453 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000454
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000455 // We allow constant strings to be used as a placeholder for expressions
456 // that are not valid C++ syntax, but warn that they are ignored.
457 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
458 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000459 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000460 continue;
461 }
462
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000463 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000464
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000465 // A pointer to member expression of the form &MyClass::mu is treated
466 // specially -- we need to look at the type of the member.
467 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
468 if (UOp->getOpcode() == UO_AddrOf)
469 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
470 if (DRE->getDecl()->isCXXInstanceMember())
471 ArgTy = DRE->getDecl()->getType();
472
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000473 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000474 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000475
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000476 // Now check if we index into a record type function param.
477 if(!RT && ParamIdxOk) {
478 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000479 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
480 if(FD && IL) {
481 unsigned int NumParams = FD->getNumParams();
482 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000483 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
484 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
485 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000486 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
487 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000488 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000489 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000490 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000491 }
492 }
493
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000494 checkForCapability(S, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000495
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000496 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000497 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000498}
499
Chris Lattner58418ff2008-06-29 00:16:31 +0000500//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000501// Attribute Implementations
502//===----------------------------------------------------------------------===//
503
Daniel Dunbar032db472008-07-31 22:40:48 +0000504// FIXME: All this manual attribute parsing code is gross. At the
505// least add some helper functions to check most argument patterns (#
506// and types of args).
507
Michael Hana9171bc2012-08-03 17:40:43 +0000508static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000509 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000510 if (!threadSafetyCheckIsPointer(S, D, Attr))
511 return;
512
Michael Han99315932013-01-24 16:46:58 +0000513 D->addAttr(::new (S.Context)
514 PtGuardedVarAttr(Attr.getRange(), S.Context,
515 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000516}
517
Michael Hana9171bc2012-08-03 17:40:43 +0000518static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
519 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000520 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000521 SmallVector<Expr*, 1> Args;
522 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000523 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000524 unsigned Size = Args.size();
525 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000526 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000527
Michael Han3be3b442012-07-23 18:48:41 +0000528 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000529
Michael Han3be3b442012-07-23 18:48:41 +0000530 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000531}
532
Michael Han3be3b442012-07-23 18:48:41 +0000533static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
534 Expr *Arg = 0;
535 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
536 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000537
Aaron Ballman36a53502014-01-16 13:03:14 +0000538 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg,
539 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000540}
541
Michael Hana9171bc2012-08-03 17:40:43 +0000542static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000543 const AttributeList &Attr) {
544 Expr *Arg = 0;
545 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
546 return;
547
548 if (!threadSafetyCheckIsPointer(S, D, Attr))
549 return;
550
551 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +0000552 S.Context, Arg,
553 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000554}
555
Michael Hana9171bc2012-08-03 17:40:43 +0000556static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
557 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000558 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000559 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000560 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000561
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000562 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000563 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000564 if (!QT->isDependentType()) {
565 const RecordType *RT = getRecordType(QT);
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000566 if (!RT || !RT->getDecl()->hasAttr<CapabilityAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000567 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000568 << Attr.getName();
569 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000570 }
571 }
572
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000573 // Check that all arguments are lockable objects.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000574 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000575 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000576 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000577
Michael Han3be3b442012-07-23 18:48:41 +0000578 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000579}
580
Michael Hana9171bc2012-08-03 17:40:43 +0000581static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000582 const AttributeList &Attr) {
583 SmallVector<Expr*, 1> Args;
584 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
585 return;
586
587 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000588 D->addAttr(::new (S.Context)
589 AcquiredAfterAttr(Attr.getRange(), S.Context,
590 StartArg, Args.size(),
591 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000592}
593
Michael Hana9171bc2012-08-03 17:40:43 +0000594static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000595 const AttributeList &Attr) {
596 SmallVector<Expr*, 1> Args;
597 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
598 return;
599
600 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000601 D->addAttr(::new (S.Context)
602 AcquiredBeforeAttr(Attr.getRange(), S.Context,
603 StartArg, Args.size(),
604 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000605}
606
Michael Hana9171bc2012-08-03 17:40:43 +0000607static bool checkLockFunAttrCommon(Sema &S, Decl *D,
608 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000609 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000610 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000611 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000612 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000613
Michael Han3be3b442012-07-23 18:48:41 +0000614 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000615}
616
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000617static void handleAssertSharedLockAttr(Sema &S, Decl *D,
618 const AttributeList &Attr) {
619 SmallVector<Expr*, 1> Args;
620 if (!checkLockFunAttrCommon(S, D, Attr, Args))
621 return;
622
623 unsigned Size = Args.size();
624 Expr **StartArg = Size == 0 ? 0 : &Args[0];
625 D->addAttr(::new (S.Context)
626 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
627 Attr.getAttributeSpellingListIndex()));
628}
629
630static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
631 const AttributeList &Attr) {
632 SmallVector<Expr*, 1> Args;
633 if (!checkLockFunAttrCommon(S, D, Attr, Args))
634 return;
635
636 unsigned Size = Args.size();
637 Expr **StartArg = Size == 0 ? 0 : &Args[0];
638 D->addAttr(::new (S.Context)
639 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
640 StartArg, Size,
641 Attr.getAttributeSpellingListIndex()));
642}
643
644
Michael Hana9171bc2012-08-03 17:40:43 +0000645static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
646 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000647 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000648 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000649 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000650
Aaron Ballman00e99962013-08-31 01:11:41 +0000651 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000652 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000653 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000654 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000655 }
656
657 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000658 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000659
Michael Han3be3b442012-07-23 18:48:41 +0000660 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000661}
662
Michael Hana9171bc2012-08-03 17:40:43 +0000663static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000664 const AttributeList &Attr) {
665 SmallVector<Expr*, 2> Args;
666 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
667 return;
668
Michael Han99315932013-01-24 16:46:58 +0000669 D->addAttr(::new (S.Context)
670 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000671 Attr.getArgAsExpr(0),
672 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000673 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000674}
675
Michael Hana9171bc2012-08-03 17:40:43 +0000676static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000677 const AttributeList &Attr) {
678 SmallVector<Expr*, 2> Args;
679 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
680 return;
681
Michael Han99315932013-01-24 16:46:58 +0000682 D->addAttr(::new (S.Context)
683 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000684 Attr.getArgAsExpr(0),
685 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000686 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000687}
688
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000689static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000690 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000691 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000692 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000693 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000694 unsigned Size = Args.size();
695 if (Size == 0)
696 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000697
Michael Han99315932013-01-24 16:46:58 +0000698 D->addAttr(::new (S.Context)
699 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
700 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000701}
702
703static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000704 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000705 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000706 return;
707
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000708 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000709 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000710 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000711 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000712 if (Size == 0)
713 return;
714 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000715
Michael Han99315932013-01-24 16:46:58 +0000716 D->addAttr(::new (S.Context)
717 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
718 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000719}
720
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000721static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
722 Expr *Cond = Attr.getArgAsExpr(0);
723 if (!Cond->isTypeDependent()) {
724 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
725 if (Converted.isInvalid())
726 return;
727 Cond = Converted.take();
728 }
729
730 StringRef Msg;
731 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
732 return;
733
734 SmallVector<PartialDiagnosticAt, 8> Diags;
735 if (!Cond->isValueDependent() &&
736 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
737 Diags)) {
738 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
739 for (int I = 0, N = Diags.size(); I != N; ++I)
740 S.Diag(Diags[I].first, Diags[I].second);
741 return;
742 }
743
744 D->addAttr(::new (S.Context)
745 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
746 Attr.getAttributeSpellingListIndex()));
747}
748
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000749static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000750 ConsumableAttr::ConsumedState DefaultState;
751
752 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000753 IdentifierLoc *IL = Attr.getArgAsIdent(0);
754 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
755 DefaultState)) {
756 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
757 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000758 return;
759 }
David Blaikie16f76d22013-09-06 01:28:43 +0000760 } else {
761 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
762 << Attr.getName() << AANT_ArgumentIdentifier;
763 return;
764 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000765
766 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000767 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000768 Attr.getAttributeSpellingListIndex()));
769}
770
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000771
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000772static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
773 const AttributeList &Attr) {
774 ASTContext &CurrContext = S.getASTContext();
775 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
776
777 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
778 if (!RD->hasAttr<ConsumableAttr>()) {
779 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
780 RD->getNameAsString();
781
782 return false;
783 }
784 }
785
786 return true;
787}
788
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000789
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000790static void handleCallableWhenAttr(Sema &S, Decl *D,
791 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000792 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
793 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000794
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000795 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
796 return;
797
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000798 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
799 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
800 CallableWhenAttr::ConsumedState CallableState;
801
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000802 StringRef StateString;
803 SourceLocation Loc;
804 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
805 return;
806
807 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000808 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000809 S.Diag(Loc, diag::warn_attribute_type_not_supported)
810 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000811 return;
812 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000813
814 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000815 }
816
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000817 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000818 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
819 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000820}
821
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000822
DeLesley Hutchins69391772013-10-17 23:23:53 +0000823static void handleParamTypestateAttr(Sema &S, Decl *D,
824 const AttributeList &Attr) {
825 if (!checkAttributeNumArgs(S, Attr, 1)) return;
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000826
DeLesley Hutchins69391772013-10-17 23:23:53 +0000827 ParamTypestateAttr::ConsumedState ParamState;
828
829 if (Attr.isArgIdent(0)) {
830 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
831 StringRef StateString = Ident->Ident->getName();
832
833 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
834 ParamState)) {
835 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
836 << Attr.getName() << StateString;
837 return;
838 }
839 } else {
840 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
841 Attr.getName() << AANT_ArgumentIdentifier;
842 return;
843 }
844
845 // FIXME: This check is currently being done in the analysis. It can be
846 // enabled here only after the parser propagates attributes at
847 // template specialization definition, not declaration.
848 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
849 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
850 //
851 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
852 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
853 // ReturnType.getAsString();
854 // return;
855 //}
856
857 D->addAttr(::new (S.Context)
858 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
859 Attr.getAttributeSpellingListIndex()));
860}
861
862
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000863static void handleReturnTypestateAttr(Sema &S, Decl *D,
864 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000865 if (!checkAttributeNumArgs(S, Attr, 1)) return;
866
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000867 ReturnTypestateAttr::ConsumedState ReturnState;
868
869 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000870 IdentifierLoc *IL = Attr.getArgAsIdent(0);
871 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
872 ReturnState)) {
873 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
874 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000875 return;
876 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000877 } else {
878 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
879 Attr.getName() << AANT_ArgumentIdentifier;
880 return;
881 }
882
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000883 // FIXME: This check is currently being done in the analysis. It can be
884 // enabled here only after the parser propagates attributes at
885 // template specialization definition, not declaration.
886 //QualType ReturnType;
887 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000888 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
889 // ReturnType = Param->getType();
890 //
891 //} else if (const CXXConstructorDecl *Constructor =
892 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000893 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
894 //
895 //} else {
896 //
897 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
898 //}
899 //
900 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
901 //
902 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
903 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
904 // ReturnType.getAsString();
905 // return;
906 //}
907
908 D->addAttr(::new (S.Context)
909 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
910 Attr.getAttributeSpellingListIndex()));
911}
912
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000913
914static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000915 if (!checkAttributeNumArgs(S, Attr, 1))
916 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000917
918 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
919 return;
920
921 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000922 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000923 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
924 StringRef Param = Ident->Ident->getName();
925 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
926 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
927 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000928 return;
929 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000930 } else {
931 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
932 Attr.getName() << AANT_ArgumentIdentifier;
933 return;
934 }
935
936 D->addAttr(::new (S.Context)
937 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
938 Attr.getAttributeSpellingListIndex()));
939}
940
Chris Wailes9385f9f2013-10-29 20:28:41 +0000941static void handleTestTypestateAttr(Sema &S, Decl *D,
942 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000943 if (!checkAttributeNumArgs(S, Attr, 1))
944 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000945
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000946 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
947 return;
948
Chris Wailes9385f9f2013-10-29 20:28:41 +0000949 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000950 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000951 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
952 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +0000953 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000954 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
955 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000956 return;
957 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000958 } else {
959 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
960 Attr.getName() << AANT_ArgumentIdentifier;
961 return;
962 }
963
964 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +0000965 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000966 Attr.getAttributeSpellingListIndex()));
967}
968
Chandler Carruthedc2c642011-07-02 00:01:44 +0000969static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
970 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +0000971 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000972 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000973}
974
Chandler Carruthedc2c642011-07-02 00:01:44 +0000975static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000976 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Aaron Ballman36a53502014-01-16 13:03:14 +0000977 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context,
978 Attr.getAttributeSpellingListIndex()));
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000979 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000980 // If the alignment is less than or equal to 8 bits, the packed attribute
981 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +0000982 if (!FD->getType()->isDependentType() &&
983 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000984 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000985 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000986 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000987 else
Michael Han99315932013-01-24 16:46:58 +0000988 FD->addAttr(::new (S.Context)
989 PackedAttr(Attr.getRange(), S.Context,
990 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000991 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000992 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000993}
994
Ted Kremenek7fd17232011-09-29 07:02:25 +0000995static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
996 // The IBOutlet/IBOutletCollection attributes only apply to instance
997 // variables or properties of Objective-C classes. The outlet must also
998 // have an object reference type.
999 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1000 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001001 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001002 << Attr.getName() << VD->getType() << 0;
1003 return false;
1004 }
1005 }
1006 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1007 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001008 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001009 << Attr.getName() << PD->getType() << 1;
1010 return false;
1011 }
1012 }
1013 else {
1014 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1015 return false;
1016 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001017
Ted Kremenek7fd17232011-09-29 07:02:25 +00001018 return true;
1019}
1020
Chandler Carruthedc2c642011-07-02 00:01:44 +00001021static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001022 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001023 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001024
Michael Han99315932013-01-24 16:46:58 +00001025 D->addAttr(::new (S.Context)
1026 IBOutletAttr(Attr.getRange(), S.Context,
1027 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001028}
1029
Chandler Carruthedc2c642011-07-02 00:01:44 +00001030static void handleIBOutletCollection(Sema &S, Decl *D,
1031 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001032
1033 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001034 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001035 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1036 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001037 return;
1038 }
1039
Ted Kremenek7fd17232011-09-29 07:02:25 +00001040 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001041 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001042
Richard Smithb1f9a282013-10-31 01:56:18 +00001043 ParsedType PT;
1044
1045 if (Attr.hasParsedType())
1046 PT = Attr.getTypeArg();
1047 else {
1048 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1049 S.getScopeForContext(D->getDeclContext()->getParent()));
1050 if (!PT) {
1051 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1052 return;
1053 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001054 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001055
Richard Smithb87c4652013-10-31 21:23:20 +00001056 TypeSourceInfo *QTLoc = 0;
1057 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1058 if (!QTLoc)
1059 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001060
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001061 // Diagnose use of non-object type in iboutletcollection attribute.
1062 // FIXME. Gnu attribute extension ignores use of builtin types in
1063 // attributes. So, __attribute__((iboutletcollection(char))) will be
1064 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001065 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001066 S.Diag(Attr.getLoc(),
1067 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1068 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001069 return;
1070 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001071
Michael Han99315932013-01-24 16:46:58 +00001072 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001073 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001074 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001075}
1076
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001077static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001078 if (const RecordType *UT = T->getAsUnionType())
1079 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1080 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001081 for (const auto *I : UD->fields()) {
1082 QualType QT = I->getType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001083 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1084 T = QT;
1085 return;
1086 }
1087 }
1088 }
1089}
1090
Ted Kremenek9aedc152014-01-17 06:24:56 +00001091static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001092 SourceRange R, bool isReturnValue = false) {
Ted Kremenek9aedc152014-01-17 06:24:56 +00001093 T = T.getNonReferenceType();
1094 possibleTransparentUnionPointerType(T);
1095
1096 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001097 S.Diag(Attr.getLoc(),
1098 isReturnValue ? diag::warn_attribute_return_pointers_only
1099 : diag::warn_attribute_pointers_only)
Ted Kremenek9aedc152014-01-17 06:24:56 +00001100 << Attr.getName() << R;
1101 return false;
1102 }
1103 return true;
1104}
1105
Chandler Carruthedc2c642011-07-02 00:01:44 +00001106static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001107 SmallVector<unsigned, 8> NonNullArgs;
1108 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001109 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001110 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001111 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001112 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001113
1114 // Is the function argument a pointer type?
Ted Kremenek9aedc152014-01-17 06:24:56 +00001115 // FIXME: Should also highlight argument in decl in the diagnostic.
Alp Toker601b22c2014-01-21 23:35:24 +00001116 if (!attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr,
1117 Ex->getSourceRange()))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001118 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001119
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001120 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001121 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001122
1123 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1124 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001125 if (NonNullArgs.empty()) {
Alp Toker601b22c2014-01-21 23:35:24 +00001126 for (unsigned i = 0, e = getFunctionOrMethodNumParams(D); i != e; ++i) {
1127 QualType T = getFunctionOrMethodParamType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001128 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001129 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001130 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001131 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001132
Ted Kremenek22813f42010-10-21 18:49:36 +00001133 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001134 if (NonNullArgs.empty()) {
1135 // Warn the trivial case only if attribute is not coming from a
1136 // macro instantiation.
1137 if (Attr.getLoc().isFileID())
1138 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001139 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001140 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001141 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001142
Nick Lewyckye1121512013-01-24 01:12:16 +00001143 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001144 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001145 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001146 D->addAttr(::new (S.Context)
1147 NonNullAttr(Attr.getRange(), S.Context, start, size,
1148 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001149}
1150
Jordan Rosec9399072014-02-11 17:27:59 +00001151static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1152 const AttributeList &Attr) {
1153 if (Attr.getNumArgs() > 0) {
1154 if (D->getFunctionType()) {
1155 handleNonNullAttr(S, D, Attr);
1156 } else {
1157 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1158 << D->getSourceRange();
1159 }
1160 return;
1161 }
1162
1163 // Is the argument a pointer type?
1164 if (!attrNonNullArgCheck(S, D->getType(), Attr, D->getSourceRange()))
1165 return;
1166
1167 D->addAttr(::new (S.Context)
1168 NonNullAttr(Attr.getRange(), S.Context, 0, 0,
1169 Attr.getAttributeSpellingListIndex()));
1170}
1171
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001172static void handleReturnsNonNullAttr(Sema &S, Decl *D,
1173 const AttributeList &Attr) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001174 QualType ResultType = getFunctionOrMethodResultType(D);
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001175 if (!attrNonNullArgCheck(S, ResultType, Attr, Attr.getRange(),
1176 /* isReturnValue */ true))
1177 return;
1178
1179 D->addAttr(::new (S.Context)
1180 ReturnsNonNullAttr(Attr.getRange(), S.Context,
1181 Attr.getAttributeSpellingListIndex()));
1182}
1183
Chandler Carruthedc2c642011-07-02 00:01:44 +00001184static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001185 // This attribute must be applied to a function declaration. The first
1186 // argument to the attribute must be an identifier, the name of the resource,
1187 // for example: malloc. The following arguments must be argument indexes, the
1188 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001189 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001190 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001191 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001192
Aaron Ballman00e99962013-08-31 01:11:41 +00001193 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001194 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001195 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001196 return;
1197 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001198
Richard Smith852e9ce2013-11-27 01:46:48 +00001199 // Figure out our Kind.
1200 OwnershipAttr::OwnershipKind K =
1201 OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0,
1202 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001203
Richard Smith852e9ce2013-11-27 01:46:48 +00001204 // Check arguments.
1205 switch (K) {
1206 case OwnershipAttr::Takes:
1207 case OwnershipAttr::Holds:
1208 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001209 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1210 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001211 return;
1212 }
1213 break;
1214 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001215 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001216 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1217 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001218 return;
1219 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001220 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001221 }
1222
Richard Smith852e9ce2013-11-27 01:46:48 +00001223 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001224
1225 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001226 StringRef ModuleName = Module->getName();
1227 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1228 ModuleName.size() > 4) {
1229 ModuleName = ModuleName.drop_front(2).drop_back(2);
1230 Module = &S.PP.getIdentifierTable().get(ModuleName);
1231 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001232
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001233 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001234 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1235 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001236 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001237 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001238 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001239
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001240 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001241 QualType T = getFunctionOrMethodParamType(D, Idx);
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001242 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001243 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001244 case OwnershipAttr::Takes:
1245 case OwnershipAttr::Holds:
1246 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1247 Err = 0;
1248 break;
1249 case OwnershipAttr::Returns:
1250 if (!T->isIntegerType())
1251 Err = 1;
1252 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001253 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001254 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001255 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001256 << Ex->getSourceRange();
1257 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001258 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001259
1260 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001261 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001262 // FIXME: A returns attribute should conflict with any returns attribute
1263 // with a different index too.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001264 if (I->getOwnKind() != K && I->args_end() !=
1265 std::find(I->args_begin(), I->args_end(), Idx)) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001266 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001267 << AL.getName() << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001268 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001269 }
1270 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001271 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001272 }
1273
1274 unsigned* start = OwnershipArgs.data();
1275 unsigned size = OwnershipArgs.size();
1276 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001277
Michael Han99315932013-01-24 16:46:58 +00001278 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001279 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001280 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001281}
1282
Chandler Carruthedc2c642011-07-02 00:01:44 +00001283static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001284 // Check the attribute arguments.
1285 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001286 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1287 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001288 return;
1289 }
1290
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001291 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001292
Rafael Espindolac18086a2010-02-23 22:00:30 +00001293 // gcc rejects
1294 // class c {
1295 // static int a __attribute__((weakref ("v2")));
1296 // static int b() __attribute__((weakref ("f3")));
1297 // };
1298 // and ignores the attributes of
1299 // void f(void) {
1300 // static int a __attribute__((weakref ("v2")));
1301 // }
1302 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001303 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001304 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001305 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1306 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001307 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001308 }
1309
1310 // The GCC manual says
1311 //
1312 // At present, a declaration to which `weakref' is attached can only
1313 // be `static'.
1314 //
1315 // It also says
1316 //
1317 // Without a TARGET,
1318 // given as an argument to `weakref' or to `alias', `weakref' is
1319 // equivalent to `weak'.
1320 //
1321 // gcc 4.4.1 will accept
1322 // int a7 __attribute__((weakref));
1323 // as
1324 // int a7 __attribute__((weak));
1325 // This looks like a bug in gcc. We reject that for now. We should revisit
1326 // it if this behaviour is actually used.
1327
Rafael Espindolac18086a2010-02-23 22:00:30 +00001328 // GCC rejects
1329 // static ((alias ("y"), weakref)).
1330 // Should we? How to check that weakref is before or after alias?
1331
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001332 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1333 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1334 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001335 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001336 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001337 // GCC will accept anything as the argument of weakref. Should we
1338 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001339 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1340 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001341
Michael Han99315932013-01-24 16:46:58 +00001342 D->addAttr(::new (S.Context)
1343 WeakRefAttr(Attr.getRange(), S.Context,
1344 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001345}
1346
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001347static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1348 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001349 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001350 return;
1351
Douglas Gregore8bbc122011-09-02 00:18:52 +00001352 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001353 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1354 return;
1355 }
1356
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001357 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001358
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001359 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001360 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001361}
1362
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001363static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001364 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001365 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001366
Michael Han99315932013-01-24 16:46:58 +00001367 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1368 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001369}
1370
1371static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001372 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001373 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001374
Michael Han99315932013-01-24 16:46:58 +00001375 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1376 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001377}
1378
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001379static void handleTLSModelAttr(Sema &S, Decl *D,
1380 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001381 StringRef Model;
1382 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001383 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001384 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001385 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001386
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001387 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001388 if (Model != "global-dynamic" && Model != "local-dynamic"
1389 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001390 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001391 return;
1392 }
1393
Michael Han99315932013-01-24 16:46:58 +00001394 D->addAttr(::new (S.Context)
1395 TLSModelAttr(Attr.getRange(), S.Context, Model,
1396 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001397}
1398
Chandler Carruthedc2c642011-07-02 00:01:44 +00001399static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001400 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Alp Toker314cc812014-01-25 16:55:45 +00001401 QualType RetTy = FD->getReturnType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001402 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001403 D->addAttr(::new (S.Context)
1404 MallocAttr(Attr.getRange(), S.Context,
1405 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001406 return;
1407 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001408 }
1409
Ted Kremenek08479ae2009-08-15 00:51:46 +00001410 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001411}
1412
Chandler Carruthedc2c642011-07-02 00:01:44 +00001413static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001414 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001415 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1416 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001417 return;
1418 }
1419
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001420 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1421 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001422}
1423
Chandler Carruthedc2c642011-07-02 00:01:44 +00001424static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001425 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001426
1427 if (S.CheckNoReturnAttr(attr)) return;
1428
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001429 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001430 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001431 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001432 return;
1433 }
1434
Michael Han99315932013-01-24 16:46:58 +00001435 D->addAttr(::new (S.Context)
1436 NoReturnAttr(attr.getRange(), S.Context,
1437 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001438}
1439
1440bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001441 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001442 attr.setInvalid();
1443 return true;
1444 }
1445
1446 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001447}
1448
Chandler Carruthedc2c642011-07-02 00:01:44 +00001449static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1450 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001451
1452 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1453 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001454 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1455 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001456 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1457 && !VD->getType()->isFunctionPointerType())) {
1458 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001459 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001460 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001461 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001462 return;
1463 }
1464 }
1465
Michael Han99315932013-01-24 16:46:58 +00001466 D->addAttr(::new (S.Context)
1467 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1468 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001469}
1470
John Thompsoncdb847ba2010-08-09 21:53:52 +00001471// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001472static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001473/*
1474 Returning a Vector Class in Registers
1475
Eric Christopherbc638a82010-12-01 22:13:54 +00001476 According to the PPU ABI specifications, a class with a single member of
1477 vector type is returned in memory when used as the return value of a function.
1478 This results in inefficient code when implementing vector classes. To return
1479 the value in a single vector register, add the vecreturn attribute to the
1480 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001481
1482 Example:
1483
1484 struct Vector
1485 {
1486 __vector float xyzw;
1487 } __attribute__((vecreturn));
1488
1489 Vector Add(Vector lhs, Vector rhs)
1490 {
1491 Vector result;
1492 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1493 return result; // This will be returned in a register
1494 }
1495*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001496 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1497 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001498 return;
1499 }
1500
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001501 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001502 int count = 0;
1503
1504 if (!isa<CXXRecordDecl>(record)) {
1505 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1506 return;
1507 }
1508
1509 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1510 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1511 return;
1512 }
1513
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001514 for (const auto *I : record->fields()) {
1515 if ((count == 1) || !I->getType()->isVectorType()) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001516 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1517 return;
1518 }
1519 count++;
1520 }
1521
Michael Han99315932013-01-24 16:46:58 +00001522 D->addAttr(::new (S.Context)
1523 VecReturnAttr(Attr.getRange(), S.Context,
1524 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001525}
1526
Richard Smithe233fbf2013-01-28 22:42:45 +00001527static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1528 const AttributeList &Attr) {
1529 if (isa<ParmVarDecl>(D)) {
1530 // [[carries_dependency]] can only be applied to a parameter if it is a
1531 // parameter of a function declaration or lambda.
1532 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1533 S.Diag(Attr.getLoc(),
1534 diag::err_carries_dependency_param_not_function_decl);
1535 return;
1536 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001537 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001538
1539 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1540 Attr.getRange(), S.Context,
1541 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001542}
1543
Chandler Carruthedc2c642011-07-02 00:01:44 +00001544static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001545 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001546 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001547 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001548 return;
1549 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001550 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001551 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001552 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001553 return;
1554 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001555
Michael Han99315932013-01-24 16:46:58 +00001556 D->addAttr(::new (S.Context)
1557 UsedAttr(Attr.getRange(), S.Context,
1558 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001559}
1560
Chandler Carruthedc2c642011-07-02 00:01:44 +00001561static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001562 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001563 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001564 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1565 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001566 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001567 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001568
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001569 uint32_t priority = ConstructorAttr::DefaultPriority;
1570 if (Attr.getNumArgs() > 0 &&
1571 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1572 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001573
Michael Han99315932013-01-24 16:46:58 +00001574 D->addAttr(::new (S.Context)
1575 ConstructorAttr(Attr.getRange(), S.Context, priority,
1576 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001577}
1578
Chandler Carruthedc2c642011-07-02 00:01:44 +00001579static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001580 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001581 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001582 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1583 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001584 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001585 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001586
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001587 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001588 if (Attr.getNumArgs() > 0 &&
1589 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1590 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001591
Michael Han99315932013-01-24 16:46:58 +00001592 D->addAttr(::new (S.Context)
1593 DestructorAttr(Attr.getRange(), S.Context, priority,
1594 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001595}
1596
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001597template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001598static void handleAttrWithMessage(Sema &S, Decl *D,
1599 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001600 unsigned NumArgs = Attr.getNumArgs();
1601 if (NumArgs > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001602 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1603 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001604 return;
1605 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001606
1607 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001608 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001609 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001610 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001611
Michael Han99315932013-01-24 16:46:58 +00001612 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1613 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001614}
1615
Ted Kremenek438f8db2014-02-22 01:06:05 +00001616static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Ted Kremenek28eace62013-11-23 01:01:34 +00001617 const AttributeList &Attr) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00001618 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Ted Kremenek27cfe102014-02-21 22:49:04 +00001619 S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition)
1620 << Attr.getName() << Attr.getRange();
1621 return;
1622 }
1623
Ted Kremenek28eace62013-11-23 01:01:34 +00001624 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001625 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1626 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001627}
1628
Jordy Rose740b0c22012-05-08 03:27:22 +00001629static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1630 IdentifierInfo *Platform,
1631 VersionTuple Introduced,
1632 VersionTuple Deprecated,
1633 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001634 StringRef PlatformName
1635 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1636 if (PlatformName.empty())
1637 PlatformName = Platform->getName();
1638
1639 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1640 // of these steps are needed).
1641 if (!Introduced.empty() && !Deprecated.empty() &&
1642 !(Introduced <= Deprecated)) {
1643 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1644 << 1 << PlatformName << Deprecated.getAsString()
1645 << 0 << Introduced.getAsString();
1646 return true;
1647 }
1648
1649 if (!Introduced.empty() && !Obsoleted.empty() &&
1650 !(Introduced <= Obsoleted)) {
1651 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1652 << 2 << PlatformName << Obsoleted.getAsString()
1653 << 0 << Introduced.getAsString();
1654 return true;
1655 }
1656
1657 if (!Deprecated.empty() && !Obsoleted.empty() &&
1658 !(Deprecated <= Obsoleted)) {
1659 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1660 << 2 << PlatformName << Obsoleted.getAsString()
1661 << 1 << Deprecated.getAsString();
1662 return true;
1663 }
1664
1665 return false;
1666}
1667
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001668/// \brief Check whether the two versions match.
1669///
1670/// If either version tuple is empty, then they are assumed to match. If
1671/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1672static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1673 bool BeforeIsOkay) {
1674 if (X.empty() || Y.empty())
1675 return true;
1676
1677 if (X == Y)
1678 return true;
1679
1680 if (BeforeIsOkay && X < Y)
1681 return true;
1682
1683 return false;
1684}
1685
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001686AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001687 IdentifierInfo *Platform,
1688 VersionTuple Introduced,
1689 VersionTuple Deprecated,
1690 VersionTuple Obsoleted,
1691 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001692 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001693 bool Override,
1694 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001695 VersionTuple MergedIntroduced = Introduced;
1696 VersionTuple MergedDeprecated = Deprecated;
1697 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001698 bool FoundAny = false;
1699
Rafael Espindolac67f2232012-05-10 02:50:16 +00001700 if (D->hasAttrs()) {
1701 AttrVec &Attrs = D->getAttrs();
1702 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1703 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1704 if (!OldAA) {
1705 ++i;
1706 continue;
1707 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001708
Rafael Espindolac67f2232012-05-10 02:50:16 +00001709 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1710 if (OldPlatform != Platform) {
1711 ++i;
1712 continue;
1713 }
1714
1715 FoundAny = true;
1716 VersionTuple OldIntroduced = OldAA->getIntroduced();
1717 VersionTuple OldDeprecated = OldAA->getDeprecated();
1718 VersionTuple OldObsoleted = OldAA->getObsoleted();
1719 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001720
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001721 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1722 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1723 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1724 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001725 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001726 if (Override) {
1727 int Which = -1;
1728 VersionTuple FirstVersion;
1729 VersionTuple SecondVersion;
1730 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1731 Which = 0;
1732 FirstVersion = OldIntroduced;
1733 SecondVersion = Introduced;
1734 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1735 Which = 1;
1736 FirstVersion = Deprecated;
1737 SecondVersion = OldDeprecated;
1738 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1739 Which = 2;
1740 FirstVersion = Obsoleted;
1741 SecondVersion = OldObsoleted;
1742 }
1743
1744 if (Which == -1) {
1745 Diag(OldAA->getLocation(),
1746 diag::warn_mismatched_availability_override_unavail)
1747 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1748 } else {
1749 Diag(OldAA->getLocation(),
1750 diag::warn_mismatched_availability_override)
1751 << Which
1752 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1753 << FirstVersion.getAsString() << SecondVersion.getAsString();
1754 }
1755 Diag(Range.getBegin(), diag::note_overridden_method);
1756 } else {
1757 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1758 Diag(Range.getBegin(), diag::note_previous_attribute);
1759 }
1760
Rafael Espindolac67f2232012-05-10 02:50:16 +00001761 Attrs.erase(Attrs.begin() + i);
1762 --e;
1763 continue;
1764 }
1765
1766 VersionTuple MergedIntroduced2 = MergedIntroduced;
1767 VersionTuple MergedDeprecated2 = MergedDeprecated;
1768 VersionTuple MergedObsoleted2 = MergedObsoleted;
1769
1770 if (MergedIntroduced2.empty())
1771 MergedIntroduced2 = OldIntroduced;
1772 if (MergedDeprecated2.empty())
1773 MergedDeprecated2 = OldDeprecated;
1774 if (MergedObsoleted2.empty())
1775 MergedObsoleted2 = OldObsoleted;
1776
1777 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1778 MergedIntroduced2, MergedDeprecated2,
1779 MergedObsoleted2)) {
1780 Attrs.erase(Attrs.begin() + i);
1781 --e;
1782 continue;
1783 }
1784
1785 MergedIntroduced = MergedIntroduced2;
1786 MergedDeprecated = MergedDeprecated2;
1787 MergedObsoleted = MergedObsoleted2;
1788 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001789 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001790 }
1791
1792 if (FoundAny &&
1793 MergedIntroduced == Introduced &&
1794 MergedDeprecated == Deprecated &&
1795 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001796 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001797
Ted Kremenekb5445722013-04-06 00:34:27 +00001798 // Only create a new attribute if !Override, but we want to do
1799 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001800 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001801 MergedDeprecated, MergedObsoleted) &&
1802 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001803 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1804 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001805 Obsoleted, IsUnavailable, Message,
1806 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001807 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001808 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001809}
1810
Chandler Carruthedc2c642011-07-02 00:01:44 +00001811static void handleAvailabilityAttr(Sema &S, Decl *D,
1812 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001813 if (!checkAttributeNumArgs(S, Attr, 1))
1814 return;
1815 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001816 unsigned Index = Attr.getAttributeSpellingListIndex();
1817
Aaron Ballman00e99962013-08-31 01:11:41 +00001818 IdentifierInfo *II = Platform->Ident;
1819 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1820 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1821 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001822
Rafael Espindolac231fab2013-01-08 21:30:32 +00001823 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1824 if (!ND) {
1825 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1826 return;
1827 }
1828
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001829 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1830 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1831 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001832 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001833 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001834 if (const StringLiteral *SE =
1835 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001836 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001837
Aaron Ballman00e99962013-08-31 01:11:41 +00001838 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001839 Introduced.Version,
1840 Deprecated.Version,
1841 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001842 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001843 /*Override=*/false,
1844 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001845 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001846 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001847}
1848
John McCalld041a9b2013-02-20 01:54:26 +00001849template <class T>
1850static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1851 typename T::VisibilityType value,
1852 unsigned attrSpellingListIndex) {
1853 T *existingAttr = D->getAttr<T>();
1854 if (existingAttr) {
1855 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1856 if (existingValue == value)
1857 return NULL;
1858 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1859 S.Diag(range.getBegin(), diag::note_previous_attribute);
1860 D->dropAttr<T>();
1861 }
1862 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1863}
1864
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001865VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001866 VisibilityAttr::VisibilityType Vis,
1867 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001868 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1869 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001870}
1871
John McCalld041a9b2013-02-20 01:54:26 +00001872TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1873 TypeVisibilityAttr::VisibilityType Vis,
1874 unsigned AttrSpellingListIndex) {
1875 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1876 AttrSpellingListIndex);
1877}
1878
1879static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1880 bool isTypeVisibility) {
1881 // Visibility attributes don't mean anything on a typedef.
1882 if (isa<TypedefNameDecl>(D)) {
1883 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
1884 << Attr.getName();
1885 return;
1886 }
1887
1888 // 'type_visibility' can only go on a type or namespace.
1889 if (isTypeVisibility &&
1890 !(isa<TagDecl>(D) ||
1891 isa<ObjCInterfaceDecl>(D) ||
1892 isa<NamespaceDecl>(D))) {
1893 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
1894 << Attr.getName() << ExpectedTypeOrNamespace;
1895 return;
1896 }
1897
Benjamin Kramer70370212013-09-09 15:08:57 +00001898 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001899 StringRef TypeStr;
1900 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001901 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001902 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001903
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001904 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00001905 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001906 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00001907 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001908 return;
1909 }
Aaron Ballman682ee422013-09-11 19:47:58 +00001910
1911 // Complain about attempts to use protected visibility on targets
1912 // (like Darwin) that don't support it.
1913 if (type == VisibilityAttr::Protected &&
1914 !S.Context.getTargetInfo().hasProtectedVisibility()) {
1915 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1916 type = VisibilityAttr::Default;
1917 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001918
Michael Han99315932013-01-24 16:46:58 +00001919 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00001920 clang::Attr *newAttr;
1921 if (isTypeVisibility) {
1922 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
1923 (TypeVisibilityAttr::VisibilityType) type,
1924 Index);
1925 } else {
1926 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
1927 }
1928 if (newAttr)
1929 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001930}
1931
Chandler Carruthedc2c642011-07-02 00:01:44 +00001932static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1933 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001934 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00001935 if (!Attr.isArgIdent(0)) {
1936 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1937 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00001938 return;
1939 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001940
Aaron Ballman682ee422013-09-11 19:47:58 +00001941 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1942 ObjCMethodFamilyAttr::FamilyKind F;
1943 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
1944 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
1945 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00001946 return;
1947 }
1948
Alp Toker314cc812014-01-25 16:55:45 +00001949 if (F == ObjCMethodFamilyAttr::OMF_init &&
1950 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00001951 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00001952 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00001953 // Ignore the attribute.
1954 return;
1955 }
1956
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001957 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00001958 S.Context, F,
1959 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00001960}
1961
Chandler Carruthedc2c642011-07-02 00:01:44 +00001962static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00001963 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001964 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00001965 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001966 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1967 return;
1968 }
1969 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00001970 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1971 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00001972 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00001973 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
1974 return;
1975 }
1976 }
1977 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00001978 // It is okay to include this attribute on properties, e.g.:
1979 //
1980 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
1981 //
1982 // In this case it follows tradition and suppresses an error in the above
1983 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00001984 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00001985 }
Michael Han99315932013-01-24 16:46:58 +00001986 D->addAttr(::new (S.Context)
1987 ObjCNSObjectAttr(Attr.getRange(), S.Context,
1988 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001989}
1990
Chandler Carruthedc2c642011-07-02 00:01:44 +00001991static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001992 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001993 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00001994 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00001995 return;
1996 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001997
Aaron Ballman00e99962013-08-31 01:11:41 +00001998 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001999 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002000 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2001 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2002 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002003 return;
2004 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002005
Michael Han99315932013-01-24 16:46:58 +00002006 D->addAttr(::new (S.Context)
2007 BlocksAttr(Attr.getRange(), S.Context, type,
2008 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002009}
2010
Chandler Carruthedc2c642011-07-02 00:01:44 +00002011static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002012 // check the attribute arguments.
2013 if (Attr.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00002014 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
2015 << Attr.getName() << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002016 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002017 }
2018
Aaron Ballman18a78382013-11-21 00:28:23 +00002019 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002020 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002021 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002022 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002023 if (E->isTypeDependent() || E->isValueDependent() ||
2024 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002025 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002026 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002027 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002028 return;
2029 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002030
John McCallb46f2872011-09-09 07:56:05 +00002031 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002032 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2033 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002034 return;
2035 }
John McCallb46f2872011-09-09 07:56:05 +00002036
2037 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002038 }
2039
Aaron Ballman18a78382013-11-21 00:28:23 +00002040 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002041 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002042 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002043 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002044 if (E->isTypeDependent() || E->isValueDependent() ||
2045 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002046 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002047 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002048 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002049 return;
2050 }
2051 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002052
John McCallb46f2872011-09-09 07:56:05 +00002053 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002054 // FIXME: This error message could be improved, it would be nice
2055 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002056 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2057 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002058 return;
2059 }
2060 }
2061
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002062 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002063 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002064 if (isa<FunctionNoProtoType>(FT)) {
2065 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2066 return;
2067 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002068
Chris Lattner9363e312009-03-17 23:03:47 +00002069 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002070 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002071 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002072 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002073 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002074 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002075 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002076 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002077 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002078 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2079 if (!BD->isVariadic()) {
2080 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2081 return;
2082 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002083 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002084 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002085 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002086 const FunctionType *FT = Ty->isFunctionPointerType()
2087 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002088 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002089 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002090 int m = Ty->isFunctionPointerType() ? 0 : 1;
2091 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002092 return;
2093 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002094 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002095 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002096 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002097 return;
2098 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002099 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002100 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002101 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002102 return;
2103 }
Michael Han99315932013-01-24 16:46:58 +00002104 D->addAttr(::new (S.Context)
2105 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2106 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002107}
2108
Chandler Carruthedc2c642011-07-02 00:01:44 +00002109static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002110 if (D->getFunctionType() &&
2111 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002112 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2113 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002114 return;
2115 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002116 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002117 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002118 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2119 << Attr.getName() << 1;
2120 return;
2121 }
2122
Michael Han99315932013-01-24 16:46:58 +00002123 D->addAttr(::new (S.Context)
2124 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2125 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002126}
2127
Chandler Carruthedc2c642011-07-02 00:01:44 +00002128static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002129 // weak_import only applies to variable & function declarations.
2130 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002131 if (!D->canBeWeakImported(isDef)) {
2132 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002133 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2134 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002135 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002136 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002137 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002138 // Nothing to warn about here.
2139 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002140 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002141 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002142
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002143 return;
2144 }
2145
Michael Han99315932013-01-24 16:46:58 +00002146 D->addAttr(::new (S.Context)
2147 WeakImportAttr(Attr.getRange(), S.Context,
2148 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002149}
2150
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002151// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002152template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002153static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002154 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002155 uint32_t WGSize[3];
2156 for (unsigned i = 0; i < 3; ++i)
2157 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002158 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002159
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002160 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2161 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2162 Existing->getYDim() == WGSize[1] &&
2163 Existing->getZDim() == WGSize[2]))
2164 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002165
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002166 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2167 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002168 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002169}
2170
Joey Goulyaba589c2013-03-08 09:42:32 +00002171static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002172 if (!Attr.hasParsedType()) {
2173 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2174 << Attr.getName() << 1;
2175 return;
2176 }
2177
Richard Smithb87c4652013-10-31 21:23:20 +00002178 TypeSourceInfo *ParmTSI = 0;
2179 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2180 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002181
2182 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2183 (ParmType->isBooleanType() ||
2184 !ParmType->isIntegralType(S.getASTContext()))) {
2185 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2186 << ParmType;
2187 return;
2188 }
2189
Aaron Ballmana9e05402013-12-02 22:16:55 +00002190 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002191 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002192 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2193 return;
2194 }
2195 }
2196
2197 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002198 ParmTSI,
2199 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002200}
2201
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002202SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002203 StringRef Name,
2204 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002205 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2206 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002207 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002208 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2209 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002210 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002211 }
Michael Han99315932013-01-24 16:46:58 +00002212 return ::new (Context) SectionAttr(Range, Context, Name,
2213 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002214}
2215
Chandler Carruthedc2c642011-07-02 00:01:44 +00002216static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002217 // Make sure that there is a string literal as the sections's single
2218 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002219 StringRef Str;
2220 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002221 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002222 return;
Mike Stump11289f42009-09-09 15:08:12 +00002223
Chris Lattner30ba6742009-08-10 19:03:04 +00002224 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002225 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002226 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002227 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002228 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002229 return;
2230 }
Mike Stump11289f42009-09-09 15:08:12 +00002231
Michael Han99315932013-01-24 16:46:58 +00002232 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002233 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002234 if (NewAttr)
2235 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002236}
2237
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002238
Chandler Carruthedc2c642011-07-02 00:01:44 +00002239static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002240 VarDecl *VD = cast<VarDecl>(D);
2241 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002242 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002243 return;
2244 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002245
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002246 Expr *E = Attr.getArgAsExpr(0);
2247 SourceLocation Loc = E->getExprLoc();
2248 FunctionDecl *FD = 0;
2249 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002250
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002251 // gcc only allows for simple identifiers. Since we support more than gcc, we
2252 // will warn the user.
2253 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2254 if (DRE->hasQualifier())
2255 S.Diag(Loc, diag::warn_cleanup_ext);
2256 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2257 NI = DRE->getNameInfo();
2258 if (!FD) {
2259 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2260 << NI.getName();
2261 return;
2262 }
2263 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2264 if (ULE->hasExplicitTemplateArgs())
2265 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002266 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2267 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002268 if (!FD) {
2269 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2270 << NI.getName();
2271 if (ULE->getType() == S.Context.OverloadTy)
2272 S.NoteAllOverloadCandidates(ULE);
2273 return;
2274 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002275 } else {
2276 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002277 return;
2278 }
2279
Anders Carlssond277d792009-01-31 01:16:18 +00002280 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002281 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2282 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002283 return;
2284 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002285
Anders Carlsson723f55d2009-02-07 23:16:50 +00002286 // We're currently more strict than GCC about what function types we accept.
2287 // If this ever proves to be a problem it should be easy to fix.
2288 QualType Ty = S.Context.getPointerType(VD->getType());
2289 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002290 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2291 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002292 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2293 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002294 return;
2295 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002296
Michael Han99315932013-01-24 16:46:58 +00002297 D->addAttr(::new (S.Context)
2298 CleanupAttr(Attr.getRange(), S.Context, FD,
2299 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002300}
2301
Mike Stumpd3bb5572009-07-24 19:02:52 +00002302/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002303/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002304static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002305 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002306 uint64_t Idx;
2307 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002308 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002309
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002310 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002311 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002312
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002313 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2314 if (not_nsstring_type &&
2315 !isCFStringType(Ty, S.Context) &&
2316 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002317 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002318 // FIXME: Should highlight the actual expression that has the wrong type.
2319 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002320 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002321 << IdxExpr->getSourceRange();
2322 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002323 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002324 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002325 if (!isNSStringType(Ty, S.Context) &&
2326 !isCFStringType(Ty, S.Context) &&
2327 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002328 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002329 // FIXME: Should highlight the actual expression that has the wrong type.
2330 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002331 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002332 << IdxExpr->getSourceRange();
2333 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002334 }
2335
Alp Toker601b22c2014-01-21 23:35:24 +00002336 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002337 // because that has corrected for the implicit this parameter, and is zero-
2338 // based. The attribute expects what the user wrote explicitly.
2339 llvm::APSInt Val;
2340 IdxExpr->EvaluateAsInt(Val, S.Context);
2341
Michael Han99315932013-01-24 16:46:58 +00002342 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002343 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002344 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002345}
2346
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002347enum FormatAttrKind {
2348 CFStringFormat,
2349 NSStringFormat,
2350 StrftimeFormat,
2351 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002352 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002353 InvalidFormat
2354};
2355
2356/// getFormatAttrKind - Map from format attribute names to supported format
2357/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002358static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002359 return llvm::StringSwitch<FormatAttrKind>(Format)
2360 // Check for formats that get handled specially.
2361 .Case("NSString", NSStringFormat)
2362 .Case("CFString", CFStringFormat)
2363 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002364
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002365 // Otherwise, check for supported formats.
2366 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2367 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2368 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002369
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002370 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2371 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002372}
2373
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002374/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002375/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002376static void handleInitPriorityAttr(Sema &S, Decl *D,
2377 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002378 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002379 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2380 return;
2381 }
2382
Aaron Ballman4a611152013-11-27 16:34:09 +00002383 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002384 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2385 Attr.setInvalid();
2386 return;
2387 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002388 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002389 if (S.Context.getAsArrayType(T))
2390 T = S.Context.getBaseElementType(T);
2391 if (!T->getAs<RecordType>()) {
2392 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2393 Attr.setInvalid();
2394 return;
2395 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002396
2397 Expr *E = Attr.getArgAsExpr(0);
2398 uint32_t prioritynum;
2399 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002400 Attr.setInvalid();
2401 return;
2402 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002403
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002404 if (prioritynum < 101 || prioritynum > 65535) {
2405 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002406 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002407 Attr.setInvalid();
2408 return;
2409 }
Michael Han99315932013-01-24 16:46:58 +00002410 D->addAttr(::new (S.Context)
2411 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2412 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002413}
2414
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002415FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2416 IdentifierInfo *Format, int FormatIdx,
2417 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002418 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002419 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002420 for (auto *F : D->specific_attrs<FormatAttr>()) {
2421 if (F->getType() == Format &&
2422 F->getFormatIdx() == FormatIdx &&
2423 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002424 // If we don't have a valid location for this attribute, adopt the
2425 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002426 if (F->getLocation().isInvalid())
2427 F->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002428 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002429 }
2430 }
2431
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002432 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2433 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002434}
2435
Mike Stumpd3bb5572009-07-24 19:02:52 +00002436/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002437/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002438static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002439 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002440 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002441 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002442 return;
2443 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002444
Chandler Carruth743682b2010-11-16 08:35:43 +00002445 // In C++ the implicit 'this' function parameter also counts, and they are
2446 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002447 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002448 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002449
Aaron Ballman00e99962013-08-31 01:11:41 +00002450 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2451 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002452
2453 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002454 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002455 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002456 // If we've modified the string name, we need a new identifier for it.
2457 II = &S.Context.Idents.get(Format);
2458 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002459
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002460 // Check for supported formats.
2461 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002462
2463 if (Kind == IgnoredFormat)
2464 return;
2465
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002466 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002467 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002468 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002469 return;
2470 }
2471
2472 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002473 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002474 uint32_t Idx;
2475 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002476 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002477
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002478 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002479 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002480 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002481 return;
2482 }
2483
2484 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002485 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002486
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002487 if (HasImplicitThisParam) {
2488 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002489 S.Diag(Attr.getLoc(),
2490 diag::err_format_attribute_implicit_this_format_string)
2491 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002492 return;
2493 }
2494 ArgIdx--;
2495 }
Mike Stump11289f42009-09-09 15:08:12 +00002496
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002497 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002498 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002499
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002500 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002501 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002502 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2503 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002504 return;
2505 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002506 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002507 // FIXME: do we need to check if the type is NSString*? What are the
2508 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002509 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002510 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002511 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2512 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002513 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002514 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002515 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002516 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002517 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002518 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2519 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002520 return;
2521 }
2522
2523 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002524 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002525 uint32_t FirstArg;
2526 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002527 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002528
2529 // check if the function is variadic if the 3rd argument non-zero
2530 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002531 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002532 ++NumArgs; // +1 for ...
2533 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002534 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002535 return;
2536 }
2537 }
2538
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002539 // strftime requires FirstArg to be 0 because it doesn't read from any
2540 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002541 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002542 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002543 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2544 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002545 return;
2546 }
2547 // if 0 it disables parameter checking (to use with e.g. va_list)
2548 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002549 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002550 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002551 return;
2552 }
2553
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002554 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002555 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002556 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002557 if (NewAttr)
2558 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002559}
2560
Chandler Carruthedc2c642011-07-02 00:01:44 +00002561static void handleTransparentUnionAttr(Sema &S, Decl *D,
2562 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002563 // Try to find the underlying union declaration.
2564 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002565 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002566 if (TD && TD->getUnderlyingType()->isUnionType())
2567 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2568 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002569 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002570
2571 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002572 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002573 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002574 return;
2575 }
2576
John McCallf937c022011-10-07 06:10:15 +00002577 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002578 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002579 diag::warn_transparent_union_attribute_not_definition);
2580 return;
2581 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002582
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002583 RecordDecl::field_iterator Field = RD->field_begin(),
2584 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002585 if (Field == FieldEnd) {
2586 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2587 return;
2588 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002589
David Blaikie40ed2972012-06-06 20:45:41 +00002590 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002591 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002592 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002593 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002594 diag::warn_transparent_union_attribute_floating)
2595 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002596 return;
2597 }
2598
2599 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2600 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2601 for (; Field != FieldEnd; ++Field) {
2602 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002603 // FIXME: this isn't fully correct; we also need to test whether the
2604 // members of the union would all have the same calling convention as the
2605 // first member of the union. Checking just the size and alignment isn't
2606 // sufficient (consider structs passed on the stack instead of in registers
2607 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002608 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002609 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002610 // Warn if we drop the attribute.
2611 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002612 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002613 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002614 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002615 diag::warn_transparent_union_attribute_field_size_align)
2616 << isSize << Field->getDeclName() << FieldBits;
2617 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002618 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002619 diag::note_transparent_union_first_field_size_align)
2620 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002621 return;
2622 }
2623 }
2624
Michael Han99315932013-01-24 16:46:58 +00002625 RD->addAttr(::new (S.Context)
2626 TransparentUnionAttr(Attr.getRange(), S.Context,
2627 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002628}
2629
Chandler Carruthedc2c642011-07-02 00:01:44 +00002630static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002631 // Make sure that there is a string literal as the annotation's single
2632 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002633 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002634 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002635 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002636
2637 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002638 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
2639 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002640 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002641 }
Michael Han99315932013-01-24 16:46:58 +00002642
2643 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002644 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002645 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002646}
2647
Chandler Carruthedc2c642011-07-02 00:01:44 +00002648static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002649 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002650 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002651 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2652 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002653 return;
2654 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002655
Richard Smith848e1f12013-02-01 08:12:08 +00002656 if (Attr.getNumArgs() == 0) {
2657 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2658 true, 0, Attr.getAttributeSpellingListIndex()));
2659 return;
2660 }
2661
Aaron Ballman00e99962013-08-31 01:11:41 +00002662 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002663 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2664 S.Diag(Attr.getEllipsisLoc(),
2665 diag::err_pack_expansion_without_parameter_packs);
2666 return;
2667 }
2668
2669 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2670 return;
2671
2672 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2673 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002674}
2675
2676void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002677 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002678 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2679 SourceLocation AttrLoc = AttrRange.getBegin();
2680
Richard Smith1dba27c2013-01-29 09:02:09 +00002681 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002682 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002683 // C++11 [dcl.align]p1:
2684 // An alignment-specifier may be applied to a variable or to a class
2685 // data member, but it shall not be applied to a bit-field, a function
2686 // parameter, the formal parameter of a catch clause, or a variable
2687 // declared with the register storage class specifier. An
2688 // alignment-specifier may also be applied to the declaration of a class
2689 // or enumeration type.
2690 // C11 6.7.5/2:
2691 // An alignment attribute shall not be specified in a declaration of
2692 // a typedef, or a bit-field, or a function, or a parameter, or an
2693 // object declared with the register storage-class specifier.
2694 int DiagKind = -1;
2695 if (isa<ParmVarDecl>(D)) {
2696 DiagKind = 0;
2697 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2698 if (VD->getStorageClass() == SC_Register)
2699 DiagKind = 1;
2700 if (VD->isExceptionVariable())
2701 DiagKind = 2;
2702 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2703 if (FD->isBitField())
2704 DiagKind = 3;
2705 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00002706 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00002707 << (TmpAttr.isC11() ? ExpectedVariableOrField
2708 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002709 return;
2710 }
2711 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002712 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00002713 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002714 return;
2715 }
2716 }
2717
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002718 if (E->isTypeDependent() || E->isValueDependent()) {
2719 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002720 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2721 AA->setPackExpansion(IsPackExpansion);
2722 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002723 return;
2724 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002725
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002726 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002727 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002728 ExprResult ICE
2729 = VerifyIntegerConstantExpression(E, &Alignment,
2730 diag::err_aligned_attribute_argument_not_int,
2731 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002732 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002733 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002734
2735 // C++11 [dcl.align]p2:
2736 // -- if the constant expression evaluates to zero, the alignment
2737 // specifier shall have no effect
2738 // C11 6.7.5p6:
2739 // An alignment specification of zero has no effect.
2740 if (!(TmpAttr.isAlignas() && !Alignment) &&
2741 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002742 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2743 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002744 return;
2745 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002746
David Majnemerabecae72014-02-12 20:36:10 +00002747 // Alignment calculations can wrap around if it's greater than 2**28.
2748 unsigned MaxValidAlignment = TmpAttr.isDeclspec() ? 8192 : 268435456;
2749 if (Alignment.getZExtValue() > MaxValidAlignment) {
2750 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
2751 << E->getSourceRange();
2752 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00002753 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002754
Richard Smith44c247f2013-02-22 08:32:16 +00002755 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2756 ICE.take(), SpellingListIndex);
2757 AA->setPackExpansion(IsPackExpansion);
2758 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002759}
2760
Michael Hanaf02bbe2013-02-01 01:19:17 +00002761void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002762 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002763 // FIXME: Cache the number on the Attr object if non-dependent?
2764 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002765 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2766 SpellingListIndex);
2767 AA->setPackExpansion(IsPackExpansion);
2768 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002769}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002770
Richard Smith848e1f12013-02-01 08:12:08 +00002771void Sema::CheckAlignasUnderalignment(Decl *D) {
2772 assert(D->hasAttrs() && "no attributes on decl");
2773
2774 QualType Ty;
2775 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2776 Ty = VD->getType();
2777 else
2778 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002779 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002780 return;
2781
2782 // C++11 [dcl.align]p5, C11 6.7.5/4:
2783 // The combined effect of all alignment attributes in a declaration shall
2784 // not specify an alignment that is less strict than the alignment that
2785 // would otherwise be required for the entity being declared.
2786 AlignedAttr *AlignasAttr = 0;
2787 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002788 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00002789 if (I->isAlignmentDependent())
2790 return;
2791 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002792 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00002793 Align = std::max(Align, I->getAlignment(Context));
2794 }
2795
2796 if (AlignasAttr && Align) {
2797 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2798 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2799 if (NaturalAlign > RequestedAlign)
2800 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2801 << Ty << (unsigned)NaturalAlign.getQuantity();
2802 }
2803}
2804
David Majnemer2c4e00a2014-01-29 22:07:36 +00002805bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00002806 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00002807 MSInheritanceAttr::Spelling SemanticSpelling) {
2808 assert(RD->hasDefinition() && "RD has no definition!");
2809
David Majnemer98c9ee22014-02-07 00:43:07 +00002810 // We may not have seen base specifiers or any virtual methods yet. We will
2811 // have to wait until the record is defined to catch any mismatches.
2812 if (!RD->getDefinition()->isCompleteDefinition())
2813 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002814
David Majnemer98c9ee22014-02-07 00:43:07 +00002815 // The unspecified model never matches what a definition could need.
2816 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
2817 return false;
2818
David Majnemer4bb09802014-02-10 19:50:15 +00002819 if (BestCase) {
2820 if (RD->calculateInheritanceModel() == SemanticSpelling)
2821 return false;
2822 } else {
2823 if (RD->calculateInheritanceModel() <= SemanticSpelling)
2824 return false;
2825 }
David Majnemer98c9ee22014-02-07 00:43:07 +00002826
2827 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
2828 << 0 /*definition*/;
2829 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
2830 << RD->getNameAsString();
2831 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002832}
2833
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002834/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002835/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002836///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002837/// Despite what would be logical, the mode attribute is a decl attribute, not a
2838/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2839/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002840static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002841 // This attribute isn't documented, but glibc uses it. It changes
2842 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002843 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002844 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2845 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002846 return;
2847 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002848
Aaron Ballman00e99962013-08-31 01:11:41 +00002849 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2850 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002851
2852 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002853 if (Str.startswith("__") && Str.endswith("__"))
2854 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002855
2856 unsigned DestWidth = 0;
2857 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002858 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002859 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002860 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002861 switch (Str[0]) {
2862 case 'Q': DestWidth = 8; break;
2863 case 'H': DestWidth = 16; break;
2864 case 'S': DestWidth = 32; break;
2865 case 'D': DestWidth = 64; break;
2866 case 'X': DestWidth = 96; break;
2867 case 'T': DestWidth = 128; break;
2868 }
2869 if (Str[1] == 'F') {
2870 IntegerMode = false;
2871 } else if (Str[1] == 'C') {
2872 IntegerMode = false;
2873 ComplexMode = true;
2874 } else if (Str[1] != 'I') {
2875 DestWidth = 0;
2876 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002877 break;
2878 case 4:
2879 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2880 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002881 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002882 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002883 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002884 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002885 break;
2886 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002887 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002888 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002889 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002890 case 11:
2891 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00002892 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002893 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002894 }
2895
2896 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002897 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002898 OldTy = TD->getUnderlyingType();
2899 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2900 OldTy = VD->getType();
2901 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002902 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00002903 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002904 return;
2905 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002906
John McCall9dd450b2009-09-21 23:43:11 +00002907 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002908 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2909 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002910 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002911 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2912 } else if (ComplexMode) {
2913 if (!OldTy->isComplexType())
2914 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2915 } else {
2916 if (!OldTy->isFloatingType())
2917 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2918 }
2919
Mike Stump87c57ac2009-05-16 07:39:55 +00002920 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2921 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002922 // FIXME: Make sure floating-point mappings are accurate
2923 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002924 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00002925 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002926 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002927 }
2928
2929 QualType NewTy;
2930
2931 if (IntegerMode)
2932 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
2933 OldTy->isSignedIntegerType());
2934 else
2935 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
2936
2937 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00002938 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002939 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002940 }
2941
Eli Friedman4735374e2009-03-03 06:41:03 +00002942 if (ComplexMode) {
2943 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002944 }
2945
2946 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002947 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
2948 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
2949 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00002950 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002951
2952 D->addAttr(::new (S.Context)
2953 ModeAttr(Attr.getRange(), S.Context, Name,
2954 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00002955}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002956
Chandler Carruthedc2c642011-07-02 00:01:44 +00002957static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00002958 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2959 if (!VD->hasGlobalStorage())
2960 S.Diag(Attr.getLoc(),
2961 diag::warn_attribute_requires_functions_or_static_globals)
2962 << Attr.getName();
2963 } else if (!isFunctionOrMethod(D)) {
2964 S.Diag(Attr.getLoc(),
2965 diag::warn_attribute_requires_functions_or_static_globals)
2966 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00002967 return;
2968 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002969
Michael Han99315932013-01-24 16:46:58 +00002970 D->addAttr(::new (S.Context)
2971 NoDebugAttr(Attr.getRange(), S.Context,
2972 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00002973}
2974
Paul Robinsonf0674352014-03-31 22:29:15 +00002975static void handleAlwaysInlineAttr(Sema &S, Decl *D,
2976 const AttributeList &Attr) {
2977 if (checkAttrMutualExclusion<OptimizeNoneAttr>(S, D, Attr))
2978 return;
2979
2980 D->addAttr(::new (S.Context)
2981 AlwaysInlineAttr(Attr.getRange(), S.Context,
2982 Attr.getAttributeSpellingListIndex()));
2983}
2984
2985static void handleOptimizeNoneAttr(Sema &S, Decl *D,
2986 const AttributeList &Attr) {
2987 if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr))
2988 return;
2989
2990 D->addAttr(::new (S.Context)
2991 OptimizeNoneAttr(Attr.getRange(), S.Context,
2992 Attr.getAttributeSpellingListIndex()));
2993}
2994
Chandler Carruthedc2c642011-07-02 00:01:44 +00002995static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00002996 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00002997 if (!FD->getReturnType()->isVoidType()) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00002998 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
2999 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
3000 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3001 << FD->getType()
Alp Toker42a16a62014-01-25 23:51:36 +00003002 << FixItHint::CreateReplacement(FTL.getReturnLoc().getSourceRange(),
Aaron Ballman3aff6332013-12-02 19:30:36 +00003003 "void");
3004 } else {
3005 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3006 << FD->getType();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003007 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003008 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003009 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003010
Aaron Ballman3aff6332013-12-02 19:30:36 +00003011 D->addAttr(::new (S.Context)
3012 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003013 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003014}
3015
Chandler Carruthedc2c642011-07-02 00:01:44 +00003016static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003017 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003018 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003019 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003020 return;
3021 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003022
Michael Han99315932013-01-24 16:46:58 +00003023 D->addAttr(::new (S.Context)
3024 GNUInlineAttr(Attr.getRange(), S.Context,
3025 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003026}
3027
Chandler Carruthedc2c642011-07-02 00:01:44 +00003028static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003029 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003030
Aaron Ballman02df2e02012-12-09 17:45:41 +00003031 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003032 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003033 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3034 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003035 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003036 return;
3037
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003038 if (!isa<ObjCMethodDecl>(D)) {
3039 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3040 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003041 return;
3042 }
3043
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003044 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003045 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003046 D->addAttr(::new (S.Context)
3047 FastCallAttr(Attr.getRange(), S.Context,
3048 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003049 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003050 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003051 D->addAttr(::new (S.Context)
3052 StdCallAttr(Attr.getRange(), S.Context,
3053 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003054 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003055 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003056 D->addAttr(::new (S.Context)
3057 ThisCallAttr(Attr.getRange(), S.Context,
3058 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003059 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003060 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003061 D->addAttr(::new (S.Context)
3062 CDeclAttr(Attr.getRange(), S.Context,
3063 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003064 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003065 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003066 D->addAttr(::new (S.Context)
3067 PascalAttr(Attr.getRange(), S.Context,
3068 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003069 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003070 case AttributeList::AT_MSABI:
3071 D->addAttr(::new (S.Context)
3072 MSABIAttr(Attr.getRange(), S.Context,
3073 Attr.getAttributeSpellingListIndex()));
3074 return;
3075 case AttributeList::AT_SysVABI:
3076 D->addAttr(::new (S.Context)
3077 SysVABIAttr(Attr.getRange(), S.Context,
3078 Attr.getAttributeSpellingListIndex()));
3079 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003080 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003081 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003082 switch (CC) {
3083 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003084 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003085 break;
3086 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003087 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003088 break;
3089 default:
3090 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003091 }
3092
Michael Han99315932013-01-24 16:46:58 +00003093 D->addAttr(::new (S.Context)
3094 PcsAttr(Attr.getRange(), S.Context, PCS,
3095 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003096 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003097 }
Derek Schuffa2020962012-10-16 22:30:41 +00003098 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003099 D->addAttr(::new (S.Context)
3100 PnaclCallAttr(Attr.getRange(), S.Context,
3101 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003102 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003103 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003104 D->addAttr(::new (S.Context)
3105 IntelOclBiccAttr(Attr.getRange(), S.Context,
3106 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003107 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003108
Abramo Bagnara50099372010-04-30 13:10:51 +00003109 default:
3110 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003111 }
3112}
3113
Aaron Ballman02df2e02012-12-09 17:45:41 +00003114bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3115 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003116 if (attr.isInvalid())
3117 return true;
3118
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003119 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003120 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003121 attr.setInvalid();
3122 return true;
3123 }
3124
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003125 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003126 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003127 case AttributeList::AT_CDecl: CC = CC_C; break;
3128 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3129 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3130 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3131 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003132 case AttributeList::AT_MSABI:
3133 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3134 CC_X86_64Win64;
3135 break;
3136 case AttributeList::AT_SysVABI:
3137 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3138 CC_C;
3139 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003140 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003141 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003142 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003143 attr.setInvalid();
3144 return true;
3145 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003146 if (StrRef == "aapcs") {
3147 CC = CC_AAPCS;
3148 break;
3149 } else if (StrRef == "aapcs-vfp") {
3150 CC = CC_AAPCS_VFP;
3151 break;
3152 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003153
3154 attr.setInvalid();
3155 Diag(attr.getLoc(), diag::err_invalid_pcs);
3156 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003157 }
Derek Schuffa2020962012-10-16 22:30:41 +00003158 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003159 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003160 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003161 }
3162
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003163 const TargetInfo &TI = Context.getTargetInfo();
3164 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3165 if (A == TargetInfo::CCCR_Warning) {
3166 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003167
3168 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3169 if (FD)
3170 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3171 TargetInfo::CCMT_NonMember;
3172 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003173 }
3174
John McCall3882ace2011-01-05 12:14:39 +00003175 return false;
3176}
3177
John McCall3882ace2011-01-05 12:14:39 +00003178/// Checks a regparm attribute, returning true if it is ill-formed and
3179/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003180bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3181 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003182 return true;
3183
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003184 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003185 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003186 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003187 }
Eli Friedman7044b762009-03-27 21:06:47 +00003188
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003189 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003190 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003191 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003192 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003193 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003194 }
3195
Douglas Gregore8bbc122011-09-02 00:18:52 +00003196 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003197 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003198 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003199 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003200 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003201 }
3202
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003203 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003204 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003205 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003206 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003207 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003208 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003209 }
3210
John McCall3882ace2011-01-05 12:14:39 +00003211 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003212}
3213
Aaron Ballman66039932013-12-19 00:41:31 +00003214static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3215 const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003216 // check the attribute arguments.
3217 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3218 // FIXME: 0 is not okay.
Aaron Ballman05e420a2014-01-02 21:26:14 +00003219 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3220 << Attr.getName() << 2;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003221 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003222 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003223
Aaron Ballman66039932013-12-19 00:41:31 +00003224 uint32_t MaxThreads, MinBlocks = 0;
3225 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3226 return;
3227 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3228 Attr.getArgAsExpr(1),
3229 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003230 return;
3231
3232 D->addAttr(::new (S.Context)
3233 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3234 MaxThreads, MinBlocks,
3235 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003236}
3237
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003238static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3239 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003240 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003241 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003242 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003243 return;
3244 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003245
3246 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003247 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003248
Aaron Ballman00e99962013-08-31 01:11:41 +00003249 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003250
3251 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3252 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3253 << Attr.getName() << ExpectedFunctionOrMethod;
3254 return;
3255 }
3256
3257 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003258 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3259 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003260 return;
3261
3262 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003263 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3264 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003265 return;
3266
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003267 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003268 if (IsPointer) {
3269 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003270 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003271 if (!BufferTy->isPointerType()) {
3272 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003273 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003274 }
3275 }
3276
Michael Han99315932013-01-24 16:46:58 +00003277 D->addAttr(::new (S.Context)
3278 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3279 ArgumentIdx, TypeTagIdx, IsPointer,
3280 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003281}
3282
3283static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3284 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003285 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003286 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003287 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003288 return;
3289 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003290
3291 if (!checkAttributeNumArgs(S, Attr, 1))
3292 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003293
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003294 if (!isa<VarDecl>(D)) {
3295 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3296 << Attr.getName() << ExpectedVariable;
3297 return;
3298 }
3299
Aaron Ballman00e99962013-08-31 01:11:41 +00003300 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003301 TypeSourceInfo *MatchingCTypeLoc = 0;
3302 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3303 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003304
Michael Han99315932013-01-24 16:46:58 +00003305 D->addAttr(::new (S.Context)
3306 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003307 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003308 Attr.getLayoutCompatible(),
3309 Attr.getMustBeNull(),
3310 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003311}
3312
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003313//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003314// Checker-specific attribute handlers.
3315//===----------------------------------------------------------------------===//
3316
John McCalled433932011-01-25 03:31:58 +00003317static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003318 return type->isDependentType() ||
3319 type->isObjCObjectPointerType() ||
3320 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003321}
3322static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003323 return type->isDependentType() ||
3324 type->isPointerType() ||
3325 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003326}
3327
Chandler Carruthedc2c642011-07-02 00:01:44 +00003328static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003329 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003330 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003331
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003332 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003333 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3334 cf = false;
3335 } else {
3336 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3337 cf = true;
3338 }
3339
3340 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003341 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003342 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003343 return;
3344 }
3345
3346 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003347 param->addAttr(::new (S.Context)
3348 CFConsumedAttr(Attr.getRange(), S.Context,
3349 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003350 else
Michael Han99315932013-01-24 16:46:58 +00003351 param->addAttr(::new (S.Context)
3352 NSConsumedAttr(Attr.getRange(), S.Context,
3353 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003354}
3355
Chandler Carruthedc2c642011-07-02 00:01:44 +00003356static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3357 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003358
John McCalled433932011-01-25 03:31:58 +00003359 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003360
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003361 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003362 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003363 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003364 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003365 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003366 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3367 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003368 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003369 returnType = FD->getReturnType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003370 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003371 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003372 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003373 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003374 return;
3375 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003376
John McCalled433932011-01-25 03:31:58 +00003377 bool typeOK;
3378 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003379 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003380 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003381 case AttributeList::AT_NSReturnsAutoreleased:
3382 case AttributeList::AT_NSReturnsRetained:
3383 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003384 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3385 cf = false;
3386 break;
3387
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003388 case AttributeList::AT_CFReturnsRetained:
3389 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003390 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3391 cf = true;
3392 break;
3393 }
3394
3395 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003396 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003397 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003398 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003399 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003400
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003401 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003402 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003403 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003404 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003405 D->addAttr(::new (S.Context)
3406 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3407 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003408 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003409 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003410 D->addAttr(::new (S.Context)
3411 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3412 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003413 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003414 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003415 D->addAttr(::new (S.Context)
3416 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3417 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003418 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003419 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003420 D->addAttr(::new (S.Context)
3421 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3422 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003423 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003424 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003425 D->addAttr(::new (S.Context)
3426 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3427 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003428 return;
3429 };
3430}
3431
John McCallcf166702011-07-22 08:53:00 +00003432static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3433 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003434 const int EP_ObjCMethod = 1;
3435 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003436
John McCallcf166702011-07-22 08:53:00 +00003437 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003438 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003439 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003440 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003441 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003442 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003443
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003444 if (!resultType->isReferenceType() &&
3445 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003446 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003447 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003448 << attr.getName()
3449 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003450 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003451
3452 // Drop the attribute.
3453 return;
3454 }
3455
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003456 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003457 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3458 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003459}
3460
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003461static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3462 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003463 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003464
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003465 DeclContext *DC = method->getDeclContext();
3466 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3467 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3468 << attr.getName() << 0;
3469 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3470 return;
3471 }
3472 if (method->getMethodFamily() == OMF_dealloc) {
3473 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3474 << attr.getName() << 1;
3475 return;
3476 }
3477
Michael Han99315932013-01-24 16:46:58 +00003478 method->addAttr(::new (S.Context)
3479 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3480 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003481}
3482
Aaron Ballmanfb763042013-12-02 18:05:46 +00003483static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3484 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003485 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00003486 return;
John McCall32f5fe12011-09-30 05:12:12 +00003487
Aaron Ballmanfb763042013-12-02 18:05:46 +00003488 D->addAttr(::new (S.Context)
3489 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3490 Attr.getAttributeSpellingListIndex()));
3491}
3492
3493static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3494 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003495 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00003496 return;
3497
3498 D->addAttr(::new (S.Context)
3499 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3500 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003501}
3502
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003503static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3504 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003505 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003506
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003507 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003508 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003509 return;
3510 }
3511
3512 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003513 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003514 Attr.getAttributeSpellingListIndex()));
3515}
3516
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003517static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3518 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003519 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003520
3521 if (!Parm) {
3522 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3523 return;
3524 }
3525
3526 D->addAttr(::new (S.Context)
3527 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3528 Attr.getAttributeSpellingListIndex()));
3529}
3530
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003531static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3532 const AttributeList &Attr) {
3533 IdentifierInfo *RelatedClass =
3534 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0;
3535 if (!RelatedClass) {
3536 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3537 return;
3538 }
3539 IdentifierInfo *ClassMethod =
3540 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0;
3541 IdentifierInfo *InstanceMethod =
3542 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0;
3543 D->addAttr(::new (S.Context)
3544 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3545 ClassMethod, InstanceMethod,
3546 Attr.getAttributeSpellingListIndex()));
3547}
3548
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003549static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3550 const AttributeList &Attr) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00003551 ObjCInterfaceDecl *IFace;
3552 if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
3553 IFace = CatDecl->getClassInterface();
3554 else
3555 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003556 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003557 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003558 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3559 Attr.getAttributeSpellingListIndex()));
3560}
3561
Chandler Carruthedc2c642011-07-02 00:01:44 +00003562static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3563 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003564 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003565
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003566 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003567 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003568}
3569
Chandler Carruthedc2c642011-07-02 00:01:44 +00003570static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3571 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003572 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003573 QualType type = vd->getType();
3574
3575 if (!type->isDependentType() &&
3576 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003577 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003578 << type;
3579 return;
3580 }
3581
3582 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3583
3584 // If we have no lifetime yet, check the lifetime we're presumably
3585 // going to infer.
3586 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3587 lifetime = type->getObjCARCImplicitLifetime();
3588
3589 switch (lifetime) {
3590 case Qualifiers::OCL_None:
3591 assert(type->isDependentType() &&
3592 "didn't infer lifetime for non-dependent type?");
3593 break;
3594
3595 case Qualifiers::OCL_Weak: // meaningful
3596 case Qualifiers::OCL_Strong: // meaningful
3597 break;
3598
3599 case Qualifiers::OCL_ExplicitNone:
3600 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003601 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003602 << (lifetime == Qualifiers::OCL_Autoreleasing);
3603 break;
3604 }
3605
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003606 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003607 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3608 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003609}
3610
Francois Picheta83957a2010-12-19 06:50:37 +00003611//===----------------------------------------------------------------------===//
3612// Microsoft specific attribute handlers.
3613//===----------------------------------------------------------------------===//
3614
Chandler Carruthedc2c642011-07-02 00:01:44 +00003615static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003616 if (!S.LangOpts.CPlusPlus) {
3617 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3618 << Attr.getName() << AttributeLangSupport::C;
3619 return;
3620 }
3621
Aaron Ballman60e705e2013-11-24 20:58:02 +00003622 if (!isa<CXXRecordDecl>(D)) {
3623 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3624 << Attr.getName() << ExpectedClass;
3625 return;
3626 }
3627
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003628 StringRef StrRef;
3629 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003630 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003631 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003632
David Majnemer89085342013-08-09 08:56:20 +00003633 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3634 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003635 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3636 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003637
Reid Kleckner140c4a72013-05-17 14:04:52 +00003638 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003639 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003640 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003641 return;
3642 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003643
David Majnemer89085342013-08-09 08:56:20 +00003644 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003645 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003646 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003647 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003648 return;
3649 }
David Majnemer89085342013-08-09 08:56:20 +00003650 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003651 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003652 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003653 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003654 }
Francois Picheta83957a2010-12-19 06:50:37 +00003655
David Majnemer89085342013-08-09 08:56:20 +00003656 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3657 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003658}
3659
David Majnemer2c4e00a2014-01-29 22:07:36 +00003660static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3661 if (!S.LangOpts.CPlusPlus) {
3662 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3663 << Attr.getName() << AttributeLangSupport::C;
3664 return;
3665 }
3666 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00003667 D, Attr.getRange(), /*BestCase=*/true,
3668 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00003669 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
3670 if (IA)
3671 D->addAttr(IA);
3672}
3673
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003674static void handleARMInterruptAttr(Sema &S, Decl *D,
3675 const AttributeList &Attr) {
3676 // Check the attribute arguments.
3677 if (Attr.getNumArgs() > 1) {
3678 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3679 << Attr.getName() << 1;
3680 return;
3681 }
3682
3683 StringRef Str;
3684 SourceLocation ArgLoc;
3685
3686 if (Attr.getNumArgs() == 0)
3687 Str = "";
3688 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
3689 return;
3690
3691 ARMInterruptAttr::InterruptType Kind;
3692 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
3693 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
3694 << Attr.getName() << Str << ArgLoc;
3695 return;
3696 }
3697
3698 unsigned Index = Attr.getAttributeSpellingListIndex();
3699 D->addAttr(::new (S.Context)
3700 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
3701}
3702
3703static void handleMSP430InterruptAttr(Sema &S, Decl *D,
3704 const AttributeList &Attr) {
3705 if (!checkAttributeNumArgs(S, Attr, 1))
3706 return;
3707
3708 if (!Attr.isArgExpr(0)) {
3709 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3710 << AANT_ArgumentIntegerConstant;
3711 return;
3712 }
3713
3714 // FIXME: Check for decl - it should be void ()(void).
3715
3716 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3717 llvm::APSInt NumParams(32);
3718 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
3719 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3720 << Attr.getName() << AANT_ArgumentIntegerConstant
3721 << NumParamsExpr->getSourceRange();
3722 return;
3723 }
3724
3725 unsigned Num = NumParams.getLimitedValue(255);
3726 if ((Num & 1) || Num > 30) {
3727 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3728 << Attr.getName() << (int)NumParams.getSExtValue()
3729 << NumParamsExpr->getSourceRange();
3730 return;
3731 }
3732
Aaron Ballman36a53502014-01-16 13:03:14 +00003733 D->addAttr(::new (S.Context)
3734 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
3735 Attr.getAttributeSpellingListIndex()));
3736 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003737}
3738
3739static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3740 // Dispatch the interrupt attribute based on the current target.
3741 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
3742 handleMSP430InterruptAttr(S, D, Attr);
3743 else
3744 handleARMInterruptAttr(S, D, Attr);
3745}
3746
3747static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
3748 const AttributeList& Attr) {
3749 // If we try to apply it to a function pointer, don't warn, but don't
3750 // do anything, either. It doesn't matter anyway, because there's nothing
3751 // special about calling a force_align_arg_pointer function.
3752 ValueDecl *VD = dyn_cast<ValueDecl>(D);
3753 if (VD && VD->getType()->isFunctionPointerType())
3754 return;
3755 // Also don't warn on function pointer typedefs.
3756 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
3757 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
3758 TD->getUnderlyingType()->isFunctionType()))
3759 return;
3760 // Attribute can only be applied to function types.
3761 if (!isa<FunctionDecl>(D)) {
3762 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3763 << Attr.getName() << /* function */0;
3764 return;
3765 }
3766
Aaron Ballman36a53502014-01-16 13:03:14 +00003767 D->addAttr(::new (S.Context)
3768 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
3769 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003770}
3771
3772DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
3773 unsigned AttrSpellingListIndex) {
3774 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00003775 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003776 return NULL;
3777 }
3778
3779 if (D->hasAttr<DLLImportAttr>())
3780 return NULL;
3781
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003782 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003783}
3784
3785static void handleDLLImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3786 // Attribute can be applied only to functions or variables.
3787 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3788 if (!FD && !isa<VarDecl>(D)) {
3789 // Apparently Visual C++ thinks it is okay to not emit a warning
3790 // in this case, so only emit a warning when -fms-extensions is not
3791 // specified.
3792 if (!S.getLangOpts().MicrosoftExt)
3793 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003794 << Attr.getName() << ExpectedVariableOrFunction;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003795 return;
3796 }
3797
3798 // Currently, the dllimport attribute is ignored for inlined functions.
3799 // Warning is emitted.
3800 if (FD && FD->isInlineSpecified()) {
3801 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3802 return;
3803 }
3804
3805 unsigned Index = Attr.getAttributeSpellingListIndex();
3806 DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index);
3807 if (NewAttr)
3808 D->addAttr(NewAttr);
3809}
3810
3811DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
3812 unsigned AttrSpellingListIndex) {
3813 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00003814 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003815 D->dropAttr<DLLImportAttr>();
3816 }
3817
3818 if (D->hasAttr<DLLExportAttr>())
3819 return NULL;
3820
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003821 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003822}
3823
3824static void handleDLLExportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3825 // Currently, the dllexport attribute is ignored for inlined functions, unless
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003826 // the -fkeep-inline-functions flag has been used. Warning is emitted.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003827 if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isInlineSpecified()) {
3828 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
3829 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3830 return;
3831 }
3832
3833 unsigned Index = Attr.getAttributeSpellingListIndex();
3834 DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index);
3835 if (NewAttr)
3836 D->addAttr(NewAttr);
3837}
3838
David Majnemer2c4e00a2014-01-29 22:07:36 +00003839MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00003840Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003841 unsigned AttrSpellingListIndex,
3842 MSInheritanceAttr::Spelling SemanticSpelling) {
3843 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
3844 if (IA->getSemanticSpelling() == SemanticSpelling)
3845 return 0;
3846 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
3847 << 1 /*previous declaration*/;
3848 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
3849 D->dropAttr<MSInheritanceAttr>();
3850 }
3851
3852 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
3853 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00003854 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
3855 SemanticSpelling)) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00003856 return 0;
3857 }
3858 } else {
3859 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
3860 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
3861 << 1 /*partial specialization*/;
3862 return 0;
3863 }
3864 if (RD->getDescribedClassTemplate()) {
3865 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
3866 << 0 /*primary template*/;
3867 return 0;
3868 }
3869 }
3870
3871 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00003872 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00003873}
3874
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003875static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3876 // The capability attributes take a single string parameter for the name of
3877 // the capability they represent. The lockable attribute does not take any
3878 // parameters. However, semantically, both attributes represent the same
3879 // concept, and so they use the same semantic attribute. Eventually, the
3880 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00003881 //
3882 // For backwards compatibility, any capability which has no specified string
3883 // literal will be considered a "mutex."
3884 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003885 SourceLocation LiteralLoc;
3886 if (Attr.getKind() == AttributeList::AT_Capability &&
3887 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
3888 return;
3889
Aaron Ballman6c810072014-03-05 21:47:13 +00003890 // Currently, there are only two names allowed for a capability: role and
3891 // mutex (case insensitive). Diagnose other capability names.
3892 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
3893 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
3894
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003895 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
3896 Attr.getAttributeSpellingListIndex()));
3897}
3898
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003899static void handleAssertCapabilityAttr(Sema &S, Decl *D,
3900 const AttributeList &Attr) {
3901 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
3902 Attr.getArgAsExpr(0),
3903 Attr.getAttributeSpellingListIndex()));
3904}
3905
3906static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
3907 const AttributeList &Attr) {
3908 SmallVector<Expr*, 1> Args;
Aaron Ballman18d85ae2014-03-20 16:02:49 +00003909 if (!checkLockFunAttrCommon(S, D, Attr, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003910 return;
3911
3912 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
3913 S.Context,
3914 Args.data(), Args.size(),
3915 Attr.getAttributeSpellingListIndex()));
3916}
3917
3918static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
3919 const AttributeList &Attr) {
3920 SmallVector<Expr*, 2> Args;
3921 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
3922 return;
3923
3924 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
3925 S.Context,
3926 Attr.getArgAsExpr(0),
3927 Args.data(),
3928 Args.size(),
3929 Attr.getAttributeSpellingListIndex()));
3930}
3931
3932static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
3933 const AttributeList &Attr) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003934 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00003935 SmallVector<Expr *, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00003936 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003937
Aaron Ballman18d85ae2014-03-20 16:02:49 +00003938 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
3939 Attr.getRange(), S.Context, Args.data(), Args.size(),
3940 Attr.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003941}
3942
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003943static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
3944 const AttributeList &Attr) {
3945 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
3946 return;
3947
3948 // check that all arguments are lockable objects
3949 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00003950 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003951 if (Args.empty())
3952 return;
3953
3954 RequiresCapabilityAttr *RCA = ::new (S.Context)
3955 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
3956 Args.size(), Attr.getAttributeSpellingListIndex());
3957
3958 D->addAttr(RCA);
3959}
3960
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003961/// Handles semantic checking for features that are common to all attributes,
3962/// such as checking whether a parameter was properly specified, or the correct
3963/// number of arguments were passed, etc.
3964static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3965 const AttributeList &Attr) {
3966 // Several attributes carry different semantics than the parsing requires, so
3967 // those are opted out of the common handling.
3968 //
3969 // We also bail on unknown and ignored attributes because those are handled
3970 // as part of the target-specific handling logic.
3971 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003972 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003973 return false;
3974
Aaron Ballman3aff6332013-12-02 19:30:36 +00003975 // Check whether the attribute requires specific language extensions to be
3976 // enabled.
3977 if (!Attr.diagnoseLangOpts(S))
3978 return true;
3979
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003980 // If there are no optional arguments, then checking for the argument count
3981 // is trivial.
3982 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
3983 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
3984 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003985
3986 // Check whether the attribute appertains to the given subject.
3987 if (!Attr.diagnoseAppertainsTo(S, D))
3988 return true;
3989
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003990 return false;
3991}
3992
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003993//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003994// Top Level Sema Entry Points
3995//===----------------------------------------------------------------------===//
3996
Richard Smithf8a75c32013-08-29 00:47:48 +00003997/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3998/// the attribute applies to decls. If the attribute is a type attribute, just
3999/// silently ignore it if a GNU attribute.
4000static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4001 const AttributeList &Attr,
4002 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004003 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00004004 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004005
Richard Smithf8a75c32013-08-29 00:47:48 +00004006 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4007 // instead.
4008 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4009 return;
4010
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004011 // Unknown attributes are automatically warned on. Target-specific attributes
4012 // which do not apply to the current target architecture are treated as
4013 // though they were unknown attributes.
4014 if (Attr.getKind() == AttributeList::UnknownAttribute ||
4015 !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) {
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004016 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute()
4017 ? diag::warn_unhandled_ms_attribute_ignored
4018 : diag::warn_unknown_attribute_ignored)
4019 << Attr.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004020 return;
4021 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004022
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004023 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4024 return;
4025
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004026 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004027 default:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004028 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004029 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004030 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004031 case AttributeList::AT_Interrupt:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004032 handleInterruptAttr(S, D, Attr);
4033 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004034 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004035 handleX86ForceAlignArgPointerAttr(S, D, Attr);
4036 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004037 case AttributeList::AT_DLLExport:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004038 handleDLLExportAttr(S, D, Attr);
4039 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004040 case AttributeList::AT_DLLImport:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004041 handleDLLImportAttr(S, D, Attr);
4042 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004043 case AttributeList::AT_Mips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004044 handleSimpleAttribute<Mips16Attr>(S, D, Attr);
4045 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004046 case AttributeList::AT_NoMips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004047 handleSimpleAttribute<NoMips16Attr>(S, D, Attr);
4048 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00004049 case AttributeList::AT_IBAction:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004050 handleSimpleAttribute<IBActionAttr>(S, D, Attr);
4051 break;
4052 case AttributeList::AT_IBOutlet:
4053 handleIBOutlet(S, D, Attr);
4054 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004055 case AttributeList::AT_IBOutletCollection:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004056 handleIBOutletCollection(S, D, Attr);
4057 break;
4058 case AttributeList::AT_Alias:
4059 handleAliasAttr(S, D, Attr);
4060 break;
4061 case AttributeList::AT_Aligned:
4062 handleAlignedAttr(S, D, Attr);
4063 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004064 case AttributeList::AT_AlwaysInline:
Paul Robinsonf0674352014-03-31 22:29:15 +00004065 handleAlwaysInlineAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004066 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004067 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004068 handleAnalyzerNoReturnAttr(S, D, Attr);
4069 break;
4070 case AttributeList::AT_TLSModel:
4071 handleTLSModelAttr(S, D, Attr);
4072 break;
4073 case AttributeList::AT_Annotate:
4074 handleAnnotateAttr(S, D, Attr);
4075 break;
4076 case AttributeList::AT_Availability:
4077 handleAvailabilityAttr(S, D, Attr);
4078 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004079 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004080 handleDependencyAttr(S, scope, D, Attr);
4081 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004082 case AttributeList::AT_Common:
4083 handleCommonAttr(S, D, Attr);
4084 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004085 case AttributeList::AT_CUDAConstant:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004086 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr);
4087 break;
4088 case AttributeList::AT_Constructor:
4089 handleConstructorAttr(S, D, Attr);
4090 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004091 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004092 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr);
4093 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004094 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004095 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004096 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004097 case AttributeList::AT_Destructor:
4098 handleDestructorAttr(S, D, Attr);
4099 break;
4100 case AttributeList::AT_EnableIf:
4101 handleEnableIfAttr(S, D, Attr);
4102 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004103 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004104 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004105 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004106 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004107 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004108 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00004109 case AttributeList::AT_OptimizeNone:
4110 handleOptimizeNoneAttr(S, D, Attr);
4111 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004112 case AttributeList::AT_Format:
4113 handleFormatAttr(S, D, Attr);
4114 break;
4115 case AttributeList::AT_FormatArg:
4116 handleFormatArgAttr(S, D, Attr);
4117 break;
4118 case AttributeList::AT_CUDAGlobal:
4119 handleGlobalAttr(S, D, Attr);
4120 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00004121 case AttributeList::AT_CUDADevice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004122 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr);
4123 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004124 case AttributeList::AT_CUDAHost:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004125 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr);
4126 break;
4127 case AttributeList::AT_GNUInline:
4128 handleGNUInlineAttr(S, D, Attr);
4129 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004130 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004131 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004132 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004133 case AttributeList::AT_Malloc:
4134 handleMallocAttr(S, D, Attr);
4135 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004136 case AttributeList::AT_MayAlias:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004137 handleSimpleAttribute<MayAliasAttr>(S, D, Attr);
4138 break;
4139 case AttributeList::AT_Mode:
4140 handleModeAttr(S, D, Attr);
4141 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004142 case AttributeList::AT_NoCommon:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004143 handleSimpleAttribute<NoCommonAttr>(S, D, Attr);
4144 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00004145 case AttributeList::AT_NonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004146 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
4147 handleNonNullAttrParameter(S, PVD, Attr);
4148 else
4149 handleNonNullAttr(S, D, Attr);
4150 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00004151 case AttributeList::AT_ReturnsNonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004152 handleReturnsNonNullAttr(S, D, Attr);
4153 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004154 case AttributeList::AT_Overloadable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004155 handleSimpleAttribute<OverloadableAttr>(S, D, Attr);
4156 break;
4157 case AttributeList::AT_Ownership:
4158 handleOwnershipAttr(S, D, Attr);
4159 break;
4160 case AttributeList::AT_Cold:
4161 handleColdAttr(S, D, Attr);
4162 break;
4163 case AttributeList::AT_Hot:
4164 handleHotAttr(S, D, Attr);
4165 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004166 case AttributeList::AT_Naked:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004167 handleSimpleAttribute<NakedAttr>(S, D, Attr);
4168 break;
4169 case AttributeList::AT_NoReturn:
4170 handleNoReturnAttr(S, D, Attr);
4171 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004172 case AttributeList::AT_NoThrow:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004173 handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
4174 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004175 case AttributeList::AT_CUDAShared:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004176 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr);
4177 break;
4178 case AttributeList::AT_VecReturn:
4179 handleVecReturnAttr(S, D, Attr);
4180 break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004181
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004182 case AttributeList::AT_ObjCOwnership:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004183 handleObjCOwnershipAttr(S, D, Attr);
4184 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004185 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004186 handleObjCPreciseLifetimeAttr(S, D, Attr);
4187 break;
John McCall31168b02011-06-15 23:02:42 +00004188
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004189 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004190 handleObjCReturnsInnerPointerAttr(S, D, Attr);
4191 break;
John McCallcf166702011-07-22 08:53:00 +00004192
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004193 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004194 handleObjCRequiresSuperAttr(S, D, Attr);
4195 break;
4196
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004197 case AttributeList::AT_ObjCBridge:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004198 handleObjCBridgeAttr(S, scope, D, Attr);
4199 break;
4200
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004201 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004202 handleObjCBridgeMutableAttr(S, scope, D, Attr);
4203 break;
4204
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004205 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004206 handleObjCBridgeRelatedAttr(S, scope, D, Attr);
4207 break;
John McCallf1e8b342011-09-29 07:17:38 +00004208
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004209 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004210 handleObjCDesignatedInitializer(S, D, Attr);
4211 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004212
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004213 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004214 handleCFAuditedTransferAttr(S, D, Attr);
4215 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004216 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004217 handleCFUnknownTransferAttr(S, D, Attr);
4218 break;
John McCall32f5fe12011-09-30 05:12:12 +00004219
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004220 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004221 case AttributeList::AT_NSConsumed:
4222 handleNSConsumedAttr(S, D, Attr);
4223 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004224 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004225 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr);
4226 break;
John McCalled433932011-01-25 03:31:58 +00004227
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004228 case AttributeList::AT_NSReturnsAutoreleased:
4229 case AttributeList::AT_NSReturnsNotRetained:
4230 case AttributeList::AT_CFReturnsNotRetained:
4231 case AttributeList::AT_NSReturnsRetained:
4232 case AttributeList::AT_CFReturnsRetained:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004233 handleNSReturnsRetainedAttr(S, D, Attr);
4234 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004235 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004236 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr);
4237 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004238 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004239 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr);
4240 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004241 case AttributeList::AT_VecTypeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004242 handleVecTypeHint(S, D, Attr);
4243 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004244
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004245 case AttributeList::AT_InitPriority:
4246 handleInitPriorityAttr(S, D, Attr);
4247 break;
4248
4249 case AttributeList::AT_Packed:
4250 handlePackedAttr(S, D, Attr);
4251 break;
4252 case AttributeList::AT_Section:
4253 handleSectionAttr(S, D, Attr);
4254 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004255 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004256 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004257 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004258 case AttributeList::AT_ArcWeakrefUnavailable:
4259 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr);
4260 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004261 case AttributeList::AT_ObjCRootClass:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004262 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr);
4263 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004264 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek438f8db2014-02-22 01:06:05 +00004265 handleObjCSuppresProtocolAttr(S, D, Attr);
Ted Kremenek28eace62013-11-23 01:01:34 +00004266 break;
4267 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004268 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr);
4269 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00004270 case AttributeList::AT_Unused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004271 handleSimpleAttribute<UnusedAttr>(S, D, Attr);
4272 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004273 case AttributeList::AT_ReturnsTwice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004274 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr);
4275 break;
4276 case AttributeList::AT_Used:
4277 handleUsedAttr(S, D, Attr);
4278 break;
John McCalld041a9b2013-02-20 01:54:26 +00004279 case AttributeList::AT_Visibility:
4280 handleVisibilityAttr(S, D, Attr, false);
4281 break;
4282 case AttributeList::AT_TypeVisibility:
4283 handleVisibilityAttr(S, D, Attr, true);
4284 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004285 case AttributeList::AT_WarnUnused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004286 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr);
4287 break;
4288 case AttributeList::AT_WarnUnusedResult:
4289 handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004290 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004291 case AttributeList::AT_Weak:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004292 handleSimpleAttribute<WeakAttr>(S, D, Attr);
4293 break;
4294 case AttributeList::AT_WeakRef:
4295 handleWeakRefAttr(S, D, Attr);
4296 break;
4297 case AttributeList::AT_WeakImport:
4298 handleWeakImportAttr(S, D, Attr);
4299 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004300 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004301 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004302 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004303 case AttributeList::AT_ObjCException:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004304 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr);
4305 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004306 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004307 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004308 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004309 case AttributeList::AT_ObjCNSObject:
4310 handleObjCNSObject(S, D, Attr);
4311 break;
4312 case AttributeList::AT_Blocks:
4313 handleBlocksAttr(S, D, Attr);
4314 break;
4315 case AttributeList::AT_Sentinel:
4316 handleSentinelAttr(S, D, Attr);
4317 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004318 case AttributeList::AT_Const:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004319 handleSimpleAttribute<ConstAttr>(S, D, Attr);
4320 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004321 case AttributeList::AT_Pure:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004322 handleSimpleAttribute<PureAttr>(S, D, Attr);
4323 break;
4324 case AttributeList::AT_Cleanup:
4325 handleCleanupAttr(S, D, Attr);
4326 break;
4327 case AttributeList::AT_NoDebug:
4328 handleNoDebugAttr(S, D, Attr);
4329 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00004330 case AttributeList::AT_NoDuplicate:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004331 handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
4332 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004333 case AttributeList::AT_NoInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004334 handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
4335 break;
4336 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
4337 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr);
4338 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004339 case AttributeList::AT_StdCall:
4340 case AttributeList::AT_CDecl:
4341 case AttributeList::AT_FastCall:
4342 case AttributeList::AT_ThisCall:
4343 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004344 case AttributeList::AT_MSABI:
4345 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004346 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004347 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004348 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004349 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004350 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004351 case AttributeList::AT_OpenCLKernel:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004352 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr);
4353 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004354 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004355 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr);
4356 break;
John McCall8d32c052012-05-22 21:28:12 +00004357
4358 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004359 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004360 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004361 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004362 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004363 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004364 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004365 case AttributeList::AT_MSInheritance:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004366 handleMSInheritanceAttr(S, D, Attr);
4367 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004368 case AttributeList::AT_SelectAny:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004369 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr);
4370 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004371
4372 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004373 case AttributeList::AT_AssertExclusiveLock:
4374 handleAssertExclusiveLockAttr(S, D, Attr);
4375 break;
4376 case AttributeList::AT_AssertSharedLock:
4377 handleAssertSharedLockAttr(S, D, Attr);
4378 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004379 case AttributeList::AT_GuardedVar:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004380 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr);
4381 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004382 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004383 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004384 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004385 case AttributeList::AT_ScopedLockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004386 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr);
4387 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004388 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004389 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004390 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004391 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004392 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004393 break;
4394 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004395 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004396 break;
4397 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004398 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004399 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004400 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004401 handleGuardedByAttr(S, D, Attr);
4402 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004403 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004404 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004405 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004406 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004407 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004408 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004409 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004410 handleLockReturnedAttr(S, D, Attr);
4411 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004412 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004413 handleLocksExcludedAttr(S, D, Attr);
4414 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004415 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004416 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004417 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004418 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004419 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004420 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004421 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004422 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004423 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004424
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004425 // Capability analysis attributes.
4426 case AttributeList::AT_Capability:
4427 case AttributeList::AT_Lockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004428 handleCapabilityAttr(S, D, Attr);
4429 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004430 case AttributeList::AT_RequiresCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004431 handleRequiresCapabilityAttr(S, D, Attr);
4432 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004433
4434 case AttributeList::AT_AssertCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004435 handleAssertCapabilityAttr(S, D, Attr);
4436 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004437 case AttributeList::AT_AcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004438 handleAcquireCapabilityAttr(S, D, Attr);
4439 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004440 case AttributeList::AT_ReleaseCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004441 handleReleaseCapabilityAttr(S, D, Attr);
4442 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004443 case AttributeList::AT_TryAcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004444 handleTryAcquireCapabilityAttr(S, D, Attr);
4445 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004446
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004447 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004448 case AttributeList::AT_Consumable:
4449 handleConsumableAttr(S, D, Attr);
4450 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004451 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004452 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr);
4453 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004454 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004455 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr);
4456 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004457 case AttributeList::AT_CallableWhen:
4458 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004459 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004460 case AttributeList::AT_ParamTypestate:
4461 handleParamTypestateAttr(S, D, Attr);
4462 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004463 case AttributeList::AT_ReturnTypestate:
4464 handleReturnTypestateAttr(S, D, Attr);
4465 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004466 case AttributeList::AT_SetTypestate:
4467 handleSetTypestateAttr(S, D, Attr);
4468 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004469 case AttributeList::AT_TestTypestate:
4470 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004471 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004472
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004473 // Type safety attributes.
4474 case AttributeList::AT_ArgumentWithTypeTag:
4475 handleArgumentWithTypeTagAttr(S, D, Attr);
4476 break;
4477 case AttributeList::AT_TypeTagForDatatype:
4478 handleTypeTagForDatatypeAttr(S, D, Attr);
4479 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004480 }
4481}
4482
4483/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4484/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004485void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004486 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004487 bool IncludeCXX11Attributes) {
4488 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004489 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004490
Joey Gouly2cd9db12013-12-13 16:15:28 +00004491 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004492 // GCC accepts
4493 // static int a9 __attribute__((weakref));
4494 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004495 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00004496 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
4497 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00004498 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004499 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004500 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004501
4502 if (!D->hasAttr<OpenCLKernelAttr>()) {
4503 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004504 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
4505 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004506 D->setInvalidDecl();
4507 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004508 if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
4509 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004510 D->setInvalidDecl();
4511 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004512 if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
4513 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004514 D->setInvalidDecl();
4515 }
4516 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004517}
4518
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004519// Annotation attributes are the only attributes allowed after an access
4520// specifier.
4521bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4522 const AttributeList *AttrList) {
4523 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004524 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004525 handleAnnotateAttr(*this, ASDecl, *l);
4526 } else {
4527 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4528 return true;
4529 }
4530 }
4531
4532 return false;
4533}
4534
John McCall42856de2011-10-01 05:17:03 +00004535/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4536/// contains any decl attributes that we should warn about.
4537static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4538 for ( ; A; A = A->getNext()) {
4539 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004540 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004541 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4542
4543 if (A->getKind() == AttributeList::UnknownAttribute) {
4544 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4545 << A->getName() << A->getRange();
4546 } else {
4547 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4548 << A->getName() << A->getRange();
4549 }
4550 }
4551}
4552
4553/// checkUnusedDeclAttributes - Given a declarator which is not being
4554/// used to build a declaration, complain about any decl attributes
4555/// which might be lying around on it.
4556void Sema::checkUnusedDeclAttributes(Declarator &D) {
4557 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4558 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4559 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4560 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4561}
4562
Ryan Flynn7d470f32009-07-30 03:15:39 +00004563/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004564/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004565NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4566 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004567 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004568 NamedDecl *NewD = 0;
4569 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004570 FunctionDecl *NewFD;
4571 // FIXME: Missing call to CheckFunctionDeclaration().
4572 // FIXME: Mangling?
4573 // FIXME: Is the qualifier info correct?
4574 // FIXME: Is the DeclContext correct?
4575 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4576 Loc, Loc, DeclarationName(II),
4577 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004578 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004579 FD->hasPrototype(),
4580 false/*isConstexprSpecified*/);
4581 NewD = NewFD;
4582
4583 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004584 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004585
4586 // Fake up parameter variables; they are declared as if this were
4587 // a typedef.
4588 QualType FDTy = FD->getType();
4589 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4590 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00004591 for (const auto &AI : FT->param_types()) {
4592 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00004593 Param->setScopeInfo(0, Params.size());
4594 Params.push_back(Param);
4595 }
David Blaikie9c70e042011-09-21 18:16:56 +00004596 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004597 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004598 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4599 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004600 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004601 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004602 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004603 if (VD->getQualifier()) {
4604 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004605 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004606 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004607 }
4608 return NewD;
4609}
4610
James Dennett634962f2012-06-14 21:40:34 +00004611/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004612/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004613void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004614 if (W.getUsed()) return; // only do this once
4615 W.setUsed(true);
4616 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4617 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004618 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00004619 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
4620 W.getLocation()));
4621 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00004622 WeakTopLevelDecl.push_back(NewD);
4623 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4624 // to insert Decl at TU scope, sorry.
4625 DeclContext *SavedContext = CurContext;
4626 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00004627 NewD->setDeclContext(CurContext);
4628 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00004629 PushOnScopeChains(NewD, S);
4630 CurContext = SavedContext;
4631 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00004632 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004633 }
4634}
4635
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004636void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4637 // It's valid to "forward-declare" #pragma weak, in which case we
4638 // have to do this.
4639 LoadExternalWeakUndeclaredIdentifiers();
4640 if (!WeakUndeclaredIdentifiers.empty()) {
4641 NamedDecl *ND = NULL;
4642 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4643 if (VD->isExternC())
4644 ND = VD;
4645 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4646 if (FD->isExternC())
4647 ND = FD;
4648 if (ND) {
4649 if (IdentifierInfo *Id = ND->getIdentifier()) {
4650 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4651 = WeakUndeclaredIdentifiers.find(Id);
4652 if (I != WeakUndeclaredIdentifiers.end()) {
4653 WeakInfo W = I->second;
4654 DeclApplyPragmaWeak(S, ND, W);
4655 WeakUndeclaredIdentifiers[Id] = W;
4656 }
4657 }
4658 }
4659 }
4660}
4661
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004662/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4663/// it, apply them to D. This is a bit tricky because PD can have attributes
4664/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004665void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004666 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004667 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004668 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004669
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004670 // Walk the declarator structure, applying decl attributes that were in a type
4671 // position to the decl itself. This handles cases like:
4672 // int *__attr__(x)** D;
4673 // when X is a decl attribute.
4674 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4675 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004676 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004677
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004678 // Finally, apply any attributes on the decl itself.
4679 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004680 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004681}
John McCall28a6aea2009-11-04 02:18:39 +00004682
John McCall31168b02011-06-15 23:02:42 +00004683/// Is the given declaration allowed to use a forbidden type?
4684static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4685 // Private ivars are always okay. Unfortunately, people don't
4686 // always properly make their ivars private, even in system headers.
4687 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004688 // Function declarations in sys headers will be marked unavailable.
4689 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4690 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004691 return false;
4692
4693 // Require it to be declared in a system header.
4694 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4695}
4696
4697/// Handle a delayed forbidden-type diagnostic.
4698static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4699 Decl *decl) {
4700 if (decl && isForbiddenTypeAllowed(S, decl)) {
Aaron Ballman36a53502014-01-16 13:03:14 +00004701 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context,
4702 "this system declaration uses an unsupported type",
4703 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00004704 return;
4705 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004706 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004707 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004708 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004709 // kind of forbidden type messages on unavailable functions.
4710 if (FD->hasAttr<UnavailableAttr>() &&
4711 diag.getForbiddenTypeDiagnostic() ==
4712 diag::err_arc_array_param_no_ownership) {
4713 diag.Triggered = true;
4714 return;
4715 }
4716 }
John McCall31168b02011-06-15 23:02:42 +00004717
4718 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4719 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4720 diag.Triggered = true;
4721}
4722
John McCall2ec85372012-05-07 06:16:41 +00004723void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4724 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004725 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004726 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004727
John McCall2ec85372012-05-07 06:16:41 +00004728 // When delaying diagnostics to run in the context of a parsed
4729 // declaration, we only want to actually emit anything if parsing
4730 // succeeds.
4731 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004732
John McCall2ec85372012-05-07 06:16:41 +00004733 // We emit all the active diagnostics in this pool or any of its
4734 // parents. In general, we'll get one pool for the decl spec
4735 // and a child pool for each declarator; in a decl group like:
4736 // deprecated_typedef foo, *bar, baz();
4737 // only the declarator pops will be passed decls. This is correct;
4738 // we really do need to consider delayed diagnostics from the decl spec
4739 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004740 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004741 do {
John McCall6347b682012-05-07 06:16:58 +00004742 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004743 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4744 // This const_cast is a bit lame. Really, Triggered should be mutable.
4745 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004746 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004747 continue;
4748
John McCallc1465822011-02-14 07:13:47 +00004749 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004750 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004751 case DelayedDiagnostic::Unavailable:
4752 // Don't bother giving deprecation/unavailable diagnostics if
4753 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004754 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004755 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004756 break;
4757
4758 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004759 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004760 break;
John McCall31168b02011-06-15 23:02:42 +00004761
4762 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004763 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004764 break;
John McCall86121512010-01-27 03:50:35 +00004765 }
4766 }
John McCall2ec85372012-05-07 06:16:41 +00004767 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004768}
4769
John McCall6347b682012-05-07 06:16:58 +00004770/// Given a set of delayed diagnostics, re-emit them as if they had
4771/// been delayed in the current context instead of in the given pool.
4772/// Essentially, this just moves them to the current pool.
4773void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4774 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4775 assert(curPool && "re-emitting in undelayed context not supported");
4776 curPool->steal(pool);
4777}
4778
John McCall28a6aea2009-11-04 02:18:39 +00004779static bool isDeclDeprecated(Decl *D) {
4780 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004781 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004782 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004783 // A category implicitly has the availability of the interface.
4784 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4785 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004786 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4787 return false;
4788}
4789
Ted Kremenekb79ee572013-12-18 23:30:06 +00004790static bool isDeclUnavailable(Decl *D) {
4791 do {
4792 if (D->isUnavailable())
4793 return true;
4794 // A category implicitly has the availability of the interface.
4795 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4796 return CatD->getClassInterface()->isUnavailable();
4797 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4798 return false;
4799}
4800
Eli Friedman971bfa12012-08-08 21:52:41 +00004801static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004802DoEmitAvailabilityWarning(Sema &S,
4803 DelayedDiagnostic::DDKind K,
4804 Decl *Ctx,
4805 const NamedDecl *D,
4806 StringRef Message,
4807 SourceLocation Loc,
4808 const ObjCInterfaceDecl *UnknownObjCClass,
4809 const ObjCPropertyDecl *ObjCProperty) {
4810
4811 // Diagnostics for deprecated or unavailable.
4812 unsigned diag, diag_message, diag_fwdclass_message;
4813
4814 // Matches 'diag::note_property_attribute' options.
4815 unsigned property_note_select;
4816
4817 // Matches diag::note_availability_specified_here.
4818 unsigned available_here_select_kind;
4819
4820 // Don't warn if our current context is deprecated or unavailable.
4821 switch (K) {
4822 case DelayedDiagnostic::Deprecation:
4823 if (isDeclDeprecated(Ctx))
4824 return;
4825 diag = diag::warn_deprecated;
4826 diag_message = diag::warn_deprecated_message;
4827 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4828 property_note_select = /* deprecated */ 0;
4829 available_here_select_kind = /* deprecated */ 2;
4830 break;
4831
4832 case DelayedDiagnostic::Unavailable:
4833 if (isDeclUnavailable(Ctx))
4834 return;
4835 diag = diag::err_unavailable;
4836 diag_message = diag::err_unavailable_message;
4837 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4838 property_note_select = /* unavailable */ 1;
4839 available_here_select_kind = /* unavailable */ 0;
4840 break;
4841
4842 default:
4843 llvm_unreachable("Neither a deprecation or unavailable kind");
4844 }
4845
Eli Friedman971bfa12012-08-08 21:52:41 +00004846 DeclarationName Name = D->getDeclName();
4847 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004848 S.Diag(Loc, diag_message) << Name << Message;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004849 if (ObjCProperty)
4850 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4851 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004852 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004853 S.Diag(Loc, diag) << Name;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004854 if (ObjCProperty)
4855 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4856 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004857 } else {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004858 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004859 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4860 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004861
4862 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4863 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004864}
4865
Ted Kremenekb79ee572013-12-18 23:30:06 +00004866void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4867 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004868 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004869 DoEmitAvailabilityWarning(*this,
4870 (DelayedDiagnostic::DDKind) DD.Kind,
4871 Ctx,
4872 DD.getDeprecationDecl(),
4873 DD.getDeprecationMessage(),
4874 DD.Loc,
4875 DD.getUnknownObjCClass(),
4876 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004877}
4878
Ted Kremenekb79ee572013-12-18 23:30:06 +00004879void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4880 NamedDecl *D, StringRef Message,
4881 SourceLocation Loc,
4882 const ObjCInterfaceDecl *UnknownObjCClass,
4883 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004884 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004885 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004886 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4887 UnknownObjCClass,
4888 ObjCProperty,
4889 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004890 return;
4891 }
4892
Ted Kremenekb79ee572013-12-18 23:30:06 +00004893 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4894 DelayedDiagnostic::DDKind K;
4895 switch (AD) {
4896 case AD_Deprecation:
4897 K = DelayedDiagnostic::Deprecation;
4898 break;
4899 case AD_Unavailable:
4900 K = DelayedDiagnostic::Unavailable;
4901 break;
4902 }
4903
4904 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
4905 UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004906}