blob: 3770b3c37301f56f5a3a82742175757a20db5f58 [file] [log] [blame]
Chris Lattner6b6b5372008-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 McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
John McCall384aff82010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000018#include "clang/AST/DeclTemplate.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000019#include "clang/AST/DeclObjC.h"
20#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000021#include "clang/Basic/SourceManager.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000022#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000023#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000024#include "clang/Sema/DelayedDiagnostic.h"
John McCallfe98da02011-09-29 07:17:38 +000025#include "clang/Sema/Lookup.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000026#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000027using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000028using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000029
John McCall883cc2c2011-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 Sadowskib51e0312011-08-09 17:59:31 +000032enum AttributeDeclKind {
John McCall883cc2c2011-03-02 12:29:23 +000033 ExpectedFunction,
34 ExpectedUnion,
35 ExpectedVariableOrFunction,
36 ExpectedFunctionOrMethod,
37 ExpectedParameter,
John McCall883cc2c2011-03-02 12:29:23 +000038 ExpectedFunctionMethodOrBlock,
John McCall883cc2c2011-03-02 12:29:23 +000039 ExpectedFunctionMethodOrParameter,
40 ExpectedClass,
John McCall883cc2c2011-03-02 12:29:23 +000041 ExpectedVariable,
42 ExpectedMethod,
Caitlin Sadowskidb33e142011-07-28 20:12:35 +000043 ExpectedVariableFunctionOrLabel,
Douglas Gregorf6b8b582012-03-14 16:55:17 +000044 ExpectedFieldOrGlobalVar,
45 ExpectedStruct
John McCall883cc2c2011-03-02 12:29:23 +000046};
47
Chris Lattnere5c5ee12008-06-29 00:16:31 +000048//===----------------------------------------------------------------------===//
49// Helper functions
50//===----------------------------------------------------------------------===//
51
Chandler Carruth87c44602011-07-01 23:49:12 +000052static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000053 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000054 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000055 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000056 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000057 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000058 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000059 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000060 Ty = decl->getUnderlyingType();
61 else
62 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000063
Chris Lattner6b6b5372008-06-26 18:38:35 +000064 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000065 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000066 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000067 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000068
John McCall183700f2009-09-21 23:43:11 +000069 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000070}
71
Daniel Dunbar35682492008-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 Lopesd20254f2009-12-20 23:11:08 +000075/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000076/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000077static bool isFunction(const Decl *D) {
78 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000079}
80
81/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000082/// type (function or function-typed variable) or an Objective-C
83/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +000084static bool isFunctionOrMethod(const Decl *D) {
85 return isFunction(D)|| isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +000086}
87
Fariborz Jahanian620d89c2009-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 Carruth87c44602011-07-01 23:49:12 +000091static bool isFunctionOrMethodOrBlock(const Decl *D) {
92 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000093 return true;
94 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +000095 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000096 QualType Ty = V->getType();
97 return Ty->isBlockPointerType();
98 }
Chandler Carruth87c44602011-07-01 23:49:12 +000099 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000100}
101
John McCall711c52b2011-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 Carruth87c44602011-07-01 23:49:12 +0000104static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000105 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000106 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
107 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000108}
109
Daniel Dunbard3f2c102008-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 Jahanian620d89c2009-05-15 23:15:03 +0000112/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000113static bool hasFunctionProto(const Decl *D) {
114 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000115 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000116 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000117 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-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 Carruth87c44602011-07-01 23:49:12 +0000125static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
126 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000127 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000128 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000129 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000130 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000131}
132
Chandler Carruth87c44602011-07-01 23:49:12 +0000133static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
134 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000135 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000136 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000137 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000138
Chandler Carruth87c44602011-07-01 23:49:12 +0000139 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000140}
141
Chandler Carruth87c44602011-07-01 23:49:12 +0000142static QualType getFunctionOrMethodResultType(const Decl *D) {
143 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000144 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000145 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000146}
147
Chandler Carruth87c44602011-07-01 23:49:12 +0000148static bool isFunctionOrMethodVariadic(const Decl *D) {
149 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000150 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000151 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000152 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000153 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000154 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000155 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000156 }
157}
158
Chandler Carruth87c44602011-07-01 23:49:12 +0000159static bool isInstanceMethod(const Decl *D) {
160 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000161 return MethodDecl->isInstance();
162 return false;
163}
164
Chris Lattner6b6b5372008-06-26 18:38:35 +0000165static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000166 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000167 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000168 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000169
John McCall506b57e2010-05-17 21:00:27 +0000170 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
171 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000172 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000173
John McCall506b57e2010-05-17 21:00:27 +0000174 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000175
Chris Lattner6b6b5372008-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 Dunbar085e8f72008-09-26 03:32:58 +0000181static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000182 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000183 if (!PT)
184 return false;
185
Ted Kremenek6217b802009-07-29 21:53:49 +0000186 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000187 if (!RT)
188 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000189
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000190 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000191 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000192 return false;
193
194 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
195}
196
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000197/// \brief Check if the attribute has exactly as many args as Num. May
198/// output an error.
Chandler Carruth1731e202011-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 Sadowskidb33e142011-07-28 20:12:35 +0000209
Caitlin Sadowskib51e0312011-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 Sadowskidb33e142011-07-28 20:12:35 +0000215 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
216 return false;
217 }
218
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000219 return true;
220}
221
222///
Caitlin Sadowskifdde9e72011-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 Kramer39997fc2011-08-02 04:50:49 +0000226static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000227 if (isa<FieldDecl>(D))
228 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000229 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000230 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
231
232 return false;
233}
234
Caitlin Sadowskib51e0312011-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 Sadowskifdde9e72011-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///
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000246static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
247 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000248 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000249 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000250 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000251 return true;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000252 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000253 << Attr.getName()->getName() << QT;
254 } else {
255 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
256 << Attr.getName();
257 }
258 return false;
259}
260
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000261/// \brief Checks that the passed in QualType either is of RecordType or points
262/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000263static const RecordType *getRecordType(QualType QT) {
264 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000265 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000266
267 // Now check if we point to record type.
268 if (const PointerType *PT = QT->getAs<PointerType>())
269 return PT->getPointeeType()->getAs<RecordType>();
270
271 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000272}
273
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000274/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000275/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000276static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
277 QualType Ty) {
278 const RecordType *RT = getRecordType(Ty);
279
280 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000281 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000282 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000283 << Attr.getName() << Ty.getAsString();
284 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000285 }
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000286 // Don't check for lockable if the class hasn't been defined yet.
287 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000288 return;
289 // Warn if the type is not lockable.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000290 if (!RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000291 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000292 << Attr.getName() << Ty.getAsString();
293 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000294 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000295}
296
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000297/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000298/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000299/// \param Sidx The attribute argument index to start checking with.
300/// \param ParamIdxOk Whether an argument can be indexing into a function
301/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000302static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000303 const AttributeList &Attr,
304 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000305 int Sidx = 0,
306 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000307 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000308 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000309
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000310 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000311 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000312 Args.push_back(ArgExp);
313 continue;
314 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000315
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000316 if (isa<StringLiteral>(ArgExp)) {
317 // We allow constant strings to be used as a placeholder for expressions
318 // that are not valid C++ syntax, but warn that they are ignored.
319 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
320 Attr.getName();
321 continue;
322 }
323
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000324 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000325
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000326 // First see if we can just cast to record type, or point to record type.
327 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000328
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000329 // Now check if we index into a record type function param.
330 if(!RT && ParamIdxOk) {
331 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000332 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
333 if(FD && IL) {
334 unsigned int NumParams = FD->getNumParams();
335 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000336 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
337 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
338 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000339 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
340 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000341 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000342 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000343 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000344 }
345 }
346
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000347 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000348
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000349 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000350 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000351}
352
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000353//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000354// Attribute Implementations
355//===----------------------------------------------------------------------===//
356
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000357// FIXME: All this manual attribute parsing code is gross. At the
358// least add some helper functions to check most argument patterns (#
359// and types of args).
360
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000361static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr,
362 bool pointer = false) {
363 assert(!Attr.isInvalid());
364
365 if (!checkAttributeNumArgs(S, Attr, 0))
366 return;
367
368 // D must be either a member field or global (potentially shared) variable.
369 if (!mayBeSharedVariable(D)) {
370 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000371 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000372 return;
373 }
374
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000375 if (pointer && !threadSafetyCheckIsPointer(S, D, Attr))
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000376 return;
377
378 if (pointer)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000379 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000380 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000381 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000382}
383
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000384static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000385 bool pointer = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000386 assert(!Attr.isInvalid());
387
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000388 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000389 return;
390
391 // D must be either a member field or global (potentially shared) variable.
392 if (!mayBeSharedVariable(D)) {
393 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000394 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000395 return;
396 }
397
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000398 if (pointer && !threadSafetyCheckIsPointer(S, D, Attr))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000399 return;
400
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000401 SmallVector<Expr*, 1> Args;
402 // check that all arguments are lockable objects
403 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
404 unsigned Size = Args.size();
405 if (Size != 1)
406 return;
407 Expr *Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000408
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000409 if (pointer)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000410 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000411 S.Context, Arg));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000412 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000413 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000414}
415
416
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000417static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr,
418 bool scoped = false) {
419 assert(!Attr.isInvalid());
420
421 if (!checkAttributeNumArgs(S, Attr, 0))
422 return;
423
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000424 // FIXME: Lockable structs for C code.
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000425 if (!isa<CXXRecordDecl>(D)) {
426 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
427 << Attr.getName() << ExpectedClass;
428 return;
429 }
430
431 if (scoped)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000432 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000433 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000434 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000435}
436
437static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
438 const AttributeList &Attr) {
439 assert(!Attr.isInvalid());
440
441 if (!checkAttributeNumArgs(S, Attr, 0))
442 return;
443
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000444 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000445 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
446 << Attr.getName() << ExpectedFunctionOrMethod;
447 return;
448 }
449
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000450 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000451 S.Context));
452}
453
Kostya Serebryany71efba02012-01-24 19:25:38 +0000454static void handleNoAddressSafetyAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000455 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000456 assert(!Attr.isInvalid());
457
458 if (!checkAttributeNumArgs(S, Attr, 0))
459 return;
460
461 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
462 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
463 << Attr.getName() << ExpectedFunctionOrMethod;
464 return;
465 }
466
467 D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(),
468 S.Context));
469}
470
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000471static void handleAcquireOrderAttr(Sema &S, Decl *D, const AttributeList &Attr,
472 bool before) {
473 assert(!Attr.isInvalid());
474
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000475 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000476 return;
477
478 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000479 ValueDecl *VD = dyn_cast<ValueDecl>(D);
480 if (!VD || !mayBeSharedVariable(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000481 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000482 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000483 return;
484 }
485
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000486 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000487 QualType QT = VD->getType();
488 if (!QT->isDependentType()) {
489 const RecordType *RT = getRecordType(QT);
490 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000491 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000492 << Attr.getName();
493 return;
494 }
495 }
496
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000497 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000498 // Check that all arguments are lockable objects.
499 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000500 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000501 if (Size == 0)
502 return;
503 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000504
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000505 if (before)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000506 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000507 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000508 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000509 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000510 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000511}
512
513static void handleLockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000514 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000515 assert(!Attr.isInvalid());
516
517 // zero or more arguments ok
518
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000519 // check that the attribute is applied to a function
520 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000521 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
522 << Attr.getName() << ExpectedFunctionOrMethod;
523 return;
524 }
525
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000526 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000527 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000528 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000529 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000530 Expr **StartArg = Size == 0 ? 0 : &Args[0];
531
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000532 if (exclusive)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000533 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000534 S.Context, StartArg,
535 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000536 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000537 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000538 S.Context, StartArg,
539 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000540}
541
542static void handleTrylockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000543 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000544 assert(!Attr.isInvalid());
545
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000546 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000547 return;
548
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000549 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000550 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
551 << Attr.getName() << ExpectedFunctionOrMethod;
552 return;
553 }
554
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000555 if (!isIntOrBool(Attr.getArg(0))) {
556 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
557 << Attr.getName();
558 return;
559 }
560
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000561 SmallVector<Expr*, 2> Args;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000562 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000563 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000564 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000565 Expr **StartArg = Size == 0 ? 0 : &Args[0];
566
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000567 if (exclusive)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000568 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000569 S.Context,
Caitlin Sadowski69f5d142011-09-15 17:50:19 +0000570 Attr.getArg(0),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000571 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000572 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000573 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(),
Caitlin Sadowski69f5d142011-09-15 17:50:19 +0000574 S.Context,
575 Attr.getArg(0),
576 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000577}
578
579static void handleLocksRequiredAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000580 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000581 assert(!Attr.isInvalid());
582
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000583 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000584 return;
585
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000586 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000587 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
588 << Attr.getName() << ExpectedFunctionOrMethod;
589 return;
590 }
591
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000592 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000593 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000594 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000595 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000596 if (Size == 0)
597 return;
598 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000599
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000600 if (exclusive)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000601 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000602 S.Context, StartArg,
603 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000604 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000605 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000606 S.Context, StartArg,
607 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000608}
609
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000610static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000611 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000612 assert(!Attr.isInvalid());
613
614 // zero or more arguments ok
615
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000616 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000617 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
618 << Attr.getName() << ExpectedFunctionOrMethod;
619 return;
620 }
621
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000622 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000623 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000624 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000625 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000626 Expr **StartArg = Size == 0 ? 0 : &Args[0];
627
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000628 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000629 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000630}
631
632static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000633 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000634 assert(!Attr.isInvalid());
635
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000636 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000637 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000638 Expr *Arg = Attr.getArg(0);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000639
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000640 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000641 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
642 << Attr.getName() << ExpectedFunctionOrMethod;
643 return;
644 }
645
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000646 if (Arg->isTypeDependent())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000647 return;
648
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000649 // check that the argument is lockable object
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000650 checkForLockableRecord(S, D, Attr, Arg->getType());
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000651
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000652 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context, Arg));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000653}
654
655static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000656 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000657 assert(!Attr.isInvalid());
658
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000659 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000660 return;
661
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000662 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000663 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
664 << Attr.getName() << ExpectedFunctionOrMethod;
665 return;
666 }
667
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000668 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000669 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000670 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000671 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000672 if (Size == 0)
673 return;
674 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000675
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000676 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000677 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000678}
679
680
Chandler Carruth1b03c872011-07-02 00:01:44 +0000681static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
682 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000683 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner545dd342008-06-28 23:36:30 +0000684 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000685 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000686 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000687 }
Mike Stumpbf916502009-07-24 19:02:52 +0000688
Chris Lattner6b6b5372008-06-26 18:38:35 +0000689 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000690
691 Expr *sizeExpr;
692
693 // Special case where the argument is a template id.
694 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000695 CXXScopeSpec SS;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000696 SourceLocation TemplateKWLoc;
John McCallf7a1a742009-11-24 19:00:30 +0000697 UnqualifiedId id;
698 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Douglas Gregor4ac01402011-06-15 16:02:29 +0000699
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000700 ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id,
701 false, false);
Douglas Gregor4ac01402011-06-15 16:02:29 +0000702 if (Size.isInvalid())
703 return;
704
705 sizeExpr = Size.get();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000706 } else {
707 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000708 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000709 return;
Chandler Carruth1731e202011-07-11 23:30:35 +0000710
Peter Collingbourne7a730022010-11-23 20:45:58 +0000711 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000712 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000713
714 // Instantiate/Install the vector type, and let Sema build the type for us.
715 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000716 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000717 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000718 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000719 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000720
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000721 // Remember this typedef decl, we will need it later for diagnostics.
722 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000723 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000724}
725
Chandler Carruth1b03c872011-07-02 00:01:44 +0000726static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000727 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000728 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000729 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000730
Chandler Carruth87c44602011-07-01 23:49:12 +0000731 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000732 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000733 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000734 // If the alignment is less than or equal to 8 bits, the packed attribute
735 // has no effect.
736 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000737 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000738 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000739 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000740 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000741 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000742 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000743 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000744}
745
Chandler Carruth1b03c872011-07-02 00:01:44 +0000746static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000747 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000748 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000749 else
750 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
751}
752
Chandler Carruth1b03c872011-07-02 00:01:44 +0000753static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000754 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000755 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000756 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000757
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000758 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000759 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000760 if (MD->isInstanceMethod()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000761 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000762 return;
763 }
764
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000765 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000766}
767
Ted Kremenek2f041d02011-09-29 07:02:25 +0000768static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
769 // The IBOutlet/IBOutletCollection attributes only apply to instance
770 // variables or properties of Objective-C classes. The outlet must also
771 // have an object reference type.
772 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
773 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +0000774 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +0000775 << Attr.getName() << VD->getType() << 0;
776 return false;
777 }
778 }
779 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
780 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +0000781 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +0000782 << Attr.getName() << PD->getType() << 1;
783 return false;
784 }
785 }
786 else {
787 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
788 return false;
789 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +0000790
Ted Kremenek2f041d02011-09-29 07:02:25 +0000791 return true;
792}
793
Chandler Carruth1b03c872011-07-02 00:01:44 +0000794static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000795 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000796 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000797 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +0000798
799 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000800 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000801
Ted Kremenek2f041d02011-09-29 07:02:25 +0000802 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
Ted Kremenek96329d42008-07-15 22:26:48 +0000803}
804
Chandler Carruth1b03c872011-07-02 00:01:44 +0000805static void handleIBOutletCollection(Sema &S, Decl *D,
806 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000807
808 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000809 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000810 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
811 return;
812 }
813
Ted Kremenek2f041d02011-09-29 07:02:25 +0000814 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +0000815 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +0000816
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000817 IdentifierInfo *II = Attr.getParameterName();
818 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +0000819 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000820
John McCallb3d87482010-08-24 05:47:05 +0000821 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +0000822 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000823 if (!TypeRep) {
824 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
825 return;
826 }
John McCallb3d87482010-08-24 05:47:05 +0000827 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000828 // Diagnose use of non-object type in iboutletcollection attribute.
829 // FIXME. Gnu attribute extension ignores use of builtin types in
830 // attributes. So, __attribute__((iboutletcollection(char))) will be
831 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +0000832 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000833 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
834 return;
835 }
Argyrios Kyrtzidisf1e7af32011-09-13 18:41:59 +0000836 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
837 QT, Attr.getParameterLoc()));
Ted Kremenek857e9182010-05-19 17:38:06 +0000838}
839
Chandler Carruthd309c812011-07-01 23:49:16 +0000840static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000841 if (const RecordType *UT = T->getAsUnionType())
842 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
843 RecordDecl *UD = UT->getDecl();
844 for (RecordDecl::field_iterator it = UD->field_begin(),
845 itend = UD->field_end(); it != itend; ++it) {
846 QualType QT = it->getType();
847 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
848 T = QT;
849 return;
850 }
851 }
852 }
853}
854
Chandler Carruth1b03c872011-07-02 00:01:44 +0000855static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +0000856 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
857 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +0000858 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000859 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000860 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000861 return;
862 }
Mike Stumpbf916502009-07-24 19:02:52 +0000863
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000864 // In C++ the implicit 'this' function parameter also counts, and they are
865 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +0000866 bool HasImplicitThisParam = isInstanceMethod(D);
867 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000868
869 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000870 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000871
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000872 for (AttributeList::arg_iterator I=Attr.arg_begin(),
873 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000874
875
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000876 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +0000877 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000878 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000879 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
880 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000881 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
882 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000883 return;
884 }
Mike Stumpbf916502009-07-24 19:02:52 +0000885
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000886 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000887
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000888 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000889 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000890 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000891 return;
892 }
Mike Stumpbf916502009-07-24 19:02:52 +0000893
Ted Kremenek465172f2008-07-21 22:09:15 +0000894 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000895 if (HasImplicitThisParam) {
896 if (x == 0) {
897 S.Diag(Attr.getLoc(),
898 diag::err_attribute_invalid_implicit_this_argument)
899 << "nonnull" << Ex->getSourceRange();
900 return;
901 }
902 --x;
903 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000904
905 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +0000906 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +0000907 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000908
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000909 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000910 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +0000911 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000912 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000913 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000914 }
Mike Stumpbf916502009-07-24 19:02:52 +0000915
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000916 NonNullArgs.push_back(x);
917 }
Mike Stumpbf916502009-07-24 19:02:52 +0000918
919 // If no arguments were specified to __attribute__((nonnull)) then all pointer
920 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000921 if (NonNullArgs.empty()) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000922 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
923 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +0000924 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000925 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000926 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000927 }
Mike Stumpbf916502009-07-24 19:02:52 +0000928
Ted Kremenekee1c08c2010-10-21 18:49:36 +0000929 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000930 if (NonNullArgs.empty()) {
931 // Warn the trivial case only if attribute is not coming from a
932 // macro instantiation.
933 if (Attr.getLoc().isFileID())
934 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000935 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000936 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000937 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000938
939 unsigned* start = &NonNullArgs[0];
940 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000941 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000942 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +0000943 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000944}
945
Chandler Carruth1b03c872011-07-02 00:01:44 +0000946static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000947 // This attribute must be applied to a function declaration.
948 // The first argument to the attribute must be a string,
949 // the name of the resource, for example "malloc".
950 // The following arguments must be argument indexes, the arguments must be
951 // of integer type for Returns, otherwise of pointer type.
952 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +0000953 // after being held. free() should be __attribute((ownership_takes)), whereas
954 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000955
956 if (!AL.getParameterName()) {
957 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
958 << AL.getName()->getName() << 1;
959 return;
960 }
961 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +0000962 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +0000963 switch (AL.getKind()) {
964 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +0000965 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000966 if (AL.getNumArgs() < 1) {
967 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
968 return;
969 }
Jordy Rose2a479922010-08-12 08:54:03 +0000970 break;
971 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +0000972 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000973 if (AL.getNumArgs() < 1) {
974 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
975 return;
976 }
Jordy Rose2a479922010-08-12 08:54:03 +0000977 break;
978 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +0000979 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000980 if (AL.getNumArgs() > 1) {
981 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
982 << AL.getNumArgs() + 1;
983 return;
984 }
Jordy Rose2a479922010-08-12 08:54:03 +0000985 break;
986 default:
987 // This should never happen given how we are called.
988 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000989 }
990
Chandler Carruth87c44602011-07-01 23:49:12 +0000991 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +0000992 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
993 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000994 return;
995 }
996
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000997 // In C++ the implicit 'this' function parameter also counts, and they are
998 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +0000999 bool HasImplicitThisParam = isInstanceMethod(D);
1000 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001001
Chris Lattner5f9e2722011-07-23 10:55:15 +00001002 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001003
1004 // Normalize the argument, __foo__ becomes foo.
1005 if (Module.startswith("__") && Module.endswith("__"))
1006 Module = Module.substr(2, Module.size() - 4);
1007
Chris Lattner5f9e2722011-07-23 10:55:15 +00001008 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001009
Jordy Rose2a479922010-08-12 08:54:03 +00001010 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1011 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001012
Peter Collingbourne7a730022010-11-23 20:45:58 +00001013 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001014 llvm::APSInt ArgNum(32);
1015 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1016 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1017 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1018 << AL.getName()->getName() << IdxExpr->getSourceRange();
1019 continue;
1020 }
1021
1022 unsigned x = (unsigned) ArgNum.getZExtValue();
1023
1024 if (x > NumArgs || x < 1) {
1025 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1026 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1027 continue;
1028 }
1029 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001030 if (HasImplicitThisParam) {
1031 if (x == 0) {
1032 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1033 << "ownership" << IdxExpr->getSourceRange();
1034 return;
1035 }
1036 --x;
1037 }
1038
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001039 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001040 case OwnershipAttr::Takes:
1041 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001042 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001043 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001044 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1045 // FIXME: Should also highlight argument in decl.
1046 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001047 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001048 << "pointer"
1049 << IdxExpr->getSourceRange();
1050 continue;
1051 }
1052 break;
1053 }
Sean Huntcf807c42010-08-18 23:23:40 +00001054 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001055 if (AL.getNumArgs() > 1) {
1056 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001057 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001058 llvm::APSInt ArgNum(32);
1059 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1060 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1061 S.Diag(AL.getLoc(), diag::err_ownership_type)
1062 << "ownership_returns" << "integer"
1063 << IdxExpr->getSourceRange();
1064 return;
1065 }
1066 }
1067 break;
1068 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001069 } // switch
1070
1071 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001072 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001073 i = D->specific_attr_begin<OwnershipAttr>(),
1074 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001075 i != e; ++i) {
1076 if ((*i)->getOwnKind() != K) {
1077 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1078 I!=E; ++I) {
1079 if (x == *I) {
1080 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1081 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001082 }
1083 }
1084 }
1085 }
1086 OwnershipArgs.push_back(x);
1087 }
1088
1089 unsigned* start = OwnershipArgs.data();
1090 unsigned size = OwnershipArgs.size();
1091 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001092
1093 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1094 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1095 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001096 }
Sean Huntcf807c42010-08-18 23:23:40 +00001097
Chandler Carruth87c44602011-07-01 23:49:12 +00001098 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +00001099 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001100}
1101
John McCall332bb2a2011-02-08 22:35:49 +00001102/// Whether this declaration has internal linkage for the purposes of
1103/// things that want to complain about things not have internal linkage.
1104static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1105 switch (D->getLinkage()) {
1106 case NoLinkage:
1107 case InternalLinkage:
1108 return true;
1109
1110 // Template instantiations that go from external to unique-external
1111 // shouldn't get diagnosed.
1112 case UniqueExternalLinkage:
1113 return true;
1114
1115 case ExternalLinkage:
1116 return false;
1117 }
1118 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001119}
1120
Chandler Carruth1b03c872011-07-02 00:01:44 +00001121static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001122 // Check the attribute arguments.
1123 if (Attr.getNumArgs() > 1) {
1124 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1125 return;
1126 }
1127
Chandler Carruth87c44602011-07-01 23:49:12 +00001128 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001129 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001130 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001131 return;
1132 }
1133
Chandler Carruth87c44602011-07-01 23:49:12 +00001134 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001135
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001136 // gcc rejects
1137 // class c {
1138 // static int a __attribute__((weakref ("v2")));
1139 // static int b() __attribute__((weakref ("f3")));
1140 // };
1141 // and ignores the attributes of
1142 // void f(void) {
1143 // static int a __attribute__((weakref ("v2")));
1144 // }
1145 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001146 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001147 if (!Ctx->isFileContext()) {
1148 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001149 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001150 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001151 }
1152
1153 // The GCC manual says
1154 //
1155 // At present, a declaration to which `weakref' is attached can only
1156 // be `static'.
1157 //
1158 // It also says
1159 //
1160 // Without a TARGET,
1161 // given as an argument to `weakref' or to `alias', `weakref' is
1162 // equivalent to `weak'.
1163 //
1164 // gcc 4.4.1 will accept
1165 // int a7 __attribute__((weakref));
1166 // as
1167 // int a7 __attribute__((weak));
1168 // This looks like a bug in gcc. We reject that for now. We should revisit
1169 // it if this behaviour is actually used.
1170
John McCall332bb2a2011-02-08 22:35:49 +00001171 if (!hasEffectivelyInternalLinkage(nd)) {
1172 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001173 return;
1174 }
1175
1176 // GCC rejects
1177 // static ((alias ("y"), weakref)).
1178 // Should we? How to check that weakref is before or after alias?
1179
1180 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001181 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001182 Arg = Arg->IgnoreParenCasts();
1183 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1184
Douglas Gregor5cee1192011-07-27 05:40:30 +00001185 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001186 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1187 << "weakref" << 1;
1188 return;
1189 }
1190 // GCC will accept anything as the argument of weakref. Should we
1191 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001192 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001193 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001194 }
1195
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001196 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001197}
1198
Chandler Carruth1b03c872011-07-02 00:01:44 +00001199static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001200 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001201 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001202 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001203 return;
1204 }
Mike Stumpbf916502009-07-24 19:02:52 +00001205
Peter Collingbourne7a730022010-11-23 20:45:58 +00001206 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001207 Arg = Arg->IgnoreParenCasts();
1208 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001209
Douglas Gregor5cee1192011-07-27 05:40:30 +00001210 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001211 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001212 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001213 return;
1214 }
Mike Stumpbf916502009-07-24 19:02:52 +00001215
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001216 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001217 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1218 return;
1219 }
1220
Chris Lattner6b6b5372008-06-26 18:38:35 +00001221 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001222
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001223 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001224 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001225}
1226
Chandler Carruth1b03c872011-07-02 00:01:44 +00001227static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001228 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001229 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001230 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001231
Chandler Carruth87c44602011-07-01 23:49:12 +00001232 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001233 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001234 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001235 return;
1236 }
1237
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001238 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001239}
1240
Chandler Carruth1b03c872011-07-02 00:01:44 +00001241static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1242 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001243 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001244 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001245 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1246 return;
1247 }
1248
Chandler Carruth87c44602011-07-01 23:49:12 +00001249 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001250 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001251 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001252 return;
1253 }
Mike Stumpbf916502009-07-24 19:02:52 +00001254
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001255 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001256}
1257
Chandler Carruth1b03c872011-07-02 00:01:44 +00001258static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001259 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001260 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001261 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1262 return;
1263 }
Mike Stump1eb44332009-09-09 15:08:12 +00001264
Chandler Carruth87c44602011-07-01 23:49:12 +00001265 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001266 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001267 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001268 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001269 return;
1270 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001271 }
1272
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001273 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001274}
1275
Chandler Carruth1b03c872011-07-02 00:01:44 +00001276static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001277 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001278 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001279 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001280
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001281 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +00001282}
1283
Chandler Carruth1b03c872011-07-02 00:01:44 +00001284static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001285 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001286 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001287 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001288 else
1289 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001290 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001291}
1292
Chandler Carruth1b03c872011-07-02 00:01:44 +00001293static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001294 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001295 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001296 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001297 else
1298 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001299 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001300}
1301
Chandler Carruth1b03c872011-07-02 00:01:44 +00001302static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001303 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001304
1305 if (S.CheckNoReturnAttr(attr)) return;
1306
Chandler Carruth87c44602011-07-01 23:49:12 +00001307 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001308 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001309 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001310 return;
1311 }
1312
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001313 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +00001314}
1315
1316bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001317 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001318 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1319 attr.setInvalid();
1320 return true;
1321 }
1322
1323 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001324}
1325
Chandler Carruth1b03c872011-07-02 00:01:44 +00001326static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1327 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001328
1329 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1330 // because 'analyzer_noreturn' does not impact the type.
1331
Chandler Carruth1731e202011-07-11 23:30:35 +00001332 if(!checkAttributeNumArgs(S, Attr, 0))
1333 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001334
Chandler Carruth87c44602011-07-01 23:49:12 +00001335 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1336 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001337 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1338 && !VD->getType()->isFunctionPointerType())) {
1339 S.Diag(Attr.getLoc(),
1340 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1341 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001342 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001343 return;
1344 }
1345 }
1346
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001347 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001348}
1349
John Thompson35cc9622010-08-09 21:53:52 +00001350// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001351static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001352/*
1353 Returning a Vector Class in Registers
1354
Eric Christopherf48f3672010-12-01 22:13:54 +00001355 According to the PPU ABI specifications, a class with a single member of
1356 vector type is returned in memory when used as the return value of a function.
1357 This results in inefficient code when implementing vector classes. To return
1358 the value in a single vector register, add the vecreturn attribute to the
1359 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001360
1361 Example:
1362
1363 struct Vector
1364 {
1365 __vector float xyzw;
1366 } __attribute__((vecreturn));
1367
1368 Vector Add(Vector lhs, Vector rhs)
1369 {
1370 Vector result;
1371 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1372 return result; // This will be returned in a register
1373 }
1374*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001375 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001376 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001377 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001378 return;
1379 }
1380
Chandler Carruth87c44602011-07-01 23:49:12 +00001381 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001382 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1383 return;
1384 }
1385
Chandler Carruth87c44602011-07-01 23:49:12 +00001386 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001387 int count = 0;
1388
1389 if (!isa<CXXRecordDecl>(record)) {
1390 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1391 return;
1392 }
1393
1394 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1395 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1396 return;
1397 }
1398
Eric Christopherf48f3672010-12-01 22:13:54 +00001399 for (RecordDecl::field_iterator iter = record->field_begin();
1400 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001401 if ((count == 1) || !iter->getType()->isVectorType()) {
1402 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1403 return;
1404 }
1405 count++;
1406 }
1407
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001408 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +00001409}
1410
Chandler Carruth1b03c872011-07-02 00:01:44 +00001411static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001412 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001413 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001414 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001415 return;
1416 }
1417 // FIXME: Actually store the attribute on the declaration
1418}
1419
Chandler Carruth1b03c872011-07-02 00:01:44 +00001420static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001421 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001422 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001423 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001424 return;
1425 }
Mike Stumpbf916502009-07-24 19:02:52 +00001426
Chandler Carruth87c44602011-07-01 23:49:12 +00001427 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
1428 !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001429 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001430 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001431 return;
1432 }
Mike Stumpbf916502009-07-24 19:02:52 +00001433
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001434 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +00001435}
1436
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001437static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1438 const AttributeList &Attr) {
1439 // check the attribute arguments.
1440 if (Attr.hasParameterOrArguments()) {
1441 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1442 return;
1443 }
1444
1445 if (!isa<FunctionDecl>(D)) {
1446 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1447 << Attr.getName() << ExpectedFunction;
1448 return;
1449 }
1450
1451 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1452}
1453
Chandler Carruth1b03c872011-07-02 00:01:44 +00001454static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001455 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001456 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001457 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1458 return;
1459 }
Mike Stumpbf916502009-07-24 19:02:52 +00001460
Chandler Carruth87c44602011-07-01 23:49:12 +00001461 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001462 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001463 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1464 return;
1465 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001466 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001467 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001468 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001469 return;
1470 }
Mike Stumpbf916502009-07-24 19:02:52 +00001471
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001472 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001473}
1474
Chandler Carruth1b03c872011-07-02 00:01:44 +00001475static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001476 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001477 if (Attr.getNumArgs() > 1) {
1478 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001479 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001480 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001481
1482 int priority = 65535; // FIXME: Do not hardcode such constants.
1483 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001484 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001485 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001486 if (E->isTypeDependent() || E->isValueDependent() ||
1487 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001488 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001489 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001490 return;
1491 }
1492 priority = Idx.getZExtValue();
1493 }
Mike Stumpbf916502009-07-24 19:02:52 +00001494
Chandler Carruth87c44602011-07-01 23:49:12 +00001495 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001496 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001497 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001498 return;
1499 }
1500
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001501 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001502 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001503}
1504
Chandler Carruth1b03c872011-07-02 00:01:44 +00001505static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001506 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001507 if (Attr.getNumArgs() > 1) {
1508 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001509 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001510 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001511
1512 int priority = 65535; // FIXME: Do not hardcode such constants.
1513 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001514 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001515 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001516 if (E->isTypeDependent() || E->isValueDependent() ||
1517 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001518 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001519 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001520 return;
1521 }
1522 priority = Idx.getZExtValue();
1523 }
Mike Stumpbf916502009-07-24 19:02:52 +00001524
Chandler Carruth87c44602011-07-01 23:49:12 +00001525 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001526 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001527 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001528 return;
1529 }
1530
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001531 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001532 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001533}
1534
Chandler Carruth1b03c872011-07-02 00:01:44 +00001535static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001536 unsigned NumArgs = Attr.getNumArgs();
1537 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001538 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001539 return;
1540 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001541
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001542 // Handle the case where deprecated attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001543 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001544 if (NumArgs == 1) {
1545 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001546 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001547 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1548 << "deprecated";
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001549 return;
1550 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001551 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001552 }
Mike Stumpbf916502009-07-24 19:02:52 +00001553
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001554 D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getRange(), S.Context, Str));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001555}
1556
Chandler Carruth1b03c872011-07-02 00:01:44 +00001557static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001558 unsigned NumArgs = Attr.getNumArgs();
1559 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001560 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001561 return;
1562 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001563
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001564 // Handle the case where unavailable attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001565 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001566 if (NumArgs == 1) {
1567 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001568 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001569 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001570 diag::err_attribute_not_string) << "unavailable";
1571 return;
1572 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001573 Str = SE->getString();
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001574 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001575 D->addAttr(::new (S.Context) UnavailableAttr(Attr.getRange(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001576}
1577
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001578static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1579 const AttributeList &Attr) {
1580 unsigned NumArgs = Attr.getNumArgs();
1581 if (NumArgs > 0) {
1582 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1583 return;
1584 }
1585
1586 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001587 Attr.getRange(), S.Context));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001588}
1589
Patrick Beardb2f68202012-04-06 18:12:22 +00001590static void handleObjCRootClassAttr(Sema &S, Decl *D,
1591 const AttributeList &Attr) {
1592 if (!isa<ObjCInterfaceDecl>(D)) {
1593 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1594 return;
1595 }
1596
1597 unsigned NumArgs = Attr.getNumArgs();
1598 if (NumArgs > 0) {
1599 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1600 return;
1601 }
1602
1603 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1604}
1605
Ted Kremenek71207fc2012-01-05 22:47:47 +00001606static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001607 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00001608 if (!isa<ObjCInterfaceDecl>(D)) {
1609 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1610 return;
1611 }
1612
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001613 unsigned NumArgs = Attr.getNumArgs();
1614 if (NumArgs > 0) {
1615 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1616 return;
1617 }
1618
Ted Kremenek71207fc2012-01-05 22:47:47 +00001619 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001620 Attr.getRange(), S.Context));
1621}
1622
Chandler Carruth1b03c872011-07-02 00:01:44 +00001623static void handleAvailabilityAttr(Sema &S, Decl *D,
1624 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001625 IdentifierInfo *Platform = Attr.getParameterName();
1626 SourceLocation PlatformLoc = Attr.getParameterLoc();
1627
Chris Lattner5f9e2722011-07-23 10:55:15 +00001628 StringRef PlatformName
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001629 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1630 if (PlatformName.empty()) {
1631 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1632 << Platform;
1633
1634 PlatformName = Platform->getName();
1635 }
1636
1637 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1638 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1639 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00001640 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001641
Douglas Gregorc90df6a2011-08-10 16:09:55 +00001642 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001643 // of these steps are needed).
1644 if (Introduced.isValid() && Deprecated.isValid() &&
Douglas Gregor3b6b7ac2011-08-10 15:31:35 +00001645 !(Introduced.Version <= Deprecated.Version)) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001646 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1647 << 1 << PlatformName << Deprecated.Version.getAsString()
1648 << 0 << Introduced.Version.getAsString();
1649 return;
1650 }
1651
1652 if (Introduced.isValid() && Obsoleted.isValid() &&
Douglas Gregor3b6b7ac2011-08-10 15:31:35 +00001653 !(Introduced.Version <= Obsoleted.Version)) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001654 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1655 << 2 << PlatformName << Obsoleted.Version.getAsString()
1656 << 0 << Introduced.Version.getAsString();
1657 return;
1658 }
1659
1660 if (Deprecated.isValid() && Obsoleted.isValid() &&
Douglas Gregor3b6b7ac2011-08-10 15:31:35 +00001661 !(Deprecated.Version <= Obsoleted.Version)) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001662 S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering)
1663 << 2 << PlatformName << Obsoleted.Version.getAsString()
1664 << 1 << Deprecated.Version.getAsString();
1665 return;
1666 }
1667
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00001668 StringRef Str;
1669 const StringLiteral *SE =
1670 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
1671 if (SE)
1672 Str = SE->getString();
1673
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001674 D->addAttr(::new (S.Context) AvailabilityAttr(Attr.getRange(), S.Context,
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001675 Platform,
1676 Introduced.Version,
1677 Deprecated.Version,
Douglas Gregorb53e4172011-03-26 03:35:55 +00001678 Obsoleted.Version,
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00001679 IsUnavailable,
1680 Str));
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001681}
1682
Chandler Carruth1b03c872011-07-02 00:01:44 +00001683static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001684 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001685 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001686 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001687
Peter Collingbourne7a730022010-11-23 20:45:58 +00001688 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001689 Arg = Arg->IgnoreParenCasts();
1690 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001691
Douglas Gregor5cee1192011-07-27 05:40:30 +00001692 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001693 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001694 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001695 return;
1696 }
Mike Stumpbf916502009-07-24 19:02:52 +00001697
Chris Lattner5f9e2722011-07-23 10:55:15 +00001698 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00001699 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00001700
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001701 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00001702 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001703 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00001704 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001705 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00001706 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00001707 else if (TypeStr == "protected") {
1708 // Complain about attempts to use protected visibility on targets
1709 // (like Darwin) that don't support it.
1710 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
1711 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1712 type = VisibilityAttr::Default;
1713 } else {
1714 type = VisibilityAttr::Protected;
1715 }
1716 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00001717 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001718 return;
1719 }
Mike Stumpbf916502009-07-24 19:02:52 +00001720
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001721 D->addAttr(::new (S.Context) VisibilityAttr(Attr.getRange(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001722}
1723
Chandler Carruth1b03c872011-07-02 00:01:44 +00001724static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1725 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00001726 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1727 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001728 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001729 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00001730 return;
1731 }
1732
Chandler Carruth87c44602011-07-01 23:49:12 +00001733 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
1734 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
1735 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00001736 << "objc_method_family" << 1;
1737 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001738 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00001739 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001740 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00001741 return;
1742 }
1743
Chris Lattner5f9e2722011-07-23 10:55:15 +00001744 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00001745 ObjCMethodFamilyAttr::FamilyKind family;
1746 if (param == "none")
1747 family = ObjCMethodFamilyAttr::OMF_None;
1748 else if (param == "alloc")
1749 family = ObjCMethodFamilyAttr::OMF_alloc;
1750 else if (param == "copy")
1751 family = ObjCMethodFamilyAttr::OMF_copy;
1752 else if (param == "init")
1753 family = ObjCMethodFamilyAttr::OMF_init;
1754 else if (param == "mutableCopy")
1755 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1756 else if (param == "new")
1757 family = ObjCMethodFamilyAttr::OMF_new;
1758 else {
1759 // Just warn and ignore it. This is future-proof against new
1760 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00001761 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00001762 return;
1763 }
1764
John McCallf85e1932011-06-15 23:02:42 +00001765 if (family == ObjCMethodFamilyAttr::OMF_init &&
1766 !method->getResultType()->isObjCObjectPointerType()) {
1767 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
1768 << method->getResultType();
1769 // Ignore the attribute.
1770 return;
1771 }
1772
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001773 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00001774 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00001775}
1776
Chandler Carruth1b03c872011-07-02 00:01:44 +00001777static void handleObjCExceptionAttr(Sema &S, Decl *D,
1778 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00001779 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00001780 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001781
Chris Lattner0db29ec2009-02-14 08:09:34 +00001782 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1783 if (OCI == 0) {
1784 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1785 return;
1786 }
Mike Stumpbf916502009-07-24 19:02:52 +00001787
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001788 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00001789}
1790
Chandler Carruth1b03c872011-07-02 00:01:44 +00001791static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001792 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001793 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001794 return;
1795 }
Richard Smith162e1c12011-04-15 14:24:37 +00001796 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001797 QualType T = TD->getUnderlyingType();
1798 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001799 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001800 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1801 return;
1802 }
1803 }
Ted Kremenekf6e88d72012-03-01 01:40:32 +00001804 else if (!isa<ObjCPropertyDecl>(D)) {
1805 // It is okay to include this attribute on properties, e.g.:
1806 //
1807 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
1808 //
1809 // In this case it follows tradition and suppresses an error in the above
1810 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00001811 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00001812 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001813 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001814}
1815
Mike Stumpbf916502009-07-24 19:02:52 +00001816static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00001817handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00001818 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001819 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001820 return;
1821 }
1822
1823 if (!isa<FunctionDecl>(D)) {
1824 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1825 return;
1826 }
1827
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001828 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001829}
1830
Chandler Carruth1b03c872011-07-02 00:01:44 +00001831static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001832 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001833 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001834 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001835 return;
1836 }
Mike Stumpbf916502009-07-24 19:02:52 +00001837
Steve Naroff9eae5762008-09-18 16:44:58 +00001838 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001839 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001840 return;
1841 }
Mike Stumpbf916502009-07-24 19:02:52 +00001842
Sean Huntcf807c42010-08-18 23:23:40 +00001843 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001844 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001845 type = BlocksAttr::ByRef;
1846 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001847 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001848 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001849 return;
1850 }
Mike Stumpbf916502009-07-24 19:02:52 +00001851
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001852 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001853}
1854
Chandler Carruth1b03c872011-07-02 00:01:44 +00001855static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00001856 // check the attribute arguments.
1857 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00001858 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00001859 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001860 }
1861
John McCall3323fad2011-09-09 07:56:05 +00001862 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001863 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001864 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00001865 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001866 if (E->isTypeDependent() || E->isValueDependent() ||
1867 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001868 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001869 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001870 return;
1871 }
Mike Stumpbf916502009-07-24 19:02:52 +00001872
John McCall3323fad2011-09-09 07:56:05 +00001873 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001874 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1875 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001876 return;
1877 }
John McCall3323fad2011-09-09 07:56:05 +00001878
1879 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00001880 }
1881
John McCall3323fad2011-09-09 07:56:05 +00001882 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001883 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001884 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00001885 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001886 if (E->isTypeDependent() || E->isValueDependent() ||
1887 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001888 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001889 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001890 return;
1891 }
1892 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001893
John McCall3323fad2011-09-09 07:56:05 +00001894 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00001895 // FIXME: This error message could be improved, it would be nice
1896 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001897 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1898 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001899 return;
1900 }
1901 }
1902
Chandler Carruth87c44602011-07-01 23:49:12 +00001903 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00001904 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001905 if (isa<FunctionNoProtoType>(FT)) {
1906 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1907 return;
1908 }
Mike Stumpbf916502009-07-24 19:02:52 +00001909
Chris Lattner897cd902009-03-17 23:03:47 +00001910 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001911 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001912 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001913 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001914 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00001915 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001916 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001917 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001918 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00001919 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
1920 if (!BD->isVariadic()) {
1921 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
1922 return;
1923 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001924 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001925 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001926 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001927 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00001928 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001929 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001930 int m = Ty->isFunctionPointerType() ? 0 : 1;
1931 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001932 return;
1933 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001934 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001935 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001936 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001937 return;
1938 }
Anders Carlsson77091822008-10-05 18:05:59 +00001939 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001940 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001941 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00001942 return;
1943 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001944 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00001945 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001946}
1947
Chandler Carruth1b03c872011-07-02 00:01:44 +00001948static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00001949 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001950 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00001951 return;
Chris Lattner026dc962009-02-14 07:37:35 +00001952
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001953 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001954 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001955 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00001956 return;
1957 }
Mike Stumpbf916502009-07-24 19:02:52 +00001958
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001959 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1960 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1961 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001962 return;
1963 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001964 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1965 if (MD->getResultType()->isVoidType()) {
1966 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1967 << Attr.getName() << 1;
1968 return;
1969 }
1970
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001971 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001972}
1973
Chandler Carruth1b03c872011-07-02 00:01:44 +00001974static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001975 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00001976 if (Attr.hasParameterOrArguments()) {
1977 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001978 return;
1979 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001980
Chandler Carruth87c44602011-07-01 23:49:12 +00001981 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00001982 if (isa<CXXRecordDecl>(D)) {
1983 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
1984 return;
1985 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001986 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1987 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001988 return;
1989 }
1990
Chandler Carruth87c44602011-07-01 23:49:12 +00001991 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001992
1993 // 'weak' only applies to declarations with external linkage.
1994 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001995 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001996 return;
1997 }
Mike Stumpbf916502009-07-24 19:02:52 +00001998
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001999 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002000}
2001
Chandler Carruth1b03c872011-07-02 00:01:44 +00002002static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002003 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002004 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002005 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002006
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002007
2008 // weak_import only applies to variable & function declarations.
2009 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002010 if (!D->canBeWeakImported(isDef)) {
2011 if (isDef)
2012 S.Diag(Attr.getLoc(),
2013 diag::warn_attribute_weak_import_invalid_on_definition)
2014 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00002015 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002016 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002017 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002018 // Nothing to warn about here.
2019 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002020 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002021 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002022
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002023 return;
2024 }
2025
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002026 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002027}
2028
Chandler Carruth1b03c872011-07-02 00:01:44 +00002029static void handleReqdWorkGroupSize(Sema &S, Decl *D,
2030 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002031 // Attribute has 3 arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002032 if (!checkAttributeNumArgs(S, Attr, 3))
Nate Begeman6f3d8382009-06-26 06:32:41 +00002033 return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002034
2035 unsigned WGSize[3];
2036 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002037 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002038 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002039 if (E->isTypeDependent() || E->isValueDependent() ||
2040 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002041 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2042 << "reqd_work_group_size" << E->getSourceRange();
2043 return;
2044 }
2045 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2046 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002047 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Sean Huntcf807c42010-08-18 23:23:40 +00002048 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00002049 WGSize[2]));
2050}
2051
Chandler Carruth1b03c872011-07-02 00:01:44 +00002052static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002053 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002054 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002055 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002056
2057 // Make sure that there is a string literal as the sections's single
2058 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002059 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002060 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002061 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002062 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002063 return;
2064 }
Mike Stump1eb44332009-09-09 15:08:12 +00002065
Chris Lattner797c3c42009-08-10 19:03:04 +00002066 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002067 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002068 if (!Error.empty()) {
2069 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2070 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002071 return;
2072 }
Mike Stump1eb44332009-09-09 15:08:12 +00002073
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002074 // This attribute cannot be applied to local variables.
2075 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2076 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2077 return;
2078 }
2079
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002080 D->addAttr(::new (S.Context) SectionAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002081 SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002082}
2083
Chris Lattner6b6b5372008-06-26 18:38:35 +00002084
Chandler Carruth1b03c872011-07-02 00:01:44 +00002085static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002086 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002087 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002088 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002089 return;
2090 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002091
Chandler Carruth87c44602011-07-01 23:49:12 +00002092 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002093 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002094 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002095 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002096 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002097 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002098}
2099
Chandler Carruth1b03c872011-07-02 00:01:44 +00002100static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002101 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002102 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002103 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002104 return;
2105 }
Mike Stumpbf916502009-07-24 19:02:52 +00002106
Chandler Carruth87c44602011-07-01 23:49:12 +00002107 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002108 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002109 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002110 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002111 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002112 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002113}
2114
Chandler Carruth1b03c872011-07-02 00:01:44 +00002115static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002116 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002117 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002118 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002119
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002120 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002121}
2122
Chandler Carruth1b03c872011-07-02 00:01:44 +00002123static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002124 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002125 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2126 return;
2127 }
Mike Stumpbf916502009-07-24 19:02:52 +00002128
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002129 if (Attr.getNumArgs() != 0) {
2130 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2131 return;
2132 }
Mike Stumpbf916502009-07-24 19:02:52 +00002133
Chandler Carruth87c44602011-07-01 23:49:12 +00002134 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002135
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002136 if (!VD || !VD->hasLocalStorage()) {
2137 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2138 return;
2139 }
Mike Stumpbf916502009-07-24 19:02:52 +00002140
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002141 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002142 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002143 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002144 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2145 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002146 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002147 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002148 Attr.getParameterName();
2149 return;
2150 }
Mike Stumpbf916502009-07-24 19:02:52 +00002151
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002152 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2153 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002154 S.Diag(Attr.getParameterLoc(),
2155 diag::err_attribute_cleanup_arg_not_function)
2156 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002157 return;
2158 }
2159
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002160 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002161 S.Diag(Attr.getParameterLoc(),
2162 diag::err_attribute_cleanup_func_must_take_one_arg)
2163 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002164 return;
2165 }
Mike Stumpbf916502009-07-24 19:02:52 +00002166
Anders Carlsson89941c12009-02-07 23:16:50 +00002167 // We're currently more strict than GCC about what function types we accept.
2168 // If this ever proves to be a problem it should be easy to fix.
2169 QualType Ty = S.Context.getPointerType(VD->getType());
2170 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002171 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2172 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002173 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002174 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2175 Attr.getParameterName() << ParamTy << Ty;
2176 return;
2177 }
Mike Stumpbf916502009-07-24 19:02:52 +00002178
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002179 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002180 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002181}
2182
Mike Stumpbf916502009-07-24 19:02:52 +00002183/// Handle __attribute__((format_arg((idx)))) attribute based on
2184/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002185static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002186 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002187 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002188
Chandler Carruth87c44602011-07-01 23:49:12 +00002189 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002190 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002191 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002192 return;
2193 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002194
2195 // In C++ the implicit 'this' function parameter also counts, and they are
2196 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002197 bool HasImplicitThisParam = isInstanceMethod(D);
2198 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002199 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002200
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002201 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002202 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002203 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002204 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2205 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002206 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2207 << "format" << 2 << IdxExpr->getSourceRange();
2208 return;
2209 }
Mike Stumpbf916502009-07-24 19:02:52 +00002210
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002211 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2212 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2213 << "format" << 2 << IdxExpr->getSourceRange();
2214 return;
2215 }
Mike Stumpbf916502009-07-24 19:02:52 +00002216
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002217 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002218
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002219 if (HasImplicitThisParam) {
2220 if (ArgIdx == 0) {
2221 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2222 << "format_arg" << IdxExpr->getSourceRange();
2223 return;
2224 }
2225 ArgIdx--;
2226 }
2227
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002228 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002229 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002230
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002231 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2232 if (not_nsstring_type &&
2233 !isCFStringType(Ty, S.Context) &&
2234 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002235 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002236 // FIXME: Should highlight the actual expression that has the wrong type.
2237 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002238 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002239 << IdxExpr->getSourceRange();
2240 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002241 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002242 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002243 if (!isNSStringType(Ty, S.Context) &&
2244 !isCFStringType(Ty, S.Context) &&
2245 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002246 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002247 // FIXME: Should highlight the actual expression that has the wrong type.
2248 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002249 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002250 << IdxExpr->getSourceRange();
2251 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002252 }
2253
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002254 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002255 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002256}
2257
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002258enum FormatAttrKind {
2259 CFStringFormat,
2260 NSStringFormat,
2261 StrftimeFormat,
2262 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002263 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002264 InvalidFormat
2265};
2266
2267/// getFormatAttrKind - Map from format attribute names to supported format
2268/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002269static FormatAttrKind getFormatAttrKind(StringRef Format) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002270 // Check for formats that get handled specially.
2271 if (Format == "NSString")
2272 return NSStringFormat;
2273 if (Format == "CFString")
2274 return CFStringFormat;
2275 if (Format == "strftime")
2276 return StrftimeFormat;
2277
2278 // Otherwise, check for supported formats.
2279 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
Jean-Daniel Dupas69d53842012-01-27 09:14:17 +00002280 Format == "strfmon" || Format == "cmn_err" || Format == "vcmn_err" ||
Chris Lattnercd5b3062011-02-18 17:05:55 +00002281 Format == "zcmn_err" ||
2282 Format == "kprintf") // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002283 return SupportedFormat;
2284
Duncan Sandsbc525952010-03-23 14:44:19 +00002285 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
2286 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00002287 return IgnoredFormat;
2288
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002289 return InvalidFormat;
2290}
2291
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002292/// Handle __attribute__((init_priority(priority))) attributes based on
2293/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002294static void handleInitPriorityAttr(Sema &S, Decl *D,
2295 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002296 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002297 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2298 return;
2299 }
2300
Chandler Carruth87c44602011-07-01 23:49:12 +00002301 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002302 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2303 Attr.setInvalid();
2304 return;
2305 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002306 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002307 if (S.Context.getAsArrayType(T))
2308 T = S.Context.getBaseElementType(T);
2309 if (!T->getAs<RecordType>()) {
2310 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2311 Attr.setInvalid();
2312 return;
2313 }
2314
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002315 if (Attr.getNumArgs() != 1) {
2316 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2317 Attr.setInvalid();
2318 return;
2319 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002320 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002321
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002322 llvm::APSInt priority(32);
2323 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2324 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2325 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2326 << "init_priority" << priorityExpr->getSourceRange();
2327 Attr.setInvalid();
2328 return;
2329 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002330 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002331 if (prioritynum < 101 || prioritynum > 65535) {
2332 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2333 << priorityExpr->getSourceRange();
2334 Attr.setInvalid();
2335 return;
2336 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002337 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002338 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002339}
2340
Mike Stumpbf916502009-07-24 19:02:52 +00002341/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2342/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002343static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002344
Chris Lattner545dd342008-06-28 23:36:30 +00002345 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002346 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002347 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002348 return;
2349 }
2350
Chris Lattner545dd342008-06-28 23:36:30 +00002351 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002352 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002353 return;
2354 }
2355
Chandler Carruth87c44602011-07-01 23:49:12 +00002356 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002357 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002358 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002359 return;
2360 }
2361
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002362 // In C++ the implicit 'this' function parameter also counts, and they are
2363 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002364 bool HasImplicitThisParam = isInstanceMethod(D);
2365 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002366 unsigned FirstIdx = 1;
2367
Chris Lattner5f9e2722011-07-23 10:55:15 +00002368 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002369
2370 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002371 if (Format.startswith("__") && Format.endswith("__"))
2372 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002373
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002374 // Check for supported formats.
2375 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00002376
2377 if (Kind == IgnoredFormat)
2378 return;
2379
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002380 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002381 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002382 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002383 return;
2384 }
2385
2386 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002387 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00002388 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002389 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2390 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002391 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002392 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002393 return;
2394 }
2395
2396 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002397 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002398 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002399 return;
2400 }
2401
2402 // FIXME: Do we need to bounds check?
2403 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002404
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002405 if (HasImplicitThisParam) {
2406 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002407 S.Diag(Attr.getLoc(),
2408 diag::err_format_attribute_implicit_this_format_string)
2409 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002410 return;
2411 }
2412 ArgIdx--;
2413 }
Mike Stump1eb44332009-09-09 15:08:12 +00002414
Chris Lattner6b6b5372008-06-26 18:38:35 +00002415 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002416 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002417
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002418 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002419 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002420 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2421 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002422 return;
2423 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002424 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002425 // FIXME: do we need to check if the type is NSString*? What are the
2426 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00002427 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002428 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002429 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2430 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002431 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002432 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002433 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002434 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002435 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002436 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2437 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002438 return;
2439 }
2440
2441 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002442 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00002443 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002444 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2445 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002446 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002447 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002448 return;
2449 }
2450
2451 // check if the function is variadic if the 3rd argument non-zero
2452 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002453 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002454 ++NumArgs; // +1 for ...
2455 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002456 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002457 return;
2458 }
2459 }
2460
Chris Lattner3c73c412008-11-19 08:23:25 +00002461 // strftime requires FirstArg to be 0 because it doesn't read from any
2462 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002463 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002464 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002465 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2466 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002467 return;
2468 }
2469 // if 0 it disables parameter checking (to use with e.g. va_list)
2470 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002471 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002472 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002473 return;
2474 }
2475
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002476 // Check whether we already have an equivalent format attribute.
2477 for (specific_attr_iterator<FormatAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00002478 i = D->specific_attr_begin<FormatAttr>(),
2479 e = D->specific_attr_end<FormatAttr>();
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002480 i != e ; ++i) {
2481 FormatAttr *f = *i;
2482 if (f->getType() == Format &&
2483 f->getFormatIdx() == (int)Idx.getZExtValue() &&
2484 f->getFirstArg() == (int)FirstArg.getZExtValue()) {
2485 // If we don't have a valid location for this attribute, adopt the
2486 // location.
2487 if (f->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002488 f->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002489 return;
2490 }
2491 }
2492
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002493 D->addAttr(::new (S.Context) FormatAttr(Attr.getRange(), S.Context, Format,
Sean Huntcf807c42010-08-18 23:23:40 +00002494 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002495 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002496}
2497
Chandler Carruth1b03c872011-07-02 00:01:44 +00002498static void handleTransparentUnionAttr(Sema &S, Decl *D,
2499 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002500 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002501 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002502 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002503
Chris Lattner6b6b5372008-06-26 18:38:35 +00002504
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002505 // Try to find the underlying union declaration.
2506 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00002507 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002508 if (TD && TD->getUnderlyingType()->isUnionType())
2509 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2510 else
Chandler Carruth87c44602011-07-01 23:49:12 +00002511 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002512
2513 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002514 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002515 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002516 return;
2517 }
2518
John McCall5e1cdac2011-10-07 06:10:15 +00002519 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002520 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002521 diag::warn_transparent_union_attribute_not_definition);
2522 return;
2523 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002524
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002525 RecordDecl::field_iterator Field = RD->field_begin(),
2526 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002527 if (Field == FieldEnd) {
2528 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2529 return;
2530 }
Eli Friedmanbc887452008-09-02 05:19:23 +00002531
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002532 FieldDecl *FirstField = *Field;
2533 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00002534 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002535 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00002536 diag::warn_transparent_union_attribute_floating)
2537 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002538 return;
2539 }
2540
2541 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2542 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2543 for (; Field != FieldEnd; ++Field) {
2544 QualType FieldType = Field->getType();
2545 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2546 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2547 // Warn if we drop the attribute.
2548 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00002549 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002550 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00002551 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002552 diag::warn_transparent_union_attribute_field_size_align)
2553 << isSize << Field->getDeclName() << FieldBits;
2554 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00002555 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002556 diag::note_transparent_union_first_field_size_align)
2557 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00002558 return;
2559 }
2560 }
2561
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002562 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002563}
2564
Chandler Carruth1b03c872011-07-02 00:01:44 +00002565static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002566 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002567 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002568 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002569
Peter Collingbourne7a730022010-11-23 20:45:58 +00002570 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002571 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00002572
Chris Lattner6b6b5372008-06-26 18:38:35 +00002573 // Make sure that there is a string literal as the annotation's single
2574 // argument.
2575 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002576 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00002577 return;
2578 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00002579
2580 // Don't duplicate annotations that are already set.
2581 for (specific_attr_iterator<AnnotateAttr>
2582 i = D->specific_attr_begin<AnnotateAttr>(),
2583 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
2584 if ((*i)->getAnnotation() == SE->getString())
2585 return;
2586 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002587 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002588 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002589}
2590
Chandler Carruth1b03c872011-07-02 00:01:44 +00002591static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002592 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00002593 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002594 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002595 return;
2596 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002597
2598 //FIXME: The C++0x version of this attribute has more limited applicabilty
2599 // than GNU's, and should error out when it is used to specify a
2600 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00002601
Chris Lattner545dd342008-06-28 23:36:30 +00002602 if (Attr.getNumArgs() == 0) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002603 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002604 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002605 }
Mike Stumpbf916502009-07-24 19:02:52 +00002606
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002607 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002608}
2609
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002610void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E) {
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002611 // FIXME: Handle pack-expansions here.
2612 if (DiagnoseUnexpandedParameterPack(E))
2613 return;
2614
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002615 if (E->isTypeDependent() || E->isValueDependent()) {
2616 // Save dependent expressions in the AST to be instantiated.
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002617 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002618 return;
2619 }
2620
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002621 SourceLocation AttrLoc = AttrRange.getBegin();
Sean Huntcf807c42010-08-18 23:23:40 +00002622 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00002623 llvm::APSInt Alignment(32);
Richard Smith282e7e62012-02-04 09:53:13 +00002624 ExprResult ICE =
2625 VerifyIntegerConstantExpression(E, &Alignment,
2626 PDiag(diag::err_attribute_argument_not_int) << "aligned",
2627 /*AllowFold*/ false);
2628 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00002629 return;
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002630 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002631 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2632 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002633 return;
2634 }
2635
Richard Smith282e7e62012-02-04 09:53:13 +00002636 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take()));
Sean Huntcf807c42010-08-18 23:23:40 +00002637}
2638
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002639void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS) {
Sean Huntcf807c42010-08-18 23:23:40 +00002640 // FIXME: Cache the number on the Attr object if non-dependent?
2641 // FIXME: Perform checking of type validity
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002642 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS));
Sean Huntcf807c42010-08-18 23:23:40 +00002643 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002644}
Chris Lattnerfbf13472008-06-27 22:18:37 +00002645
Chandler Carruthd309c812011-07-01 23:49:16 +00002646/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00002647/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00002648///
Mike Stumpbf916502009-07-24 19:02:52 +00002649/// Despite what would be logical, the mode attribute is a decl attribute, not a
2650/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2651/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00002652static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002653 // This attribute isn't documented, but glibc uses it. It changes
2654 // the width of an int or unsigned int to the specified size.
2655
2656 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00002657 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002658 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002659
Chris Lattnerfbf13472008-06-27 22:18:37 +00002660
2661 IdentifierInfo *Name = Attr.getParameterName();
2662 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002663 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002664 return;
2665 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00002666
Chris Lattner5f9e2722011-07-23 10:55:15 +00002667 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002668
2669 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002670 if (Str.startswith("__") && Str.endswith("__"))
2671 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002672
2673 unsigned DestWidth = 0;
2674 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00002675 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00002676 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002677 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00002678 switch (Str[0]) {
2679 case 'Q': DestWidth = 8; break;
2680 case 'H': DestWidth = 16; break;
2681 case 'S': DestWidth = 32; break;
2682 case 'D': DestWidth = 64; break;
2683 case 'X': DestWidth = 96; break;
2684 case 'T': DestWidth = 128; break;
2685 }
2686 if (Str[1] == 'F') {
2687 IntegerMode = false;
2688 } else if (Str[1] == 'C') {
2689 IntegerMode = false;
2690 ComplexMode = true;
2691 } else if (Str[1] != 'I') {
2692 DestWidth = 0;
2693 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002694 break;
2695 case 4:
2696 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2697 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002698 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002699 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00002700 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002701 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002702 break;
2703 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00002704 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002705 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002706 break;
2707 }
2708
2709 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00002710 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002711 OldTy = TD->getUnderlyingType();
2712 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2713 OldTy = VD->getType();
2714 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002715 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002716 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002717 return;
2718 }
Eli Friedman73397492009-03-03 06:41:03 +00002719
John McCall183700f2009-09-21 23:43:11 +00002720 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00002721 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2722 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002723 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00002724 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2725 } else if (ComplexMode) {
2726 if (!OldTy->isComplexType())
2727 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2728 } else {
2729 if (!OldTy->isFloatingType())
2730 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2731 }
2732
Mike Stump390b4cc2009-05-16 07:39:55 +00002733 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2734 // and friends, at least with glibc.
2735 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2736 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002737 // FIXME: Make sure floating-point mappings are accurate
2738 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00002739 QualType NewTy;
2740 switch (DestWidth) {
2741 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00002742 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002743 return;
2744 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00002745 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002746 return;
2747 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00002748 if (!IntegerMode) {
2749 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2750 return;
2751 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002752 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002753 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002754 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002755 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002756 break;
2757 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00002758 if (!IntegerMode) {
2759 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2760 return;
2761 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002762 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002763 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002764 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002765 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002766 break;
2767 case 32:
2768 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002769 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002770 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002771 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002772 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002773 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002774 break;
2775 case 64:
2776 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002777 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002778 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002779 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002780 NewTy = S.Context.LongTy;
2781 else
2782 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002783 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002784 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002785 NewTy = S.Context.UnsignedLongTy;
2786 else
2787 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002788 break;
Eli Friedman73397492009-03-03 06:41:03 +00002789 case 96:
2790 NewTy = S.Context.LongDoubleTy;
2791 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002792 case 128:
2793 if (!IntegerMode) {
2794 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2795 return;
2796 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00002797 if (OldTy->isSignedIntegerType())
2798 NewTy = S.Context.Int128Ty;
2799 else
2800 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00002801 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002802 }
2803
Eli Friedman73397492009-03-03 06:41:03 +00002804 if (ComplexMode) {
2805 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002806 }
2807
2808 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00002809 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00002810 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00002811 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00002812 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00002813 cast<ValueDecl>(D)->setType(NewTy);
2814}
Chris Lattner0744e5f2008-06-29 00:23:49 +00002815
Chandler Carruth1b03c872011-07-02 00:01:44 +00002816static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00002817 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002818 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00002819 return;
Anders Carlssone896d982009-02-13 08:11:52 +00002820
Chandler Carruth87c44602011-07-01 23:49:12 +00002821 if (!isFunctionOrMethod(D)) {
Anders Carlssond87df372009-02-13 06:46:13 +00002822 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002823 << Attr.getName() << ExpectedFunction;
Anders Carlssond87df372009-02-13 06:46:13 +00002824 return;
2825 }
Mike Stumpbf916502009-07-24 19:02:52 +00002826
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002827 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00002828}
2829
Chandler Carruth1b03c872011-07-02 00:01:44 +00002830static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002831 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002832 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00002833 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002834
Mike Stumpbf916502009-07-24 19:02:52 +00002835
Chandler Carruth87c44602011-07-01 23:49:12 +00002836 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002837 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002838 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00002839 return;
2840 }
Mike Stumpbf916502009-07-24 19:02:52 +00002841
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002842 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00002843}
2844
Chandler Carruth1b03c872011-07-02 00:01:44 +00002845static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
2846 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002847 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002848 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00002849 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002850
Chris Lattner7255a2d2010-06-22 00:03:40 +00002851
Chandler Carruth87c44602011-07-01 23:49:12 +00002852 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002853 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002854 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002855 return;
2856 }
2857
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002858 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002859 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002860}
2861
Chandler Carruth1b03c872011-07-02 00:01:44 +00002862static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002863 if (S.LangOpts.CUDA) {
2864 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002865 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002866 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2867 return;
2868 }
2869
Chandler Carruth87c44602011-07-01 23:49:12 +00002870 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002871 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002872 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002873 return;
2874 }
2875
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002876 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002877 } else {
2878 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2879 }
2880}
2881
Chandler Carruth1b03c872011-07-02 00:01:44 +00002882static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002883 if (S.LangOpts.CUDA) {
2884 // check the attribute arguments.
2885 if (Attr.getNumArgs() != 0) {
2886 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2887 return;
2888 }
2889
Chandler Carruth87c44602011-07-01 23:49:12 +00002890 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002891 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002892 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002893 return;
2894 }
2895
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002896 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002897 } else {
2898 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2899 }
2900}
2901
Chandler Carruth1b03c872011-07-02 00:01:44 +00002902static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002903 if (S.LangOpts.CUDA) {
2904 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002905 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002906 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00002907
Chandler Carruth87c44602011-07-01 23:49:12 +00002908 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002909 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002910 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002911 return;
2912 }
2913
Chandler Carruth87c44602011-07-01 23:49:12 +00002914 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002915 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00002916 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002917 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2918 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2919 << FD->getType()
2920 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2921 "void");
2922 } else {
2923 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2924 << FD->getType();
2925 }
2926 return;
2927 }
2928
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002929 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002930 } else {
2931 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2932 }
2933}
2934
Chandler Carruth1b03c872011-07-02 00:01:44 +00002935static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002936 if (S.LangOpts.CUDA) {
2937 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002938 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002939 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002940
Peter Collingbourneced76712010-12-01 03:15:31 +00002941
Chandler Carruth87c44602011-07-01 23:49:12 +00002942 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002943 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002944 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002945 return;
2946 }
2947
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002948 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002949 } else {
2950 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2951 }
2952}
2953
Chandler Carruth1b03c872011-07-02 00:01:44 +00002954static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002955 if (S.LangOpts.CUDA) {
2956 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002957 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002958 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002959
Peter Collingbourneced76712010-12-01 03:15:31 +00002960
Chandler Carruth87c44602011-07-01 23:49:12 +00002961 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002962 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002963 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002964 return;
2965 }
2966
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002967 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002968 } else {
2969 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2970 }
2971}
2972
Chandler Carruth1b03c872011-07-02 00:01:44 +00002973static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00002974 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002975 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00002976 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002977
Chandler Carruth87c44602011-07-01 23:49:12 +00002978 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00002979 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00002980 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002981 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00002982 return;
2983 }
Mike Stumpbf916502009-07-24 19:02:52 +00002984
Douglas Gregor0130f3c2009-10-27 21:01:01 +00002985 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00002986 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00002987 return;
2988 }
Mike Stumpbf916502009-07-24 19:02:52 +00002989
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002990 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00002991}
2992
Chandler Carruth1b03c872011-07-02 00:01:44 +00002993static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002994 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002995
Chandler Carruth87c44602011-07-01 23:49:12 +00002996 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00002997 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2998 CallingConv CC;
Chandler Carruth87c44602011-07-01 23:49:12 +00002999 if (S.CheckCallingConvAttr(Attr, CC))
John McCall711c52b2011-01-05 12:14:39 +00003000 return;
3001
Chandler Carruth87c44602011-07-01 23:49:12 +00003002 if (!isa<ObjCMethodDecl>(D)) {
3003 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3004 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003005 return;
3006 }
3007
Chandler Carruth87c44602011-07-01 23:49:12 +00003008 switch (Attr.getKind()) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00003009 case AttributeList::AT_fastcall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003010 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003011 return;
3012 case AttributeList::AT_stdcall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003013 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003014 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003015 case AttributeList::AT_thiscall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003016 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003017 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003018 case AttributeList::AT_cdecl:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003019 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003020 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003021 case AttributeList::AT_pascal:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003022 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003023 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003024 case AttributeList::AT_pcs: {
Chandler Carruth87c44602011-07-01 23:49:12 +00003025 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003026 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003027 if (!Str || !Str->isAscii()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003028 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003029 << "pcs" << 1;
Chandler Carruth87c44602011-07-01 23:49:12 +00003030 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003031 return;
3032 }
3033
Chris Lattner5f9e2722011-07-23 10:55:15 +00003034 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003035 PcsAttr::PCSType PCS;
3036 if (StrRef == "aapcs")
3037 PCS = PcsAttr::AAPCS;
3038 else if (StrRef == "aapcs-vfp")
3039 PCS = PcsAttr::AAPCS_VFP;
3040 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003041 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
3042 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003043 return;
3044 }
3045
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003046 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003047 }
Abramo Bagnarae215f722010-04-30 13:10:51 +00003048 default:
3049 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003050 }
3051}
3052
Chandler Carruth1b03c872011-07-02 00:01:44 +00003053static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00003054 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003055 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003056}
3057
John McCall711c52b2011-01-05 12:14:39 +00003058bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3059 if (attr.isInvalid())
3060 return true;
3061
Ted Kremenek831efae2011-04-15 05:49:29 +00003062 if ((attr.getNumArgs() != 0 &&
3063 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
3064 attr.getParameterName()) {
John McCall711c52b2011-01-05 12:14:39 +00003065 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3066 attr.setInvalid();
3067 return true;
3068 }
3069
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003070 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3071 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003072 switch (attr.getKind()) {
3073 case AttributeList::AT_cdecl: CC = CC_C; break;
3074 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
3075 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
3076 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
3077 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003078 case AttributeList::AT_pcs: {
3079 Expr *Arg = attr.getArg(0);
3080 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003081 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003082 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3083 << "pcs" << 1;
3084 attr.setInvalid();
3085 return true;
3086 }
3087
Chris Lattner5f9e2722011-07-23 10:55:15 +00003088 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003089 if (StrRef == "aapcs") {
3090 CC = CC_AAPCS;
3091 break;
3092 } else if (StrRef == "aapcs-vfp") {
3093 CC = CC_AAPCS_VFP;
3094 break;
3095 }
3096 // FALLS THROUGH
3097 }
David Blaikie7530c032012-01-17 06:56:22 +00003098 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003099 }
3100
3101 return false;
3102}
3103
Chandler Carruth1b03c872011-07-02 00:01:44 +00003104static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003105 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003106
3107 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003108 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003109 return;
3110
Chandler Carruth87c44602011-07-01 23:49:12 +00003111 if (!isa<ObjCMethodDecl>(D)) {
3112 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3113 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003114 return;
3115 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003116
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003117 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00003118}
3119
3120/// Checks a regparm attribute, returning true if it is ill-formed and
3121/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003122bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3123 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003124 return true;
3125
Chandler Carruth87c44602011-07-01 23:49:12 +00003126 if (Attr.getNumArgs() != 1) {
3127 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3128 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003129 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003130 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003131
Chandler Carruth87c44602011-07-01 23:49:12 +00003132 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003133 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003134 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003135 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003136 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003137 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003138 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003139 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003140 }
3141
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003142 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003143 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003144 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003145 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003146 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003147 }
3148
John McCall711c52b2011-01-05 12:14:39 +00003149 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003150 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003151 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003152 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003153 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003154 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003155 }
3156
John McCall711c52b2011-01-05 12:14:39 +00003157 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003158}
3159
Chandler Carruth1b03c872011-07-02 00:01:44 +00003160static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003161 if (S.LangOpts.CUDA) {
3162 // check the attribute arguments.
3163 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003164 // FIXME: 0 is not okay.
3165 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003166 return;
3167 }
3168
Chandler Carruth87c44602011-07-01 23:49:12 +00003169 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003170 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003171 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003172 return;
3173 }
3174
3175 Expr *MaxThreadsExpr = Attr.getArg(0);
3176 llvm::APSInt MaxThreads(32);
3177 if (MaxThreadsExpr->isTypeDependent() ||
3178 MaxThreadsExpr->isValueDependent() ||
3179 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3180 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3181 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3182 return;
3183 }
3184
3185 llvm::APSInt MinBlocks(32);
3186 if (Attr.getNumArgs() > 1) {
3187 Expr *MinBlocksExpr = Attr.getArg(1);
3188 if (MinBlocksExpr->isTypeDependent() ||
3189 MinBlocksExpr->isValueDependent() ||
3190 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3191 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3192 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3193 return;
3194 }
3195 }
3196
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003197 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00003198 MaxThreads.getZExtValue(),
3199 MinBlocks.getZExtValue()));
3200 } else {
3201 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3202 }
3203}
3204
Chris Lattner0744e5f2008-06-29 00:23:49 +00003205//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00003206// Checker-specific attribute handlers.
3207//===----------------------------------------------------------------------===//
3208
John McCallc7ad3812011-01-25 03:31:58 +00003209static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003210 return type->isDependentType() ||
3211 type->isObjCObjectPointerType() ||
3212 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00003213}
3214static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003215 return type->isDependentType() ||
3216 type->isPointerType() ||
3217 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00003218}
3219
Chandler Carruth1b03c872011-07-02 00:01:44 +00003220static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003221 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00003222 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003223 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003224 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00003225 return;
3226 }
3227
3228 bool typeOK, cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003229 if (Attr.getKind() == AttributeList::AT_ns_consumed) {
John McCallc7ad3812011-01-25 03:31:58 +00003230 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3231 cf = false;
3232 } else {
3233 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3234 cf = true;
3235 }
3236
3237 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003238 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003239 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00003240 return;
3241 }
3242
3243 if (cf)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003244 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003245 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003246 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003247}
3248
Chandler Carruth1b03c872011-07-02 00:01:44 +00003249static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3250 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003251 if (!isa<ObjCMethodDecl>(D)) {
3252 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003253 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00003254 return;
3255 }
3256
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003257 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003258}
3259
Chandler Carruth1b03c872011-07-02 00:01:44 +00003260static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3261 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003262
John McCallc7ad3812011-01-25 03:31:58 +00003263 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00003264
Chandler Carruth87c44602011-07-01 23:49:12 +00003265 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003266 returnType = MD->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003267 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian831fb962011-06-25 00:17:46 +00003268 returnType = PD->getType();
David Blaikie4e4d0842012-03-11 07:00:24 +00003269 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Chandler Carruth87c44602011-07-01 23:49:12 +00003270 (Attr.getKind() == AttributeList::AT_ns_returns_retained))
John McCallf85e1932011-06-15 23:02:42 +00003271 return; // ignore: was handled as a type attribute
Chandler Carruth87c44602011-07-01 23:49:12 +00003272 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003273 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003274 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003275 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003276 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00003277 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003278 return;
3279 }
Mike Stumpbf916502009-07-24 19:02:52 +00003280
John McCallc7ad3812011-01-25 03:31:58 +00003281 bool typeOK;
3282 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003283 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00003284 default: llvm_unreachable("invalid ownership attribute");
John McCallc7ad3812011-01-25 03:31:58 +00003285 case AttributeList::AT_ns_returns_autoreleased:
3286 case AttributeList::AT_ns_returns_retained:
3287 case AttributeList::AT_ns_returns_not_retained:
3288 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3289 cf = false;
3290 break;
3291
3292 case AttributeList::AT_cf_returns_retained:
3293 case AttributeList::AT_cf_returns_not_retained:
3294 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3295 cf = true;
3296 break;
3297 }
3298
3299 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003300 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003301 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00003302 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003303 }
Mike Stumpbf916502009-07-24 19:02:52 +00003304
Chandler Carruth87c44602011-07-01 23:49:12 +00003305 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003306 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003307 llvm_unreachable("invalid ownership attribute");
John McCallc7ad3812011-01-25 03:31:58 +00003308 case AttributeList::AT_ns_returns_autoreleased:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003309 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCallc7ad3812011-01-25 03:31:58 +00003310 S.Context));
3311 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00003312 case AttributeList::AT_cf_returns_not_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003313 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003314 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003315 return;
3316 case AttributeList::AT_ns_returns_not_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003317 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003318 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003319 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003320 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003321 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003322 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003323 return;
3324 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003325 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003326 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003327 return;
3328 };
3329}
3330
John McCalldc7c5ad2011-07-22 08:53:00 +00003331static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3332 const AttributeList &attr) {
3333 SourceLocation loc = attr.getLoc();
3334
3335 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3336
3337 if (!isa<ObjCMethodDecl>(method)) {
3338 S.Diag(method->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003339 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00003340 return;
3341 }
3342
3343 // Check that the method returns a normal pointer.
3344 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00003345
3346 if (!resultType->isReferenceType() &&
3347 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00003348 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3349 << SourceRange(loc)
3350 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3351
3352 // Drop the attribute.
3353 return;
3354 }
3355
3356 method->addAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003357 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCalldc7c5ad2011-07-22 08:53:00 +00003358}
3359
John McCall8dfac0b2011-09-30 05:12:12 +00003360/// Handle cf_audited_transfer and cf_unknown_transfer.
3361static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
3362 if (!isa<FunctionDecl>(D)) {
3363 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003364 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00003365 return;
3366 }
3367
3368 bool IsAudited = (A.getKind() == AttributeList::AT_cf_audited_transfer);
3369
3370 // Check whether there's a conflicting attribute already present.
3371 Attr *Existing;
3372 if (IsAudited) {
3373 Existing = D->getAttr<CFUnknownTransferAttr>();
3374 } else {
3375 Existing = D->getAttr<CFAuditedTransferAttr>();
3376 }
3377 if (Existing) {
3378 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
3379 << A.getName()
3380 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
3381 << A.getRange() << Existing->getRange();
3382 return;
3383 }
3384
3385 // All clear; add the attribute.
3386 if (IsAudited) {
3387 D->addAttr(
3388 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
3389 } else {
3390 D->addAttr(
3391 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
3392 }
3393}
3394
John McCallfe98da02011-09-29 07:17:38 +00003395static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3396 const AttributeList &Attr) {
3397 RecordDecl *RD = dyn_cast<RecordDecl>(D);
3398 if (!RD || RD->isUnion()) {
3399 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003400 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00003401 }
3402
3403 IdentifierInfo *ParmName = Attr.getParameterName();
3404
3405 // In Objective-C, verify that the type names an Objective-C type.
3406 // We don't want to check this outside of ObjC because people sometimes
3407 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00003408 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00003409 // Check for an existing type with this name.
3410 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
3411 Sema::LookupOrdinaryName);
3412 if (S.LookupName(R, Sc)) {
3413 NamedDecl *Target = R.getFoundDecl();
3414 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3415 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3416 S.Diag(Target->getLocStart(), diag::note_declared_at);
3417 }
3418 }
3419 }
3420
3421 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
3422 ParmName));
3423}
3424
Chandler Carruth1b03c872011-07-02 00:01:44 +00003425static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3426 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003427 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00003428
Chandler Carruth87c44602011-07-01 23:49:12 +00003429 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003430 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00003431}
3432
Chandler Carruth1b03c872011-07-02 00:01:44 +00003433static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3434 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003435 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003436 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003437 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00003438 return;
3439 }
3440
Chandler Carruth87c44602011-07-01 23:49:12 +00003441 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00003442 QualType type = vd->getType();
3443
3444 if (!type->isDependentType() &&
3445 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003446 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00003447 << type;
3448 return;
3449 }
3450
3451 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3452
3453 // If we have no lifetime yet, check the lifetime we're presumably
3454 // going to infer.
3455 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3456 lifetime = type->getObjCARCImplicitLifetime();
3457
3458 switch (lifetime) {
3459 case Qualifiers::OCL_None:
3460 assert(type->isDependentType() &&
3461 "didn't infer lifetime for non-dependent type?");
3462 break;
3463
3464 case Qualifiers::OCL_Weak: // meaningful
3465 case Qualifiers::OCL_Strong: // meaningful
3466 break;
3467
3468 case Qualifiers::OCL_ExplicitNone:
3469 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00003470 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00003471 << (lifetime == Qualifiers::OCL_Autoreleasing);
3472 break;
3473 }
3474
Chandler Carruth87c44602011-07-01 23:49:12 +00003475 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003476 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00003477}
3478
Charles Davisf0122fe2010-02-16 18:27:26 +00003479static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
Aaron Ballman94287722012-02-23 22:46:33 +00003480 switch (Attr.getKind()) {
3481 default:
3482 return false;
3483 case AttributeList::AT_dllimport:
3484 case AttributeList::AT_dllexport:
3485 case AttributeList::AT_uuid:
3486 case AttributeList::AT_deprecated:
3487 case AttributeList::AT_noreturn:
3488 case AttributeList::AT_nothrow:
3489 case AttributeList::AT_naked:
3490 case AttributeList::AT_noinline:
3491 return true;
3492 }
Francois Pichet11542142010-12-19 06:50:37 +00003493}
3494
3495//===----------------------------------------------------------------------===//
3496// Microsoft specific attribute handlers.
3497//===----------------------------------------------------------------------===//
3498
Chandler Carruth1b03c872011-07-02 00:01:44 +00003499static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00003500 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Pichet11542142010-12-19 06:50:37 +00003501 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003502 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00003503 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003504
Francois Pichet11542142010-12-19 06:50:37 +00003505 Expr *Arg = Attr.getArg(0);
3506 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003507 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003508 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
3509 << "uuid" << 1;
3510 return;
3511 }
3512
Chris Lattner5f9e2722011-07-23 10:55:15 +00003513 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00003514
3515 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
3516 StrRef.back() == '}';
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003517
Francois Pichetd3d3be92010-12-20 01:41:49 +00003518 // Validate GUID length.
3519 if (IsCurly && StrRef.size() != 38) {
3520 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3521 return;
3522 }
3523 if (!IsCurly && StrRef.size() != 36) {
3524 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3525 return;
3526 }
3527
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003528 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichetd3d3be92010-12-20 01:41:49 +00003529 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00003530 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00003531 if (IsCurly) // Skip the optional '{'
3532 ++I;
3533
3534 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003535 if (i == 8 || i == 13 || i == 18 || i == 23) {
3536 if (*I != '-') {
3537 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3538 return;
3539 }
3540 } else if (!isxdigit(*I)) {
3541 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3542 return;
3543 }
3544 I++;
3545 }
Francois Pichet11542142010-12-19 06:50:37 +00003546
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003547 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00003548 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00003549 } else
Francois Pichet11542142010-12-19 06:50:37 +00003550 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00003551}
3552
Ted Kremenekb71368d2009-05-09 02:44:38 +00003553//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00003554// Top Level Sema Entry Points
3555//===----------------------------------------------------------------------===//
3556
Chandler Carruth1b03c872011-07-02 00:01:44 +00003557static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3558 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00003559 switch (Attr.getKind()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003560 case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break;
3561 case AttributeList::AT_host: handleHostAttr (S, D, Attr); break;
3562 case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003563 default:
3564 break;
3565 }
3566}
Abramo Bagnarae215f722010-04-30 13:10:51 +00003567
Chandler Carruth1b03c872011-07-02 00:01:44 +00003568static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3569 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00003570 switch (Attr.getKind()) {
Michael Hane53ac8a2012-03-07 00:12:16 +00003571 case AttributeList::AT_ibaction: handleIBAction(S, D, Attr); break;
3572 case AttributeList::AT_iboutlet: handleIBOutlet(S, D, Attr); break;
3573 case AttributeList::AT_iboutletcollection:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003574 handleIBOutletCollection(S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003575 case AttributeList::AT_address_space:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003576 case AttributeList::AT_opencl_image_access:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00003577 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00003578 case AttributeList::AT_vector_size:
Bob Wilson4211bb62010-11-16 00:32:24 +00003579 case AttributeList::AT_neon_vector_type:
3580 case AttributeList::AT_neon_polyvector_type:
Mike Stumpbf916502009-07-24 19:02:52 +00003581 // Ignore these, these are type attributes, handled by
3582 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00003583 break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003584 case AttributeList::AT_device:
3585 case AttributeList::AT_host:
3586 case AttributeList::AT_overloadable:
3587 // Ignore, this is a non-inheritable attribute, handled
3588 // by ProcessNonInheritableDeclAttr.
3589 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003590 case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break;
3591 case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003592 case AttributeList::AT_always_inline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003593 handleAlwaysInlineAttr (S, D, Attr); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00003594 case AttributeList::AT_analyzer_noreturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003595 handleAnalyzerNoReturnAttr (S, D, Attr); break;
3596 case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break;
3597 case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00003598 case AttributeList::AT_carries_dependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003599 handleDependencyAttr (S, D, Attr); break;
3600 case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break;
3601 case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break;
3602 case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break;
3603 case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break;
3604 case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003605 case AttributeList::AT_ext_vector_type:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003606 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003607 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003608 case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break;
3609 case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break;
3610 case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break;
3611 case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003612 case AttributeList::AT_launch_bounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003613 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00003614 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003615 case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break;
3616 case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break;
3617 case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break;
3618 case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break;
3619 case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003620 case AttributeList::AT_ownership_returns:
3621 case AttributeList::AT_ownership_takes:
3622 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003623 handleOwnershipAttr (S, D, Attr); break;
3624 case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break;
3625 case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break;
3626 case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break;
3627 case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break;
3628 case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003629
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +00003630 case AttributeList::AT_objc_ownership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003631 handleObjCOwnershipAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00003632 case AttributeList::AT_objc_precise_lifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003633 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00003634
John McCalldc7c5ad2011-07-22 08:53:00 +00003635 case AttributeList::AT_objc_returns_inner_pointer:
3636 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3637
John McCallfe98da02011-09-29 07:17:38 +00003638 case AttributeList::AT_ns_bridged:
3639 handleNSBridgedAttr(S, scope, D, Attr); break;
3640
John McCall8dfac0b2011-09-30 05:12:12 +00003641 case AttributeList::AT_cf_audited_transfer:
3642 case AttributeList::AT_cf_unknown_transfer:
3643 handleCFTransferAttr(S, D, Attr); break;
3644
Ted Kremenekb71368d2009-05-09 02:44:38 +00003645 // Checker-specific.
John McCallc7ad3812011-01-25 03:31:58 +00003646 case AttributeList::AT_cf_consumed:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003647 case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003648 case AttributeList::AT_ns_consumes_self:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003649 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003650
3651 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenek31c780d2010-02-18 00:05:45 +00003652 case AttributeList::AT_ns_returns_not_retained:
3653 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00003654 case AttributeList::AT_ns_returns_retained:
3655 case AttributeList::AT_cf_returns_retained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003656 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003657
Michael Hane53ac8a2012-03-07 00:12:16 +00003658 case AttributeList::AT_reqd_work_group_size:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003659 handleReqdWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00003660
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003661 case AttributeList::AT_init_priority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003662 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003663
Chandler Carruth1b03c872011-07-02 00:01:44 +00003664 case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break;
Michael Hane53ac8a2012-03-07 00:12:16 +00003665 case AttributeList::AT_ms_struct: handleMsStructAttr (S, D, Attr); break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003666 case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break;
3667 case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break;
Michael Hane53ac8a2012-03-07 00:12:16 +00003668 case AttributeList::AT_objc_arc_weak_reference_unavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00003669 handleArcWeakrefUnavailableAttr (S, D, Attr);
3670 break;
Patrick Beardb2f68202012-04-06 18:12:22 +00003671 case AttributeList::AT_objc_root_class:
3672 handleObjCRootClassAttr(S, D, Attr);
3673 break;
Ted Kremenek71207fc2012-01-05 22:47:47 +00003674 case AttributeList::AT_objc_requires_property_definitions:
3675 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00003676 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003677 case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break;
Rafael Espindolaf87cced2011-10-03 14:59:42 +00003678 case AttributeList::AT_returns_twice:
3679 handleReturnsTwiceAttr(S, D, Attr);
3680 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003681 case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break;
3682 case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break;
3683 case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00003684 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003685 case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break;
3686 case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break;
3687 case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003688 case AttributeList::AT_transparent_union:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003689 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003690 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00003691 case AttributeList::AT_objc_exception:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003692 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00003693 break;
John McCalld5313b02011-03-02 11:33:24 +00003694 case AttributeList::AT_objc_method_family:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003695 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00003696 break;
Michael Hane53ac8a2012-03-07 00:12:16 +00003697 case AttributeList::AT_NSObject: handleObjCNSObject (S, D, Attr); break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003698 case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break;
3699 case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break;
3700 case AttributeList::AT_const: handleConstAttr (S, D, Attr); break;
3701 case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break;
3702 case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break;
3703 case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break;
3704 case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break;
3705 case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003706 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00003707 // Just ignore
3708 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003709 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003710 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00003711 break;
John McCall04a67a62010-02-05 21:31:56 +00003712 case AttributeList::AT_stdcall:
3713 case AttributeList::AT_cdecl:
3714 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003715 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003716 case AttributeList::AT_pascal:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003717 case AttributeList::AT_pcs:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003718 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00003719 break;
Peter Collingbournef315fa82011-02-14 01:42:53 +00003720 case AttributeList::AT_opencl_kernel_function:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003721 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00003722 break;
Francois Pichet11542142010-12-19 06:50:37 +00003723 case AttributeList::AT_uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003724 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00003725 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003726
3727 // Thread safety attributes:
3728 case AttributeList::AT_guarded_var:
3729 handleGuardedVarAttr(S, D, Attr);
3730 break;
3731 case AttributeList::AT_pt_guarded_var:
3732 handleGuardedVarAttr(S, D, Attr, /*pointer = */true);
3733 break;
3734 case AttributeList::AT_scoped_lockable:
3735 handleLockableAttr(S, D, Attr, /*scoped = */true);
3736 break;
Kostya Serebryany71efba02012-01-24 19:25:38 +00003737 case AttributeList::AT_no_address_safety_analysis:
3738 handleNoAddressSafetyAttr(S, D, Attr);
3739 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003740 case AttributeList::AT_no_thread_safety_analysis:
3741 handleNoThreadSafetyAttr(S, D, Attr);
3742 break;
3743 case AttributeList::AT_lockable:
3744 handleLockableAttr(S, D, Attr);
3745 break;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00003746 case AttributeList::AT_guarded_by:
3747 handleGuardedByAttr(S, D, Attr);
3748 break;
3749 case AttributeList::AT_pt_guarded_by:
3750 handleGuardedByAttr(S, D, Attr, /*pointer = */true);
3751 break;
3752 case AttributeList::AT_exclusive_lock_function:
3753 handleLockFunAttr(S, D, Attr, /*exclusive = */true);
3754 break;
3755 case AttributeList::AT_exclusive_locks_required:
3756 handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true);
3757 break;
3758 case AttributeList::AT_exclusive_trylock_function:
3759 handleTrylockFunAttr(S, D, Attr, /*exclusive = */true);
3760 break;
3761 case AttributeList::AT_lock_returned:
3762 handleLockReturnedAttr(S, D, Attr);
3763 break;
3764 case AttributeList::AT_locks_excluded:
3765 handleLocksExcludedAttr(S, D, Attr);
3766 break;
3767 case AttributeList::AT_shared_lock_function:
3768 handleLockFunAttr(S, D, Attr);
3769 break;
3770 case AttributeList::AT_shared_locks_required:
3771 handleLocksRequiredAttr(S, D, Attr);
3772 break;
3773 case AttributeList::AT_shared_trylock_function:
3774 handleTrylockFunAttr(S, D, Attr);
3775 break;
3776 case AttributeList::AT_unlock_function:
3777 handleUnlockFunAttr(S, D, Attr);
3778 break;
3779 case AttributeList::AT_acquired_before:
3780 handleAcquireOrderAttr(S, D, Attr, /*before = */true);
3781 break;
3782 case AttributeList::AT_acquired_after:
3783 handleAcquireOrderAttr(S, D, Attr, /*before = */false);
3784 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003785
Chris Lattner803d0802008-06-29 00:43:07 +00003786 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003787 // Ask target about the attribute.
3788 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
3789 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00003790 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
3791 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00003792 break;
3793 }
3794}
3795
Peter Collingbourne60700392011-01-21 02:08:45 +00003796/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3797/// the attribute applies to decls. If the attribute is a type attribute, just
3798/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
3799/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruth1b03c872011-07-02 00:01:44 +00003800static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3801 const AttributeList &Attr,
Peter Collingbourne60700392011-01-21 02:08:45 +00003802 bool NonInheritable, bool Inheritable) {
3803 if (Attr.isInvalid())
3804 return;
3805
3806 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
3807 // FIXME: Try to deal with other __declspec attributes!
3808 return;
3809
3810 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003811 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003812
3813 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003814 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003815}
3816
Chris Lattner803d0802008-06-29 00:43:07 +00003817/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
3818/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00003819void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00003820 const AttributeList *AttrList,
3821 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003822 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003823 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003824 }
3825
3826 // GCC accepts
3827 // static int a9 __attribute__((weakref));
3828 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00003829 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003830 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003831 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003832 return;
Chris Lattner803d0802008-06-29 00:43:07 +00003833 }
3834}
3835
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003836// Annotation attributes are the only attributes allowed after an access
3837// specifier.
3838bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
3839 const AttributeList *AttrList) {
3840 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
3841 if (l->getKind() == AttributeList::AT_annotate) {
3842 handleAnnotateAttr(*this, ASDecl, *l);
3843 } else {
3844 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
3845 return true;
3846 }
3847 }
3848
3849 return false;
3850}
3851
John McCalle82247a2011-10-01 05:17:03 +00003852/// checkUnusedDeclAttributes - Check a list of attributes to see if it
3853/// contains any decl attributes that we should warn about.
3854static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
3855 for ( ; A; A = A->getNext()) {
3856 // Only warn if the attribute is an unignored, non-type attribute.
3857 if (A->isUsedAsTypeAttr()) continue;
3858 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
3859
3860 if (A->getKind() == AttributeList::UnknownAttribute) {
3861 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
3862 << A->getName() << A->getRange();
3863 } else {
3864 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
3865 << A->getName() << A->getRange();
3866 }
3867 }
3868}
3869
3870/// checkUnusedDeclAttributes - Given a declarator which is not being
3871/// used to build a declaration, complain about any decl attributes
3872/// which might be lying around on it.
3873void Sema::checkUnusedDeclAttributes(Declarator &D) {
3874 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
3875 ::checkUnusedDeclAttributes(*this, D.getAttributes());
3876 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
3877 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
3878}
3879
Ryan Flynne25ff832009-07-30 03:15:39 +00003880/// DeclClonePragmaWeak - clone existing decl (maybe definition),
3881/// #pragma weak needs a non-definition decl and source may not have one
Eli Friedman900693b2011-09-07 04:05:06 +00003882NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
3883 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003884 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00003885 NamedDecl *NewD = 0;
3886 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00003887 FunctionDecl *NewFD;
3888 // FIXME: Missing call to CheckFunctionDeclaration().
3889 // FIXME: Mangling?
3890 // FIXME: Is the qualifier info correct?
3891 // FIXME: Is the DeclContext correct?
3892 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
3893 Loc, Loc, DeclarationName(II),
3894 FD->getType(), FD->getTypeSourceInfo(),
3895 SC_None, SC_None,
3896 false/*isInlineSpecified*/,
3897 FD->hasPrototype(),
3898 false/*isConstexprSpecified*/);
3899 NewD = NewFD;
3900
3901 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003902 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00003903
3904 // Fake up parameter variables; they are declared as if this were
3905 // a typedef.
3906 QualType FDTy = FD->getType();
3907 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
3908 SmallVector<ParmVarDecl*, 16> Params;
3909 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
3910 AE = FT->arg_type_end(); AI != AE; ++AI) {
3911 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
3912 Param->setScopeInfo(0, Params.size());
3913 Params.push_back(Param);
3914 }
David Blaikie4278c652011-09-21 18:16:56 +00003915 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00003916 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003917 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
3918 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003919 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00003920 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00003921 VD->getStorageClass(),
3922 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00003923 if (VD->getQualifier()) {
3924 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003925 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00003926 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003927 }
3928 return NewD;
3929}
3930
3931/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
3932/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003933void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003934 if (W.getUsed()) return; // only do this once
3935 W.setUsed(true);
3936 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
3937 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00003938 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00003939 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
3940 NDId->getName()));
3941 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003942 WeakTopLevelDecl.push_back(NewD);
3943 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
3944 // to insert Decl at TU scope, sorry.
3945 DeclContext *SavedContext = CurContext;
3946 CurContext = Context.getTranslationUnitDecl();
3947 PushOnScopeChains(NewD, S);
3948 CurContext = SavedContext;
3949 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00003950 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00003951 }
3952}
3953
Chris Lattner0744e5f2008-06-29 00:23:49 +00003954/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
3955/// it, apply them to D. This is a bit tricky because PD can have attributes
3956/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00003957void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
3958 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00003959 // It's valid to "forward-declare" #pragma weak, in which case we
3960 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00003961 if (Inheritable) {
3962 LoadExternalWeakUndeclaredIdentifiers();
3963 if (!WeakUndeclaredIdentifiers.empty()) {
3964 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
3965 if (IdentifierInfo *Id = ND->getIdentifier()) {
3966 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
3967 = WeakUndeclaredIdentifiers.find(Id);
3968 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
3969 WeakInfo W = I->second;
3970 DeclApplyPragmaWeak(S, ND, W);
3971 WeakUndeclaredIdentifiers[Id] = W;
3972 }
John McCalld4aff0e2010-10-27 00:59:00 +00003973 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003974 }
3975 }
3976 }
3977
Chris Lattner0744e5f2008-06-29 00:23:49 +00003978 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00003979 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00003980 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003981
Chris Lattner0744e5f2008-06-29 00:23:49 +00003982 // Walk the declarator structure, applying decl attributes that were in a type
3983 // position to the decl itself. This handles cases like:
3984 // int *__attr__(x)** D;
3985 // when X is a decl attribute.
3986 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
3987 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00003988 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003989
Chris Lattner0744e5f2008-06-29 00:23:49 +00003990 // Finally, apply any attributes on the decl itself.
3991 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00003992 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00003993}
John McCall54abf7d2009-11-04 02:18:39 +00003994
John McCallf85e1932011-06-15 23:02:42 +00003995/// Is the given declaration allowed to use a forbidden type?
3996static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
3997 // Private ivars are always okay. Unfortunately, people don't
3998 // always properly make their ivars private, even in system headers.
3999 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00004000 // Function declarations in sys headers will be marked unavailable.
4001 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4002 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00004003 return false;
4004
4005 // Require it to be declared in a system header.
4006 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4007}
4008
4009/// Handle a delayed forbidden-type diagnostic.
4010static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4011 Decl *decl) {
4012 if (decl && isForbiddenTypeAllowed(S, decl)) {
4013 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4014 "this system declaration uses an unsupported type"));
4015 return;
4016 }
David Blaikie4e4d0842012-03-11 07:00:24 +00004017 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004018 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
4019 // FIXME. we may want to supress diagnostics for all
4020 // kind of forbidden type messages on unavailable functions.
4021 if (FD->hasAttr<UnavailableAttr>() &&
4022 diag.getForbiddenTypeDiagnostic() ==
4023 diag::err_arc_array_param_no_ownership) {
4024 diag.Triggered = true;
4025 return;
4026 }
4027 }
John McCallf85e1932011-06-15 23:02:42 +00004028
4029 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4030 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4031 diag.Triggered = true;
4032}
4033
John McCalleee1d542011-02-14 07:13:47 +00004034// This duplicates a vector push_back but hides the need to know the
4035// size of the type.
4036void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
4037 assert(StackSize <= StackCapacity);
4038
4039 // Grow the stack if necessary.
4040 if (StackSize == StackCapacity) {
4041 unsigned newCapacity = 2 * StackCapacity + 2;
4042 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
4043 const char *oldBuffer = (const char*) Stack;
4044
4045 if (StackCapacity)
4046 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
4047
4048 delete[] oldBuffer;
4049 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
4050 StackCapacity = newCapacity;
4051 }
4052
4053 assert(StackSize < StackCapacity);
4054 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall2f514482010-01-27 03:50:35 +00004055}
4056
John McCalleee1d542011-02-14 07:13:47 +00004057void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
4058 Decl *decl) {
4059 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall2f514482010-01-27 03:50:35 +00004060
John McCalleee1d542011-02-14 07:13:47 +00004061 // Check the invariants.
4062 assert(DD.StackSize >= state.SavedStackSize);
4063 assert(state.SavedStackSize >= DD.ActiveStackBase);
4064 assert(DD.ParsingDepth > 0);
4065
4066 // Drop the parsing depth.
4067 DD.ParsingDepth--;
4068
4069 // If there are no active diagnostics, we're done.
4070 if (DD.StackSize == DD.ActiveStackBase)
John McCall2f514482010-01-27 03:50:35 +00004071 return;
4072
John McCall2f514482010-01-27 03:50:35 +00004073 // We only want to actually emit delayed diagnostics when we
4074 // successfully parsed a decl.
John McCalle8c904f2012-01-26 20:04:03 +00004075 if (decl) {
John McCalleee1d542011-02-14 07:13:47 +00004076 // We emit all the active diagnostics, not just those starting
4077 // from the saved state. The idea is this: we get one push for a
John McCall2f514482010-01-27 03:50:35 +00004078 // decl spec and another for each declarator; in a decl group like:
4079 // deprecated_typedef foo, *bar, baz();
4080 // only the declarator pops will be passed decls. This is correct;
4081 // we really do need to consider delayed diagnostics from the decl spec
4082 // for each of the different declarations.
John McCalleee1d542011-02-14 07:13:47 +00004083 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
4084 DelayedDiagnostic &diag = DD.Stack[i];
4085 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00004086 continue;
4087
John McCalleee1d542011-02-14 07:13:47 +00004088 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00004089 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00004090 // Don't bother giving deprecation diagnostics if the decl is invalid.
4091 if (!decl->isInvalidDecl())
4092 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004093 break;
4094
4095 case DelayedDiagnostic::Access:
John McCalleee1d542011-02-14 07:13:47 +00004096 S.HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004097 break;
John McCallf85e1932011-06-15 23:02:42 +00004098
4099 case DelayedDiagnostic::ForbiddenType:
4100 handleDelayedForbiddenType(S, diag, decl);
4101 break;
John McCall2f514482010-01-27 03:50:35 +00004102 }
4103 }
4104 }
4105
John McCall58e6f342010-03-16 05:22:47 +00004106 // Destroy all the delayed diagnostics we're about to pop off.
John McCalleee1d542011-02-14 07:13:47 +00004107 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
Douglas Gregor29233802011-03-23 15:13:44 +00004108 DD.Stack[i].Destroy();
John McCall58e6f342010-03-16 05:22:47 +00004109
John McCalleee1d542011-02-14 07:13:47 +00004110 DD.StackSize = state.SavedStackSize;
John McCall54abf7d2009-11-04 02:18:39 +00004111}
4112
4113static bool isDeclDeprecated(Decl *D) {
4114 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004115 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00004116 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00004117 // A category implicitly has the availability of the interface.
4118 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4119 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00004120 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4121 return false;
4122}
4123
John McCall9c3087b2010-08-26 02:13:20 +00004124void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00004125 Decl *Ctx) {
4126 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00004127 return;
4128
John McCall2f514482010-01-27 03:50:35 +00004129 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004130 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004131 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004132 << DD.getDeprecationDecl()->getDeclName()
4133 << DD.getDeprecationMessage();
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004134 else if (DD.getUnknownObjCClass()) {
4135 Diag(DD.Loc, diag::warn_deprecated_fwdclass_message)
4136 << DD.getDeprecationDecl()->getDeclName();
4137 Diag(DD.getUnknownObjCClass()->getLocation(), diag::note_forward_class);
4138 }
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004139 else
4140 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004141 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00004142}
4143
Chris Lattner5f9e2722011-07-23 10:55:15 +00004144void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004145 SourceLocation Loc,
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004146 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00004147 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00004148 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004149 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4150 UnknownObjCClass,
4151 Message));
John McCall54abf7d2009-11-04 02:18:39 +00004152 return;
4153 }
4154
4155 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00004156 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00004157 return;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004158 if (!Message.empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004159 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
4160 << Message;
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004161 else {
Peter Collingbourne743b82b2011-01-02 19:53:12 +00004162 if (!UnknownObjCClass)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004163 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004164 else {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004165 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004166 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4167 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004168 }
John McCall54abf7d2009-11-04 02:18:39 +00004169}