blob: 5c6ddd2cb97dbc8e5150576b785c9179d2f92e74 [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"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
John McCall28a0cf72010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000018#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000019#include "clang/AST/DeclObjC.h"
20#include "clang/AST/Expr.h"
John McCall31168b02011-06-15 23:02:42 +000021#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000022#include "clang/Basic/TargetInfo.h"
John McCall8b0666c2010-08-20 18:27:03 +000023#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000024#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000025#include "clang/Sema/Lookup.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000026#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000027using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000028using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000029
John McCall5fca7ea2011-03-02 12:29:23 +000030/// These constants match the enumerated choices of
31/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000032enum AttributeDeclKind {
John McCall5fca7ea2011-03-02 12:29:23 +000033 ExpectedFunction,
34 ExpectedUnion,
35 ExpectedVariableOrFunction,
36 ExpectedFunctionOrMethod,
37 ExpectedParameter,
John McCall5fca7ea2011-03-02 12:29:23 +000038 ExpectedFunctionMethodOrBlock,
John McCall5fca7ea2011-03-02 12:29:23 +000039 ExpectedFunctionMethodOrParameter,
40 ExpectedClass,
John McCall5fca7ea2011-03-02 12:29:23 +000041 ExpectedVariable,
42 ExpectedMethod,
Caitlin Sadowski63fa6672011-07-28 20:12:35 +000043 ExpectedVariableFunctionOrLabel,
Douglas Gregor5c3cc422012-03-14 16:55:17 +000044 ExpectedFieldOrGlobalVar,
45 ExpectedStruct
John McCall5fca7ea2011-03-02 12:29:23 +000046};
47
Chris Lattner58418ff2008-06-29 00:16:31 +000048//===----------------------------------------------------------------------===//
49// Helper functions
50//===----------------------------------------------------------------------===//
51
Chandler Carruthff4c4f02011-07-01 23:49:12 +000052static const FunctionType *getFunctionType(const Decl *D,
Ted Kremenek527042b2009-08-14 20:49:40 +000053 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000054 QualType Ty;
Chandler Carruthff4c4f02011-07-01 23:49:12 +000055 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000056 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000057 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000058 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000059 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000060 Ty = decl->getUnderlyingType();
61 else
62 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000063
Chris Lattner2c6fcf52008-06-26 18:38:35 +000064 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000065 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000066 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000067 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000068
John McCall9dd450b2009-09-21 23:43:11 +000069 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000070}
71
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000072// FIXME: We should provide an abstraction around a method or function
73// to provide the following bits of information.
74
Nuno Lopes518e3702009-12-20 23:11:08 +000075/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000076/// type (function or function-typed variable).
Chandler Carruthff4c4f02011-07-01 23:49:12 +000077static bool isFunction(const Decl *D) {
78 return getFunctionType(D, false) != NULL;
Ted Kremenek527042b2009-08-14 20:49:40 +000079}
80
81/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000082/// type (function or function-typed variable) or an Objective-C
83/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000084static bool isFunctionOrMethod(const Decl *D) {
85 return isFunction(D)|| isa<ObjCMethodDecl>(D);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000086}
87
Fariborz Jahanian4447e172009-05-15 23:15:03 +000088/// isFunctionOrMethodOrBlock - Return true if the given decl has function
89/// type (function or function-typed variable) or an Objective-C
90/// method or a block.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000091static bool isFunctionOrMethodOrBlock(const Decl *D) {
92 if (isFunctionOrMethod(D))
Fariborz Jahanian4447e172009-05-15 23:15:03 +000093 return true;
94 // check for block is more involved.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000095 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000096 QualType Ty = V->getType();
97 return Ty->isBlockPointerType();
98 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +000099 return isa<BlockDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000100}
101
John McCall3882ace2011-01-05 12:14:39 +0000102/// Return true if the given decl has a declarator that should have
103/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000104static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +0000105 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000106 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
107 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +0000108}
109
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000110/// hasFunctionProto - Return true if the given decl has a argument
111/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000112/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000113static bool hasFunctionProto(const Decl *D) {
114 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000115 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000116 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000117 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000118 return true;
119 }
120}
121
122/// getFunctionOrMethodNumArgs - Return number of function or method
123/// arguments. It is an error to call this on a K&R function (use
124/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000125static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
126 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000127 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000128 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000129 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000130 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000131}
132
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000133static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
134 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000135 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000136 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000137 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000138
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000139 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000140}
141
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000142static QualType getFunctionOrMethodResultType(const Decl *D) {
143 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000144 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000145 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000146}
147
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000148static bool isFunctionOrMethodVariadic(const Decl *D) {
149 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000150 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000151 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000152 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000153 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000154 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000155 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000156 }
157}
158
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000159static bool isInstanceMethod(const Decl *D) {
160 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000161 return MethodDecl->isInstance();
162 return false;
163}
164
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000165static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000166 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000167 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000168 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000169
John McCall96fa4842010-05-17 21:00:27 +0000170 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
171 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000172 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000173
John McCall96fa4842010-05-17 21:00:27 +0000174 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000175
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000176 // FIXME: Should we walk the chain of classes?
177 return ClsName == &Ctx.Idents.get("NSString") ||
178 ClsName == &Ctx.Idents.get("NSMutableString");
179}
180
Daniel Dunbar980c6692008-09-26 03:32:58 +0000181static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000182 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000183 if (!PT)
184 return false;
185
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000186 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000187 if (!RT)
188 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000189
Daniel Dunbar980c6692008-09-26 03:32:58 +0000190 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000191 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000192 return false;
193
194 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
195}
196
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000197/// \brief Check if the attribute has exactly as many args as Num. May
198/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000199static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
200 unsigned int Num) {
201 if (Attr.getNumArgs() != Num) {
202 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
203 return false;
204 }
205
206 return true;
207}
208
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000209
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000210/// \brief Check if the attribute has at least as many args as Num. May
211/// output an error.
212static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
213 unsigned int Num) {
214 if (Attr.getNumArgs() < Num) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000215 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
216 return false;
217 }
218
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000219 return true;
220}
221
222///
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000223/// \brief Check if passed in Decl is a field or potentially shared global var
224/// \return true if the Decl is a field or potentially shared global variable
225///
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000226static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000227 if (isa<FieldDecl>(D))
228 return true;
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000229 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000230 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
231
232 return false;
233}
234
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000235/// \brief Check if the passed-in expression is of type int or bool.
236static bool isIntOrBool(Expr *Exp) {
237 QualType QT = Exp->getType();
238 return QT->isBooleanType() || QT->isIntegerType();
239}
240
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000241///
242/// \brief Check if passed in Decl is a pointer type.
243/// Note that this function may produce an error message.
244/// \return true if the Decl is a pointer type; false otherwise
245///
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000246static bool checkIsPointer(Sema &S, const Decl *D, const AttributeList &Attr) {
247 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000248 QualType QT = vd->getType();
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000249 if (QT->isAnyPointerType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000250 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000251 S.Diag(Attr.getLoc(), diag::warn_pointer_attribute_wrong_type)
252 << Attr.getName()->getName() << QT;
253 } else {
254 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
255 << Attr.getName();
256 }
257 return false;
258}
259
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000260/// \brief Checks that the passed in QualType either is of RecordType or points
261/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000262static const RecordType *getRecordType(QualType QT) {
263 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000264 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000265
266 // Now check if we point to record type.
267 if (const PointerType *PT = QT->getAs<PointerType>())
268 return PT->getPointeeType()->getAs<RecordType>();
269
270 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000271}
272
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000273/// \brief Thread Safety Analysis: Checks that the passed in RecordType
274/// resolves to a lockable object. May flag an error.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000275static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
276 QualType Ty) {
277 const RecordType *RT = getRecordType(Ty);
278
279 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000280 if (!RT) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000281 S.Diag(Attr.getLoc(), diag::warn_attribute_argument_not_class)
282 << Attr.getName() << Ty.getAsString();
283 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000284 }
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000285 // Don't check for lockable if the class hasn't been defined yet.
286 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000287 return;
288 // Warn if the type is not lockable.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000289 if (!RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000290 S.Diag(Attr.getLoc(), diag::warn_attribute_argument_not_lockable)
291 << Attr.getName() << Ty.getAsString();
292 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000293 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000294}
295
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000296/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
297/// from Sidx, resolve to a lockable object. May flag an error.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000298/// \param Sidx The attribute argument index to start checking with.
299/// \param ParamIdxOk Whether an argument can be indexing into a function
300/// parameter list.
301static bool checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
302 const AttributeList &Attr,
303 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000304 int Sidx = 0,
305 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000306 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000307 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000308
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000309 if (ArgExp->isTypeDependent()) {
310 // FIXME -- need to processs this again on template instantiation
311 Args.push_back(ArgExp);
312 continue;
313 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000314
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000315 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000316
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000317 // First see if we can just cast to record type, or point to record type.
318 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000319
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000320 // Now check if we index into a record type function param.
321 if(!RT && ParamIdxOk) {
322 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000323 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
324 if(FD && IL) {
325 unsigned int NumParams = FD->getNumParams();
326 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000327 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
328 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
329 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000330 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
331 << Attr.getName() << Idx + 1 << NumParams;
332 return false;
333 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000334 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000335 }
336 }
337
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000338 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000339
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000340 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000341 }
342 return true;
343}
344
Chris Lattner58418ff2008-06-29 00:16:31 +0000345//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000346// Attribute Implementations
347//===----------------------------------------------------------------------===//
348
Daniel Dunbar032db472008-07-31 22:40:48 +0000349// FIXME: All this manual attribute parsing code is gross. At the
350// least add some helper functions to check most argument patterns (#
351// and types of args).
352
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000353static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr,
354 bool pointer = false) {
355 assert(!Attr.isInvalid());
356
357 if (!checkAttributeNumArgs(S, Attr, 0))
358 return;
359
360 // D must be either a member field or global (potentially shared) variable.
361 if (!mayBeSharedVariable(D)) {
362 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000363 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000364 return;
365 }
366
367 if (pointer && !checkIsPointer(S, D, Attr))
368 return;
369
370 if (pointer)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000371 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000372 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000373 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000374}
375
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000376static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000377 bool pointer = false) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000378 assert(!Attr.isInvalid());
379
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000380 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000381 return;
382
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000383 Expr *Arg = Attr.getArg(0);
384
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000385 // D must be either a member field or global (potentially shared) variable.
386 if (!mayBeSharedVariable(D)) {
387 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000388 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000389 return;
390 }
391
392 if (pointer && !checkIsPointer(S, D, Attr))
393 return;
394
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000395 if (!Arg->isTypeDependent()) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000396 checkForLockableRecord(S, D, Attr, Arg->getType());
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000397 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000398
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000399 if (pointer)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000400 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000401 S.Context, Arg));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000402 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000403 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000404}
405
406
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000407static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr,
408 bool scoped = false) {
409 assert(!Attr.isInvalid());
410
411 if (!checkAttributeNumArgs(S, Attr, 0))
412 return;
413
Caitlin Sadowski086fb952011-09-16 00:35:54 +0000414 // FIXME: Lockable structs for C code.
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000415 if (!isa<CXXRecordDecl>(D)) {
416 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
417 << Attr.getName() << ExpectedClass;
418 return;
419 }
420
421 if (scoped)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000422 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000423 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000424 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000425}
426
427static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
428 const AttributeList &Attr) {
429 assert(!Attr.isInvalid());
430
431 if (!checkAttributeNumArgs(S, Attr, 0))
432 return;
433
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000434 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000435 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
436 << Attr.getName() << ExpectedFunctionOrMethod;
437 return;
438 }
439
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000440 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000441 S.Context));
442}
443
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000444static void handleNoAddressSafetyAttr(Sema &S, Decl *D,
445 const AttributeList &Attr) {
446 assert(!Attr.isInvalid());
447
448 if (!checkAttributeNumArgs(S, Attr, 0))
449 return;
450
451 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
452 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
453 << Attr.getName() << ExpectedFunctionOrMethod;
454 return;
455 }
456
457 D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(),
458 S.Context));
459}
460
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000461static void handleAcquireOrderAttr(Sema &S, Decl *D, const AttributeList &Attr,
462 bool before) {
463 assert(!Attr.isInvalid());
464
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000465 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000466 return;
467
468 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000469 ValueDecl *VD = dyn_cast<ValueDecl>(D);
470 if (!VD || !mayBeSharedVariable(D)) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000471 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000472 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000473 return;
474 }
475
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000476 // Check that this attribute only applies to lockable types
477 QualType QT = VD->getType();
478 if (!QT->isDependentType()) {
479 const RecordType *RT = getRecordType(QT);
480 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000481 S.Diag(Attr.getLoc(), diag::warn_attribute_decl_not_lockable)
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000482 << Attr.getName();
483 return;
484 }
485 }
486
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000487 SmallVector<Expr*, 1> Args;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000488 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000489 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args))
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000490 return;
491
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000492 unsigned Size = Args.size();
493 assert(Size == Attr.getNumArgs());
494 Expr **StartArg = Size == 0 ? 0 : &Args[0];
495
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000496 if (before)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000497 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000498 StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000499 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000500 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000501 StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000502}
503
504static void handleLockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000505 bool exclusive = false) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000506 assert(!Attr.isInvalid());
507
508 // zero or more arguments ok
509
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000510 // check that the attribute is applied to a function
511 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000512 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
513 << Attr.getName() << ExpectedFunctionOrMethod;
514 return;
515 }
516
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000517 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000518 SmallVector<Expr*, 1> Args;
519 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true))
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000520 return;
521
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000522 unsigned Size = Args.size();
523 assert(Size == Attr.getNumArgs());
524 Expr **StartArg = Size == 0 ? 0 : &Args[0];
525
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000526 if (exclusive)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000527 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(),
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000528 S.Context, StartArg,
529 Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000530 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000531 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(),
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000532 S.Context, StartArg,
533 Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000534}
535
536static void handleTrylockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000537 bool exclusive = false) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000538 assert(!Attr.isInvalid());
539
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000540 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000541 return;
542
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000543
544 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000545 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
546 << Attr.getName() << ExpectedFunctionOrMethod;
547 return;
548 }
549
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000550 if (!isIntOrBool(Attr.getArg(0))) {
551 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
552 << Attr.getName();
553 return;
554 }
555
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000556 SmallVector<Expr*, 2> Args;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000557 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000558 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1))
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000559 return;
560
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000561 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000562 Expr **StartArg = Size == 0 ? 0 : &Args[0];
563
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000564 if (exclusive)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000565 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(),
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000566 S.Context,
Caitlin Sadowskibf06c722011-09-15 17:50:19 +0000567 Attr.getArg(0),
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000568 StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000569 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000570 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(),
Caitlin Sadowskibf06c722011-09-15 17:50:19 +0000571 S.Context,
572 Attr.getArg(0),
573 StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000574}
575
576static void handleLocksRequiredAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000577 bool exclusive = false) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000578 assert(!Attr.isInvalid());
579
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000580 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000581 return;
582
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000583 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000584 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
585 << Attr.getName() << ExpectedFunctionOrMethod;
586 return;
587 }
588
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000589 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000590 SmallVector<Expr*, 1> Args;
591 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args))
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000592 return;
593
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000594 unsigned Size = Args.size();
595 assert(Size == Attr.getNumArgs());
596 Expr **StartArg = Size == 0 ? 0 : &Args[0];
597
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000598 if (exclusive)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000599 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(),
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000600 S.Context, StartArg,
601 Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000602 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000603 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(),
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000604 S.Context, StartArg,
605 Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000606}
607
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000608static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000609 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000610 assert(!Attr.isInvalid());
611
612 // zero or more arguments ok
613
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000614 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000615 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
616 << Attr.getName() << ExpectedFunctionOrMethod;
617 return;
618 }
619
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000620 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000621 SmallVector<Expr*, 1> Args;
622 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true))
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000623 return;
624
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000625 unsigned Size = Args.size();
626 assert(Size == Attr.getNumArgs());
627 Expr **StartArg = Size == 0 ? 0 : &Args[0];
628
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000629 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000630 StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000631}
632
633static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000634 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000635 assert(!Attr.isInvalid());
636
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000637 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000638 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000639 Expr *Arg = Attr.getArg(0);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000640
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000641 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000642 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
643 << Attr.getName() << ExpectedFunctionOrMethod;
644 return;
645 }
646
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000647 if (Arg->isTypeDependent())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000648 return;
649
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000650 // check that the argument is lockable object
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000651 checkForLockableRecord(S, D, Attr, Arg->getType());
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000652
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000653 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context, Arg));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000654}
655
656static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000657 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000658 assert(!Attr.isInvalid());
659
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000660 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000661 return;
662
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000663 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000664 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
665 << Attr.getName() << ExpectedFunctionOrMethod;
666 return;
667 }
668
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000669 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000670 SmallVector<Expr*, 1> Args;
671 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args))
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000672 return;
673
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000674 unsigned Size = Args.size();
675 assert(Size == Attr.getNumArgs());
676 Expr **StartArg = Size == 0 ? 0 : &Args[0];
677
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000678 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000679 StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000680}
681
682
Chandler Carruthedc2c642011-07-02 00:01:44 +0000683static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
684 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000685 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000686 if (tDecl == 0) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000687 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000688 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000689 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000690
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000691 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor758a8692009-06-17 21:51:59 +0000692
693 Expr *sizeExpr;
694
695 // Special case where the argument is a template id.
696 if (Attr.getParameterName()) {
John McCalle66edc12009-11-24 19:00:30 +0000697 CXXScopeSpec SS;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000698 SourceLocation TemplateKWLoc;
John McCalle66edc12009-11-24 19:00:30 +0000699 UnqualifiedId id;
700 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Douglas Gregor39c02722011-06-15 16:02:29 +0000701
Abramo Bagnara7945c982012-01-27 09:46:47 +0000702 ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id,
703 false, false);
Douglas Gregor39c02722011-06-15 16:02:29 +0000704 if (Size.isInvalid())
705 return;
706
707 sizeExpr = Size.get();
Douglas Gregor758a8692009-06-17 21:51:59 +0000708 } else {
709 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000710 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor758a8692009-06-17 21:51:59 +0000711 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000712
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000713 sizeExpr = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000714 }
Douglas Gregor758a8692009-06-17 21:51:59 +0000715
716 // Instantiate/Install the vector type, and let Sema build the type for us.
717 // This will run the reguired checks.
John McCallb268a282010-08-23 23:25:46 +0000718 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor758a8692009-06-17 21:51:59 +0000719 if (!T.isNull()) {
John McCall703a3f82009-10-24 08:00:42 +0000720 // FIXME: preserve the old source info.
John McCallbcd03502009-12-07 02:54:59 +0000721 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpd3bb5572009-07-24 19:02:52 +0000722
Douglas Gregor758a8692009-06-17 21:51:59 +0000723 // Remember this typedef decl, we will need it later for diagnostics.
724 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000725 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000726}
727
Chandler Carruthedc2c642011-07-02 00:01:44 +0000728static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000729 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000730 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000731 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000732
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000733 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000734 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000735 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000736 // If the alignment is less than or equal to 8 bits, the packed attribute
737 // has no effect.
738 if (!FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000739 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000740 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000741 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000742 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000743 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000744 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000745 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000746}
747
Chandler Carruthedc2c642011-07-02 00:01:44 +0000748static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000749 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000750 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +0000751 else
752 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
753}
754
Chandler Carruthedc2c642011-07-02 00:01:44 +0000755static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000756 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000757 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000758 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000759
Ted Kremenek1f672822010-02-18 03:08:58 +0000760 // The IBAction attributes only apply to instance methods.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000761 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek1f672822010-02-18 03:08:58 +0000762 if (MD->isInstanceMethod()) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000763 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
Ted Kremenek1f672822010-02-18 03:08:58 +0000764 return;
765 }
766
Ted Kremenekd68ec812011-02-04 06:54:16 +0000767 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek1f672822010-02-18 03:08:58 +0000768}
769
Ted Kremenek7fd17232011-09-29 07:02:25 +0000770static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
771 // The IBOutlet/IBOutletCollection attributes only apply to instance
772 // variables or properties of Objective-C classes. The outlet must also
773 // have an object reference type.
774 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
775 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +0000776 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +0000777 << Attr.getName() << VD->getType() << 0;
778 return false;
779 }
780 }
781 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
782 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +0000783 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +0000784 << Attr.getName() << PD->getType() << 1;
785 return false;
786 }
787 }
788 else {
789 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
790 return false;
791 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +0000792
Ted Kremenek7fd17232011-09-29 07:02:25 +0000793 return true;
794}
795
Chandler Carruthedc2c642011-07-02 00:01:44 +0000796static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek1f672822010-02-18 03:08:58 +0000797 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000798 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek1f672822010-02-18 03:08:58 +0000799 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +0000800
801 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +0000802 return;
Ted Kremenek1f672822010-02-18 03:08:58 +0000803
Ted Kremenek7fd17232011-09-29 07:02:25 +0000804 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000805}
806
Chandler Carruthedc2c642011-07-02 00:01:44 +0000807static void handleIBOutletCollection(Sema &S, Decl *D,
808 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +0000809
810 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000811 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek26bde772010-05-19 17:38:06 +0000812 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
813 return;
814 }
815
Ted Kremenek7fd17232011-09-29 07:02:25 +0000816 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +0000817 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +0000818
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000819 IdentifierInfo *II = Attr.getParameterName();
820 if (!II)
Fariborz Jahanian2f31b332011-10-18 19:54:31 +0000821 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000822
John McCallba7bf592010-08-24 05:47:05 +0000823 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000824 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000825 if (!TypeRep) {
826 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
827 return;
828 }
John McCallba7bf592010-08-24 05:47:05 +0000829 QualType QT = TypeRep.get();
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000830 // Diagnose use of non-object type in iboutletcollection attribute.
831 // FIXME. Gnu attribute extension ignores use of builtin types in
832 // attributes. So, __attribute__((iboutletcollection(char))) will be
833 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +0000834 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000835 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
836 return;
837 }
Argyrios Kyrtzidis8db67df2011-09-13 18:41:59 +0000838 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
839 QT, Attr.getParameterLoc()));
Ted Kremenek26bde772010-05-19 17:38:06 +0000840}
841
Chandler Carruth3ed22c32011-07-01 23:49:16 +0000842static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +0000843 if (const RecordType *UT = T->getAsUnionType())
844 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
845 RecordDecl *UD = UT->getDecl();
846 for (RecordDecl::field_iterator it = UD->field_begin(),
847 itend = UD->field_end(); it != itend; ++it) {
848 QualType QT = it->getType();
849 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
850 T = QT;
851 return;
852 }
853 }
854 }
855}
856
Chandler Carruthedc2c642011-07-02 00:01:44 +0000857static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000858 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
859 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000860 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000861 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000862 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000863 return;
864 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000865
Chandler Carruth743682b2010-11-16 08:35:43 +0000866 // In C++ the implicit 'this' function parameter also counts, and they are
867 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000868 bool HasImplicitThisParam = isInstanceMethod(D);
869 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000870
871 // The nonnull attribute only applies to pointers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000872 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000873
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000874 for (AttributeList::arg_iterator I=Attr.arg_begin(),
875 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000876
877
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000878 // The argument must be an integer constant expression.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000879 Expr *Ex = *I;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000880 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000881 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
882 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000883 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
884 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000885 return;
886 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000887
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000888 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000889
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000890 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +0000891 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +0000892 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000893 return;
894 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000895
Ted Kremenek5224e6a2008-07-21 22:09:15 +0000896 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +0000897 if (HasImplicitThisParam) {
898 if (x == 0) {
899 S.Diag(Attr.getLoc(),
900 diag::err_attribute_invalid_implicit_this_argument)
901 << "nonnull" << Ex->getSourceRange();
902 return;
903 }
904 --x;
905 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000906
907 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000908 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +0000909 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +0000910
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000911 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000912 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +0000913 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +0000914 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000915 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000916 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000917
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000918 NonNullArgs.push_back(x);
919 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000920
921 // If no arguments were specified to __attribute__((nonnull)) then all pointer
922 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000923 if (NonNullArgs.empty()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000924 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
925 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +0000926 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000927 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000928 NonNullArgs.push_back(I);
Ted Kremenek5fa50522008-11-18 06:52:58 +0000929 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000930
Ted Kremenek22813f42010-10-21 18:49:36 +0000931 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +0000932 if (NonNullArgs.empty()) {
933 // Warn the trivial case only if attribute is not coming from a
934 // macro instantiation.
935 if (Attr.getLoc().isFileID())
936 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000937 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +0000938 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000939 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000940
941 unsigned* start = &NonNullArgs[0];
942 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +0000943 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000944 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000945 size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000946}
947
Chandler Carruthedc2c642011-07-02 00:01:44 +0000948static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000949 // This attribute must be applied to a function declaration.
950 // The first argument to the attribute must be a string,
951 // the name of the resource, for example "malloc".
952 // The following arguments must be argument indexes, the arguments must be
953 // of integer type for Returns, otherwise of pointer type.
954 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000955 // after being held. free() should be __attribute((ownership_takes)), whereas
956 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +0000957
958 if (!AL.getParameterName()) {
959 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
960 << AL.getName()->getName() << 1;
961 return;
962 }
963 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000964 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000965 switch (AL.getKind()) {
966 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000967 K = OwnershipAttr::Takes;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000968 if (AL.getNumArgs() < 1) {
969 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
970 return;
971 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000972 break;
973 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000974 K = OwnershipAttr::Holds;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000975 if (AL.getNumArgs() < 1) {
976 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
977 return;
978 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000979 break;
980 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000981 K = OwnershipAttr::Returns;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000982 if (AL.getNumArgs() > 1) {
983 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
984 << AL.getNumArgs() + 1;
985 return;
986 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000987 break;
988 default:
989 // This should never happen given how we are called.
990 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +0000991 }
992
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000993 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +0000994 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
995 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000996 return;
997 }
998
Chandler Carruth743682b2010-11-16 08:35:43 +0000999 // In C++ the implicit 'this' function parameter also counts, and they are
1000 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001001 bool HasImplicitThisParam = isInstanceMethod(D);
1002 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001003
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001004 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001005
1006 // Normalize the argument, __foo__ becomes foo.
1007 if (Module.startswith("__") && Module.endswith("__"))
1008 Module = Module.substr(2, Module.size() - 4);
1009
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001010 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001011
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001012 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1013 ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001014
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001015 Expr *IdxExpr = *I;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001016 llvm::APSInt ArgNum(32);
1017 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1018 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1019 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1020 << AL.getName()->getName() << IdxExpr->getSourceRange();
1021 continue;
1022 }
1023
1024 unsigned x = (unsigned) ArgNum.getZExtValue();
1025
1026 if (x > NumArgs || x < 1) {
1027 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1028 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1029 continue;
1030 }
1031 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +00001032 if (HasImplicitThisParam) {
1033 if (x == 0) {
1034 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1035 << "ownership" << IdxExpr->getSourceRange();
1036 return;
1037 }
1038 --x;
1039 }
1040
Ted Kremenekd21139a2010-07-31 01:52:11 +00001041 switch (K) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001042 case OwnershipAttr::Takes:
1043 case OwnershipAttr::Holds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001044 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001045 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001046 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1047 // FIXME: Should also highlight argument in decl.
1048 S.Diag(AL.getLoc(), diag::err_ownership_type)
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001049 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekd21139a2010-07-31 01:52:11 +00001050 << "pointer"
1051 << IdxExpr->getSourceRange();
1052 continue;
1053 }
1054 break;
1055 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001056 case OwnershipAttr::Returns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001057 if (AL.getNumArgs() > 1) {
1058 // Is the function argument an integer type?
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001059 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001060 llvm::APSInt ArgNum(32);
1061 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1062 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1063 S.Diag(AL.getLoc(), diag::err_ownership_type)
1064 << "ownership_returns" << "integer"
1065 << IdxExpr->getSourceRange();
1066 return;
1067 }
1068 }
1069 break;
1070 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001071 } // switch
1072
1073 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001074 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001075 i = D->specific_attr_begin<OwnershipAttr>(),
1076 e = D->specific_attr_end<OwnershipAttr>();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001077 i != e; ++i) {
1078 if ((*i)->getOwnKind() != K) {
1079 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1080 I!=E; ++I) {
1081 if (x == *I) {
1082 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1083 << AL.getName()->getName() << "ownership_*";
Ted Kremenekd21139a2010-07-31 01:52:11 +00001084 }
1085 }
1086 }
1087 }
1088 OwnershipArgs.push_back(x);
1089 }
1090
1091 unsigned* start = OwnershipArgs.data();
1092 unsigned size = OwnershipArgs.size();
1093 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001094
1095 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1096 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1097 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001098 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001099
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001100 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001101 start, size));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001102}
1103
John McCall7a198ce2011-02-08 22:35:49 +00001104/// Whether this declaration has internal linkage for the purposes of
1105/// things that want to complain about things not have internal linkage.
1106static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1107 switch (D->getLinkage()) {
1108 case NoLinkage:
1109 case InternalLinkage:
1110 return true;
1111
1112 // Template instantiations that go from external to unique-external
1113 // shouldn't get diagnosed.
1114 case UniqueExternalLinkage:
1115 return true;
1116
1117 case ExternalLinkage:
1118 return false;
1119 }
1120 llvm_unreachable("unknown linkage kind!");
Rafael Espindolac18086a2010-02-23 22:00:30 +00001121}
1122
Chandler Carruthedc2c642011-07-02 00:01:44 +00001123static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001124 // Check the attribute arguments.
1125 if (Attr.getNumArgs() > 1) {
1126 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1127 return;
1128 }
1129
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001130 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall7a198ce2011-02-08 22:35:49 +00001131 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001132 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +00001133 return;
1134 }
1135
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001136 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001137
Rafael Espindolac18086a2010-02-23 22:00:30 +00001138 // gcc rejects
1139 // class c {
1140 // static int a __attribute__((weakref ("v2")));
1141 // static int b() __attribute__((weakref ("f3")));
1142 // };
1143 // and ignores the attributes of
1144 // void f(void) {
1145 // static int a __attribute__((weakref ("v2")));
1146 // }
1147 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001148 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001149 if (!Ctx->isFileContext()) {
1150 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001151 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001152 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001153 }
1154
1155 // The GCC manual says
1156 //
1157 // At present, a declaration to which `weakref' is attached can only
1158 // be `static'.
1159 //
1160 // It also says
1161 //
1162 // Without a TARGET,
1163 // given as an argument to `weakref' or to `alias', `weakref' is
1164 // equivalent to `weak'.
1165 //
1166 // gcc 4.4.1 will accept
1167 // int a7 __attribute__((weakref));
1168 // as
1169 // int a7 __attribute__((weak));
1170 // This looks like a bug in gcc. We reject that for now. We should revisit
1171 // it if this behaviour is actually used.
1172
John McCall7a198ce2011-02-08 22:35:49 +00001173 if (!hasEffectivelyInternalLinkage(nd)) {
1174 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindolac18086a2010-02-23 22:00:30 +00001175 return;
1176 }
1177
1178 // GCC rejects
1179 // static ((alias ("y"), weakref)).
1180 // Should we? How to check that weakref is before or after alias?
1181
1182 if (Attr.getNumArgs() == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001183 Expr *Arg = Attr.getArg(0);
Rafael Espindolac18086a2010-02-23 22:00:30 +00001184 Arg = Arg->IgnoreParenCasts();
1185 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1186
Douglas Gregorfb65e592011-07-27 05:40:30 +00001187 if (!Str || !Str->isAscii()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001188 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1189 << "weakref" << 1;
1190 return;
1191 }
1192 // GCC will accept anything as the argument of weakref. Should we
1193 // check for an existing decl?
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001194 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001195 Str->getString()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001196 }
1197
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001198 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001199}
1200
Chandler Carruthedc2c642011-07-02 00:01:44 +00001201static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001202 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001203 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001204 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001205 return;
1206 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001207
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001208 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001209 Arg = Arg->IgnoreParenCasts();
1210 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001211
Douglas Gregorfb65e592011-07-27 05:40:30 +00001212 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001213 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001214 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001215 return;
1216 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001217
Douglas Gregore8bbc122011-09-02 00:18:52 +00001218 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001219 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1220 return;
1221 }
1222
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001223 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001224
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001225 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001226 Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001227}
1228
Chandler Carruthedc2c642011-07-02 00:01:44 +00001229static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001230 // Check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001231 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar03a38442008-10-28 00:17:57 +00001232 return;
Anders Carlsson88097122009-02-19 19:16:48 +00001233
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001234 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001235 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001236 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001237 return;
1238 }
1239
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001240 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001241}
1242
Chandler Carruthedc2c642011-07-02 00:01:44 +00001243static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1244 const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001245 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001246 if (Attr.hasParameterOrArguments()) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001247 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1248 return;
1249 }
1250
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001251 if (!isa<FunctionDecl>(D)) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001252 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001253 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00001254 return;
1255 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001256
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001257 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbar03a38442008-10-28 00:17:57 +00001258}
1259
Chandler Carruthedc2c642011-07-02 00:01:44 +00001260static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001261 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001262 if (Attr.hasParameterOrArguments()) {
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001263 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1264 return;
1265 }
Mike Stump11289f42009-09-09 15:08:12 +00001266
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001267 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001268 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001269 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001270 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001271 return;
1272 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001273 }
1274
Ted Kremenek08479ae2009-08-15 00:51:46 +00001275 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001276}
1277
Chandler Carruthedc2c642011-07-02 00:01:44 +00001278static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001279 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001280 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001281 return;
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001282
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001283 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001284}
1285
Chandler Carruthedc2c642011-07-02 00:01:44 +00001286static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001287 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001288 if (isa<VarDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001289 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher515d87f2010-12-03 06:58:14 +00001290 else
1291 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001292 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001293}
1294
Chandler Carruthedc2c642011-07-02 00:01:44 +00001295static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001296 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001297 if (isa<VarDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001298 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher515d87f2010-12-03 06:58:14 +00001299 else
1300 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001301 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001302}
1303
Chandler Carruthedc2c642011-07-02 00:01:44 +00001304static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001305 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001306
1307 if (S.CheckNoReturnAttr(attr)) return;
1308
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001309 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001310 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001311 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001312 return;
1313 }
1314
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001315 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall3882ace2011-01-05 12:14:39 +00001316}
1317
1318bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek1551d552011-04-15 05:49:29 +00001319 if (attr.hasParameterOrArguments()) {
John McCall3882ace2011-01-05 12:14:39 +00001320 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1321 attr.setInvalid();
1322 return true;
1323 }
1324
1325 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001326}
1327
Chandler Carruthedc2c642011-07-02 00:01:44 +00001328static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1329 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001330
1331 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1332 // because 'analyzer_noreturn' does not impact the type.
1333
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001334 if(!checkAttributeNumArgs(S, Attr, 0))
1335 return;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001336
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001337 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1338 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001339 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1340 && !VD->getType()->isFunctionPointerType())) {
1341 S.Diag(Attr.getLoc(),
1342 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1343 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001344 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001345 return;
1346 }
1347 }
1348
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001349 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001350}
1351
John Thompsoncdb847ba2010-08-09 21:53:52 +00001352// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001353static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001354/*
1355 Returning a Vector Class in Registers
1356
Eric Christopherbc638a82010-12-01 22:13:54 +00001357 According to the PPU ABI specifications, a class with a single member of
1358 vector type is returned in memory when used as the return value of a function.
1359 This results in inefficient code when implementing vector classes. To return
1360 the value in a single vector register, add the vecreturn attribute to the
1361 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001362
1363 Example:
1364
1365 struct Vector
1366 {
1367 __vector float xyzw;
1368 } __attribute__((vecreturn));
1369
1370 Vector Add(Vector lhs, Vector rhs)
1371 {
1372 Vector result;
1373 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1374 return result; // This will be returned in a register
1375 }
1376*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001377 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001378 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001379 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001380 return;
1381 }
1382
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001383 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001384 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1385 return;
1386 }
1387
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001388 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001389 int count = 0;
1390
1391 if (!isa<CXXRecordDecl>(record)) {
1392 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1393 return;
1394 }
1395
1396 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1397 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1398 return;
1399 }
1400
Eric Christopherbc638a82010-12-01 22:13:54 +00001401 for (RecordDecl::field_iterator iter = record->field_begin();
1402 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001403 if ((count == 1) || !iter->getType()->isVectorType()) {
1404 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1405 return;
1406 }
1407 count++;
1408 }
1409
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001410 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001411}
1412
Chandler Carruthedc2c642011-07-02 00:01:44 +00001413static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001414 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001415 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001416 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001417 return;
1418 }
1419 // FIXME: Actually store the attribute on the declaration
1420}
1421
Chandler Carruthedc2c642011-07-02 00:01:44 +00001422static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek39c59a82008-07-25 04:39:19 +00001423 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001424 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001425 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001426 return;
1427 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001428
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001429 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
1430 !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001431 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001432 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001433 return;
1434 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001435
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001436 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001437}
1438
Rafael Espindola70107f92011-10-03 14:59:42 +00001439static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1440 const AttributeList &Attr) {
1441 // check the attribute arguments.
1442 if (Attr.hasParameterOrArguments()) {
1443 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1444 return;
1445 }
1446
1447 if (!isa<FunctionDecl>(D)) {
1448 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1449 << Attr.getName() << ExpectedFunction;
1450 return;
1451 }
1452
1453 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1454}
1455
Chandler Carruthedc2c642011-07-02 00:01:44 +00001456static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001457 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001458 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001459 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1460 return;
1461 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001462
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001463 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +00001464 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001465 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1466 return;
1467 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001468 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001469 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001470 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001471 return;
1472 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001473
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001474 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001475}
1476
Chandler Carruthedc2c642011-07-02 00:01:44 +00001477static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001478 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001479 if (Attr.getNumArgs() > 1) {
1480 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001481 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001482 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001483
1484 int priority = 65535; // FIXME: Do not hardcode such constants.
1485 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001486 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001487 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001488 if (E->isTypeDependent() || E->isValueDependent() ||
1489 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001490 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001491 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001492 return;
1493 }
1494 priority = Idx.getZExtValue();
1495 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001496
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001497 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001498 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001499 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001500 return;
1501 }
1502
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001503 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001504 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001505}
1506
Chandler Carruthedc2c642011-07-02 00:01:44 +00001507static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001508 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001509 if (Attr.getNumArgs() > 1) {
1510 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001511 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001512 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001513
1514 int priority = 65535; // FIXME: Do not hardcode such constants.
1515 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001516 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001517 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001518 if (E->isTypeDependent() || E->isValueDependent() ||
1519 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001520 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001521 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001522 return;
1523 }
1524 priority = Idx.getZExtValue();
1525 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001526
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001527 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001528 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001529 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001530 return;
1531 }
1532
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001533 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001534 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001535}
1536
Chandler Carruthedc2c642011-07-02 00:01:44 +00001537static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001538 unsigned NumArgs = Attr.getNumArgs();
1539 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001540 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001541 return;
1542 }
Chris Lattner190aa102011-02-24 05:42:24 +00001543
Fariborz Jahanian551063102010-10-06 21:18:44 +00001544 // Handle the case where deprecated attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001545 StringRef Str;
Chris Lattner190aa102011-02-24 05:42:24 +00001546 if (NumArgs == 1) {
1547 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanian551063102010-10-06 21:18:44 +00001548 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001549 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1550 << "deprecated";
Fariborz Jahanian551063102010-10-06 21:18:44 +00001551 return;
1552 }
Chris Lattner190aa102011-02-24 05:42:24 +00001553 Str = SE->getString();
Fariborz Jahanian551063102010-10-06 21:18:44 +00001554 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001555
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001556 D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getRange(), S.Context, Str));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001557}
1558
Chandler Carruthedc2c642011-07-02 00:01:44 +00001559static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001560 unsigned NumArgs = Attr.getNumArgs();
1561 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001562 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001563 return;
1564 }
Chris Lattner190aa102011-02-24 05:42:24 +00001565
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001566 // Handle the case where unavailable attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001567 StringRef Str;
Chris Lattner190aa102011-02-24 05:42:24 +00001568 if (NumArgs == 1) {
1569 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001570 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001571 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001572 diag::err_attribute_not_string) << "unavailable";
1573 return;
1574 }
Chris Lattner190aa102011-02-24 05:42:24 +00001575 Str = SE->getString();
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001576 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001577 D->addAttr(::new (S.Context) UnavailableAttr(Attr.getRange(), S.Context, Str));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001578}
1579
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00001580static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1581 const AttributeList &Attr) {
1582 unsigned NumArgs = Attr.getNumArgs();
1583 if (NumArgs > 0) {
1584 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1585 return;
1586 }
1587
1588 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001589 Attr.getRange(), S.Context));
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00001590}
1591
Patrick Beardacfbe9e2012-04-06 18:12:22 +00001592static void handleObjCRootClassAttr(Sema &S, Decl *D,
1593 const AttributeList &Attr) {
1594 if (!isa<ObjCInterfaceDecl>(D)) {
1595 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1596 return;
1597 }
1598
1599 unsigned NumArgs = Attr.getNumArgs();
1600 if (NumArgs > 0) {
1601 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1602 return;
1603 }
1604
1605 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1606}
1607
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001608static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001609 const AttributeList &Attr) {
Fariborz Jahanian7249e362012-01-03 22:52:32 +00001610 if (!isa<ObjCInterfaceDecl>(D)) {
1611 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1612 return;
1613 }
1614
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001615 unsigned NumArgs = Attr.getNumArgs();
1616 if (NumArgs > 0) {
1617 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1618 return;
1619 }
1620
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001621 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001622 Attr.getRange(), S.Context));
1623}
1624
Chandler Carruthedc2c642011-07-02 00:01:44 +00001625static void handleAvailabilityAttr(Sema &S, Decl *D,
1626 const AttributeList &Attr) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001627 IdentifierInfo *Platform = Attr.getParameterName();
1628 SourceLocation PlatformLoc = Attr.getParameterLoc();
1629
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001630 StringRef PlatformName
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001631 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1632 if (PlatformName.empty()) {
1633 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1634 << Platform;
1635
1636 PlatformName = Platform->getName();
1637 }
1638
1639 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1640 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1641 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001642 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001643
Douglas Gregor05a51ae2011-08-10 16:09:55 +00001644 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001645 // of these steps are needed).
1646 if (Introduced.isValid() && Deprecated.isValid() &&
Douglas Gregor6f47e5c2011-08-10 15:31:35 +00001647 !(Introduced.Version <= Deprecated.Version)) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001648 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1649 << 1 << PlatformName << Deprecated.Version.getAsString()
1650 << 0 << Introduced.Version.getAsString();
1651 return;
1652 }
1653
1654 if (Introduced.isValid() && Obsoleted.isValid() &&
Douglas Gregor6f47e5c2011-08-10 15:31:35 +00001655 !(Introduced.Version <= Obsoleted.Version)) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001656 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1657 << 2 << PlatformName << Obsoleted.Version.getAsString()
1658 << 0 << Introduced.Version.getAsString();
1659 return;
1660 }
1661
1662 if (Deprecated.isValid() && Obsoleted.isValid() &&
Douglas Gregor6f47e5c2011-08-10 15:31:35 +00001663 !(Deprecated.Version <= Obsoleted.Version)) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001664 S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering)
1665 << 2 << PlatformName << Obsoleted.Version.getAsString()
1666 << 1 << Deprecated.Version.getAsString();
1667 return;
1668 }
1669
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001670 StringRef Str;
1671 const StringLiteral *SE =
1672 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
1673 if (SE)
1674 Str = SE->getString();
1675
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001676 D->addAttr(::new (S.Context) AvailabilityAttr(Attr.getRange(), S.Context,
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001677 Platform,
1678 Introduced.Version,
1679 Deprecated.Version,
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001680 Obsoleted.Version,
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001681 IsUnavailable,
1682 Str));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001683}
1684
Chandler Carruthedc2c642011-07-02 00:01:44 +00001685static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001686 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001687 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001688 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001689
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001690 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001691 Arg = Arg->IgnoreParenCasts();
1692 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001693
Douglas Gregorfb65e592011-07-27 05:40:30 +00001694 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001695 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001696 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001697 return;
1698 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001699
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001700 StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001701 VisibilityAttr::VisibilityType type;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001702
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001703 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001704 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001705 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001706 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001707 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001708 type = VisibilityAttr::Hidden; // FIXME
John McCalleed64c72012-01-29 01:20:30 +00001709 else if (TypeStr == "protected") {
1710 // Complain about attempts to use protected visibility on targets
1711 // (like Darwin) that don't support it.
1712 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
1713 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1714 type = VisibilityAttr::Default;
1715 } else {
1716 type = VisibilityAttr::Protected;
1717 }
1718 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001719 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001720 return;
1721 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001722
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001723 D->addAttr(::new (S.Context) VisibilityAttr(Attr.getRange(), S.Context, type));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001724}
1725
Chandler Carruthedc2c642011-07-02 00:01:44 +00001726static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1727 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00001728 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1729 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001730 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001731 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00001732 return;
1733 }
1734
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001735 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
1736 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
1737 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCall86bc21f2011-03-02 11:33:24 +00001738 << "objc_method_family" << 1;
1739 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001740 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCall86bc21f2011-03-02 11:33:24 +00001741 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001742 Attr.setInvalid();
John McCall86bc21f2011-03-02 11:33:24 +00001743 return;
1744 }
1745
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001746 StringRef param = Attr.getParameterName()->getName();
John McCall86bc21f2011-03-02 11:33:24 +00001747 ObjCMethodFamilyAttr::FamilyKind family;
1748 if (param == "none")
1749 family = ObjCMethodFamilyAttr::OMF_None;
1750 else if (param == "alloc")
1751 family = ObjCMethodFamilyAttr::OMF_alloc;
1752 else if (param == "copy")
1753 family = ObjCMethodFamilyAttr::OMF_copy;
1754 else if (param == "init")
1755 family = ObjCMethodFamilyAttr::OMF_init;
1756 else if (param == "mutableCopy")
1757 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1758 else if (param == "new")
1759 family = ObjCMethodFamilyAttr::OMF_new;
1760 else {
1761 // Just warn and ignore it. This is future-proof against new
1762 // families being used in system headers.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001763 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCall86bc21f2011-03-02 11:33:24 +00001764 return;
1765 }
1766
John McCall31168b02011-06-15 23:02:42 +00001767 if (family == ObjCMethodFamilyAttr::OMF_init &&
1768 !method->getResultType()->isObjCObjectPointerType()) {
1769 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
1770 << method->getResultType();
1771 // Ignore the attribute.
1772 return;
1773 }
1774
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001775 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCall31168b02011-06-15 23:02:42 +00001776 S.Context, family));
John McCall86bc21f2011-03-02 11:33:24 +00001777}
1778
Chandler Carruthedc2c642011-07-02 00:01:44 +00001779static void handleObjCExceptionAttr(Sema &S, Decl *D,
1780 const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001781 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner677a3582009-02-14 08:09:34 +00001782 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001783
Chris Lattner677a3582009-02-14 08:09:34 +00001784 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1785 if (OCI == 0) {
1786 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1787 return;
1788 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001789
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001790 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner677a3582009-02-14 08:09:34 +00001791}
1792
Chandler Carruthedc2c642011-07-02 00:01:44 +00001793static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001794 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001795 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001796 return;
1797 }
Richard Smithdda56e42011-04-15 14:24:37 +00001798 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001799 QualType T = TD->getUnderlyingType();
1800 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001801 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001802 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1803 return;
1804 }
1805 }
Ted Kremenek05e916b2012-03-01 01:40:32 +00001806 else if (!isa<ObjCPropertyDecl>(D)) {
1807 // It is okay to include this attribute on properties, e.g.:
1808 //
1809 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
1810 //
1811 // In this case it follows tradition and suppresses an error in the above
1812 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00001813 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00001814 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001815 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001816}
1817
Mike Stumpd3bb5572009-07-24 19:02:52 +00001818static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00001819handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001820 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001821 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001822 return;
1823 }
1824
1825 if (!isa<FunctionDecl>(D)) {
1826 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1827 return;
1828 }
1829
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001830 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001831}
1832
Chandler Carruthedc2c642011-07-02 00:01:44 +00001833static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001834 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001835 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001836 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001837 return;
1838 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001839
Steve Naroff3405a732008-09-18 16:44:58 +00001840 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001841 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001842 return;
1843 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001844
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001845 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00001846 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00001847 type = BlocksAttr::ByRef;
1848 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001849 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001850 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00001851 return;
1852 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001853
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001854 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff3405a732008-09-18 16:44:58 +00001855}
1856
Chandler Carruthedc2c642011-07-02 00:01:44 +00001857static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00001858 // check the attribute arguments.
1859 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00001860 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00001861 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001862 }
1863
John McCallb46f2872011-09-09 07:56:05 +00001864 unsigned sentinel = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001865 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001866 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00001867 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001868 if (E->isTypeDependent() || E->isValueDependent() ||
1869 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001870 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001871 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001872 return;
1873 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001874
John McCallb46f2872011-09-09 07:56:05 +00001875 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001876 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1877 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001878 return;
1879 }
John McCallb46f2872011-09-09 07:56:05 +00001880
1881 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00001882 }
1883
John McCallb46f2872011-09-09 07:56:05 +00001884 unsigned nullPos = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001885 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001886 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00001887 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001888 if (E->isTypeDependent() || E->isValueDependent() ||
1889 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001890 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001891 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001892 return;
1893 }
1894 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001895
John McCallb46f2872011-09-09 07:56:05 +00001896 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00001897 // FIXME: This error message could be improved, it would be nice
1898 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00001899 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1900 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001901 return;
1902 }
1903 }
1904
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001905 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00001906 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00001907 if (isa<FunctionNoProtoType>(FT)) {
1908 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1909 return;
1910 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001911
Chris Lattner9363e312009-03-17 23:03:47 +00001912 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001913 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001914 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001915 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001916 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00001917 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001918 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001919 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001920 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00001921 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
1922 if (!BD->isVariadic()) {
1923 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
1924 return;
1925 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001926 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001927 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00001928 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001929 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00001930 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001931 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001932 int m = Ty->isFunctionPointerType() ? 0 : 1;
1933 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001934 return;
1935 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001936 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001937 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001938 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001939 return;
1940 }
Anders Carlssonc181b012008-10-05 18:05:59 +00001941 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00001942 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001943 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00001944 return;
1945 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001946 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherbc638a82010-12-01 22:13:54 +00001947 nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00001948}
1949
Chandler Carruthedc2c642011-07-02 00:01:44 +00001950static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner237f2752009-02-14 07:37:35 +00001951 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001952 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner237f2752009-02-14 07:37:35 +00001953 return;
Chris Lattner237f2752009-02-14 07:37:35 +00001954
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001955 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00001956 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001957 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner237f2752009-02-14 07:37:35 +00001958 return;
1959 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001960
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001961 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1962 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1963 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00001964 return;
1965 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001966 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1967 if (MD->getResultType()->isVoidType()) {
1968 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1969 << Attr.getName() << 1;
1970 return;
1971 }
1972
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001973 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner237f2752009-02-14 07:37:35 +00001974}
1975
Chandler Carruthedc2c642011-07-02 00:01:44 +00001976static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001977 // check the attribute arguments.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001978 if (Attr.hasParameterOrArguments()) {
1979 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001980 return;
1981 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001982
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001983 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00001984 if (isa<CXXRecordDecl>(D)) {
1985 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
1986 return;
1987 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001988 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1989 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00001990 return;
1991 }
1992
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001993 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001994
1995 // 'weak' only applies to declarations with external linkage.
1996 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001997 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001998 return;
1999 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002000
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002001 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002002}
2003
Chandler Carruthedc2c642011-07-02 00:01:44 +00002004static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002005 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002006 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002007 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002008
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002009
2010 // weak_import only applies to variable & function declarations.
2011 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002012 if (!D->canBeWeakImported(isDef)) {
2013 if (isDef)
2014 S.Diag(Attr.getLoc(),
2015 diag::warn_attribute_weak_import_invalid_on_definition)
2016 << "weak_import" << 2 /*variable and function*/;
Douglas Gregord71149a2011-03-23 13:27:51 +00002017 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002018 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002019 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002020 // Nothing to warn about here.
2021 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002022 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002023 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002024
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002025 return;
2026 }
2027
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002028 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002029}
2030
Chandler Carruthedc2c642011-07-02 00:01:44 +00002031static void handleReqdWorkGroupSize(Sema &S, Decl *D,
2032 const AttributeList &Attr) {
Nate Begemanf2758702009-06-26 06:32:41 +00002033 // Attribute has 3 arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002034 if (!checkAttributeNumArgs(S, Attr, 3))
Nate Begemanf2758702009-06-26 06:32:41 +00002035 return;
Nate Begemanf2758702009-06-26 06:32:41 +00002036
2037 unsigned WGSize[3];
2038 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002039 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00002040 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002041 if (E->isTypeDependent() || E->isValueDependent() ||
2042 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00002043 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2044 << "reqd_work_group_size" << E->getSourceRange();
2045 return;
2046 }
2047 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2048 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002049 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002050 WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +00002051 WGSize[2]));
2052}
2053
Chandler Carruthedc2c642011-07-02 00:01:44 +00002054static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002055 // Attribute has no arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002056 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002057 return;
Daniel Dunbar648bf782009-02-12 17:28:23 +00002058
2059 // Make sure that there is a string literal as the sections's single
2060 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002061 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002062 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002063 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002064 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00002065 return;
2066 }
Mike Stump11289f42009-09-09 15:08:12 +00002067
Chris Lattner30ba6742009-08-10 19:03:04 +00002068 // If the target wants to validate the section specifier, make it happen.
Douglas Gregore8bbc122011-09-02 00:18:52 +00002069 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00002070 if (!Error.empty()) {
2071 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2072 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002073 return;
2074 }
Mike Stump11289f42009-09-09 15:08:12 +00002075
Chris Lattner20aee9b2010-01-12 20:58:53 +00002076 // This attribute cannot be applied to local variables.
2077 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2078 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2079 return;
2080 }
2081
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002082 D->addAttr(::new (S.Context) SectionAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00002083 SE->getString()));
Daniel Dunbar648bf782009-02-12 17:28:23 +00002084}
2085
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002086
Chandler Carruthedc2c642011-07-02 00:01:44 +00002087static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002088 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002089 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002090 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002091 return;
2092 }
Douglas Gregor88336832011-06-15 05:45:11 +00002093
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002094 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002095 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002096 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002097 } else {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002098 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00002099 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002100}
2101
Chandler Carruthedc2c642011-07-02 00:01:44 +00002102static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002103 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002104 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002105 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00002106 return;
2107 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002108
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002109 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002110 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002111 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002112 } else {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002113 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00002114 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002115}
2116
Chandler Carruthedc2c642011-07-02 00:01:44 +00002117static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002118 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002119 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssonb8316282008-10-05 23:32:53 +00002120 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002121
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002122 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00002123}
2124
Chandler Carruthedc2c642011-07-02 00:01:44 +00002125static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002126 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00002127 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2128 return;
2129 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002130
Anders Carlssond277d792009-01-31 01:16:18 +00002131 if (Attr.getNumArgs() != 0) {
2132 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2133 return;
2134 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002135
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002136 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002137
Anders Carlssond277d792009-01-31 01:16:18 +00002138 if (!VD || !VD->hasLocalStorage()) {
2139 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2140 return;
2141 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002142
Anders Carlssond277d792009-01-31 01:16:18 +00002143 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00002144 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00002145 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002146 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2147 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00002148 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002149 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00002150 Attr.getParameterName();
2151 return;
2152 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002153
Anders Carlssond277d792009-01-31 01:16:18 +00002154 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2155 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002156 S.Diag(Attr.getParameterLoc(),
2157 diag::err_attribute_cleanup_arg_not_function)
2158 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002159 return;
2160 }
2161
Anders Carlssond277d792009-01-31 01:16:18 +00002162 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002163 S.Diag(Attr.getParameterLoc(),
2164 diag::err_attribute_cleanup_func_must_take_one_arg)
2165 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002166 return;
2167 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002168
Anders Carlsson723f55d2009-02-07 23:16:50 +00002169 // We're currently more strict than GCC about what function types we accept.
2170 // If this ever proves to be a problem it should be easy to fix.
2171 QualType Ty = S.Context.getPointerType(VD->getType());
2172 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002173 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2174 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002175 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00002176 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2177 Attr.getParameterName() << ParamTy << Ty;
2178 return;
2179 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002180
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002181 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedmanfa0df832012-02-02 03:46:19 +00002182 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssond277d792009-01-31 01:16:18 +00002183}
2184
Mike Stumpd3bb5572009-07-24 19:02:52 +00002185/// Handle __attribute__((format_arg((idx)))) attribute based on
2186/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002187static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002188 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002189 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002190
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002191 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002192 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002193 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002194 return;
2195 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002196
2197 // In C++ the implicit 'this' function parameter also counts, and they are
2198 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002199 bool HasImplicitThisParam = isInstanceMethod(D);
2200 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002201 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00002202
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002203 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002204 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002205 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002206 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2207 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002208 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2209 << "format" << 2 << IdxExpr->getSourceRange();
2210 return;
2211 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002212
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002213 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2214 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2215 << "format" << 2 << IdxExpr->getSourceRange();
2216 return;
2217 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002218
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002219 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002220
Chandler Carruth743682b2010-11-16 08:35:43 +00002221 if (HasImplicitThisParam) {
2222 if (ArgIdx == 0) {
2223 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2224 << "format_arg" << IdxExpr->getSourceRange();
2225 return;
2226 }
2227 ArgIdx--;
2228 }
2229
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002230 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002231 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002232
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002233 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2234 if (not_nsstring_type &&
2235 !isCFStringType(Ty, S.Context) &&
2236 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002237 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002238 // FIXME: Should highlight the actual expression that has the wrong type.
2239 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002240 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002241 << IdxExpr->getSourceRange();
2242 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002243 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002244 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002245 if (!isNSStringType(Ty, S.Context) &&
2246 !isCFStringType(Ty, S.Context) &&
2247 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002248 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002249 // FIXME: Should highlight the actual expression that has the wrong type.
2250 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002251 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002252 << IdxExpr->getSourceRange();
2253 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002254 }
2255
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002256 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth743682b2010-11-16 08:35:43 +00002257 Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002258}
2259
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002260enum FormatAttrKind {
2261 CFStringFormat,
2262 NSStringFormat,
2263 StrftimeFormat,
2264 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002265 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002266 InvalidFormat
2267};
2268
2269/// getFormatAttrKind - Map from format attribute names to supported format
2270/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002271static FormatAttrKind getFormatAttrKind(StringRef Format) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002272 // Check for formats that get handled specially.
2273 if (Format == "NSString")
2274 return NSStringFormat;
2275 if (Format == "CFString")
2276 return CFStringFormat;
2277 if (Format == "strftime")
2278 return StrftimeFormat;
2279
2280 // Otherwise, check for supported formats.
2281 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
Jean-Daniel Dupasfc0da1a2012-01-27 09:14:17 +00002282 Format == "strfmon" || Format == "cmn_err" || Format == "vcmn_err" ||
Chris Lattner0ddd0ae2011-02-18 17:05:55 +00002283 Format == "zcmn_err" ||
2284 Format == "kprintf") // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002285 return SupportedFormat;
2286
Duncan Sandsde4fe352010-03-23 14:44:19 +00002287 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
2288 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner12161d32010-03-22 21:08:50 +00002289 return IgnoredFormat;
2290
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002291 return InvalidFormat;
2292}
2293
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002294/// Handle __attribute__((init_priority(priority))) attributes based on
2295/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002296static void handleInitPriorityAttr(Sema &S, Decl *D,
2297 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002298 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002299 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2300 return;
2301 }
2302
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002303 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002304 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2305 Attr.setInvalid();
2306 return;
2307 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002308 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002309 if (S.Context.getAsArrayType(T))
2310 T = S.Context.getBaseElementType(T);
2311 if (!T->getAs<RecordType>()) {
2312 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2313 Attr.setInvalid();
2314 return;
2315 }
2316
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002317 if (Attr.getNumArgs() != 1) {
2318 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2319 Attr.setInvalid();
2320 return;
2321 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002322 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002323
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002324 llvm::APSInt priority(32);
2325 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2326 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2327 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2328 << "init_priority" << priorityExpr->getSourceRange();
2329 Attr.setInvalid();
2330 return;
2331 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00002332 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002333 if (prioritynum < 101 || prioritynum > 65535) {
2334 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2335 << priorityExpr->getSourceRange();
2336 Attr.setInvalid();
2337 return;
2338 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002339 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00002340 prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002341}
2342
Mike Stumpd3bb5572009-07-24 19:02:52 +00002343/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2344/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002345static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002346
Chris Lattner4a927cb2008-06-28 23:36:30 +00002347 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002348 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002349 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002350 return;
2351 }
2352
Chris Lattner4a927cb2008-06-28 23:36:30 +00002353 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002354 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002355 return;
2356 }
2357
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002358 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002359 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002360 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002361 return;
2362 }
2363
Chandler Carruth743682b2010-11-16 08:35:43 +00002364 // In C++ the implicit 'this' function parameter also counts, and they are
2365 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002366 bool HasImplicitThisParam = isInstanceMethod(D);
2367 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002368 unsigned FirstIdx = 1;
2369
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002370 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002371
2372 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002373 if (Format.startswith("__") && Format.endswith("__"))
2374 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002375
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002376 // Check for supported formats.
2377 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002378
2379 if (Kind == IgnoredFormat)
2380 return;
2381
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002382 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002383 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00002384 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002385 return;
2386 }
2387
2388 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002389 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002390 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002391 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2392 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002393 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002394 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002395 return;
2396 }
2397
2398 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002399 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002400 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002401 return;
2402 }
2403
2404 // FIXME: Do we need to bounds check?
2405 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002406
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002407 if (HasImplicitThisParam) {
2408 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002409 S.Diag(Attr.getLoc(),
2410 diag::err_format_attribute_implicit_this_format_string)
2411 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002412 return;
2413 }
2414 ArgIdx--;
2415 }
Mike Stump11289f42009-09-09 15:08:12 +00002416
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002417 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002418 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002419
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002420 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002421 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002422 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2423 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002424 return;
2425 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002426 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002427 // FIXME: do we need to check if the type is NSString*? What are the
2428 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002429 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002430 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002431 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2432 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002433 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002434 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002435 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002436 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002437 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002438 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2439 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002440 return;
2441 }
2442
2443 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002444 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002445 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002446 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2447 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002448 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002449 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002450 return;
2451 }
2452
2453 // check if the function is variadic if the 3rd argument non-zero
2454 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002455 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002456 ++NumArgs; // +1 for ...
2457 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002458 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002459 return;
2460 }
2461 }
2462
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002463 // strftime requires FirstArg to be 0 because it doesn't read from any
2464 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002465 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002466 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002467 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2468 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002469 return;
2470 }
2471 // if 0 it disables parameter checking (to use with e.g. va_list)
2472 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002473 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002474 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002475 return;
2476 }
2477
Douglas Gregor88336832011-06-15 05:45:11 +00002478 // Check whether we already have an equivalent format attribute.
2479 for (specific_attr_iterator<FormatAttr>
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002480 i = D->specific_attr_begin<FormatAttr>(),
2481 e = D->specific_attr_end<FormatAttr>();
Douglas Gregor88336832011-06-15 05:45:11 +00002482 i != e ; ++i) {
2483 FormatAttr *f = *i;
2484 if (f->getType() == Format &&
2485 f->getFormatIdx() == (int)Idx.getZExtValue() &&
2486 f->getFirstArg() == (int)FirstArg.getZExtValue()) {
2487 // If we don't have a valid location for this attribute, adopt the
2488 // location.
2489 if (f->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002490 f->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002491 return;
2492 }
2493 }
2494
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002495 D->addAttr(::new (S.Context) FormatAttr(Attr.getRange(), S.Context, Format,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002496 Idx.getZExtValue(),
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002497 FirstArg.getZExtValue()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002498}
2499
Chandler Carruthedc2c642011-07-02 00:01:44 +00002500static void handleTransparentUnionAttr(Sema &S, Decl *D,
2501 const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002502 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002503 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002504 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002505
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002506
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002507 // Try to find the underlying union declaration.
2508 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002509 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002510 if (TD && TD->getUnderlyingType()->isUnionType())
2511 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2512 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002513 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002514
2515 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002516 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002517 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002518 return;
2519 }
2520
John McCallf937c022011-10-07 06:10:15 +00002521 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002522 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002523 diag::warn_transparent_union_attribute_not_definition);
2524 return;
2525 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002526
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002527 RecordDecl::field_iterator Field = RD->field_begin(),
2528 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002529 if (Field == FieldEnd) {
2530 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2531 return;
2532 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002533
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002534 FieldDecl *FirstField = *Field;
2535 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002536 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002537 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002538 diag::warn_transparent_union_attribute_floating)
2539 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002540 return;
2541 }
2542
2543 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2544 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2545 for (; Field != FieldEnd; ++Field) {
2546 QualType FieldType = Field->getType();
2547 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2548 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2549 // Warn if we drop the attribute.
2550 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002551 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002552 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002553 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002554 diag::warn_transparent_union_attribute_field_size_align)
2555 << isSize << Field->getDeclName() << FieldBits;
2556 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002557 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002558 diag::note_transparent_union_first_field_size_align)
2559 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002560 return;
2561 }
2562 }
2563
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002564 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002565}
2566
Chandler Carruthedc2c642011-07-02 00:01:44 +00002567static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002568 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002569 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002570 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002571
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002572 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002573 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002574
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002575 // Make sure that there is a string literal as the annotation's single
2576 // argument.
2577 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002578 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002579 return;
2580 }
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002581
2582 // Don't duplicate annotations that are already set.
2583 for (specific_attr_iterator<AnnotateAttr>
2584 i = D->specific_attr_begin<AnnotateAttr>(),
2585 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
2586 if ((*i)->getAnnotation() == SE->getString())
2587 return;
2588 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002589 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00002590 SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002591}
2592
Chandler Carruthedc2c642011-07-02 00:01:44 +00002593static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002594 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002595 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002596 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002597 return;
2598 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002599
2600 //FIXME: The C++0x version of this attribute has more limited applicabilty
2601 // than GNU's, and should error out when it is used to specify a
2602 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002603
Chris Lattner4a927cb2008-06-28 23:36:30 +00002604 if (Attr.getNumArgs() == 0) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002605 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, true, 0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002606 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002607 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002608
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002609 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002610}
2611
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002612void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E) {
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002613 // FIXME: Handle pack-expansions here.
2614 if (DiagnoseUnexpandedParameterPack(E))
2615 return;
2616
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002617 if (E->isTypeDependent() || E->isValueDependent()) {
2618 // Save dependent expressions in the AST to be instantiated.
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002619 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002620 return;
2621 }
2622
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002623 SourceLocation AttrLoc = AttrRange.getBegin();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002624 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002625 llvm::APSInt Alignment(32);
Richard Smithf4c51d92012-02-04 09:53:13 +00002626 ExprResult ICE =
2627 VerifyIntegerConstantExpression(E, &Alignment,
2628 PDiag(diag::err_attribute_argument_not_int) << "aligned",
2629 /*AllowFold*/ false);
2630 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002631 return;
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002632 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002633 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2634 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002635 return;
2636 }
2637
Richard Smithf4c51d92012-02-04 09:53:13 +00002638 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take()));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002639}
2640
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002641void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002642 // FIXME: Cache the number on the Attr object if non-dependent?
2643 // FIXME: Perform checking of type validity
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002644 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002645 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002646}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002647
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002648/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002649/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002650///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002651/// Despite what would be logical, the mode attribute is a decl attribute, not a
2652/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2653/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002654static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002655 // This attribute isn't documented, but glibc uses it. It changes
2656 // the width of an int or unsigned int to the specified size.
2657
2658 // Check that there aren't any arguments
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002659 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002660 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002661
Chris Lattneracbc2d22008-06-27 22:18:37 +00002662
2663 IdentifierInfo *Name = Attr.getParameterName();
2664 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00002665 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002666 return;
2667 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00002668
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002669 StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002670
2671 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002672 if (Str.startswith("__") && Str.endswith("__"))
2673 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002674
2675 unsigned DestWidth = 0;
2676 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002677 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002678 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002679 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002680 switch (Str[0]) {
2681 case 'Q': DestWidth = 8; break;
2682 case 'H': DestWidth = 16; break;
2683 case 'S': DestWidth = 32; break;
2684 case 'D': DestWidth = 64; break;
2685 case 'X': DestWidth = 96; break;
2686 case 'T': DestWidth = 128; break;
2687 }
2688 if (Str[1] == 'F') {
2689 IntegerMode = false;
2690 } else if (Str[1] == 'C') {
2691 IntegerMode = false;
2692 ComplexMode = true;
2693 } else if (Str[1] != 'I') {
2694 DestWidth = 0;
2695 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002696 break;
2697 case 4:
2698 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2699 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002700 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002701 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002702 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002703 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002704 break;
2705 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002706 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002707 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002708 break;
2709 }
2710
2711 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002712 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002713 OldTy = TD->getUnderlyingType();
2714 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2715 OldTy = VD->getType();
2716 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002717 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002718 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002719 return;
2720 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002721
John McCall9dd450b2009-09-21 23:43:11 +00002722 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002723 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2724 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002725 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002726 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2727 } else if (ComplexMode) {
2728 if (!OldTy->isComplexType())
2729 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2730 } else {
2731 if (!OldTy->isFloatingType())
2732 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2733 }
2734
Mike Stump87c57ac2009-05-16 07:39:55 +00002735 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2736 // and friends, at least with glibc.
2737 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2738 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002739 // FIXME: Make sure floating-point mappings are accurate
2740 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00002741 QualType NewTy;
2742 switch (DestWidth) {
2743 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002744 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002745 return;
2746 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002747 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002748 return;
2749 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00002750 if (!IntegerMode) {
2751 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2752 return;
2753 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002754 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002755 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002756 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002757 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002758 break;
2759 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00002760 if (!IntegerMode) {
2761 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2762 return;
2763 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002764 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002765 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002766 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002767 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002768 break;
2769 case 32:
2770 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002771 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002772 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002773 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002774 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002775 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002776 break;
2777 case 64:
2778 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002779 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002780 else if (OldTy->isSignedIntegerType())
Douglas Gregore8bbc122011-09-02 00:18:52 +00002781 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00002782 NewTy = S.Context.LongTy;
2783 else
2784 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002785 else
Douglas Gregore8bbc122011-09-02 00:18:52 +00002786 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00002787 NewTy = S.Context.UnsignedLongTy;
2788 else
2789 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002790 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00002791 case 96:
2792 NewTy = S.Context.LongDoubleTy;
2793 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00002794 case 128:
2795 if (!IntegerMode) {
2796 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2797 return;
2798 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00002799 if (OldTy->isSignedIntegerType())
2800 NewTy = S.Context.Int128Ty;
2801 else
2802 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00002803 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002804 }
2805
Eli Friedman4735374e2009-03-03 06:41:03 +00002806 if (ComplexMode) {
2807 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002808 }
2809
2810 // Install the new type.
Richard Smithdda56e42011-04-15 14:24:37 +00002811 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall703a3f82009-10-24 08:00:42 +00002812 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00002813 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00002814 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00002815 cast<ValueDecl>(D)->setType(NewTy);
2816}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002817
Chandler Carruthedc2c642011-07-02 00:01:44 +00002818static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson76187b42009-02-13 06:46:13 +00002819 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002820 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson76187b42009-02-13 06:46:13 +00002821 return;
Anders Carlsson63784f42009-02-13 08:11:52 +00002822
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002823 if (!isFunctionOrMethod(D)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00002824 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002825 << Attr.getName() << ExpectedFunction;
Anders Carlsson76187b42009-02-13 06:46:13 +00002826 return;
2827 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002828
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002829 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlsson76187b42009-02-13 06:46:13 +00002830}
2831
Chandler Carruthedc2c642011-07-02 00:01:44 +00002832static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson88097122009-02-19 19:16:48 +00002833 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002834 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson88097122009-02-19 19:16:48 +00002835 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002836
Mike Stumpd3bb5572009-07-24 19:02:52 +00002837
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002838 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00002839 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002840 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00002841 return;
2842 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002843
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002844 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson88097122009-02-19 19:16:48 +00002845}
2846
Chandler Carruthedc2c642011-07-02 00:01:44 +00002847static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
2848 const AttributeList &Attr) {
Chris Lattner3c77a352010-06-22 00:03:40 +00002849 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002850 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner3c77a352010-06-22 00:03:40 +00002851 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002852
Chris Lattner3c77a352010-06-22 00:03:40 +00002853
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002854 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00002855 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002856 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00002857 return;
2858 }
2859
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002860 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002861 S.Context));
Chris Lattner3c77a352010-06-22 00:03:40 +00002862}
2863
Chandler Carruthedc2c642011-07-02 00:01:44 +00002864static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002865 if (S.LangOpts.CUDA) {
2866 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002867 if (Attr.hasParameterOrArguments()) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002868 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2869 return;
2870 }
2871
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002872 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002873 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002874 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002875 return;
2876 }
2877
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002878 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002879 } else {
2880 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2881 }
2882}
2883
Chandler Carruthedc2c642011-07-02 00:01:44 +00002884static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002885 if (S.LangOpts.CUDA) {
2886 // check the attribute arguments.
2887 if (Attr.getNumArgs() != 0) {
2888 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2889 return;
2890 }
2891
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002892 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002893 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002894 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002895 return;
2896 }
2897
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002898 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002899 } else {
2900 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2901 }
2902}
2903
Chandler Carruthedc2c642011-07-02 00:01:44 +00002904static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002905 if (S.LangOpts.CUDA) {
2906 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002907 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002908 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002909
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002910 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002911 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002912 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002913 return;
2914 }
2915
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002916 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00002917 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00002918 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00002919 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2920 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2921 << FD->getType()
2922 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2923 "void");
2924 } else {
2925 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2926 << FD->getType();
2927 }
2928 return;
2929 }
2930
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002931 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002932 } else {
2933 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2934 }
2935}
2936
Chandler Carruthedc2c642011-07-02 00:01:44 +00002937static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002938 if (S.LangOpts.CUDA) {
2939 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002940 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002941 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002942
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002943
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002944 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002945 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002946 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002947 return;
2948 }
2949
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002950 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002951 } else {
2952 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2953 }
2954}
2955
Chandler Carruthedc2c642011-07-02 00:01:44 +00002956static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002957 if (S.LangOpts.CUDA) {
2958 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002959 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002960 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002961
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002962
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002963 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002964 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002965 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002966 return;
2967 }
2968
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002969 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002970 } else {
2971 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2972 }
2973}
2974
Chandler Carruthedc2c642011-07-02 00:01:44 +00002975static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00002976 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002977 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnereaad6b72009-04-14 16:30:50 +00002978 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002979
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002980 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00002981 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00002982 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002983 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00002984 return;
2985 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002986
Douglas Gregor35b57532009-10-27 21:01:01 +00002987 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00002988 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00002989 return;
2990 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002991
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002992 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattnereaad6b72009-04-14 16:30:50 +00002993}
2994
Chandler Carruthedc2c642011-07-02 00:01:44 +00002995static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002996 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002997
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002998 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00002999 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3000 CallingConv CC;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003001 if (S.CheckCallingConvAttr(Attr, CC))
John McCall3882ace2011-01-05 12:14:39 +00003002 return;
3003
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003004 if (!isa<ObjCMethodDecl>(D)) {
3005 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3006 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003007 return;
3008 }
3009
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003010 switch (Attr.getKind()) {
Abramo Bagnara50099372010-04-30 13:10:51 +00003011 case AttributeList::AT_fastcall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003012 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003013 return;
3014 case AttributeList::AT_stdcall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003015 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003016 return;
Douglas Gregora941dca2010-05-18 16:57:00 +00003017 case AttributeList::AT_thiscall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003018 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003019 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003020 case AttributeList::AT_cdecl:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003021 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003022 return;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003023 case AttributeList::AT_pascal:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003024 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003025 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003026 case AttributeList::AT_pcs: {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003027 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003028 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003029 if (!Str || !Str->isAscii()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003030 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003031 << "pcs" << 1;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003032 Attr.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003033 return;
3034 }
3035
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003036 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003037 PcsAttr::PCSType PCS;
3038 if (StrRef == "aapcs")
3039 PCS = PcsAttr::AAPCS;
3040 else if (StrRef == "aapcs-vfp")
3041 PCS = PcsAttr::AAPCS_VFP;
3042 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003043 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
3044 Attr.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003045 return;
3046 }
3047
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003048 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003049 }
Abramo Bagnara50099372010-04-30 13:10:51 +00003050 default:
3051 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003052 }
3053}
3054
Chandler Carruthedc2c642011-07-02 00:01:44 +00003055static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth9312c642011-07-11 23:33:05 +00003056 assert(!Attr.isInvalid());
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003057 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003058}
3059
John McCall3882ace2011-01-05 12:14:39 +00003060bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3061 if (attr.isInvalid())
3062 return true;
3063
Ted Kremenek1551d552011-04-15 05:49:29 +00003064 if ((attr.getNumArgs() != 0 &&
3065 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
3066 attr.getParameterName()) {
John McCall3882ace2011-01-05 12:14:39 +00003067 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3068 attr.setInvalid();
3069 return true;
3070 }
3071
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003072 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3073 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003074 switch (attr.getKind()) {
3075 case AttributeList::AT_cdecl: CC = CC_C; break;
3076 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
3077 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
3078 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
3079 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003080 case AttributeList::AT_pcs: {
3081 Expr *Arg = attr.getArg(0);
3082 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003083 if (!Str || !Str->isAscii()) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003084 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3085 << "pcs" << 1;
3086 attr.setInvalid();
3087 return true;
3088 }
3089
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003090 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003091 if (StrRef == "aapcs") {
3092 CC = CC_AAPCS;
3093 break;
3094 } else if (StrRef == "aapcs-vfp") {
3095 CC = CC_AAPCS_VFP;
3096 break;
3097 }
3098 // FALLS THROUGH
3099 }
David Blaikie8a40f702012-01-17 06:56:22 +00003100 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003101 }
3102
3103 return false;
3104}
3105
Chandler Carruthedc2c642011-07-02 00:01:44 +00003106static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003107 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003108
3109 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003110 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003111 return;
3112
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003113 if (!isa<ObjCMethodDecl>(D)) {
3114 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3115 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003116 return;
3117 }
Eli Friedman7044b762009-03-27 21:06:47 +00003118
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003119 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall3882ace2011-01-05 12:14:39 +00003120}
3121
3122/// Checks a regparm attribute, returning true if it is ill-formed and
3123/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003124bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3125 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003126 return true;
3127
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003128 if (Attr.getNumArgs() != 1) {
3129 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3130 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003131 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003132 }
Eli Friedman7044b762009-03-27 21:06:47 +00003133
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003134 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00003135 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003136 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00003137 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003138 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00003139 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003140 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003141 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003142 }
3143
Douglas Gregore8bbc122011-09-02 00:18:52 +00003144 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003145 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003146 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003147 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003148 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003149 }
3150
John McCall3882ace2011-01-05 12:14:39 +00003151 numParams = NumParams.getZExtValue();
Douglas Gregore8bbc122011-09-02 00:18:52 +00003152 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003153 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003154 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003155 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003156 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003157 }
3158
John McCall3882ace2011-01-05 12:14:39 +00003159 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003160}
3161
Chandler Carruthedc2c642011-07-02 00:01:44 +00003162static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00003163 if (S.LangOpts.CUDA) {
3164 // check the attribute arguments.
3165 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00003166 // FIXME: 0 is not okay.
3167 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003168 return;
3169 }
3170
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003171 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00003172 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003173 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003174 return;
3175 }
3176
3177 Expr *MaxThreadsExpr = Attr.getArg(0);
3178 llvm::APSInt MaxThreads(32);
3179 if (MaxThreadsExpr->isTypeDependent() ||
3180 MaxThreadsExpr->isValueDependent() ||
3181 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3182 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3183 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3184 return;
3185 }
3186
3187 llvm::APSInt MinBlocks(32);
3188 if (Attr.getNumArgs() > 1) {
3189 Expr *MinBlocksExpr = Attr.getArg(1);
3190 if (MinBlocksExpr->isTypeDependent() ||
3191 MinBlocksExpr->isValueDependent() ||
3192 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3193 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3194 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3195 return;
3196 }
3197 }
3198
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003199 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne827301e2010-12-12 23:03:07 +00003200 MaxThreads.getZExtValue(),
3201 MinBlocks.getZExtValue()));
3202 } else {
3203 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3204 }
3205}
3206
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003207//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003208// Checker-specific attribute handlers.
3209//===----------------------------------------------------------------------===//
3210
John McCalled433932011-01-25 03:31:58 +00003211static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003212 return type->isDependentType() ||
3213 type->isObjCObjectPointerType() ||
3214 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003215}
3216static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003217 return type->isDependentType() ||
3218 type->isPointerType() ||
3219 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003220}
3221
Chandler Carruthedc2c642011-07-02 00:01:44 +00003222static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003223 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003224 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003225 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003226 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00003227 return;
3228 }
3229
3230 bool typeOK, cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003231 if (Attr.getKind() == AttributeList::AT_ns_consumed) {
John McCalled433932011-01-25 03:31:58 +00003232 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3233 cf = false;
3234 } else {
3235 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3236 cf = true;
3237 }
3238
3239 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003240 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003241 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003242 return;
3243 }
3244
3245 if (cf)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003246 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003247 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003248 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003249}
3250
Chandler Carruthedc2c642011-07-02 00:01:44 +00003251static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3252 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003253 if (!isa<ObjCMethodDecl>(D)) {
3254 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003255 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00003256 return;
3257 }
3258
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003259 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003260}
3261
Chandler Carruthedc2c642011-07-02 00:01:44 +00003262static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3263 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003264
John McCalled433932011-01-25 03:31:58 +00003265 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003266
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003267 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003268 returnType = MD->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003269 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00003270 returnType = PD->getType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003271 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003272 (Attr.getKind() == AttributeList::AT_ns_returns_retained))
John McCall31168b02011-06-15 23:02:42 +00003273 return; // ignore: was handled as a type attribute
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003274 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003275 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003276 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003277 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003278 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003279 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003280 return;
3281 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003282
John McCalled433932011-01-25 03:31:58 +00003283 bool typeOK;
3284 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003285 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003286 default: llvm_unreachable("invalid ownership attribute");
John McCalled433932011-01-25 03:31:58 +00003287 case AttributeList::AT_ns_returns_autoreleased:
3288 case AttributeList::AT_ns_returns_retained:
3289 case AttributeList::AT_ns_returns_not_retained:
3290 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3291 cf = false;
3292 break;
3293
3294 case AttributeList::AT_cf_returns_retained:
3295 case AttributeList::AT_cf_returns_not_retained:
3296 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3297 cf = true;
3298 break;
3299 }
3300
3301 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003302 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003303 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003304 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003305 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003306
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003307 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003308 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003309 llvm_unreachable("invalid ownership attribute");
John McCalled433932011-01-25 03:31:58 +00003310 case AttributeList::AT_ns_returns_autoreleased:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003311 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCalled433932011-01-25 03:31:58 +00003312 S.Context));
3313 return;
Ted Kremenekd9c66632010-02-18 00:05:45 +00003314 case AttributeList::AT_cf_returns_not_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003315 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003316 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003317 return;
3318 case AttributeList::AT_ns_returns_not_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003319 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003320 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003321 return;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003322 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003323 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003324 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003325 return;
3326 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003327 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003328 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003329 return;
3330 };
3331}
3332
John McCallcf166702011-07-22 08:53:00 +00003333static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3334 const AttributeList &attr) {
3335 SourceLocation loc = attr.getLoc();
3336
3337 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3338
3339 if (!isa<ObjCMethodDecl>(method)) {
3340 S.Diag(method->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003341 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCallcf166702011-07-22 08:53:00 +00003342 return;
3343 }
3344
3345 // Check that the method returns a normal pointer.
3346 QualType resultType = method->getResultType();
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003347
3348 if (!resultType->isReferenceType() &&
3349 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCallcf166702011-07-22 08:53:00 +00003350 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3351 << SourceRange(loc)
3352 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3353
3354 // Drop the attribute.
3355 return;
3356 }
3357
3358 method->addAttr(
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003359 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCallcf166702011-07-22 08:53:00 +00003360}
3361
John McCall32f5fe12011-09-30 05:12:12 +00003362/// Handle cf_audited_transfer and cf_unknown_transfer.
3363static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
3364 if (!isa<FunctionDecl>(D)) {
3365 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003366 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00003367 return;
3368 }
3369
3370 bool IsAudited = (A.getKind() == AttributeList::AT_cf_audited_transfer);
3371
3372 // Check whether there's a conflicting attribute already present.
3373 Attr *Existing;
3374 if (IsAudited) {
3375 Existing = D->getAttr<CFUnknownTransferAttr>();
3376 } else {
3377 Existing = D->getAttr<CFAuditedTransferAttr>();
3378 }
3379 if (Existing) {
3380 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
3381 << A.getName()
3382 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
3383 << A.getRange() << Existing->getRange();
3384 return;
3385 }
3386
3387 // All clear; add the attribute.
3388 if (IsAudited) {
3389 D->addAttr(
3390 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
3391 } else {
3392 D->addAttr(
3393 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
3394 }
3395}
3396
John McCallf1e8b342011-09-29 07:17:38 +00003397static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3398 const AttributeList &Attr) {
3399 RecordDecl *RD = dyn_cast<RecordDecl>(D);
3400 if (!RD || RD->isUnion()) {
3401 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003402 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00003403 }
3404
3405 IdentifierInfo *ParmName = Attr.getParameterName();
3406
3407 // In Objective-C, verify that the type names an Objective-C type.
3408 // We don't want to check this outside of ObjC because people sometimes
3409 // do crazy C declarations of Objective-C types.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003410 if (ParmName && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00003411 // Check for an existing type with this name.
3412 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
3413 Sema::LookupOrdinaryName);
3414 if (S.LookupName(R, Sc)) {
3415 NamedDecl *Target = R.getFoundDecl();
3416 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3417 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3418 S.Diag(Target->getLocStart(), diag::note_declared_at);
3419 }
3420 }
3421 }
3422
3423 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
3424 ParmName));
3425}
3426
Chandler Carruthedc2c642011-07-02 00:01:44 +00003427static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3428 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003429 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003430
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003431 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003432 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003433}
3434
Chandler Carruthedc2c642011-07-02 00:01:44 +00003435static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3436 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003437 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003438 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003439 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003440 return;
3441 }
3442
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003443 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003444 QualType type = vd->getType();
3445
3446 if (!type->isDependentType() &&
3447 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003448 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003449 << type;
3450 return;
3451 }
3452
3453 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3454
3455 // If we have no lifetime yet, check the lifetime we're presumably
3456 // going to infer.
3457 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3458 lifetime = type->getObjCARCImplicitLifetime();
3459
3460 switch (lifetime) {
3461 case Qualifiers::OCL_None:
3462 assert(type->isDependentType() &&
3463 "didn't infer lifetime for non-dependent type?");
3464 break;
3465
3466 case Qualifiers::OCL_Weak: // meaningful
3467 case Qualifiers::OCL_Strong: // meaningful
3468 break;
3469
3470 case Qualifiers::OCL_ExplicitNone:
3471 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003472 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003473 << (lifetime == Qualifiers::OCL_Autoreleasing);
3474 break;
3475 }
3476
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003477 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003478 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCall31168b02011-06-15 23:02:42 +00003479}
3480
Charles Davis163855f2010-02-16 18:27:26 +00003481static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
Aaron Ballman0c84ebb2012-02-23 22:46:33 +00003482 switch (Attr.getKind()) {
3483 default:
3484 return false;
3485 case AttributeList::AT_dllimport:
3486 case AttributeList::AT_dllexport:
3487 case AttributeList::AT_uuid:
3488 case AttributeList::AT_deprecated:
3489 case AttributeList::AT_noreturn:
3490 case AttributeList::AT_nothrow:
3491 case AttributeList::AT_naked:
3492 case AttributeList::AT_noinline:
3493 return true;
3494 }
Francois Picheta83957a2010-12-19 06:50:37 +00003495}
3496
3497//===----------------------------------------------------------------------===//
3498// Microsoft specific attribute handlers.
3499//===----------------------------------------------------------------------===//
3500
Chandler Carruthedc2c642011-07-02 00:01:44 +00003501static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet0706d202011-09-17 17:15:52 +00003502 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Picheta83957a2010-12-19 06:50:37 +00003503 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003504 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Picheta83957a2010-12-19 06:50:37 +00003505 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003506
Francois Picheta83957a2010-12-19 06:50:37 +00003507 Expr *Arg = Attr.getArg(0);
3508 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003509 if (!Str || !Str->isAscii()) {
Francois Pichet7da11662010-12-20 01:41:49 +00003510 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
3511 << "uuid" << 1;
3512 return;
3513 }
3514
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003515 StringRef StrRef = Str->getString();
Francois Pichet7da11662010-12-20 01:41:49 +00003516
3517 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
3518 StrRef.back() == '}';
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003519
Francois Pichet7da11662010-12-20 01:41:49 +00003520 // Validate GUID length.
3521 if (IsCurly && StrRef.size() != 38) {
3522 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3523 return;
3524 }
3525 if (!IsCurly && StrRef.size() != 36) {
3526 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3527 return;
3528 }
3529
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003530 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichet7da11662010-12-20 01:41:49 +00003531 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003532 StringRef::iterator I = StrRef.begin();
Anders Carlsson19588aa2011-01-23 21:07:30 +00003533 if (IsCurly) // Skip the optional '{'
3534 ++I;
3535
3536 for (int i = 0; i < 36; ++i) {
Francois Pichet7da11662010-12-20 01:41:49 +00003537 if (i == 8 || i == 13 || i == 18 || i == 23) {
3538 if (*I != '-') {
3539 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3540 return;
3541 }
3542 } else if (!isxdigit(*I)) {
3543 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3544 return;
3545 }
3546 I++;
3547 }
Francois Picheta83957a2010-12-19 06:50:37 +00003548
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003549 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Picheta83957a2010-12-19 06:50:37 +00003550 Str->getString()));
Francois Pichet7da11662010-12-20 01:41:49 +00003551 } else
Francois Picheta83957a2010-12-19 06:50:37 +00003552 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davis163855f2010-02-16 18:27:26 +00003553}
3554
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003555//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003556// Top Level Sema Entry Points
3557//===----------------------------------------------------------------------===//
3558
Chandler Carruthedc2c642011-07-02 00:01:44 +00003559static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3560 const AttributeList &Attr) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00003561 switch (Attr.getKind()) {
Chandler Carruthedc2c642011-07-02 00:01:44 +00003562 case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break;
3563 case AttributeList::AT_host: handleHostAttr (S, D, Attr); break;
3564 case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00003565 default:
3566 break;
3567 }
3568}
Abramo Bagnara50099372010-04-30 13:10:51 +00003569
Chandler Carruthedc2c642011-07-02 00:01:44 +00003570static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3571 const AttributeList &Attr) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003572 switch (Attr.getKind()) {
Michael Han4a045172012-03-07 00:12:16 +00003573 case AttributeList::AT_ibaction: handleIBAction(S, D, Attr); break;
3574 case AttributeList::AT_iboutlet: handleIBOutlet(S, D, Attr); break;
3575 case AttributeList::AT_iboutletcollection:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003576 handleIBOutletCollection(S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003577 case AttributeList::AT_address_space:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003578 case AttributeList::AT_opencl_image_access:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00003579 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00003580 case AttributeList::AT_vector_size:
Bob Wilson118baf72010-11-16 00:32:24 +00003581 case AttributeList::AT_neon_vector_type:
3582 case AttributeList::AT_neon_polyvector_type:
Mike Stumpd3bb5572009-07-24 19:02:52 +00003583 // Ignore these, these are type attributes, handled by
3584 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003585 break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00003586 case AttributeList::AT_device:
3587 case AttributeList::AT_host:
3588 case AttributeList::AT_overloadable:
3589 // Ignore, this is a non-inheritable attribute, handled
3590 // by ProcessNonInheritableDeclAttr.
3591 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003592 case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break;
3593 case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003594 case AttributeList::AT_always_inline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003595 handleAlwaysInlineAttr (S, D, Attr); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00003596 case AttributeList::AT_analyzer_noreturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003597 handleAnalyzerNoReturnAttr (S, D, Attr); break;
3598 case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break;
3599 case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003600 case AttributeList::AT_carries_dependency:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003601 handleDependencyAttr (S, D, Attr); break;
3602 case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break;
3603 case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break;
3604 case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break;
3605 case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break;
3606 case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003607 case AttributeList::AT_ext_vector_type:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003608 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003609 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003610 case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break;
3611 case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break;
3612 case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break;
3613 case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003614 case AttributeList::AT_launch_bounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003615 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00003616 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003617 case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break;
3618 case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break;
3619 case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break;
3620 case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break;
3621 case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00003622 case AttributeList::AT_ownership_returns:
3623 case AttributeList::AT_ownership_takes:
3624 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003625 handleOwnershipAttr (S, D, Attr); break;
3626 case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break;
3627 case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break;
3628 case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break;
3629 case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break;
3630 case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003631
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +00003632 case AttributeList::AT_objc_ownership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003633 handleObjCOwnershipAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003634 case AttributeList::AT_objc_precise_lifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003635 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003636
John McCallcf166702011-07-22 08:53:00 +00003637 case AttributeList::AT_objc_returns_inner_pointer:
3638 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3639
John McCallf1e8b342011-09-29 07:17:38 +00003640 case AttributeList::AT_ns_bridged:
3641 handleNSBridgedAttr(S, scope, D, Attr); break;
3642
John McCall32f5fe12011-09-30 05:12:12 +00003643 case AttributeList::AT_cf_audited_transfer:
3644 case AttributeList::AT_cf_unknown_transfer:
3645 handleCFTransferAttr(S, D, Attr); break;
3646
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003647 // Checker-specific.
John McCalled433932011-01-25 03:31:58 +00003648 case AttributeList::AT_cf_consumed:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003649 case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003650 case AttributeList::AT_ns_consumes_self:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003651 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003652
3653 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenekd9c66632010-02-18 00:05:45 +00003654 case AttributeList::AT_ns_returns_not_retained:
3655 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003656 case AttributeList::AT_ns_returns_retained:
3657 case AttributeList::AT_cf_returns_retained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003658 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003659
Michael Han4a045172012-03-07 00:12:16 +00003660 case AttributeList::AT_reqd_work_group_size:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003661 handleReqdWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00003662
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003663 case AttributeList::AT_init_priority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003664 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003665
Chandler Carruthedc2c642011-07-02 00:01:44 +00003666 case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break;
Michael Han4a045172012-03-07 00:12:16 +00003667 case AttributeList::AT_ms_struct: handleMsStructAttr (S, D, Attr); break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003668 case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break;
3669 case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break;
Michael Han4a045172012-03-07 00:12:16 +00003670 case AttributeList::AT_objc_arc_weak_reference_unavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00003671 handleArcWeakrefUnavailableAttr (S, D, Attr);
3672 break;
Patrick Beardacfbe9e2012-04-06 18:12:22 +00003673 case AttributeList::AT_objc_root_class:
3674 handleObjCRootClassAttr(S, D, Attr);
3675 break;
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00003676 case AttributeList::AT_objc_requires_property_definitions:
3677 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00003678 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003679 case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break;
Rafael Espindola70107f92011-10-03 14:59:42 +00003680 case AttributeList::AT_returns_twice:
3681 handleReturnsTwiceAttr(S, D, Attr);
3682 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003683 case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break;
3684 case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break;
3685 case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00003686 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003687 case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break;
3688 case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break;
3689 case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003690 case AttributeList::AT_transparent_union:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003691 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003692 break;
Chris Lattner677a3582009-02-14 08:09:34 +00003693 case AttributeList::AT_objc_exception:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003694 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00003695 break;
John McCall86bc21f2011-03-02 11:33:24 +00003696 case AttributeList::AT_objc_method_family:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003697 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00003698 break;
Michael Han4a045172012-03-07 00:12:16 +00003699 case AttributeList::AT_NSObject: handleObjCNSObject (S, D, Attr); break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003700 case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break;
3701 case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break;
3702 case AttributeList::AT_const: handleConstAttr (S, D, Attr); break;
3703 case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break;
3704 case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break;
3705 case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break;
3706 case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break;
3707 case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003708 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00003709 // Just ignore
3710 break;
Chris Lattner3c77a352010-06-22 00:03:40 +00003711 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003712 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00003713 break;
John McCallab26cfa2010-02-05 21:31:56 +00003714 case AttributeList::AT_stdcall:
3715 case AttributeList::AT_cdecl:
3716 case AttributeList::AT_fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003717 case AttributeList::AT_thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003718 case AttributeList::AT_pascal:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003719 case AttributeList::AT_pcs:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003720 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00003721 break;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003722 case AttributeList::AT_opencl_kernel_function:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003723 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003724 break;
Francois Picheta83957a2010-12-19 06:50:37 +00003725 case AttributeList::AT_uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003726 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00003727 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00003728
3729 // Thread safety attributes:
3730 case AttributeList::AT_guarded_var:
3731 handleGuardedVarAttr(S, D, Attr);
3732 break;
3733 case AttributeList::AT_pt_guarded_var:
3734 handleGuardedVarAttr(S, D, Attr, /*pointer = */true);
3735 break;
3736 case AttributeList::AT_scoped_lockable:
3737 handleLockableAttr(S, D, Attr, /*scoped = */true);
3738 break;
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00003739 case AttributeList::AT_no_address_safety_analysis:
3740 handleNoAddressSafetyAttr(S, D, Attr);
3741 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00003742 case AttributeList::AT_no_thread_safety_analysis:
3743 handleNoThreadSafetyAttr(S, D, Attr);
3744 break;
3745 case AttributeList::AT_lockable:
3746 handleLockableAttr(S, D, Attr);
3747 break;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00003748 case AttributeList::AT_guarded_by:
3749 handleGuardedByAttr(S, D, Attr);
3750 break;
3751 case AttributeList::AT_pt_guarded_by:
3752 handleGuardedByAttr(S, D, Attr, /*pointer = */true);
3753 break;
3754 case AttributeList::AT_exclusive_lock_function:
3755 handleLockFunAttr(S, D, Attr, /*exclusive = */true);
3756 break;
3757 case AttributeList::AT_exclusive_locks_required:
3758 handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true);
3759 break;
3760 case AttributeList::AT_exclusive_trylock_function:
3761 handleTrylockFunAttr(S, D, Attr, /*exclusive = */true);
3762 break;
3763 case AttributeList::AT_lock_returned:
3764 handleLockReturnedAttr(S, D, Attr);
3765 break;
3766 case AttributeList::AT_locks_excluded:
3767 handleLocksExcludedAttr(S, D, Attr);
3768 break;
3769 case AttributeList::AT_shared_lock_function:
3770 handleLockFunAttr(S, D, Attr);
3771 break;
3772 case AttributeList::AT_shared_locks_required:
3773 handleLocksRequiredAttr(S, D, Attr);
3774 break;
3775 case AttributeList::AT_shared_trylock_function:
3776 handleTrylockFunAttr(S, D, Attr);
3777 break;
3778 case AttributeList::AT_unlock_function:
3779 handleUnlockFunAttr(S, D, Attr);
3780 break;
3781 case AttributeList::AT_acquired_before:
3782 handleAcquireOrderAttr(S, D, Attr, /*before = */true);
3783 break;
3784 case AttributeList::AT_acquired_after:
3785 handleAcquireOrderAttr(S, D, Attr, /*before = */false);
3786 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00003787
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003788 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003789 // Ask target about the attribute.
3790 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
3791 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruthdd1bc0f2010-07-08 09:42:26 +00003792 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
3793 << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003794 break;
3795 }
3796}
3797
Peter Collingbourneb331b262011-01-21 02:08:45 +00003798/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3799/// the attribute applies to decls. If the attribute is a type attribute, just
3800/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
3801/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruthedc2c642011-07-02 00:01:44 +00003802static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3803 const AttributeList &Attr,
Peter Collingbourneb331b262011-01-21 02:08:45 +00003804 bool NonInheritable, bool Inheritable) {
3805 if (Attr.isInvalid())
3806 return;
3807
3808 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
3809 // FIXME: Try to deal with other __declspec attributes!
3810 return;
3811
3812 if (NonInheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00003813 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00003814
3815 if (Inheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00003816 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00003817}
3818
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003819/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
3820/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00003821void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00003822 const AttributeList *AttrList,
3823 bool NonInheritable, bool Inheritable) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00003824 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Chandler Carruthedc2c642011-07-02 00:01:44 +00003825 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindolac18086a2010-02-23 22:00:30 +00003826 }
3827
3828 // GCC accepts
3829 // static int a9 __attribute__((weakref));
3830 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00003831 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00003832 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekd21139a2010-07-31 01:52:11 +00003833 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindolac18086a2010-02-23 22:00:30 +00003834 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003835 }
3836}
3837
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00003838// Annotation attributes are the only attributes allowed after an access
3839// specifier.
3840bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
3841 const AttributeList *AttrList) {
3842 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
3843 if (l->getKind() == AttributeList::AT_annotate) {
3844 handleAnnotateAttr(*this, ASDecl, *l);
3845 } else {
3846 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
3847 return true;
3848 }
3849 }
3850
3851 return false;
3852}
3853
John McCall42856de2011-10-01 05:17:03 +00003854/// checkUnusedDeclAttributes - Check a list of attributes to see if it
3855/// contains any decl attributes that we should warn about.
3856static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
3857 for ( ; A; A = A->getNext()) {
3858 // Only warn if the attribute is an unignored, non-type attribute.
3859 if (A->isUsedAsTypeAttr()) continue;
3860 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
3861
3862 if (A->getKind() == AttributeList::UnknownAttribute) {
3863 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
3864 << A->getName() << A->getRange();
3865 } else {
3866 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
3867 << A->getName() << A->getRange();
3868 }
3869 }
3870}
3871
3872/// checkUnusedDeclAttributes - Given a declarator which is not being
3873/// used to build a declaration, complain about any decl attributes
3874/// which might be lying around on it.
3875void Sema::checkUnusedDeclAttributes(Declarator &D) {
3876 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
3877 ::checkUnusedDeclAttributes(*this, D.getAttributes());
3878 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
3879 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
3880}
3881
Ryan Flynn7d470f32009-07-30 03:15:39 +00003882/// DeclClonePragmaWeak - clone existing decl (maybe definition),
3883/// #pragma weak needs a non-definition decl and source may not have one
Eli Friedmance3e2c82011-09-07 04:05:06 +00003884NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
3885 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00003886 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00003887 NamedDecl *NewD = 0;
3888 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00003889 FunctionDecl *NewFD;
3890 // FIXME: Missing call to CheckFunctionDeclaration().
3891 // FIXME: Mangling?
3892 // FIXME: Is the qualifier info correct?
3893 // FIXME: Is the DeclContext correct?
3894 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
3895 Loc, Loc, DeclarationName(II),
3896 FD->getType(), FD->getTypeSourceInfo(),
3897 SC_None, SC_None,
3898 false/*isInlineSpecified*/,
3899 FD->hasPrototype(),
3900 false/*isConstexprSpecified*/);
3901 NewD = NewFD;
3902
3903 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00003904 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00003905
3906 // Fake up parameter variables; they are declared as if this were
3907 // a typedef.
3908 QualType FDTy = FD->getType();
3909 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
3910 SmallVector<ParmVarDecl*, 16> Params;
3911 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
3912 AE = FT->arg_type_end(); AI != AE; ++AI) {
3913 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
3914 Param->setScopeInfo(0, Params.size());
3915 Params.push_back(Param);
3916 }
David Blaikie9c70e042011-09-21 18:16:56 +00003917 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00003918 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00003919 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
3920 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003921 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00003922 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00003923 VD->getStorageClass(),
3924 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00003925 if (VD->getQualifier()) {
3926 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00003927 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00003928 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00003929 }
3930 return NewD;
3931}
3932
3933/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
3934/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00003935void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00003936 if (W.getUsed()) return; // only do this once
3937 W.setUsed(true);
3938 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
3939 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00003940 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003941 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
3942 NDId->getName()));
3943 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00003944 WeakTopLevelDecl.push_back(NewD);
3945 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
3946 // to insert Decl at TU scope, sorry.
3947 DeclContext *SavedContext = CurContext;
3948 CurContext = Context.getTranslationUnitDecl();
3949 PushOnScopeChains(NewD, S);
3950 CurContext = SavedContext;
3951 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003952 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00003953 }
3954}
3955
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003956/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
3957/// it, apply them to D. This is a bit tricky because PD can have attributes
3958/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00003959void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
3960 bool NonInheritable, bool Inheritable) {
John McCall6fe02402010-10-27 00:59:00 +00003961 // It's valid to "forward-declare" #pragma weak, in which case we
3962 // have to do this.
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00003963 if (Inheritable) {
3964 LoadExternalWeakUndeclaredIdentifiers();
3965 if (!WeakUndeclaredIdentifiers.empty()) {
3966 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
3967 if (IdentifierInfo *Id = ND->getIdentifier()) {
3968 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
3969 = WeakUndeclaredIdentifiers.find(Id);
3970 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
3971 WeakInfo W = I->second;
3972 DeclApplyPragmaWeak(S, ND, W);
3973 WeakUndeclaredIdentifiers[Id] = W;
3974 }
John McCall6fe02402010-10-27 00:59:00 +00003975 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00003976 }
3977 }
3978 }
3979
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003980 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00003981 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00003982 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003983
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003984 // Walk the declarator structure, applying decl attributes that were in a type
3985 // position to the decl itself. This handles cases like:
3986 // int *__attr__(x)** D;
3987 // when X is a decl attribute.
3988 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
3989 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourneb331b262011-01-21 02:08:45 +00003990 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003991
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003992 // Finally, apply any attributes on the decl itself.
3993 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00003994 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003995}
John McCall28a6aea2009-11-04 02:18:39 +00003996
John McCall31168b02011-06-15 23:02:42 +00003997/// Is the given declaration allowed to use a forbidden type?
3998static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
3999 // Private ivars are always okay. Unfortunately, people don't
4000 // always properly make their ivars private, even in system headers.
4001 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004002 // Function declarations in sys headers will be marked unavailable.
4003 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4004 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004005 return false;
4006
4007 // Require it to be declared in a system header.
4008 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4009}
4010
4011/// Handle a delayed forbidden-type diagnostic.
4012static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4013 Decl *decl) {
4014 if (decl && isForbiddenTypeAllowed(S, decl)) {
4015 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4016 "this system declaration uses an unsupported type"));
4017 return;
4018 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004019 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004020 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
4021 // FIXME. we may want to supress diagnostics for all
4022 // kind of forbidden type messages on unavailable functions.
4023 if (FD->hasAttr<UnavailableAttr>() &&
4024 diag.getForbiddenTypeDiagnostic() ==
4025 diag::err_arc_array_param_no_ownership) {
4026 diag.Triggered = true;
4027 return;
4028 }
4029 }
John McCall31168b02011-06-15 23:02:42 +00004030
4031 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4032 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4033 diag.Triggered = true;
4034}
4035
John McCallc1465822011-02-14 07:13:47 +00004036// This duplicates a vector push_back but hides the need to know the
4037// size of the type.
4038void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
4039 assert(StackSize <= StackCapacity);
4040
4041 // Grow the stack if necessary.
4042 if (StackSize == StackCapacity) {
4043 unsigned newCapacity = 2 * StackCapacity + 2;
4044 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
4045 const char *oldBuffer = (const char*) Stack;
4046
4047 if (StackCapacity)
4048 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
4049
4050 delete[] oldBuffer;
4051 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
4052 StackCapacity = newCapacity;
4053 }
4054
4055 assert(StackSize < StackCapacity);
4056 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall86121512010-01-27 03:50:35 +00004057}
4058
John McCallc1465822011-02-14 07:13:47 +00004059void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
4060 Decl *decl) {
4061 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall86121512010-01-27 03:50:35 +00004062
John McCallc1465822011-02-14 07:13:47 +00004063 // Check the invariants.
4064 assert(DD.StackSize >= state.SavedStackSize);
4065 assert(state.SavedStackSize >= DD.ActiveStackBase);
4066 assert(DD.ParsingDepth > 0);
4067
4068 // Drop the parsing depth.
4069 DD.ParsingDepth--;
4070
4071 // If there are no active diagnostics, we're done.
4072 if (DD.StackSize == DD.ActiveStackBase)
John McCall86121512010-01-27 03:50:35 +00004073 return;
4074
John McCall86121512010-01-27 03:50:35 +00004075 // We only want to actually emit delayed diagnostics when we
4076 // successfully parsed a decl.
John McCall18a962b2012-01-26 20:04:03 +00004077 if (decl) {
John McCallc1465822011-02-14 07:13:47 +00004078 // We emit all the active diagnostics, not just those starting
4079 // from the saved state. The idea is this: we get one push for a
John McCall86121512010-01-27 03:50:35 +00004080 // decl spec and another for each declarator; in a decl group like:
4081 // deprecated_typedef foo, *bar, baz();
4082 // only the declarator pops will be passed decls. This is correct;
4083 // we really do need to consider delayed diagnostics from the decl spec
4084 // for each of the different declarations.
John McCallc1465822011-02-14 07:13:47 +00004085 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
4086 DelayedDiagnostic &diag = DD.Stack[i];
4087 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004088 continue;
4089
John McCallc1465822011-02-14 07:13:47 +00004090 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004091 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00004092 // Don't bother giving deprecation diagnostics if the decl is invalid.
4093 if (!decl->isInvalidDecl())
4094 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004095 break;
4096
4097 case DelayedDiagnostic::Access:
John McCallc1465822011-02-14 07:13:47 +00004098 S.HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004099 break;
John McCall31168b02011-06-15 23:02:42 +00004100
4101 case DelayedDiagnostic::ForbiddenType:
4102 handleDelayedForbiddenType(S, diag, decl);
4103 break;
John McCall86121512010-01-27 03:50:35 +00004104 }
4105 }
4106 }
4107
John McCall1064d7e2010-03-16 05:22:47 +00004108 // Destroy all the delayed diagnostics we're about to pop off.
John McCallc1465822011-02-14 07:13:47 +00004109 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
Douglas Gregor899b68f2011-03-23 15:13:44 +00004110 DD.Stack[i].Destroy();
John McCall1064d7e2010-03-16 05:22:47 +00004111
John McCallc1465822011-02-14 07:13:47 +00004112 DD.StackSize = state.SavedStackSize;
John McCall28a6aea2009-11-04 02:18:39 +00004113}
4114
4115static bool isDeclDeprecated(Decl *D) {
4116 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004117 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004118 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004119 // A category implicitly has the availability of the interface.
4120 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4121 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004122 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4123 return false;
4124}
4125
John McCallb45a1e72010-08-26 02:13:20 +00004126void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00004127 Decl *Ctx) {
4128 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00004129 return;
4130
John McCall86121512010-01-27 03:50:35 +00004131 DD.Triggered = true;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00004132 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00004133 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00004134 << DD.getDeprecationDecl()->getDeclName()
4135 << DD.getDeprecationMessage();
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004136 else if (DD.getUnknownObjCClass()) {
4137 Diag(DD.Loc, diag::warn_deprecated_fwdclass_message)
4138 << DD.getDeprecationDecl()->getDeclName();
4139 Diag(DD.getUnknownObjCClass()->getLocation(), diag::note_forward_class);
4140 }
Fariborz Jahanian551063102010-10-06 21:18:44 +00004141 else
4142 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00004143 << DD.getDeprecationDecl()->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00004144}
4145
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004146void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004147 SourceLocation Loc,
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00004148 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall28a6aea2009-11-04 02:18:39 +00004149 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004150 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004151 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4152 UnknownObjCClass,
4153 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004154 return;
4155 }
4156
4157 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00004158 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00004159 return;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00004160 if (!Message.empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00004161 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
4162 << Message;
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004163 else {
Peter Collingbourneed12ffb2011-01-02 19:53:12 +00004164 if (!UnknownObjCClass)
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004165 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00004166 else {
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004167 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00004168 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4169 }
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004170 }
John McCall28a6aea2009-11-04 02:18:39 +00004171}