blob: 66591e4bf36035177f9b9cc9a923bde65bbd7538 [file] [log] [blame]
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000015#include "clang/AST/ASTContext.h"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000016#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000020#include "clang/AST/Expr.h"
Rafael Espindoladb77c4a2013-02-26 19:13:56 +000021#include "clang/AST/Mangle.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000022#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000023#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000024#include "clang/Basic/TargetInfo.h"
Benjamin Kramer6ee15622013-09-13 15:35:43 +000025#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000026#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000027#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000028#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000029#include "clang/Sema/Scope.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000030#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000031using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000032using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000033
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000034namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000035 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000036 C,
37 Cpp,
38 ObjC
39 };
40}
41
Chris Lattner58418ff2008-06-29 00:16:31 +000042//===----------------------------------------------------------------------===//
43// Helper functions
44//===----------------------------------------------------------------------===//
45
Ted Kremenek527042b2009-08-14 20:49:40 +000046/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000047/// type (function or function-typed variable) or an Objective-C
48/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000049static bool isFunctionOrMethod(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000050 return (D->getFunctionType() != NULL) || isa<ObjCMethodDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000051}
52
John McCall3882ace2011-01-05 12:14:39 +000053/// Return true if the given decl has a declarator that should have
54/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000055static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000056 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000057 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
58 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +000059}
60
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000061/// hasFunctionProto - Return true if the given decl has a argument
62/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000063/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000064static bool hasFunctionProto(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000065 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000066 return isa<FunctionProtoType>(FnTy);
Aaron Ballman12b9f652014-01-16 13:55:42 +000067 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000068}
69
Alp Toker601b22c2014-01-21 23:35:24 +000070/// getFunctionOrMethodNumParams - Return number of function or method
71/// parameters. It is an error to call this on a K&R function (use
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000072/// hasFunctionProto first).
Alp Toker601b22c2014-01-21 23:35:24 +000073static unsigned getFunctionOrMethodNumParams(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000074 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000075 return cast<FunctionProtoType>(FnTy)->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000076 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000077 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000078 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000079}
80
Alp Toker601b22c2014-01-21 23:35:24 +000081static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000082 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000083 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +000084 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000085 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +000086
Chandler Carruthff4c4f02011-07-01 23:49:12 +000087 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000088}
89
Chandler Carruthff4c4f02011-07-01 23:49:12 +000090static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000091 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker314cc812014-01-25 16:55:45 +000092 return cast<FunctionProtoType>(FnTy)->getReturnType();
93 return cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +000094}
95
Chandler Carruthff4c4f02011-07-01 23:49:12 +000096static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000097 if (const FunctionType *FnTy = D->getFunctionType()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +000098 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000099 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000100 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000101 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000102 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000103 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000104 }
105}
106
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000107static bool isInstanceMethod(const Decl *D) {
108 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000109 return MethodDecl->isInstance();
110 return false;
111}
112
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000113static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000114 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000115 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000116 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000117
John McCall96fa4842010-05-17 21:00:27 +0000118 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
119 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000120 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000121
John McCall96fa4842010-05-17 21:00:27 +0000122 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000123
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000124 // FIXME: Should we walk the chain of classes?
125 return ClsName == &Ctx.Idents.get("NSString") ||
126 ClsName == &Ctx.Idents.get("NSMutableString");
127}
128
Daniel Dunbar980c6692008-09-26 03:32:58 +0000129static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000130 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000131 if (!PT)
132 return false;
133
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000134 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000135 if (!RT)
136 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000137
Daniel Dunbar980c6692008-09-26 03:32:58 +0000138 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000139 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000140 return false;
141
142 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
143}
144
Richard Smithb87c4652013-10-31 21:23:20 +0000145static unsigned getNumAttributeArgs(const AttributeList &Attr) {
146 // FIXME: Include the type in the argument list.
147 return Attr.getNumArgs() + Attr.hasParsedType();
148}
149
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000150/// \brief Check if the attribute has exactly as many args as Num. May
151/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000152static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000153 unsigned Num) {
154 if (getNumAttributeArgs(Attr) != Num) {
Aaron Ballmanb7243382013-07-23 19:30:11 +0000155 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
156 << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000157 return false;
158 }
159
160 return true;
161}
162
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000163/// \brief Check if the attribute has at least as many args as Num. May
164/// output an error.
165static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000166 unsigned Num) {
167 if (getNumAttributeArgs(Attr) < Num) {
Aaron Ballman05e420a2014-01-02 21:26:14 +0000168 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments)
169 << Attr.getName() << Num;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000170 return false;
171 }
172
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000173 return true;
174}
175
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000176/// \brief If Expr is a valid integer constant, get the value of the integer
177/// expression and return success or failure. May output an error.
178static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
179 const Expr *Expr, uint32_t &Val,
180 unsigned Idx = UINT_MAX) {
181 llvm::APSInt I(32);
182 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
183 !Expr->isIntegerConstantExpr(I, S.Context)) {
184 if (Idx != UINT_MAX)
185 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
186 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
187 << Expr->getSourceRange();
188 else
189 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
190 << Attr.getName() << AANT_ArgumentIntegerConstant
191 << Expr->getSourceRange();
192 return false;
193 }
194 Val = (uint32_t)I.getZExtValue();
195 return true;
196}
197
Aaron Ballmanfb763042013-12-02 18:05:46 +0000198/// \brief Diagnose mutually exclusive attributes when present on a given
199/// declaration. Returns true if diagnosed.
200template <typename AttrTy>
201static bool checkAttrMutualExclusion(Sema &S, Decl *D,
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000202 const AttributeList &Attr) {
203 if (AttrTy *A = D->getAttr<AttrTy>()) {
Aaron Ballmanfb763042013-12-02 18:05:46 +0000204 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000205 << Attr.getName() << A;
Aaron Ballmanfb763042013-12-02 18:05:46 +0000206 return true;
207 }
208 return false;
209}
210
Alp Toker601b22c2014-01-21 23:35:24 +0000211/// \brief Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000212/// instance method D. May output an error.
213///
214/// \returns true if IdxExpr is a valid index.
Alp Toker601b22c2014-01-21 23:35:24 +0000215static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D,
216 const AttributeList &Attr,
217 unsigned AttrArgNum,
218 const Expr *IdxExpr,
219 uint64_t &Idx) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000220 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000221
222 // In C++ the implicit 'this' function parameter also counts.
223 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000224 bool HP = hasFunctionProto(D);
225 bool HasImplicitThisParam = isInstanceMethod(D);
226 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000227 unsigned NumParams =
228 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000229
230 llvm::APSInt IdxInt;
231 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
232 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000233 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
234 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
235 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000236 return false;
237 }
238
239 Idx = IdxInt.getLimitedValue();
Alp Toker601b22c2014-01-21 23:35:24 +0000240 if (Idx < 1 || (!IV && Idx > NumParams)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000241 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
242 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000243 return false;
244 }
245 Idx--; // Convert to zero-based.
246 if (HasImplicitThisParam) {
247 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000248 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000249 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000250 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000251 return false;
252 }
253 --Idx;
254 }
255
256 return true;
257}
258
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000259/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
260/// If not emit an error and return false. If the argument is an identifier it
261/// will emit an error with a fixit hint and treat it as if it was a string
262/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000263bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
264 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000265 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000266 // Look for identifiers. If we have one emit a hint to fix it to a literal.
267 if (Attr.isArgIdent(ArgNum)) {
268 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000269 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000270 << Attr.getName() << AANT_ArgumentString
271 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000272 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000273 Str = Loc->Ident->getName();
274 if (ArgLocation)
275 *ArgLocation = Loc->Loc;
276 return true;
277 }
278
279 // Now check for an actual string literal.
280 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
281 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
282 if (ArgLocation)
283 *ArgLocation = ArgExpr->getLocStart();
284
285 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000286 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000287 << Attr.getName() << AANT_ArgumentString;
288 return false;
289 }
290
291 Str = Literal->getString();
292 return true;
293}
294
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000295/// \brief Applies the given attribute to the Decl without performing any
296/// additional semantic checking.
297template <typename AttrType>
298static void handleSimpleAttribute(Sema &S, Decl *D,
299 const AttributeList &Attr) {
300 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
301 Attr.getAttributeSpellingListIndex()));
302}
303
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000304/// \brief Check if the passed-in expression is of type int or bool.
305static bool isIntOrBool(Expr *Exp) {
306 QualType QT = Exp->getType();
307 return QT->isBooleanType() || QT->isIntegerType();
308}
309
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000310
311// Check to see if the type is a smart pointer of some kind. We assume
312// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000313static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
314 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
315 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000316 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000317 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000318
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000319 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
320 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000321 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000322 return false;
323
324 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000325}
326
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000327/// \brief Check if passed in Decl is a pointer type.
328/// Note that this function may produce an error message.
329/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000330static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
331 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000332 const ValueDecl *vd = cast<ValueDecl>(D);
333 QualType QT = vd->getType();
334 if (QT->isAnyPointerType())
335 return true;
336
337 if (const RecordType *RT = QT->getAs<RecordType>()) {
338 // If it's an incomplete type, it could be a smart pointer; skip it.
339 // (We don't want to force template instantiation if we can avoid it,
340 // since that would alter the order in which templates are instantiated.)
341 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000342 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000343
Aaron Ballman553e6812013-12-26 14:54:11 +0000344 if (threadSafetyCheckIsSmartPointer(S, RT))
345 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000346 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000347
348 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000349 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000350 return false;
351}
352
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000353/// \brief Checks that the passed in QualType either is of RecordType or points
354/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000355static const RecordType *getRecordType(QualType QT) {
356 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000357 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000358
359 // Now check if we point to record type.
360 if (const PointerType *PT = QT->getAs<PointerType>())
361 return PT->getPointeeType()->getAs<RecordType>();
362
363 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000364}
365
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000366
Jordy Rose740b0c22012-05-08 03:27:22 +0000367static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
368 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000369 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
Aaron Ballman9ead1242013-12-19 02:39:40 +0000370 return RT->getDecl()->hasAttr<LockableAttr>();
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000371}
372
373
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000374/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000375/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000376static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
377 QualType Ty) {
378 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000379
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000380 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000381 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000382 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
Aaron Ballman1da282a2014-01-02 23:15:58 +0000383 << Attr.getName() << Ty;
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000384 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000385 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000386
Michael Hana9171bc2012-08-03 17:40:43 +0000387 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000388 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000389 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000390
391 // Allow smart pointers to be used as lockable objects.
392 // FIXME -- Check the type that the smart pointer points to.
393 if (threadSafetyCheckIsSmartPointer(S, RT))
394 return;
395
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000396 // Check if the type is lockable.
397 RecordDecl *RD = RT->getDecl();
Aaron Ballman9ead1242013-12-19 02:39:40 +0000398 if (RD->hasAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000399 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000400
401 // Else check if any base classes are lockable.
402 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
403 CXXBasePaths BPaths(false, false);
404 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
405 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000406 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000407
408 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
Aaron Ballman1da282a2014-01-02 23:15:58 +0000409 << Attr.getName() << Ty;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000410}
411
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000412/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000413/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000414/// \param Sidx The attribute argument index to start checking with.
415/// \param ParamIdxOk Whether an argument can be indexing into a function
416/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000417static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000418 const AttributeList &Attr,
419 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000420 int Sidx = 0,
421 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000422 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000423 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000424
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000425 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000426 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000427 Args.push_back(ArgExp);
428 continue;
429 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000430
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000431 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000432 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000433 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000434 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000435 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000436 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000437 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000438 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000439
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000440 // We allow constant strings to be used as a placeholder for expressions
441 // that are not valid C++ syntax, but warn that they are ignored.
442 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
443 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000444 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000445 continue;
446 }
447
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000448 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000449
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000450 // A pointer to member expression of the form &MyClass::mu is treated
451 // specially -- we need to look at the type of the member.
452 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
453 if (UOp->getOpcode() == UO_AddrOf)
454 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
455 if (DRE->getDecl()->isCXXInstanceMember())
456 ArgTy = DRE->getDecl()->getType();
457
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000458 // First see if we can just cast to record type, or point to record type.
459 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000460
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000461 // Now check if we index into a record type function param.
462 if(!RT && ParamIdxOk) {
463 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000464 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
465 if(FD && IL) {
466 unsigned int NumParams = FD->getNumParams();
467 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000468 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
469 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
470 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000471 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
472 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000473 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000474 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000475 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000476 }
477 }
478
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000479 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000480
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000481 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000482 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000483}
484
Chris Lattner58418ff2008-06-29 00:16:31 +0000485//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000486// Attribute Implementations
487//===----------------------------------------------------------------------===//
488
Daniel Dunbar032db472008-07-31 22:40:48 +0000489// FIXME: All this manual attribute parsing code is gross. At the
490// least add some helper functions to check most argument patterns (#
491// and types of args).
492
Michael Hana9171bc2012-08-03 17:40:43 +0000493static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000494 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000495 if (!threadSafetyCheckIsPointer(S, D, Attr))
496 return;
497
Michael Han99315932013-01-24 16:46:58 +0000498 D->addAttr(::new (S.Context)
499 PtGuardedVarAttr(Attr.getRange(), S.Context,
500 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000501}
502
Michael Hana9171bc2012-08-03 17:40:43 +0000503static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
504 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000505 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000506 SmallVector<Expr*, 1> Args;
507 // check that all arguments are lockable objects
508 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
509 unsigned Size = Args.size();
510 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000511 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000512
Michael Han3be3b442012-07-23 18:48:41 +0000513 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000514
Michael Han3be3b442012-07-23 18:48:41 +0000515 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000516}
517
Michael Han3be3b442012-07-23 18:48:41 +0000518static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
519 Expr *Arg = 0;
520 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
521 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000522
Aaron Ballman36a53502014-01-16 13:03:14 +0000523 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg,
524 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000525}
526
Michael Hana9171bc2012-08-03 17:40:43 +0000527static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000528 const AttributeList &Attr) {
529 Expr *Arg = 0;
530 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
531 return;
532
533 if (!threadSafetyCheckIsPointer(S, D, Attr))
534 return;
535
536 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +0000537 S.Context, Arg,
538 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000539}
540
Michael Hana9171bc2012-08-03 17:40:43 +0000541static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
542 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000543 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000544 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000545 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000546
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000547 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000548 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000549 if (!QT->isDependentType()) {
550 const RecordType *RT = getRecordType(QT);
Aaron Ballman9ead1242013-12-19 02:39:40 +0000551 if (!RT || !RT->getDecl()->hasAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000552 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000553 << Attr.getName();
554 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000555 }
556 }
557
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000558 // Check that all arguments are lockable objects.
559 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000560 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000561 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000562
Michael Han3be3b442012-07-23 18:48:41 +0000563 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000564}
565
Michael Hana9171bc2012-08-03 17:40:43 +0000566static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000567 const AttributeList &Attr) {
568 SmallVector<Expr*, 1> Args;
569 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
570 return;
571
572 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000573 D->addAttr(::new (S.Context)
574 AcquiredAfterAttr(Attr.getRange(), S.Context,
575 StartArg, Args.size(),
576 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000577}
578
Michael Hana9171bc2012-08-03 17:40:43 +0000579static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000580 const AttributeList &Attr) {
581 SmallVector<Expr*, 1> Args;
582 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
583 return;
584
585 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000586 D->addAttr(::new (S.Context)
587 AcquiredBeforeAttr(Attr.getRange(), S.Context,
588 StartArg, Args.size(),
589 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000590}
591
Michael Hana9171bc2012-08-03 17:40:43 +0000592static bool checkLockFunAttrCommon(Sema &S, Decl *D,
593 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000594 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000595 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000596 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000597 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000598
Michael Han3be3b442012-07-23 18:48:41 +0000599 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000600}
601
Michael Hana9171bc2012-08-03 17:40:43 +0000602static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000603 const AttributeList &Attr) {
604 SmallVector<Expr*, 1> Args;
605 if (!checkLockFunAttrCommon(S, D, Attr, Args))
606 return;
607
608 unsigned Size = Args.size();
609 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000610 D->addAttr(::new (S.Context)
611 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
612 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000613}
614
Michael Hana9171bc2012-08-03 17:40:43 +0000615static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000616 const AttributeList &Attr) {
617 SmallVector<Expr*, 1> Args;
618 if (!checkLockFunAttrCommon(S, D, Attr, Args))
619 return;
620
621 unsigned Size = Args.size();
622 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000623 D->addAttr(::new (S.Context)
624 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
625 StartArg, Size,
626 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000627}
628
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000629static void handleAssertSharedLockAttr(Sema &S, Decl *D,
630 const AttributeList &Attr) {
631 SmallVector<Expr*, 1> Args;
632 if (!checkLockFunAttrCommon(S, D, Attr, Args))
633 return;
634
635 unsigned Size = Args.size();
636 Expr **StartArg = Size == 0 ? 0 : &Args[0];
637 D->addAttr(::new (S.Context)
638 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
639 Attr.getAttributeSpellingListIndex()));
640}
641
642static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
643 const AttributeList &Attr) {
644 SmallVector<Expr*, 1> Args;
645 if (!checkLockFunAttrCommon(S, D, Attr, Args))
646 return;
647
648 unsigned Size = Args.size();
649 Expr **StartArg = Size == 0 ? 0 : &Args[0];
650 D->addAttr(::new (S.Context)
651 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
652 StartArg, Size,
653 Attr.getAttributeSpellingListIndex()));
654}
655
656
Michael Hana9171bc2012-08-03 17:40:43 +0000657static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
658 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000659 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000660 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000661 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000662
Aaron Ballman00e99962013-08-31 01:11:41 +0000663 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000664 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000665 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000666 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000667 }
668
669 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000670 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000671
Michael Han3be3b442012-07-23 18:48:41 +0000672 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000673}
674
Michael Hana9171bc2012-08-03 17:40:43 +0000675static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000676 const AttributeList &Attr) {
677 SmallVector<Expr*, 2> Args;
678 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
679 return;
680
Michael Han99315932013-01-24 16:46:58 +0000681 D->addAttr(::new (S.Context)
682 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000683 Attr.getArgAsExpr(0),
684 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000685 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000686}
687
Michael Hana9171bc2012-08-03 17:40:43 +0000688static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000689 const AttributeList &Attr) {
690 SmallVector<Expr*, 2> Args;
691 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
692 return;
693
Michael Han99315932013-01-24 16:46:58 +0000694 D->addAttr(::new (S.Context)
695 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000696 Attr.getArgAsExpr(0),
697 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000698 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000699}
700
Michael Hana9171bc2012-08-03 17:40:43 +0000701static bool checkLocksRequiredCommon(Sema &S, Decl *D,
702 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000703 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000704 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000705 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000706
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000707 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000708 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000709 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000710 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000711
Michael Han3be3b442012-07-23 18:48:41 +0000712 return true;
713}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000714
Michael Hana9171bc2012-08-03 17:40:43 +0000715static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000716 const AttributeList &Attr) {
717 SmallVector<Expr*, 1> Args;
718 if (!checkLocksRequiredCommon(S, D, Attr, Args))
719 return;
720
721 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000722 D->addAttr(::new (S.Context)
723 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
724 StartArg, Args.size(),
725 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000726}
727
Michael Hana9171bc2012-08-03 17:40:43 +0000728static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000729 const AttributeList &Attr) {
730 SmallVector<Expr*, 1> Args;
731 if (!checkLocksRequiredCommon(S, D, Attr, Args))
732 return;
733
734 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000735 D->addAttr(::new (S.Context)
736 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
737 StartArg, Args.size(),
738 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000739}
740
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000741static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000742 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000743 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000744 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000745 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000746 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000747 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000748 Expr **StartArg = Size == 0 ? 0 : &Args[0];
749
Michael Han99315932013-01-24 16:46:58 +0000750 D->addAttr(::new (S.Context)
751 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
752 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000753}
754
755static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000756 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000757 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000758 SmallVector<Expr*, 1> Args;
759 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
760 unsigned Size = Args.size();
761 if (Size == 0)
762 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000763
Michael Han99315932013-01-24 16:46:58 +0000764 D->addAttr(::new (S.Context)
765 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
766 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000767}
768
769static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000770 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000771 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000772 return;
773
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000774 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000775 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000776 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000777 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000778 if (Size == 0)
779 return;
780 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000781
Michael Han99315932013-01-24 16:46:58 +0000782 D->addAttr(::new (S.Context)
783 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
784 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000785}
786
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000787static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
788 Expr *Cond = Attr.getArgAsExpr(0);
789 if (!Cond->isTypeDependent()) {
790 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
791 if (Converted.isInvalid())
792 return;
793 Cond = Converted.take();
794 }
795
796 StringRef Msg;
797 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
798 return;
799
800 SmallVector<PartialDiagnosticAt, 8> Diags;
801 if (!Cond->isValueDependent() &&
802 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
803 Diags)) {
804 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
805 for (int I = 0, N = Diags.size(); I != N; ++I)
806 S.Diag(Diags[I].first, Diags[I].second);
807 return;
808 }
809
810 D->addAttr(::new (S.Context)
811 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
812 Attr.getAttributeSpellingListIndex()));
813}
814
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000815static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000816 ConsumableAttr::ConsumedState DefaultState;
817
818 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000819 IdentifierLoc *IL = Attr.getArgAsIdent(0);
820 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
821 DefaultState)) {
822 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
823 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000824 return;
825 }
David Blaikie16f76d22013-09-06 01:28:43 +0000826 } else {
827 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
828 << Attr.getName() << AANT_ArgumentIdentifier;
829 return;
830 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000831
832 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000833 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000834 Attr.getAttributeSpellingListIndex()));
835}
836
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000837
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000838static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
839 const AttributeList &Attr) {
840 ASTContext &CurrContext = S.getASTContext();
841 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
842
843 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
844 if (!RD->hasAttr<ConsumableAttr>()) {
845 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
846 RD->getNameAsString();
847
848 return false;
849 }
850 }
851
852 return true;
853}
854
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000855
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000856static void handleCallableWhenAttr(Sema &S, Decl *D,
857 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000858 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
859 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000860
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000861 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
862 return;
863
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000864 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
865 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
866 CallableWhenAttr::ConsumedState CallableState;
867
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000868 StringRef StateString;
869 SourceLocation Loc;
870 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
871 return;
872
873 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000874 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000875 S.Diag(Loc, diag::warn_attribute_type_not_supported)
876 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000877 return;
878 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000879
880 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000881 }
882
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000883 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000884 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
885 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000886}
887
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000888
DeLesley Hutchins69391772013-10-17 23:23:53 +0000889static void handleParamTypestateAttr(Sema &S, Decl *D,
890 const AttributeList &Attr) {
891 if (!checkAttributeNumArgs(S, Attr, 1)) return;
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000892
DeLesley Hutchins69391772013-10-17 23:23:53 +0000893 ParamTypestateAttr::ConsumedState ParamState;
894
895 if (Attr.isArgIdent(0)) {
896 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
897 StringRef StateString = Ident->Ident->getName();
898
899 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
900 ParamState)) {
901 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
902 << Attr.getName() << StateString;
903 return;
904 }
905 } else {
906 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
907 Attr.getName() << AANT_ArgumentIdentifier;
908 return;
909 }
910
911 // FIXME: This check is currently being done in the analysis. It can be
912 // enabled here only after the parser propagates attributes at
913 // template specialization definition, not declaration.
914 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
915 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
916 //
917 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
918 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
919 // ReturnType.getAsString();
920 // return;
921 //}
922
923 D->addAttr(::new (S.Context)
924 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
925 Attr.getAttributeSpellingListIndex()));
926}
927
928
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000929static void handleReturnTypestateAttr(Sema &S, Decl *D,
930 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000931 if (!checkAttributeNumArgs(S, Attr, 1)) return;
932
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000933 ReturnTypestateAttr::ConsumedState ReturnState;
934
935 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000936 IdentifierLoc *IL = Attr.getArgAsIdent(0);
937 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
938 ReturnState)) {
939 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
940 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000941 return;
942 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000943 } else {
944 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
945 Attr.getName() << AANT_ArgumentIdentifier;
946 return;
947 }
948
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000949 // FIXME: This check is currently being done in the analysis. It can be
950 // enabled here only after the parser propagates attributes at
951 // template specialization definition, not declaration.
952 //QualType ReturnType;
953 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000954 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
955 // ReturnType = Param->getType();
956 //
957 //} else if (const CXXConstructorDecl *Constructor =
958 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000959 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
960 //
961 //} else {
962 //
963 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
964 //}
965 //
966 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
967 //
968 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
969 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
970 // ReturnType.getAsString();
971 // return;
972 //}
973
974 D->addAttr(::new (S.Context)
975 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
976 Attr.getAttributeSpellingListIndex()));
977}
978
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000979
980static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000981 if (!checkAttributeNumArgs(S, Attr, 1))
982 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000983
984 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
985 return;
986
987 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000988 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000989 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
990 StringRef Param = Ident->Ident->getName();
991 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
992 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
993 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000994 return;
995 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000996 } else {
997 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
998 Attr.getName() << AANT_ArgumentIdentifier;
999 return;
1000 }
1001
1002 D->addAttr(::new (S.Context)
1003 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1004 Attr.getAttributeSpellingListIndex()));
1005}
1006
Chris Wailes9385f9f2013-10-29 20:28:41 +00001007static void handleTestTypestateAttr(Sema &S, Decl *D,
1008 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001009 if (!checkAttributeNumArgs(S, Attr, 1))
1010 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001011
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001012 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1013 return;
1014
Chris Wailes9385f9f2013-10-29 20:28:41 +00001015 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001016 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001017 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1018 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001019 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001020 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1021 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001022 return;
1023 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001024 } else {
1025 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1026 Attr.getName() << AANT_ArgumentIdentifier;
1027 return;
1028 }
1029
1030 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001031 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001032 Attr.getAttributeSpellingListIndex()));
1033}
1034
Chandler Carruthedc2c642011-07-02 00:01:44 +00001035static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1036 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001037 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001038 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001039}
1040
Chandler Carruthedc2c642011-07-02 00:01:44 +00001041static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001042 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Aaron Ballman36a53502014-01-16 13:03:14 +00001043 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context,
1044 Attr.getAttributeSpellingListIndex()));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001045 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001046 // If the alignment is less than or equal to 8 bits, the packed attribute
1047 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001048 if (!FD->getType()->isDependentType() &&
1049 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001050 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001051 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001052 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001053 else
Michael Han99315932013-01-24 16:46:58 +00001054 FD->addAttr(::new (S.Context)
1055 PackedAttr(Attr.getRange(), S.Context,
1056 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001057 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001058 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001059}
1060
Ted Kremenek7fd17232011-09-29 07:02:25 +00001061static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1062 // The IBOutlet/IBOutletCollection attributes only apply to instance
1063 // variables or properties of Objective-C classes. The outlet must also
1064 // have an object reference type.
1065 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1066 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001067 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001068 << Attr.getName() << VD->getType() << 0;
1069 return false;
1070 }
1071 }
1072 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1073 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001074 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001075 << Attr.getName() << PD->getType() << 1;
1076 return false;
1077 }
1078 }
1079 else {
1080 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1081 return false;
1082 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001083
Ted Kremenek7fd17232011-09-29 07:02:25 +00001084 return true;
1085}
1086
Chandler Carruthedc2c642011-07-02 00:01:44 +00001087static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001088 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001089 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001090
Michael Han99315932013-01-24 16:46:58 +00001091 D->addAttr(::new (S.Context)
1092 IBOutletAttr(Attr.getRange(), S.Context,
1093 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001094}
1095
Chandler Carruthedc2c642011-07-02 00:01:44 +00001096static void handleIBOutletCollection(Sema &S, Decl *D,
1097 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001098
1099 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001100 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001101 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1102 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001103 return;
1104 }
1105
Ted Kremenek7fd17232011-09-29 07:02:25 +00001106 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001107 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001108
Richard Smithb1f9a282013-10-31 01:56:18 +00001109 ParsedType PT;
1110
1111 if (Attr.hasParsedType())
1112 PT = Attr.getTypeArg();
1113 else {
1114 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1115 S.getScopeForContext(D->getDeclContext()->getParent()));
1116 if (!PT) {
1117 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1118 return;
1119 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001120 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001121
Richard Smithb87c4652013-10-31 21:23:20 +00001122 TypeSourceInfo *QTLoc = 0;
1123 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1124 if (!QTLoc)
1125 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001126
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001127 // Diagnose use of non-object type in iboutletcollection attribute.
1128 // FIXME. Gnu attribute extension ignores use of builtin types in
1129 // attributes. So, __attribute__((iboutletcollection(char))) will be
1130 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001131 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001132 S.Diag(Attr.getLoc(),
1133 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1134 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001135 return;
1136 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001137
Michael Han99315932013-01-24 16:46:58 +00001138 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001139 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001140 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001141}
1142
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001143static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001144 if (const RecordType *UT = T->getAsUnionType())
1145 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1146 RecordDecl *UD = UT->getDecl();
1147 for (RecordDecl::field_iterator it = UD->field_begin(),
1148 itend = UD->field_end(); it != itend; ++it) {
1149 QualType QT = it->getType();
1150 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1151 T = QT;
1152 return;
1153 }
1154 }
1155 }
1156}
1157
Ted Kremenek9aedc152014-01-17 06:24:56 +00001158static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001159 SourceRange R, bool isReturnValue = false) {
Ted Kremenek9aedc152014-01-17 06:24:56 +00001160 T = T.getNonReferenceType();
1161 possibleTransparentUnionPointerType(T);
1162
1163 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001164 S.Diag(Attr.getLoc(),
1165 isReturnValue ? diag::warn_attribute_return_pointers_only
1166 : diag::warn_attribute_pointers_only)
Ted Kremenek9aedc152014-01-17 06:24:56 +00001167 << Attr.getName() << R;
1168 return false;
1169 }
1170 return true;
1171}
1172
1173static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1174 const AttributeList &Attr) {
1175 // Is the argument a pointer type?
1176 if (!attrNonNullArgCheck(S, D->getType(), Attr, D->getSourceRange()))
1177 return;
1178
1179 if (Attr.getNumArgs() > 0) {
1180 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1181 << D->getSourceRange();
1182 return;
1183 }
1184
1185 D->addAttr(::new (S.Context)
1186 NonNullAttr(Attr.getRange(), S.Context, 0, 0,
1187 Attr.getAttributeSpellingListIndex()));
1188}
1189
Chandler Carruthedc2c642011-07-02 00:01:44 +00001190static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001191 SmallVector<unsigned, 8> NonNullArgs;
1192 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001193 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001194 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001195 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001196 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001197
1198 // Is the function argument a pointer type?
Ted Kremenek9aedc152014-01-17 06:24:56 +00001199 // FIXME: Should also highlight argument in decl in the diagnostic.
Alp Toker601b22c2014-01-21 23:35:24 +00001200 if (!attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr,
1201 Ex->getSourceRange()))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001202 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001203
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001204 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001205 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001206
1207 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1208 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001209 if (NonNullArgs.empty()) {
Alp Toker601b22c2014-01-21 23:35:24 +00001210 for (unsigned i = 0, e = getFunctionOrMethodNumParams(D); i != e; ++i) {
1211 QualType T = getFunctionOrMethodParamType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001212 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001213 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001214 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001215 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001216
Ted Kremenek22813f42010-10-21 18:49:36 +00001217 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001218 if (NonNullArgs.empty()) {
1219 // Warn the trivial case only if attribute is not coming from a
1220 // macro instantiation.
1221 if (Attr.getLoc().isFileID())
1222 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001223 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001224 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001225 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001226
Nick Lewyckye1121512013-01-24 01:12:16 +00001227 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001228 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001229 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001230 D->addAttr(::new (S.Context)
1231 NonNullAttr(Attr.getRange(), S.Context, start, size,
1232 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001233}
1234
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001235static void handleReturnsNonNullAttr(Sema &S, Decl *D,
1236 const AttributeList &Attr) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001237 QualType ResultType = getFunctionOrMethodResultType(D);
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001238 if (!attrNonNullArgCheck(S, ResultType, Attr, Attr.getRange(),
1239 /* isReturnValue */ true))
1240 return;
1241
1242 D->addAttr(::new (S.Context)
1243 ReturnsNonNullAttr(Attr.getRange(), S.Context,
1244 Attr.getAttributeSpellingListIndex()));
1245}
1246
Chandler Carruthedc2c642011-07-02 00:01:44 +00001247static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001248 // This attribute must be applied to a function declaration. The first
1249 // argument to the attribute must be an identifier, the name of the resource,
1250 // for example: malloc. The following arguments must be argument indexes, the
1251 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001252 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001253 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001254 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001255
Aaron Ballman00e99962013-08-31 01:11:41 +00001256 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001257 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001258 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001259 return;
1260 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001261
Richard Smith852e9ce2013-11-27 01:46:48 +00001262 // Figure out our Kind.
1263 OwnershipAttr::OwnershipKind K =
1264 OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0,
1265 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001266
Richard Smith852e9ce2013-11-27 01:46:48 +00001267 // Check arguments.
1268 switch (K) {
1269 case OwnershipAttr::Takes:
1270 case OwnershipAttr::Holds:
1271 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001272 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1273 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001274 return;
1275 }
1276 break;
1277 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001278 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001279 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1280 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001281 return;
1282 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001283 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001284 }
1285
Richard Smith852e9ce2013-11-27 01:46:48 +00001286 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001287
1288 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001289 StringRef ModuleName = Module->getName();
1290 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1291 ModuleName.size() > 4) {
1292 ModuleName = ModuleName.drop_front(2).drop_back(2);
1293 Module = &S.PP.getIdentifierTable().get(ModuleName);
1294 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001295
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001296 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001297 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1298 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001299 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001300 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001301 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001302
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001303 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001304 QualType T = getFunctionOrMethodParamType(D, Idx);
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001305 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001306 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001307 case OwnershipAttr::Takes:
1308 case OwnershipAttr::Holds:
1309 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1310 Err = 0;
1311 break;
1312 case OwnershipAttr::Returns:
1313 if (!T->isIntegerType())
1314 Err = 1;
1315 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001316 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001317 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001318 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001319 << Ex->getSourceRange();
1320 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001321 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001322
1323 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001324 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001325 i = D->specific_attr_begin<OwnershipAttr>(),
1326 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001327 // FIXME: A returns attribute should conflict with any returns attribute
1328 // with a different index too.
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001329 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1330 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1331 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballman05a63782014-01-20 15:06:09 +00001332 << AL.getName() << *i;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001333 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001334 }
1335 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001336 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001337 }
1338
1339 unsigned* start = OwnershipArgs.data();
1340 unsigned size = OwnershipArgs.size();
1341 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001342
Michael Han99315932013-01-24 16:46:58 +00001343 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001344 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001345 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001346}
1347
Chandler Carruthedc2c642011-07-02 00:01:44 +00001348static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001349 // Check the attribute arguments.
1350 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001351 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1352 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001353 return;
1354 }
1355
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001356 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001357
Rafael Espindolac18086a2010-02-23 22:00:30 +00001358 // gcc rejects
1359 // class c {
1360 // static int a __attribute__((weakref ("v2")));
1361 // static int b() __attribute__((weakref ("f3")));
1362 // };
1363 // and ignores the attributes of
1364 // void f(void) {
1365 // static int a __attribute__((weakref ("v2")));
1366 // }
1367 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001368 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001369 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001370 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1371 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001372 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001373 }
1374
1375 // The GCC manual says
1376 //
1377 // At present, a declaration to which `weakref' is attached can only
1378 // be `static'.
1379 //
1380 // It also says
1381 //
1382 // Without a TARGET,
1383 // given as an argument to `weakref' or to `alias', `weakref' is
1384 // equivalent to `weak'.
1385 //
1386 // gcc 4.4.1 will accept
1387 // int a7 __attribute__((weakref));
1388 // as
1389 // int a7 __attribute__((weak));
1390 // This looks like a bug in gcc. We reject that for now. We should revisit
1391 // it if this behaviour is actually used.
1392
Rafael Espindolac18086a2010-02-23 22:00:30 +00001393 // GCC rejects
1394 // static ((alias ("y"), weakref)).
1395 // Should we? How to check that weakref is before or after alias?
1396
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001397 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1398 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1399 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001400 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001401 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001402 // GCC will accept anything as the argument of weakref. Should we
1403 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001404 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1405 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001406
Michael Han99315932013-01-24 16:46:58 +00001407 D->addAttr(::new (S.Context)
1408 WeakRefAttr(Attr.getRange(), S.Context,
1409 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001410}
1411
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001412static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1413 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001414 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001415 return;
1416
Douglas Gregore8bbc122011-09-02 00:18:52 +00001417 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001418 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1419 return;
1420 }
1421
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001422 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001423
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001424 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001425 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001426}
1427
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001428static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001429 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001430 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001431
Michael Han99315932013-01-24 16:46:58 +00001432 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1433 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001434}
1435
1436static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001437 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001438 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001439
Michael Han99315932013-01-24 16:46:58 +00001440 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1441 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001442}
1443
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001444static void handleTLSModelAttr(Sema &S, Decl *D,
1445 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001446 StringRef Model;
1447 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001448 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001449 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001450 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001451
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001452 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001453 if (Model != "global-dynamic" && Model != "local-dynamic"
1454 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001455 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001456 return;
1457 }
1458
Michael Han99315932013-01-24 16:46:58 +00001459 D->addAttr(::new (S.Context)
1460 TLSModelAttr(Attr.getRange(), S.Context, Model,
1461 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001462}
1463
Chandler Carruthedc2c642011-07-02 00:01:44 +00001464static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001465 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Alp Toker314cc812014-01-25 16:55:45 +00001466 QualType RetTy = FD->getReturnType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001467 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001468 D->addAttr(::new (S.Context)
1469 MallocAttr(Attr.getRange(), S.Context,
1470 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001471 return;
1472 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001473 }
1474
Ted Kremenek08479ae2009-08-15 00:51:46 +00001475 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001476}
1477
Chandler Carruthedc2c642011-07-02 00:01:44 +00001478static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001479 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001480 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1481 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001482 return;
1483 }
1484
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001485 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1486 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001487}
1488
Chandler Carruthedc2c642011-07-02 00:01:44 +00001489static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001490 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001491
1492 if (S.CheckNoReturnAttr(attr)) return;
1493
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001494 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001495 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001496 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001497 return;
1498 }
1499
Michael Han99315932013-01-24 16:46:58 +00001500 D->addAttr(::new (S.Context)
1501 NoReturnAttr(attr.getRange(), S.Context,
1502 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001503}
1504
1505bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001506 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001507 attr.setInvalid();
1508 return true;
1509 }
1510
1511 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001512}
1513
Chandler Carruthedc2c642011-07-02 00:01:44 +00001514static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1515 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001516
1517 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1518 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001519 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1520 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001521 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1522 && !VD->getType()->isFunctionPointerType())) {
1523 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001524 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001525 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001526 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001527 return;
1528 }
1529 }
1530
Michael Han99315932013-01-24 16:46:58 +00001531 D->addAttr(::new (S.Context)
1532 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1533 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001534}
1535
John Thompsoncdb847ba2010-08-09 21:53:52 +00001536// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001537static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001538/*
1539 Returning a Vector Class in Registers
1540
Eric Christopherbc638a82010-12-01 22:13:54 +00001541 According to the PPU ABI specifications, a class with a single member of
1542 vector type is returned in memory when used as the return value of a function.
1543 This results in inefficient code when implementing vector classes. To return
1544 the value in a single vector register, add the vecreturn attribute to the
1545 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001546
1547 Example:
1548
1549 struct Vector
1550 {
1551 __vector float xyzw;
1552 } __attribute__((vecreturn));
1553
1554 Vector Add(Vector lhs, Vector rhs)
1555 {
1556 Vector result;
1557 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1558 return result; // This will be returned in a register
1559 }
1560*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001561 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1562 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001563 return;
1564 }
1565
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001566 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001567 int count = 0;
1568
1569 if (!isa<CXXRecordDecl>(record)) {
1570 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1571 return;
1572 }
1573
1574 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1575 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1576 return;
1577 }
1578
Eric Christopherbc638a82010-12-01 22:13:54 +00001579 for (RecordDecl::field_iterator iter = record->field_begin();
1580 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001581 if ((count == 1) || !iter->getType()->isVectorType()) {
1582 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1583 return;
1584 }
1585 count++;
1586 }
1587
Michael Han99315932013-01-24 16:46:58 +00001588 D->addAttr(::new (S.Context)
1589 VecReturnAttr(Attr.getRange(), S.Context,
1590 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001591}
1592
Richard Smithe233fbf2013-01-28 22:42:45 +00001593static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1594 const AttributeList &Attr) {
1595 if (isa<ParmVarDecl>(D)) {
1596 // [[carries_dependency]] can only be applied to a parameter if it is a
1597 // parameter of a function declaration or lambda.
1598 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1599 S.Diag(Attr.getLoc(),
1600 diag::err_carries_dependency_param_not_function_decl);
1601 return;
1602 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001603 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001604
1605 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1606 Attr.getRange(), S.Context,
1607 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001608}
1609
Chandler Carruthedc2c642011-07-02 00:01:44 +00001610static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001611 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001612 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001613 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001614 return;
1615 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001616 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001617 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001618 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001619 return;
1620 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001621
Michael Han99315932013-01-24 16:46:58 +00001622 D->addAttr(::new (S.Context)
1623 UsedAttr(Attr.getRange(), S.Context,
1624 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001625}
1626
Chandler Carruthedc2c642011-07-02 00:01:44 +00001627static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001628 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001629 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001630 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1631 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001632 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001633 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001634
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001635 uint32_t priority = ConstructorAttr::DefaultPriority;
1636 if (Attr.getNumArgs() > 0 &&
1637 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1638 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001639
Michael Han99315932013-01-24 16:46:58 +00001640 D->addAttr(::new (S.Context)
1641 ConstructorAttr(Attr.getRange(), S.Context, priority,
1642 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001643}
1644
Chandler Carruthedc2c642011-07-02 00:01:44 +00001645static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001646 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001647 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001648 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1649 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001650 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001651 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001652
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001653 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001654 if (Attr.getNumArgs() > 0 &&
1655 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1656 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001657
Michael Han99315932013-01-24 16:46:58 +00001658 D->addAttr(::new (S.Context)
1659 DestructorAttr(Attr.getRange(), S.Context, priority,
1660 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001661}
1662
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001663template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001664static void handleAttrWithMessage(Sema &S, Decl *D,
1665 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001666 unsigned NumArgs = Attr.getNumArgs();
1667 if (NumArgs > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001668 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1669 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001670 return;
1671 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001672
1673 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001674 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001675 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001676 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001677
Michael Han99315932013-01-24 16:46:58 +00001678 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1679 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001680}
1681
Ted Kremenek28eace62013-11-23 01:01:34 +00001682static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
1683 const AttributeList &Attr) {
Ted Kremenek28eace62013-11-23 01:01:34 +00001684 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001685 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1686 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001687}
1688
Jordy Rose740b0c22012-05-08 03:27:22 +00001689static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1690 IdentifierInfo *Platform,
1691 VersionTuple Introduced,
1692 VersionTuple Deprecated,
1693 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001694 StringRef PlatformName
1695 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1696 if (PlatformName.empty())
1697 PlatformName = Platform->getName();
1698
1699 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1700 // of these steps are needed).
1701 if (!Introduced.empty() && !Deprecated.empty() &&
1702 !(Introduced <= Deprecated)) {
1703 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1704 << 1 << PlatformName << Deprecated.getAsString()
1705 << 0 << Introduced.getAsString();
1706 return true;
1707 }
1708
1709 if (!Introduced.empty() && !Obsoleted.empty() &&
1710 !(Introduced <= Obsoleted)) {
1711 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1712 << 2 << PlatformName << Obsoleted.getAsString()
1713 << 0 << Introduced.getAsString();
1714 return true;
1715 }
1716
1717 if (!Deprecated.empty() && !Obsoleted.empty() &&
1718 !(Deprecated <= Obsoleted)) {
1719 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1720 << 2 << PlatformName << Obsoleted.getAsString()
1721 << 1 << Deprecated.getAsString();
1722 return true;
1723 }
1724
1725 return false;
1726}
1727
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001728/// \brief Check whether the two versions match.
1729///
1730/// If either version tuple is empty, then they are assumed to match. If
1731/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1732static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1733 bool BeforeIsOkay) {
1734 if (X.empty() || Y.empty())
1735 return true;
1736
1737 if (X == Y)
1738 return true;
1739
1740 if (BeforeIsOkay && X < Y)
1741 return true;
1742
1743 return false;
1744}
1745
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001746AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001747 IdentifierInfo *Platform,
1748 VersionTuple Introduced,
1749 VersionTuple Deprecated,
1750 VersionTuple Obsoleted,
1751 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001752 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001753 bool Override,
1754 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001755 VersionTuple MergedIntroduced = Introduced;
1756 VersionTuple MergedDeprecated = Deprecated;
1757 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001758 bool FoundAny = false;
1759
Rafael Espindolac67f2232012-05-10 02:50:16 +00001760 if (D->hasAttrs()) {
1761 AttrVec &Attrs = D->getAttrs();
1762 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1763 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1764 if (!OldAA) {
1765 ++i;
1766 continue;
1767 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001768
Rafael Espindolac67f2232012-05-10 02:50:16 +00001769 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1770 if (OldPlatform != Platform) {
1771 ++i;
1772 continue;
1773 }
1774
1775 FoundAny = true;
1776 VersionTuple OldIntroduced = OldAA->getIntroduced();
1777 VersionTuple OldDeprecated = OldAA->getDeprecated();
1778 VersionTuple OldObsoleted = OldAA->getObsoleted();
1779 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001780
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001781 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1782 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1783 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1784 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001785 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001786 if (Override) {
1787 int Which = -1;
1788 VersionTuple FirstVersion;
1789 VersionTuple SecondVersion;
1790 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1791 Which = 0;
1792 FirstVersion = OldIntroduced;
1793 SecondVersion = Introduced;
1794 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1795 Which = 1;
1796 FirstVersion = Deprecated;
1797 SecondVersion = OldDeprecated;
1798 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1799 Which = 2;
1800 FirstVersion = Obsoleted;
1801 SecondVersion = OldObsoleted;
1802 }
1803
1804 if (Which == -1) {
1805 Diag(OldAA->getLocation(),
1806 diag::warn_mismatched_availability_override_unavail)
1807 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1808 } else {
1809 Diag(OldAA->getLocation(),
1810 diag::warn_mismatched_availability_override)
1811 << Which
1812 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1813 << FirstVersion.getAsString() << SecondVersion.getAsString();
1814 }
1815 Diag(Range.getBegin(), diag::note_overridden_method);
1816 } else {
1817 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1818 Diag(Range.getBegin(), diag::note_previous_attribute);
1819 }
1820
Rafael Espindolac67f2232012-05-10 02:50:16 +00001821 Attrs.erase(Attrs.begin() + i);
1822 --e;
1823 continue;
1824 }
1825
1826 VersionTuple MergedIntroduced2 = MergedIntroduced;
1827 VersionTuple MergedDeprecated2 = MergedDeprecated;
1828 VersionTuple MergedObsoleted2 = MergedObsoleted;
1829
1830 if (MergedIntroduced2.empty())
1831 MergedIntroduced2 = OldIntroduced;
1832 if (MergedDeprecated2.empty())
1833 MergedDeprecated2 = OldDeprecated;
1834 if (MergedObsoleted2.empty())
1835 MergedObsoleted2 = OldObsoleted;
1836
1837 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1838 MergedIntroduced2, MergedDeprecated2,
1839 MergedObsoleted2)) {
1840 Attrs.erase(Attrs.begin() + i);
1841 --e;
1842 continue;
1843 }
1844
1845 MergedIntroduced = MergedIntroduced2;
1846 MergedDeprecated = MergedDeprecated2;
1847 MergedObsoleted = MergedObsoleted2;
1848 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001849 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001850 }
1851
1852 if (FoundAny &&
1853 MergedIntroduced == Introduced &&
1854 MergedDeprecated == Deprecated &&
1855 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001856 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001857
Ted Kremenekb5445722013-04-06 00:34:27 +00001858 // Only create a new attribute if !Override, but we want to do
1859 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001860 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001861 MergedDeprecated, MergedObsoleted) &&
1862 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001863 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1864 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001865 Obsoleted, IsUnavailable, Message,
1866 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001867 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001868 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001869}
1870
Chandler Carruthedc2c642011-07-02 00:01:44 +00001871static void handleAvailabilityAttr(Sema &S, Decl *D,
1872 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001873 if (!checkAttributeNumArgs(S, Attr, 1))
1874 return;
1875 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001876 unsigned Index = Attr.getAttributeSpellingListIndex();
1877
Aaron Ballman00e99962013-08-31 01:11:41 +00001878 IdentifierInfo *II = Platform->Ident;
1879 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1880 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1881 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001882
Rafael Espindolac231fab2013-01-08 21:30:32 +00001883 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1884 if (!ND) {
1885 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1886 return;
1887 }
1888
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001889 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1890 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1891 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001892 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001893 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001894 if (const StringLiteral *SE =
1895 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001896 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001897
Aaron Ballman00e99962013-08-31 01:11:41 +00001898 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001899 Introduced.Version,
1900 Deprecated.Version,
1901 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001902 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001903 /*Override=*/false,
1904 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001905 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001906 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001907}
1908
John McCalld041a9b2013-02-20 01:54:26 +00001909template <class T>
1910static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1911 typename T::VisibilityType value,
1912 unsigned attrSpellingListIndex) {
1913 T *existingAttr = D->getAttr<T>();
1914 if (existingAttr) {
1915 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1916 if (existingValue == value)
1917 return NULL;
1918 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1919 S.Diag(range.getBegin(), diag::note_previous_attribute);
1920 D->dropAttr<T>();
1921 }
1922 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1923}
1924
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001925VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001926 VisibilityAttr::VisibilityType Vis,
1927 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001928 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1929 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001930}
1931
John McCalld041a9b2013-02-20 01:54:26 +00001932TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1933 TypeVisibilityAttr::VisibilityType Vis,
1934 unsigned AttrSpellingListIndex) {
1935 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1936 AttrSpellingListIndex);
1937}
1938
1939static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1940 bool isTypeVisibility) {
1941 // Visibility attributes don't mean anything on a typedef.
1942 if (isa<TypedefNameDecl>(D)) {
1943 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
1944 << Attr.getName();
1945 return;
1946 }
1947
1948 // 'type_visibility' can only go on a type or namespace.
1949 if (isTypeVisibility &&
1950 !(isa<TagDecl>(D) ||
1951 isa<ObjCInterfaceDecl>(D) ||
1952 isa<NamespaceDecl>(D))) {
1953 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
1954 << Attr.getName() << ExpectedTypeOrNamespace;
1955 return;
1956 }
1957
Benjamin Kramer70370212013-09-09 15:08:57 +00001958 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001959 StringRef TypeStr;
1960 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001961 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001962 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001963
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001964 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00001965 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001966 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00001967 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001968 return;
1969 }
Aaron Ballman682ee422013-09-11 19:47:58 +00001970
1971 // Complain about attempts to use protected visibility on targets
1972 // (like Darwin) that don't support it.
1973 if (type == VisibilityAttr::Protected &&
1974 !S.Context.getTargetInfo().hasProtectedVisibility()) {
1975 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1976 type = VisibilityAttr::Default;
1977 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001978
Michael Han99315932013-01-24 16:46:58 +00001979 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00001980 clang::Attr *newAttr;
1981 if (isTypeVisibility) {
1982 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
1983 (TypeVisibilityAttr::VisibilityType) type,
1984 Index);
1985 } else {
1986 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
1987 }
1988 if (newAttr)
1989 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001990}
1991
Chandler Carruthedc2c642011-07-02 00:01:44 +00001992static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1993 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001994 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00001995 if (!Attr.isArgIdent(0)) {
1996 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1997 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00001998 return;
1999 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002000
Aaron Ballman682ee422013-09-11 19:47:58 +00002001 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2002 ObjCMethodFamilyAttr::FamilyKind F;
2003 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2004 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2005 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002006 return;
2007 }
2008
Alp Toker314cc812014-01-25 16:55:45 +00002009 if (F == ObjCMethodFamilyAttr::OMF_init &&
2010 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00002011 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00002012 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002013 // Ignore the attribute.
2014 return;
2015 }
2016
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002017 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00002018 S.Context, F,
2019 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002020}
2021
Chandler Carruthedc2c642011-07-02 00:01:44 +00002022static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002023 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002024 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002025 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002026 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2027 return;
2028 }
2029 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002030 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2031 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002032 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002033 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2034 return;
2035 }
2036 }
2037 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002038 // It is okay to include this attribute on properties, e.g.:
2039 //
2040 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2041 //
2042 // In this case it follows tradition and suppresses an error in the above
2043 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002044 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002045 }
Michael Han99315932013-01-24 16:46:58 +00002046 D->addAttr(::new (S.Context)
2047 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2048 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002049}
2050
Chandler Carruthedc2c642011-07-02 00:01:44 +00002051static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002052 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002053 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002054 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002055 return;
2056 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002057
Aaron Ballman00e99962013-08-31 01:11:41 +00002058 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002059 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002060 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2061 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2062 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002063 return;
2064 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002065
Michael Han99315932013-01-24 16:46:58 +00002066 D->addAttr(::new (S.Context)
2067 BlocksAttr(Attr.getRange(), S.Context, type,
2068 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002069}
2070
Chandler Carruthedc2c642011-07-02 00:01:44 +00002071static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002072 // check the attribute arguments.
2073 if (Attr.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00002074 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
2075 << Attr.getName() << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002076 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002077 }
2078
Aaron Ballman18a78382013-11-21 00:28:23 +00002079 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002080 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002081 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002082 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002083 if (E->isTypeDependent() || E->isValueDependent() ||
2084 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002085 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002086 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002087 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002088 return;
2089 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002090
John McCallb46f2872011-09-09 07:56:05 +00002091 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002092 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2093 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002094 return;
2095 }
John McCallb46f2872011-09-09 07:56:05 +00002096
2097 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002098 }
2099
Aaron Ballman18a78382013-11-21 00:28:23 +00002100 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002101 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002102 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002103 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002104 if (E->isTypeDependent() || E->isValueDependent() ||
2105 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002106 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002107 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002108 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002109 return;
2110 }
2111 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002112
John McCallb46f2872011-09-09 07:56:05 +00002113 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002114 // FIXME: This error message could be improved, it would be nice
2115 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002116 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2117 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002118 return;
2119 }
2120 }
2121
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002122 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002123 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002124 if (isa<FunctionNoProtoType>(FT)) {
2125 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2126 return;
2127 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002128
Chris Lattner9363e312009-03-17 23:03:47 +00002129 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002130 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002131 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002132 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002133 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002134 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002135 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002136 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002137 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002138 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2139 if (!BD->isVariadic()) {
2140 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2141 return;
2142 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002143 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002144 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002145 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002146 const FunctionType *FT = Ty->isFunctionPointerType()
2147 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002148 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002149 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002150 int m = Ty->isFunctionPointerType() ? 0 : 1;
2151 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002152 return;
2153 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002154 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002155 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002156 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002157 return;
2158 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002159 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002160 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002161 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002162 return;
2163 }
Michael Han99315932013-01-24 16:46:58 +00002164 D->addAttr(::new (S.Context)
2165 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2166 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002167}
2168
Chandler Carruthedc2c642011-07-02 00:01:44 +00002169static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002170 if (D->getFunctionType() &&
2171 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002172 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2173 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002174 return;
2175 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002176 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002177 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002178 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2179 << Attr.getName() << 1;
2180 return;
2181 }
2182
Michael Han99315932013-01-24 16:46:58 +00002183 D->addAttr(::new (S.Context)
2184 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2185 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002186}
2187
Chandler Carruthedc2c642011-07-02 00:01:44 +00002188static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002189 // weak_import only applies to variable & function declarations.
2190 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002191 if (!D->canBeWeakImported(isDef)) {
2192 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002193 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2194 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002195 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002196 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002197 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002198 // Nothing to warn about here.
2199 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002200 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002201 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002202
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002203 return;
2204 }
2205
Michael Han99315932013-01-24 16:46:58 +00002206 D->addAttr(::new (S.Context)
2207 WeakImportAttr(Attr.getRange(), S.Context,
2208 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002209}
2210
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002211// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002212template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002213static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002214 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002215 uint32_t WGSize[3];
2216 for (unsigned i = 0; i < 3; ++i)
2217 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002218 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002219
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002220 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2221 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2222 Existing->getYDim() == WGSize[1] &&
2223 Existing->getZDim() == WGSize[2]))
2224 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002225
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002226 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2227 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002228 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002229}
2230
Joey Goulyaba589c2013-03-08 09:42:32 +00002231static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002232 if (!Attr.hasParsedType()) {
2233 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2234 << Attr.getName() << 1;
2235 return;
2236 }
2237
Richard Smithb87c4652013-10-31 21:23:20 +00002238 TypeSourceInfo *ParmTSI = 0;
2239 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2240 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002241
2242 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2243 (ParmType->isBooleanType() ||
2244 !ParmType->isIntegralType(S.getASTContext()))) {
2245 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2246 << ParmType;
2247 return;
2248 }
2249
Aaron Ballmana9e05402013-12-02 22:16:55 +00002250 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002251 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002252 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2253 return;
2254 }
2255 }
2256
2257 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002258 ParmTSI,
2259 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002260}
2261
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002262SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002263 StringRef Name,
2264 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002265 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2266 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002267 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002268 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2269 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002270 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002271 }
Michael Han99315932013-01-24 16:46:58 +00002272 return ::new (Context) SectionAttr(Range, Context, Name,
2273 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002274}
2275
Chandler Carruthedc2c642011-07-02 00:01:44 +00002276static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002277 // Make sure that there is a string literal as the sections's single
2278 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002279 StringRef Str;
2280 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002281 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002282 return;
Mike Stump11289f42009-09-09 15:08:12 +00002283
Chris Lattner30ba6742009-08-10 19:03:04 +00002284 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002285 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002286 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002287 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002288 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002289 return;
2290 }
Mike Stump11289f42009-09-09 15:08:12 +00002291
Michael Han99315932013-01-24 16:46:58 +00002292 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002293 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002294 if (NewAttr)
2295 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002296}
2297
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002298
Chandler Carruthedc2c642011-07-02 00:01:44 +00002299static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002300 VarDecl *VD = cast<VarDecl>(D);
2301 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002302 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002303 return;
2304 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002305
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002306 Expr *E = Attr.getArgAsExpr(0);
2307 SourceLocation Loc = E->getExprLoc();
2308 FunctionDecl *FD = 0;
2309 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002310
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002311 // gcc only allows for simple identifiers. Since we support more than gcc, we
2312 // will warn the user.
2313 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2314 if (DRE->hasQualifier())
2315 S.Diag(Loc, diag::warn_cleanup_ext);
2316 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2317 NI = DRE->getNameInfo();
2318 if (!FD) {
2319 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2320 << NI.getName();
2321 return;
2322 }
2323 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2324 if (ULE->hasExplicitTemplateArgs())
2325 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002326 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2327 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002328 if (!FD) {
2329 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2330 << NI.getName();
2331 if (ULE->getType() == S.Context.OverloadTy)
2332 S.NoteAllOverloadCandidates(ULE);
2333 return;
2334 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002335 } else {
2336 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002337 return;
2338 }
2339
Anders Carlssond277d792009-01-31 01:16:18 +00002340 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002341 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2342 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002343 return;
2344 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002345
Anders Carlsson723f55d2009-02-07 23:16:50 +00002346 // We're currently more strict than GCC about what function types we accept.
2347 // If this ever proves to be a problem it should be easy to fix.
2348 QualType Ty = S.Context.getPointerType(VD->getType());
2349 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002350 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2351 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002352 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2353 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002354 return;
2355 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002356
Michael Han99315932013-01-24 16:46:58 +00002357 D->addAttr(::new (S.Context)
2358 CleanupAttr(Attr.getRange(), S.Context, FD,
2359 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002360}
2361
Mike Stumpd3bb5572009-07-24 19:02:52 +00002362/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002363/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002364static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002365 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002366 uint64_t Idx;
2367 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002368 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002369
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002370 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002371 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002372
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002373 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2374 if (not_nsstring_type &&
2375 !isCFStringType(Ty, S.Context) &&
2376 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002377 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002378 // FIXME: Should highlight the actual expression that has the wrong type.
2379 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002380 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002381 << IdxExpr->getSourceRange();
2382 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002383 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002384 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002385 if (!isNSStringType(Ty, S.Context) &&
2386 !isCFStringType(Ty, S.Context) &&
2387 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002388 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002389 // FIXME: Should highlight the actual expression that has the wrong type.
2390 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002391 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002392 << IdxExpr->getSourceRange();
2393 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002394 }
2395
Alp Toker601b22c2014-01-21 23:35:24 +00002396 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002397 // because that has corrected for the implicit this parameter, and is zero-
2398 // based. The attribute expects what the user wrote explicitly.
2399 llvm::APSInt Val;
2400 IdxExpr->EvaluateAsInt(Val, S.Context);
2401
Michael Han99315932013-01-24 16:46:58 +00002402 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002403 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002404 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002405}
2406
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002407enum FormatAttrKind {
2408 CFStringFormat,
2409 NSStringFormat,
2410 StrftimeFormat,
2411 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002412 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002413 InvalidFormat
2414};
2415
2416/// getFormatAttrKind - Map from format attribute names to supported format
2417/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002418static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002419 return llvm::StringSwitch<FormatAttrKind>(Format)
2420 // Check for formats that get handled specially.
2421 .Case("NSString", NSStringFormat)
2422 .Case("CFString", CFStringFormat)
2423 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002424
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002425 // Otherwise, check for supported formats.
2426 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2427 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2428 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002429
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002430 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2431 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002432}
2433
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002434/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002435/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002436static void handleInitPriorityAttr(Sema &S, Decl *D,
2437 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002438 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002439 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2440 return;
2441 }
2442
Aaron Ballman4a611152013-11-27 16:34:09 +00002443 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002444 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2445 Attr.setInvalid();
2446 return;
2447 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002448 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002449 if (S.Context.getAsArrayType(T))
2450 T = S.Context.getBaseElementType(T);
2451 if (!T->getAs<RecordType>()) {
2452 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2453 Attr.setInvalid();
2454 return;
2455 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002456
2457 Expr *E = Attr.getArgAsExpr(0);
2458 uint32_t prioritynum;
2459 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002460 Attr.setInvalid();
2461 return;
2462 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002463
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002464 if (prioritynum < 101 || prioritynum > 65535) {
2465 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002466 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002467 Attr.setInvalid();
2468 return;
2469 }
Michael Han99315932013-01-24 16:46:58 +00002470 D->addAttr(::new (S.Context)
2471 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2472 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002473}
2474
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002475FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2476 IdentifierInfo *Format, int FormatIdx,
2477 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002478 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002479 // Check whether we already have an equivalent format attribute.
2480 for (specific_attr_iterator<FormatAttr>
2481 i = D->specific_attr_begin<FormatAttr>(),
2482 e = D->specific_attr_end<FormatAttr>();
2483 i != e ; ++i) {
2484 FormatAttr *f = *i;
2485 if (f->getType() == Format &&
2486 f->getFormatIdx() == FormatIdx &&
2487 f->getFirstArg() == FirstArg) {
2488 // If we don't have a valid location for this attribute, adopt the
2489 // location.
2490 if (f->getLocation().isInvalid())
2491 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002492 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002493 }
2494 }
2495
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002496 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2497 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002498}
2499
Mike Stumpd3bb5572009-07-24 19:02:52 +00002500/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002501/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002502static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002503 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002504 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002505 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002506 return;
2507 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002508
Chandler Carruth743682b2010-11-16 08:35:43 +00002509 // In C++ the implicit 'this' function parameter also counts, and they are
2510 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002511 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002512 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002513
Aaron Ballman00e99962013-08-31 01:11:41 +00002514 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2515 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002516
2517 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002518 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002519 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002520 // If we've modified the string name, we need a new identifier for it.
2521 II = &S.Context.Idents.get(Format);
2522 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002523
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002524 // Check for supported formats.
2525 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002526
2527 if (Kind == IgnoredFormat)
2528 return;
2529
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002530 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002531 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002532 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002533 return;
2534 }
2535
2536 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002537 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002538 uint32_t Idx;
2539 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002540 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002541
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002542 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002543 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002544 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002545 return;
2546 }
2547
2548 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002549 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002550
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002551 if (HasImplicitThisParam) {
2552 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002553 S.Diag(Attr.getLoc(),
2554 diag::err_format_attribute_implicit_this_format_string)
2555 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002556 return;
2557 }
2558 ArgIdx--;
2559 }
Mike Stump11289f42009-09-09 15:08:12 +00002560
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002561 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002562 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002563
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002564 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002565 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002566 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2567 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002568 return;
2569 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002570 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002571 // FIXME: do we need to check if the type is NSString*? What are the
2572 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002573 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002574 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002575 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2576 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002577 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002578 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002579 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002580 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002581 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002582 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2583 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002584 return;
2585 }
2586
2587 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002588 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002589 uint32_t FirstArg;
2590 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002591 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002592
2593 // check if the function is variadic if the 3rd argument non-zero
2594 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002595 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002596 ++NumArgs; // +1 for ...
2597 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002598 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002599 return;
2600 }
2601 }
2602
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002603 // strftime requires FirstArg to be 0 because it doesn't read from any
2604 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002605 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002606 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002607 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2608 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002609 return;
2610 }
2611 // if 0 it disables parameter checking (to use with e.g. va_list)
2612 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002613 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002614 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002615 return;
2616 }
2617
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002618 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002619 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002620 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002621 if (NewAttr)
2622 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002623}
2624
Chandler Carruthedc2c642011-07-02 00:01:44 +00002625static void handleTransparentUnionAttr(Sema &S, Decl *D,
2626 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002627 // Try to find the underlying union declaration.
2628 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002629 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002630 if (TD && TD->getUnderlyingType()->isUnionType())
2631 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2632 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002633 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002634
2635 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002636 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002637 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002638 return;
2639 }
2640
John McCallf937c022011-10-07 06:10:15 +00002641 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002642 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002643 diag::warn_transparent_union_attribute_not_definition);
2644 return;
2645 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002646
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002647 RecordDecl::field_iterator Field = RD->field_begin(),
2648 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002649 if (Field == FieldEnd) {
2650 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2651 return;
2652 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002653
David Blaikie40ed2972012-06-06 20:45:41 +00002654 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002655 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002656 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002657 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002658 diag::warn_transparent_union_attribute_floating)
2659 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002660 return;
2661 }
2662
2663 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2664 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2665 for (; Field != FieldEnd; ++Field) {
2666 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002667 // FIXME: this isn't fully correct; we also need to test whether the
2668 // members of the union would all have the same calling convention as the
2669 // first member of the union. Checking just the size and alignment isn't
2670 // sufficient (consider structs passed on the stack instead of in registers
2671 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002672 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002673 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002674 // Warn if we drop the attribute.
2675 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002676 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002677 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002678 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002679 diag::warn_transparent_union_attribute_field_size_align)
2680 << isSize << Field->getDeclName() << FieldBits;
2681 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002682 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002683 diag::note_transparent_union_first_field_size_align)
2684 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002685 return;
2686 }
2687 }
2688
Michael Han99315932013-01-24 16:46:58 +00002689 RD->addAttr(::new (S.Context)
2690 TransparentUnionAttr(Attr.getRange(), S.Context,
2691 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002692}
2693
Chandler Carruthedc2c642011-07-02 00:01:44 +00002694static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002695 // Make sure that there is a string literal as the annotation's single
2696 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002697 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002698 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002699 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002700
2701 // Don't duplicate annotations that are already set.
2702 for (specific_attr_iterator<AnnotateAttr>
2703 i = D->specific_attr_begin<AnnotateAttr>(),
2704 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002705 if ((*i)->getAnnotation() == Str)
2706 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002707 }
Michael Han99315932013-01-24 16:46:58 +00002708
2709 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002710 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002711 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002712}
2713
Chandler Carruthedc2c642011-07-02 00:01:44 +00002714static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002715 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002716 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002717 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2718 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002719 return;
2720 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002721
Richard Smith848e1f12013-02-01 08:12:08 +00002722 if (Attr.getNumArgs() == 0) {
2723 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2724 true, 0, Attr.getAttributeSpellingListIndex()));
2725 return;
2726 }
2727
Aaron Ballman00e99962013-08-31 01:11:41 +00002728 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002729 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2730 S.Diag(Attr.getEllipsisLoc(),
2731 diag::err_pack_expansion_without_parameter_packs);
2732 return;
2733 }
2734
2735 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2736 return;
2737
2738 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2739 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002740}
2741
2742void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002743 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002744 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2745 SourceLocation AttrLoc = AttrRange.getBegin();
2746
Richard Smith1dba27c2013-01-29 09:02:09 +00002747 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002748 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002749 // C++11 [dcl.align]p1:
2750 // An alignment-specifier may be applied to a variable or to a class
2751 // data member, but it shall not be applied to a bit-field, a function
2752 // parameter, the formal parameter of a catch clause, or a variable
2753 // declared with the register storage class specifier. An
2754 // alignment-specifier may also be applied to the declaration of a class
2755 // or enumeration type.
2756 // C11 6.7.5/2:
2757 // An alignment attribute shall not be specified in a declaration of
2758 // a typedef, or a bit-field, or a function, or a parameter, or an
2759 // object declared with the register storage-class specifier.
2760 int DiagKind = -1;
2761 if (isa<ParmVarDecl>(D)) {
2762 DiagKind = 0;
2763 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2764 if (VD->getStorageClass() == SC_Register)
2765 DiagKind = 1;
2766 if (VD->isExceptionVariable())
2767 DiagKind = 2;
2768 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2769 if (FD->isBitField())
2770 DiagKind = 3;
2771 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00002772 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00002773 << (TmpAttr.isC11() ? ExpectedVariableOrField
2774 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002775 return;
2776 }
2777 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002778 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00002779 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002780 return;
2781 }
2782 }
2783
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002784 if (E->isTypeDependent() || E->isValueDependent()) {
2785 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002786 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2787 AA->setPackExpansion(IsPackExpansion);
2788 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002789 return;
2790 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002791
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002792 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002793 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002794 ExprResult ICE
2795 = VerifyIntegerConstantExpression(E, &Alignment,
2796 diag::err_aligned_attribute_argument_not_int,
2797 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002798 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002799 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002800
2801 // C++11 [dcl.align]p2:
2802 // -- if the constant expression evaluates to zero, the alignment
2803 // specifier shall have no effect
2804 // C11 6.7.5p6:
2805 // An alignment specification of zero has no effect.
2806 if (!(TmpAttr.isAlignas() && !Alignment) &&
2807 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002808 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2809 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002810 return;
2811 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002812
Richard Smith848e1f12013-02-01 08:12:08 +00002813 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00002814 // We've already verified it's a power of 2, now let's make sure it's
2815 // 8192 or less.
2816 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00002817 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00002818 << E->getSourceRange();
2819 return;
2820 }
2821 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002822
Richard Smith44c247f2013-02-22 08:32:16 +00002823 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2824 ICE.take(), SpellingListIndex);
2825 AA->setPackExpansion(IsPackExpansion);
2826 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002827}
2828
Michael Hanaf02bbe2013-02-01 01:19:17 +00002829void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002830 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002831 // FIXME: Cache the number on the Attr object if non-dependent?
2832 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002833 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2834 SpellingListIndex);
2835 AA->setPackExpansion(IsPackExpansion);
2836 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002837}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002838
Richard Smith848e1f12013-02-01 08:12:08 +00002839void Sema::CheckAlignasUnderalignment(Decl *D) {
2840 assert(D->hasAttrs() && "no attributes on decl");
2841
2842 QualType Ty;
2843 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2844 Ty = VD->getType();
2845 else
2846 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002847 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002848 return;
2849
2850 // C++11 [dcl.align]p5, C11 6.7.5/4:
2851 // The combined effect of all alignment attributes in a declaration shall
2852 // not specify an alignment that is less strict than the alignment that
2853 // would otherwise be required for the entity being declared.
2854 AlignedAttr *AlignasAttr = 0;
2855 unsigned Align = 0;
2856 for (specific_attr_iterator<AlignedAttr>
2857 I = D->specific_attr_begin<AlignedAttr>(),
2858 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
2859 if (I->isAlignmentDependent())
2860 return;
2861 if (I->isAlignas())
2862 AlignasAttr = *I;
2863 Align = std::max(Align, I->getAlignment(Context));
2864 }
2865
2866 if (AlignasAttr && Align) {
2867 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2868 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2869 if (NaturalAlign > RequestedAlign)
2870 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2871 << Ty << (unsigned)NaturalAlign.getQuantity();
2872 }
2873}
2874
David Majnemer2c4e00a2014-01-29 22:07:36 +00002875bool Sema::checkMSInheritanceAttrOnDefinition(
2876 CXXRecordDecl *RD, SourceRange Range,
2877 MSInheritanceAttr::Spelling SemanticSpelling) {
2878 assert(RD->hasDefinition() && "RD has no definition!");
2879
David Majnemer98c9ee22014-02-07 00:43:07 +00002880 // We may not have seen base specifiers or any virtual methods yet. We will
2881 // have to wait until the record is defined to catch any mismatches.
2882 if (!RD->getDefinition()->isCompleteDefinition())
2883 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002884
David Majnemer98c9ee22014-02-07 00:43:07 +00002885 // The unspecified model never matches what a definition could need.
2886 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
2887 return false;
2888
2889 if (RD->calculateInheritanceModel() == SemanticSpelling)
2890 return false;
2891
2892 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
2893 << 0 /*definition*/;
2894 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
2895 << RD->getNameAsString();
2896 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002897}
2898
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002899/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002900/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002901///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002902/// Despite what would be logical, the mode attribute is a decl attribute, not a
2903/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2904/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002905static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002906 // This attribute isn't documented, but glibc uses it. It changes
2907 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002908 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002909 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2910 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002911 return;
2912 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002913
Aaron Ballman00e99962013-08-31 01:11:41 +00002914 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2915 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002916
2917 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002918 if (Str.startswith("__") && Str.endswith("__"))
2919 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002920
2921 unsigned DestWidth = 0;
2922 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002923 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002924 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002925 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002926 switch (Str[0]) {
2927 case 'Q': DestWidth = 8; break;
2928 case 'H': DestWidth = 16; break;
2929 case 'S': DestWidth = 32; break;
2930 case 'D': DestWidth = 64; break;
2931 case 'X': DestWidth = 96; break;
2932 case 'T': DestWidth = 128; break;
2933 }
2934 if (Str[1] == 'F') {
2935 IntegerMode = false;
2936 } else if (Str[1] == 'C') {
2937 IntegerMode = false;
2938 ComplexMode = true;
2939 } else if (Str[1] != 'I') {
2940 DestWidth = 0;
2941 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002942 break;
2943 case 4:
2944 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2945 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002946 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002947 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002948 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002949 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002950 break;
2951 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002952 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002953 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002954 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002955 case 11:
2956 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00002957 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002958 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002959 }
2960
2961 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002962 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002963 OldTy = TD->getUnderlyingType();
2964 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2965 OldTy = VD->getType();
2966 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002967 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00002968 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002969 return;
2970 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002971
John McCall9dd450b2009-09-21 23:43:11 +00002972 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002973 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2974 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002975 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002976 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2977 } else if (ComplexMode) {
2978 if (!OldTy->isComplexType())
2979 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2980 } else {
2981 if (!OldTy->isFloatingType())
2982 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2983 }
2984
Mike Stump87c57ac2009-05-16 07:39:55 +00002985 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2986 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002987 // FIXME: Make sure floating-point mappings are accurate
2988 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002989 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00002990 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002991 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002992 }
2993
2994 QualType NewTy;
2995
2996 if (IntegerMode)
2997 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
2998 OldTy->isSignedIntegerType());
2999 else
3000 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3001
3002 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00003003 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003004 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003005 }
3006
Eli Friedman4735374e2009-03-03 06:41:03 +00003007 if (ComplexMode) {
3008 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003009 }
3010
3011 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003012 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3013 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3014 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003015 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003016
3017 D->addAttr(::new (S.Context)
3018 ModeAttr(Attr.getRange(), S.Context, Name,
3019 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003020}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003021
Chandler Carruthedc2c642011-07-02 00:01:44 +00003022static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003023 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3024 if (!VD->hasGlobalStorage())
3025 S.Diag(Attr.getLoc(),
3026 diag::warn_attribute_requires_functions_or_static_globals)
3027 << Attr.getName();
3028 } else if (!isFunctionOrMethod(D)) {
3029 S.Diag(Attr.getLoc(),
3030 diag::warn_attribute_requires_functions_or_static_globals)
3031 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003032 return;
3033 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003034
Michael Han99315932013-01-24 16:46:58 +00003035 D->addAttr(::new (S.Context)
3036 NoDebugAttr(Attr.getRange(), S.Context,
3037 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003038}
3039
Chandler Carruthedc2c642011-07-02 00:01:44 +00003040static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003041 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003042 if (!FD->getReturnType()->isVoidType()) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003043 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
3044 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
3045 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3046 << FD->getType()
Alp Toker42a16a62014-01-25 23:51:36 +00003047 << FixItHint::CreateReplacement(FTL.getReturnLoc().getSourceRange(),
Aaron Ballman3aff6332013-12-02 19:30:36 +00003048 "void");
3049 } else {
3050 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3051 << FD->getType();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003052 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003053 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003054 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003055
Aaron Ballman3aff6332013-12-02 19:30:36 +00003056 D->addAttr(::new (S.Context)
3057 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003058 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003059}
3060
Chandler Carruthedc2c642011-07-02 00:01:44 +00003061static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003062 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003063 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003064 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003065 return;
3066 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003067
Michael Han99315932013-01-24 16:46:58 +00003068 D->addAttr(::new (S.Context)
3069 GNUInlineAttr(Attr.getRange(), S.Context,
3070 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003071}
3072
Chandler Carruthedc2c642011-07-02 00:01:44 +00003073static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003074 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003075
Aaron Ballman02df2e02012-12-09 17:45:41 +00003076 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003077 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003078 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3079 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003080 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003081 return;
3082
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003083 if (!isa<ObjCMethodDecl>(D)) {
3084 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3085 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003086 return;
3087 }
3088
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003089 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003090 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003091 D->addAttr(::new (S.Context)
3092 FastCallAttr(Attr.getRange(), S.Context,
3093 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003094 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003095 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003096 D->addAttr(::new (S.Context)
3097 StdCallAttr(Attr.getRange(), S.Context,
3098 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003099 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003100 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003101 D->addAttr(::new (S.Context)
3102 ThisCallAttr(Attr.getRange(), S.Context,
3103 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003104 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003105 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003106 D->addAttr(::new (S.Context)
3107 CDeclAttr(Attr.getRange(), S.Context,
3108 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003109 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003110 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003111 D->addAttr(::new (S.Context)
3112 PascalAttr(Attr.getRange(), S.Context,
3113 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003114 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003115 case AttributeList::AT_MSABI:
3116 D->addAttr(::new (S.Context)
3117 MSABIAttr(Attr.getRange(), S.Context,
3118 Attr.getAttributeSpellingListIndex()));
3119 return;
3120 case AttributeList::AT_SysVABI:
3121 D->addAttr(::new (S.Context)
3122 SysVABIAttr(Attr.getRange(), S.Context,
3123 Attr.getAttributeSpellingListIndex()));
3124 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003125 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003126 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003127 switch (CC) {
3128 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003129 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003130 break;
3131 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003132 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003133 break;
3134 default:
3135 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003136 }
3137
Michael Han99315932013-01-24 16:46:58 +00003138 D->addAttr(::new (S.Context)
3139 PcsAttr(Attr.getRange(), S.Context, PCS,
3140 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003141 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003142 }
Derek Schuffa2020962012-10-16 22:30:41 +00003143 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003144 D->addAttr(::new (S.Context)
3145 PnaclCallAttr(Attr.getRange(), S.Context,
3146 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003147 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003148 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003149 D->addAttr(::new (S.Context)
3150 IntelOclBiccAttr(Attr.getRange(), S.Context,
3151 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003152 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003153
Abramo Bagnara50099372010-04-30 13:10:51 +00003154 default:
3155 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003156 }
3157}
3158
Aaron Ballman02df2e02012-12-09 17:45:41 +00003159bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3160 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003161 if (attr.isInvalid())
3162 return true;
3163
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003164 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003165 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003166 attr.setInvalid();
3167 return true;
3168 }
3169
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003170 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003171 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003172 case AttributeList::AT_CDecl: CC = CC_C; break;
3173 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3174 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3175 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3176 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003177 case AttributeList::AT_MSABI:
3178 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3179 CC_X86_64Win64;
3180 break;
3181 case AttributeList::AT_SysVABI:
3182 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3183 CC_C;
3184 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003185 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003186 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003187 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003188 attr.setInvalid();
3189 return true;
3190 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003191 if (StrRef == "aapcs") {
3192 CC = CC_AAPCS;
3193 break;
3194 } else if (StrRef == "aapcs-vfp") {
3195 CC = CC_AAPCS_VFP;
3196 break;
3197 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003198
3199 attr.setInvalid();
3200 Diag(attr.getLoc(), diag::err_invalid_pcs);
3201 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003202 }
Derek Schuffa2020962012-10-16 22:30:41 +00003203 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003204 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003205 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003206 }
3207
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003208 const TargetInfo &TI = Context.getTargetInfo();
3209 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3210 if (A == TargetInfo::CCCR_Warning) {
3211 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003212
3213 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3214 if (FD)
3215 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3216 TargetInfo::CCMT_NonMember;
3217 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003218 }
3219
John McCall3882ace2011-01-05 12:14:39 +00003220 return false;
3221}
3222
John McCall3882ace2011-01-05 12:14:39 +00003223/// Checks a regparm attribute, returning true if it is ill-formed and
3224/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003225bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3226 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003227 return true;
3228
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003229 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003230 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003231 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003232 }
Eli Friedman7044b762009-03-27 21:06:47 +00003233
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003234 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003235 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003236 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003237 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003238 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003239 }
3240
Douglas Gregore8bbc122011-09-02 00:18:52 +00003241 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003242 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003243 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003244 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003245 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003246 }
3247
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003248 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003249 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003250 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003251 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003252 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003253 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003254 }
3255
John McCall3882ace2011-01-05 12:14:39 +00003256 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003257}
3258
Aaron Ballman66039932013-12-19 00:41:31 +00003259static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3260 const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003261 // check the attribute arguments.
3262 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3263 // FIXME: 0 is not okay.
Aaron Ballman05e420a2014-01-02 21:26:14 +00003264 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3265 << Attr.getName() << 2;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003266 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003267 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003268
Aaron Ballman66039932013-12-19 00:41:31 +00003269 uint32_t MaxThreads, MinBlocks = 0;
3270 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3271 return;
3272 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3273 Attr.getArgAsExpr(1),
3274 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003275 return;
3276
3277 D->addAttr(::new (S.Context)
3278 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3279 MaxThreads, MinBlocks,
3280 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003281}
3282
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003283static void handleArgumentWithTypeTagAttr(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() << /* arg num = */ 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, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003292 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003293
Aaron Ballman00e99962013-08-31 01:11:41 +00003294 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003295
3296 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3297 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3298 << Attr.getName() << ExpectedFunctionOrMethod;
3299 return;
3300 }
3301
3302 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003303 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3304 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003305 return;
3306
3307 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003308 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3309 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003310 return;
3311
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003312 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003313 if (IsPointer) {
3314 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003315 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003316 if (!BufferTy->isPointerType()) {
3317 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003318 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003319 }
3320 }
3321
Michael Han99315932013-01-24 16:46:58 +00003322 D->addAttr(::new (S.Context)
3323 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3324 ArgumentIdx, TypeTagIdx, IsPointer,
3325 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003326}
3327
3328static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3329 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003330 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003331 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003332 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003333 return;
3334 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003335
3336 if (!checkAttributeNumArgs(S, Attr, 1))
3337 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003338
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003339 if (!isa<VarDecl>(D)) {
3340 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3341 << Attr.getName() << ExpectedVariable;
3342 return;
3343 }
3344
Aaron Ballman00e99962013-08-31 01:11:41 +00003345 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003346 TypeSourceInfo *MatchingCTypeLoc = 0;
3347 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3348 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003349
Michael Han99315932013-01-24 16:46:58 +00003350 D->addAttr(::new (S.Context)
3351 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003352 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003353 Attr.getLayoutCompatible(),
3354 Attr.getMustBeNull(),
3355 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003356}
3357
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003358//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003359// Checker-specific attribute handlers.
3360//===----------------------------------------------------------------------===//
3361
John McCalled433932011-01-25 03:31:58 +00003362static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003363 return type->isDependentType() ||
3364 type->isObjCObjectPointerType() ||
3365 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003366}
3367static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003368 return type->isDependentType() ||
3369 type->isPointerType() ||
3370 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003371}
3372
Chandler Carruthedc2c642011-07-02 00:01:44 +00003373static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003374 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003375 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003376
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003377 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003378 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3379 cf = false;
3380 } else {
3381 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3382 cf = true;
3383 }
3384
3385 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003386 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003387 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003388 return;
3389 }
3390
3391 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003392 param->addAttr(::new (S.Context)
3393 CFConsumedAttr(Attr.getRange(), S.Context,
3394 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003395 else
Michael Han99315932013-01-24 16:46:58 +00003396 param->addAttr(::new (S.Context)
3397 NSConsumedAttr(Attr.getRange(), S.Context,
3398 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003399}
3400
Chandler Carruthedc2c642011-07-02 00:01:44 +00003401static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3402 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003403
John McCalled433932011-01-25 03:31:58 +00003404 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003405
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003406 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003407 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003408 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003409 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003410 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003411 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3412 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003413 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003414 returnType = FD->getReturnType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003415 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003416 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003417 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003418 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003419 return;
3420 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003421
John McCalled433932011-01-25 03:31:58 +00003422 bool typeOK;
3423 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003424 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003425 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003426 case AttributeList::AT_NSReturnsAutoreleased:
3427 case AttributeList::AT_NSReturnsRetained:
3428 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003429 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3430 cf = false;
3431 break;
3432
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003433 case AttributeList::AT_CFReturnsRetained:
3434 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003435 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3436 cf = true;
3437 break;
3438 }
3439
3440 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003441 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003442 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003443 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003444 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003445
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003446 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003447 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003448 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003449 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003450 D->addAttr(::new (S.Context)
3451 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3452 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003453 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003454 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003455 D->addAttr(::new (S.Context)
3456 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3457 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003458 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003459 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003460 D->addAttr(::new (S.Context)
3461 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3462 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003463 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003464 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003465 D->addAttr(::new (S.Context)
3466 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3467 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003468 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003469 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003470 D->addAttr(::new (S.Context)
3471 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3472 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003473 return;
3474 };
3475}
3476
John McCallcf166702011-07-22 08:53:00 +00003477static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3478 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003479 const int EP_ObjCMethod = 1;
3480 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003481
John McCallcf166702011-07-22 08:53:00 +00003482 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003483 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003484 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003485 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003486 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003487 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003488
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003489 if (!resultType->isReferenceType() &&
3490 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003491 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003492 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003493 << attr.getName()
3494 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003495 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003496
3497 // Drop the attribute.
3498 return;
3499 }
3500
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003501 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003502 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3503 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003504}
3505
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003506static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3507 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003508 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003509
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003510 DeclContext *DC = method->getDeclContext();
3511 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3512 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3513 << attr.getName() << 0;
3514 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3515 return;
3516 }
3517 if (method->getMethodFamily() == OMF_dealloc) {
3518 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3519 << attr.getName() << 1;
3520 return;
3521 }
3522
Michael Han99315932013-01-24 16:46:58 +00003523 method->addAttr(::new (S.Context)
3524 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3525 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003526}
3527
Aaron Ballmanfb763042013-12-02 18:05:46 +00003528static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3529 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003530 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00003531 return;
John McCall32f5fe12011-09-30 05:12:12 +00003532
Aaron Ballmanfb763042013-12-02 18:05:46 +00003533 D->addAttr(::new (S.Context)
3534 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3535 Attr.getAttributeSpellingListIndex()));
3536}
3537
3538static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3539 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003540 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00003541 return;
3542
3543 D->addAttr(::new (S.Context)
3544 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3545 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003546}
3547
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003548static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3549 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003550 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003551
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003552 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003553 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003554 return;
3555 }
3556
3557 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003558 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003559 Attr.getAttributeSpellingListIndex()));
3560}
3561
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003562static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3563 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003564 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003565
3566 if (!Parm) {
3567 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3568 return;
3569 }
3570
3571 D->addAttr(::new (S.Context)
3572 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3573 Attr.getAttributeSpellingListIndex()));
3574}
3575
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003576static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3577 const AttributeList &Attr) {
3578 IdentifierInfo *RelatedClass =
3579 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0;
3580 if (!RelatedClass) {
3581 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3582 return;
3583 }
3584 IdentifierInfo *ClassMethod =
3585 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0;
3586 IdentifierInfo *InstanceMethod =
3587 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0;
3588 D->addAttr(::new (S.Context)
3589 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3590 ClassMethod, InstanceMethod,
3591 Attr.getAttributeSpellingListIndex()));
3592}
3593
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003594static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3595 const AttributeList &Attr) {
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003596 ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003597 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003598 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003599 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3600 Attr.getAttributeSpellingListIndex()));
3601}
3602
Chandler Carruthedc2c642011-07-02 00:01:44 +00003603static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3604 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003605 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003606
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003607 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003608 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003609}
3610
Chandler Carruthedc2c642011-07-02 00:01:44 +00003611static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3612 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003613 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003614 QualType type = vd->getType();
3615
3616 if (!type->isDependentType() &&
3617 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003618 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003619 << type;
3620 return;
3621 }
3622
3623 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3624
3625 // If we have no lifetime yet, check the lifetime we're presumably
3626 // going to infer.
3627 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3628 lifetime = type->getObjCARCImplicitLifetime();
3629
3630 switch (lifetime) {
3631 case Qualifiers::OCL_None:
3632 assert(type->isDependentType() &&
3633 "didn't infer lifetime for non-dependent type?");
3634 break;
3635
3636 case Qualifiers::OCL_Weak: // meaningful
3637 case Qualifiers::OCL_Strong: // meaningful
3638 break;
3639
3640 case Qualifiers::OCL_ExplicitNone:
3641 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003642 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003643 << (lifetime == Qualifiers::OCL_Autoreleasing);
3644 break;
3645 }
3646
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003647 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003648 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3649 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003650}
3651
Francois Picheta83957a2010-12-19 06:50:37 +00003652//===----------------------------------------------------------------------===//
3653// Microsoft specific attribute handlers.
3654//===----------------------------------------------------------------------===//
3655
Chandler Carruthedc2c642011-07-02 00:01:44 +00003656static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003657 if (!S.LangOpts.CPlusPlus) {
3658 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3659 << Attr.getName() << AttributeLangSupport::C;
3660 return;
3661 }
3662
Aaron Ballman60e705e2013-11-24 20:58:02 +00003663 if (!isa<CXXRecordDecl>(D)) {
3664 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3665 << Attr.getName() << ExpectedClass;
3666 return;
3667 }
3668
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003669 StringRef StrRef;
3670 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003671 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003672 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003673
David Majnemer89085342013-08-09 08:56:20 +00003674 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3675 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003676 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3677 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003678
Reid Kleckner140c4a72013-05-17 14:04:52 +00003679 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003680 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003681 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003682 return;
3683 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003684
David Majnemer89085342013-08-09 08:56:20 +00003685 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003686 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003687 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003688 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003689 return;
3690 }
David Majnemer89085342013-08-09 08:56:20 +00003691 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003692 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003693 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003694 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003695 }
Francois Picheta83957a2010-12-19 06:50:37 +00003696
David Majnemer89085342013-08-09 08:56:20 +00003697 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3698 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003699}
3700
David Majnemer2c4e00a2014-01-29 22:07:36 +00003701static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3702 if (!S.LangOpts.CPlusPlus) {
3703 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3704 << Attr.getName() << AttributeLangSupport::C;
3705 return;
3706 }
3707 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
3708 D, Attr.getRange(), Attr.getAttributeSpellingListIndex(),
3709 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
3710 if (IA)
3711 D->addAttr(IA);
3712}
3713
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003714static void handleARMInterruptAttr(Sema &S, Decl *D,
3715 const AttributeList &Attr) {
3716 // Check the attribute arguments.
3717 if (Attr.getNumArgs() > 1) {
3718 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3719 << Attr.getName() << 1;
3720 return;
3721 }
3722
3723 StringRef Str;
3724 SourceLocation ArgLoc;
3725
3726 if (Attr.getNumArgs() == 0)
3727 Str = "";
3728 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
3729 return;
3730
3731 ARMInterruptAttr::InterruptType Kind;
3732 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
3733 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
3734 << Attr.getName() << Str << ArgLoc;
3735 return;
3736 }
3737
3738 unsigned Index = Attr.getAttributeSpellingListIndex();
3739 D->addAttr(::new (S.Context)
3740 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
3741}
3742
3743static void handleMSP430InterruptAttr(Sema &S, Decl *D,
3744 const AttributeList &Attr) {
3745 if (!checkAttributeNumArgs(S, Attr, 1))
3746 return;
3747
3748 if (!Attr.isArgExpr(0)) {
3749 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3750 << AANT_ArgumentIntegerConstant;
3751 return;
3752 }
3753
3754 // FIXME: Check for decl - it should be void ()(void).
3755
3756 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3757 llvm::APSInt NumParams(32);
3758 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
3759 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3760 << Attr.getName() << AANT_ArgumentIntegerConstant
3761 << NumParamsExpr->getSourceRange();
3762 return;
3763 }
3764
3765 unsigned Num = NumParams.getLimitedValue(255);
3766 if ((Num & 1) || Num > 30) {
3767 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3768 << Attr.getName() << (int)NumParams.getSExtValue()
3769 << NumParamsExpr->getSourceRange();
3770 return;
3771 }
3772
Aaron Ballman36a53502014-01-16 13:03:14 +00003773 D->addAttr(::new (S.Context)
3774 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
3775 Attr.getAttributeSpellingListIndex()));
3776 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003777}
3778
3779static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3780 // Dispatch the interrupt attribute based on the current target.
3781 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
3782 handleMSP430InterruptAttr(S, D, Attr);
3783 else
3784 handleARMInterruptAttr(S, D, Attr);
3785}
3786
3787static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
3788 const AttributeList& Attr) {
3789 // If we try to apply it to a function pointer, don't warn, but don't
3790 // do anything, either. It doesn't matter anyway, because there's nothing
3791 // special about calling a force_align_arg_pointer function.
3792 ValueDecl *VD = dyn_cast<ValueDecl>(D);
3793 if (VD && VD->getType()->isFunctionPointerType())
3794 return;
3795 // Also don't warn on function pointer typedefs.
3796 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
3797 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
3798 TD->getUnderlyingType()->isFunctionType()))
3799 return;
3800 // Attribute can only be applied to function types.
3801 if (!isa<FunctionDecl>(D)) {
3802 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3803 << Attr.getName() << /* function */0;
3804 return;
3805 }
3806
Aaron Ballman36a53502014-01-16 13:03:14 +00003807 D->addAttr(::new (S.Context)
3808 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
3809 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003810}
3811
3812DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
3813 unsigned AttrSpellingListIndex) {
3814 if (D->hasAttr<DLLExportAttr>()) {
3815 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "dllimport";
3816 return NULL;
3817 }
3818
3819 if (D->hasAttr<DLLImportAttr>())
3820 return NULL;
3821
3822 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3823 if (VD->hasDefinition()) {
3824 // dllimport cannot be applied to definitions.
3825 Diag(D->getLocation(), diag::warn_attribute_invalid_on_definition)
3826 << "dllimport";
3827 return NULL;
3828 }
3829 }
3830
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003831 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003832}
3833
3834static void handleDLLImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3835 // Attribute can be applied only to functions or variables.
3836 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3837 if (!FD && !isa<VarDecl>(D)) {
3838 // Apparently Visual C++ thinks it is okay to not emit a warning
3839 // in this case, so only emit a warning when -fms-extensions is not
3840 // specified.
3841 if (!S.getLangOpts().MicrosoftExt)
3842 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003843 << Attr.getName() << ExpectedVariableOrFunction;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003844 return;
3845 }
3846
3847 // Currently, the dllimport attribute is ignored for inlined functions.
3848 // Warning is emitted.
3849 if (FD && FD->isInlineSpecified()) {
3850 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3851 return;
3852 }
3853
3854 unsigned Index = Attr.getAttributeSpellingListIndex();
3855 DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index);
3856 if (NewAttr)
3857 D->addAttr(NewAttr);
3858}
3859
3860DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
3861 unsigned AttrSpellingListIndex) {
3862 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
3863 Diag(Import->getLocation(), diag::warn_attribute_ignored) << "dllimport";
3864 D->dropAttr<DLLImportAttr>();
3865 }
3866
3867 if (D->hasAttr<DLLExportAttr>())
3868 return NULL;
3869
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003870 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003871}
3872
3873static void handleDLLExportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3874 // Currently, the dllexport attribute is ignored for inlined functions, unless
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003875 // the -fkeep-inline-functions flag has been used. Warning is emitted.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003876 if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isInlineSpecified()) {
3877 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
3878 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3879 return;
3880 }
3881
3882 unsigned Index = Attr.getAttributeSpellingListIndex();
3883 DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index);
3884 if (NewAttr)
3885 D->addAttr(NewAttr);
3886}
3887
David Majnemer2c4e00a2014-01-29 22:07:36 +00003888MSInheritanceAttr *
3889Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range,
3890 unsigned AttrSpellingListIndex,
3891 MSInheritanceAttr::Spelling SemanticSpelling) {
3892 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
3893 if (IA->getSemanticSpelling() == SemanticSpelling)
3894 return 0;
3895 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
3896 << 1 /*previous declaration*/;
3897 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
3898 D->dropAttr<MSInheritanceAttr>();
3899 }
3900
3901 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
3902 if (RD->hasDefinition()) {
3903 if (checkMSInheritanceAttrOnDefinition(RD, Range, SemanticSpelling)) {
3904 return 0;
3905 }
3906 } else {
3907 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
3908 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
3909 << 1 /*partial specialization*/;
3910 return 0;
3911 }
3912 if (RD->getDescribedClassTemplate()) {
3913 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
3914 << 0 /*primary template*/;
3915 return 0;
3916 }
3917 }
3918
3919 return ::new (Context)
3920 MSInheritanceAttr(Range, Context, AttrSpellingListIndex);
3921}
3922
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003923/// Handles semantic checking for features that are common to all attributes,
3924/// such as checking whether a parameter was properly specified, or the correct
3925/// number of arguments were passed, etc.
3926static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3927 const AttributeList &Attr) {
3928 // Several attributes carry different semantics than the parsing requires, so
3929 // those are opted out of the common handling.
3930 //
3931 // We also bail on unknown and ignored attributes because those are handled
3932 // as part of the target-specific handling logic.
3933 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003934 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003935 return false;
3936
Aaron Ballman3aff6332013-12-02 19:30:36 +00003937 // Check whether the attribute requires specific language extensions to be
3938 // enabled.
3939 if (!Attr.diagnoseLangOpts(S))
3940 return true;
3941
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003942 // If there are no optional arguments, then checking for the argument count
3943 // is trivial.
3944 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
3945 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
3946 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003947
3948 // Check whether the attribute appertains to the given subject.
3949 if (!Attr.diagnoseAppertainsTo(S, D))
3950 return true;
3951
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003952 return false;
3953}
3954
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003955//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003956// Top Level Sema Entry Points
3957//===----------------------------------------------------------------------===//
3958
Richard Smithf8a75c32013-08-29 00:47:48 +00003959/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3960/// the attribute applies to decls. If the attribute is a type attribute, just
3961/// silently ignore it if a GNU attribute.
3962static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3963 const AttributeList &Attr,
3964 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003965 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00003966 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003967
Richard Smithf8a75c32013-08-29 00:47:48 +00003968 // Ignore C++11 attributes on declarator chunks: they appertain to the type
3969 // instead.
3970 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
3971 return;
3972
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003973 // Unknown attributes are automatically warned on. Target-specific attributes
3974 // which do not apply to the current target architecture are treated as
3975 // though they were unknown attributes.
3976 if (Attr.getKind() == AttributeList::UnknownAttribute ||
3977 !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) {
3978 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
3979 diag::warn_unhandled_ms_attribute_ignored :
3980 diag::warn_unknown_attribute_ignored) << Attr.getName();
3981 return;
3982 }
3983
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003984 if (handleCommonAttributeFeatures(S, scope, D, Attr))
3985 return;
3986
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003987 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003988 default:
3989 // Type attributes are handled elsewhere; silently move on.
3990 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
3991 break;
3992 case AttributeList::AT_Interrupt:
3993 handleInterruptAttr(S, D, Attr); break;
3994 case AttributeList::AT_X86ForceAlignArgPointer:
3995 handleX86ForceAlignArgPointerAttr(S, D, Attr); break;
3996 case AttributeList::AT_DLLExport:
3997 handleDLLExportAttr(S, D, Attr); break;
3998 case AttributeList::AT_DLLImport:
3999 handleDLLImportAttr(S, D, Attr); break;
4000 case AttributeList::AT_Mips16:
4001 handleSimpleAttribute<Mips16Attr>(S, D, Attr); break;
4002 case AttributeList::AT_NoMips16:
4003 handleSimpleAttribute<NoMips16Attr>(S, D, Attr); break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00004004 case AttributeList::AT_IBAction:
4005 handleSimpleAttribute<IBActionAttr>(S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004006 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4007 case AttributeList::AT_IBOutletCollection:
4008 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004009 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4010 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004011 case AttributeList::AT_AlwaysInline:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004012 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004013 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004014 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004015 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004016 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4017 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4018 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004019 handleDependencyAttr(S, scope, D, Attr);
4020 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004021 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004022 case AttributeList::AT_CUDAConstant:
4023 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004024 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004025 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman3a8e2d92013-11-27 18:53:58 +00004026 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004027 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004028 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004029 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004030 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
Nick Lewycky35a6ef42014-01-11 02:50:57 +00004031 case AttributeList::AT_EnableIf: handleEnableIfAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004032 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004033 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004034 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004035 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004036 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004037 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004038 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4039 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4040 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00004041 case AttributeList::AT_CUDADevice:
4042 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004043 case AttributeList::AT_CUDAHost:
4044 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004045 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4046 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004047 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004048 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004049 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004050 case AttributeList::AT_MayAlias:
4051 handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004052 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004053 case AttributeList::AT_NoCommon:
4054 handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00004055 case AttributeList::AT_NonNull:
4056 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
4057 handleNonNullAttrParameter(S, PVD, Attr);
4058 else
4059 handleNonNullAttr(S, D, Attr);
4060 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00004061 case AttributeList::AT_ReturnsNonNull:
4062 handleReturnsNonNullAttr(S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004063 case AttributeList::AT_Overloadable:
4064 handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00004065 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004066 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4067 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004068 case AttributeList::AT_Naked:
4069 handleSimpleAttribute<NakedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004070 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004071 case AttributeList::AT_NoThrow:
4072 handleSimpleAttribute<NoThrowAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004073 case AttributeList::AT_CUDAShared:
4074 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004075 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004076
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004077 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004078 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004079 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004080 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004081
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004082 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004083 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4084
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004085 case AttributeList::AT_ObjCRequiresSuper:
4086 handleObjCRequiresSuperAttr(S, D, Attr); break;
4087
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004088 case AttributeList::AT_ObjCBridge:
4089 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004090
4091 case AttributeList::AT_ObjCBridgeMutable:
4092 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004093
4094 case AttributeList::AT_ObjCBridgeRelated:
4095 handleObjCBridgeRelatedAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00004096
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004097 case AttributeList::AT_ObjCDesignatedInitializer:
4098 handleObjCDesignatedInitializer(S, D, Attr); break;
4099
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004100 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00004101 handleCFAuditedTransferAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004102 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00004103 handleCFUnknownTransferAttr(S, D, Attr); break;
John McCall32f5fe12011-09-30 05:12:12 +00004104
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004105 case AttributeList::AT_CFConsumed:
4106 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4107 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004108 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004109
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004110 case AttributeList::AT_NSReturnsAutoreleased:
4111 case AttributeList::AT_NSReturnsNotRetained:
4112 case AttributeList::AT_CFReturnsNotRetained:
4113 case AttributeList::AT_NSReturnsRetained:
4114 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004115 handleNSReturnsRetainedAttr(S, D, Attr); break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004116 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004117 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004118 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004119 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004120 case AttributeList::AT_VecTypeHint:
4121 handleVecTypeHint(S, D, Attr); break;
4122
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004123 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004124 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004125
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004126 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4127 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4128 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004129 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004130 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004131 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004132 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004133 case AttributeList::AT_ObjCRootClass:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004134 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004135 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek28eace62013-11-23 01:01:34 +00004136 handleObjCSuppresProtocolAttr(S, D, Attr);
4137 break;
4138 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004139 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00004140 case AttributeList::AT_Unused:
4141 handleSimpleAttribute<UnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004142 case AttributeList::AT_ReturnsTwice:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004143 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004144 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004145 case AttributeList::AT_Visibility:
4146 handleVisibilityAttr(S, D, Attr, false);
4147 break;
4148 case AttributeList::AT_TypeVisibility:
4149 handleVisibilityAttr(S, D, Attr, true);
4150 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004151 case AttributeList::AT_WarnUnused:
Aaron Ballman6a42b5a2013-11-27 16:59:17 +00004152 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004153 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004154 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004155 case AttributeList::AT_Weak:
4156 handleSimpleAttribute<WeakAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004157 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4158 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4159 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004160 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004161 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004162 case AttributeList::AT_ObjCException:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004163 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004164 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004165 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004166 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004167 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4168 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4169 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004170 case AttributeList::AT_Const:
4171 handleSimpleAttribute<ConstAttr>(S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004172 case AttributeList::AT_Pure:
4173 handleSimpleAttribute<PureAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004174 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4175 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004176 case AttributeList::AT_NoInline:
4177 handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004178 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004179 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004180 case AttributeList::AT_StdCall:
4181 case AttributeList::AT_CDecl:
4182 case AttributeList::AT_FastCall:
4183 case AttributeList::AT_ThisCall:
4184 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004185 case AttributeList::AT_MSABI:
4186 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004187 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004188 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004189 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004190 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004191 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004192 case AttributeList::AT_OpenCLKernel:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004193 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004194 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman26891332014-01-14 17:41:53 +00004195 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr); break;
John McCall8d32c052012-05-22 21:28:12 +00004196
4197 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004198 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004199 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004200 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004201 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004202 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004203 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004204 case AttributeList::AT_MSInheritance:
David Majnemer2c4e00a2014-01-29 22:07:36 +00004205 handleMSInheritanceAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004206 case AttributeList::AT_ForceInline:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004207 handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004208 case AttributeList::AT_SelectAny:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004209 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004210
4211 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004212 case AttributeList::AT_AssertExclusiveLock:
4213 handleAssertExclusiveLockAttr(S, D, Attr);
4214 break;
4215 case AttributeList::AT_AssertSharedLock:
4216 handleAssertSharedLockAttr(S, D, Attr);
4217 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004218 case AttributeList::AT_GuardedVar:
Aaron Ballmane61b8b82013-12-02 15:02:49 +00004219 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004220 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004221 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004222 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004223 case AttributeList::AT_ScopedLockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004224 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004225 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004226 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004227 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004228 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004229 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004230 break;
4231 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004232 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004233 break;
4234 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004235 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004236 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004237 case AttributeList::AT_Lockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004238 handleSimpleAttribute<LockableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004239 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004240 handleGuardedByAttr(S, D, Attr);
4241 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004242 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004243 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004244 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004245 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004246 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004247 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004248 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004249 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004250 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004251 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004252 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004253 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004254 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004255 handleLockReturnedAttr(S, D, Attr);
4256 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004257 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004258 handleLocksExcludedAttr(S, D, Attr);
4259 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004260 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004261 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004262 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004263 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004264 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004265 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004266 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004267 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004268 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004269 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004270 handleUnlockFunAttr(S, D, Attr);
4271 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004272 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004273 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004274 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004275 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004276 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004277 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004278
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004279 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004280 case AttributeList::AT_Consumable:
4281 handleConsumableAttr(S, D, Attr);
4282 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004283 case AttributeList::AT_ConsumableAutoCast:
4284 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr); break;
4285 break;
4286 case AttributeList::AT_ConsumableSetOnRead:
4287 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr); break;
4288 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004289 case AttributeList::AT_CallableWhen:
4290 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004291 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004292 case AttributeList::AT_ParamTypestate:
4293 handleParamTypestateAttr(S, D, Attr);
4294 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004295 case AttributeList::AT_ReturnTypestate:
4296 handleReturnTypestateAttr(S, D, Attr);
4297 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004298 case AttributeList::AT_SetTypestate:
4299 handleSetTypestateAttr(S, D, Attr);
4300 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004301 case AttributeList::AT_TestTypestate:
4302 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004303 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004304
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004305 // Type safety attributes.
4306 case AttributeList::AT_ArgumentWithTypeTag:
4307 handleArgumentWithTypeTagAttr(S, D, Attr);
4308 break;
4309 case AttributeList::AT_TypeTagForDatatype:
4310 handleTypeTagForDatatypeAttr(S, D, Attr);
4311 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004312 }
4313}
4314
4315/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4316/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004317void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004318 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004319 bool IncludeCXX11Attributes) {
4320 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004321 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004322
Joey Gouly2cd9db12013-12-13 16:15:28 +00004323 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004324 // GCC accepts
4325 // static int a9 __attribute__((weakref));
4326 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004327 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00004328 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
4329 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00004330 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004331 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004332 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004333
4334 if (!D->hasAttr<OpenCLKernelAttr>()) {
4335 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004336 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
4337 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004338 D->setInvalidDecl();
4339 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004340 if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
4341 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004342 D->setInvalidDecl();
4343 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004344 if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
4345 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004346 D->setInvalidDecl();
4347 }
4348 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004349}
4350
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004351// Annotation attributes are the only attributes allowed after an access
4352// specifier.
4353bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4354 const AttributeList *AttrList) {
4355 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004356 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004357 handleAnnotateAttr(*this, ASDecl, *l);
4358 } else {
4359 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4360 return true;
4361 }
4362 }
4363
4364 return false;
4365}
4366
John McCall42856de2011-10-01 05:17:03 +00004367/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4368/// contains any decl attributes that we should warn about.
4369static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4370 for ( ; A; A = A->getNext()) {
4371 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004372 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004373 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4374
4375 if (A->getKind() == AttributeList::UnknownAttribute) {
4376 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4377 << A->getName() << A->getRange();
4378 } else {
4379 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4380 << A->getName() << A->getRange();
4381 }
4382 }
4383}
4384
4385/// checkUnusedDeclAttributes - Given a declarator which is not being
4386/// used to build a declaration, complain about any decl attributes
4387/// which might be lying around on it.
4388void Sema::checkUnusedDeclAttributes(Declarator &D) {
4389 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4390 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4391 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4392 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4393}
4394
Ryan Flynn7d470f32009-07-30 03:15:39 +00004395/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004396/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004397NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4398 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004399 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004400 NamedDecl *NewD = 0;
4401 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004402 FunctionDecl *NewFD;
4403 // FIXME: Missing call to CheckFunctionDeclaration().
4404 // FIXME: Mangling?
4405 // FIXME: Is the qualifier info correct?
4406 // FIXME: Is the DeclContext correct?
4407 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4408 Loc, Loc, DeclarationName(II),
4409 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004410 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004411 FD->hasPrototype(),
4412 false/*isConstexprSpecified*/);
4413 NewD = NewFD;
4414
4415 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004416 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004417
4418 // Fake up parameter variables; they are declared as if this were
4419 // a typedef.
4420 QualType FDTy = FD->getType();
4421 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4422 SmallVector<ParmVarDecl*, 16> Params;
Alp Toker9cacbab2014-01-20 20:26:09 +00004423 for (FunctionProtoType::param_type_iterator AI = FT->param_type_begin(),
4424 AE = FT->param_type_end();
4425 AI != AE; ++AI) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004426 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4427 Param->setScopeInfo(0, Params.size());
4428 Params.push_back(Param);
4429 }
David Blaikie9c70e042011-09-21 18:16:56 +00004430 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004431 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004432 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4433 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004434 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004435 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004436 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004437 if (VD->getQualifier()) {
4438 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004439 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004440 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004441 }
4442 return NewD;
4443}
4444
James Dennett634962f2012-06-14 21:40:34 +00004445/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004446/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004447void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004448 if (W.getUsed()) return; // only do this once
4449 W.setUsed(true);
4450 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4451 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004452 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00004453 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
4454 W.getLocation()));
4455 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00004456 WeakTopLevelDecl.push_back(NewD);
4457 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4458 // to insert Decl at TU scope, sorry.
4459 DeclContext *SavedContext = CurContext;
4460 CurContext = Context.getTranslationUnitDecl();
4461 PushOnScopeChains(NewD, S);
4462 CurContext = SavedContext;
4463 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00004464 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004465 }
4466}
4467
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004468void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4469 // It's valid to "forward-declare" #pragma weak, in which case we
4470 // have to do this.
4471 LoadExternalWeakUndeclaredIdentifiers();
4472 if (!WeakUndeclaredIdentifiers.empty()) {
4473 NamedDecl *ND = NULL;
4474 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4475 if (VD->isExternC())
4476 ND = VD;
4477 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4478 if (FD->isExternC())
4479 ND = FD;
4480 if (ND) {
4481 if (IdentifierInfo *Id = ND->getIdentifier()) {
4482 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4483 = WeakUndeclaredIdentifiers.find(Id);
4484 if (I != WeakUndeclaredIdentifiers.end()) {
4485 WeakInfo W = I->second;
4486 DeclApplyPragmaWeak(S, ND, W);
4487 WeakUndeclaredIdentifiers[Id] = W;
4488 }
4489 }
4490 }
4491 }
4492}
4493
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004494/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4495/// it, apply them to D. This is a bit tricky because PD can have attributes
4496/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004497void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004498 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004499 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004500 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004501
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004502 // Walk the declarator structure, applying decl attributes that were in a type
4503 // position to the decl itself. This handles cases like:
4504 // int *__attr__(x)** D;
4505 // when X is a decl attribute.
4506 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4507 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004508 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004509
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004510 // Finally, apply any attributes on the decl itself.
4511 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004512 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004513}
John McCall28a6aea2009-11-04 02:18:39 +00004514
John McCall31168b02011-06-15 23:02:42 +00004515/// Is the given declaration allowed to use a forbidden type?
4516static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4517 // Private ivars are always okay. Unfortunately, people don't
4518 // always properly make their ivars private, even in system headers.
4519 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004520 // Function declarations in sys headers will be marked unavailable.
4521 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4522 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004523 return false;
4524
4525 // Require it to be declared in a system header.
4526 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4527}
4528
4529/// Handle a delayed forbidden-type diagnostic.
4530static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4531 Decl *decl) {
4532 if (decl && isForbiddenTypeAllowed(S, decl)) {
Aaron Ballman36a53502014-01-16 13:03:14 +00004533 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context,
4534 "this system declaration uses an unsupported type",
4535 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00004536 return;
4537 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004538 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004539 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004540 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004541 // kind of forbidden type messages on unavailable functions.
4542 if (FD->hasAttr<UnavailableAttr>() &&
4543 diag.getForbiddenTypeDiagnostic() ==
4544 diag::err_arc_array_param_no_ownership) {
4545 diag.Triggered = true;
4546 return;
4547 }
4548 }
John McCall31168b02011-06-15 23:02:42 +00004549
4550 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4551 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4552 diag.Triggered = true;
4553}
4554
John McCall2ec85372012-05-07 06:16:41 +00004555void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4556 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004557 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004558 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004559
John McCall2ec85372012-05-07 06:16:41 +00004560 // When delaying diagnostics to run in the context of a parsed
4561 // declaration, we only want to actually emit anything if parsing
4562 // succeeds.
4563 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004564
John McCall2ec85372012-05-07 06:16:41 +00004565 // We emit all the active diagnostics in this pool or any of its
4566 // parents. In general, we'll get one pool for the decl spec
4567 // and a child pool for each declarator; in a decl group like:
4568 // deprecated_typedef foo, *bar, baz();
4569 // only the declarator pops will be passed decls. This is correct;
4570 // we really do need to consider delayed diagnostics from the decl spec
4571 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004572 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004573 do {
John McCall6347b682012-05-07 06:16:58 +00004574 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004575 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4576 // This const_cast is a bit lame. Really, Triggered should be mutable.
4577 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004578 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004579 continue;
4580
John McCallc1465822011-02-14 07:13:47 +00004581 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004582 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004583 case DelayedDiagnostic::Unavailable:
4584 // Don't bother giving deprecation/unavailable diagnostics if
4585 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004586 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004587 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004588 break;
4589
4590 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004591 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004592 break;
John McCall31168b02011-06-15 23:02:42 +00004593
4594 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004595 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004596 break;
John McCall86121512010-01-27 03:50:35 +00004597 }
4598 }
John McCall2ec85372012-05-07 06:16:41 +00004599 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004600}
4601
John McCall6347b682012-05-07 06:16:58 +00004602/// Given a set of delayed diagnostics, re-emit them as if they had
4603/// been delayed in the current context instead of in the given pool.
4604/// Essentially, this just moves them to the current pool.
4605void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4606 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4607 assert(curPool && "re-emitting in undelayed context not supported");
4608 curPool->steal(pool);
4609}
4610
John McCall28a6aea2009-11-04 02:18:39 +00004611static bool isDeclDeprecated(Decl *D) {
4612 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004613 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004614 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004615 // A category implicitly has the availability of the interface.
4616 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4617 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004618 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4619 return false;
4620}
4621
Ted Kremenekb79ee572013-12-18 23:30:06 +00004622static bool isDeclUnavailable(Decl *D) {
4623 do {
4624 if (D->isUnavailable())
4625 return true;
4626 // A category implicitly has the availability of the interface.
4627 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4628 return CatD->getClassInterface()->isUnavailable();
4629 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4630 return false;
4631}
4632
Eli Friedman971bfa12012-08-08 21:52:41 +00004633static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004634DoEmitAvailabilityWarning(Sema &S,
4635 DelayedDiagnostic::DDKind K,
4636 Decl *Ctx,
4637 const NamedDecl *D,
4638 StringRef Message,
4639 SourceLocation Loc,
4640 const ObjCInterfaceDecl *UnknownObjCClass,
4641 const ObjCPropertyDecl *ObjCProperty) {
4642
4643 // Diagnostics for deprecated or unavailable.
4644 unsigned diag, diag_message, diag_fwdclass_message;
4645
4646 // Matches 'diag::note_property_attribute' options.
4647 unsigned property_note_select;
4648
4649 // Matches diag::note_availability_specified_here.
4650 unsigned available_here_select_kind;
4651
4652 // Don't warn if our current context is deprecated or unavailable.
4653 switch (K) {
4654 case DelayedDiagnostic::Deprecation:
4655 if (isDeclDeprecated(Ctx))
4656 return;
4657 diag = diag::warn_deprecated;
4658 diag_message = diag::warn_deprecated_message;
4659 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4660 property_note_select = /* deprecated */ 0;
4661 available_here_select_kind = /* deprecated */ 2;
4662 break;
4663
4664 case DelayedDiagnostic::Unavailable:
4665 if (isDeclUnavailable(Ctx))
4666 return;
4667 diag = diag::err_unavailable;
4668 diag_message = diag::err_unavailable_message;
4669 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4670 property_note_select = /* unavailable */ 1;
4671 available_here_select_kind = /* unavailable */ 0;
4672 break;
4673
4674 default:
4675 llvm_unreachable("Neither a deprecation or unavailable kind");
4676 }
4677
Eli Friedman971bfa12012-08-08 21:52:41 +00004678 DeclarationName Name = D->getDeclName();
4679 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004680 S.Diag(Loc, diag_message) << Name << Message;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004681 if (ObjCProperty)
4682 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4683 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004684 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004685 S.Diag(Loc, diag) << Name;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004686 if (ObjCProperty)
4687 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4688 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004689 } else {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004690 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004691 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4692 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004693
4694 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4695 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004696}
4697
Ted Kremenekb79ee572013-12-18 23:30:06 +00004698void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4699 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004700 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004701 DoEmitAvailabilityWarning(*this,
4702 (DelayedDiagnostic::DDKind) DD.Kind,
4703 Ctx,
4704 DD.getDeprecationDecl(),
4705 DD.getDeprecationMessage(),
4706 DD.Loc,
4707 DD.getUnknownObjCClass(),
4708 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004709}
4710
Ted Kremenekb79ee572013-12-18 23:30:06 +00004711void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4712 NamedDecl *D, StringRef Message,
4713 SourceLocation Loc,
4714 const ObjCInterfaceDecl *UnknownObjCClass,
4715 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004716 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004717 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004718 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4719 UnknownObjCClass,
4720 ObjCProperty,
4721 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004722 return;
4723 }
4724
Ted Kremenekb79ee572013-12-18 23:30:06 +00004725 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4726 DelayedDiagnostic::DDKind K;
4727 switch (AD) {
4728 case AD_Deprecation:
4729 K = DelayedDiagnostic::Deprecation;
4730 break;
4731 case AD_Unavailable:
4732 K = DelayedDiagnostic::Unavailable;
4733 break;
4734 }
4735
4736 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
4737 UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004738}